Skip to content

Commit 0b4c09c

Browse files
committed
Modify AES-CTR to not reinit after being keyed
1 parent 02c18e7 commit 0b4c09c

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

src/we_aes_ctr.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ typedef struct we_AesCtr
3434
{
3535
/* The wolfSSL AES data object. */
3636
Aes aes;
37+
word32 keyed;
3738
} we_AesCtr;
3839

3940

@@ -67,7 +68,8 @@ static int we_aes_ctr_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
6768
ret = 0;
6869
}
6970

70-
if (ret == 1) {
71+
/* Do not reinitialize if already keyed, unless setting a new key */
72+
if ((ret == 1) && ((aes->keyed == 0) || (key != NULL))) {
7173
rc = wc_AesInit(&aes->aes, NULL, INVALID_DEVID);
7274
if (rc != 0) {
7375
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesInit", rc);
@@ -86,6 +88,7 @@ static int we_aes_ctr_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
8688
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesSetKey", rc);
8789
ret = 0;
8890
}
91+
aes->keyed = 1;
8992
}
9093
if ((ret == 1) && (iv != NULL)) {
9194
rc = wc_AesSetIV(&aes->aes, iv);
@@ -94,7 +97,7 @@ static int we_aes_ctr_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
9497
ret = -1;
9598
}
9699
else {
97-
/*
100+
/*
98101
* wc_AesSetIV should clear this field, but it doesn't in some
99102
* wolfSSL versions.
100103
*/

test/test_cipher.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,17 @@ int test_aes_ctr_leftover_data_regression(ENGINE *e, void *data)
599599
}
600600
}
601601

602-
/* Try the other way, now. Encrypt with wolfEngine, decrypt with wolfSSL. */
602+
/* Try the other way, now. Encrypt with wolfEngine, decrypt with openSSL.
603+
* The EVP_CIPHER_CTX remembers any engine it was loaded with, meaning we
604+
* need to reset the ctxs before reuse or the decCtx will still pick up
605+
* wolfEngine */
606+
if (err == 0) {
607+
err = EVP_CIPHER_CTX_reset(encCtx) != 1;
608+
}
609+
if (err == 0) {
610+
err = EVP_CIPHER_CTX_reset(decCtx) != 1;
611+
}
612+
603613
if (err == 0) {
604614
err = EVP_CipherInit_ex(encCtx, EVP_aes_128_ctr(), e, key,
605615
NULL, -1) != 1;

0 commit comments

Comments
 (0)