Skip to content

Commit 8d9a4b4

Browse files
committed
zero CAAM ECC private key stack buffers
1 parent 6f710aa commit 8d9a4b4

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

wolfcrypt/src/port/caam/wolfcaam_ecdsa.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ static int wc_CAAM_DevEccSign(const byte* in, int inlen, byte* out,
8787
/* private key */
8888
if (mp_to_unsigned_bin_len(wc_ecc_key_get_priv(key), pk, keySz) != MP_OKAY)
8989
{
90+
ForceZero(pk, sizeof(pk));
9091
return MP_TO_E;
9192
}
9293

@@ -108,10 +109,12 @@ static int wc_CAAM_DevEccSign(const byte* in, int inlen, byte* out,
108109
mp_free(&mps);
109110
if (ret != 0) {
110111
WOLFSSL_MSG("Issue converting to signature\n");
112+
ForceZero(pk, sizeof(pk));
111113
return -1;
112114
}
113115
}
114116

117+
ForceZero(pk, sizeof(pk));
115118
return ret;
116119
}
117120

@@ -201,6 +204,7 @@ static int wc_CAAM_DevEcdh(ecc_key* private_key, ecc_key* public_key, byte* out,
201204
if (mp_to_unsigned_bin_len(wc_ecc_key_get_priv(private_key), pk, keySz) !=
202205
MP_OKAY) {
203206
WOLFSSL_MSG("error getting private key buffer");
207+
ForceZero(pk, sizeof(pk));
204208
return MP_TO_E;
205209
}
206210

@@ -209,6 +213,7 @@ static int wc_CAAM_DevEcdh(ecc_key* private_key, ecc_key* public_key, byte* out,
209213
if (ret == 0) {
210214
*outlen = keySz;
211215
}
216+
ForceZero(pk, sizeof(pk));
212217
return ret;
213218
}
214219

@@ -237,10 +242,12 @@ static int wc_CAAM_DevMakeEccKey(WC_RNG* rng, int keySize, ecc_key* key,
237242
ret = wc_DevCryptoEccKeyGen(curveId, blackKey, s, keySize, xy, keySize*2);
238243
if (wc_ecc_import_unsigned(key, xy, xy + keySize, s, curveId) != 0) {
239244
WOLFSSL_MSG("issue importing key");
245+
ForceZero(s, sizeof(s));
240246
return -1;
241247
}
242248
key->blackKey = blackKey;
243249

250+
ForceZero(s, sizeof(s));
244251
(void)rng;
245252
return ret;
246253
}
@@ -340,13 +347,15 @@ int wc_CAAM_EccSign(const byte* in, int inlen, byte* out, word32* outlen,
340347
if (key->blackKey == CAAM_BLACK_KEY_CCM) {
341348
if (mp_to_unsigned_bin_len(wc_ecc_key_get_priv(key), pk,
342349
keySz + WC_CAAM_MAC_SZ) != MP_OKAY) {
350+
ForceZero(pk, sizeof(pk));
343351
return MP_TO_E;
344352
}
345353
buf[idx].Length = keySz + WC_CAAM_MAC_SZ;
346354
}
347355
else {
348356
if (mp_to_unsigned_bin_len(wc_ecc_key_get_priv(key), pk, keySz) !=
349357
MP_OKAY) {
358+
ForceZero(pk, sizeof(pk));
350359
return MP_TO_E;
351360
}
352361
buf[idx].Length = keySz;
@@ -376,8 +385,10 @@ int wc_CAAM_EccSign(const byte* in, int inlen, byte* out, word32* outlen,
376385
args[3] = keySz;
377386

378387
ret = wc_caamAddAndWait(buf, idx, args, CAAM_ECDSA_SIGN);
379-
if (ret != 0)
388+
if (ret != 0) {
389+
ForceZero(pk, sizeof(pk));
380390
return -1;
391+
}
381392

382393
/* convert signature from raw bytes to signature format */
383394
{
@@ -394,10 +405,12 @@ int wc_CAAM_EccSign(const byte* in, int inlen, byte* out, word32* outlen,
394405
mp_free(&mps);
395406
if (ret != 0) {
396407
WOLFSSL_MSG("Issue converting to signature");
408+
ForceZero(pk, sizeof(pk));
397409
return -1;
398410
}
399411
}
400412

413+
ForceZero(pk, sizeof(pk));
401414
(void)devId;
402415
return MP_OKAY;
403416
}
@@ -610,13 +623,15 @@ int wc_CAAM_Ecdh(ecc_key* private_key, ecc_key* public_key, byte* out,
610623
if (private_key->blackKey == CAAM_BLACK_KEY_CCM) {
611624
if (mp_to_unsigned_bin_len(wc_ecc_key_get_priv(private_key), pk,
612625
keySz + WC_CAAM_MAC_SZ) != MP_OKAY) {
626+
ForceZero(pk, sizeof(pk));
613627
return MP_TO_E;
614628
}
615629
buf[idx].Length = keySz + WC_CAAM_MAC_SZ;
616630
}
617631
else {
618632
if (mp_to_unsigned_bin_len(wc_ecc_key_get_priv(private_key), pk,
619633
keySz) != MP_OKAY) {
634+
ForceZero(pk, sizeof(pk));
620635
return MP_TO_E;
621636
}
622637
buf[idx].Length = keySz;
@@ -646,6 +661,7 @@ int wc_CAAM_Ecdh(ecc_key* private_key, ecc_key* public_key, byte* out,
646661
args[2] = ecdsel;
647662
args[3] = keySz;
648663
ret = wc_caamAddAndWait(buf, idx, args, CAAM_ECDSA_ECDH);
664+
ForceZero(pk, sizeof(pk));
649665
(void)devId;
650666
if (ret == 0) {
651667
*outlen = keySz;
@@ -737,20 +753,25 @@ int wc_CAAM_MakeEccKey(WC_RNG* rng, int keySize, ecc_key* key, int curveId,
737753
key->blackKey = (pt[0] << 24) | (pt[1] << 16) | (pt[2] << 8) | pt[3];
738754
if (wc_ecc_import_unsigned(key, xy, xy + keySize, NULL, curveId) != 0) {
739755
WOLFSSL_MSG("issue importing public key");
756+
ForceZero(s, sizeof(s));
740757
return -1;
741758
}
742759
key->partNum = args[2];
760+
ForceZero(s, sizeof(s));
743761
return MP_OKAY;
744762
}
745763
else if (ret == 0) {
746764
if (wc_ecc_import_unsigned(key, xy, xy + keySize,
747765
s, curveId) != 0) {
748766
WOLFSSL_MSG("issue importing key");
767+
ForceZero(s, sizeof(s));
749768
return -1;
750769
}
751770
key->blackKey = args[0];
771+
ForceZero(s, sizeof(s));
752772
return MP_OKAY;
753773
}
774+
ForceZero(s, sizeof(s));
754775
return -1;
755776
}
756777
#endif /* WOLFSSL_KEY_GEN */

0 commit comments

Comments
 (0)