Skip to content

Commit a7e5c6c

Browse files
Merge pull request #7011 from philljj/add_missing_aesinit
Add missing wc_AesInit calls.
2 parents cc65c3e + 8c1ab78 commit a7e5c6c

6 files changed

Lines changed: 84 additions & 2 deletions

File tree

src/ssl_crypto.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2904,7 +2904,6 @@ void wolfSSL_DES_ecb_encrypt(WOLFSSL_DES_cblock* in, WOLFSSL_DES_cblock* out,
29042904
/* Sets the key into the AES key object for encryption or decryption.
29052905
*
29062906
* TODO: check bits value?
2907-
* TODO: initialize AES key?
29082907
*
29092908
* @param [in] key Key data.
29102909
* @param [in] bits Number of bits in key.
@@ -2927,6 +2926,12 @@ static int wolfssl_aes_set_key(const unsigned char *key, const int bits,
29272926
}
29282927

29292928
XMEMSET(aes, 0, sizeof(AES_KEY));
2929+
2930+
if (wc_AesInit((Aes*)aes, NULL, INVALID_DEVID) != 0) {
2931+
WOLFSSL_MSG("Error in initting AES key");
2932+
return -1;
2933+
}
2934+
29302935
if (wc_AesSetKey((Aes*)aes, key, ((bits)/8), NULL, enc) != 0) {
29312936
WOLFSSL_MSG("Error in setting AES key");
29322937
return -1;

tests/api.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18165,6 +18165,7 @@ static int test_wc_GmacUpdate(void)
1816518165

1816618166
#ifdef WOLFSSL_AES_192
1816718167
ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
18168+
ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
1816818169
ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
1816918170
ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
1817018171
tagOut2, sizeof(tag2)), 0);
@@ -18173,6 +18174,7 @@ static int test_wc_GmacUpdate(void)
1817318174

1817418175
#ifdef WOLFSSL_AES_256
1817518176
ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
18177+
ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
1817618178
ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
1817718179
ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
1817818180
tagOut3, sizeof(tag3)), 0);

wolfcrypt/benchmark/benchmark.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4521,6 +4521,12 @@ static void bench_aescfb_internal(const byte* key,
45214521
int i, ret, count;
45224522
DECLARE_MULTI_VALUE_STATS_VARS()
45234523

4524+
ret = wc_AesInit(&enc, HEAP_HINT, INVALID_DEVID);
4525+
if (ret != 0) {
4526+
printf("AesInit failed, ret = %d\n", ret);
4527+
return;
4528+
}
4529+
45244530
ret = wc_AesSetKey(&enc, key, keySz, iv, AES_ENCRYPTION);
45254531
if (ret != 0) {
45264532
printf("AesSetKey failed, ret = %d\n", ret);

wolfcrypt/src/cmac.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,19 @@ int wc_InitCmac_ex(Cmac* cmac, const byte* key, word32 keySz,
137137
return BAD_FUNC_ARG;
138138
}
139139

140+
ret = wc_AesInit(&cmac->aes, heap, devId);
141+
140142
#if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_CRYPT)
141143
cmac->useSWCrypt = useSW;
142144
if (cmac->useSWCrypt == 1) {
143145
cmac->aes.useSWCrypt = 1;
144146
}
145147
#endif
146148

147-
ret = wc_AesSetKey(&cmac->aes, key, keySz, NULL, AES_ENCRYPTION);
149+
if (ret == 0) {
150+
ret = wc_AesSetKey(&cmac->aes, key, keySz, NULL, AES_ENCRYPTION);
151+
}
152+
148153
if (ret == 0) {
149154
byte l[AES_BLOCK_SIZE];
150155

wolfcrypt/src/evp.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6762,6 +6762,10 @@ void wolfSSL_EVP_init(void)
67626762
if (enc == 0 || enc == 1)
67636763
ctx->enc = enc ? 1 : 0;
67646764
if (key) {
6765+
ret = wc_AesInit(&ctx->cipher.aes, NULL, INVALID_DEVID);
6766+
if (ret != 0)
6767+
return WOLFSSL_FAILURE;
6768+
67656769
ret = AesSetKey_ex(&ctx->cipher.aes, key, (word32)ctx->keyLen,
67666770
iv, ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION, 0);
67676771
if (ret != 0)
@@ -7034,6 +7038,10 @@ void wolfSSL_EVP_init(void)
70347038
if (enc == 0 || enc == 1)
70357039
ctx->enc = enc ? 1 : 0;
70367040
if (key) {
7041+
ret = wc_AesInit(&ctx->cipher.aes, NULL, INVALID_DEVID);
7042+
if (ret != 0)
7043+
return WOLFSSL_FAILURE;
7044+
70377045
ret = AesSetKey_ex(&ctx->cipher.aes, key, (word32)ctx->keyLen,
70387046
iv, AES_ENCRYPTION, 0);
70397047
if (ret != 0)
@@ -7058,6 +7066,10 @@ void wolfSSL_EVP_init(void)
70587066
if (enc == 0 || enc == 1)
70597067
ctx->enc = enc ? 1 : 0;
70607068
if (key) {
7069+
ret = wc_AesInit(&ctx->cipher.aes, NULL, INVALID_DEVID);
7070+
if (ret != 0)
7071+
return WOLFSSL_FAILURE;
7072+
70617073
ret = AesSetKey_ex(&ctx->cipher.aes, key, (word32)ctx->keyLen,
70627074
iv, AES_ENCRYPTION, 0);
70637075
if (ret != 0)
@@ -7082,6 +7094,10 @@ void wolfSSL_EVP_init(void)
70827094
if (enc == 0 || enc == 1)
70837095
ctx->enc = enc ? 1 : 0;
70847096
if (key) {
7097+
ret = wc_AesInit(&ctx->cipher.aes, NULL, INVALID_DEVID);
7098+
if (ret != 0)
7099+
return WOLFSSL_FAILURE;
7100+
70857101
ret = AesSetKey_ex(&ctx->cipher.aes, key, (word32)ctx->keyLen,
70867102
iv, AES_ENCRYPTION, 0);
70877103
if (ret != 0){
@@ -7110,6 +7126,10 @@ void wolfSSL_EVP_init(void)
71107126
if (enc == 0 || enc == 1)
71117127
ctx->enc = enc ? 1 : 0;
71127128
if (key) {
7129+
ret = wc_AesInit(&ctx->cipher.aes, NULL, INVALID_DEVID);
7130+
if (ret != 0)
7131+
return WOLFSSL_FAILURE;
7132+
71137133
ret = AesSetKey_ex(&ctx->cipher.aes, key, (word32)ctx->keyLen,
71147134
iv, AES_ENCRYPTION, 0);
71157135
if (ret != 0)
@@ -7134,6 +7154,10 @@ void wolfSSL_EVP_init(void)
71347154
if (enc == 0 || enc == 1)
71357155
ctx->enc = enc ? 1 : 0;
71367156
if (key) {
7157+
ret = wc_AesInit(&ctx->cipher.aes, NULL, INVALID_DEVID);
7158+
if (ret != 0)
7159+
return WOLFSSL_FAILURE;
7160+
71377161
ret = AesSetKey_ex(&ctx->cipher.aes, key, (word32)ctx->keyLen,
71387162
iv, AES_ENCRYPTION, 0);
71397163
if (ret != 0)
@@ -7158,6 +7182,10 @@ void wolfSSL_EVP_init(void)
71587182
if (enc == 0 || enc == 1)
71597183
ctx->enc = enc ? 1 : 0;
71607184
if (key) {
7185+
ret = wc_AesInit(&ctx->cipher.aes, NULL, INVALID_DEVID);
7186+
if (ret != 0)
7187+
return WOLFSSL_FAILURE;
7188+
71617189
ret = AesSetKey_ex(&ctx->cipher.aes, key, (word32)ctx->keyLen,
71627190
iv, AES_ENCRYPTION, 0);
71637191
if (ret != 0){
@@ -7186,6 +7214,10 @@ void wolfSSL_EVP_init(void)
71867214
if (enc == 0 || enc == 1)
71877215
ctx->enc = enc ? 1 : 0;
71887216
if (key) {
7217+
ret = wc_AesInit(&ctx->cipher.aes, NULL, INVALID_DEVID);
7218+
if (ret != 0)
7219+
return WOLFSSL_FAILURE;
7220+
71897221
ret = AesSetKey_ex(&ctx->cipher.aes, key, (word32)ctx->keyLen,
71907222
iv, AES_ENCRYPTION, 0);
71917223
if (ret != 0)
@@ -7210,6 +7242,10 @@ void wolfSSL_EVP_init(void)
72107242
if (enc == 0 || enc == 1)
72117243
ctx->enc = enc ? 1 : 0;
72127244
if (key) {
7245+
ret = wc_AesInit(&ctx->cipher.aes, NULL, INVALID_DEVID);
7246+
if (ret != 0)
7247+
return WOLFSSL_FAILURE;
7248+
72137249
ret = AesSetKey_ex(&ctx->cipher.aes, key, (word32)ctx->keyLen,
72147250
iv, AES_ENCRYPTION, 0);
72157251
if (ret != 0)
@@ -7234,6 +7270,10 @@ void wolfSSL_EVP_init(void)
72347270
if (enc == 0 || enc == 1)
72357271
ctx->enc = enc ? 1 : 0;
72367272
if (key) {
7273+
ret = wc_AesInit(&ctx->cipher.aes, NULL, INVALID_DEVID);
7274+
if (ret != 0)
7275+
return WOLFSSL_FAILURE;
7276+
72377277
ret = AesSetKey_ex(&ctx->cipher.aes, key, (word32)ctx->keyLen,
72387278
iv, AES_ENCRYPTION, 0);
72397279
if (ret != 0){
@@ -7264,6 +7304,10 @@ void wolfSSL_EVP_init(void)
72647304
if (enc == 0 || enc == 1)
72657305
ctx->enc = enc ? 1 : 0;
72667306
if (key) {
7307+
ret = wc_AesInit(&ctx->cipher.aes, NULL, INVALID_DEVID);
7308+
if (ret != 0)
7309+
return WOLFSSL_FAILURE;
7310+
72677311
ret = AesSetKey_ex(&ctx->cipher.aes, key, (word32)ctx->keyLen,
72687312
iv, AES_ENCRYPTION, 0);
72697313
if (ret != 0)
@@ -7288,6 +7332,10 @@ void wolfSSL_EVP_init(void)
72887332
if (enc == 0 || enc == 1)
72897333
ctx->enc = enc ? 1 : 0;
72907334
if (key) {
7335+
ret = wc_AesInit(&ctx->cipher.aes, NULL, INVALID_DEVID);
7336+
if (ret != 0)
7337+
return WOLFSSL_FAILURE;
7338+
72917339
ret = AesSetKey_ex(&ctx->cipher.aes, key, (word32)ctx->keyLen,
72927340
iv, AES_ENCRYPTION, 0);
72937341
if (ret != 0)
@@ -7312,6 +7360,10 @@ void wolfSSL_EVP_init(void)
73127360
if (enc == 0 || enc == 1)
73137361
ctx->enc = enc ? 1 : 0;
73147362
if (key) {
7363+
ret = wc_AesInit(&ctx->cipher.aes, NULL, INVALID_DEVID);
7364+
if (ret != 0)
7365+
return WOLFSSL_FAILURE;
7366+
73157367
ret = AesSetKey_ex(&ctx->cipher.aes, key, (word32)ctx->keyLen,
73167368
iv, AES_ENCRYPTION, 0);
73177369
if (ret != 0){

wolfcrypt/test/test.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8163,6 +8163,14 @@ static wc_test_ret_t EVP_test(const WOLFSSL_EVP_CIPHER* type, const byte* key,
81638163
}
81648164
#endif
81658165

8166+
ret = wc_AesInit(enc, HEAP_HINT, INVALID_DEVID);
8167+
if (ret != 0)
8168+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
8169+
8170+
ret = wc_AesInit(dec, HEAP_HINT, INVALID_DEVID);
8171+
if (ret != 0)
8172+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
8173+
81668174
ret = wc_AesSetKey(enc, key2, sizeof(key2), iv2, AES_ENCRYPTION);
81678175
if (ret != 0)
81688176
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
@@ -13564,6 +13572,10 @@ static wc_test_ret_t aesccm_128_test(void)
1356413572
XMEMSET(p2, 0, sizeof(p2));
1356513573
XMEMSET(iv2, 0, sizeof(iv2));
1356613574

13575+
ret = wc_AesInit(enc, HEAP_HINT, devId);
13576+
if (ret != 0)
13577+
ERROR_OUT(WC_TEST_RET_ENC_EC(ret), out);
13578+
1356713579
#ifndef HAVE_SELFTEST
1356813580
/* selftest build does not have wc_AesCcmSetNonce() or
1356913581
* wc_AesCcmEncrypt_ex() */

0 commit comments

Comments
 (0)