Skip to content

Commit 70670a5

Browse files
authored
Merge pull request #213 from ColtonWilley/aes_ctr_dont_reinit_after_keying
Modify AES-CTR to not reinit after being keyed
2 parents 02c18e7 + 4e2e1eb commit 70670a5

2 files changed

Lines changed: 25 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 inited;
3738
} we_AesCtr;
3839

3940

@@ -67,12 +68,14 @@ 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->inited == 0)) {
7173
rc = wc_AesInit(&aes->aes, NULL, INVALID_DEVID);
7274
if (rc != 0) {
7375
WOLFENGINE_ERROR_FUNC(WE_LOG_CIPHER, "wc_AesInit", rc);
7476
ret = 0;
7577
}
78+
aes->inited = 1;
7679
}
7780
if ((ret == 1) && (key != NULL)) {
7881
if (tmpIv == NULL) {
@@ -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: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,26 @@ 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+
if (encCtx != NULL)
608+
EVP_CIPHER_CTX_free(encCtx);
609+
}
610+
if (err == 0) {
611+
if (decCtx != NULL)
612+
EVP_CIPHER_CTX_free(decCtx);
613+
}
614+
615+
if (err == 0) {
616+
err = (encCtx = EVP_CIPHER_CTX_new()) == NULL;
617+
}
618+
if (err == 0) {
619+
err = (decCtx = EVP_CIPHER_CTX_new()) == NULL;
620+
}
621+
603622
if (err == 0) {
604623
err = EVP_CipherInit_ex(encCtx, EVP_aes_128_ctr(), e, key,
605624
NULL, -1) != 1;

0 commit comments

Comments
 (0)