|
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 |
@@ -305,6 +311,9 @@ int wc_ed25519_sign_msg_ex(const byte* in, word32 inLen, byte* out, |
305 | 311 | ALIGN16 byte nonce[WC_SHA512_DIGEST_SIZE]; |
306 | 312 | ALIGN16 byte hram[WC_SHA512_DIGEST_SIZE]; |
307 | 313 | ALIGN16 byte az[ED25519_PRV_KEY_SIZE]; |
| 314 | +#ifdef WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN |
| 315 | + byte orig_k[ED25519_KEY_SIZE]; |
| 316 | +#endif |
308 | 317 |
|
309 | 318 | /* sanity check on arguments */ |
310 | 319 | if (in == NULL || out == NULL || outLen == NULL || key == NULL || |
@@ -332,6 +341,10 @@ int wc_ed25519_sign_msg_ex(const byte* in, word32 inLen, byte* out, |
332 | 341 | } |
333 | 342 | *outLen = ED25519_SIG_SIZE; |
334 | 343 |
|
| 344 | +#ifdef WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN |
| 345 | + XMEMCPY(orig_k, key->k, ED25519_KEY_SIZE); |
| 346 | +#endif |
| 347 | + |
335 | 348 | /* step 1: create nonce to use where nonce is r in |
336 | 349 | r = H(h_b, ... ,h_2b-1,M) */ |
337 | 350 | ret = ed25519_hash(key, key->k, ED25519_KEY_SIZE, az); |
@@ -442,6 +455,18 @@ int wc_ed25519_sign_msg_ex(const byte* in, word32 inLen, byte* out, |
442 | 455 | sc_muladd(out + (ED25519_SIG_SIZE/2), hram, az, nonce); |
443 | 456 | #endif |
444 | 457 | #endif /* WOLFSSL_SE050 */ |
| 458 | + |
| 459 | +#ifdef WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN |
| 460 | + { |
| 461 | + int i; |
| 462 | + byte c = 0; |
| 463 | + for (i = 0; i < ED25519_KEY_SIZE; i++) { |
| 464 | + c |= key->k[i] ^ orig_k[i]; |
| 465 | + } |
| 466 | + ret = ctMaskGT(c, 0) & SIG_VERIFY_E; |
| 467 | + } |
| 468 | +#endif |
| 469 | + |
445 | 470 | return ret; |
446 | 471 | } |
447 | 472 |
|
|
0 commit comments