本帖最后由 aple0807 于 2022-2-13 10:42 编辑
之前看国民系列MCU支持硬件加密解码模块,可惜本开发板MCU没有该功能,测下软件加密解密速度及用法。
下面主要测试 AES 和 DES。
AES加密模式比较多,代码如下:
void aes_demo(void)
{
uu32 clk_start;
uu8 *src, *enc, *dec, *iv;
mbedtls_aes_context *aes_enc, *aes_dec;
size_t offset;
// 内存请求
src = calloc(1, 1024);
enc = calloc(1, 1024);
dec = calloc(1, 1024);
iv = calloc(1, 80);
aes_enc = malloc(sizeof(mbedtls_aes_context));
aes_dec = malloc(sizeof(mbedtls_aes_context));
dbg_puts("\n===========================================================\n");
dbg_print("AES 128 encode test: <128 bits> \n");
// 初始化
dat_buff_init();
// 初始化结构
mbedtls_aes_init(aes_enc);
mbedtls_aes_init(aes_dec);
// 生成 KEY
clk_start = bsp_cpu_clks_get();
mbedtls_aes_setkey_enc(aes_enc, (void *)ase_key_src, 128);
mbedtls_aes_setkey_dec(aes_dec, (void *)ase_key_src, 128);
dbg_print("key make time: %d us\n", bsp_clks_to_us(bsp_cpu_clks_get() - clk_start));
/*------------------------------------------------------------------------*/
// 加密 + 解密 ECB 电子密码本模式
clk_start = bsp_cpu_clks_get();
mbedtls_aes_crypt_ecb(aes_enc, MBEDTLS_AES_ENCRYPT, src, enc);
mbedtls_aes_crypt_ecb(aes_dec, MBEDTLS_AES_DECRYPT, enc, dec);
dbg_print("AES ecb : enc + dec time: %d us\n", bsp_clks_to_us(bsp_cpu_clks_get() - clk_start));
// 显示
//dbg_u8_print("src: ", src, 16, 20);
dbg_u8_print("enc: ", enc, 16, 20);
dbg_u8_print("dec: ", dec, 16, 20);
/*------------------------------------------------------------------------*/
// 加密 + 解密 CBC 密码分组连接模式 - 内部分组 IO长度相同
// 加密解密需要相同的初始向量, 向量长度16B
dat_buff_init();
clk_start = bsp_cpu_clks_get();
memset(iv, 0xFF, 16);
mbedtls_aes_crypt_cbc(aes_enc, MBEDTLS_AES_ENCRYPT, 512, iv, src, enc);
memset(iv, 0xFF, 16);
mbedtls_aes_crypt_cbc(aes_dec, MBEDTLS_AES_DECRYPT, 512, iv, enc, dec);
dbg_print("\nAES CBC 512 : enc + dec time: %d us\n", bsp_clks_to_us(bsp_cpu_clks_get() - clk_start));
// 显示
//dbg_u8_print("src: \n", src, 64, 32);
dbg_u8_print("enc: \n", enc, 128, 32);
dbg_u8_print("dec: \n", dec, 128, 32);
/*------------------------------------------------------------------------*/
// 加密 + 解密 CFB8 密文反馈模式 - 内部分组 IO长度相同
// 加密解密需要相同的初始向量, 向量长度16B,加密解密都使用加密器
dat_buff_init();
mbedtls_aes_setkey_enc(aes_enc, (void *)ase_key_src, 128);
clk_start = bsp_cpu_clks_get();
memset(iv, 0xFF, 16);
mbedtls_aes_crypt_cfb8(aes_enc, MBEDTLS_AES_ENCRYPT, 60, iv, src, enc);
memset(iv, 0xFF, 16);
mbedtls_aes_crypt_cfb8(aes_enc, MBEDTLS_AES_DECRYPT, 60, iv, enc, dec);
dbg_print("\nAES CFB8 60 : enc + dec time: %d us\n", bsp_clks_to_us(bsp_cpu_clks_get() - clk_start));
// 显示
//dbg_u8_print("src: \n", src, 64, 32);
dbg_u8_print("enc: \n", enc, 64, 32);
dbg_u8_print("dec: \n", dec, 64, 32);
/*------------------------------------------------------------------------*/
// 加密 + 解密 CFB128 密文反馈模式 - 内部分组 IO长度相同
// 加密解密需要相同的初始向量, 向量长度16B,加密解密都使用加密器
dat_buff_init();
offset = 8; // 0~15
clk_start = bsp_cpu_clks_get();
memset(iv, 0xFF, 16);
mbedtls_aes_setkey_enc(aes_enc, (void *)ase_key_src, 128);
mbedtls_aes_crypt_cfb128(aes_enc, MBEDTLS_AES_ENCRYPT, 512, &offset, iv, src, enc);
memset(iv, 0xFF, 16);
mbedtls_aes_setkey_enc(aes_enc, (void *)ase_key_src, 128);
mbedtls_aes_crypt_cfb128(aes_enc, MBEDTLS_AES_DECRYPT, 512, &offset, iv, enc, dec);
dbg_print("\nAES CFB128 512 : enc + dec time: %d us\n", bsp_clks_to_us(bsp_cpu_clks_get() - clk_start));
// 显示
//dbg_u8_print("src: \n", src, 64, 32);
dbg_u8_print("enc: \n", enc, 64, 32);
dbg_u8_print("dec: \n", dec, 64, 32);
/*------------------------------------------------------------------------*/
// 加密 + 解密 OFB 输出反馈模式
// 加密解密需要相同的初始向量, 向量长度16B,加密解密都使用加密器
dat_buff_init();
offset = 8; // 0~15
clk_start = bsp_cpu_clks_get();
memset(iv, 0xFF, 16);
mbedtls_aes_setkey_enc(aes_enc, (void *)ase_key_src, 128);
mbedtls_aes_crypt_ofb(aes_enc, 512, &offset, iv, src, enc);
memset(iv, 0xFF, 16);
mbedtls_aes_setkey_enc(aes_enc, (void *)ase_key_src, 128);
mbedtls_aes_crypt_ofb(aes_enc, 512, &offset, iv, enc, dec);
dbg_print("\nAES OFB 512 : enc + dec time: %d us\n", bsp_clks_to_us(bsp_cpu_clks_get() - clk_start));
// 显示
//dbg_u8_print("src: \n", src, 64, 32);
dbg_u8_print("enc: \n", enc, 64, 32);
dbg_u8_print("dec: \n", dec, 64, 32);
// 释放,用于清除内存中的密匙,属于安全性代码
mbedtls_aes_free(aes_enc);
mbedtls_aes_free(aes_dec);
// 释放内存
free(src);
free(enc);
free(dec);
free(iv);
free(aes_enc);
free(aes_dec);
}
DES测试:
void des_demo(void)
{
uu32 clk_start;
uu8 *src, *enc, *dec;
mbedtls_des_context *mbed_des_enc;
mbedtls_des_context *mbed_des_dec;
// 内存请求
src = calloc(1, 80);
enc = calloc(1, 80);
dec = calloc(1, 80);
mbed_des_enc = calloc(1, sizeof(mbedtls_des_context));
mbed_des_dec = calloc(1, sizeof(mbedtls_des_context));
dbg_puts("\n===========================================================\n");
dbg_print("mbed DES 64 test :\n");
memcpy(src, led_code, 8);
//memset(ptr, 0x53, 8);
mbedtls_des_init(mbed_des_enc);
mbedtls_des_init(mbed_des_dec);
// mbed des key creat
clk_start = bsp_cpu_clks_get();
mbedtls_des_setkey_enc(mbed_des_enc, (void *)"12345678");
mbedtls_des_setkey_dec(mbed_des_dec, (void *)"12345678");
dbg_print("key make time: %d us\n", bsp_clks_to_us(bsp_cpu_clks_get() - clk_start));
// enc dec
clk_start = bsp_cpu_clks_get();
mbedtls_des_crypt_ecb(mbed_des_enc, src, enc);
mbedtls_des_crypt_ecb(mbed_des_dec, enc, dec);
dbg_print("enc and dec time: %d us\n", bsp_clks_to_us(bsp_cpu_clks_get() - clk_start));
// displsy
dbg_u8_print("src: ", src, 8, 12);
dbg_u8_print("enc: ", enc, 8, 12);
dbg_u8_print("dec: ", dec, 8, 12);
// 清除内存关键数据
mbedtls_des_free(mbed_des_enc);
mbedtls_des_free(mbed_des_dec);
// 释放内存
free(src);
free(enc);
free(dec);
free(mbed_des_enc);
free(mbed_des_dec);
}
测试结果
AES 128 encode test: <128 bits>
key make time: 454 us
AES ecb : enc + dec time: 22 us
enc: 39 5B AB C7 F2 72 0A AA F6 12 15 58 39 86 95 FF
dec: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
AES CBC 512 : enc + dec time: 807 us
enc:
22 F6 8C 6E 7B B0 B5 53 AA 2C 24 36 58 E8 C6 DB 0D B2 B7 A7 11 D6 C9 65 F9 96 2B 40 14 A0 86 6A =>line#1
D2 6C 10 6E 07 B0 B2 50 C8 36 0B 56 3F A8 CE 33 D5 CC E4 60 4A 63 EE 52 9C 4A 14 33 82 D4 45 28 =>line#2
C5 28 05 5D 4D B7 72 B1 6F 8A F1 C8 5C 9B B9 6C 95 9F 76 40 88 EF D5 D9 53 1F 9B 98 BE EA 3B 54 =>line#3
8C 5A 9C 12 E6 F9 DA FF 57 40 A7 46 D4 89 AD 72 44 AA 10 CB FF 12 52 56 37 B8 CA 42 57 54 6D CC =>line#4
dec:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F =>line#1
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F =>line#2
40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F =>line#3
60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F 70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F =>line#4
AES CFB8 60 : enc + dec time: 1391 us
enc:
83 EE 6E 28 BB 81 0A AC C6 50 74 E8 D7 F9 5D 88 9D 7B 71 31 35 00 43 6B 43 7F 1D 6D CE BD 7B B2 =>line#1
0E E6 4C 56 0A 06 59 2B D0 5B 8C 1C 04 15 55 A8 08 1E 97 91 81 D2 1E 55 27 93 DF F0 00 00 00 00 =>line#2
dec:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F =>line#1
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 00 00 00 00 =>line#2
AES CFB128 512 : enc + dec time: 901 us
enc:
FF FE FD FC FB FA F9 F8 30 86 76 38 13 31 D3 CA C1 E2 A1 D4 C5 A9 64 5E D3 7E 48 C7 92 D4 47 3D =>line#1
93 3E 3E 95 21 27 BC 8F 74 FD 9A 70 74 2A 94 74 44 4A E8 79 4A EF A2 8D 91 DC CB C9 4A 5E 6D 3A =>line#2
dec:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F =>line#1
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F =>line#2
AES OFB 512 : enc + dec time: 863 us
enc:
FF FE FD FC FB FA F9 F8 8B CA FF BB 07 FC 73 5A A0 A5 53 25 65 7D D8 9A 64 9B 3E 77 AD F0 99 C4 =>line#1
9A 36 0B 7F 5F 27 D3 57 B5 31 E2 EF FF E1 C0 13 C3 A3 A7 13 77 22 07 E5 9D 9F FB 63 27 45 90 80 =>line#2
dec:
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F =>line#1
20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F =>line#2
===========================================================
mbed DES 64 test :
key make time: 70 us
enc and dec time: 27 us
src: 3F 06 5B 4F 66 6D 7D 07
enc: 27 94 E5 83 D9 76 42 6F
dec: 3F 06 5B 4F 66 6D 7D 07
需要注意的是AES加密需要的堆栈比较大,建议任务堆栈分配3KB以上。
测试结果比预想的要好的多,AES加解密速度大约700KB/s.
另外,初始化代码默认开启了指令cache加速,关闭该功能进行测试,速度相差30%左右。
下面附上测试KEIL工程:
software.rar
(3.48 MB)
|
共1人点赞
|