asn: refactoring ToTraditionalEnc to reduce stack usage: 128 bytes - pointers size moved to the heap.
--- variable salt moved to the heap (64 bytes saved) --- variable cbcIv moved to the heap (64 bytes saved)
This commit is contained in:
parent
96aa460d03
commit
8c1a4a4944
@ -1007,8 +1007,13 @@ int ToTraditionalEnc(byte* input, word32 sz,const char* password,int passwordSz)
|
||||
word32 inOutIdx = 0, oid;
|
||||
int first, second, length, version, saltSz, id;
|
||||
int iterations = 0;
|
||||
#ifdef CYASSL_SMALL_STACK
|
||||
byte* salt = NULL;
|
||||
byte* cbcIv = NULL;
|
||||
#else
|
||||
byte salt[MAX_SALT_SIZE];
|
||||
byte cbcIv[MAX_IV_SIZE];
|
||||
#endif
|
||||
|
||||
if (GetSequence(input, &inOutIdx, &length, sz) < 0)
|
||||
return ASN_PARSE_E;
|
||||
@ -1046,39 +1051,97 @@ int ToTraditionalEnc(byte* input, word32 sz,const char* password,int passwordSz)
|
||||
if (saltSz > MAX_SALT_SIZE)
|
||||
return ASN_PARSE_E;
|
||||
|
||||
#ifdef CYASSL_SMALL_STACK
|
||||
salt = (byte*)XMALLOC(MAX_SALT_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
if (salt == NULL)
|
||||
return MEMORY_E;
|
||||
#endif
|
||||
|
||||
XMEMCPY(salt, &input[inOutIdx], saltSz);
|
||||
inOutIdx += saltSz;
|
||||
|
||||
if (GetShortInt(input, &inOutIdx, &iterations) < 0)
|
||||
if (GetShortInt(input, &inOutIdx, &iterations) < 0) {
|
||||
#ifdef CYASSL_SMALL_STACK
|
||||
XFREE(salt, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
#endif
|
||||
return ASN_PARSE_E;
|
||||
}
|
||||
|
||||
#ifdef CYASSL_SMALL_STACK
|
||||
cbcIv = (byte*)XMALLOC(MAX_IV_SIZE, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
if (cbcIv == NULL) {
|
||||
XFREE(salt, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
return MEMORY_E;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (version == PKCS5v2) {
|
||||
/* get encryption algo */
|
||||
if (GetAlgoId(input, &inOutIdx, &oid, sz) < 0)
|
||||
if (GetAlgoId(input, &inOutIdx, &oid, sz) < 0) {
|
||||
#ifdef CYASSL_SMALL_STACK
|
||||
XFREE(salt, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
XFREE(cbcIv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
#endif
|
||||
return ASN_PARSE_E;
|
||||
}
|
||||
|
||||
if (CheckAlgoV2(oid, &id) < 0)
|
||||
if (CheckAlgoV2(oid, &id) < 0) {
|
||||
#ifdef CYASSL_SMALL_STACK
|
||||
XFREE(salt, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
XFREE(cbcIv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
#endif
|
||||
return ASN_PARSE_E; /* PKCS v2 algo id error */
|
||||
}
|
||||
|
||||
if (input[inOutIdx++] != ASN_OCTET_STRING)
|
||||
if (input[inOutIdx++] != ASN_OCTET_STRING) {
|
||||
#ifdef CYASSL_SMALL_STACK
|
||||
XFREE(salt, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
XFREE(cbcIv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
#endif
|
||||
return ASN_PARSE_E;
|
||||
}
|
||||
|
||||
if (GetLength(input, &inOutIdx, &length, sz) < 0)
|
||||
if (GetLength(input, &inOutIdx, &length, sz) < 0) {
|
||||
#ifdef CYASSL_SMALL_STACK
|
||||
XFREE(salt, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
XFREE(cbcIv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
#endif
|
||||
return ASN_PARSE_E;
|
||||
}
|
||||
|
||||
XMEMCPY(cbcIv, &input[inOutIdx], length);
|
||||
inOutIdx += length;
|
||||
}
|
||||
|
||||
if (input[inOutIdx++] != ASN_OCTET_STRING)
|
||||
if (input[inOutIdx++] != ASN_OCTET_STRING) {
|
||||
#ifdef CYASSL_SMALL_STACK
|
||||
XFREE(salt, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
XFREE(cbcIv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
#endif
|
||||
return ASN_PARSE_E;
|
||||
|
||||
if (GetLength(input, &inOutIdx, &length, sz) < 0)
|
||||
}
|
||||
|
||||
if (GetLength(input, &inOutIdx, &length, sz) < 0) {
|
||||
#ifdef CYASSL_SMALL_STACK
|
||||
XFREE(salt, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
XFREE(cbcIv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
#endif
|
||||
return ASN_PARSE_E;
|
||||
}
|
||||
|
||||
if (DecryptKey(password, passwordSz, salt, saltSz, iterations, id,
|
||||
input + inOutIdx, length, version, cbcIv) < 0)
|
||||
input + inOutIdx, length, version, cbcIv) < 0) {
|
||||
#ifdef CYASSL_SMALL_STACK
|
||||
XFREE(salt, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
XFREE(cbcIv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
#endif
|
||||
return ASN_INPUT_E; /* decrypt failure */
|
||||
}
|
||||
|
||||
#ifdef CYASSL_SMALL_STACK
|
||||
XFREE(salt, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
XFREE(cbcIv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||
#endif
|
||||
|
||||
XMEMMOVE(input, input + inOutIdx, length);
|
||||
return ToTraditional(input, length);
|
||||
|
Loading…
Reference in New Issue
Block a user