diff --git a/wolfcrypt/src/port/ti/ti-aes.c b/wolfcrypt/src/port/ti/ti-aes.c index 4d77dcfe16e..1a4ec640b5d 100644 --- a/wolfcrypt/src/port/ti/ti-aes.c +++ b/wolfcrypt/src/port/ti/ti-aes.c @@ -251,6 +251,17 @@ int wc_AesCtrEncrypt(Aes* aes, byte* out, const byte* in, word32 sz) aes->left = 0; XMEMSET(tmp, 0x0, WC_AES_BLOCK_SIZE); } + else { + /* buffered bytes do not fill a block: encrypt the partial block + * without advancing the counter, emit the new bytes, and keep + * the remaining buffered bytes for the next call */ + ret = AesProcess(aes, (byte*)out_block, (byte const *)tmp, WC_AES_BLOCK_SIZE, + AES_CFG_DIR_ENCRYPT, AES_CFG_MODE_CTR_NOCTR); + if (ret != 0) + return ret; + XMEMCPY(out, out_block+aes->left, odd); + aes->left += odd; + } in += odd; out+= odd; sz -= odd; diff --git a/wolfcrypt/src/port/ti/ti-hash.c b/wolfcrypt/src/port/ti/ti-hash.c index f239b18f646..a0b529ef2bc 100644 --- a/wolfcrypt/src/port/ti/ti-hash.c +++ b/wolfcrypt/src/port/ti/ti-hash.c @@ -127,10 +127,19 @@ static int hashCopy(wolfssl_TI_Hash *src, wolfssl_TI_Hash *dst) { if (src == NULL || dst == NULL) return BAD_FUNC_ARG; - /* only copy hash, zero the rest of the struct to avoid double-free */ - dst->msg = NULL; - dst->used = 0; - dst->len = 0; + /* copy the accumulated message into a fresh buffer so each descriptor + * owns its own allocation and can be freed independently */ + dst->used = src->used; + dst->len = src->len; + if ((src->msg != NULL) && (src->len > 0)) { + dst->msg = (byte*)XMALLOC(src->len, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (dst->msg == NULL) + return MEMORY_E; + XMEMCPY(dst->msg, src->msg, src->len); + } + else { + dst->msg = NULL; + } XMEMCPY(dst->hash, src->hash, sizeof(dst->hash)); return 0; }