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