[GD32L233C-START 评测] 4 测试加密模块

wm20031015   2022-3-16 22:03 楼主

M23内置了硬件加密模块,对于做通信、bootloader数据安全提供了高效的解决方案,今天简单测试一下AES256的加密、解密,数据是一个自定义的8*8的ascii字体库。‘

演示部分代码如下:


#define TEXT_SIZE (95 * 8)

uint8_t key_128[16] = 
{   0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
    0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
};
uint8_t key_192[24] = 
{   0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,
    0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,
    0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b
};
uint8_t key_256[32] = 
{   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
};

__attribute__((aligned(4))) uint8_t plaintext[TEXT_SIZE] = {
    0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,    //" ",0
0x00,0x00,0x5F,0x00,0x00,0x00,0x00,0x00,    //"!",1
0x00,0x03,0x00,0x03,0x00,0x00,0x00,0x00,    //""",2
0x14,0x7F,0x14,0x7F,0x14,0x00,0x00,0x00,    //"#",3
0x00,0x4C,0x7A,0x4F,0x32,0x00,0x00,0x00,    //"$",4
0x00,0x66,0x16,0x68,0x66,0x00,0x00,0x00,    //"%",5
0x00,0x38,0x4F,0x4D,0x32,0x00,0x00,0x00,    //"&",6
0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,    //"'",7
0x00,0x00,0x3E,0x41,0x00,0x00,0x00,0x00,    //"(",8
0x00,0x00,0x41,0x3E,0x00,0x00,0x00,0x00,    //")",9
0x00,0x1C,0x3E,0x1C,0x00,0x00,0x00,0x00,    //"*",10
0x00,0x08,0x3E,0x08,0x00,0x00,0x00,0x00,    //"+",11
0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,    //",",12
0x00,0x08,0x08,0x08,0x08,0x00,0x00,0x00,    //"-",13
0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,    //".",14
0x00,0x40,0x30,0x0C,0x03,0x00,0x00,0x00,    //"/",15
0x00,0x3E,0x41,0x41,0x3E,0x00,0x00,0x00,    //"0",16
0x00,0x42,0x7F,0x40,0x00,0x00,0x00,0x00,    //"1",17
0x00,0x62,0x51,0x49,0x46,0x00,0x00,0x00,    //"2",18
0x00,0x22,0x49,0x49,0x36,0x00,0x00,0x00,    //"3",19
0x00,0x38,0x26,0x7F,0x20,0x00,0x00,0x00,    //"4",20
0x00,0x4F,0x49,0x49,0x31,0x00,0x00,0x00,    //"5",21
0x00,0x3E,0x49,0x49,0x32,0x00,0x00,0x00,    //"6",22
0x00,0x03,0x71,0x09,0x07,0x00,0x00,0x00,    //"7",23
0x00,0x36,0x49,0x49,0x36,0x00,0x00,0x00,    //"8",24
0x00,0x26,0x49,0x49,0x3E,0x00,0x00,0x00,    //"9",25
0x00,0x00,0x24,0x00,0x00,0x00,0x00,0x00,    //":",26
0x00,0x40,0x24,0x00,0x00,0x00,0x00,0x00,    //";",27
0x00,0x08,0x14,0x22,0x41,0x00,0x00,0x00,    //"<",28
0x00,0x14,0x14,0x14,0x14,0x00,0x00,0x00,    //"=",29
0x00,0x41,0x22,0x14,0x08,0x00,0x00,0x00,    //">",30
0x00,0x02,0x51,0x09,0x06,0x00,0x00,0x00,    //"?",31
0x00,0x0E,0x71,0x49,0x7E,0x00,0x00,0x00,    //"@",32
0x00,0x7E,0x11,0x11,0x7E,0x00,0x00,0x00,    //"A",33
0x00,0x7F,0x49,0x49,0x36,0x00,0x00,0x00,    //"B",34
0x00,0x3E,0x41,0x41,0x22,0x00,0x00,0x00,    //"C",35
0x00,0x7F,0x41,0x41,0x3E,0x00,0x00,0x00,    //"D",36
0x00,0x7F,0x49,0x49,0x41,0x00,0x00,0x00,    //"E",37
0x00,0x7F,0x09,0x09,0x01,0x00,0x00,0x00,    //"F",38
0x00,0x3E,0x41,0x49,0x3A,0x00,0x00,0x00,    //"G",39
0x00,0x7F,0x08,0x08,0x7F,0x00,0x00,0x00,    //"H",40
0x00,0x41,0x7F,0x41,0x00,0x00,0x00,0x00,    //"I",41
0x00,0x30,0x40,0x40,0x3F,0x00,0x00,0x00,    //"J",42
0x00,0x7F,0x08,0x14,0x63,0x00,0x00,0x00,    //"K",43
0x00,0x7F,0x40,0x40,0x40,0x00,0x00,0x00,    //"L",44
0x00,0x7F,0x06,0x06,0x7F,0x00,0x00,0x00,    //"M",45
0x00,0x7F,0x06,0x18,0x7F,0x00,0x00,0x00,    //"N",46
0x00,0x7F,0x41,0x41,0x7F,0x00,0x00,0x00,    //"O",47
0x00,0x7F,0x09,0x09,0x06,0x00,0x00,0x00,    //"P",48
0x00,0x3E,0x51,0x61,0x7E,0x00,0x00,0x00,    //"Q",49
0x00,0x7F,0x19,0x29,0x46,0x00,0x00,0x00,    //"R",50
0x00,0x26,0x49,0x49,0x32,0x00,0x00,0x00,    //"S",51
0x00,0x01,0x7F,0x01,0x01,0x00,0x00,0x00,    //"T",52
0x00,0x3F,0x40,0x40,0x3F,0x00,0x00,0x00,    //"U",53
0x00,0x0F,0x70,0x70,0x0F,0x00,0x00,0x00,    //"V",54
0x00,0x7F,0x30,0x30,0x7F,0x00,0x00,0x00,    //"W",55
0x00,0x63,0x1C,0x1C,0x63,0x00,0x00,0x00,    //"X",56
0x03,0x04,0x78,0x04,0x03,0x00,0x00,0x00,    //"Y",57
0x00,0x61,0x59,0x4D,0x43,0x00,0x00,0x00,    //"Z",58
0x00,0x00,0x7F,0x41,0x00,0x00,0x00,0x00,    //"[",59
0x03,0x0C,0x30,0x40,0x00,0x00,0x00,0x00,    //"\",60
0x00,0x00,0x41,0x7F,0x00,0x00,0x00,0x00,    //"]",61
0x00,0x02,0x01,0x02,0x00,0x00,0x00,0x00,    //"^",62
0x00,0x40,0x40,0x40,0x40,0x00,0x00,0x00,    //"_",63
0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,    //"`",64
0x00,0x20,0x58,0x58,0x78,0x00,0x00,0x00,    //"a",65
0x00,0x7F,0x48,0x48,0x30,0x00,0x00,0x00,    //"b",66
0x00,0x30,0x48,0x48,0x48,0x00,0x00,0x00,    //"c",67
0x00,0x30,0x48,0x48,0x7F,0x00,0x00,0x00,    //"d",68
0x00,0x30,0x58,0x58,0x10,0x00,0x00,0x00,    //"e",69
0x00,0x08,0x7C,0x0A,0x08,0x00,0x00,0x00,    //"f",70
0x00,0x10,0xA8,0xA8,0x78,0x00,0x00,0x00,    //"g",71
0x00,0x7F,0x08,0x78,0x00,0x00,0x00,0x00,    //"h",72
0x00,0x00,0x74,0x00,0x00,0x00,0x00,0x00,    //"i",73
0x00,0x80,0x80,0x74,0x00,0x00,0x00,0x00,    //"j",74
0x00,0x7F,0x10,0x28,0x48,0x00,0x00,0x00,    //"k",75
0x00,0x40,0x7F,0x40,0x00,0x00,0x00,0x00,    //"l",76
0x00,0x78,0x08,0x78,0x78,0x00,0x00,0x00,    //"m",77
0x00,0x78,0x08,0x08,0x70,0x00,0x00,0x00,    //"n",78
0x00,0x30,0x48,0x48,0x30,0x00,0x00,0x00,    //"o",79
0x00,0xF8,0x48,0x48,0x30,0x00,0x00,0x00,    //"p",80
0x00,0x30,0x48,0x48,0xF8,0x00,0x00,0x00,    //"q",81
0x00,0x78,0x10,0x08,0x08,0x00,0x00,0x00,    //"r",82
0x00,0x50,0x58,0x68,0x28,0x00,0x00,0x00,    //"s",83
0x00,0x08,0x7C,0x48,0x00,0x00,0x00,0x00,    //"t",84
0x00,0x38,0x40,0x40,0x78,0x00,0x00,0x00,    //"u",85
0x00,0x18,0x60,0x60,0x18,0x00,0x00,0x00,    //"v",86
0x18,0x60,0x38,0x60,0x18,0x00,0x00,0x00,    //"w",87
0x00,0x48,0x30,0x30,0x48,0x00,0x00,0x00,    //"x",88
0x00,0x18,0xA0,0xA0,0x78,0x00,0x00,0x00,    //"y",89
0x00,0x48,0x68,0x58,0x48,0x00,0x00,0x00,    //"z",90
0x00,0x08,0x77,0x41,0x00,0x00,0x00,0x00,    //"{",91
0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00,    //"|",92
0x00,0x41,0x77,0x08,0x00,0x00,0x00,0x00,    //"}",93
0x00,0x02,0x01,0x02,0x01,0x00,0x00,0x00,    //"~",94

};

__attribute__((aligned(4))) uint8_t ciphertext[TEXT_SIZE] = {
    0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
    0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
    0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
    0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
    0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b,
    0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
    0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09,
    0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7
};

uint8_t encrypt_result[TEXT_SIZE + 8];    //库有bug,数据长度大的时候,最后8字节解密失败,原因待查,临时增加8个字节的数据,这样前面的数据可以正常解密
uint8_t decrypt_result[TEXT_SIZE + 8];
cau_parameter_struct cau_parameter_config;
cau_key_parameter_struct cau_key_config;
cau_iv_parameter_struct cau_iv_parameter_config;
cau_context_parameter_struct cau_context_parameter_config;

void    CAU_Demo(void)
{
    uint8_t i;

    cau_key_struct_para_init(&cau_key_config);
    cau_iv_struct_para_init(&cau_iv_parameter_config);
    cau_context_struct_para_init(&cau_context_parameter_config);

    rcu_periph_clock_enable(RCU_CAU);
    cau_deinit();
    cau_struct_para_init(&cau_parameter_config);

    //打印原始数据
    printf(" \r\n\r\nSource Data %d byte :\r\n\r\n", TEXT_SIZE);
    printf_data(plaintext, TEXT_SIZE);
    
    //加密,打印加密数据
    cau_parameter_config.alg_dir = CAU_ENCRYPT;
    cau_parameter_config.key = &key_256[0];
    cau_parameter_config.key_size = 256;
    cau_parameter_config.input = &plaintext[0];
    cau_parameter_config.in_length = TEXT_SIZE + 8;
    cau_aes_ecb(&cau_parameter_config, encrypt_result);
    
    printf(" \r\n\r\nEncrypted Data with AES %d  Mode  ECB :\r\n\r\n", cau_parameter_config.key_size);
    printf_data(encrypt_result, TEXT_SIZE);

    //解密,打印解密数据
    cau_parameter_config.alg_dir = CAU_DECRYPT;;
    cau_parameter_config.key = &key_256[0];
    cau_parameter_config.key_size = 256;
    cau_parameter_config.input = &encrypt_result[0];
    cau_parameter_config.in_length = TEXT_SIZE + 8;
    cau_aes_ecb(&cau_parameter_config, decrypt_result);
    printf(" \r\n\r\nDecrypted Data with AES %d  Mode  ECB :\r\n\r\n",  cau_parameter_config.key_size);
    printf_data(decrypt_result, TEXT_SIZE);

}
运行结果从串口打印,如下图:

image.png  

 

回复评论 (3)

这个加密模块测试代码的流程是个什么

点赞  2022-3-18 07:29
引用: Jacktang 发表于 2022-3-18 07:29 这个加密模块测试代码的流程是个什么

开加密模块时钟,初始化化加密模块,配置密钥数据指针、密钥长度,加密数据指针、数据长度,打印出原始数据,对数据加密后,存放在加密数据缓冲区,打印加密数据。对加密模块进行解密配置,然后对加密数据进行解密,存放在解密数据缓冲区,打印解密后的数据。

点赞  2022-3-19 09:56
引用: wm20031015 发表于 2022-3-19 09:56 开加密模块时钟,初始化化加密模块,配置密钥数据指针、密钥长度,加密数据指针、数据长度,打印出原始数 ...

谢谢,了解了

点赞  2022-3-20 16:16
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复