forked from wolfSSL/wolfssl
asn: refactoring SetRsaPublicKey to reduce stack usage: 553 bytes - pointers size moved to the heap.
--- variable n moved to the heap (517 bytes saved) --- variable e moved to the heap (16 bytes saved) --- variable algo moved to the heap (20 bytes saved)
This commit is contained in:
@ -4854,9 +4854,15 @@ static int SetEccPublicKey(byte* output, ecc_key* key)
|
|||||||
/* Write a public RSA key to output */
|
/* Write a public RSA key to output */
|
||||||
static int SetRsaPublicKey(byte* output, RsaKey* key)
|
static int SetRsaPublicKey(byte* output, RsaKey* key)
|
||||||
{
|
{
|
||||||
|
#ifdef CYASSL_SMALL_STACK
|
||||||
|
byte* n = NULL;
|
||||||
|
byte* e = NULL;
|
||||||
|
byte* algo = NULL;
|
||||||
|
#else
|
||||||
byte n[MAX_RSA_INT_SZ];
|
byte n[MAX_RSA_INT_SZ];
|
||||||
byte e[MAX_RSA_E_SZ];
|
byte e[MAX_RSA_E_SZ];
|
||||||
byte algo[MAX_ALGO_SZ];
|
byte algo[MAX_ALGO_SZ];
|
||||||
|
#endif
|
||||||
byte seq[MAX_SEQ_SZ];
|
byte seq[MAX_SEQ_SZ];
|
||||||
byte len[MAX_LENGTH_SZ + 1]; /* trailing 0 */
|
byte len[MAX_LENGTH_SZ + 1]; /* trailing 0 */
|
||||||
int nSz;
|
int nSz;
|
||||||
@ -4870,40 +4876,83 @@ static int SetRsaPublicKey(byte* output, RsaKey* key)
|
|||||||
int err;
|
int err;
|
||||||
|
|
||||||
/* n */
|
/* n */
|
||||||
|
#ifdef CYASSL_SMALL_STACK
|
||||||
|
n = (byte*)XMALLOC(MAX_RSA_INT_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
if (n == NULL)
|
||||||
|
return MEMORY_E;
|
||||||
|
#endif
|
||||||
|
|
||||||
leadingBit = mp_leading_bit(&key->n);
|
leadingBit = mp_leading_bit(&key->n);
|
||||||
rawLen = mp_unsigned_bin_size(&key->n) + leadingBit;
|
rawLen = mp_unsigned_bin_size(&key->n) + leadingBit;
|
||||||
n[0] = ASN_INTEGER;
|
n[0] = ASN_INTEGER;
|
||||||
nSz = SetLength(rawLen, n + 1) + 1; /* int tag */
|
nSz = SetLength(rawLen, n + 1) + 1; /* int tag */
|
||||||
|
|
||||||
if ( (nSz + rawLen) < (int)sizeof(n)) {
|
if ( (nSz + rawLen) < MAX_RSA_INT_SZ) {
|
||||||
if (leadingBit)
|
if (leadingBit)
|
||||||
n[nSz] = 0;
|
n[nSz] = 0;
|
||||||
err = mp_to_unsigned_bin(&key->n, n + nSz + leadingBit);
|
err = mp_to_unsigned_bin(&key->n, n + nSz + leadingBit);
|
||||||
if (err == MP_OKAY)
|
if (err == MP_OKAY)
|
||||||
nSz += rawLen;
|
nSz += rawLen;
|
||||||
else
|
else {
|
||||||
|
#ifdef CYASSL_SMALL_STACK
|
||||||
|
XFREE(n, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
#endif
|
||||||
return MP_TO_E;
|
return MP_TO_E;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
|
#ifdef CYASSL_SMALL_STACK
|
||||||
|
XFREE(n, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
#endif
|
||||||
return BUFFER_E;
|
return BUFFER_E;
|
||||||
|
}
|
||||||
|
|
||||||
/* e */
|
/* e */
|
||||||
|
#ifdef CYASSL_SMALL_STACK
|
||||||
|
e = (byte*)XMALLOC(MAX_RSA_E_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
if (e == NULL) {
|
||||||
|
#ifdef CYASSL_SMALL_STACK
|
||||||
|
XFREE(n, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
#endif
|
||||||
|
return MEMORY_E;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
leadingBit = mp_leading_bit(&key->e);
|
leadingBit = mp_leading_bit(&key->e);
|
||||||
rawLen = mp_unsigned_bin_size(&key->e) + leadingBit;
|
rawLen = mp_unsigned_bin_size(&key->e) + leadingBit;
|
||||||
e[0] = ASN_INTEGER;
|
e[0] = ASN_INTEGER;
|
||||||
eSz = SetLength(rawLen, e + 1) + 1; /* int tag */
|
eSz = SetLength(rawLen, e + 1) + 1; /* int tag */
|
||||||
|
|
||||||
if ( (eSz + rawLen) < (int)sizeof(e)) {
|
if ( (eSz + rawLen) < MAX_RSA_E_SZ) {
|
||||||
if (leadingBit)
|
if (leadingBit)
|
||||||
e[eSz] = 0;
|
e[eSz] = 0;
|
||||||
err = mp_to_unsigned_bin(&key->e, e + eSz + leadingBit);
|
err = mp_to_unsigned_bin(&key->e, e + eSz + leadingBit);
|
||||||
if (err == MP_OKAY)
|
if (err == MP_OKAY)
|
||||||
eSz += rawLen;
|
eSz += rawLen;
|
||||||
else
|
else {
|
||||||
|
#ifdef CYASSL_SMALL_STACK
|
||||||
|
XFREE(n, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
XFREE(e, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
#endif
|
||||||
return MP_TO_E;
|
return MP_TO_E;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else {
|
||||||
|
#ifdef CYASSL_SMALL_STACK
|
||||||
|
XFREE(n, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
XFREE(e, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
#endif
|
||||||
return BUFFER_E;
|
return BUFFER_E;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef CYASSL_SMALL_STACK
|
||||||
|
algo = (byte*)XMALLOC(MAX_ALGO_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
if (algo == NULL) {
|
||||||
|
XFREE(n, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
XFREE(e, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
return MEMORY_E;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* headers */
|
/* headers */
|
||||||
algoSz = SetAlgoID(RSAk, algo, keyType, 0);
|
algoSz = SetAlgoID(RSAk, algo, keyType, 0);
|
||||||
@ -4932,6 +4981,12 @@ static int SetRsaPublicKey(byte* output, RsaKey* key)
|
|||||||
XMEMCPY(output + idx, e, eSz);
|
XMEMCPY(output + idx, e, eSz);
|
||||||
idx += eSz;
|
idx += eSz;
|
||||||
|
|
||||||
|
#ifdef CYASSL_SMALL_STACK
|
||||||
|
XFREE(n, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
XFREE(e, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
XFREE(algo, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
#endif
|
||||||
|
|
||||||
return idx;
|
return idx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user