mirror of
https://github.com/espressif/ESP8266_RTOS_SDK.git
synced 2025-08-06 07:00:05 +08:00
feat(util): add AES OFB calculation
This commit is contained in:
@ -265,6 +265,20 @@ int esp_aes_xts_set_decrypt_key(esp_aes_xts_t *aes, const void *p_key, size_t ke
|
||||
*/
|
||||
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);
|
||||
|
||||
/**
|
||||
* @brief AES OFB encrypt/decrypt calculation
|
||||
*
|
||||
* @param aes AES contex pointer
|
||||
* @param length data length by bytes
|
||||
* @param iv_off IV offset
|
||||
* @param p_iv IV data buffer
|
||||
* @param p_src input data buffer
|
||||
* @param p_dst output data buffer
|
||||
*
|
||||
* @return 0 if success or fail
|
||||
*/
|
||||
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);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -1006,3 +1006,40 @@ int esp_aes_crypt_xts(esp_aes_xts_t *ctx,
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
size_t n;
|
||||
uint8_t *iv = (uint8_t *)p_iv;
|
||||
const uint8_t *input = (const uint8_t *)p_src;
|
||||
uint8_t *output = (uint8_t *)p_dst;
|
||||
|
||||
util_assert(ctx != NULL);
|
||||
util_assert(iv_off != NULL);
|
||||
util_assert(iv != NULL);
|
||||
util_assert(input != NULL);
|
||||
util_assert(output != NULL);
|
||||
|
||||
n = *iv_off;
|
||||
|
||||
if (n > 15)
|
||||
return -EINVAL;
|
||||
|
||||
while (length--) {
|
||||
if (n == 0) {
|
||||
esp_aes_encrypt(ctx, iv, 16, iv, 16);
|
||||
}
|
||||
*output++ = *input++ ^ iv[n];
|
||||
|
||||
n = (n + 1) & 0x0F;
|
||||
}
|
||||
|
||||
*iv_off = n;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -201,6 +201,82 @@ static const uint8_t aes_test_xts_data_unit[][16] =
|
||||
},
|
||||
};
|
||||
|
||||
static const unsigned char aes_test_ofb_key[3][32] =
|
||||
{
|
||||
{
|
||||
0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
|
||||
0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
|
||||
},
|
||||
|
||||
{
|
||||
0x8E, 0x73, 0xB0, 0xF7, 0xDA, 0x0E, 0x64, 0x52,
|
||||
0xC8, 0x10, 0xF3, 0x2B, 0x80, 0x90, 0x79, 0xE5,
|
||||
0x62, 0xF8, 0xEA, 0xD2, 0x52, 0x2C, 0x6B, 0x7B
|
||||
},
|
||||
|
||||
{
|
||||
0x60, 0x3D, 0xEB, 0x10, 0x15, 0xCA, 0x71, 0xBE,
|
||||
0x2B, 0x73, 0xAE, 0xF0, 0x85, 0x7D, 0x77, 0x81,
|
||||
0x1F, 0x35, 0x2C, 0x07, 0x3B, 0x61, 0x08, 0xD7,
|
||||
0x2D, 0x98, 0x10, 0xA3, 0x09, 0x14, 0xDF, 0xF4
|
||||
}
|
||||
};
|
||||
|
||||
static const unsigned char aes_test_ofb_iv[16] =
|
||||
{
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
|
||||
};
|
||||
|
||||
static const unsigned char aes_test_ofb_pt[64] =
|
||||
{
|
||||
0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
|
||||
0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A,
|
||||
0xAE, 0x2D, 0x8A, 0x57, 0x1E, 0x03, 0xAC, 0x9C,
|
||||
0x9E, 0xB7, 0x6F, 0xAC, 0x45, 0xAF, 0x8E, 0x51,
|
||||
0x30, 0xC8, 0x1C, 0x46, 0xA3, 0x5C, 0xE4, 0x11,
|
||||
0xE5, 0xFB, 0xC1, 0x19, 0x1A, 0x0A, 0x52, 0xEF,
|
||||
0xF6, 0x9F, 0x24, 0x45, 0xDF, 0x4F, 0x9B, 0x17,
|
||||
0xAD, 0x2B, 0x41, 0x7B, 0xE6, 0x6C, 0x37, 0x10
|
||||
};
|
||||
|
||||
static const unsigned char aes_test_ofb_ct[3][64] =
|
||||
{
|
||||
{
|
||||
0x3B, 0x3F, 0xD9, 0x2E, 0xB7, 0x2D, 0xAD, 0x20,
|
||||
0x33, 0x34, 0x49, 0xF8, 0xE8, 0x3C, 0xFB, 0x4A,
|
||||
0x77, 0x89, 0x50, 0x8d, 0x16, 0x91, 0x8f, 0x03,
|
||||
0xf5, 0x3c, 0x52, 0xda, 0xc5, 0x4e, 0xd8, 0x25,
|
||||
0x97, 0x40, 0x05, 0x1e, 0x9c, 0x5f, 0xec, 0xf6,
|
||||
0x43, 0x44, 0xf7, 0xa8, 0x22, 0x60, 0xed, 0xcc,
|
||||
0x30, 0x4c, 0x65, 0x28, 0xf6, 0x59, 0xc7, 0x78,
|
||||
0x66, 0xa5, 0x10, 0xd9, 0xc1, 0xd6, 0xae, 0x5e
|
||||
},
|
||||
|
||||
{
|
||||
0xCD, 0xC8, 0x0D, 0x6F, 0xDD, 0xF1, 0x8C, 0xAB,
|
||||
0x34, 0xC2, 0x59, 0x09, 0xC9, 0x9A, 0x41, 0x74,
|
||||
0xfc, 0xc2, 0x8b, 0x8d, 0x4c, 0x63, 0x83, 0x7c,
|
||||
0x09, 0xe8, 0x17, 0x00, 0xc1, 0x10, 0x04, 0x01,
|
||||
0x8d, 0x9a, 0x9a, 0xea, 0xc0, 0xf6, 0x59, 0x6f,
|
||||
0x55, 0x9c, 0x6d, 0x4d, 0xaf, 0x59, 0xa5, 0xf2,
|
||||
0x6d, 0x9f, 0x20, 0x08, 0x57, 0xca, 0x6c, 0x3e,
|
||||
0x9c, 0xac, 0x52, 0x4b, 0xd9, 0xac, 0xc9, 0x2a
|
||||
},
|
||||
|
||||
{
|
||||
0xDC, 0x7E, 0x84, 0xBF, 0xDA, 0x79, 0x16, 0x4B,
|
||||
0x7E, 0xCD, 0x84, 0x86, 0x98, 0x5D, 0x38, 0x60,
|
||||
0x4f, 0xeb, 0xdc, 0x67, 0x40, 0xd2, 0x0b, 0x3a,
|
||||
0xc8, 0x8f, 0x6a, 0xd8, 0x2a, 0x4f, 0xb0, 0x8d,
|
||||
0x71, 0xab, 0x47, 0xa0, 0x86, 0xe8, 0x6e, 0xed,
|
||||
0xf3, 0x9d, 0x1c, 0x5b, 0xba, 0x97, 0xc4, 0x08,
|
||||
0x01, 0x26, 0x14, 0x1d, 0x67, 0xf3, 0x7b, 0xe8,
|
||||
0x53, 0x8f, 0x5a, 0x8b, 0xe7, 0x40, 0xe4, 0x84
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
TEST_CASE("Test AES-ECB", "[AES]")
|
||||
{
|
||||
uint32_t buf[4];
|
||||
@ -587,3 +663,59 @@ TEST_CASE("Test AES-XTS", "[AES]")
|
||||
|
||||
// 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
|
||||
// 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
|
||||
|
||||
|
||||
|
||||
TEST_CASE("Test AES-OFB", "[AES]")
|
||||
{
|
||||
uint32_t encode_time = 0, decode_time = 0;
|
||||
const int num_tests = sizeof(aes_test_ofb_ct) / sizeof(*aes_test_ofb_ct);
|
||||
extern uint32_t esp_get_time(void);
|
||||
|
||||
for (int cnt = 0; cnt < TEST_AES_COUNT; cnt++) {
|
||||
for (int i = 0; i < num_tests; i++) {
|
||||
uint8_t buf[64];
|
||||
uint8_t key[32];
|
||||
uint8_t iv[16];
|
||||
esp_aes_t ctx;
|
||||
size_t keybits;
|
||||
size_t offset;
|
||||
|
||||
offset = 0;
|
||||
keybits = i * 64 + 128;
|
||||
memcpy(iv, aes_test_ofb_iv, 16);
|
||||
memcpy(key, aes_test_ofb_key[i], keybits / 8);
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_set_encrypt_key(&ctx, key, keybits) == 0);
|
||||
memcpy(buf, aes_test_ofb_ct[i], 64);
|
||||
|
||||
uint32_t tmp = esp_get_time();
|
||||
TEST_ASSERT_TRUE(esp_aes_crypt_ofb(&ctx, 64, &offset, iv, buf, buf) == 0);
|
||||
encode_time += esp_get_time() - tmp;
|
||||
|
||||
TEST_ASSERT_TRUE(memcmp(buf, aes_test_ofb_pt, 64) == 0);
|
||||
|
||||
offset = 0;
|
||||
keybits = i * 64 + 128;
|
||||
memcpy(iv, aes_test_ofb_iv, 16);
|
||||
memcpy(key, aes_test_ofb_key[i], keybits / 8);
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_set_encrypt_key(&ctx, key, keybits) == 0);
|
||||
memcpy(buf, aes_test_ofb_pt, 64);
|
||||
|
||||
tmp = esp_get_time();
|
||||
TEST_ASSERT_TRUE(esp_aes_crypt_ofb(&ctx, 64, &offset, iv, buf, buf) == 0);
|
||||
decode_time += esp_get_time() - tmp;
|
||||
|
||||
TEST_ASSERT_TRUE(memcmp(buf, aes_test_ofb_ct[i], 64) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
#if TEST_AES_DEBUG_TIME
|
||||
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",
|
||||
encode_time, decode_time, encode_time / TEST_AES_COUNT, decode_time / TEST_AES_COUNT);
|
||||
#endif
|
||||
}
|
||||
|
||||
// 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
|
||||
// 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
|
||||
|
Reference in New Issue
Block a user