Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/curve25519/internal.h

raw
1 // Copyright (c) 2020, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #ifndef OPENSSL_HEADER_CURVE25519_INTERNAL_H
5 #define OPENSSL_HEADER_CURVE25519_INTERNAL_H
6
7 #if defined(__cplusplus)
8 extern "C" {
9 #endif
10
11 #include <openssl/base.h>
12 #include <openssl/curve25519.h>
13
14 #include "../../internal.h"
15
16 typedef enum {
17 ED25519_ALG,
18 ED25519CTX_ALG,
19 ED25519PH_ALG,
20 } ed25519_algorithm_t;
21
22 #define DOM2_PREFIX_SIZE 32
23 #define DOM2_F_SIZE 1
24 #define DOM2_C_SIZE 1
25 #define DOM2_F_OFFSET DOM2_PREFIX_SIZE
26 #define DOM2_C_OFFSET (DOM2_F_OFFSET + DOM2_F_SIZE)
27 #define DOM2_CONTEXT_OFFSET (DOM2_C_OFFSET + DOM2_C_SIZE)
28 #define MAX_DOM2_CONTEXT_SIZE 255
29 #define MAX_DOM2_SIZE \
30 (DOM2_PREFIX_SIZE + DOM2_F_SIZE + DOM2_C_SIZE + MAX_DOM2_CONTEXT_SIZE)
31
32 int ED25519_keypair_internal(
33 uint8_t out_public_key[ED25519_PUBLIC_KEY_LEN],
34 uint8_t out_private_key[ED25519_PRIVATE_KEY_LEN]);
35
36 int ed25519_sign_internal(
37 ed25519_algorithm_t alg,
38 uint8_t out_sig[ED25519_SIGNATURE_LEN],
39 const uint8_t *message, size_t message_len,
40 const uint8_t private_key[ED25519_PRIVATE_KEY_LEN],
41 const uint8_t *ctx, size_t ctx_len);
42
43 int ed25519_verify_internal(
44 ed25519_algorithm_t alg,
45 const uint8_t *message, size_t message_len,
46 const uint8_t signature[ED25519_SIGNATURE_LEN],
47 const uint8_t public_key[ED25519_PUBLIC_KEY_LEN],
48 const uint8_t *ctx, size_t ctx_len);
49
50 int ED25519_sign_no_self_test(
51 uint8_t out_sig[ED25519_SIGNATURE_LEN],
52 const uint8_t *message, size_t message_len,
53 const uint8_t private_key[ED25519_PRIVATE_KEY_LEN]);
54
55 int ED25519_verify_no_self_test(
56 const uint8_t *message, size_t message_len,
57 const uint8_t signature[ED25519_SIGNATURE_LEN],
58 const uint8_t public_key[ED25519_PUBLIC_KEY_LEN]);
59
60 int ED25519ctx_sign_no_self_test(
61 uint8_t out_sig[ED25519_SIGNATURE_LEN],
62 const uint8_t *message, size_t message_len,
63 const uint8_t private_key[ED25519_PRIVATE_KEY_LEN],
64 const uint8_t *context, size_t context_len);
65
66 int ED25519ctx_verify_no_self_test(
67 const uint8_t *message, size_t message_len,
68 const uint8_t signature[ED25519_SIGNATURE_LEN],
69 const uint8_t public_key[ED25519_PUBLIC_KEY_LEN],
70 const uint8_t *context, size_t context_len);
71
72 int ED25519ph_sign_no_self_test(
73 uint8_t out_sig[ED25519_SIGNATURE_LEN],
74 const uint8_t *message, size_t message_len,
75 const uint8_t private_key[ED25519_PRIVATE_KEY_LEN],
76 const uint8_t *context, size_t context_len);
77
78 int ED25519ph_sign_digest_no_self_test(
79 uint8_t out_sig[ED25519_SIGNATURE_LEN],
80 const uint8_t digest[SHA512_DIGEST_LENGTH],
81 const uint8_t private_key[ED25519_PRIVATE_KEY_LEN],
82 const uint8_t *context, size_t context_len);
83
84 int ED25519ph_verify_no_self_test(
85 const uint8_t *message, size_t message_len,
86 const uint8_t signature[ED25519_SIGNATURE_LEN],
87 const uint8_t public_key[ED25519_PUBLIC_KEY_LEN],
88 const uint8_t *context, size_t context_len);
89
90 int ED25519ph_verify_digest_no_self_test(
91 const uint8_t digest[SHA512_DIGEST_LENGTH],
92 const uint8_t signature[ED25519_SIGNATURE_LEN],
93 const uint8_t public_key[ED25519_PUBLIC_KEY_LEN],
94 const uint8_t *context, size_t context_len);
95
96 // If (1) x86_64 or aarch64, (2) linux or apple, and (3) OPENSSL_NO_ASM is not
97 // set, s2n-bignum path is capable.
98 #if ((defined(OPENSSL_X86_64) && !defined(MY_ASSEMBLER_IS_TOO_OLD_FOR_512AVX)) || \
99 defined(OPENSSL_AARCH64)) && \
100 (defined(OPENSSL_LINUX) || defined(OPENSSL_APPLE) || \
101 defined(OPENSSL_OPENBSD) || defined(OPENSSL_FREEBSD) || \
102 defined(OPENSSL_NETBSD)) && \
103 !defined(OPENSSL_NO_ASM)
104 #define CURVE25519_S2N_BIGNUM_CAPABLE
105 #endif
106
107 #if defined(BORINGSSL_HAS_UINT128)
108 #define BORINGSSL_CURVE25519_64BIT
109 #endif
110
111 #if defined(BORINGSSL_CURVE25519_64BIT)
112 // fe means field element. Here the field is \Z/(2^255-19). An element t,
113 // entries t[0]...t[4], represents the integer t[0]+2^51 t[1]+2^102 t[2]+2^153
114 // t[3]+2^204 t[4].
115 // fe limbs are bounded by 1.125*2^51.
116 // Multiplication and carrying produce fe from fe_loose.
117 typedef struct fe { uint64_t v[5]; } fe;
118
119 // fe_loose limbs are bounded by 3.375*2^51.
120 // Addition and subtraction produce fe_loose from (fe, fe).
121 typedef struct fe_loose { uint64_t v[5]; } fe_loose;
122 #else
123 // fe means field element. Here the field is \Z/(2^255-19). An element t,
124 // entries t[0]...t[9], represents the integer t[0]+2^26 t[1]+2^51 t[2]+2^77
125 // t[3]+2^102 t[4]+...+2^230 t[9].
126 // fe limbs are bounded by 1.125*2^26,1.125*2^25,1.125*2^26,1.125*2^25,etc.
127 // Multiplication and carrying produce fe from fe_loose.
128 typedef struct fe { uint32_t v[10]; } fe;
129
130 // fe_loose limbs are bounded by 3.375*2^26,3.375*2^25,3.375*2^26,3.375*2^25,etc.
131 // Addition and subtraction produce fe_loose from (fe, fe).
132 typedef struct fe_loose { uint32_t v[10]; } fe_loose;
133 #endif
134
135 // ge means group element.
136 //
137 // Here the group is the set of pairs (x,y) of field elements (see fe.h)
138 // satisfying -x^2 + y^2 = 1 + d x^2y^2
139 // where d = -121665/121666.
140 //
141 // Representations:
142 // ge_p2 (projective): (X:Y:Z) satisfying x=X/Z, y=Y/Z
143 // ge_p3 (extended): (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT
144 // ge_p1p1 (completed): ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T
145 // ge_precomp (Duif): (y+x,y-x,2dxy)
146
147 typedef struct {
148 fe X;
149 fe Y;
150 fe Z;
151 } ge_p2;
152
153 typedef struct {
154 fe X;
155 fe Y;
156 fe Z;
157 fe T;
158 } ge_p3;
159
160 typedef struct {
161 fe_loose X;
162 fe_loose Y;
163 fe_loose Z;
164 fe_loose T;
165 } ge_p1p1;
166
167 typedef struct {
168 fe_loose yplusx;
169 fe_loose yminusx;
170 fe_loose xy2d;
171 } ge_precomp;
172
173 typedef struct {
174 fe_loose YplusX;
175 fe_loose YminusX;
176 fe_loose Z;
177 fe_loose T2d;
178 } ge_cached;
179
180 void x25519_ge_tobytes(uint8_t s[32], const ge_p2 *h);
181 int x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t s[32]);
182 void x25519_ge_p3_to_cached(ge_cached *r, const ge_p3 *p);
183 void x25519_ge_p1p1_to_p2(ge_p2 *r, const ge_p1p1 *p);
184 void x25519_ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p);
185 void x25519_ge_add(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q);
186 void x25519_ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q);
187 void x25519_ge_scalarmult_small_precomp(
188 ge_p3 *h, const uint8_t a[32], const uint8_t precomp_table[15 * 2 * 32]);
189 void x25519_ge_scalarmult_base(ge_p3 *h, const uint8_t a[32]);
190 void x25519_ge_scalarmult(ge_p2 *r, const uint8_t *scalar, const ge_p3 *A);
191 void x25519_sc_reduce(uint8_t s[64]);
192
193 // x25519_scalar_mult_generic_[s2n_bignum,nohw] computes the x25519 function
194 // from rfc7748 6.1 using the peer coordinate (either K_A or K_B) encoded in
195 // |peer_public_value| and the scalar is |private_key|. The resulting shared key
196 // is returned in |out_shared_key|.
197 void x25519_scalar_mult_generic_s2n_bignum(
198 uint8_t out_shared_key[X25519_SHARED_KEY_LEN],
199 const uint8_t private_key[X25519_PRIVATE_KEY_LEN],
200 const uint8_t peer_public_value[X25519_PUBLIC_VALUE_LEN]);
201 void x25519_scalar_mult_generic_nohw(
202 uint8_t out_shared_key[X25519_SHARED_KEY_LEN],
203 const uint8_t private_key[X25519_PRIVATE_KEY_LEN],
204 const uint8_t peer_public_value[X25519_PUBLIC_VALUE_LEN]);
205
206 // x25519_public_from_private_[s2n_bignum,nohw] computes the x25519 function
207 // from rfc7748 6.1 using the base-coordinate 9 and scalar |private_key|. The
208 // resulting (encoded) public key coordinate (either K_A or K_B) is returned in
209 // |out_public_value|.
210 void x25519_public_from_private_s2n_bignum(
211 uint8_t out_public_value[X25519_PUBLIC_VALUE_LEN],
212 const uint8_t private_key[X25519_PRIVATE_KEY_LEN]);
213 void x25519_public_from_private_nohw(
214 uint8_t out_public_value[X25519_PUBLIC_VALUE_LEN],
215 const uint8_t private_key[X25519_PRIVATE_KEY_LEN]);
216
217 // ed25519_public_key_from_hashed_seed_[s2n_bignum,nohw] handles steps
218 // rfc8032 5.1.5.[3,4]. Computes [az]B and encodes the public key to a 32-byte
219 // octet string returning it in |out_public_key|.
220 void ed25519_public_key_from_hashed_seed_s2n_bignum(
221 uint8_t out_public_key[ED25519_PUBLIC_KEY_LEN],
222 uint8_t az[SHA512_DIGEST_LENGTH]);
223 void ed25519_public_key_from_hashed_seed_nohw(
224 uint8_t out_public_key[ED25519_PUBLIC_KEY_LEN],
225 uint8_t az[SHA512_DIGEST_LENGTH]);
226
227 // ed25519_sign_[s2n_bignum,nohw] handles steps rfc8032 5.1.6.[3,5,6,7].
228 // Computes the signature S = r + k * s modulo the order of the base-point B.
229 // Returns R || S in |out_sig|. |s| must have length
230 // |ED25519_PRIVATE_KEY_SEED_LEN| and |A| must have length
231 // |ED25519_PUBLIC_KEY_LEN|.
232 void ed25519_sign_s2n_bignum(uint8_t out_sig[ED25519_SIGNATURE_LEN],
233 uint8_t r[SHA512_DIGEST_LENGTH], const uint8_t *s,
234 const uint8_t *A,
235 const void *message, size_t message_len,
236 const uint8_t *dom2, size_t dom2_len);
237 void ed25519_sign_nohw(uint8_t out_sig[ED25519_SIGNATURE_LEN],
238 uint8_t r[SHA512_DIGEST_LENGTH], const uint8_t *s,
239 const uint8_t *A,
240 const void *message, size_t message_len,
241 const uint8_t *dom2, size_t dom2_len);
242
243 // ed25519_verify_[s2n_bignum,nohw] handles steps rfc8032 5.1.7.[1,2,3].
244 // Computes [S]B - [k]A' and returns the result in |R_computed_encoded|. Returns
245 // 1 on success and 0 otherwise. The failure case occurs if decoding of the
246 // public key |public_key| fails.
247 int ed25519_verify_s2n_bignum(uint8_t R_computed_encoded[32],
248 const uint8_t public_key[ED25519_PUBLIC_KEY_LEN],
249 uint8_t R_expected[32], uint8_t S[32],
250 const uint8_t *message, size_t message_len,
251 const uint8_t *dom2, size_t dom2_len);
252 int ed25519_verify_nohw(uint8_t R_computed_encoded[32],
253 const uint8_t public_key[ED25519_PUBLIC_KEY_LEN],
254 uint8_t R_expected[32], uint8_t S[32],
255 const uint8_t *message, size_t message_len,
256 const uint8_t *dom2, size_t dom2_len);
257
258 // Computes the SHA512 function of up to four input pairs: (|input1|, |len1|),
259 // (|input2|, |len2|), (|input3|, |len3|), (|input4|, |len4|). Specifically, the
260 // hash is computed over the concatenation: |input1| || |input2| || |input3| ||
261 // |input4|. The final two pairs may have |len3| == 0 or |len4| == 0, meaning
262 // those input values will be ignored. The result is written to |out|.
263 void ed25519_sha512(uint8_t out[SHA512_DIGEST_LENGTH],
264 const void *input1, size_t len1, const void *input2, size_t len2,
265 const void *input3, size_t len3, const void *input4, size_t len4);
266
267
268 int ed25519_check_public_key_s2n_bignum(const uint8_t public_key[ED25519_PUBLIC_KEY_LEN]);
269 int ed25519_check_public_key_nohw(const uint8_t public_key[ED25519_PUBLIC_KEY_LEN]);
270 OPENSSL_EXPORT int ED25519_check_public_key(const uint8_t public_key[ED25519_PUBLIC_KEY_LEN]);
271
272 #if defined(__cplusplus)
273 } // extern C
274 #endif
275
276 #endif // OPENSSL_HEADER_CURVE25519_INTERNAL_H