mirror of
https://github.com/wolfSSL/wolfssl.git
synced 2025-07-30 02:37:28 +02:00
Merge pull request #3704 from douzzer/aesgcm-table-small-stack
--enable-aesgcm=table --enable-smallstack
This commit is contained in:
@ -6311,6 +6311,7 @@ echo " * AES-GCM: $ENABLED_AESGCM"
|
|||||||
echo " * AES-CCM: $ENABLED_AESCCM"
|
echo " * AES-CCM: $ENABLED_AESCCM"
|
||||||
echo " * AES-CTR: $ENABLED_AESCTR"
|
echo " * AES-CTR: $ENABLED_AESCTR"
|
||||||
echo " * AES-CFB: $ENABLED_AESCFB"
|
echo " * AES-CFB: $ENABLED_AESCFB"
|
||||||
|
echo " * AES-OFB: $ENABLED_AESOFB"
|
||||||
echo " * DES3: $ENABLED_DES3"
|
echo " * DES3: $ENABLED_DES3"
|
||||||
echo " * IDEA: $ENABLED_IDEA"
|
echo " * IDEA: $ENABLED_IDEA"
|
||||||
echo " * Camellia: $ENABLED_CAMELLIA"
|
echo " * Camellia: $ENABLED_CAMELLIA"
|
||||||
|
@ -775,17 +775,34 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
|
|||||||
const int bits, Aes* aes)
|
const int bits, Aes* aes)
|
||||||
{
|
{
|
||||||
int nr;
|
int nr;
|
||||||
Aes temp_key;
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
__m128i *Key_Schedule = (__m128i*)aes->key;
|
Aes *temp_key;
|
||||||
__m128i *Temp_Key_Schedule = (__m128i*)temp_key.key;
|
#else
|
||||||
|
Aes temp_key[1];
|
||||||
|
#endif
|
||||||
|
__m128i *Key_Schedule;
|
||||||
|
__m128i *Temp_Key_Schedule;
|
||||||
|
|
||||||
if (!userKey || !aes)
|
if (!userKey || !aes)
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
if (AES_set_encrypt_key(userKey,bits,&temp_key) == BAD_FUNC_ARG)
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
return BAD_FUNC_ARG;
|
if ((temp_key = (Aes *)XMALLOC(sizeof *aes, aes->heap,
|
||||||
|
DYNAMIC_TYPE_AES)) == NULL)
|
||||||
|
return MEMORY_E;
|
||||||
|
#endif
|
||||||
|
|
||||||
nr = temp_key.rounds;
|
if (AES_set_encrypt_key(userKey,bits,temp_key) == BAD_FUNC_ARG) {
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(temp_key, aes->heap, DYNAMIC_TYPE_AES);
|
||||||
|
#endif
|
||||||
|
return BAD_FUNC_ARG;
|
||||||
|
}
|
||||||
|
|
||||||
|
Key_Schedule = (__m128i*)aes->key;
|
||||||
|
Temp_Key_Schedule = (__m128i*)temp_key->key;
|
||||||
|
|
||||||
|
nr = temp_key->rounds;
|
||||||
aes->rounds = nr;
|
aes->rounds = nr;
|
||||||
|
|
||||||
SAVE_VECTOR_REGISTERS();
|
SAVE_VECTOR_REGISTERS();
|
||||||
@ -815,6 +832,10 @@ block cipher mechanism that uses n-bit binary string parameter key with 128-bits
|
|||||||
|
|
||||||
RESTORE_VECTOR_REGISTERS();
|
RESTORE_VECTOR_REGISTERS();
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(temp_key, aes->heap, DYNAMIC_TYPE_AES);
|
||||||
|
#endif
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#endif /* HAVE_AES_DECRYPT */
|
#endif /* HAVE_AES_DECRYPT */
|
||||||
@ -7473,7 +7494,11 @@ int wc_Gmac(const byte* key, word32 keySz, byte* iv, word32 ivSz,
|
|||||||
const byte* authIn, word32 authInSz,
|
const byte* authIn, word32 authInSz,
|
||||||
byte* authTag, word32 authTagSz, WC_RNG* rng)
|
byte* authTag, word32 authTagSz, WC_RNG* rng)
|
||||||
{
|
{
|
||||||
Aes aes;
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
Aes *aes = NULL;
|
||||||
|
#else
|
||||||
|
Aes aes[1];
|
||||||
|
#endif
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (key == NULL || iv == NULL || (authIn == NULL && authInSz != 0) ||
|
if (key == NULL || iv == NULL || (authIn == NULL && authInSz != 0) ||
|
||||||
@ -7482,17 +7507,26 @@ int wc_Gmac(const byte* key, word32 keySz, byte* iv, word32 ivSz,
|
|||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
|
||||||
|
DYNAMIC_TYPE_AES)) == NULL)
|
||||||
|
return MEMORY_E;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_AesGcmSetKey(&aes, key, keySz);
|
ret = wc_AesGcmSetKey(aes, key, keySz);
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
ret = wc_AesGcmSetIV(&aes, ivSz, NULL, 0, rng);
|
ret = wc_AesGcmSetIV(aes, ivSz, NULL, 0, rng);
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
ret = wc_AesGcmEncrypt_ex(&aes, NULL, NULL, 0, iv, ivSz,
|
ret = wc_AesGcmEncrypt_ex(aes, NULL, NULL, 0, iv, ivSz,
|
||||||
authTag, authTagSz, authIn, authInSz);
|
authTag, authTagSz, authIn, authInSz);
|
||||||
wc_AesFree(&aes);
|
wc_AesFree(aes);
|
||||||
}
|
}
|
||||||
ForceZero(&aes, sizeof(aes));
|
ForceZero(aes, sizeof *aes);
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
|
||||||
|
#endif
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@ -7504,7 +7538,11 @@ int wc_GmacVerify(const byte* key, word32 keySz,
|
|||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
#ifdef HAVE_AES_DECRYPT
|
#ifdef HAVE_AES_DECRYPT
|
||||||
Aes aes;
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
Aes *aes = NULL;
|
||||||
|
#else
|
||||||
|
Aes aes[1];
|
||||||
|
#endif
|
||||||
|
|
||||||
if (key == NULL || iv == NULL || (authIn == NULL && authInSz != 0) ||
|
if (key == NULL || iv == NULL || (authIn == NULL && authInSz != 0) ||
|
||||||
authTag == NULL || authTagSz == 0 || authTagSz > AES_BLOCK_SIZE) {
|
authTag == NULL || authTagSz == 0 || authTagSz > AES_BLOCK_SIZE) {
|
||||||
@ -7512,15 +7550,24 @@ int wc_GmacVerify(const byte* key, word32 keySz,
|
|||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
|
||||||
|
DYNAMIC_TYPE_AES)) == NULL)
|
||||||
|
return MEMORY_E;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_AesGcmSetKey(&aes, key, keySz);
|
ret = wc_AesGcmSetKey(aes, key, keySz);
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
ret = wc_AesGcmDecrypt(&aes, NULL, NULL, 0, iv, ivSz,
|
ret = wc_AesGcmDecrypt(aes, NULL, NULL, 0, iv, ivSz,
|
||||||
authTag, authTagSz, authIn, authInSz);
|
authTag, authTagSz, authIn, authInSz);
|
||||||
wc_AesFree(&aes);
|
wc_AesFree(aes);
|
||||||
}
|
}
|
||||||
ForceZero(&aes, sizeof(aes));
|
ForceZero(aes, sizeof *aes);
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
|
||||||
|
#endif
|
||||||
#else
|
#else
|
||||||
(void)key;
|
(void)key;
|
||||||
(void)keySz;
|
(void)keySz;
|
||||||
@ -8854,7 +8901,12 @@ static WC_INLINE void DecrementKeyWrapCounter(byte* inOutCtr)
|
|||||||
int wc_AesKeyWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
|
int wc_AesKeyWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
|
||||||
byte* out, word32 outSz, const byte* iv)
|
byte* out, word32 outSz, const byte* iv)
|
||||||
{
|
{
|
||||||
Aes aes;
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
Aes *aes = NULL;
|
||||||
|
#else
|
||||||
|
Aes aes[1];
|
||||||
|
#endif
|
||||||
|
int aes_inited = 0;
|
||||||
byte* r;
|
byte* r;
|
||||||
word32 i;
|
word32 i;
|
||||||
int ret, j;
|
int ret, j;
|
||||||
@ -8871,6 +8923,12 @@ int wc_AesKeyWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
|
|||||||
if (inSz % KEYWRAP_BLOCK_SIZE != 0)
|
if (inSz % KEYWRAP_BLOCK_SIZE != 0)
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
|
||||||
|
DYNAMIC_TYPE_AES)) == NULL)
|
||||||
|
return MEMORY_E;
|
||||||
|
#endif
|
||||||
|
|
||||||
/* user IV is optional */
|
/* user IV is optional */
|
||||||
if (iv == NULL) {
|
if (iv == NULL) {
|
||||||
XMEMSET(tmp, 0xA6, KEYWRAP_BLOCK_SIZE);
|
XMEMSET(tmp, 0xA6, KEYWRAP_BLOCK_SIZE);
|
||||||
@ -8882,13 +8940,15 @@ int wc_AesKeyWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
|
|||||||
XMEMCPY(r, in, inSz);
|
XMEMCPY(r, in, inSz);
|
||||||
XMEMSET(t, 0, sizeof(t));
|
XMEMSET(t, 0, sizeof(t));
|
||||||
|
|
||||||
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
|
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
return ret;
|
goto out;
|
||||||
|
else
|
||||||
|
aes_inited = 1;
|
||||||
|
|
||||||
ret = wc_AesSetKey(&aes, key, keySz, NULL, AES_ENCRYPTION);
|
ret = wc_AesSetKey(aes, key, keySz, NULL, AES_ENCRYPTION);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
return ret;
|
goto out;
|
||||||
|
|
||||||
for (j = 0; j <= 5; j++) {
|
for (j = 0; j <= 5; j++) {
|
||||||
for (i = 1; i <= inSz / KEYWRAP_BLOCK_SIZE; i++) {
|
for (i = 1; i <= inSz / KEYWRAP_BLOCK_SIZE; i++) {
|
||||||
@ -8896,7 +8956,7 @@ int wc_AesKeyWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
|
|||||||
/* load R[i] */
|
/* load R[i] */
|
||||||
XMEMCPY(tmp + KEYWRAP_BLOCK_SIZE, r, KEYWRAP_BLOCK_SIZE);
|
XMEMCPY(tmp + KEYWRAP_BLOCK_SIZE, r, KEYWRAP_BLOCK_SIZE);
|
||||||
|
|
||||||
wc_AesEncryptDirect(&aes, tmp, tmp);
|
wc_AesEncryptDirect(aes, tmp, tmp);
|
||||||
|
|
||||||
/* calculate new A */
|
/* calculate new A */
|
||||||
IncrementKeyWrapCounter(t);
|
IncrementKeyWrapCounter(t);
|
||||||
@ -8912,15 +8972,32 @@ int wc_AesKeyWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
|
|||||||
/* C[0] = A */
|
/* C[0] = A */
|
||||||
XMEMCPY(out, tmp, KEYWRAP_BLOCK_SIZE);
|
XMEMCPY(out, tmp, KEYWRAP_BLOCK_SIZE);
|
||||||
|
|
||||||
wc_AesFree(&aes);
|
ret = 0;
|
||||||
|
|
||||||
return inSz + KEYWRAP_BLOCK_SIZE;
|
out:
|
||||||
|
|
||||||
|
if (aes_inited)
|
||||||
|
wc_AesFree(aes);
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
if (aes)
|
||||||
|
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (ret != 0)
|
||||||
|
return ret;
|
||||||
|
else
|
||||||
|
return inSz + KEYWRAP_BLOCK_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int wc_AesKeyUnWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
|
int wc_AesKeyUnWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
|
||||||
byte* out, word32 outSz, const byte* iv)
|
byte* out, word32 outSz, const byte* iv)
|
||||||
{
|
{
|
||||||
Aes aes;
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
Aes *aes = NULL;
|
||||||
|
#else
|
||||||
|
Aes aes[1];
|
||||||
|
#endif
|
||||||
|
int aes_inited = 0;
|
||||||
byte* r;
|
byte* r;
|
||||||
word32 i, n;
|
word32 i, n;
|
||||||
int ret, j;
|
int ret, j;
|
||||||
@ -8943,6 +9020,12 @@ int wc_AesKeyUnWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
|
|||||||
if (inSz % KEYWRAP_BLOCK_SIZE != 0)
|
if (inSz % KEYWRAP_BLOCK_SIZE != 0)
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
|
||||||
|
DYNAMIC_TYPE_AES)) == NULL)
|
||||||
|
return MEMORY_E;
|
||||||
|
#endif
|
||||||
|
|
||||||
/* user IV optional */
|
/* user IV optional */
|
||||||
if (iv != NULL) {
|
if (iv != NULL) {
|
||||||
expIv = iv;
|
expIv = iv;
|
||||||
@ -8955,13 +9038,15 @@ int wc_AesKeyUnWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
|
|||||||
XMEMCPY(out, in + KEYWRAP_BLOCK_SIZE, inSz - KEYWRAP_BLOCK_SIZE);
|
XMEMCPY(out, in + KEYWRAP_BLOCK_SIZE, inSz - KEYWRAP_BLOCK_SIZE);
|
||||||
XMEMSET(t, 0, sizeof(t));
|
XMEMSET(t, 0, sizeof(t));
|
||||||
|
|
||||||
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
|
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
return ret;
|
goto out;
|
||||||
|
else
|
||||||
|
aes_inited = 1;
|
||||||
|
|
||||||
ret = wc_AesSetKey(&aes, key, keySz, NULL, AES_DECRYPTION);
|
ret = wc_AesSetKey(aes, key, keySz, NULL, AES_DECRYPTION);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
return ret;
|
goto out;
|
||||||
|
|
||||||
/* initialize counter to 6n */
|
/* initialize counter to 6n */
|
||||||
n = (inSz - 1) / KEYWRAP_BLOCK_SIZE;
|
n = (inSz - 1) / KEYWRAP_BLOCK_SIZE;
|
||||||
@ -8977,20 +9062,32 @@ int wc_AesKeyUnWrap(const byte* key, word32 keySz, const byte* in, word32 inSz,
|
|||||||
/* load R[i], starting at end of R */
|
/* load R[i], starting at end of R */
|
||||||
r = out + ((i - 1) * KEYWRAP_BLOCK_SIZE);
|
r = out + ((i - 1) * KEYWRAP_BLOCK_SIZE);
|
||||||
XMEMCPY(tmp + KEYWRAP_BLOCK_SIZE, r, KEYWRAP_BLOCK_SIZE);
|
XMEMCPY(tmp + KEYWRAP_BLOCK_SIZE, r, KEYWRAP_BLOCK_SIZE);
|
||||||
wc_AesDecryptDirect(&aes, tmp, tmp);
|
wc_AesDecryptDirect(aes, tmp, tmp);
|
||||||
|
|
||||||
/* save R[i] */
|
/* save R[i] */
|
||||||
XMEMCPY(r, tmp + KEYWRAP_BLOCK_SIZE, KEYWRAP_BLOCK_SIZE);
|
XMEMCPY(r, tmp + KEYWRAP_BLOCK_SIZE, KEYWRAP_BLOCK_SIZE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wc_AesFree(&aes);
|
|
||||||
|
|
||||||
/* verify IV */
|
/* verify IV */
|
||||||
if (XMEMCMP(tmp, expIv, KEYWRAP_BLOCK_SIZE) != 0)
|
if (XMEMCMP(tmp, expIv, KEYWRAP_BLOCK_SIZE) != 0) {
|
||||||
return BAD_KEYWRAP_IV_E;
|
ret = BAD_KEYWRAP_IV_E;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
return inSz - KEYWRAP_BLOCK_SIZE;
|
out:
|
||||||
|
|
||||||
|
if (aes_inited)
|
||||||
|
wc_AesFree(aes);
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
if (aes)
|
||||||
|
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (ret != 0)
|
||||||
|
return ret;
|
||||||
|
else
|
||||||
|
return inSz - KEYWRAP_BLOCK_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* HAVE_AES_KEYWRAP */
|
#endif /* HAVE_AES_KEYWRAP */
|
||||||
|
@ -207,31 +207,14 @@ int blake2b_init_key( blake2b_state *S, const byte outlen, const void *key,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int blake2b_compress( blake2b_state *S,
|
static WC_INLINE int blake2b_compress(
|
||||||
const byte block[BLAKE2B_BLOCKBYTES] )
|
blake2b_state *S,
|
||||||
|
const byte block[BLAKE2B_BLOCKBYTES],
|
||||||
|
word64* m,
|
||||||
|
word64* v)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
#ifdef WOLFSSL_SMALL_STACK
|
|
||||||
word64* m;
|
|
||||||
word64* v;
|
|
||||||
|
|
||||||
m = (word64*)XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
|
||||||
|
|
||||||
if ( m == NULL ) return MEMORY_E;
|
|
||||||
|
|
||||||
v = (word64*)XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
|
||||||
|
|
||||||
if ( v == NULL )
|
|
||||||
{
|
|
||||||
XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
|
||||||
return MEMORY_E;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
word64 m[16];
|
|
||||||
word64 v[16];
|
|
||||||
#endif
|
|
||||||
|
|
||||||
for( i = 0; i < 16; ++i )
|
for( i = 0; i < 16; ++i )
|
||||||
m[i] = load64( block + i * sizeof( m[i] ) );
|
m[i] = load64( block + i * sizeof( m[i] ) );
|
||||||
|
|
||||||
@ -287,17 +270,27 @@ static int blake2b_compress( blake2b_state *S,
|
|||||||
#undef G
|
#undef G
|
||||||
#undef ROUND
|
#undef ROUND
|
||||||
|
|
||||||
#ifdef WOLFSSL_SMALL_STACK
|
|
||||||
XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
|
||||||
XFREE(v, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* inlen now in bytes */
|
/* inlen now in bytes */
|
||||||
int blake2b_update( blake2b_state *S, const byte *in, word64 inlen )
|
int blake2b_update( blake2b_state *S, const byte *in, word64 inlen )
|
||||||
{
|
{
|
||||||
|
int ret = 0;
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
word64* m;
|
||||||
|
word64* v;
|
||||||
|
|
||||||
|
m = (word64*)XMALLOC(sizeof(word64) * 32, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
|
||||||
|
if ( m == NULL ) return MEMORY_E;
|
||||||
|
|
||||||
|
v = &m[16];
|
||||||
|
#else
|
||||||
|
word64 m[16];
|
||||||
|
word64 v[16];
|
||||||
|
#endif
|
||||||
|
|
||||||
while( inlen > 0 )
|
while( inlen > 0 )
|
||||||
{
|
{
|
||||||
word64 left = S->buflen;
|
word64 left = S->buflen;
|
||||||
@ -310,8 +303,8 @@ int blake2b_update( blake2b_state *S, const byte *in, word64 inlen )
|
|||||||
blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
|
blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
|
||||||
|
|
||||||
{
|
{
|
||||||
int ret = blake2b_compress( S, S->buf );
|
ret = blake2b_compress( S, S->buf, m, v );
|
||||||
if (ret < 0) return ret;
|
if (ret < 0) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
XMEMCPY( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES );
|
XMEMCPY( S->buf, S->buf + BLAKE2B_BLOCKBYTES, BLAKE2B_BLOCKBYTES );
|
||||||
@ -328,22 +321,40 @@ int blake2b_update( blake2b_state *S, const byte *in, word64 inlen )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Is this correct? */
|
/* Is this correct? */
|
||||||
int blake2b_final( blake2b_state *S, byte *out, byte outlen )
|
int blake2b_final( blake2b_state *S, byte *out, byte outlen )
|
||||||
{
|
{
|
||||||
|
int ret = 0;
|
||||||
byte buffer[BLAKE2B_OUTBYTES];
|
byte buffer[BLAKE2B_OUTBYTES];
|
||||||
int i;
|
int i;
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
word64* m;
|
||||||
|
word64* v;
|
||||||
|
|
||||||
|
m = (word64*)XMALLOC(sizeof(word64) * 32, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
|
||||||
|
if ( m == NULL ) return MEMORY_E;
|
||||||
|
|
||||||
|
v = &m[16];
|
||||||
|
#else
|
||||||
|
word64 m[16];
|
||||||
|
word64 v[16];
|
||||||
|
#endif
|
||||||
|
|
||||||
if( S->buflen > BLAKE2B_BLOCKBYTES )
|
if( S->buflen > BLAKE2B_BLOCKBYTES )
|
||||||
{
|
{
|
||||||
blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
|
blake2b_increment_counter( S, BLAKE2B_BLOCKBYTES );
|
||||||
|
|
||||||
{
|
{
|
||||||
int ret = blake2b_compress( S, S->buf );
|
ret = blake2b_compress( S, S->buf, m, v );
|
||||||
if (ret < 0) return ret;
|
if (ret < 0) goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
S->buflen -= BLAKE2B_BLOCKBYTES;
|
S->buflen -= BLAKE2B_BLOCKBYTES;
|
||||||
@ -355,15 +366,22 @@ int blake2b_final( blake2b_state *S, byte *out, byte outlen )
|
|||||||
XMEMSET( S->buf + S->buflen, 0, (wolfssl_word)(2 * BLAKE2B_BLOCKBYTES - S->buflen) );
|
XMEMSET( S->buf + S->buflen, 0, (wolfssl_word)(2 * BLAKE2B_BLOCKBYTES - S->buflen) );
|
||||||
/* Padding */
|
/* Padding */
|
||||||
{
|
{
|
||||||
int ret = blake2b_compress( S, S->buf );
|
ret = blake2b_compress( S, S->buf, m, v );
|
||||||
if (ret < 0) return ret;
|
if (ret < 0) goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
|
for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
|
||||||
store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
|
store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
|
||||||
|
|
||||||
XMEMCPY( out, buffer, outlen );
|
XMEMCPY( out, buffer, outlen );
|
||||||
return 0;
|
|
||||||
|
out:
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* inlen, at least, should be word64. Others can be size_t. */
|
/* inlen, at least, should be word64. Others can be size_t. */
|
||||||
|
@ -204,31 +204,14 @@ int blake2s_init_key( blake2s_state *S, const byte outlen, const void *key,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int blake2s_compress( blake2s_state *S,
|
static WC_INLINE int blake2s_compress(
|
||||||
const byte block[BLAKE2S_BLOCKBYTES] )
|
blake2s_state *S,
|
||||||
|
const byte block[BLAKE2S_BLOCKBYTES],
|
||||||
|
word32* m,
|
||||||
|
word32* v)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
#ifdef WOLFSSL_SMALL_STACK
|
|
||||||
word32* m;
|
|
||||||
word32* v;
|
|
||||||
|
|
||||||
m = (word32*)XMALLOC(sizeof(word32) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
|
||||||
|
|
||||||
if ( m == NULL ) return MEMORY_E;
|
|
||||||
|
|
||||||
v = (word32*)XMALLOC(sizeof(word32) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
|
||||||
|
|
||||||
if ( v == NULL )
|
|
||||||
{
|
|
||||||
XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
|
||||||
return MEMORY_E;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
word32 m[16];
|
|
||||||
word32 v[16];
|
|
||||||
#endif
|
|
||||||
|
|
||||||
for( i = 0; i < 16; ++i )
|
for( i = 0; i < 16; ++i )
|
||||||
m[i] = load32( block + i * sizeof( m[i] ) );
|
m[i] = load32( block + i * sizeof( m[i] ) );
|
||||||
|
|
||||||
@ -282,17 +265,27 @@ static int blake2s_compress( blake2s_state *S,
|
|||||||
#undef G
|
#undef G
|
||||||
#undef ROUND
|
#undef ROUND
|
||||||
|
|
||||||
#ifdef WOLFSSL_SMALL_STACK
|
|
||||||
XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
|
||||||
XFREE(v, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* inlen now in bytes */
|
/* inlen now in bytes */
|
||||||
int blake2s_update( blake2s_state *S, const byte *in, word32 inlen )
|
int blake2s_update( blake2s_state *S, const byte *in, word32 inlen )
|
||||||
{
|
{
|
||||||
|
int ret = 0;
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
word32* m;
|
||||||
|
word32* v;
|
||||||
|
|
||||||
|
m = (word32*)XMALLOC(sizeof(word32) * 32, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
|
||||||
|
if ( m == NULL ) return MEMORY_E;
|
||||||
|
|
||||||
|
v = &m[16];
|
||||||
|
#else
|
||||||
|
word32 m[16];
|
||||||
|
word32 v[16];
|
||||||
|
#endif
|
||||||
|
|
||||||
while( inlen > 0 )
|
while( inlen > 0 )
|
||||||
{
|
{
|
||||||
word32 left = S->buflen;
|
word32 left = S->buflen;
|
||||||
@ -305,8 +298,8 @@ int blake2s_update( blake2s_state *S, const byte *in, word32 inlen )
|
|||||||
blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
|
blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
|
||||||
|
|
||||||
{
|
{
|
||||||
int ret= blake2s_compress( S, S->buf );
|
ret= blake2s_compress( S, S->buf, m, v );
|
||||||
if (ret < 0) return ret;
|
if (ret < 0) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
XMEMCPY( S->buf, S->buf + BLAKE2S_BLOCKBYTES, BLAKE2S_BLOCKBYTES );
|
XMEMCPY( S->buf, S->buf + BLAKE2S_BLOCKBYTES, BLAKE2S_BLOCKBYTES );
|
||||||
@ -323,22 +316,40 @@ int blake2s_update( blake2s_state *S, const byte *in, word32 inlen )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Is this correct? */
|
/* Is this correct? */
|
||||||
int blake2s_final( blake2s_state *S, byte *out, byte outlen )
|
int blake2s_final( blake2s_state *S, byte *out, byte outlen )
|
||||||
{
|
{
|
||||||
|
int ret = 0;
|
||||||
int i;
|
int i;
|
||||||
byte buffer[BLAKE2S_BLOCKBYTES];
|
byte buffer[BLAKE2S_BLOCKBYTES];
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
word32* m;
|
||||||
|
word32* v;
|
||||||
|
|
||||||
|
m = (word32*)XMALLOC(sizeof(word32) * 32, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
|
||||||
|
if ( m == NULL ) return MEMORY_E;
|
||||||
|
|
||||||
|
v = &m[16];
|
||||||
|
#else
|
||||||
|
word32 m[16];
|
||||||
|
word32 v[16];
|
||||||
|
#endif
|
||||||
|
|
||||||
if( S->buflen > BLAKE2S_BLOCKBYTES )
|
if( S->buflen > BLAKE2S_BLOCKBYTES )
|
||||||
{
|
{
|
||||||
blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
|
blake2s_increment_counter( S, BLAKE2S_BLOCKBYTES );
|
||||||
|
|
||||||
{
|
{
|
||||||
int ret = blake2s_compress( S, S->buf );
|
ret = blake2s_compress( S, S->buf, m, v );
|
||||||
if (ret < 0) return ret;
|
if (ret < 0) goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
S->buflen -= BLAKE2S_BLOCKBYTES;
|
S->buflen -= BLAKE2S_BLOCKBYTES;
|
||||||
@ -350,15 +361,22 @@ int blake2s_final( blake2s_state *S, byte *out, byte outlen )
|
|||||||
XMEMSET( S->buf + S->buflen, 0, (wolfssl_word)(2 * BLAKE2S_BLOCKBYTES - S->buflen) );
|
XMEMSET( S->buf + S->buflen, 0, (wolfssl_word)(2 * BLAKE2S_BLOCKBYTES - S->buflen) );
|
||||||
/* Padding */
|
/* Padding */
|
||||||
{
|
{
|
||||||
int ret = blake2s_compress( S, S->buf );
|
ret = blake2s_compress( S, S->buf, m, v );
|
||||||
if (ret < 0) return ret;
|
if (ret < 0) goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
|
for( i = 0; i < 8; ++i ) /* Output full hash to temp buffer */
|
||||||
store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
|
store64( buffer + sizeof( S->h[i] ) * i, S->h[i] );
|
||||||
|
|
||||||
XMEMCPY( out, buffer, outlen );
|
XMEMCPY( out, buffer, outlen );
|
||||||
return 0;
|
|
||||||
|
out:
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(m, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* inlen, at least, should be word32. Others can be size_t. */
|
/* inlen, at least, should be word32. Others can be size_t. */
|
||||||
|
@ -165,25 +165,42 @@ int wc_AesCmacGenerate(byte* out, word32* outSz,
|
|||||||
const byte* in, word32 inSz,
|
const byte* in, word32 inSz,
|
||||||
const byte* key, word32 keySz)
|
const byte* key, word32 keySz)
|
||||||
{
|
{
|
||||||
Cmac cmac;
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
Cmac *cmac;
|
||||||
|
#else
|
||||||
|
Cmac cmac[1];
|
||||||
|
#endif
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
if (out == NULL || (in == NULL && inSz > 0) || key == NULL || keySz == 0)
|
if (out == NULL || (in == NULL && inSz > 0) || key == NULL || keySz == 0)
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
ret = wc_InitCmac(&cmac, key, keySz, WC_CMAC_AES, NULL);
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
if (ret != 0)
|
if ((cmac = (Cmac *)XMALLOC(sizeof *cmac, NULL,
|
||||||
return ret;
|
DYNAMIC_TYPE_CMAC)) == NULL)
|
||||||
|
return MEMORY_E;
|
||||||
|
#endif
|
||||||
|
|
||||||
ret = wc_CmacUpdate(&cmac, in, inSz);
|
ret = wc_InitCmac(cmac, key, keySz, WC_CMAC_AES, NULL);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
return ret;
|
goto out;
|
||||||
|
|
||||||
ret = wc_CmacFinal(&cmac, out, outSz);
|
ret = wc_CmacUpdate(cmac, in, inSz);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
return ret;
|
goto out;
|
||||||
|
|
||||||
return 0;
|
ret = wc_CmacFinal(cmac, out, outSz);
|
||||||
|
if (ret != 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
|
out:
|
||||||
|
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
if (cmac)
|
||||||
|
XFREE(cmac, NULL, DYNAMIC_TYPE_CMAC);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -10615,20 +10615,32 @@ int wc_ecc_encrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
|
|||||||
switch (ctx->encAlgo) {
|
switch (ctx->encAlgo) {
|
||||||
case ecAES_128_CBC:
|
case ecAES_128_CBC:
|
||||||
{
|
{
|
||||||
Aes aes;
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
|
Aes *aes = (Aes *)XMALLOC(sizeof *aes, NULL,
|
||||||
|
DYNAMIC_TYPE_AES);
|
||||||
|
if (aes == NULL) {
|
||||||
|
ret = MEMORY_E;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
Aes aes[1];
|
||||||
|
#endif
|
||||||
|
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_AesSetKey(&aes, encKey, KEY_SIZE_128, encIv,
|
ret = wc_AesSetKey(aes, encKey, KEY_SIZE_128, encIv,
|
||||||
AES_ENCRYPTION);
|
AES_ENCRYPTION);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_AesCbcEncrypt(&aes, out, msg, msgSz);
|
ret = wc_AesCbcEncrypt(aes, out, msg, msgSz);
|
||||||
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
|
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
|
||||||
ret = wc_AsyncWait(ret, &aes.asyncDev,
|
ret = wc_AsyncWait(ret, &aes->asyncDev,
|
||||||
WC_ASYNC_FLAG_NONE);
|
WC_ASYNC_FLAG_NONE);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
wc_AesFree(&aes);
|
wc_AesFree(aes);
|
||||||
}
|
}
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
|
||||||
|
#endif
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -10644,18 +10656,30 @@ int wc_ecc_encrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
|
|||||||
switch (ctx->macAlgo) {
|
switch (ctx->macAlgo) {
|
||||||
case ecHMAC_SHA256:
|
case ecHMAC_SHA256:
|
||||||
{
|
{
|
||||||
Hmac hmac;
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
|
Hmac *hmac = (Hmac *)XMALLOC(sizeof *hmac, NULL,
|
||||||
if (ret == 0) {
|
DYNAMIC_TYPE_HMAC);
|
||||||
ret = wc_HmacSetKey(&hmac, WC_SHA256, macKey, WC_SHA256_DIGEST_SIZE);
|
if (hmac == NULL) {
|
||||||
if (ret == 0)
|
ret = MEMORY_E;
|
||||||
ret = wc_HmacUpdate(&hmac, out, msgSz);
|
break;
|
||||||
if (ret == 0)
|
|
||||||
ret = wc_HmacUpdate(&hmac, ctx->macSalt, ctx->macSaltSz);
|
|
||||||
if (ret == 0)
|
|
||||||
ret = wc_HmacFinal(&hmac, out+msgSz);
|
|
||||||
wc_HmacFree(&hmac);
|
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
Hmac hmac[1];
|
||||||
|
#endif
|
||||||
|
ret = wc_HmacInit(hmac, NULL, INVALID_DEVID);
|
||||||
|
if (ret == 0) {
|
||||||
|
ret = wc_HmacSetKey(hmac, WC_SHA256, macKey, WC_SHA256_DIGEST_SIZE);
|
||||||
|
if (ret == 0)
|
||||||
|
ret = wc_HmacUpdate(hmac, out, msgSz);
|
||||||
|
if (ret == 0)
|
||||||
|
ret = wc_HmacUpdate(hmac, ctx->macSalt, ctx->macSaltSz);
|
||||||
|
if (ret == 0)
|
||||||
|
ret = wc_HmacFinal(hmac, out+msgSz);
|
||||||
|
wc_HmacFree(hmac);
|
||||||
|
}
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(hmac, NULL, DYNAMIC_TYPE_HMAC);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -10785,24 +10809,34 @@ int wc_ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
|
|||||||
case ecHMAC_SHA256:
|
case ecHMAC_SHA256:
|
||||||
{
|
{
|
||||||
byte verify[WC_SHA256_DIGEST_SIZE];
|
byte verify[WC_SHA256_DIGEST_SIZE];
|
||||||
Hmac hmac;
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
Hmac *hmac = (Hmac *)XMALLOC(sizeof *hmac, NULL, DYNAMIC_TYPE_HMAC);
|
||||||
ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
|
if (hmac == NULL) {
|
||||||
|
ret = MEMORY_E;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
Hmac hmac[1];
|
||||||
|
#endif
|
||||||
|
ret = wc_HmacInit(hmac, NULL, INVALID_DEVID);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_HmacSetKey(&hmac, WC_SHA256, macKey, WC_SHA256_DIGEST_SIZE);
|
ret = wc_HmacSetKey(hmac, WC_SHA256, macKey, WC_SHA256_DIGEST_SIZE);
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
ret = wc_HmacUpdate(&hmac, msg, msgSz-digestSz);
|
ret = wc_HmacUpdate(hmac, msg, msgSz-digestSz);
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
ret = wc_HmacUpdate(&hmac, ctx->macSalt, ctx->macSaltSz);
|
ret = wc_HmacUpdate(hmac, ctx->macSalt, ctx->macSaltSz);
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
ret = wc_HmacFinal(&hmac, verify);
|
ret = wc_HmacFinal(hmac, verify);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
if (XMEMCMP(verify, msg + msgSz - digestSz, digestSz) != 0)
|
if (XMEMCMP(verify, msg + msgSz - digestSz, digestSz) != 0)
|
||||||
ret = -1;
|
ret = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
wc_HmacFree(&hmac);
|
wc_HmacFree(hmac);
|
||||||
}
|
}
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(hmac, NULL, DYNAMIC_TYPE_HMAC);
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -10817,21 +10851,33 @@ int wc_ecc_decrypt(ecc_key* privKey, ecc_key* pubKey, const byte* msg,
|
|||||||
#ifdef HAVE_AES_CBC
|
#ifdef HAVE_AES_CBC
|
||||||
case ecAES_128_CBC:
|
case ecAES_128_CBC:
|
||||||
{
|
{
|
||||||
Aes aes;
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
|
Aes *aes = (Aes *)XMALLOC(sizeof *aes, NULL,
|
||||||
|
DYNAMIC_TYPE_AES);
|
||||||
|
if (aes == NULL) {
|
||||||
|
ret = MEMORY_E;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
Aes aes[1];
|
||||||
|
#endif
|
||||||
|
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_AesSetKey(&aes, encKey, KEY_SIZE_128, encIv,
|
ret = wc_AesSetKey(aes, encKey, KEY_SIZE_128, encIv,
|
||||||
AES_DECRYPTION);
|
AES_DECRYPTION);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_AesCbcDecrypt(&aes, out, msg,
|
ret = wc_AesCbcDecrypt(aes, out, msg,
|
||||||
msgSz-digestSz);
|
msgSz-digestSz);
|
||||||
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
|
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)
|
||||||
ret = wc_AsyncWait(ret, &aes.asyncDev,
|
ret = wc_AsyncWait(ret, &aes->asyncDev,
|
||||||
WC_ASYNC_FLAG_NONE);
|
WC_ASYNC_FLAG_NONE);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
wc_AesFree(&aes);
|
wc_AesFree(aes);
|
||||||
}
|
}
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
|
||||||
|
#endif
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -6628,7 +6628,11 @@ static int wc_PKCS7_EncryptContent(int encryptOID, byte* key, int keySz,
|
|||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
#ifndef NO_AES
|
#ifndef NO_AES
|
||||||
Aes aes;
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
Aes *aes;
|
||||||
|
#else
|
||||||
|
Aes aes[1];
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
#ifndef NO_DES3
|
#ifndef NO_DES3
|
||||||
Des des;
|
Des des;
|
||||||
@ -6662,13 +6666,21 @@ static int wc_PKCS7_EncryptContent(int encryptOID, byte* key, int keySz,
|
|||||||
(ivSz != AES_BLOCK_SIZE) )
|
(ivSz != AES_BLOCK_SIZE) )
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
|
||||||
|
DYNAMIC_TYPE_AES)) == NULL)
|
||||||
|
return MEMORY_E;
|
||||||
|
#endif
|
||||||
|
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_AesSetKey(&aes, key, keySz, iv, AES_ENCRYPTION);
|
ret = wc_AesSetKey(aes, key, keySz, iv, AES_ENCRYPTION);
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
ret = wc_AesCbcEncrypt(&aes, out, in, inSz);
|
ret = wc_AesCbcEncrypt(aes, out, in, inSz);
|
||||||
wc_AesFree(&aes);
|
wc_AesFree(aes);
|
||||||
}
|
}
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
#ifdef HAVE_AESGCM
|
#ifdef HAVE_AESGCM
|
||||||
#ifdef WOLFSSL_AES_128
|
#ifdef WOLFSSL_AES_128
|
||||||
@ -6685,14 +6697,22 @@ static int wc_PKCS7_EncryptContent(int encryptOID, byte* key, int keySz,
|
|||||||
if (authTag == NULL)
|
if (authTag == NULL)
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
|
||||||
|
DYNAMIC_TYPE_AES)) == NULL)
|
||||||
|
return MEMORY_E;
|
||||||
|
#endif
|
||||||
|
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_AesGcmSetKey(&aes, key, keySz);
|
ret = wc_AesGcmSetKey(aes, key, keySz);
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
ret = wc_AesGcmEncrypt(&aes, out, in, inSz, iv, ivSz,
|
ret = wc_AesGcmEncrypt(aes, out, in, inSz, iv, ivSz,
|
||||||
authTag, authTagSz, aad, aadSz);
|
authTag, authTagSz, aad, aadSz);
|
||||||
wc_AesFree(&aes);
|
wc_AesFree(aes);
|
||||||
}
|
}
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
#endif /* HAVE_AESGCM */
|
#endif /* HAVE_AESGCM */
|
||||||
@ -6711,14 +6731,22 @@ static int wc_PKCS7_EncryptContent(int encryptOID, byte* key, int keySz,
|
|||||||
if (authTag == NULL)
|
if (authTag == NULL)
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
|
||||||
|
DYNAMIC_TYPE_AES)) == NULL)
|
||||||
|
return MEMORY_E;
|
||||||
|
#endif
|
||||||
|
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_AesCcmSetKey(&aes, key, keySz);
|
ret = wc_AesCcmSetKey(aes, key, keySz);
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
ret = wc_AesCcmEncrypt(&aes, out, in, inSz, iv, ivSz,
|
ret = wc_AesCcmEncrypt(aes, out, in, inSz, iv, ivSz,
|
||||||
authTag, authTagSz, aad, aadSz);
|
authTag, authTagSz, aad, aadSz);
|
||||||
wc_AesFree(&aes);
|
wc_AesFree(aes);
|
||||||
}
|
}
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
#endif /* HAVE_AESCCM */
|
#endif /* HAVE_AESCCM */
|
||||||
@ -6770,7 +6798,11 @@ static int wc_PKCS7_DecryptContent(PKCS7* pkcs7, int encryptOID, byte* key,
|
|||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
#ifndef NO_AES
|
#ifndef NO_AES
|
||||||
Aes aes;
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
Aes *aes;
|
||||||
|
#else
|
||||||
|
Aes aes[1];
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
#ifndef NO_DES3
|
#ifndef NO_DES3
|
||||||
Des des;
|
Des des;
|
||||||
@ -6812,13 +6844,21 @@ static int wc_PKCS7_DecryptContent(PKCS7* pkcs7, int encryptOID, byte* key,
|
|||||||
#endif
|
#endif
|
||||||
(ivSz != AES_BLOCK_SIZE) )
|
(ivSz != AES_BLOCK_SIZE) )
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
|
||||||
|
DYNAMIC_TYPE_AES)) == NULL)
|
||||||
|
return MEMORY_E;
|
||||||
|
#endif
|
||||||
|
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_AesSetKey(&aes, key, keySz, iv, AES_DECRYPTION);
|
ret = wc_AesSetKey(aes, key, keySz, iv, AES_DECRYPTION);
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
|
ret = wc_AesCbcDecrypt(aes, out, in, inSz);
|
||||||
wc_AesFree(&aes);
|
wc_AesFree(aes);
|
||||||
}
|
}
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
#ifdef HAVE_AESGCM
|
#ifdef HAVE_AESGCM
|
||||||
#ifdef WOLFSSL_AES_128
|
#ifdef WOLFSSL_AES_128
|
||||||
@ -6835,14 +6875,22 @@ static int wc_PKCS7_DecryptContent(PKCS7* pkcs7, int encryptOID, byte* key,
|
|||||||
if (authTag == NULL)
|
if (authTag == NULL)
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
|
||||||
|
DYNAMIC_TYPE_AES)) == NULL)
|
||||||
|
return MEMORY_E;
|
||||||
|
#endif
|
||||||
|
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_AesGcmSetKey(&aes, key, keySz);
|
ret = wc_AesGcmSetKey(aes, key, keySz);
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
ret = wc_AesGcmDecrypt(&aes, out, in, inSz, iv, ivSz,
|
ret = wc_AesGcmDecrypt(aes, out, in, inSz, iv, ivSz,
|
||||||
authTag, authTagSz, aad, aadSz);
|
authTag, authTagSz, aad, aadSz);
|
||||||
wc_AesFree(&aes);
|
wc_AesFree(aes);
|
||||||
}
|
}
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
#endif /* HAVE_AESGCM */
|
#endif /* HAVE_AESGCM */
|
||||||
@ -6861,14 +6909,22 @@ static int wc_PKCS7_DecryptContent(PKCS7* pkcs7, int encryptOID, byte* key,
|
|||||||
if (authTag == NULL)
|
if (authTag == NULL)
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
if ((aes = (Aes *)XMALLOC(sizeof *aes, NULL,
|
||||||
|
DYNAMIC_TYPE_AES)) == NULL)
|
||||||
|
return MEMORY_E;
|
||||||
|
#endif
|
||||||
|
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
ret = wc_AesCcmSetKey(&aes, key, keySz);
|
ret = wc_AesCcmSetKey(aes, key, keySz);
|
||||||
if (ret == 0)
|
if (ret == 0)
|
||||||
ret = wc_AesCcmDecrypt(&aes, out, in, inSz, iv, ivSz,
|
ret = wc_AesCcmDecrypt(aes, out, in, inSz, iv, ivSz,
|
||||||
authTag, authTagSz, aad, aadSz);
|
authTag, authTagSz, aad, aadSz);
|
||||||
wc_AesFree(&aes);
|
wc_AesFree(aes);
|
||||||
}
|
}
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
|
||||||
|
#endif
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
#endif /* HAVE_AESCCM */
|
#endif /* HAVE_AESCCM */
|
||||||
@ -10695,7 +10751,6 @@ int wc_PKCS7_EncodeAuthEnvelopedData(PKCS7* pkcs7, byte* output,
|
|||||||
const byte contentTypeOid[] =
|
const byte contentTypeOid[] =
|
||||||
{ ASN_OBJECT_ID, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01,
|
{ ASN_OBJECT_ID, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01,
|
||||||
0x09, 0x03 };
|
0x09, 0x03 };
|
||||||
|
|
||||||
if (pkcs7 == NULL || pkcs7->content == NULL || pkcs7->contentSz == 0)
|
if (pkcs7 == NULL || pkcs7->content == NULL || pkcs7->contentSz == 0)
|
||||||
return BAD_FUNC_ARG;
|
return BAD_FUNC_ARG;
|
||||||
|
|
||||||
|
@ -618,31 +618,41 @@ int wc_CryptKey(const char* password, int passwordSz, byte* salt,
|
|||||||
case PBE_AES256_CBC:
|
case PBE_AES256_CBC:
|
||||||
case PBE_AES128_CBC:
|
case PBE_AES128_CBC:
|
||||||
{
|
{
|
||||||
Aes aes;
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
|
Aes *aes;
|
||||||
|
aes = (Aes *)XMALLOC(sizeof *aes, NULL, DYNAMIC_TYPE_AES);
|
||||||
|
if (aes == NULL)
|
||||||
|
return MEMORY_E;
|
||||||
|
#else
|
||||||
|
Aes aes[1];
|
||||||
|
#endif
|
||||||
|
ret = wc_AesInit(aes, NULL, INVALID_DEVID);
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
if (enc) {
|
if (enc) {
|
||||||
ret = wc_AesSetKey(&aes, key, derivedLen, cbcIv,
|
ret = wc_AesSetKey(aes, key, derivedLen, cbcIv,
|
||||||
AES_ENCRYPTION);
|
AES_ENCRYPTION);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
ret = wc_AesSetKey(&aes, key, derivedLen, cbcIv,
|
ret = wc_AesSetKey(aes, key, derivedLen, cbcIv,
|
||||||
AES_DECRYPTION);
|
AES_DECRYPTION);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ret == 0) {
|
if (ret == 0) {
|
||||||
if (enc)
|
if (enc)
|
||||||
ret = wc_AesCbcEncrypt(&aes, input, input, length);
|
ret = wc_AesCbcEncrypt(aes, input, input, length);
|
||||||
else
|
else
|
||||||
ret = wc_AesCbcDecrypt(&aes, input, input, length);
|
ret = wc_AesCbcDecrypt(aes, input, input, length);
|
||||||
}
|
}
|
||||||
|
ForceZero(aes, sizeof(Aes));
|
||||||
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
|
XFREE(aes, NULL, DYNAMIC_TYPE_AES);
|
||||||
|
#endif
|
||||||
if (ret != 0) {
|
if (ret != 0) {
|
||||||
#ifdef WOLFSSL_SMALL_STACK
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
|
||||||
#endif
|
#endif
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
ForceZero(&aes, sizeof(Aes));
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#endif /* WOLFSSL_AES_256 */
|
#endif /* WOLFSSL_AES_256 */
|
||||||
|
1790
wolfcrypt/test/test.c
Executable file → Normal file
1790
wolfcrypt/test/test.c
Executable file → Normal file
File diff suppressed because it is too large
Load Diff
@ -757,6 +757,8 @@ decouple library dependencies with standard string, memory and so on.
|
|||||||
DYNAMIC_TYPE_NAME_ENTRY = 90,
|
DYNAMIC_TYPE_NAME_ENTRY = 90,
|
||||||
DYNAMIC_TYPE_CURVE448 = 91,
|
DYNAMIC_TYPE_CURVE448 = 91,
|
||||||
DYNAMIC_TYPE_ED448 = 92,
|
DYNAMIC_TYPE_ED448 = 92,
|
||||||
|
DYNAMIC_TYPE_AES = 93,
|
||||||
|
DYNAMIC_TYPE_CMAC = 94,
|
||||||
DYNAMIC_TYPE_SNIFFER_SERVER = 1000,
|
DYNAMIC_TYPE_SNIFFER_SERVER = 1000,
|
||||||
DYNAMIC_TYPE_SNIFFER_SESSION = 1001,
|
DYNAMIC_TYPE_SNIFFER_SESSION = 1001,
|
||||||
DYNAMIC_TYPE_SNIFFER_PB = 1002,
|
DYNAMIC_TYPE_SNIFFER_PB = 1002,
|
||||||
|
Reference in New Issue
Block a user