diff --git a/doc/dox_comments/header_files/asn_public.h b/doc/dox_comments/header_files/asn_public.h index 76891997e28..f940f0bff87 100644 --- a/doc/dox_comments/header_files/asn_public.h +++ b/doc/dox_comments/header_files/asn_public.h @@ -2154,7 +2154,11 @@ int wc_GetPubKeyDerFromCert(struct DecodedCert* cert, \code DsaKey key; word32 idx = 0; - int ret = wc_DsaParamsDecode(derBuf, &idx, &key, derSz); + int ret = wc_InitDsaKey(&key); + if (ret == 0) { + ret = wc_DsaParamsDecode(derBuf, &idx, &key, derSz); + wc_FreeDsaKey(&key); + } \endcode \sa wc_DsaKeyToParamsDer diff --git a/doc/dox_comments/header_files/dh.h b/doc/dox_comments/header_files/dh.h index 9516b384ebe..6570d93bcca 100644 --- a/doc/dox_comments/header_files/dh.h +++ b/doc/dox_comments/header_files/dh.h @@ -938,8 +938,16 @@ int wc_DhGeneratePublic(DhKey* key, byte* priv, word32 privSz, \code DhKey key; byte priv[256], pub[256]; - int ret = wc_DhImportKeyPair(&key, priv, sizeof(priv), pub, - sizeof(pub)); + int ret2; + int ret = wc_InitDhKey(&key); + if (ret == 0) { + ret = wc_DhImportKeyPair(&key, priv, sizeof(priv), pub, + sizeof(pub)); + ret2 = wc_FreeDhKey(&key); + if (ret == 0) { + ret = ret2; + } + } \endcode \sa wc_DhExportKeyPair diff --git a/tests/api/test_dh.c b/tests/api/test_dh.c index 28a37b1979e..a2e96e9e008 100644 --- a/tests/api/test_dh.c +++ b/tests/api/test_dh.c @@ -76,6 +76,11 @@ int test_wc_DhPublicKeyDecode(void) ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); inOutIdx = 0; + ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, + sizeof_dh_pub_key_der_2048), 0); + /* Decode again into the same key; the first values must be released, + * not orphaned. */ + inOutIdx = 0; ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, sizeof_dh_pub_key_der_2048), 0); ExpectIntNE(key.p.used, 0); @@ -274,6 +279,10 @@ int test_wc_DhSetKey(void) if (params != NULL) { /* valid: known FFDHE prime, short-circuits the primality test */ ExpectIntEQ(wc_InitDhKey(&key), 0); + ExpectIntEQ(wc_DhSetKey(&key, params->p, params->p_len, params->g, + params->g_len), 0); + /* Set again on the same key; the first values must be released, + * not orphaned. */ ExpectIntEQ(wc_DhSetKey(&key, params->p, params->p_len, params->g, params->g_len), 0); wc_FreeDhKey(&key); @@ -762,6 +771,9 @@ int test_wc_DhImportExportKeyPair(void) /* both */ ExpectIntEQ(wc_InitDhKey(&key), 0); ExpectIntEQ(wc_DhImportKeyPair(&key, priv, privSz, pub, pubSz), 0); + /* Import again into the same key; the first values must be released, + * not orphaned. */ + ExpectIntEQ(wc_DhImportKeyPair(&key, priv, privSz, pub, pubSz), 0); /* export bad args */ privOutSz = sizeof(privOut); @@ -1033,6 +1045,9 @@ int test_wc_DhGenerateParams_and_ExportRaw(void) WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_DhGenerateParams(&rng, 1024, &dh), 0); + /* Generate again into the same key; the first values must be released, + * not orphaned. */ + ExpectIntEQ(wc_DhGenerateParams(&rng, 1024, &dh), 0); /* bad args */ pSz = sizeof(pOut); qSz = sizeof(qOut); gSz = sizeof(gOut); diff --git a/tests/api/test_dsa.c b/tests/api/test_dsa.c index 6291f0c91cd..839d7afd459 100644 --- a/tests/api/test_dsa.c +++ b/tests/api/test_dsa.c @@ -179,6 +179,10 @@ int test_wc_DsaPublicPrivateKeyDecode(void) ExpectIntEQ(wc_InitDsaKey(&key), 0); ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0); + /* Decode again into the same key; the first values must be released, + * not orphaned. */ + idx = 0; + ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0); /* Test bad args. */ ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); @@ -190,6 +194,10 @@ int test_wc_DsaPublicPrivateKeyDecode(void) ExpectIntEQ(wc_InitDsaKey(&key), 0); idx = 0; /* Reset */ ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0); + /* Decode again into the same key; the first values must be released, + * not orphaned. */ + idx = 0; + ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0); /* Test bad args. */ ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); @@ -218,6 +226,9 @@ int test_wc_MakeDsaKey(void) ExpectIntEQ(wc_InitDsaKey(&genKey), 0); ExpectIntEQ(wc_InitRng(&rng), 0); + ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0); + /* Generate again into the same key; the first values must be released, + * not orphaned. */ ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0); /* Test bad args. */ ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); @@ -226,6 +237,9 @@ int test_wc_MakeDsaKey(void) WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0); + /* Generate again into the same key; the first key must be wiped and + * released, not orphaned. */ + ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0); /* Test bad args. */ ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); diff --git a/tests/api/test_ecc.c b/tests/api/test_ecc.c index e8790ffa513..c93af7973b9 100644 --- a/tests/api/test_ecc.c +++ b/tests/api/test_ecc.c @@ -293,6 +293,14 @@ int test_wc_ecc_make_key(void) #endif ExpectIntEQ(ret, 0); + /* Generate again into the same key; the first private scalar and public + * point must be released, not orphaned. */ + ret = wc_ecc_make_key(&rng, KEY14, &key); +#if defined(WOLFSSL_ASYNC_CRYPT) + ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE); +#endif + ExpectIntEQ(ret, 0); + /* Pass in bad args. */ ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); @@ -1504,6 +1512,9 @@ int test_wc_ecc_import_x963(void) PRIVATE_KEY_LOCK(); ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0); + /* Import again into the same key; the first values must be released, + * not orphaned. */ + ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0); /* Test bad args. */ ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), @@ -2392,6 +2403,9 @@ int test_wc_ecc_import_raw(void) /* Test good import */ ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0); + /* Import again into the same key; the first values must be released, + * not orphaned. */ + ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0); /* Test bad args. */ ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), @@ -2479,6 +2493,10 @@ int test_wc_ecc_import_unsigned(void) ExpectIntEQ(wc_ecc_init(&key), 0); + ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d, + curveId), 0); + /* Import again into the same key; the first values must be released, + * not orphaned. */ ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d, curveId), 0); /* Test bad args. */ diff --git a/tests/api/test_rsa.c b/tests/api/test_rsa.c index 58940ba1c54..e7dea4a036e 100644 --- a/tests/api/test_rsa.c +++ b/tests/api/test_rsa.c @@ -93,6 +93,10 @@ int test_wc_RsaPrivateKeyDecode(void) #endif /* Use cert buffers. */ } + ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0); + /* Decode again into the same key; the first values must be released, + * not orphaned. */ + idx = 0; ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0); /* Test bad args. */ @@ -181,6 +185,10 @@ int test_wc_RsaPublicKeyDecode(void) #endif } + ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0); + /* Decode again into the same key; the first values must be released, + * not orphaned. */ + idx = 0; ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0); /* Pass in bad args. */ @@ -396,6 +404,9 @@ int test_wc_MakeRsaKey(void) ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0); ExpectIntEQ(wc_InitRng(&rng), 0); ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0); + /* Generate again into the same key; the first key must be wiped and + * released, not orphaned. */ + ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0); DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0); /* Test bad args. */ diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 097f604c2d4..a66b07d1497 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -9284,6 +9284,8 @@ static int _RsaPrivateKeyDecode(const byte* input, word32* inOutIdx, int i; /* Extract all public fields. */ for (i = 0; i < RSA_ASN_INTS; i++) { + /* Wipe and free an earlier value; GetASN_MP() inits again. */ + mp_forcezero(GetRsaInt(key, i)); GetASN_MP(&dataASN[(byte)RSAKEYASN_IDX_N + i], GetRsaInt(key, i)); } @@ -12432,6 +12434,11 @@ int wc_DhPublicKeyDecode(const byte* input, word32* inOutIdx, if (GetSequence(input, inOutIdx, &length, inSz) < 0) return ASN_PARSE_E; + /* Free p, g and pub from wc_InitDhKey() or an earlier load; GetInt() + * inits them again. */ + mp_clear(&key->p); + mp_clear(&key->g); + mp_clear(&key->pub); if (GetInt(&key->p, input, inOutIdx, inSz) < 0) return ASN_DH_KEY_E; @@ -12582,6 +12589,10 @@ int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, word32 inSz) if (ret == 0) { /* Initialize data and set mp_ints to hold p and g. */ XMEMSET(dataASN, 0, sizeof(*dataASN) * dhParamASN_Length); + /* Free p and g from wc_InitDhKey() or an earlier load; GetASN_MP() + * inits them again. */ + mp_clear(&key->p); + mp_clear(&key->g); GetASN_MP(&dataASN[DHPARAMASN_IDX_PRIME], &key->p); GetASN_MP(&dataASN[DHPARAMASN_IDX_BASE], &key->g); /* Try simple PKCS #3 template. */ @@ -12596,6 +12607,10 @@ int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, word32 inSz) XMEMSET(dataASN, 0, sizeof(*dataASN) * dhKeyPkcs8ASN_Length); GetASN_ExpBuffer(&dataASN[DHKEYPKCS8ASN_IDX_PKEYALGO_OID], keyDhOid, sizeof(keyDhOid)); + /* Wipe and free q, priv and pub from an earlier load. */ + mp_clear(&key->q); + mp_forcezero(&key->priv); + mp_clear(&key->pub); GetASN_MP(&dataASN[DHKEYPKCS8ASN_IDX_PKEYALGO_PARAM_P], &key->p); GetASN_MP(&dataASN[DHKEYPKCS8ASN_IDX_PKEYALGO_PARAM_G], &key->g); GetASN_MP(&dataASN[DHKEYPKCS8ASN_IDX_PKEYALGO_PARAM_Q], &key->q); @@ -12928,8 +12943,11 @@ int wc_DsaPublicKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key, /* seq * p, q, g, y * Start DSA ints from DSAKEYASN_IDX_VER instead of DSAKEYASN_IDX_P */ - for (i = 0; i < DSA_INTS - 1; i++) + for (i = 0; i < DSA_INTS - 1; i++) { + /* Free an earlier value; GetASN_MP() inits it again. */ + mp_clear(GetDsaInt(key, i)); GetASN_MP(&dataASN[(int)DSAKEYASN_IDX_VER + i], GetDsaInt(key, i)); + } /* Parse as simple form. */ ret = GetASN_Items(dsaKeyASN, dataASN, dsaPublicKeyASN_Length, 0, input, inOutIdx, inSz); @@ -12939,11 +12957,14 @@ int wc_DsaPublicKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key, /* Set DSA OID to expect. */ GetASN_ExpBuffer(&dataASN[DSAPUBKEYASN_IDX_ALGOID_OID], keyDsaOid, sizeof(keyDsaOid)); - /* p, q, g */ - for (i = 0; i < DSA_INTS - 2; i++) + /* p, q, g; the simple form above may have filled them. */ + for (i = 0; i < DSA_INTS - 2; i++) { + mp_clear(GetDsaInt(key, i)); GetASN_MP(&dataASN[(int)DSAPUBKEYASN_IDX_ALGOID_PARAMS_P + i], GetDsaInt(key, i)); + } /* y */ + mp_clear(GetDsaInt(key, i)); GetASN_MP(&dataASN[DSAPUBKEYASN_IDX_PUBKEY_Y], GetDsaInt(key, i)); /* Parse as SubjectPublicKeyInfo. */ ret = GetASN_Items(dsaPubKeyASN, dataASN, dsaPubKeyASN_Length, 1, @@ -12973,6 +12994,11 @@ int wc_DsaParamsDecode(const byte* input, word32* inOutIdx, DsaKey* key, return ASN_PARSE_E; maxIdx = (word32)(*inOutIdx + (word32)length); + /* Free p, q and g from wc_InitDsaKey() or earlier parameters; GetInt() + * inits them again. */ + mp_clear(&key->p); + mp_clear(&key->q); + mp_clear(&key->g); if (GetInt(&key->p, input, inOutIdx, maxIdx) < 0 || GetInt(&key->q, input, inOutIdx, maxIdx) < 0 || GetInt(&key->g, input, inOutIdx, maxIdx) < 0) @@ -13044,6 +13070,11 @@ int wc_DsaPrivateKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key, if (ret == 0) { int i; + /* Wipe and free values from wc_InitDsaKey() or an earlier decode; + * GetASN_MP() inits them again. */ + for (i = 0; i < DSA_INTS; i++) { + mp_forcezero(GetDsaInt(key, i)); + } /* Try dsaKeyOctASN */ /* Initialize key data and set mp_ints for params */ for (i = 0; i < DSA_INTS - 2; i++) { @@ -42206,6 +42237,9 @@ static int _RsaPublicKeyDecodeRaw(const byte* n, word32 nSz, const byte* e, key->type = RSA_PUBLIC; + /* Free n and e from wc_InitRsaKey() or an earlier decode before re-init. */ + mp_clear(&key->n); + mp_clear(&key->e); if (mp_init(&key->n) != MP_OKAY) { return MP_INIT_E; } diff --git a/wolfcrypt/src/asn_orig.c b/wolfcrypt/src/asn_orig.c index db50c035799..8172978e724 100644 --- a/wolfcrypt/src/asn_orig.c +++ b/wolfcrypt/src/asn_orig.c @@ -234,6 +234,8 @@ static int _RsaPrivateKeyDecode(const byte* input, word32* inOutIdx, /* Extract all public fields. */ for (i = 0; i < RSA_INT_CNT; i++) { + /* Wipe and free an earlier value; GetInt() inits it again. */ + mp_forcezero(GetRsaInt(key, i)); if (GetInt(GetRsaInt(key, i), input, inOutIdx, inSz) < 0) { for (i--; i >= 0; i--) { mp_clear(GetRsaInt(key, i)); @@ -929,6 +931,10 @@ int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, word32 inSz) temp = *inOutIdx; #endif #endif + /* Free p and g from wc_InitDhKey() or an earlier load; GetInt() inits + * them again. */ + mp_clear(&key->p); + mp_clear(&key->g); /* Assume input started after 1.2.840.113549.1.3.1 dhKeyAgreement */ if (GetInt(&key->p, input, inOutIdx, inSz) < 0) { ret = ASN_DH_KEY_E; @@ -975,6 +981,7 @@ int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, word32 inSz) ret = (CheckBitString(input, inOutIdx, &length, inSz, 0, NULL) == 0); if (ret > 0) { /* Found Bit String */ + mp_clear(&key->pub); if (GetInt(&key->pub, input, inOutIdx, inSz) == 0) { WOLFSSL_MSG("Found Public Key"); ret = 0; @@ -984,6 +991,7 @@ int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key, word32 inSz) ret = (GetOctetString(input, inOutIdx, &length, inSz) >= 0); if (ret > 0) { /* Found Octet String */ + mp_forcezero(&key->priv); if (GetInt(&key->priv, input, inOutIdx, inSz) == 0) { WOLFSSL_MSG("Found Private Key"); @@ -1192,6 +1200,18 @@ int wc_DhParamsLoad(const byte* input, word32 inSz, byte* p, word32* pInOutSz, #endif #ifndef NO_DSA +/* Free p, q, g and y, filled by wc_InitDsaKey(), an earlier decode or a + * partly failed parse, before GetInt() inits them again. + */ +static void DsaFreePublicInts(DsaKey* key) +{ + int i; + + for (i = 0; i < DSA_INTS - 1; i++) { + mp_clear(GetDsaInt(key, i)); + } +} + int wc_DsaPublicKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key, word32 inSz) { @@ -1206,6 +1226,7 @@ int wc_DsaPublicKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key, if (GetSequence(input, inOutIdx, &length, inSz) < 0) return ASN_PARSE_E; + DsaFreePublicInts(key); maxIdx = (word32)(*inOutIdx + (word32)length); if (GetInt(&key->p, input, inOutIdx, maxIdx) < 0 || GetInt(&key->q, input, inOutIdx, maxIdx) < 0 || @@ -1224,6 +1245,8 @@ int wc_DsaPublicKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key, if (GetSequence(input, inOutIdx, &length, inSz) < 0) return ASN_PARSE_E; + /* The simple form above may have filled some of them. */ + DsaFreePublicInts(key); if (GetInt(&key->p, input, inOutIdx, inSz) < 0 || GetInt(&key->q, input, inOutIdx, inSz) < 0 || GetInt(&key->g, input, inOutIdx, inSz) < 0) @@ -1246,6 +1269,7 @@ int wc_DsaPrivateKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key, word32 inSz) { int length, version, ret = 0, temp = 0; + int i; word32 algId = 0; /* Sanity checks on input */ @@ -1253,6 +1277,12 @@ int wc_DsaPrivateKeyDecode(const byte* input, word32* inOutIdx, DsaKey* key, return BAD_FUNC_ARG; } + /* Wipe and free values from wc_InitDsaKey() or an earlier decode; + * GetInt() inits them again. */ + for (i = 0; i < DSA_INTS; i++) { + mp_forcezero(GetDsaInt(key, i)); + } + /* if has pkcs8 header skip it */ if (ToTraditionalInline_ex(input, inOutIdx, inSz, &algId) < 0) { /* ignore error, did not have pkcs8 header */ diff --git a/wolfcrypt/src/dh.c b/wolfcrypt/src/dh.c index ab966d78a68..6b94bd22292 100644 --- a/wolfcrypt/src/dh.c +++ b/wolfcrypt/src/dh.c @@ -2640,6 +2640,8 @@ int wc_DhImportKeyPair(DhKey* key, const byte* priv, word32 privSz, if (priv[0] == 0) { privSz--; priv++; } + /* Wipe and free an earlier private key before re-init. */ + mp_forcezero(&key->priv); if (mp_init(&key->priv) != MP_OKAY) havePriv = 0; } @@ -2661,6 +2663,8 @@ int wc_DhImportKeyPair(DhKey* key, const byte* priv, word32 privSz, if (pub[0] == 0) { pubSz--; pub++; } + /* Free an earlier public key before re-init. */ + mp_clear(&key->pub); if (mp_init(&key->pub) != MP_OKAY) havePub = 0; } @@ -2753,6 +2757,10 @@ static int _DhSetKey(DhKey* key, const byte* p, word32 pSz, const byte* g, } } + /* Free p and g from wc_InitDhKey() or an earlier load before + * re-init; q is freed below only when a new q is set. */ + mp_clear(&key->p); + mp_clear(&key->g); if (mp_init(&key->p) != MP_OKAY) ret = MP_INIT_E; } @@ -2858,6 +2866,7 @@ static int _DhSetKey(DhKey* key, const byte* p, word32 pSz, const byte* g, } if (ret == 0 && q != NULL) { + mp_clear(&key->q); if (mp_init(&key->q) != MP_OKAY) ret = MP_INIT_E; } @@ -3374,6 +3383,11 @@ int wc_DhGenerateParams(WC_RNG *rng, int modSz, DhKey *dh) /* force even */ buf[bufSz - 1] &= 0xfe; + /* Free p, q and g from wc_InitDhKey() or an earlier load before + * re-init. */ + mp_clear(&dh->p); + mp_clear(&dh->q); + mp_clear(&dh->g); if (mp_init_multi(tmp, tmp2, &dh->p, &dh->q, &dh->g, 0) != MP_OKAY) { ret = MP_INIT_E; diff --git a/wolfcrypt/src/dsa.c b/wolfcrypt/src/dsa.c index 6414405e333..385f1660f12 100644 --- a/wolfcrypt/src/dsa.c +++ b/wolfcrypt/src/dsa.c @@ -284,6 +284,10 @@ int wc_MakeDsaKey(WC_RNG *rng, DsaKey *dsa) * backend's own error (MP_MEM from the heap backends) and, on failure, * leaves every argument either cleared or never touched, so the * cleanup must not mp_clear() any of them. */ + /* Wipe and free x and y from wc_InitDsaKey() or an earlier key + * before re-init. */ + mp_forcezero(&dsa->x); + mp_clear(&dsa->y); err = mp_init_multi(&dsa->x, &dsa->y, tmpQ, NULL, NULL, NULL); if (err != MP_OKAY) err = MP_INIT_E; @@ -436,6 +440,11 @@ int wc_MakeDsaParameters(WC_RNG *rng, int modulus_size, DsaKey *dsa) * that code to skip mp_clear() on objects the failed init never * constructed, but mp_init_multi() returns the backend's own error * (MP_MEM from the heap backends), so the guard never fired. */ + /* Free p, q and g from wc_InitDsaKey() or earlier parameters before + * re-init. */ + mp_clear(&dsa->p); + mp_clear(&dsa->q); + mp_clear(&dsa->g); err = mp_init_multi(tmp, tmp2, &dsa->p, &dsa->q, &dsa->g, 0); if (err != MP_OKAY) err = MP_INIT_E; diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index 27c73a84c0c..a69af5b1bbe 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -5836,6 +5836,19 @@ static WC_INLINE void wc_ecc_reset(ecc_key* key) key->state = ECC_STATE_NONE; } +#ifndef ALT_ECC_SIZE +/* Wipe and free the private scalar and public point that wc_ecc_init() set up + * or an earlier import filled, before an import initializes them again. + */ +static WC_INLINE void ecc_free_key_ints(ecc_key* key) +{ + ecc_forcezero_k(key); + mp_clear(key->pubkey.x); + mp_clear(key->pubkey.y); + mp_clear(key->pubkey.z); +} +#endif + #if defined(HAVE_ECC_MAKE_PUB) && !defined(WOLF_CRYPTO_CB_ONLY_ECC) /* compute the public key Q = d*G in software. @@ -5996,6 +6009,13 @@ static int ecc_make_pub_ex(ecc_key* key, ecc_curve_spec* curve, ECC_PRIVATEKEY_ONLY */ pub = &key->pubkey; key->type = ECC_PRIVATEKEY_ONLY; + #ifndef ALT_ECC_SIZE + /* wc_ecc_init() set up key->pubkey; free any digits a previous call + * grew before mp_init_multi() below drops them. */ + mp_clear(pub->x); + mp_clear(pub->y); + mp_clear(pub->z); + #endif } if (err == MP_OKAY) { @@ -6450,6 +6470,9 @@ static int _ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, DECLARE_CURVE_SPECS(ECC_CURVE_FIELD_COUNT); /* setup the key variables */ + /* wc_ecc_init() set up key->k; wipe and free any earlier private + * scalar and its blind before the init below drops them. */ + ecc_forcezero_k(key); #ifndef ALT_ECC_SIZE err = mp_init(key->k); #else @@ -11967,6 +11990,7 @@ static int _ecc_import_x963_ex2(const byte* in, word32 inLen, ecc_key* key, alt_fp_init(key->ku); #endif #else + ecc_free_key_ints(key); err = mp_init_multi(key->k, key->pubkey.x, key->pubkey.y, key->pubkey.z, #ifndef WOLFSSL_ECC_BLIND_K NULL, NULL @@ -13161,6 +13185,7 @@ static int _ecc_import_raw_private(ecc_key* key, const char* qx, alt_fp_init(key->ku); #endif #else + ecc_free_key_ints(key); err = mp_init_multi(key->k, key->pubkey.x, key->pubkey.y, key->pubkey.z, #ifndef WOLFSSL_ECC_BLIND_K NULL, NULL diff --git a/wolfcrypt/src/rsa.c b/wolfcrypt/src/rsa.c index 76dccf2d24d..52f01a1f7b8 100644 --- a/wolfcrypt/src/rsa.c +++ b/wolfcrypt/src/rsa.c @@ -6029,7 +6029,13 @@ int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng) mp_copy(tmp1, q); } - /* Setup RsaKey buffers */ + /* Setup RsaKey buffers: wipe and free what wc_InitRsaKey() or an earlier + * decode or generation left, as mp_init_multi() below would drop it. */ + if (err == MP_OKAY) { + RsaForceZeroPriv(key); + mp_clear(&key->e); + mp_clear(&key->n); + } if (err == MP_OKAY) err = mp_init_multi(&key->n, &key->e, &key->d, &key->p, &key->q, NULL); if (err == MP_OKAY) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index b05b7da0311..d56e34ce741 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -46468,6 +46468,13 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t dh_test(void) ret = wc_DhKeyDecode(tmp, &idx, key2, bytes); if (ret != 0) ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done); + + /* Decode again into the same key; the first values must be released, + * not orphaned. */ + idx = 0; + ret = wc_DhKeyDecode(tmp, &idx, key2, bytes); + if (ret != 0) + ERROR_OUT(WC_TEST_RET_ENC_EC(ret), done); #endif #ifndef WC_NO_RNG