|
22 | 22 |
|
23 | 23 | /* Based On Daniel J Bernstein's ed25519 Public Domain ref10 work. */ |
24 | 24 |
|
| 25 | + |
| 26 | +/* Possible Ed25519 enable options: |
| 27 | + * WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN Default: OFF |
| 28 | + * Check that the private key didn't change during the signing operations. |
| 29 | + */ |
| 30 | + |
25 | 31 | #ifdef HAVE_CONFIG_H |
26 | 32 | #include <config.h> |
27 | 33 | #endif |
@@ -304,6 +310,9 @@ int wc_ed25519_sign_msg_ex(const byte* in, word32 inLen, byte* out, |
304 | 310 | ALIGN16 byte nonce[WC_SHA512_DIGEST_SIZE]; |
305 | 311 | ALIGN16 byte hram[WC_SHA512_DIGEST_SIZE]; |
306 | 312 | ALIGN16 byte az[ED25519_PRV_KEY_SIZE]; |
| 313 | +#ifdef WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN |
| 314 | + byte orig_k[ED25519_KEY_SIZE]; |
| 315 | +#endif |
307 | 316 |
|
308 | 317 | /* sanity check on arguments */ |
309 | 318 | if (in == NULL || out == NULL || outLen == NULL || key == NULL || |
@@ -331,6 +340,10 @@ int wc_ed25519_sign_msg_ex(const byte* in, word32 inLen, byte* out, |
331 | 340 | } |
332 | 341 | *outLen = ED25519_SIG_SIZE; |
333 | 342 |
|
| 343 | +#ifdef WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN |
| 344 | + XMEMCPY(orig_k, key->k, ED25519_KEY_SIZE); |
| 345 | +#endif |
| 346 | + |
334 | 347 | /* step 1: create nonce to use where nonce is r in |
335 | 348 | r = H(h_b, ... ,h_2b-1,M) */ |
336 | 349 | ret = ed25519_hash(key, key->k, ED25519_KEY_SIZE, az); |
@@ -441,6 +454,18 @@ int wc_ed25519_sign_msg_ex(const byte* in, word32 inLen, byte* out, |
441 | 454 | sc_muladd(out + (ED25519_SIG_SIZE/2), hram, az, nonce); |
442 | 455 | #endif |
443 | 456 | #endif /* WOLFSSL_SE050 */ |
| 457 | + |
| 458 | +#ifdef WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN |
| 459 | + { |
| 460 | + int i; |
| 461 | + byte c = 0; |
| 462 | + for (i = 0; i < ED25519_KEY_SIZE; i++) { |
| 463 | + c |= key->k[i] ^ orig_k[i]; |
| 464 | + } |
| 465 | + ret = ctMaskGT(c, 0) & SIG_VERIFY_E; |
| 466 | + } |
| 467 | +#endif |
| 468 | + |
444 | 469 | return ret; |
445 | 470 | } |
446 | 471 |
|
|
0 commit comments