1. crypt初始化
2.新建一个RT-Thread任务线程对AES-CBC 加密和解密进行测试,具体代码如下:
#include "board.h"
#include "rtthread.h"
struct rt_thread Task1_thread;
struct rt_thread Task2_thread;
struct rt_thread Task3_thread;
rt_uint8_t rt_task1_thread_stack[1024];
rt_uint8_t rt_task2_thread_stack[1024];
rt_uint8_t rt_task3_thread_stack[1024];
md_crypt_init_t h_crypt1;
uint8_t loop = 0;
uint8_t count = 0;
uint8_t error = 0;
//测试初始数据 128*4位数据
uint32_t plain[16] = {0x3243F6A8, 0x885A308D, 0x313198A2, 0xE0370734,
0x3243F6A8, 0x885A308D, 0x313198A2, 0xE0370734,
0x3243F6A8, 0x885A308D, 0x313198A2, 0xE0370734,
0x3243F6A8, 0x885A308D, 0x313198A2, 0xE0370734};
//用于存放输出数据
uint32_t result[16] = {0};
//存放的正确加密数据
uint32_t expect[16] = {0x925afa80, 0x48a07d7a, 0xd868c5f2, 0xc39468a6,
0x1db9bf55, 0xf0be8867, 0x5f04cbc2, 0xfac9578c,
0xf4a47fad, 0x5961e322, 0xd6a04406, 0x01754289,
0x1c22ee2f, 0xa04608aa, 0xc22f6429,0x9240066d};
//128位的密钥
uint32_t key[4] = {0x2b7e1516, 0x28aed2a6, 0xabf71588, 0x09cf4f3c};
//128位的初始化向量
uint32_t ivr[4] = {0xf0f1f2f3, 0xf4f5f6f7, 0xf8f9fafb, 0xfcfdfeff};
void task1(void *parameter);
void task2(void *parameter);
void task3(void *parameter);
int main()
{
rt_kprintf("The Project is ES32F0654 RT-Thread Demo.........\r\n");
rt_thread_init(&Task1_thread,"task1",
task1,RT_NULL,&rt_task1_thread_stack[0],
sizeof(rt_task1_thread_stack),3,20);
rt_thread_init(&Task2_thread,"task2",
task2,RT_NULL,&rt_task2_thread_stack[0],
sizeof(rt_task2_thread_stack),3,20);
rt_thread_init(&Task3_thread,"task3",
task3,RT_NULL,&rt_task3_thread_stack[0],
sizeof(rt_task3_thread_stack),3,20);
rt_thread_startup(&Task1_thread);
rt_thread_startup(&Task2_thread);
rt_thread_startup(&Task3_thread);
}
//任务1:状态指示
void task1(void *parameter)
{
while(1)
{
LED1_TOGGLE;
LED2_TOGGLE;
rt_thread_delay(500);
rt_kprintf("Thread1 is running.........\r\n");
}
}
unsigned int num;
//任务2:产生一个随机数
void task2(void *parameter)
{
while(1)
{
num = trng_module_get_data();
rt_thread_delay(500);
rt_kprintf("Thread2 is running Generate random number:%d \r\n",num);
}
}
/* aes cbc key = 128bits
* input 32 43 f6 a8 88 5a 30 8d 31 31 98 a2 e0 37 07 34
* 32 43 f6 a8 88 5a 30 8d 31 31 98 a2 e0 37 07 34
* 32 43 f6 a8 88 5a 30 8d 31 31 98 a2 e0 37 07 34
* 32 43 f6 a8 88 5a 30 8d 31 31 98 a2 e0 37 07 34
* cipher key 2b 7e 15 16 28 ae d2 a6 ab f7 15 88 09 cf 4f 3c
* iv f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff
* refer result 92 5a fa 80 48 a0 7d 7a d8 68 c5 f2 c3 94 68 a6
1d b9 bf 55 f0 be 88 67 5f 04 cb c2 fa c9 57 8c
f4 a4 7f ad 59 61 e3 22 d6 a0 44 06 01 75 42 89
1c 22 ee 2f a0 46 08 aa c2 2f 64 29 92 40 06 6d
*/
void test_aes_cbc_128()
{
//开始AES加密
md_crypt_set_key0(CRYPT, key[3]);
md_crypt_set_key1(CRYPT, key[2]);
md_crypt_set_key2(CRYPT, key[1]);
md_crypt_set_key3(CRYPT, key[0]);
md_crypt_set_iv0(CRYPT, ivr[3]);
md_crypt_set_iv1(CRYPT, ivr[2]);
md_crypt_set_iv2(CRYPT, ivr[1]);
md_crypt_set_iv3(CRYPT, ivr[0]);
md_crypt_enable_iv(CRYPT);
md_crypt_encrypt(&h_crypt1,(uint8_t*)plain, (uint8_t*)result, sizeof(plain));
//打印加密后的数据
rt_kprintf("The encrypt result is :\r\n");
for(loop =0;loop<16;loop++)
{
rt_kprintf("0X%08x ",result[loop]);
}
rt_kprintf("\r\n");
//开始AES解密
md_crypt_set_key0(CRYPT, key[3]);
md_crypt_set_key1(CRYPT, key[2]);
md_crypt_set_key2(CRYPT, key[1]);
md_crypt_set_key3(CRYPT, key[0]);
md_crypt_set_iv0(CRYPT, ivr[3]);
md_crypt_set_iv1(CRYPT, ivr[2]);
md_crypt_set_iv2(CRYPT, ivr[1]);
md_crypt_set_iv3(CRYPT, ivr[0]);
md_crypt_enable_iv(CRYPT);
md_crypt_decrypt(&h_crypt1,(uint8_t*)expect, (uint8_t*)result, sizeof(expect));
//打印加密后的数据
rt_kprintf("The decrypt result is :\r\n");
for(loop =0;loop<16;loop++)
{
rt_kprintf("0X%08x ",result[loop]);
}
rt_kprintf("\r\n");
//输出解答密过程的错误率
}
//测试AES-CBC 加密和解密
void task3(void *parameter)
{
while(1)
{
rt_kprintf("Thread3 is running test_aes_cbc_128 \r\n",num);
test_aes_cbc_128();
rt_thread_delay(1000);
}
}