Debian Codegraph @

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

raw
1 // Copyright (c) 2020, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 // Some of this code is taken from the ref10 version of Ed25519 in SUPERCOP
5 // 20141124 (http://bench.cr.yp.to/supercop.html). That code is released as
6 // public domain. Other parts have been replaced to call into code generated by
7 // Fiat (https://github.com/mit-plv/fiat-crypto) in //third_party/fiat.
8 //
9 // The field functions are shared by Ed25519 and X25519 where possible.
10
11 #include <openssl/curve25519.h>
12
13 #include <string.h>
14
15 #include <openssl/err.h>
16 #include <openssl/mem.h>
17 #include <openssl/rand.h>
18 #include <openssl/sha.h>
19
20 #include "../../internal.h"
21 #include "../cpucap/internal.h"
22 #include "internal.h"
23
24 const uint8_t RFC8032_DOM2_PREFIX[DOM2_PREFIX_SIZE] = {
25 'S', 'i', 'g', 'E', 'd', '2', '5', '5', '1', '9', ' ',
26 'n', 'o', ' ', 'E', 'd', '2', '5', '5', '1', '9', ' ',
27 'c', 'o', 'l', 'l', 'i', 's', 'i', 'o', 'n', 's'};
28
29 // X25519 [1] and Ed25519 [2] is an ECDHE protocol and signature scheme,
30 // respectively. This file contains an implementation of both using two
31 // different backends:
32 // 1) One backend is a pure C backend that should work on any platform.
33 // 2) The other backend is machine-optimized using s2n-bignum [3] as backend.
34 //
35 // [1]: https://datatracker.ietf.org/doc/html/rfc7748
36 // [2]: https://datatracker.ietf.org/doc/html/rfc8032
37 // [3]: https://github.com/awslabs/s2n-bignum
38 //
39 // "Clamping":
40 // Both X25519 and Ed25519 contain "clamping" steps; bit-twiddling, masking or
41 // setting specific bits. Generally, the bit-twiddling is to avoid common
42 // implementation errors and weak instances. Details can be found through the
43 // following two references:
44 // * https://mailarchive.ietf.org/arch/msg/cfrg/pt2bt3fGQbNF8qdEcorp-rJSJrc/
45 // * https://neilmadden.blog/2020/05/28/whats-the-curve25519-clamping-all-about
46 //
47 // Ed25519 domain and pre-hash functions:
48 // For Ed25519, dom2(F,C) is the empty string and PH the identify function,
49 // cf. rfc8032 5.1.
50
51 void ed25519_sha512(uint8_t out[SHA512_DIGEST_LENGTH], const void *input1,
52 size_t len1, const void *input2, size_t len2,
53 const void *input3, size_t len3, const void *input4,
54 size_t len4) {
55 SHA512_CTX hash_ctx;
56 SHA512_Init(&hash_ctx);
57 SHA512_Update(&hash_ctx, input1, len1);
58 SHA512_Update(&hash_ctx, input2, len2);
59 if (len3 != 0) {
60 SHA512_Update(&hash_ctx, input3, len3);
61 }
62 if (len4 != 0) {
63 SHA512_Update(&hash_ctx, input4, len4);
64 }
65 SHA512_Final(out, &hash_ctx);
66 }
67
68 // Public interface functions
69
70 void ED25519_keypair_from_seed(uint8_t out_public_key[ED25519_PUBLIC_KEY_LEN],
71 uint8_t out_private_key[ED25519_PRIVATE_KEY_LEN],
72 const uint8_t seed[ED25519_SEED_LEN]) {
73 // ED25519_keypair already ensures this with the same check, and is also the
74 // function that is approved for FIPS (sets the indicator). Ensuring it here
75 // for brevity.
76 boringssl_ensure_eddsa_self_test();
77
78 // Step: rfc8032 5.1.5.1
79 // Compute SHA512(seed).
80 uint8_t az[SHA512_DIGEST_LENGTH];
81 SHA512(seed, ED25519_SEED_LEN, az);
82
83 // Step: rfc8032 5.1.5.2
84 az[0] &= 248; // 11111000_2
85 az[31] &= 127; // 01111111_2
86 az[31] |= 64; // 01000000_2
87
88 // Step: rfc8032 5.1.5.[3,4]
89 // Compute [az]B and encode public key to a 32 byte octet.
90 #if defined(CURVE25519_S2N_BIGNUM_CAPABLE)
91 ed25519_public_key_from_hashed_seed_s2n_bignum(out_public_key, az);
92 #else
93 ed25519_public_key_from_hashed_seed_nohw(out_public_key, az);
94 #endif
95
96 // Encoded public key is a suffix in the private key. Avoids having to
97 // generate the public key from the private key when signing.
98 OPENSSL_STATIC_ASSERT(ED25519_PRIVATE_KEY_LEN == (ED25519_SEED_LEN + ED25519_PUBLIC_KEY_LEN), ed25519_parameter_length_mismatch)
99 OPENSSL_memcpy(out_private_key, seed, ED25519_SEED_LEN);
100 OPENSSL_memcpy(out_private_key + ED25519_SEED_LEN, out_public_key,
101 ED25519_PUBLIC_KEY_LEN);
102
103 OPENSSL_cleanse(az, sizeof(az));
104 }
105
106 static int ed25519_keypair_pct(uint8_t public_key[ED25519_PUBLIC_KEY_LEN],
107 uint8_t private_key[ED25519_PRIVATE_KEY_LEN]) {
108 #if defined(AWSLC_FIPS)
109 uint8_t msg[16] = {16};
110 uint8_t out_sig[ED25519_SIGNATURE_LEN];
111 if (ED25519_sign_no_self_test(out_sig, msg, 16, private_key) != 1) {
112 // This should never happen and static analysis will say that ED25519_sign_no_self_test
113 // always returns 1
114 AWS_LC_FIPS_failure("Ed25519 keygen PCT failed");
115 return 0;
116 }
117 if (boringssl_fips_break_test("EDDSA_PWCT")) {
118 msg[0] = ~msg[0];
119 }
120 if (ED25519_verify_no_self_test(msg, 16, out_sig, public_key) != 1) {
121 AWS_LC_FIPS_failure("Ed25519 keygen PCT failed");
122 return 0;
123 }
124 #endif
125 return 1;
126 }
127
128 int ED25519_keypair_internal(uint8_t out_public_key[ED25519_PUBLIC_KEY_LEN],
129 uint8_t out_private_key[ED25519_PRIVATE_KEY_LEN]) {
130 // We have to avoid the self tests and digest function in ed25519_keypair_pct
131 // from updating the service indicator.
132 FIPS_service_indicator_lock_state();
133 boringssl_ensure_eddsa_self_test();
134 SET_DIT_AUTO_RESET;
135
136 // Ed25519 key generation: rfc8032 5.1.5
137 // Private key is 32 octets of random data.
138 uint8_t seed[ED25519_SEED_LEN];
139 AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(seed, ED25519_SEED_LEN));
140
141 // Public key generation is handled in a separate function. See function
142 // description why this is useful.
143 ED25519_keypair_from_seed(out_public_key, out_private_key, seed);
144 OPENSSL_cleanse(seed, ED25519_SEED_LEN);
145
146 int result = ed25519_keypair_pct(out_public_key, out_private_key);
147
148 FIPS_service_indicator_unlock_state();
149 if (result) {
150 FIPS_service_indicator_update_state();
151 }
152 return result;
153 }
154
155 void ED25519_keypair(uint8_t out_public_key[ED25519_PUBLIC_KEY_LEN],
156 uint8_t out_private_key[ED25519_PRIVATE_KEY_LEN]) {
157 // The existing public function is void, ED25519_keypair_internal can only
158 // fail if the PWCT fails and we're in a callback build where AWS_LC_FIPS_failure
159 // doesn't abort on FIPS failure.
160 AWSLC_ASSERT(ED25519_keypair_internal(out_public_key, out_private_key));
161 }
162
163 int ED25519_sign(uint8_t out_sig[ED25519_SIGNATURE_LEN],
164 const uint8_t *message, size_t message_len,
165 const uint8_t private_key[ED25519_PRIVATE_KEY_LEN]) {
166 FIPS_service_indicator_lock_state();
167 boringssl_ensure_eddsa_self_test();
168 int res =
169 ED25519_sign_no_self_test(out_sig, message, message_len, private_key);
170 FIPS_service_indicator_unlock_state();
171 if (res) {
172 FIPS_service_indicator_update_state();
173 }
174 return res;
175 }
176
177 int ED25519_sign_no_self_test(
178 uint8_t out_sig[ED25519_SIGNATURE_LEN], const uint8_t *message,
179 size_t message_len, const uint8_t private_key[ED25519_PRIVATE_KEY_LEN]) {
180 return ed25519_sign_internal(ED25519_ALG, out_sig, message, message_len,
181 private_key, NULL, 0);
182 }
183
184 static int dom2(ed25519_algorithm_t alg, uint8_t buffer[MAX_DOM2_SIZE],
185 size_t *buffer_len, const uint8_t *context,
186 size_t context_len) {
187 GUARD_PTR(buffer_len);
188 *buffer_len = 0;
189
190 uint8_t phflag = 0;
191
192 switch (alg) {
193 case ED25519_ALG:
194 // Per rfc8032:
195 // For Ed25519, dom2(f,c) is the empty string. The phflag value is
196 // irrelevant. The context (if present at all) MUST be empty.
197 return context_len == 0;
198 case ED25519CTX_ALG:
199 // Per rfc8032:
200 // For Ed25519ctx, phflag=0. The context input SHOULD NOT be empty.
201 if (context_len == 0) {
202 return 0;
203 }
204 phflag = 0;
205 break;
206 case ED25519PH_ALG:
207 // For Ed25519ph, phflag=1
208 phflag = 1;
209 break;
210 default:
211 // Should never happen unless we missed a ed25519_algorithm_t enum variant
212 // case in this switch statement
213 abort();
214 }
215
216 OPENSSL_memcpy(buffer, RFC8032_DOM2_PREFIX, DOM2_PREFIX_SIZE);
217 buffer[DOM2_F_OFFSET] = phflag;
218 buffer[DOM2_C_OFFSET] = context_len;
219 if (context_len > 0) {
220 GUARD_PTR(context);
221 if (context_len > MAX_DOM2_CONTEXT_SIZE) {
222 return 0;
223 }
224 OPENSSL_memcpy(&buffer[DOM2_CONTEXT_OFFSET], context, context_len);
225 }
226 *buffer_len = DOM2_PREFIX_SIZE + DOM2_F_SIZE + DOM2_C_SIZE + context_len;
227 return 1;
228 }
229
230 int ed25519_sign_internal(
231 ed25519_algorithm_t alg,
232 uint8_t out_sig[ED25519_SIGNATURE_LEN],
233 const uint8_t *message, size_t message_len,
234 const uint8_t private_key[ED25519_PRIVATE_KEY_LEN],
235 const uint8_t *ctx, size_t ctx_len) {
236 // NOTE: The documentation on this function says that it returns zero on
237 // allocation failure. While that can't happen with the current
238 // implementation, we want to reserve the ability to allocate in this
239 // implementation in the future.
240
241 if (alg == ED25519PH_ALG &&
242 message_len != SHA512_DIGEST_LENGTH) {
243 OPENSSL_PUT_ERROR(CRYPTO, ERR_R_CRYPTO_LIB);
244 return 0;
245 }
246
247 // Ed25519 sign: rfc8032 5.1.6
248 //
249 // Step: rfc8032 5.1.6.1
250 // This step is a repeat of rfc8032 5.1.5.[1,2].
251 // seed = private_key[0:31]
252 // A = private_key[32:61] (per 5.1.5.4)
253 // Compute az = SHA512(seed).
254 SET_DIT_AUTO_RESET;
255 uint8_t az[SHA512_DIGEST_LENGTH];
256 SHA512(private_key, ED25519_PRIVATE_KEY_SEED_LEN, az);
257 // s = az[0:31]
258 // prefix = az[32:61]
259 az[0] &= 248; // 11111000_2
260 az[31] &= 63; // 00111111_2
261 az[31] |= 64; // 01000000_2
262
263 uint8_t r[SHA512_DIGEST_LENGTH];
264 uint8_t dom2_buffer[MAX_DOM2_SIZE] = {0};
265 size_t dom2_buffer_len = 0;
266
267 if (!dom2(alg, dom2_buffer, &dom2_buffer_len, ctx, ctx_len)) {
268 OPENSSL_PUT_ERROR(CRYPTO, ERR_R_CRYPTO_LIB);
269 OPENSSL_cleanse(az, sizeof(az));
270 return 0;
271 }
272
273 // Step: rfc8032 5.1.6.2
274 if (dom2_buffer_len > 0) {
275 // Compute r = SHA512(dom2(phflag, context) || prefix || message).
276 ed25519_sha512(r, dom2_buffer, dom2_buffer_len,
277 az + ED25519_PRIVATE_KEY_SEED_LEN,
278 ED25519_PRIVATE_KEY_SEED_LEN, message, message_len, NULL, 0);
279 } else {
280 // Compute r = SHA512(prefix || message).
281 ed25519_sha512(r, az + ED25519_PRIVATE_KEY_SEED_LEN,
282 ED25519_PRIVATE_KEY_SEED_LEN, message, message_len, NULL, 0,
283 NULL, 0);
284 }
285
286 // Step: rfc8032 5.1.6.[3,5,6,7]
287 #if defined(CURVE25519_S2N_BIGNUM_CAPABLE)
288 ed25519_sign_s2n_bignum(out_sig, r, az,
289 private_key + ED25519_PRIVATE_KEY_SEED_LEN, message,
290 message_len, dom2_buffer, dom2_buffer_len);
291 #else
292 ed25519_sign_nohw(out_sig, r, az, private_key + ED25519_PRIVATE_KEY_SEED_LEN,
293 message, message_len, dom2_buffer, dom2_buffer_len);
294 #endif
295
296 // The signature is computed from the private key, but is public.
297 CONSTTIME_DECLASSIFY(out_sig, 64);
298
299 OPENSSL_cleanse(az, sizeof(az));
300 OPENSSL_cleanse(r, sizeof(r));
301
302 return 1;
303 }
304
305 int ED25519_verify(const uint8_t *message, size_t message_len,
306 const uint8_t signature[ED25519_SIGNATURE_LEN],
307 const uint8_t public_key[ED25519_PUBLIC_KEY_LEN]) {
308 FIPS_service_indicator_lock_state();
309 boringssl_ensure_eddsa_self_test();
310 int res =
311 ED25519_verify_no_self_test(message, message_len, signature, public_key);
312 FIPS_service_indicator_unlock_state();
313 if(res) {
314 FIPS_service_indicator_update_state();
315 }
316 return res;
317 }
318
319 int ED25519_verify_no_self_test(
320 const uint8_t *message, size_t message_len,
321 const uint8_t signature[ED25519_SIGNATURE_LEN],
322 const uint8_t public_key[ED25519_PUBLIC_KEY_LEN]) {
323 return ed25519_verify_internal(ED25519_ALG, message, message_len, signature,
324 public_key, NULL, 0);
325 }
326
327 int ED25519ctx_sign(uint8_t out_sig[ED25519_SIGNATURE_LEN],
328 const uint8_t *message, size_t message_len,
329 const uint8_t private_key[ED25519_PRIVATE_KEY_LEN],
330 const uint8_t *context, size_t context_len) {
331 FIPS_service_indicator_lock_state();
332 boringssl_ensure_eddsa_self_test();
333 int res = ED25519ctx_sign_no_self_test(out_sig, message, message_len,
334 private_key, context, context_len);
335 FIPS_service_indicator_unlock_state();
336 return res;
337 }
338
339 int ED25519ctx_sign_no_self_test(
340 uint8_t out_sig[ED25519_SIGNATURE_LEN], const uint8_t *message,
341 size_t message_len, const uint8_t private_key[ED25519_PRIVATE_KEY_LEN],
342 const uint8_t *context, size_t context_len) {
343 return ed25519_sign_internal(ED25519CTX_ALG, out_sig, message, message_len,
344 private_key, context, context_len);
345 }
346
347 int ED25519ctx_verify(const uint8_t *message, size_t message_len,
348 const uint8_t signature[ED25519_SIGNATURE_LEN],
349 const uint8_t public_key[ED25519_PUBLIC_KEY_LEN],
350 const uint8_t *context, size_t context_len) {
351 FIPS_service_indicator_lock_state();
352 boringssl_ensure_eddsa_self_test();
353 int res = ED25519ctx_verify_no_self_test(message, message_len, signature,
354 public_key, context, context_len);
355 FIPS_service_indicator_unlock_state();
356 return res;
357 }
358
359 int ED25519ctx_verify_no_self_test(
360 const uint8_t *message, size_t message_len,
361 const uint8_t signature[ED25519_SIGNATURE_LEN],
362 const uint8_t public_key[ED25519_PUBLIC_KEY_LEN], const uint8_t *context,
363 size_t context_len) {
364 return ed25519_verify_internal(ED25519CTX_ALG, message, message_len,
365 signature, public_key, context, context_len);
366 }
367
368 int ED25519ph_sign(uint8_t out_sig[ED25519_SIGNATURE_LEN],
369 const uint8_t *message, size_t message_len,
370 const uint8_t private_key[ED25519_PRIVATE_KEY_LEN],
371 const uint8_t *context, size_t context_len) {
372 FIPS_service_indicator_lock_state();
373 boringssl_ensure_hasheddsa_self_test();
374 int res = ED25519ph_sign_no_self_test(out_sig, message, message_len,
375 private_key, context, context_len);
376 FIPS_service_indicator_unlock_state();
377 if (res) {
378 FIPS_service_indicator_update_state();
379 }
380 return res;
381 }
382
383 int ED25519ph_sign_no_self_test(
384 uint8_t out_sig[ED25519_SIGNATURE_LEN], const uint8_t *message,
385 size_t message_len, const uint8_t private_key[ED25519_PRIVATE_KEY_LEN],
386 const uint8_t *context, size_t context_len) {
387 uint8_t digest[SHA512_DIGEST_LENGTH] = {0};
388 SHA512_CTX ctx;
389 SHA512_Init(&ctx);
390 SHA512_Update(&ctx, message, message_len);
391 SHA512_Final(digest, &ctx);
392 return ED25519ph_sign_digest_no_self_test(out_sig, digest, private_key,
393 context, context_len);
394 }
395
396 int ED25519ph_sign_digest(uint8_t out_sig[ED25519_SIGNATURE_LEN],
397 const uint8_t digest[SHA512_DIGEST_LENGTH],
398 const uint8_t private_key[ED25519_PRIVATE_KEY_LEN],
399 const uint8_t *context, size_t context_len) {
400 FIPS_service_indicator_lock_state();
401 boringssl_ensure_hasheddsa_self_test();
402 int res = ED25519ph_sign_digest_no_self_test(out_sig, digest, private_key,
403 context, context_len);
404 FIPS_service_indicator_unlock_state();
405 if (res) {
406 FIPS_service_indicator_update_state();
407 }
408 return res;
409 }
410
411 int ED25519ph_sign_digest_no_self_test(
412 uint8_t out_sig[ED25519_SIGNATURE_LEN],
413 const uint8_t digest[SHA512_DIGEST_LENGTH],
414 const uint8_t private_key[ED25519_PRIVATE_KEY_LEN],
415 const uint8_t *context, size_t context_len) {
416 return ed25519_sign_internal(ED25519PH_ALG, out_sig, digest,
417 SHA512_DIGEST_LENGTH, private_key, context,
418 context_len);
419 }
420
421 int ED25519ph_verify(const uint8_t *message, size_t message_len,
422 const uint8_t signature[ED25519_SIGNATURE_LEN],
423 const uint8_t public_key[ED25519_PUBLIC_KEY_LEN],
424 const uint8_t *context, size_t context_len) {
425 FIPS_service_indicator_lock_state();
426 boringssl_ensure_hasheddsa_self_test();
427 int res = ED25519ph_verify_no_self_test(message, message_len, signature,
428 public_key, context, context_len);
429 FIPS_service_indicator_unlock_state();
430 if (res) {
431 FIPS_service_indicator_update_state();
432 }
433 return res;
434 }
435
436 int ED25519ph_verify_no_self_test(
437 const uint8_t *message, size_t message_len,
438 const uint8_t signature[ED25519_SIGNATURE_LEN],
439 const uint8_t public_key[ED25519_PUBLIC_KEY_LEN], const uint8_t *context,
440 size_t context_len) {
441 uint8_t digest[SHA512_DIGEST_LENGTH] = {0};
442 SHA512_CTX ctx;
443 SHA512_Init(&ctx);
444 SHA512_Update(&ctx, message, message_len);
445 SHA512_Final(digest, &ctx);
446 return ED25519ph_verify_digest_no_self_test(digest, signature, public_key,
447 context, context_len);
448 }
449
450 int ED25519ph_verify_digest(const uint8_t digest[SHA512_DIGEST_LENGTH],
451 const uint8_t signature[ED25519_SIGNATURE_LEN],
452 const uint8_t public_key[ED25519_PUBLIC_KEY_LEN],
453 const uint8_t *context, size_t context_len) {
454 FIPS_service_indicator_lock_state();
455 boringssl_ensure_hasheddsa_self_test();
456 int res = ED25519ph_verify_digest_no_self_test(
457 digest, signature, public_key, context, context_len);
458 FIPS_service_indicator_unlock_state();
459 if(res) {
460 FIPS_service_indicator_update_state();
461 }
462 return res;
463 }
464
465 int ED25519ph_verify_digest_no_self_test(
466 const uint8_t digest[SHA512_DIGEST_LENGTH],
467 const uint8_t signature[ED25519_SIGNATURE_LEN],
468 const uint8_t public_key[ED25519_PUBLIC_KEY_LEN], const uint8_t *context,
469 size_t context_len) {
470 return ed25519_verify_internal(ED25519PH_ALG, digest,
471 SHA512_DIGEST_LENGTH, signature, public_key,
472 context, context_len);
473 }
474
475 int ed25519_verify_internal(
476 ed25519_algorithm_t alg,
477 const uint8_t *message, size_t message_len,
478 const uint8_t signature[ED25519_SIGNATURE_LEN],
479 const uint8_t public_key[ED25519_PUBLIC_KEY_LEN],
480 const uint8_t *ctx, size_t ctx_len) {
481 // Ed25519 verify: rfc8032 5.1.7
482
483 if (alg == ED25519PH_ALG &&
484 message_len != SHA512_DIGEST_LENGTH) {
485 OPENSSL_PUT_ERROR(CRYPTO, ERR_R_CRYPTO_LIB);
486 return 0;
487 }
488
489 // Step: rfc8032 5.1.7.1 (up to decoding the public key)
490 // Decode signature as:
491 // - signature[0:31]: encoded point R, aliased to R_expected.
492 // - signature[32:61]: integer S.
493 uint8_t R_expected[32];
494 OPENSSL_memcpy(R_expected, signature, 32);
495 uint8_t S[32];
496 OPENSSL_memcpy(S, signature + 32, 32);
497
498 // Per rfc8032 5.1.6.6
499 // "the three most significant bits of the final octet are always zero"
500 // 224 = 11100000_2
501 if ((signature[63] & 224) != 0) {
502 return 0;
503 }
504
505 // S must be in the range [0, order) in order to prevent signature
506 // malleability. kOrder is the order of curve25519 in little-endian form.
507 static const uint64_t kOrder[4] = {
508 UINT64_C(0x5812631a5cf5d3ed),
509 UINT64_C(0x14def9dea2f79cd6),
510 0,
511 UINT64_C(0x1000000000000000),
512 };
513 for (size_t i = 3;; i--) {
514 uint64_t word = CRYPTO_load_u64_le(S + i * 8);
515 if (word > kOrder[i]) {
516 return 0;
517 } else if (word < kOrder[i]) {
518 break;
519 } else if (i == 0) {
520 return 0;
521 }
522 }
523
524 uint8_t dom2_buffer[MAX_DOM2_SIZE] = {0};
525 size_t dom2_buffer_len = 0;
526 if (!dom2(alg, dom2_buffer, &dom2_buffer_len, ctx, ctx_len)) {
527 OPENSSL_PUT_ERROR(CRYPTO, ERR_R_CRYPTO_LIB);
528 return 0;
529 }
530
531 // Step: rfc8032 5.1.7.[1,2,3]
532 // Verification works by computing [S]B - [k]A' and comparing against R_expected.
533 int res = 0;
534 uint8_t R_computed_encoded[32];
535 #if defined(CURVE25519_S2N_BIGNUM_CAPABLE)
536 res = ed25519_verify_s2n_bignum(R_computed_encoded, public_key, R_expected, S,
537 message, message_len, dom2_buffer,
538 dom2_buffer_len);
539 #else
540 res = ed25519_verify_nohw(R_computed_encoded, public_key, R_expected, S,
541 message, message_len, dom2_buffer, dom2_buffer_len);
542 #endif
543
544 // Comparison [S]B - [k]A' =? R_expected. Short-circuits if decoding failed.
545 return (res == 1) && CRYPTO_memcmp(R_computed_encoded, R_expected,
546 sizeof(R_computed_encoded)) == 0;
547 }
548
549 int ED25519_check_public_key(const uint8_t public_key[ED25519_PUBLIC_KEY_LEN]) {
550 #if defined(CURVE25519_S2N_BIGNUM_CAPABLE)
551 return ed25519_check_public_key_s2n_bignum(public_key);
552 #else
553 return ed25519_check_public_key_nohw(public_key);
554 #endif
555 }
556
557 void X25519_public_from_private(
558 uint8_t out_public_value[X25519_PUBLIC_VALUE_LEN],
559 const uint8_t private_key[X25519_PRIVATE_KEY_LEN]) {
560 SET_DIT_AUTO_RESET;
561
562 #if defined(CURVE25519_S2N_BIGNUM_CAPABLE)
563 x25519_public_from_private_s2n_bignum(out_public_value, private_key);
564 #else
565 x25519_public_from_private_nohw(out_public_value, private_key);
566 #endif
567 // The public key is derived from the private key, but it is public.
568 CONSTTIME_DECLASSIFY(out_public_value, X25519_PUBLIC_VALUE_LEN);
569 }
570
571 void X25519_keypair(uint8_t out_public_value[X25519_PUBLIC_VALUE_LEN],
572 uint8_t out_private_key[X25519_PRIVATE_KEY_LEN]) {
573 SET_DIT_AUTO_RESET;
574
575 AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(out_private_key, X25519_PRIVATE_KEY_LEN));
576
577 // All X25519 implementations should decode scalars correctly (see
578 // https://tools.ietf.org/html/rfc7748#section-5). However, if an
579 // implementation doesn't then it might interoperate with random keys a
580 // fraction of the time because they'll, randomly, happen to be correctly
581 // formed.
582 //
583 // Thus we do the opposite of the masking here to make sure that our private
584 // keys are never correctly masked and so, hopefully, any incorrect
585 // implementations are deterministically broken.
586 //
587 // This does not affect security because, although we're throwing away
588 // entropy, a valid implementation of scalarmult should throw away the exact
589 // same bits anyway.
590 out_private_key[0] |= ~248;
591 out_private_key[31] &= ~64;
592 out_private_key[31] |= ~127;
593
594 X25519_public_from_private(out_public_value, out_private_key);
595 }
596
597 int X25519(uint8_t out_shared_key[X25519_SHARED_KEY_LEN],
598 const uint8_t private_key[X25519_PRIVATE_KEY_LEN],
599 const uint8_t peer_public_value[X25519_PUBLIC_VALUE_LEN]) {
600
601 SET_DIT_AUTO_RESET;
602 static const uint8_t kZeros[X25519_SHARED_KEY_LEN] = {0};
603
604 #if defined(CURVE25519_S2N_BIGNUM_CAPABLE)
605 x25519_scalar_mult_generic_s2n_bignum(out_shared_key, private_key, peer_public_value);
606 #else
607 x25519_scalar_mult_generic_nohw(out_shared_key, private_key, peer_public_value);
608 #endif
609
610 // The all-zero output results when the input is a point of small order.
611 return constant_time_declassify_int(
612 CRYPTO_memcmp(kZeros, out_shared_key, X25519_SHARED_KEY_LEN)) != 0;
613 }