diff --git a/wolfcrypt/src/asn.c b/wolfcrypt/src/asn.c index 83a24b420..bdd6bd1e4 100644 --- a/wolfcrypt/src/asn.c +++ b/wolfcrypt/src/asn.c @@ -16044,10 +16044,16 @@ static int wc_BuildEccKeyDer(ecc_key* key, byte* output, word32 inLen, byte curve[MAX_ALGO_SZ+2]; byte ver[MAX_VERSION_SZ]; byte seq[MAX_SEQ_SZ]; - byte *prv = NULL, *pub = NULL; int ret, totalSz, curveSz, verSz; int privHdrSz = ASN_ECC_HEADER_SZ; int pubHdrSz = ASN_ECC_CONTEXT_SZ + ASN_ECC_HEADER_SZ; +#ifdef WOLFSSL_NO_MALLOC + byte prv[MAX_ECC_BYTES + ASN_ECC_HEADER_SZ + MAX_SEQ_SZ]; + byte pub[(MAX_ECC_BYTES * 2) + 1 + ASN_ECC_CONTEXT_SZ + + ASN_ECC_HEADER_SZ + MAX_SEQ_SZ]; +#else + byte *prv = NULL, *pub = NULL; +#endif word32 idx = 0, prvidx = 0, pubidx = 0, curveidx = 0; word32 seqSz, privSz, pubSz = ECC_BUFSIZE; @@ -16067,15 +16073,23 @@ static int wc_BuildEccKeyDer(ecc_key* key, byte* output, word32 inLen, /* private */ privSz = key->dp->size; +#ifndef WOLFSSL_NO_MALLOC prv = (byte*)XMALLOC(privSz + privHdrSz + MAX_SEQ_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER); if (prv == NULL) { return MEMORY_E; } +#else + if (sizeof(prv) < privSz + privHdrSz + MAX_SEQ_SZ) { + return BUFFER_E; + } +#endif prvidx += SetOctetString8Bit(key->dp->size, &prv[prvidx]); ret = wc_ecc_export_private_only(key, prv + prvidx, &privSz); if (ret < 0) { + #ifndef WOLFSSL_NO_MALLOC XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER); + #endif return ret; } prvidx += privSz; @@ -16084,16 +16098,24 @@ static int wc_BuildEccKeyDer(ecc_key* key, byte* output, word32 inLen, if (pubIn) { ret = wc_ecc_export_x963(key, NULL, &pubSz); if (ret != LENGTH_ONLY_E) { + #ifndef WOLFSSL_NO_MALLOC XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER); + #endif return ret; } + #ifndef WOLFSSL_NO_MALLOC pub = (byte*)XMALLOC(pubSz + pubHdrSz + MAX_SEQ_SZ, key->heap, DYNAMIC_TYPE_TMP_BUFFER); if (pub == NULL) { XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER); return MEMORY_E; } + #else + if (sizeof(pub) < pubSz + pubHdrSz + MAX_SEQ_SZ) { + return BUFFER_E; + } + #endif pub[pubidx++] = ECC_PREFIX_1; if (pubSz > 128) /* leading zero + extra size byte */ @@ -16105,8 +16127,10 @@ static int wc_BuildEccKeyDer(ecc_key* key, byte* output, word32 inLen, pubidx += SetBitString(pubSz, 0, pub + pubidx); ret = wc_ecc_export_x963(key, pub + pubidx, &pubSz); if (ret != 0) { + #ifndef WOLFSSL_NO_MALLOC XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER); XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER); + #endif return ret; } pubidx += pubSz; @@ -16120,7 +16144,9 @@ static int wc_BuildEccKeyDer(ecc_key* key, byte* output, word32 inLen, if (totalSz > (int)inLen) { XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER); if (pubIn) { + #ifndef WOLFSSL_NO_MALLOC XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER); + #endif } return BAD_FUNC_ARG; } @@ -16137,7 +16163,9 @@ static int wc_BuildEccKeyDer(ecc_key* key, byte* output, word32 inLen, /* private */ XMEMCPY(output + idx, prv, prvidx); idx += prvidx; +#ifndef WOLFSSL_NO_MALLOC XFREE(prv, key->heap, DYNAMIC_TYPE_TMP_BUFFER); +#endif /* curve */ XMEMCPY(output + idx, curve, curveidx); @@ -16147,7 +16175,9 @@ static int wc_BuildEccKeyDer(ecc_key* key, byte* output, word32 inLen, if (pubIn) { XMEMCPY(output + idx, pub, pubidx); /* idx += pubidx; not used after write, if more data remove comment */ + #ifndef WOLFSSL_NO_MALLOC XFREE(pub, key->heap, DYNAMIC_TYPE_TMP_BUFFER); + #endif } return totalSz; @@ -16182,7 +16212,11 @@ int wc_EccPrivateKeyToPKCS8(ecc_key* key, byte* output, word32* outLen) word32 oidSz = 0; word32 pkcs8Sz = 0; const byte* curveOID = NULL; +#ifdef WOLFSSL_NO_MALLOC + byte tmpDer[ECC_BUFSIZE]; +#else byte* tmpDer = NULL; +#endif if (key == NULL || outLen == NULL) return BAD_FUNC_ARG; @@ -16193,16 +16227,19 @@ int wc_EccPrivateKeyToPKCS8(ecc_key* key, byte* output, word32* outLen) if (ret < 0) return ret; +#ifndef WOLFSSL_NO_MALLOC /* temp buffer for plain DER key */ tmpDer = (byte*)XMALLOC(ECC_BUFSIZE, key->heap, DYNAMIC_TYPE_TMP_BUFFER); if (tmpDer == NULL) return MEMORY_E; - +#endif XMEMSET(tmpDer, 0, ECC_BUFSIZE); tmpDerSz = wc_BuildEccKeyDer(key, tmpDer, ECC_BUFSIZE, 0); if (tmpDerSz < 0) { + #ifndef WOLFSSL_NO_MALLOC XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER); + #endif return tmpDerSz; } @@ -16210,17 +16247,24 @@ int wc_EccPrivateKeyToPKCS8(ecc_key* key, byte* output, word32* outLen) ret = wc_CreatePKCS8Key(NULL, &pkcs8Sz, tmpDer, tmpDerSz, algoID, curveOID, oidSz); if (ret != LENGTH_ONLY_E) { + #ifndef WOLFSSL_NO_MALLOC XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER); + #endif return ret; } if (output == NULL) { + #ifndef WOLFSSL_NO_MALLOC XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER); + #endif *outLen = pkcs8Sz; return LENGTH_ONLY_E; - } else if (*outLen < pkcs8Sz) { + } + else if (*outLen < pkcs8Sz) { + #ifndef WOLFSSL_NO_MALLOC XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER); + #endif WOLFSSL_MSG("Input buffer too small for ECC PKCS#8 key"); return BUFFER_E; } @@ -16228,11 +16272,15 @@ int wc_EccPrivateKeyToPKCS8(ecc_key* key, byte* output, word32* outLen) ret = wc_CreatePKCS8Key(output, &pkcs8Sz, tmpDer, tmpDerSz, algoID, curveOID, oidSz); if (ret < 0) { + #ifndef WOLFSSL_NO_MALLOC XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER); + #endif return ret; } +#ifndef WOLFSSL_NO_MALLOC XFREE(tmpDer, key->heap, DYNAMIC_TYPE_TMP_BUFFER); +#endif *outLen = ret; return ret; diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 54846351a..caf260b9d 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -10663,7 +10663,7 @@ static int random_rng_test(void) if (ret != 0) return ret; -#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) +#if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_NO_MALLOC) { byte nonce[8] = { 0 }; /* Test dynamic RNG. */ @@ -10834,7 +10834,7 @@ static int simple_mem_test(int sz) static int memory_test(void) { int ret = 0; -#ifndef USE_FAST_MATH +#if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC) byte* b = NULL; #endif #if defined(COMPLEX_MEM_TEST) || defined(WOLFSSL_STATIC_MEMORY) @@ -10951,7 +10951,7 @@ static int memory_test(void) } #endif -#ifndef USE_FAST_MATH +#if !defined(USE_FAST_MATH) && !defined(WOLFSSL_NO_MALLOC) /* realloc test */ b = (byte*)XMALLOC(MEM_TEST_SZ, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); if (b) { @@ -19610,6 +19610,7 @@ done: return ret; } +#if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) static int ecc_test_key_decode(WC_RNG* rng, int keySize) { int ret; @@ -19687,8 +19688,10 @@ static int ecc_test_key_decode(WC_RNG* rng, int keySize) return ret; } +#endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */ #endif /* HAVE_ECC_KEY_IMPORT */ +#if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) static int ecc_test_key_gen(WC_RNG* rng, int keySize) { int ret = 0; @@ -19784,6 +19787,7 @@ done: return ret; } +#endif /* HAVE_ECC_KEY_EXPORT && !NO_ASN_CRYPT */ static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount, int curve_id, const ecc_set_type* dp) @@ -20222,6 +20226,8 @@ static int ecc_test_curve(WC_RNG* rng, int keySize) } #endif +#if defined(HAVE_ECC_KEY_IMPORT) && defined(HAVE_ECC_KEY_EXPORT) && \ + !defined(NO_ASN_CRYPT) ret = ecc_test_key_decode(rng, keySize); if (ret < 0) { if (ret == ECC_CURVE_OID_E) { @@ -20232,7 +20238,9 @@ static int ecc_test_curve(WC_RNG* rng, int keySize) return ret; } } +#endif +#if defined(HAVE_ECC_KEY_EXPORT) && !defined(NO_ASN_CRYPT) ret = ecc_test_key_gen(rng, keySize); if (ret < 0) { if (ret == ECC_CURVE_OID_E) { @@ -20243,6 +20251,7 @@ static int ecc_test_curve(WC_RNG* rng, int keySize) return ret; } } +#endif return 0; }