Skip to content

Commit 109e765

Browse files
julek-wolfssldgarske
authored andcommitted
Clear sensitive stack buffers in ed25519 signing
F-764
1 parent e4b55be commit 109e765

1 file changed

Lines changed: 33 additions & 39 deletions

File tree

wolfcrypt/src/ed25519.c

Lines changed: 33 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -416,26 +416,21 @@ int wc_ed25519_sign_msg_ex(const byte* in, word32 inLen, byte* out,
416416
/* step 1: create nonce to use where nonce is r in
417417
r = H(h_b, ... ,h_2b-1,M) */
418418
ret = ed25519_hash(key, key->k, ED25519_KEY_SIZE, az);
419-
if (ret != 0)
420-
return ret;
421419

422-
/* apply clamp */
423-
az[0] &= 248;
424-
az[31] &= 63; /* same than az[31] &= 127 because of az[31] |= 64 */
425-
az[31] |= 64;
426-
427-
{
420+
if (ret == 0) {
428421
#ifdef WOLFSSL_ED25519_PERSISTENT_SHA
429422
wc_Sha512 *sha = &key->sha;
430423
#else
431424
wc_Sha512 sha[1];
432425
ret = ed25519_hash_init(key, sha);
433-
if (ret < 0) {
434-
return ret;
435-
}
436426
#endif
437427

438-
if (type == Ed25519ctx || type == Ed25519ph) {
428+
/* apply clamp */
429+
az[0] &= 248;
430+
az[31] &= 63; /* same than az[31] &= 127 because of az[31] |= 64 */
431+
az[31] |= 64;
432+
433+
if (ret == 0 && (type == Ed25519ctx || type == Ed25519ph)) {
439434
ret = ed25519_hash_update(key, sha, ed25519Ctx, ED25519CTX_SIZE);
440435
if (ret == 0)
441436
ret = ed25519_hash_update(key, sha, &type, sizeof(type));
@@ -457,39 +452,36 @@ int wc_ed25519_sign_msg_ex(const byte* in, word32 inLen, byte* out,
457452
#endif
458453
}
459454

460-
if (ret != 0)
461-
return ret;
462-
455+
if (ret == 0) {
463456
#ifdef FREESCALE_LTC_ECC
464-
ltcPoint.X = &tempBuf[0];
465-
ltcPoint.Y = &tempBuf[32];
466-
LTC_PKHA_sc_reduce(nonce);
467-
LTC_PKHA_Ed25519_PointMul(LTC_PKHA_Ed25519_BasePoint(), nonce,
468-
ED25519_KEY_SIZE, &ltcPoint, kLTC_Ed25519 /* result on Ed25519 */);
469-
LTC_PKHA_Ed25519_Compress(&ltcPoint, out);
457+
ltcPoint.X = &tempBuf[0];
458+
ltcPoint.Y = &tempBuf[32];
459+
LTC_PKHA_sc_reduce(nonce);
460+
LTC_PKHA_Ed25519_PointMul(LTC_PKHA_Ed25519_BasePoint(), nonce,
461+
ED25519_KEY_SIZE, &ltcPoint,
462+
kLTC_Ed25519 /* result on Ed25519 */);
463+
LTC_PKHA_Ed25519_Compress(&ltcPoint, out);
470464
#else
471-
sc_reduce(nonce);
465+
sc_reduce(nonce);
472466

473-
/* step 2: computing R = rB where rB is the scalar multiplication of
474-
r and B */
475-
ge_scalarmult_base(&R,nonce);
476-
ge_p3_tobytes(out,&R);
467+
/* step 2: computing R = rB where rB is the scalar multiplication of
468+
r and B */
469+
ge_scalarmult_base(&R,nonce);
470+
ge_p3_tobytes(out,&R);
477471
#endif
472+
}
478473

479474
/* step 3: hash R + public key + message getting H(R,A,M) then
480475
creating S = (r + H(R,A,M)a) mod l */
481-
{
476+
if (ret == 0) {
482477
#ifdef WOLFSSL_ED25519_PERSISTENT_SHA
483478
wc_Sha512 *sha = &key->sha;
484479
#else
485480
wc_Sha512 sha[1];
486-
487481
ret = ed25519_hash_init(key, sha);
488-
if (ret < 0)
489-
return ret;
490482
#endif
491483

492-
if (type == Ed25519ctx || type == Ed25519ph) {
484+
if (ret == 0 && (type == Ed25519ctx || type == Ed25519ph)) {
493485
ret = ed25519_hash_update(key, sha, ed25519Ctx, ED25519CTX_SIZE);
494486
if (ret == 0)
495487
ret = ed25519_hash_update(key, sha, &type, sizeof(type));
@@ -512,20 +504,22 @@ int wc_ed25519_sign_msg_ex(const byte* in, word32 inLen, byte* out,
512504
#endif
513505
}
514506

515-
if (ret != 0)
516-
return ret;
517-
507+
if (ret == 0) {
518508
#ifdef FREESCALE_LTC_ECC
519-
LTC_PKHA_sc_reduce(hram);
520-
LTC_PKHA_sc_muladd(out + (ED25519_SIG_SIZE/2), hram, az, nonce);
509+
LTC_PKHA_sc_reduce(hram);
510+
LTC_PKHA_sc_muladd(out + (ED25519_SIG_SIZE/2), hram, az, nonce);
521511
#else
522-
sc_reduce(hram);
523-
sc_muladd(out + (ED25519_SIG_SIZE/2), hram, az, nonce);
512+
sc_reduce(hram);
513+
sc_muladd(out + (ED25519_SIG_SIZE/2), hram, az, nonce);
524514
#endif
515+
}
516+
517+
ForceZero(az, sizeof(az));
518+
ForceZero(nonce, sizeof(nonce));
525519
#endif /* WOLFSSL_SE050 */
526520

527521
#ifdef WOLFSSL_EDDSA_CHECK_PRIV_ON_SIGN
528-
{
522+
if (ret == 0) {
529523
int i;
530524
byte c = 0;
531525
for (i = 0; i < ED25519_KEY_SIZE; i++) {

0 commit comments

Comments
 (0)