4 测试加密模块
<p>M23内置了硬件加密模块,对于做通信、bootloader数据安全提供了高效的解决方案,今天简单测试一下AES256的加密、解密,数据是一个自定义的8*8的ascii字体库。‘</p><p>演示部分代码如下:</p>
<p><br />
#define TEXT_SIZE (95 * 8)</p>
<p>uint8_t key_128 = <br />
{ 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,<br />
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c<br />
};<br />
uint8_t key_192 = <br />
{ 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,<br />
0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,<br />
0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b<br />
};<br />
uint8_t key_256 = <br />
{ 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,<br />
0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,<br />
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,<br />
0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4<br />
};</p>
<p>__attribute__((aligned(4))) uint8_t plaintext = {<br />
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //" ",0<br />
0x00,0x00,0x5F,0x00,0x00,0x00,0x00,0x00, //"!",1<br />
0x00,0x03,0x00,0x03,0x00,0x00,0x00,0x00, //""",2<br />
0x14,0x7F,0x14,0x7F,0x14,0x00,0x00,0x00, //"#",3<br />
0x00,0x4C,0x7A,0x4F,0x32,0x00,0x00,0x00, //"$",4<br />
0x00,0x66,0x16,0x68,0x66,0x00,0x00,0x00, //"%",5<br />
0x00,0x38,0x4F,0x4D,0x32,0x00,0x00,0x00, //"&",6<br />
0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00, //"'",7<br />
0x00,0x00,0x3E,0x41,0x00,0x00,0x00,0x00, //"(",8<br />
0x00,0x00,0x41,0x3E,0x00,0x00,0x00,0x00, //")",9<br />
0x00,0x1C,0x3E,0x1C,0x00,0x00,0x00,0x00, //"*",10<br />
0x00,0x08,0x3E,0x08,0x00,0x00,0x00,0x00, //"+",11<br />
0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00, //",",12<br />
0x00,0x08,0x08,0x08,0x08,0x00,0x00,0x00, //"-",13<br />
0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00, //".",14<br />
0x00,0x40,0x30,0x0C,0x03,0x00,0x00,0x00, //"/",15<br />
0x00,0x3E,0x41,0x41,0x3E,0x00,0x00,0x00, //"0",16<br />
0x00,0x42,0x7F,0x40,0x00,0x00,0x00,0x00, //"1",17<br />
0x00,0x62,0x51,0x49,0x46,0x00,0x00,0x00, //"2",18<br />
0x00,0x22,0x49,0x49,0x36,0x00,0x00,0x00, //"3",19<br />
0x00,0x38,0x26,0x7F,0x20,0x00,0x00,0x00, //"4",20<br />
0x00,0x4F,0x49,0x49,0x31,0x00,0x00,0x00, //"5",21<br />
0x00,0x3E,0x49,0x49,0x32,0x00,0x00,0x00, //"6",22<br />
0x00,0x03,0x71,0x09,0x07,0x00,0x00,0x00, //"7",23<br />
0x00,0x36,0x49,0x49,0x36,0x00,0x00,0x00, //"8",24<br />
0x00,0x26,0x49,0x49,0x3E,0x00,0x00,0x00, //"9",25<br />
0x00,0x00,0x24,0x00,0x00,0x00,0x00,0x00, //":",26<br />
0x00,0x40,0x24,0x00,0x00,0x00,0x00,0x00, //";",27<br />
0x00,0x08,0x14,0x22,0x41,0x00,0x00,0x00, //"<",28<br />
0x00,0x14,0x14,0x14,0x14,0x00,0x00,0x00, //"=",29<br />
0x00,0x41,0x22,0x14,0x08,0x00,0x00,0x00, //">",30<br />
0x00,0x02,0x51,0x09,0x06,0x00,0x00,0x00, //"?",31<br />
0x00,0x0E,0x71,0x49,0x7E,0x00,0x00,0x00, //"@",32<br />
0x00,0x7E,0x11,0x11,0x7E,0x00,0x00,0x00, //"A",33<br />
0x00,0x7F,0x49,0x49,0x36,0x00,0x00,0x00, //"B",34<br />
0x00,0x3E,0x41,0x41,0x22,0x00,0x00,0x00, //"C",35<br />
0x00,0x7F,0x41,0x41,0x3E,0x00,0x00,0x00, //"D",36<br />
0x00,0x7F,0x49,0x49,0x41,0x00,0x00,0x00, //"E",37<br />
0x00,0x7F,0x09,0x09,0x01,0x00,0x00,0x00, //"F",38<br />
0x00,0x3E,0x41,0x49,0x3A,0x00,0x00,0x00, //"G",39<br />
0x00,0x7F,0x08,0x08,0x7F,0x00,0x00,0x00, //"H",40<br />
0x00,0x41,0x7F,0x41,0x00,0x00,0x00,0x00, //"I",41<br />
0x00,0x30,0x40,0x40,0x3F,0x00,0x00,0x00, //"J",42<br />
0x00,0x7F,0x08,0x14,0x63,0x00,0x00,0x00, //"K",43<br />
0x00,0x7F,0x40,0x40,0x40,0x00,0x00,0x00, //"L",44<br />
0x00,0x7F,0x06,0x06,0x7F,0x00,0x00,0x00, //"M",45<br />
0x00,0x7F,0x06,0x18,0x7F,0x00,0x00,0x00, //"N",46<br />
0x00,0x7F,0x41,0x41,0x7F,0x00,0x00,0x00, //"O",47<br />
0x00,0x7F,0x09,0x09,0x06,0x00,0x00,0x00, //"P",48<br />
0x00,0x3E,0x51,0x61,0x7E,0x00,0x00,0x00, //"Q",49<br />
0x00,0x7F,0x19,0x29,0x46,0x00,0x00,0x00, //"R",50<br />
0x00,0x26,0x49,0x49,0x32,0x00,0x00,0x00, //"S",51<br />
0x00,0x01,0x7F,0x01,0x01,0x00,0x00,0x00, //"T",52<br />
0x00,0x3F,0x40,0x40,0x3F,0x00,0x00,0x00, //"U",53<br />
0x00,0x0F,0x70,0x70,0x0F,0x00,0x00,0x00, //"V",54<br />
0x00,0x7F,0x30,0x30,0x7F,0x00,0x00,0x00, //"W",55<br />
0x00,0x63,0x1C,0x1C,0x63,0x00,0x00,0x00, //"X",56<br />
0x03,0x04,0x78,0x04,0x03,0x00,0x00,0x00, //"Y",57<br />
0x00,0x61,0x59,0x4D,0x43,0x00,0x00,0x00, //"Z",58<br />
0x00,0x00,0x7F,0x41,0x00,0x00,0x00,0x00, //"[",59<br />
0x03,0x0C,0x30,0x40,0x00,0x00,0x00,0x00, //"\",60<br />
0x00,0x00,0x41,0x7F,0x00,0x00,0x00,0x00, //"]",61<br />
0x00,0x02,0x01,0x02,0x00,0x00,0x00,0x00, //"^",62<br />
0x00,0x40,0x40,0x40,0x40,0x00,0x00,0x00, //"_",63<br />
0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00, //"`",64<br />
0x00,0x20,0x58,0x58,0x78,0x00,0x00,0x00, //"a",65<br />
0x00,0x7F,0x48,0x48,0x30,0x00,0x00,0x00, //"b",66<br />
0x00,0x30,0x48,0x48,0x48,0x00,0x00,0x00, //"c",67<br />
0x00,0x30,0x48,0x48,0x7F,0x00,0x00,0x00, //"d",68<br />
0x00,0x30,0x58,0x58,0x10,0x00,0x00,0x00, //"e",69<br />
0x00,0x08,0x7C,0x0A,0x08,0x00,0x00,0x00, //"f",70<br />
0x00,0x10,0xA8,0xA8,0x78,0x00,0x00,0x00, //"g",71<br />
0x00,0x7F,0x08,0x78,0x00,0x00,0x00,0x00, //"h",72<br />
0x00,0x00,0x74,0x00,0x00,0x00,0x00,0x00, //"i",73<br />
0x00,0x80,0x80,0x74,0x00,0x00,0x00,0x00, //"j",74<br />
0x00,0x7F,0x10,0x28,0x48,0x00,0x00,0x00, //"k",75<br />
0x00,0x40,0x7F,0x40,0x00,0x00,0x00,0x00, //"l",76<br />
0x00,0x78,0x08,0x78,0x78,0x00,0x00,0x00, //"m",77<br />
0x00,0x78,0x08,0x08,0x70,0x00,0x00,0x00, //"n",78<br />
0x00,0x30,0x48,0x48,0x30,0x00,0x00,0x00, //"o",79<br />
0x00,0xF8,0x48,0x48,0x30,0x00,0x00,0x00, //"p",80<br />
0x00,0x30,0x48,0x48,0xF8,0x00,0x00,0x00, //"q",81<br />
0x00,0x78,0x10,0x08,0x08,0x00,0x00,0x00, //"r",82<br />
0x00,0x50,0x58,0x68,0x28,0x00,0x00,0x00, //"s",83<br />
0x00,0x08,0x7C,0x48,0x00,0x00,0x00,0x00, //"t",84<br />
0x00,0x38,0x40,0x40,0x78,0x00,0x00,0x00, //"u",85<br />
0x00,0x18,0x60,0x60,0x18,0x00,0x00,0x00, //"v",86<br />
0x18,0x60,0x38,0x60,0x18,0x00,0x00,0x00, //"w",87<br />
0x00,0x48,0x30,0x30,0x48,0x00,0x00,0x00, //"x",88<br />
0x00,0x18,0xA0,0xA0,0x78,0x00,0x00,0x00, //"y",89<br />
0x00,0x48,0x68,0x58,0x48,0x00,0x00,0x00, //"z",90<br />
0x00,0x08,0x77,0x41,0x00,0x00,0x00,0x00, //"{",91<br />
0x00,0x00,0x7F,0x00,0x00,0x00,0x00,0x00, //"|",92<br />
0x00,0x41,0x77,0x08,0x00,0x00,0x00,0x00, //"}",93<br />
0x00,0x02,0x01,0x02,0x01,0x00,0x00,0x00, //"~",94</p>
<p>};</p>
<p>__attribute__((aligned(4))) uint8_t ciphertext = {<br />
0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,<br />
0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,<br />
0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,<br />
0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,<br />
0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b,<br />
0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,<br />
0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09,<br />
0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7<br />
};</p>
<p>uint8_t encrypt_result; //库有bug,数据长度大的时候,最后8字节解密失败,原因待查,临时增加8个字节的数据,这样前面的数据可以正常解密<br />
uint8_t decrypt_result;<br />
cau_parameter_struct cau_parameter_config;<br />
cau_key_parameter_struct cau_key_config;<br />
cau_iv_parameter_struct cau_iv_parameter_config;<br />
cau_context_parameter_struct cau_context_parameter_config;</p>
<p>void CAU_Demo(void)<br />
{<br />
uint8_t i;</p>
<p> cau_key_struct_para_init(&cau_key_config);<br />
cau_iv_struct_para_init(&cau_iv_parameter_config);<br />
cau_context_struct_para_init(&cau_context_parameter_config);</p>
<p> rcu_periph_clock_enable(RCU_CAU);<br />
cau_deinit();<br />
cau_struct_para_init(&cau_parameter_config);</p>
<p> //打印原始数据<br />
printf(" \r\n\r\nSource Data %d byte :\r\n\r\n", TEXT_SIZE);<br />
printf_data(plaintext, TEXT_SIZE);<br />
<br />
//加密,打印加密数据<br />
cau_parameter_config.alg_dir = CAU_ENCRYPT;<br />
cau_parameter_config.key = &key_256;<br />
cau_parameter_config.key_size = 256;<br />
cau_parameter_config.input = &plaintext;<br />
cau_parameter_config.in_length = TEXT_SIZE + 8;<br />
cau_aes_ecb(&cau_parameter_config, encrypt_result);<br />
<br />
printf(" \r\n\r\nEncrypted Data with AES %d Mode ECB :\r\n\r\n", cau_parameter_config.key_size);<br />
printf_data(encrypt_result, TEXT_SIZE);</p>
<p> //解密,打印解密数据<br />
cau_parameter_config.alg_dir = CAU_DECRYPT;;<br />
cau_parameter_config.key = &key_256;<br />
cau_parameter_config.key_size = 256;<br />
cau_parameter_config.input = &encrypt_result;<br />
cau_parameter_config.in_length = TEXT_SIZE + 8;<br />
cau_aes_ecb(&cau_parameter_config, decrypt_result);<br />
printf(" \r\n\r\nDecrypted Data with AES %d Mode ECB :\r\n\r\n", cau_parameter_config.key_size);<br />
printf_data(decrypt_result, TEXT_SIZE);</p>
<p>}<br />
运行结果从串口打印,如下图:</p>
<p> </p>
<p> </p>
<p>这个加密模块测试代码的流程是个什么</p>
Jacktang 发表于 2022-3-18 07:29
这个加密模块测试代码的流程是个什么
<p>开加密模块时钟,初始化化加密模块,配置密钥数据指针、密钥长度,加密数据指针、数据长度,打印出原始数据,对数据加密后,存放在加密数据缓冲区,打印加密数据。对加密模块进行解密配置,然后对加密数据进行解密,存放在解密数据缓冲区,打印解密后的数据。</p>
wm20031015 发表于 2022-3-19 09:56
开加密模块时钟,初始化化加密模块,配置密钥数据指针、密钥长度,加密数据指针、数据长度,打印出原始数 ...
<p>谢谢,了解了</p>
页:
[1]