Skip to content

Commit c8d0bb0

Browse files
authored
Merge pull request #7212 from SparkiDev/eddsa_check_priv
EdDsa: check private value after sign
2 parents 9468099 + e28d6a7 commit c8d0bb0

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

wolfcrypt/src/ed25519.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222

2323
/* Based On Daniel J Bernstein's ed25519 Public Domain ref10 work. */
2424

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+
2531
#ifdef HAVE_CONFIG_H
2632
#include <config.h>
2733
#endif
@@ -305,6 +311,9 @@ int wc_ed25519_sign_msg_ex(const byte* in, word32 inLen, byte* out,
305311
ALIGN16 byte nonce[WC_SHA512_DIGEST_SIZE];
306312
ALIGN16 byte hram[WC_SHA512_DIGEST_SIZE];
307313
ALIGN16 byte az[ED25519_PRV_KEY_SIZE];
314+
#ifdef WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN
315+
byte orig_k[ED25519_KEY_SIZE];
316+
#endif
308317

309318
/* sanity check on arguments */
310319
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,
332341
}
333342
*outLen = ED25519_SIG_SIZE;
334343

344+
#ifdef WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN
345+
XMEMCPY(orig_k, key->k, ED25519_KEY_SIZE);
346+
#endif
347+
335348
/* step 1: create nonce to use where nonce is r in
336349
r = H(h_b, ... ,h_2b-1,M) */
337350
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,
442455
sc_muladd(out + (ED25519_SIG_SIZE/2), hram, az, nonce);
443456
#endif
444457
#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+
445470
return ret;
446471
}
447472

wolfcrypt/src/ed448.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
* Reworked for curve448 by Sean Parkinson.
2626
*/
2727

28+
/* Possible Ed448 enable options:
29+
* WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN Default: OFF
30+
* Check that the private key didn't change during the signing operations.
31+
*/
32+
2833
#ifdef HAVE_CONFIG_H
2934
#include <config.h>
3035
#endif
@@ -279,6 +284,9 @@ int wc_ed448_sign_msg_ex(const byte* in, word32 inLen, byte* out,
279284
byte hram[ED448_SIG_SIZE];
280285
byte az[ED448_PRV_KEY_SIZE];
281286
int ret = 0;
287+
#ifdef WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN
288+
byte orig_k[ED448_KEY_SIZE];
289+
#endif
282290

283291
/* sanity check on arguments */
284292
if ((in == NULL) || (out == NULL) || (outLen == NULL) || (key == NULL) ||
@@ -298,6 +306,10 @@ int wc_ed448_sign_msg_ex(const byte* in, word32 inLen, byte* out,
298306
if (ret == 0) {
299307
*outLen = ED448_SIG_SIZE;
300308

309+
#ifdef WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN
310+
XMEMCPY(orig_k, key->k, ED448_KEY_SIZE);
311+
#endif
312+
301313
/* step 1: create nonce to use where nonce is r in
302314
r = H(h_b, ... ,h_2b-1,M) */
303315
ret = ed448_hash(key, key->k, ED448_KEY_SIZE, az, sizeof(az));
@@ -391,6 +403,17 @@ int wc_ed448_sign_msg_ex(const byte* in, word32 inLen, byte* out,
391403
sc448_muladd(out + (ED448_SIG_SIZE/2), hram, az, nonce);
392404
}
393405

406+
#ifdef WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN
407+
if (ret == 0) {
408+
int i;
409+
byte c = 0;
410+
for (i = 0; i < ED448_KEY_SIZE; i++) {
411+
c |= key->k[i] ^ orig_k[i];
412+
}
413+
ret = ctMaskGT(c, 0) & SIG_VERIFY_E;
414+
}
415+
#endif
416+
394417
return ret;
395418
}
396419

wolfssl/wolfcrypt/ed448.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ enum {
8080
/* An ED448 Key */
8181
struct ed448_key {
8282
byte p[ED448_PUB_KEY_SIZE]; /* compressed public key */
83-
byte k[ED448_PRV_KEY_SIZE]; /* private key : 56 secret -- 56 public */
83+
byte k[ED448_PRV_KEY_SIZE]; /* private key : 57 secret -- 57 public */
8484
#ifdef FREESCALE_LTC_ECC
8585
/* uncompressed point coordinates */
8686
byte pointX[ED448_KEY_SIZE]; /* recovered X coordinate */

0 commit comments

Comments
 (0)