mirror of
https://github.com/espressif/ESP8266_RTOS_SDK.git
synced 2025-05-31 15:41:02 +08:00
feat(mbedtls): remove util algorithm and add them to esp8266 rom and mbedtls
This commit is contained in:
721
components/mbedtls/test/test_aes.c
Normal file
721
components/mbedtls/test/test_aes.c
Normal file
@ -0,0 +1,721 @@
|
||||
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include "esp_aes.h"
|
||||
#include "unity.h"
|
||||
|
||||
#define TEST_AES_COUNT 1
|
||||
#define TEST_AES_DEBUG 0
|
||||
#define TEST_AES_DEBUG_TIME 0
|
||||
|
||||
#if TEST_AES_DEBUG_TIME
|
||||
#undef TEST_ASSERT_TRUE
|
||||
#define TEST_ASSERT_TRUE(__e) ((__e) ? (void)0 : (void)0)
|
||||
#endif
|
||||
|
||||
static const uint32_t s_aes_ecb_result[3][4] = {
|
||||
// keybits = 128
|
||||
{
|
||||
0x34eff23c, 0xb90aab82, 0x5781d455, 0x743e4cef,
|
||||
},
|
||||
|
||||
// keybits = 192
|
||||
{
|
||||
0x8b47f409, 0x2752feb6, 0x8001f471, 0x76f0bb1d,
|
||||
},
|
||||
|
||||
// keybits = 256
|
||||
{
|
||||
0x3d33ac44, 0x709b21d1, 0x7372cd80, 0x6bad3758,
|
||||
}
|
||||
};
|
||||
|
||||
static const uint32_t s_aes_cbc_result[3][8] = {
|
||||
// keybits = 128
|
||||
{
|
||||
0xda3f4f4f, 0x9b96f894, 0xbfbbf671, 0x2da753f9, 0xad8a1a91, 0x958360a3, 0x857d1ae4, 0xda4c89e1,
|
||||
},
|
||||
|
||||
// keybits = 192
|
||||
{
|
||||
0xe92a9715, 0xbe5983de, 0xefce73cf, 0x21ef8004, 0x31499c08, 0xb7753f3d, 0x54afc778, 0xc2a8209f,
|
||||
},
|
||||
|
||||
// keybits = 256
|
||||
{
|
||||
0x2288383b, 0x8e541787, 0x3eb2c691, 0x76ae0dcd, 0x2a6f677a, 0xa1b0118d, 0x150ca144, 0x41def443,
|
||||
}
|
||||
};
|
||||
|
||||
static const uint32_t s_aes_cfb8_result[3][8] = {
|
||||
// keybits = 128
|
||||
{
|
||||
0x6b5dabee, 0x41102894, 0x13e333be, 0x4fd60d9c, 0x2974dde7, 0xcaa73794, 0x1dc3d2d8, 0x3a210c38,
|
||||
},
|
||||
|
||||
// keybits = 192
|
||||
{
|
||||
0xf3e04de7, 0xeca9fd81, 0xebf7ee9e, 0x7b6e7091, 0xa8bde865, 0xc150d7d9, 0xddaf6657, 0x77d6e3f0,
|
||||
},
|
||||
|
||||
// keybits = 256
|
||||
{
|
||||
0xc510918d, 0xac9004da, 0xc23fea19, 0x6914df10, 0xa34d6dd5, 0x9ecc2cc7, 0x9941cd88, 0x1d472bb7,
|
||||
}
|
||||
};
|
||||
|
||||
static const uint32_t s_aes_cfb128_result[3][8] = {
|
||||
// keybits = 128
|
||||
{
|
||||
0xfe2d65ee, 0x2d86806b, 0xea5d2e84, 0x10ebab93, 0x32f9354f, 0x0dcbc189, 0x087d75d8, 0xc456acd3,
|
||||
},
|
||||
|
||||
// keybits = 192
|
||||
{
|
||||
0xfaaf66e7, 0x9c2cbe18, 0xa1f67fe2, 0x5f92ac9e, 0x1432de12, 0x4e115c08, 0x36d6a739, 0x5d8c1bce,
|
||||
},
|
||||
|
||||
// keybits = 256
|
||||
{
|
||||
0x14e2318d, 0x185ac4df, 0xe6efc826, 0x65b796fb, 0x39f9a2e7, 0xffd706f4, 0x7ac79119, 0x47b99a97,
|
||||
}
|
||||
};
|
||||
|
||||
static const uint32_t s_aes_ctr_result[3][8] = {
|
||||
// keybits = 128
|
||||
{
|
||||
0xfe2d65ee, 0x2d86806b, 0xea5d2e84, 0x10ebab93, 0x26a701de, 0x0e9d716f, 0x2d87c066, 0x2ff1f3af,
|
||||
},
|
||||
|
||||
// keybits = 192
|
||||
{
|
||||
0xfaaf66e7, 0x9c2cbe18, 0xa1f67fe2, 0x5f92ac9e, 0xf7e0935e, 0xf0d3b9c1, 0x0f442d3b, 0xf7368de3,
|
||||
},
|
||||
|
||||
// keybits = 256
|
||||
{
|
||||
0x14e2318d, 0x185ac4df, 0xe6efc826, 0x65b796fb, 0x9fef3b32, 0x742db24c, 0x08c56021, 0x5f5c8bf4,
|
||||
}
|
||||
};
|
||||
|
||||
static const uint8_t aes_test_xts_key[][32] = {
|
||||
{
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
},
|
||||
|
||||
{
|
||||
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
|
||||
0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
|
||||
0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
|
||||
0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22
|
||||
},
|
||||
|
||||
{
|
||||
0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8,
|
||||
0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0,
|
||||
0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
|
||||
0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22
|
||||
},
|
||||
};
|
||||
|
||||
static const uint8_t aes_test_xts_pt32[][32] =
|
||||
{
|
||||
{
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
},
|
||||
|
||||
{
|
||||
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
|
||||
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
|
||||
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
|
||||
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44
|
||||
},
|
||||
|
||||
{
|
||||
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
|
||||
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
|
||||
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
|
||||
0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44
|
||||
},
|
||||
};
|
||||
|
||||
static const uint8_t aes_test_xts_ct32[][32] =
|
||||
{
|
||||
{
|
||||
0x91, 0x7c, 0xf6, 0x9e, 0xbd, 0x68, 0xb2, 0xec,
|
||||
0x9b, 0x9f, 0xe9, 0xa3, 0xea, 0xdd, 0xa6, 0x92,
|
||||
0xcd, 0x43, 0xd2, 0xf5, 0x95, 0x98, 0xed, 0x85,
|
||||
0x8c, 0x02, 0xc2, 0x65, 0x2f, 0xbf, 0x92, 0x2e
|
||||
},
|
||||
|
||||
{
|
||||
0xc4, 0x54, 0x18, 0x5e, 0x6a, 0x16, 0x93, 0x6e,
|
||||
0x39, 0x33, 0x40, 0x38, 0xac, 0xef, 0x83, 0x8b,
|
||||
0xfb, 0x18, 0x6f, 0xff, 0x74, 0x80, 0xad, 0xc4,
|
||||
0x28, 0x93, 0x82, 0xec, 0xd6, 0xd3, 0x94, 0xf0
|
||||
},
|
||||
|
||||
{
|
||||
0xaf, 0x85, 0x33, 0x6b, 0x59, 0x7a, 0xfc, 0x1a,
|
||||
0x90, 0x0b, 0x2e, 0xb2, 0x1e, 0xc9, 0x49, 0xd2,
|
||||
0x92, 0xdf, 0x4c, 0x04, 0x7e, 0x0b, 0x21, 0x53,
|
||||
0x21, 0x86, 0xa5, 0x97, 0x1a, 0x22, 0x7a, 0x89
|
||||
},
|
||||
};
|
||||
|
||||
static const uint8_t aes_test_xts_data_unit[][16] =
|
||||
{
|
||||
{
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
},
|
||||
|
||||
{
|
||||
0x33, 0x33, 0x33, 0x33, 0x33, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
},
|
||||
|
||||
{
|
||||
0x33, 0x33, 0x33, 0x33, 0x33, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
},
|
||||
};
|
||||
|
||||
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];
|
||||
uint32_t output[4];
|
||||
uint8_t key[32];
|
||||
esp_aes_t ctx;
|
||||
uint32_t encode_time = 0, decode_time = 0;
|
||||
extern uint32_t esp_get_time(void);
|
||||
|
||||
memset(key, 0x11, sizeof(key));
|
||||
|
||||
for (int cnt = 0; cnt < TEST_AES_COUNT; cnt++) {
|
||||
int index = 0;
|
||||
|
||||
for (int i = 128; i <= 256; i += 64) {
|
||||
memset(buf, 11, sizeof(buf));
|
||||
memset(output, 0, sizeof(output));
|
||||
|
||||
uint32_t tmp = esp_get_time();
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_set_encrypt_key(&ctx, key, i) == 0);
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_encrypt_ecb(&ctx, buf, output) == 0);
|
||||
|
||||
encode_time += esp_get_time() - tmp;
|
||||
|
||||
#if TEST_AES_DEBUG
|
||||
for (int j = 0; j < 4; j++)
|
||||
printf("0x%08x(0x%08x), ", output[j], s_aes_ecb_result[index][j]);
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
TEST_ASSERT_TRUE(memcmp(output, s_aes_ecb_result[index++], sizeof(output)) == 0);
|
||||
|
||||
tmp = esp_get_time();
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_set_decrypt_key(&ctx, key, i) == 0);
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_decrypt_ecb(&ctx, output, buf) == 0);
|
||||
|
||||
decode_time += esp_get_time() - tmp;
|
||||
|
||||
memset(output, 11, sizeof(output));
|
||||
|
||||
#if TEST_AES_DEBUG
|
||||
for (int j = 0; j < 4; j++)
|
||||
printf("0x%08x(0x%08x), ", buf[j], output[j]);
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
TEST_ASSERT_TRUE(memcmp(output, buf, sizeof(output)) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
#if TEST_AES_DEBUG_TIME
|
||||
printf("AES-ECB 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-ECB test cost time totally encode 370305 us and decode 905644 us, once cost is about encode 361 us and decode 884 us
|
||||
// no align: AES-ECB test cost time totally encode 513219 us and decode 1729937 us, once cost is about encode 501 us and decode 1689 us
|
||||
|
||||
TEST_CASE("Test AES-CBC", "[AES]")
|
||||
{
|
||||
uint32_t buf[8];
|
||||
uint32_t output[8];
|
||||
uint8_t key[32];
|
||||
uint8_t iv[16];
|
||||
esp_aes_t ctx;
|
||||
uint32_t encode_time = 0, decode_time = 0;
|
||||
extern uint32_t esp_get_time(void);
|
||||
|
||||
memset(key, 0x11, sizeof(key));
|
||||
|
||||
for (int cnt = 0; cnt < TEST_AES_COUNT; cnt++) {
|
||||
int index = 0;
|
||||
|
||||
for (int i = 128; i <= 256; i += 64) {
|
||||
memset(buf, 11, sizeof(buf));
|
||||
memset(output, 0, sizeof(output));
|
||||
memset(iv, 0x11, sizeof(iv));
|
||||
|
||||
uint32_t tmp = esp_get_time();
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_set_encrypt_key(&ctx, key, i) == 0);
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_encrypt_cbc(&ctx, buf, sizeof(buf), output, sizeof(output), iv) == 0);
|
||||
|
||||
encode_time += esp_get_time() - tmp;
|
||||
|
||||
#if TEST_AES_DEBUG
|
||||
for (int j = 0; j < 8; j++)
|
||||
printf("0x%08x(0x%08x), ", output[j], s_aes_cbc_result[index][j]);
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
TEST_ASSERT_TRUE(memcmp(output, s_aes_cbc_result[index++], sizeof(output)) == 0);
|
||||
|
||||
memset(iv, 0x11, sizeof(iv));
|
||||
|
||||
tmp = esp_get_time();
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_set_decrypt_key(&ctx, key, i) == 0);
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_decrypt_cbc(&ctx, output, sizeof(output), buf, sizeof(buf), iv) == 0);
|
||||
|
||||
decode_time += esp_get_time() - tmp;
|
||||
|
||||
memset(output, 11, sizeof(output));
|
||||
|
||||
#if TEST_AES_DEBUG
|
||||
for (int j = 0; j < 8; j++)
|
||||
printf("0x%08x(0x%08x), ", buf[j], output[j]);
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
TEST_ASSERT_TRUE(memcmp(output, buf, sizeof(output)) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
#if TEST_AES_DEBUG_TIME
|
||||
printf("AES-CBC 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-CBC test cost time totally encode 554920 us and decode 1098010 us, once cost is about encode 541 us and decode 1072 us
|
||||
// no align: AES-CBC test cost time totally encode 730527 us and decode 2052009 us, once cost is about encode 713 us and decode 2003 us
|
||||
|
||||
|
||||
TEST_CASE("Test AES-CFB8", "[AES]")
|
||||
{
|
||||
uint32_t buf[8];
|
||||
uint32_t output[8];
|
||||
uint8_t key[32];
|
||||
uint8_t iv[16];
|
||||
esp_aes_t ctx;
|
||||
uint32_t encode_time = 0, decode_time = 0;
|
||||
extern uint32_t esp_get_time(void);
|
||||
|
||||
memset(key, 0x11, sizeof(key));
|
||||
|
||||
for (int cnt = 0; cnt < TEST_AES_COUNT; cnt++) {
|
||||
int index = 0;
|
||||
|
||||
for (int i = 128; i <= 256; i += 64) {
|
||||
memset(buf, 11, sizeof(buf));
|
||||
memset(output, 0, sizeof(output));
|
||||
memset(iv, 0x11, sizeof(iv));
|
||||
|
||||
uint32_t tmp = esp_get_time();
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_set_encrypt_key(&ctx, key, i) == 0);
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_encrypt_cfb8(&ctx, buf, sizeof(buf), output, sizeof(output), iv) == 0);
|
||||
|
||||
encode_time += esp_get_time() - tmp;
|
||||
|
||||
#if TEST_AES_DEBUG
|
||||
for (int j = 0; j < 8; j++)
|
||||
printf("0x%08x(0x%08x), ", output[j], s_aes_cfb8_result[index][j]);
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
TEST_ASSERT_TRUE(memcmp(output, s_aes_cfb8_result[index++], sizeof(output)) == 0);
|
||||
|
||||
memset(iv, 0x11, sizeof(iv));
|
||||
|
||||
tmp = esp_get_time();
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_set_encrypt_key(&ctx, key, i) == 0);
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_decrypt_cfb8(&ctx, output, sizeof(output), buf, sizeof(buf), iv) == 0);
|
||||
|
||||
decode_time += esp_get_time() - tmp;
|
||||
|
||||
memset(output, 11, sizeof(output));
|
||||
|
||||
#if TEST_AES_DEBUG
|
||||
for (int j = 0; j < 8; j++)
|
||||
printf("0x%08x(0x%08x), ", buf[j], output[j]);
|
||||
printf("\n");
|
||||
#endif
|
||||
TEST_ASSERT_TRUE(memcmp(output, buf, sizeof(output)) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
#if TEST_AES_DEBUG_TIME
|
||||
printf("AES-CFB8 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-CFB8 test cost time totally encode 3951207 us and decode 3918394 us, once cost is about encode 3858 us and decode 3826 us
|
||||
// no align: AES-CFB8 test cost time totally encode 5510528 us and decode 5481662 us, once cost is about encode 5381 us and decode 5353 us
|
||||
|
||||
|
||||
TEST_CASE("Test AES-CFB128", "[AES]")
|
||||
{
|
||||
uint32_t buf[8];
|
||||
uint32_t output[8];
|
||||
uint8_t key[32];
|
||||
uint8_t iv[16];
|
||||
size_t iv_off;
|
||||
esp_aes_t ctx;
|
||||
uint32_t encode_time = 0, decode_time = 0;
|
||||
extern uint32_t esp_get_time(void);
|
||||
|
||||
memset(key, 0x11, sizeof(key));
|
||||
|
||||
for (int cnt = 0; cnt < TEST_AES_COUNT; cnt++) {
|
||||
int index = 0;
|
||||
|
||||
for (int i = 128; i <= 256; i += 64) {
|
||||
memset(buf, 11, sizeof(buf));
|
||||
memset(output, 0, sizeof(output));
|
||||
memset(iv, 0x11, sizeof(iv));
|
||||
iv_off = 0;
|
||||
|
||||
uint32_t tmp = esp_get_time();
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_set_encrypt_key(&ctx, key, i) == 0);
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_encrypt_cfb128(&ctx, buf, sizeof(buf), output, sizeof(output), iv, &iv_off) == 0);
|
||||
|
||||
encode_time += esp_get_time() - tmp;
|
||||
|
||||
#if TEST_AES_DEBUG
|
||||
for (int j = 0; j < 8; j++)
|
||||
printf("0x%08x(0x%08x), ", output[j], s_aes_cfb128_result[index][j]);
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
TEST_ASSERT_TRUE(memcmp(output, s_aes_cfb128_result[index++], sizeof(output)) == 0);
|
||||
|
||||
memset(iv, 0x11, sizeof(iv));
|
||||
iv_off = 0;
|
||||
|
||||
tmp = esp_get_time();
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_set_encrypt_key(&ctx, key, i) == 0);
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_decrypt_cfb128(&ctx, output, sizeof(output), buf, sizeof(buf), iv, &iv_off) == 0);
|
||||
|
||||
decode_time += esp_get_time() - tmp;
|
||||
|
||||
memset(output, 11, sizeof(output));
|
||||
|
||||
#if TEST_AES_DEBUG
|
||||
for (int j = 0; j < 8; j++)
|
||||
printf("0x%08x(0x%08x), ", buf[j], output[j]);
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
TEST_ASSERT_TRUE(memcmp(output, buf, sizeof(buf)) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
#if TEST_AES_DEBUG_TIME
|
||||
printf("AES-CFB128 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-CFB128 test cost time totally encode 468238 us and decode 423293 us, once cost is about encode 457 us and decode 413 us
|
||||
// no align: AES-CFB128 test cost time totally encode 675645 us and decode 667750 us, once cost is about encode 659 us and decode 652 us
|
||||
|
||||
|
||||
TEST_CASE("Test AES-CTR", "[AES]")
|
||||
{
|
||||
uint32_t buf[8];
|
||||
uint32_t output[8];
|
||||
uint8_t key[32];
|
||||
uint8_t nonce_counter[16];
|
||||
uint8_t stream_block[16];
|
||||
size_t nc_off;
|
||||
esp_aes_t ctx;
|
||||
uint32_t encode_time = 0, decode_time = 0;
|
||||
extern uint32_t esp_get_time(void);
|
||||
|
||||
memset(key, 0x11, sizeof(key));
|
||||
|
||||
for (int cnt = 0; cnt < TEST_AES_COUNT; cnt++) {
|
||||
int index = 0;
|
||||
|
||||
for (int i = 128; i <= 256; i += 64) {
|
||||
memset(buf, 11, sizeof(buf));
|
||||
memset(output, 0, sizeof(output));
|
||||
memset(nonce_counter, 0x11, sizeof(nonce_counter));
|
||||
nc_off = 0;
|
||||
|
||||
uint32_t tmp = esp_get_time();
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_set_encrypt_key(&ctx, key, i) == 0);
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_encrypt_ctr(&ctx, &nc_off, nonce_counter, stream_block, buf, sizeof(buf), output, sizeof(output)) == 0);
|
||||
|
||||
encode_time += esp_get_time() - tmp;
|
||||
|
||||
#if TEST_AES_DEBUG
|
||||
for (int j = 0; j < 8; j++)
|
||||
printf("0x%08x(0x%08x), ", output[j], s_aes_cfb128_result[index][j]);
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
TEST_ASSERT_TRUE(memcmp(output, s_aes_ctr_result[index++], sizeof(output)) == 0);
|
||||
|
||||
memset(nonce_counter, 0x11, sizeof(nonce_counter));
|
||||
nc_off = 0;
|
||||
|
||||
tmp = esp_get_time();
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_set_encrypt_key(&ctx, key, i) == 0);
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_decrypt_ctr(&ctx, &nc_off, nonce_counter, stream_block, output, sizeof(output), buf, sizeof(buf)) == 0);
|
||||
|
||||
decode_time += esp_get_time() - tmp;
|
||||
|
||||
memset(output, 11, sizeof(output));
|
||||
|
||||
#if TEST_AES_DEBUG
|
||||
for (int j = 0; j < 8; j++)
|
||||
printf("0x%08x(0x%08x), ", buf[j], output[j]);
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
TEST_ASSERT_TRUE(memcmp(output, buf, sizeof(buf)) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
#if TEST_AES_DEBUG_TIME
|
||||
printf("AES-CRT 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-CRT test cost time totally encode 344845 us and decode 344421 us, once cost is about encode 336 us and decode 336 us
|
||||
// no align: AES-CRT test cost time totally encode 639256 us and decode 635343 us, once cost is about encode 624 us and decode 620 us
|
||||
|
||||
|
||||
|
||||
TEST_CASE("Test AES-XTS", "[AES]")
|
||||
{
|
||||
uint32_t encode_time = 0, decode_time = 0;
|
||||
const int num_tests = sizeof(aes_test_xts_key) / sizeof(*aes_test_xts_key);
|
||||
extern uint32_t esp_get_time(void);
|
||||
|
||||
for (int cnt = 0; cnt < TEST_AES_COUNT; cnt++) {
|
||||
for (int i = 0; i < num_tests; i++) {
|
||||
const int len = sizeof(*aes_test_xts_ct32);
|
||||
uint32_t buf[8];
|
||||
uint8_t key[32];
|
||||
const uint8_t *data_unit = aes_test_xts_data_unit[i];
|
||||
esp_aes_xts_t ctx_xts;
|
||||
|
||||
memcpy(key, aes_test_xts_key[i], 32);
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_xts_set_encrypt_key(&ctx_xts, key, 256) == 0);
|
||||
memcpy(buf, aes_test_xts_pt32[i], len);
|
||||
|
||||
uint32_t tmp = esp_get_time();
|
||||
TEST_ASSERT_TRUE(esp_aes_crypt_xts(&ctx_xts, 1, len, data_unit, buf, buf) == 0);
|
||||
encode_time += esp_get_time() - tmp;
|
||||
|
||||
TEST_ASSERT_TRUE(memcmp(buf, aes_test_xts_ct32[i], len) == 0);
|
||||
|
||||
TEST_ASSERT_TRUE(esp_aes_xts_set_decrypt_key(&ctx_xts, key, 256) == 0);
|
||||
memcpy(buf, aes_test_xts_ct32[i], len);
|
||||
|
||||
tmp = esp_get_time();
|
||||
TEST_ASSERT_TRUE(esp_aes_crypt_xts(&ctx_xts, 0, len, data_unit, buf, buf) == 0);
|
||||
decode_time += esp_get_time() - tmp;
|
||||
|
||||
TEST_ASSERT_TRUE(memcmp(buf, aes_test_xts_pt32[i], len) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
#if TEST_AES_DEBUG_TIME
|
||||
printf("AES-XTS 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-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
|
74
components/mbedtls/test/test_arc4.c
Normal file
74
components/mbedtls/test/test_arc4.c
Normal file
@ -0,0 +1,74 @@
|
||||
|
||||
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "esp_arc4.h"
|
||||
#include "unity.h"
|
||||
|
||||
#define TEST_ARC4_COUNT 512
|
||||
#define TEST_ARC4_DEBUG 1
|
||||
|
||||
static const uint8_t arc4_test_key[3][8] =
|
||||
{
|
||||
{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
|
||||
{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
|
||||
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
|
||||
};
|
||||
|
||||
static const uint8_t arc4_test_pt[3][8] =
|
||||
{
|
||||
{ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
|
||||
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
|
||||
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
|
||||
};
|
||||
|
||||
static const uint8_t arc4_test_ct[3][8] =
|
||||
{
|
||||
{ 0x75, 0xB7, 0x87, 0x80, 0x99, 0xE0, 0xC5, 0x96 },
|
||||
{ 0x74, 0x94, 0xC2, 0xE7, 0x10, 0x4B, 0x08, 0x79 },
|
||||
{ 0xDE, 0x18, 0x89, 0x41, 0xA3, 0x37, 0x5D, 0x3A }
|
||||
};
|
||||
|
||||
TEST_CASE("Test ARC4", "[ARC4]")
|
||||
{
|
||||
int i, n = 0;
|
||||
uint32_t encode_time = 0;
|
||||
uint8_t ibuf[8];
|
||||
uint8_t obuf[8];
|
||||
extern uint32_t esp_get_time(void);
|
||||
|
||||
for (n = 0; n < TEST_ARC4_COUNT; n++) {
|
||||
for(i = 0; i < 3; i++)
|
||||
{
|
||||
memcpy(ibuf, arc4_test_pt[i], 8);
|
||||
|
||||
uint32_t tmp = esp_get_time();
|
||||
esp_arc4_context ctx;
|
||||
esp_arc4_setup(&ctx, arc4_test_key[i], 8);
|
||||
TEST_ASSERT_TRUE(esp_arc4_encrypt(&ctx, 8, ibuf, obuf) == 0);
|
||||
encode_time += esp_get_time() - tmp;
|
||||
|
||||
TEST_ASSERT_TRUE(memcmp(obuf, arc4_test_ct[i], 8) == 0);
|
||||
}
|
||||
}
|
||||
#if TEST_ARC4_DEBUG
|
||||
printf("ARC4 test cost time totally encode %u us , once cost is about encode %u us\n",
|
||||
encode_time, encode_time / (TEST_ARC4_COUNT * 3));
|
||||
#endif
|
||||
}
|
||||
|
||||
// ARC4 test cost time totally encode 794288 us , once cost is about encode 517 us
|
||||
|
77
components/mbedtls/test/test_base64.c
Normal file
77
components/mbedtls/test/test_base64.c
Normal file
@ -0,0 +1,77 @@
|
||||
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "esp_base64.h"
|
||||
#include "unity.h"
|
||||
|
||||
#define TEST_BASE64_COUNT 4096
|
||||
#define TEST_BASE64_DEBUG 1
|
||||
|
||||
static unsigned char base64_test_dec[] =
|
||||
{
|
||||
0x24, 0x48, 0x6E, 0x56, 0x87, 0x62, 0x5A, 0xBD,
|
||||
0xBF, 0x17, 0xD9, 0xA2, 0xC4, 0x17, 0x1A, 0x01,
|
||||
0x94, 0xED, 0x8F, 0x1E, 0x11, 0xB3, 0xD7, 0x09,
|
||||
0x0C, 0xB6, 0xE9, 0x10, 0x6F, 0x22, 0xEE, 0x13,
|
||||
0xCA, 0xB3, 0x07, 0x05, 0x76, 0xC9, 0xFA, 0x31,
|
||||
0x6C, 0x08, 0x34, 0xFF, 0x8D, 0xC2, 0x6C, 0x38,
|
||||
0x00, 0x43, 0xE9, 0x54, 0x97, 0xAF, 0x50, 0x4B,
|
||||
0xD1, 0x41, 0xBA, 0x95, 0x31, 0x5A, 0x0B, 0x97
|
||||
};
|
||||
|
||||
static unsigned char base64_test_enc[] =
|
||||
"JEhuVodiWr2/F9mixBcaAZTtjx4Rs9cJDLbpEG8i7hPK"
|
||||
"swcFdsn6MWwINP+Nwmw4AEPpVJevUEvRQbqVMVoLlw==";
|
||||
|
||||
TEST_CASE("Test base64 codec", "[base64]")
|
||||
{
|
||||
int ret;
|
||||
uint32_t encode_time = 0, decode_time = 0;
|
||||
unsigned char buffer[128];
|
||||
|
||||
extern uint32_t esp_get_time(void);
|
||||
|
||||
for (int i = 0; i < TEST_BASE64_COUNT; i++) {
|
||||
uint32_t tmp = esp_get_time();
|
||||
|
||||
ret = esp_base64_encode(base64_test_dec, sizeof(base64_test_dec), buffer, sizeof(buffer));
|
||||
TEST_ASSERT_TRUE(ret > 0);
|
||||
|
||||
encode_time += esp_get_time() - tmp;
|
||||
|
||||
TEST_ASSERT_TRUE(memcmp(base64_test_enc, buffer, ret) == 0);
|
||||
|
||||
tmp = esp_get_time();
|
||||
|
||||
ret = esp_base64_decode(base64_test_enc, sizeof(base64_test_enc) - 1, buffer, sizeof(buffer));
|
||||
TEST_ASSERT_TRUE(ret > 0);
|
||||
|
||||
decode_time += esp_get_time() - tmp;
|
||||
|
||||
TEST_ASSERT_TRUE(memcmp(base64_test_dec, buffer, ret) == 0);
|
||||
}
|
||||
|
||||
#if TEST_BASE64_DEBUG
|
||||
printf("base64 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_BASE64_COUNT, decode_time / TEST_BASE64_COUNT);
|
||||
#endif
|
||||
}
|
||||
|
||||
// align: base64 test cost time totally encode 100921 us and decode 549040 us,
|
||||
// once cost is about encode 29 us and decode 133 us
|
||||
|
||||
// no align : base64 test cost time totally encode 640726 us and decode 1601834 us,
|
||||
// once cost is about encode 156 us and decode 391 us
|
81
components/mbedtls/test/test_crc.c
Normal file
81
components/mbedtls/test/test_crc.c
Normal file
@ -0,0 +1,81 @@
|
||||
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include "rom/crc.h"
|
||||
#include "unity.h"
|
||||
|
||||
#define TEST_CRC_COUNT 4096
|
||||
#define TEST_CRC_DEBUG 1
|
||||
#define TEST_DATA_BYTES 128
|
||||
|
||||
static uint8_t s_crc_src[TEST_DATA_BYTES];
|
||||
|
||||
TEST_CASE("Test CRC8", "[CRC8]")
|
||||
{
|
||||
uint8_t crc = 0;
|
||||
uint32_t crc_time = 0;
|
||||
|
||||
extern uint32_t esp_get_time(void);
|
||||
|
||||
memset(s_crc_src, 11, TEST_DATA_BYTES);
|
||||
|
||||
for (int i = 0; i < TEST_CRC_COUNT; i++) {
|
||||
uint32_t tmp = esp_get_time();
|
||||
|
||||
crc = esp_crc8(s_crc_src, sizeof(s_crc_src));
|
||||
|
||||
crc_time += esp_get_time() - tmp;
|
||||
|
||||
TEST_ASSERT_EQUAL(0xd7, crc);
|
||||
}
|
||||
|
||||
#if TEST_CRC_DEBUG
|
||||
printf("CRC8 test cost time totally %u us, once cost is about %u us\n",
|
||||
crc_time, crc_time / TEST_CRC_COUNT);
|
||||
#endif
|
||||
}
|
||||
|
||||
// no align: CRC8 test cost time totally 955287 us, once cost is about 233 us
|
||||
// align : CRC8 test cost time totally 186694 us, once cost is about 45 us
|
||||
|
||||
TEST_CASE("Test CRC16", "[CRC16]")
|
||||
{
|
||||
uint16_t crc = 0;
|
||||
uint32_t crc_time = 0;
|
||||
|
||||
extern uint32_t esp_get_time(void);
|
||||
|
||||
memset(s_crc_src, 11, TEST_DATA_BYTES);
|
||||
|
||||
for (int i = 0; i < TEST_CRC_COUNT; i++) {
|
||||
uint32_t tmp = esp_get_time();
|
||||
|
||||
crc = crc16_le(0, s_crc_src, sizeof(s_crc_src));
|
||||
|
||||
crc_time += esp_get_time() - tmp;
|
||||
|
||||
TEST_ASSERT_EQUAL(0x495d, crc);
|
||||
}
|
||||
|
||||
#if TEST_CRC_DEBUG
|
||||
printf("CRC16 test cost time totally %u us, once cost is about %u us\n",
|
||||
crc_time, crc_time / TEST_CRC_COUNT);
|
||||
#endif
|
||||
}
|
||||
|
||||
// no align: CRC16 test cost time totally 1114398 us, once cost is about 272 us
|
||||
// align : CRC16 test cost time totally 200268 us, once cost is about 48 us
|
86
components/mbedtls/test/test_md5.c
Normal file
86
components/mbedtls/test/test_md5.c
Normal file
@ -0,0 +1,86 @@
|
||||
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include "esp_md5.h"
|
||||
#include "unity.h"
|
||||
|
||||
#define TEST_MD5_COUNT 256
|
||||
#define TEST_MD5_DEBUG 1
|
||||
|
||||
static const uint8_t md5_test_buf[7][81] =
|
||||
{
|
||||
{""},
|
||||
{"a"},
|
||||
{"abc"},
|
||||
{"message digest"},
|
||||
{"abcdefghijklmnopqrstuvwxyz"},
|
||||
{"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"},
|
||||
{"12345678901234567890123456789012345678901234567890123456789012"
|
||||
"345678901234567890"}
|
||||
};
|
||||
|
||||
static const size_t md5_test_buflen[7] =
|
||||
{
|
||||
0, 1, 3, 14, 26, 62, 80
|
||||
};
|
||||
|
||||
static const uint8_t md5_test_sum[7][16] =
|
||||
{
|
||||
{ 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
|
||||
0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E },
|
||||
{ 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,
|
||||
0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 },
|
||||
{ 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
|
||||
0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 },
|
||||
{ 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,
|
||||
0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 },
|
||||
{ 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,
|
||||
0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B },
|
||||
{ 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,
|
||||
0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F },
|
||||
{ 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,
|
||||
0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A }
|
||||
};
|
||||
|
||||
TEST_CASE("Test MD5", "[md5]")
|
||||
{
|
||||
uint32_t encode_time = 0;
|
||||
uint8_t md5sum[16] = {0};
|
||||
extern uint32_t esp_get_time(void);
|
||||
|
||||
for (int i = 0; i < TEST_MD5_COUNT; i++) {
|
||||
for (int j = 0; j < 7; j++)
|
||||
{
|
||||
uint32_t tmp = esp_get_time();
|
||||
|
||||
esp_md5_context_t ctx;
|
||||
TEST_ASSERT_TRUE(esp_md5_init(&ctx) == 0);
|
||||
TEST_ASSERT_TRUE(esp_md5_update(&ctx, md5_test_buf[j], md5_test_buflen[j]) == 0);
|
||||
TEST_ASSERT_TRUE(esp_md5_final(&ctx, md5sum) == 0);
|
||||
|
||||
encode_time += esp_get_time() - tmp;
|
||||
TEST_ASSERT_TRUE(memcmp(md5sum, md5_test_sum[j], 16) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
#if TEST_MD5_DEBUG
|
||||
printf("MD5 test cost time totally encode %u us , once cost is about encode %u us\n",
|
||||
encode_time, encode_time / (TEST_MD5_COUNT * 7));
|
||||
#endif
|
||||
}
|
||||
|
||||
// MD5 test cost time totally encode 88614 us , once cost is about encode 49 us
|
||||
|
223
components/mbedtls/test/test_sha.c
Normal file
223
components/mbedtls/test/test_sha.c
Normal file
@ -0,0 +1,223 @@
|
||||
// Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
#include "esp_sha.h"
|
||||
#include "esp_heap_caps.h"
|
||||
#include "esp_log.h"
|
||||
#include "unity.h"
|
||||
|
||||
//#define DEBUG_SHA_RESULT
|
||||
|
||||
#define TAG "SHA_TEST"
|
||||
|
||||
TEST_CASE("Test SHA1", "[SHA]")
|
||||
{
|
||||
int ret;
|
||||
uint8_t *buf;
|
||||
esp_sha1_t sha_ctx;
|
||||
|
||||
const uint32_t sha_result[] = {
|
||||
0xc3b837ce, 0x0c8a528c, 0x032939b5, 0x50a88285, 0x6433458d
|
||||
};
|
||||
|
||||
buf = heap_caps_malloc(1024, MALLOC_CAP_8BIT);
|
||||
TEST_ASSERT(buf != NULL);
|
||||
|
||||
memset(buf, 11, 1024);
|
||||
|
||||
ret = esp_sha1_init(&sha_ctx);
|
||||
TEST_ASSERT(ret == 0);
|
||||
|
||||
ret = esp_sha1_update(&sha_ctx, buf, 1024);
|
||||
TEST_ASSERT(ret == 0);
|
||||
|
||||
memset(buf, 0, 1024);
|
||||
ret = esp_sha1_finish(&sha_ctx, buf);
|
||||
TEST_ASSERT(ret == 0);
|
||||
|
||||
#ifdef DEBUG_SHA_RESULT
|
||||
const uint32_t *pbuf = (const uint32_t *)buf;
|
||||
printf("\n");
|
||||
for (int j = 0; j < sizeof(sha_result) / sizeof(sha_result[0]); j++) {
|
||||
printf("0x%x(0x%x) ", pbuf[j], sha_result[j]);
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
TEST_ASSERT(memcmp(buf, sha_result, sizeof(sha_result)) == 0);
|
||||
|
||||
heap_caps_free(buf);
|
||||
}
|
||||
|
||||
TEST_CASE("Test SHA224", "[SHA]")
|
||||
{
|
||||
int ret;
|
||||
uint8_t *buf;
|
||||
esp_sha224_t sha_ctx;
|
||||
|
||||
const uint32_t sha_result[] = {
|
||||
0xeb5dd981, 0x0ea2508e, 0xfe5708b8, 0xc15f30b5, 0x833f2144, 0xbbf4de16, 0x50d112b7
|
||||
};
|
||||
|
||||
buf = heap_caps_malloc(1024, MALLOC_CAP_8BIT);
|
||||
TEST_ASSERT(buf != NULL);
|
||||
|
||||
memset(buf, 11, 1024);
|
||||
|
||||
ret = esp_sha224_init(&sha_ctx);
|
||||
TEST_ASSERT(ret == 0);
|
||||
|
||||
ret = esp_sha224_update(&sha_ctx, buf, 1024);
|
||||
TEST_ASSERT(ret == 0);
|
||||
|
||||
memset(buf, 0, 1024);
|
||||
ret = esp_sha224_finish(&sha_ctx, buf);
|
||||
TEST_ASSERT(ret == 0);
|
||||
|
||||
#ifdef DEBUG_SHA_RESULT
|
||||
const uint32_t *pbuf = (const uint32_t *)buf;
|
||||
printf("\n");
|
||||
for (int j = 0; j < sizeof(sha_result) / sizeof(sha_result[0]); j++) {
|
||||
printf("0x%x(0x%x) ", pbuf[j], sha_result[j]);
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
TEST_ASSERT(memcmp(buf, sha_result, sizeof(sha_result)) == 0);
|
||||
|
||||
heap_caps_free(buf);
|
||||
}
|
||||
|
||||
TEST_CASE("Test SHA256", "[SHA]")
|
||||
{
|
||||
int ret;
|
||||
uint8_t *buf;
|
||||
esp_sha256_t sha_ctx;
|
||||
|
||||
const uint32_t sha_result[] = {
|
||||
0xfc875b5a, 0x318e3c5a, 0xac2b3233, 0x4df7b366, 0x4c4c9261, 0x0e70af8d, 0x69a7e57c, 0x179cd56e
|
||||
};
|
||||
|
||||
buf = heap_caps_malloc(1024, MALLOC_CAP_8BIT);
|
||||
TEST_ASSERT(buf != NULL);
|
||||
|
||||
memset(buf, 11, 1024);
|
||||
|
||||
ret = esp_sha256_init(&sha_ctx);
|
||||
TEST_ASSERT(ret == 0);
|
||||
|
||||
ret = esp_sha256_update(&sha_ctx, buf, 1024);
|
||||
TEST_ASSERT(ret == 0);
|
||||
|
||||
memset(buf, 0, 1024);
|
||||
ret = esp_sha256_finish(&sha_ctx, buf);
|
||||
TEST_ASSERT(ret == 0);
|
||||
|
||||
#ifdef DEBUG_SHA_RESULT
|
||||
const uint32_t *pbuf = (const uint32_t *)buf;
|
||||
printf("\n");
|
||||
for (int j = 0; j < sizeof(sha_result) / sizeof(sha_result[0]); j++) {
|
||||
printf("0x%x(0x%x) ", pbuf[j], sha_result[j]);
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
TEST_ASSERT(memcmp(buf, sha_result, sizeof(sha_result)) == 0);
|
||||
|
||||
heap_caps_free(buf);
|
||||
}
|
||||
|
||||
TEST_CASE("Test SHA384", "[SHA]")
|
||||
{
|
||||
int ret;
|
||||
uint8_t *buf;
|
||||
esp_sha384_t sha_ctx;
|
||||
|
||||
const uint32_t sha_result[] = {
|
||||
0xd1d31575, 0x494afdef, 0x1d042951, 0x77a02c7b, 0x546db656, 0xdf31c571,
|
||||
0x1c3f87c1, 0x0d5cd544, 0x73628b2a, 0xecf051e7, 0xb72e6478, 0x83cee28b
|
||||
};
|
||||
|
||||
buf = heap_caps_malloc(1024, MALLOC_CAP_8BIT);
|
||||
TEST_ASSERT(buf != NULL);
|
||||
|
||||
memset(buf, 11, 1024);
|
||||
|
||||
ret = esp_sha384_init(&sha_ctx);
|
||||
TEST_ASSERT(ret == 0);
|
||||
|
||||
ret = esp_sha384_update(&sha_ctx, buf, 1024);
|
||||
TEST_ASSERT(ret == 0);
|
||||
|
||||
memset(buf, 0, 1024);
|
||||
ret = esp_sha384_finish(&sha_ctx, buf);
|
||||
TEST_ASSERT(ret == 0);
|
||||
|
||||
#if DEBUG_SHA_RESULT
|
||||
const uint32_t *pbuf = (const uint32_t *)buf;
|
||||
printf("\n");
|
||||
for (int j = 0; j < sizeof(sha_result) / sizeof(sha_result[0]); j++) {
|
||||
printf("0x%x(0x%x) ", pbuf[j], sha_result[j]);
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
TEST_ASSERT(memcmp(buf, sha_result, sizeof(sha_result)) == 0);
|
||||
|
||||
heap_caps_free(buf);
|
||||
}
|
||||
|
||||
TEST_CASE("Test SHA512", "[SHA]")
|
||||
{
|
||||
int ret;
|
||||
uint8_t *buf;
|
||||
esp_sha512_t sha_ctx;
|
||||
|
||||
const uint32_t sha_result[] = {
|
||||
0x153be81b, 0x37abc24e, 0x3b6f1a5b, 0x42c713f9, 0x51c9a8e1, 0x7303f29b,
|
||||
0x2c979121, 0x1c4e632d, 0xad470c5a, 0xe7643b5e, 0x63447f10, 0x05d613e6,
|
||||
0xa3c6b5cc, 0x99e52218, 0x665b659f, 0x1bfc639b
|
||||
};
|
||||
|
||||
buf = heap_caps_malloc(1024, MALLOC_CAP_8BIT);
|
||||
TEST_ASSERT(buf != NULL);
|
||||
|
||||
memset(buf, 11, 1024);
|
||||
|
||||
ret = esp_sha512_init(&sha_ctx);
|
||||
TEST_ASSERT(ret == 0);
|
||||
|
||||
ret = esp_sha512_update(&sha_ctx, buf, 1024);
|
||||
TEST_ASSERT(ret == 0);
|
||||
|
||||
memset(buf, 0, 1024);
|
||||
ret = esp_sha512_finish(&sha_ctx, buf);
|
||||
TEST_ASSERT(ret == 0);
|
||||
|
||||
#if DEBUG_SHA_RESULT
|
||||
const uint32_t *pbuf = (const uint32_t *)buf;
|
||||
printf("\n");
|
||||
for (int j = 0; j < sizeof(sha_result) / sizeof(sha_result[0]); j++) {
|
||||
printf("0x%x(0x%x) ", pbuf[j], sha_result[j]);
|
||||
}
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
TEST_ASSERT(memcmp(buf, sha_result, sizeof(sha_result)) == 0);
|
||||
|
||||
heap_caps_free(buf);
|
||||
}
|
Reference in New Issue
Block a user