[Atmel] SAML21走起14:先进的加密解密基础

[复制链接]
1443|1
 楼主| ddllxxrr 发表于 2015-8-13 21:56 | 显示全部楼层 |阅读模式
AES-Advanced Encryption Standard Module,先进的加密解密模块。
这个程序,就是先加密然后解密。

根据快速指导,形成程序。然后编译程序。
在编译的过程中有一个错误总是出现

我加入了所有的USART模块,编译后还是不行。

最后我仔细地看了下,所有模块发现Standard serial I/O(stdio)(driver)没有加,加进后。编译成功。


以下是错误提示:



编译的程序如下:

  1. #include <asf.h>
  2. #define AES_EXAMPLE_REFBUF_SIZE 4
  3. /* @{ */
  4.         uint32_t ref_plain_text[AES_EXAMPLE_REFBUF_SIZE] = {
  5.                 0xe2bec16b,
  6.                 0x969f402e,
  7.                 0x117e3de9,
  8.                 0x2a179373
  9.         };
  10.         uint32_t ref_cipher_text_ecb[AES_EXAMPLE_REFBUF_SIZE] = {
  11.                 0xb47bd73a,
  12.                 0x60367a0d,
  13.                 0xf3ca9ea8,
  14.                 0x97ef6624
  15.         };
  16.         const uint32_t key128[4] = {
  17.                 0x16157e2b,
  18.                 0xa6d2ae28,
  19.                 0x8815f7ab,
  20.                 0x3c4fcf09
  21.         };
  22.        
  23. /* Output data array */
  24. static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE];
  25. /* State indicate */
  26. volatile bool state = false;
  27. /* AES configuration */
  28. struct aes_config g_aes_cfg;
  29. /* AES instance*/
  30. struct aes_module aes_instance;
  31. struct usart_module usart_instance;

  32. static void configure_usart(void);
  33. static void ecb_mode_test(void);
  34. static void configure_usart(void)
  35. {
  36.         struct usart_config config_usart;
  37.         usart_get_config_defaults(&config_usart);
  38.         config_usart.baudrate    = 38400;
  39.         config_usart.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
  40.         config_usart.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
  41.         config_usart.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
  42.         config_usart.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
  43.         config_usart.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;
  44.         stdio_serial_init(&usart_instance, EDBG_CDC_MODULE, &config_usart);
  45.         usart_enable(&usart_instance);
  46. }
  47. static void ecb_mode_test(void)
  48. {
  49.         printf("\r\n-----------------------------------\r\n");
  50.         printf("- 128bit cryptographic key\r\n");
  51.         printf("- ECB cipher mode\r\n");
  52.         printf("- Auto start mode\r\n");
  53.         printf("- 4 32bit words\r\n");
  54.         printf("-----------------------------------\r\n");
  55.         state = false;
  56.         /* Configure the AES. */
  57.         g_aes_cfg.encrypt_mode = AES_ENCRYPTION;
  58.         g_aes_cfg.key_size = AES_KEY_SIZE_128;
  59.         g_aes_cfg.start_mode = AES_AUTO_START;
  60.         g_aes_cfg.opmode = AES_ECB_MODE;
  61.         g_aes_cfg.cfb_size = AES_CFB_SIZE_128;
  62.         g_aes_cfg.lod = false;
  63.         aes_set_config(&aes_instance,AES, &g_aes_cfg);
  64.         /* Set the cryptographic key. */
  65.         aes_write_key(&aes_instance, key128);
  66.         /* The initialization vector is not used by the ECB cipher mode. */
  67.         aes_set_new_message(&aes_instance);
  68.         /* Write the data to be ciphered to the input data registers. */
  69.         aes_write_input_data(&aes_instance, ref_plain_text);
  70.         aes_clear_new_message(&aes_instance);
  71.         /* Wait for the end of the encryption process. */
  72.         while (!(aes_get_status(&aes_instance) & AES_ENCRYPTION_COMPLETE)) {
  73.         }
  74.         aes_read_output_data(&aes_instance,output_data);
  75.         if ((ref_cipher_text_ecb[0] != output_data[0]) ||
  76.         (ref_cipher_text_ecb[1] != output_data[1]) ||
  77.         (ref_cipher_text_ecb[2] != output_data[2]) ||
  78.         (ref_cipher_text_ecb[3] != output_data[3])) {
  79.                 printf("\r\nKO!!!\r\n");
  80.                 } else {
  81.                 printf("\r\nOK!!!\r\n");
  82.         }
  83.         printf("\r\n-----------------------------------\r\n");
  84.         printf("- 128bit cryptographic key\r\n");
  85.         printf("- ECB decipher mode\r\n");
  86.         printf("- Auto start mode\r\n");
  87.         printf("- 4 32bit words\r\n");
  88.         printf("-----------------------------------\r\n");
  89.         state = false;
  90.         /* Configure the AES. */
  91.         g_aes_cfg.encrypt_mode = AES_DECRYPTION;
  92.         g_aes_cfg.key_size = AES_KEY_SIZE_128;
  93.         g_aes_cfg.start_mode = AES_AUTO_START;
  94.         g_aes_cfg.opmode = AES_ECB_MODE;
  95.         g_aes_cfg.cfb_size = AES_CFB_SIZE_128;
  96.         g_aes_cfg.lod = false;
  97.         aes_set_config(&aes_instance,AES, &g_aes_cfg);
  98.         /* Set the cryptographic key. */
  99.         aes_write_key(&aes_instance, key128);
  100.         /* The initialization vector is not used by the ECB cipher mode. */
  101.         /* Write the data to be deciphered to the input data registers. */
  102.         aes_write_input_data(&aes_instance, ref_cipher_text_ecb);
  103.         /* Wait for the end of the decryption process. */
  104.         while (!(aes_get_status(&aes_instance) & AES_ENCRYPTION_COMPLETE)) {
  105.         }
  106.         aes_read_output_data(&aes_instance,output_data);
  107.         /* check the result. */
  108.         if ((ref_plain_text[0] != output_data[0]) ||
  109.         (ref_plain_text[1] != output_data[1]) ||
  110.         (ref_plain_text[2] != output_data[2]) ||
  111.         (ref_plain_text[3] != output_data[3])) {
  112.                 printf("\r\nKO!!!\r\n");
  113.                 } else {
  114.                 printf("\r\nOK!!!\r\n");
  115.         }
  116. }


  117. int main (void)
  118. {
  119.        
  120.           /* Initialize the system and console*/
  121.       system_init();
  122.       configure_usart();
  123.       aes_get_config_defaults(&g_aes_cfg);
  124.       aes_init(&aes_instance,AES, &g_aes_cfg);
  125.       aes_enable(&aes_instance);
  126.           
  127.           ecb_mode_test();
  128.        
  129. }
运行结果如下:



这个波特率是38400

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
菁菁木华1 发表于 2015-8-15 13:13 | 显示全部楼层
我是来绑定的,学习了学习了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2404

主题

7008

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部