wm20031015 发表于 2022-3-16 22:03

4 测试加密模块

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

<p>演示部分代码如下:</p>

<p><br />
#define TEXT_SIZE (95 * 8)</p>

<p>uint8_t key_128 =&nbsp;<br />
{ &nbsp; 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,<br />
&nbsp; &nbsp; 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c<br />
};<br />
uint8_t key_192 =&nbsp;<br />
{ &nbsp; 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52,<br />
&nbsp; &nbsp; 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5,<br />
&nbsp; &nbsp; 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b<br />
};<br />
uint8_t key_256 =&nbsp;<br />
{ &nbsp; 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,<br />
&nbsp; &nbsp; 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,<br />
&nbsp; &nbsp; 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,<br />
&nbsp; &nbsp; 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4<br />
};</p>

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

<p>};</p>

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

<p>uint8_t encrypt_result;&nbsp; &nbsp; //库有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 &nbsp; &nbsp;CAU_Demo(void)<br />
{<br />
&nbsp; &nbsp; uint8_t i;</p>

<p>&nbsp; &nbsp; cau_key_struct_para_init(&amp;cau_key_config);<br />
&nbsp; &nbsp; cau_iv_struct_para_init(&amp;cau_iv_parameter_config);<br />
&nbsp; &nbsp; cau_context_struct_para_init(&amp;cau_context_parameter_config);</p>

<p>&nbsp; &nbsp; rcu_periph_clock_enable(RCU_CAU);<br />
&nbsp; &nbsp; cau_deinit();<br />
&nbsp; &nbsp; cau_struct_para_init(&amp;cau_parameter_config);</p>

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

<p>&nbsp; &nbsp; //解密,打印解密数据<br />
&nbsp; &nbsp; cau_parameter_config.alg_dir = CAU_DECRYPT;;<br />
&nbsp; &nbsp; cau_parameter_config.key = &amp;key_256;<br />
&nbsp; &nbsp; cau_parameter_config.key_size = 256;<br />
&nbsp; &nbsp; cau_parameter_config.input = &amp;encrypt_result;<br />
&nbsp; &nbsp; cau_parameter_config.in_length = TEXT_SIZE + 8;<br />
&nbsp; &nbsp; cau_aes_ecb(&amp;cau_parameter_config, decrypt_result);<br />
&nbsp; &nbsp; printf(&quot; \r\n\r\nDecrypted Data with AES %d &nbsp;Mode &nbsp;ECB :\r\n\r\n&quot;, &nbsp;cau_parameter_config.key_size);<br />
&nbsp; &nbsp; printf_data(decrypt_result, TEXT_SIZE);</p>

<p>}<br />
运行结果从串口打印,如下图:</p>

<p> &nbsp;</p>

<p>&nbsp;</p>

Jacktang 发表于 2022-3-18 07:29

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

wm20031015 发表于 2022-3-19 09:56

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

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

Jacktang 发表于 2022-3-20 16:16

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

<p>谢谢,了解了</p>
页: [1]
查看完整版本: 4 测试加密模块