Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ca3e701

Browse files
donghengdongheng
authored andcommitted
feat(util): add AES OFB calculation
1 parent d8d355b commit ca3e701

File tree

3 files changed

+183
-0
lines changed

3 files changed

+183
-0
lines changed

components/util/include/esp_aes.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,20 @@ int esp_aes_xts_set_decrypt_key(esp_aes_xts_t *aes, const void *p_key, size_t ke
265265
*/
266266
int esp_aes_crypt_xts(esp_aes_xts_t *aes, int encrypt, size_t length, const void *p_data_unit, const void *p_src, void *p_dst);
267267

268+
/**
269+
* @brief AES OFB encrypt/decrypt calculation
270+
*
271+
* @param aes AES contex pointer
272+
* @param length data length by bytes
273+
* @param iv_off IV offset
274+
* @param p_iv IV data buffer
275+
* @param p_src input data buffer
276+
* @param p_dst output data buffer
277+
*
278+
* @return 0 if success or fail
279+
*/
280+
int esp_aes_crypt_ofb(esp_aes_t *ctx, size_t length, size_t *iv_off, void *p_iv, const void *p_src, void *p_dst);
281+
268282
#ifdef __cplusplus
269283
}
270284
#endif

components/util/src/aes.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,3 +1006,40 @@ int esp_aes_crypt_xts(esp_aes_xts_t *ctx,
10061006

10071007
return 0;
10081008
}
1009+
1010+
int esp_aes_crypt_ofb(esp_aes_t *ctx,
1011+
size_t length,
1012+
size_t *iv_off,
1013+
void *p_iv,
1014+
const void *p_src,
1015+
void *p_dst)
1016+
{
1017+
size_t n;
1018+
uint8_t *iv = (uint8_t *)p_iv;
1019+
const uint8_t *input = (const uint8_t *)p_src;
1020+
uint8_t *output = (uint8_t *)p_dst;
1021+
1022+
util_assert(ctx != NULL);
1023+
util_assert(iv_off != NULL);
1024+
util_assert(iv != NULL);
1025+
util_assert(input != NULL);
1026+
util_assert(output != NULL);
1027+
1028+
n = *iv_off;
1029+
1030+
if (n > 15)
1031+
return -EINVAL;
1032+
1033+
while (length--) {
1034+
if (n == 0) {
1035+
esp_aes_encrypt(ctx, iv, 16, iv, 16);
1036+
}
1037+
*output++ = *input++ ^ iv[n];
1038+
1039+
n = (n + 1) & 0x0F;
1040+
}
1041+
1042+
*iv_off = n;
1043+
1044+
return 0;
1045+
}

components/util/test/test_aes.c

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,82 @@ static const uint8_t aes_test_xts_data_unit[][16] =
201201
},
202202
};
203203

204+
static const unsigned char aes_test_ofb_key[3][32] =
205+
{
206+
{
207+
0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
208+
0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
209+
},
210+
211+
{
212+
0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
213+
0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
214+
0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
215+
},
216+
217+
{
218+
0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
219+
0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
220+
0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
221+
0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
222+
}
223+
};
224+
225+
static const unsigned char aes_test_ofb_iv[16] =
226+
{
227+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
228+
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
229+
};
230+
231+
static const unsigned char aes_test_ofb_pt[64] =
232+
{
233+
0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
234+
0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A,
235+
0xAE, 0x2D, 0x8A, 0x57, 0x1E, 0x03, 0xAC, 0x9C,
236+
0x9E, 0xB7, 0x6F, 0xAC, 0x45, 0xAF, 0x8E, 0x51,
237+
0x30, 0xC8, 0x1C, 0x46, 0xA3, 0x5C, 0xE4, 0x11,
238+
0xE5, 0xFB, 0xC1, 0x19, 0x1A, 0x0A, 0x52, 0xEF,
239+
0xF6, 0x9F, 0x24, 0x45, 0xDF, 0x4F, 0x9B, 0x17,
240+
0xAD, 0x2B, 0x41, 0x7B, 0xE6, 0x6C, 0x37, 0x10
241+
};
242+
243+
static const unsigned char aes_test_ofb_ct[3][64] =
244+
{
245+
{
246+
0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20,
247+
0x33, 0x34, 0x49, 0xF8, 0xE8, 0x3C, 0xFB, 0x4A,
248+
0x77, 0x89, 0x50, 0x8d, 0x16, 0x91, 0x8f, 0x03,
249+
0xf5, 0x3c, 0x52, 0xda, 0xc5, 0x4e, 0xd8, 0x25,
250+
0x97, 0x40, 0x05, 0x1e, 0x9c, 0x5f, 0xec, 0xf6,
251+
0x43, 0x44, 0xf7, 0xa8, 0x22, 0x60, 0xed, 0xcc,
252+
0x30, 0x4c, 0x65, 0x28, 0xf6, 0x59, 0xc7, 0x78,
253+
0x66, 0xa5, 0x10, 0xd9, 0xc1, 0xd6, 0xae, 0x5e
254+
},
255+
256+
{
257+
0xCD, 0xC8, 0x0D, 0x6F, 0xDD, 0xF1, 0x8C, 0xAB,
258+
0x34, 0xC2, 0x59, 0x09, 0xC9, 0x9A, 0x41, 0x74,
259+
0xfc, 0xc2, 0x8b, 0x8d, 0x4c, 0x63, 0x83, 0x7c,
260+
0x09, 0xe8, 0x17, 0x00, 0xc1, 0x10, 0x04, 0x01,
261+
0x8d, 0x9a, 0x9a, 0xea, 0xc0, 0xf6, 0x59, 0x6f,
262+
0x55, 0x9c, 0x6d, 0x4d, 0xaf, 0x59, 0xa5, 0xf2,
263+
0x6d, 0x9f, 0x20, 0x08, 0x57, 0xca, 0x6c, 0x3e,
264+
0x9c, 0xac, 0x52, 0x4b, 0xd9, 0xac, 0xc9, 0x2a
265+
},
266+
267+
{
268+
0xDC, 0x7E, 0x84, 0xBF, 0xDA, 0x79, 0x16, 0x4B,
269+
0x7E, 0xCD, 0x84, 0x86, 0x98, 0x5D, 0x38, 0x60,
270+
0x4f, 0xeb, 0xdc, 0x67, 0x40, 0xd2, 0x0b, 0x3a,
271+
0xc8, 0x8f, 0x6a, 0xd8, 0x2a, 0x4f, 0xb0, 0x8d,
272+
0x71, 0xab, 0x47, 0xa0, 0x86, 0xe8, 0x6e, 0xed,
273+
0xf3, 0x9d, 0x1c, 0x5b, 0xba, 0x97, 0xc4, 0x08,
274+
0x01, 0x26, 0x14, 0x1d, 0x67, 0xf3, 0x7b, 0xe8,
275+
0x53, 0x8f, 0x5a, 0x8b, 0xe7, 0x40, 0xe4, 0x84
276+
}
277+
};
278+
279+
204280
TEST_CASE("Test AES-ECB", "[AES]")
205281
{
206282
uint32_t buf[4];
@@ -587,3 +663,59 @@ TEST_CASE("Test AES-XTS", "[AES]")
587663

588664
// align: AES-XTS test cost time totally encode 723197 us and decode 533441 us, once cost is about encode 706 us and decode 520 us
589665
// no align: AES-XTS test cost time totally encode 675249 us and decode 645998 us, once cost is about encode 659 us and decode 630 us
666+
667+
668+
669+
TEST_CASE("Test AES-OFB", "[AES]")
670+
{
671+
uint32_t encode_time = 0, decode_time = 0;
672+
const int num_tests = sizeof(aes_test_ofb_ct) / sizeof(*aes_test_ofb_ct);
673+
extern uint32_t esp_get_time(void);
674+
675+
for (int cnt = 0; cnt < TEST_AES_COUNT; cnt++) {
676+
for (int i = 0; i < num_tests; i++) {
677+
uint8_t buf[64];
678+
uint8_t key[32];
679+
uint8_t iv[16];
680+
esp_aes_t ctx;
681+
size_t keybits;
682+
size_t offset;
683+
684+
offset = 0;
685+
keybits = i * 64 + 128;
686+
memcpy(iv, aes_test_ofb_iv, 16);
687+
memcpy(key, aes_test_ofb_key[i], keybits / 8);
688+
689+
TEST_ASSERT_TRUE(esp_aes_set_encrypt_key(&ctx, key, keybits) == 0);
690+
memcpy(buf, aes_test_ofb_ct[i], 64);
691+
692+
uint32_t tmp = esp_get_time();
693+
TEST_ASSERT_TRUE(esp_aes_crypt_ofb(&ctx, 64, &offset, iv, buf, buf) == 0);
694+
encode_time += esp_get_time() - tmp;
695+
696+
TEST_ASSERT_TRUE(memcmp(buf, aes_test_ofb_pt, 64) == 0);
697+
698+
offset = 0;
699+
keybits = i * 64 + 128;
700+
memcpy(iv, aes_test_ofb_iv, 16);
701+
memcpy(key, aes_test_ofb_key[i], keybits / 8);
702+
703+
TEST_ASSERT_TRUE(esp_aes_set_encrypt_key(&ctx, key, keybits) == 0);
704+
memcpy(buf, aes_test_ofb_pt, 64);
705+
706+
tmp = esp_get_time();
707+
TEST_ASSERT_TRUE(esp_aes_crypt_ofb(&ctx, 64, &offset, iv, buf, buf) == 0);
708+
decode_time += esp_get_time() - tmp;
709+
710+
TEST_ASSERT_TRUE(memcmp(buf, aes_test_ofb_ct[i], 64) == 0);
711+
}
712+
}
713+
714+
#if TEST_AES_DEBUG_TIME
715+
printf("AES-OFB test cost time totally encode %u us and decode %u us, once cost is about encode %u us and decode %u us\n",
716+
encode_time, decode_time, encode_time / TEST_AES_COUNT, decode_time / TEST_AES_COUNT);
717+
#endif
718+
}
719+
720+
// align: AES-OFB test cost time totally encode 465340 us and decode 455726 us, once cost is about encode 454 us and decode 445 us
721+
// no align: AES-OFB test cost time totally encode 743898 us and decode 736479 us, once cost is about encode 726 us and decode 719 us

0 commit comments

Comments
 (0)