Skip to content

Commit 41591e7

Browse files
committed
Fixes for TSIP AES CTR unit tests and handling of invalid cases.
1 parent 191165a commit 41591e7

2 files changed

Lines changed: 40 additions & 42 deletions

File tree

IDE/Renesas/e2studio/RX72N/EnvisionKit/wolfssl_demo/wolfssl_tsip_unit_test.c

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,6 @@ static int tsip_aes128_ctr_test(int prnt, tsip_aes_key_index_t* aes_key)
226226
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
227227
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
228228
};
229-
WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
230-
{
231-
0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
232-
0xc2
233-
};
234-
235229
WOLFSSL_SMALL_STACK_STATIC const byte ctr128Key[] =
236230
{
237231
0x2b,0x7e,0x15,0x16,0x28,0xae,0xd2,0xa6,
@@ -249,13 +243,6 @@ static int tsip_aes128_ctr_test(int prnt, tsip_aes_key_index_t* aes_key)
249243
0x1e,0x03,0x1d,0xda,0x2f,0xbe,0x03,0xd1,
250244
0x79,0x21,0x70,0xa0,0xf3,0x00,0x9c,0xee
251245
};
252-
253-
WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128Cipher[] =
254-
{
255-
0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
256-
0xe0,0x0d,0x02,0x0b,0x4c,0xed,0xbd,0x86,
257-
0xd3,0xda,0xe1,0x5b,0x04
258-
};
259246
WOLFSSL_SMALL_STACK_STATIC const byte ctr128Wrap128CipherLong[] =
260247
{
261248
0xe1,0x33,0x38,0xe3,0x6c,0xb7,0x19,0x62,
@@ -274,14 +261,6 @@ static int tsip_aes128_ctr_test(int prnt, tsip_aes_key_index_t* aes_key)
274261
} testVec[] = {
275262
{ ctr128Key, (int)sizeof(ctr128Key), ctrIv,
276263
ctrPlain, (int)sizeof(ctrPlain), ctr128Cipher },
277-
/* let's try with just 9 bytes, non block size test */
278-
{ ctr128Key, (int)sizeof(ctr128Key), ctrIv,
279-
ctrPlain, (int)sizeof(oddCipher), ctr128Cipher },
280-
/* and an additional 9 bytes to reuse tmp left buffer */
281-
{ NULL, 0, NULL, ctrPlain, (int)sizeof(oddCipher), oddCipher },
282-
/* Counter wrapping */
283-
{ ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
284-
ctrPlain, (int)sizeof(ctr128Wrap128Cipher), ctr128Wrap128Cipher },
285264
{ ctr128Key, (int)sizeof(ctr128Key), ctrIvWrap128,
286265
ctrPlain, (int)sizeof(ctr128Wrap128CipherLong),
287266
ctr128Wrap128CipherLong },
@@ -302,6 +281,22 @@ static int tsip_aes128_ctr_test(int prnt, tsip_aes_key_index_t* aes_key)
302281
goto out;
303282
}
304283

284+
/* test failure cases - null */
285+
ret = wc_tsip_AesCtr(NULL, NULL, NULL, 1);
286+
if (ret != BAD_FUNC_ARG) {
287+
ret = -8; goto out;
288+
}
289+
/* test failure cases - size, but no buffer */
290+
ret = wc_tsip_AesCtr(enc, NULL, NULL, 1);
291+
if (ret != BAD_FUNC_ARG) {
292+
ret = -9; goto out;
293+
}
294+
/* test failure cases - non block aligned */
295+
ret = wc_tsip_AesCtr(enc, plain, cipher, 15);
296+
if (ret != BAD_FUNC_ARG) {
297+
ret = -10; goto out;
298+
}
299+
305300
for (i = 0; i < AES_CTR_TEST_LEN; i++) {
306301
if (testVec[i].key != NULL) {
307302
ret = wc_AesSetKeyDirect(enc, testVec[i].key, (word32)testVec[i].keySz,
@@ -386,11 +381,6 @@ static int tsip_aes256_ctr_test(int prnt, tsip_aes_key_index_t* aes_key)
386381
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
387382
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
388383
};
389-
WOLFSSL_SMALL_STACK_STATIC const byte oddCipher[] =
390-
{
391-
0xb9,0xd7,0xcb,0x08,0xb0,0xe1,0x7b,0xa0,
392-
0xc2
393-
};
394384
WOLFSSL_SMALL_STACK_STATIC const byte ctr256Key[] =
395385
{
396386
0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
@@ -410,12 +400,6 @@ static int tsip_aes256_ctr_test(int prnt, tsip_aes_key_index_t* aes_key)
410400
0xdf,0xc9,0xc5,0x8d,0xb6,0x7a,0xad,0xa6,
411401
0x13,0xc2,0xdd,0x08,0x45,0x79,0x41,0xa6
412402
};
413-
WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128Cipher[] =
414-
{
415-
0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
416-
0x73,0xfb,0x78,0xdf,0x1e,0x8e,0x77,0xe6,
417-
0x4b,0x45,0x7c,0xd6,0x8a
418-
};
419403
WOLFSSL_SMALL_STACK_STATIC const byte ctr256Wrap128CipherLong[] =
420404
{
421405
0x50,0xfd,0x97,0xc3,0xe6,0x1a,0xbb,0x48,
@@ -435,12 +419,6 @@ static int tsip_aes256_ctr_test(int prnt, tsip_aes_key_index_t* aes_key)
435419
} testVec[] = {
436420
{ ctr256Key, (int)sizeof(ctr256Key), ctrIv,
437421
ctrPlain, (int)sizeof(ctrPlain), ctr256Cipher },
438-
/* let's try with just 9 bytes, non block size test */
439-
{ ctr256Key, (int)sizeof(ctr256Key), ctrIv,
440-
ctrPlain, (int)sizeof(oddCipher), ctr256Cipher },
441-
/* Counter wrapping */
442-
{ ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
443-
ctrPlain, (int)sizeof(ctr256Wrap128Cipher), ctr256Wrap128Cipher },
444422
{ ctr256Key, (int)sizeof(ctr256Key), ctrIvWrap128,
445423
ctrPlain, (int)sizeof(ctr256Wrap128CipherLong),
446424
ctr256Wrap128CipherLong },
@@ -461,6 +439,22 @@ static int tsip_aes256_ctr_test(int prnt, tsip_aes_key_index_t* aes_key)
461439
goto out;
462440
}
463441

442+
/* test failure cases - null */
443+
ret = wc_tsip_AesCtr(NULL, NULL, NULL, 1);
444+
if (ret != BAD_FUNC_ARG) {
445+
ret = -8; goto out;
446+
}
447+
/* test failure cases - size, but no buffer */
448+
ret = wc_tsip_AesCtr(enc, NULL, NULL, 1);
449+
if (ret != BAD_FUNC_ARG) {
450+
ret = -9; goto out;
451+
}
452+
/* test failure cases - non block aligned */
453+
ret = wc_tsip_AesCtr(enc, plain, cipher, 15);
454+
if (ret != BAD_FUNC_ARG) {
455+
ret = -10; goto out;
456+
}
457+
464458
for (i = 0; i < AES_CTR_TEST_LEN; i++) {
465459
if (testVec[i].key != NULL) {
466460
ret = wc_AesSetKeyDirect(enc, testVec[i].key, (word32)testVec[i].keySz,

wolfcrypt/src/port/Renesas/renesas_tsip_aes.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -606,13 +606,17 @@ int wc_tsip_AesCbcDecrypt(struct Aes* aes, byte* out, const byte* in, word32 sz)
606606
#endif /* HAVE_AES_CBC */
607607

608608
#ifdef WOLFSSL_AES_COUNTER
609+
/* API only supports even blocks (16 byte) */
610+
/* Use the public wc_AesCtrEncrypt with crypto callbacks to handle odd remain */
609611
int wc_tsip_AesCtr(struct Aes* aes, byte* out, const byte* in, word32 sz)
610612
{
611613
tsip_aes_handle_t _handle;
612614
int ret;
615+
int blocks = (int)(sz / WC_AES_BLOCK_SIZE);
616+
int remain = (int)(sz % WC_AES_BLOCK_SIZE);
613617
byte *iv;
614618

615-
if ((in == NULL) || (out == NULL) || (aes == NULL)) {
619+
if (aes == NULL || in == NULL || out == NULL || sz == 0 || remain != 0) {
616620
return BAD_FUNC_ARG;
617621
}
618622

@@ -630,7 +634,7 @@ int wc_tsip_AesCtr(struct Aes* aes, byte* out, const byte* in, word32 sz)
630634
ret = R_TSIP_Aes128CtrInit(&_handle, &aes->ctx.tsip_keyIdx, iv);
631635
if (ret == TSIP_SUCCESS) {
632636
ret = R_TSIP_Aes128CtrUpdate(&_handle, (uint8_t*)in,
633-
(uint8_t*)out, sz);
637+
(uint8_t*)out, blocks * WC_AES_BLOCK_SIZE);
634638
if (ret == TSIP_SUCCESS) {
635639
ret = R_TSIP_Aes128CtrFinal(&_handle);
636640
}
@@ -644,7 +648,7 @@ int wc_tsip_AesCtr(struct Aes* aes, byte* out, const byte* in, word32 sz)
644648
ret = R_TSIP_Aes256CtrInit(&_handle, &aes->ctx.tsip_keyIdx, iv);
645649
if (ret == TSIP_SUCCESS) {
646650
ret = R_TSIP_Aes256CtrUpdate(&_handle, (uint8_t*)in,
647-
(uint8_t*)out, sz);
651+
(uint8_t*)out, blocks * WC_AES_BLOCK_SIZE);
648652
if (ret == TSIP_SUCCESS) {
649653
ret = R_TSIP_Aes256CtrFinal(&_handle);
650654
}
@@ -656,9 +660,9 @@ int wc_tsip_AesCtr(struct Aes* aes, byte* out, const byte* in, word32 sz)
656660

657661
if (ret == TSIP_SUCCESS) {
658662
/* increment IV counter */
659-
int i, blocks = (int)(sz / WC_AES_BLOCK_SIZE);
660663
while (blocks--) {
661664
/* in network byte order so start at end and work back */
665+
int i;
662666
for (i = WC_AES_BLOCK_SIZE - 1; i >= 0; i--) {
663667
if (++iv[i]) /* we're done unless we overflow */
664668
break;

0 commit comments

Comments
 (0)