[Atmel] SAML21走起15:加密解密CALLBACK模式

[复制链接]
831|0
 楼主| ddllxxrr 发表于 2015-8-18 21:15 | 显示全部楼层 |阅读模式
本帖最后由 ddllxxrr 于 2015-8-18 21:17 编辑

CALLBACK模式只是在加密或解密结束后自动设一个标志而已,同上次的基本一样。
首先打开快速指导的加密解密CALLBACK模式。
按照指导步骤完成程序。然后编译,以下程序一次通过。可见该程序执行了以下操作:
  • ECB encryption and decryption
  • CBC encryption and decryption
  • CFB128 encryption and decryption
  • OFB encryption and decryption
  • CTR encryption and decryption


每次执行完加密解密都调一下中断,这就是CALLBACK模式

程序如下:
  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.         uint32_t ref_cipher_text_cbc[AES_EXAMPLE_REFBUF_SIZE] = {
  17.                 0xacab4976,
  18.                 0x46b21981,
  19.                 0x9b8ee9ce,
  20.                 0x7d19e912
  21.         };
  22.         uint32_t ref_cipher_text_cfb128[AES_EXAMPLE_REFBUF_SIZE] = {
  23.                 0x2ed93f3b,
  24.                 0x20ad2db7,
  25.                 0xf8493433,
  26.                 0x4afb3ce8
  27.         };
  28.         uint32_t ref_cipher_text_ofb[AES_EXAMPLE_REFBUF_SIZE] = {
  29.                 0x2ed93f3b,
  30.                 0x20ad2db7,
  31.                 0xf8493433,
  32.                 0x4afb3ce8
  33.         };
  34.         uint32_t ref_cipher_text_ctr[AES_EXAMPLE_REFBUF_SIZE] = {
  35.                 0x91614d87,
  36.                 0x26e320b6,
  37.                 0x6468ef1b,
  38.                 0xceb60d99
  39.         };
  40.         const uint32_t key128[4] = {
  41.                 0x16157e2b,
  42.                 0xa6d2ae28,
  43.                 0x8815f7ab,
  44.                 0x3c4fcf09
  45.         };
  46.         const uint32_t init_vector[4] = {
  47.                 0x03020100,
  48.                 0x07060504,
  49.                 0x0b0a0908,
  50.                 0x0f0e0d0c
  51.         };
  52.         const uint32_t init_vector_ctr[4] = {
  53.                 0xf3f2f1f0,
  54.                 0xf7f6f5f4,
  55.                 0xfbfaf9f8,
  56.                 0xfffefdfc
  57.         };


  58. /* Output data array */
  59. static uint32_t output_data[AES_EXAMPLE_REFBUF_SIZE];
  60. /* State indicate */
  61. volatile bool state = false;
  62. struct aes_config g_aes_cfg;
  63. struct aes_module aes_instance;
  64. struct usart_module usart_instance;


  65. static void configure_usart(void)
  66. {
  67.         struct usart_config config_usart;
  68.         usart_get_config_defaults(&config_usart);
  69.         config_usart.baudrate    = 38400;
  70.         config_usart.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
  71.         config_usart.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
  72.         config_usart.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
  73.         config_usart.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
  74.         config_usart.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;
  75.         stdio_serial_init(&usart_instance, EDBG_CDC_MODULE, &config_usart);
  76.         usart_enable(&usart_instance);
  77. }
  78. static void aes_callback(void)
  79. {
  80.         /* Read the output. */
  81.         aes_read_output_data(&aes_instance, output_data);
  82.         state = true;
  83. }
  84. static void ecb_mode_test(void)
  85. {
  86.         printf("\r\n-----------------------------------\r\n");
  87.         printf("- 128bit cryptographic key\r\n");
  88.         printf("- ECB cipher mode\r\n");
  89.         printf("- Auto start mode\r\n");
  90.         printf("- 4 32bit words\r\n");
  91.         printf("-----------------------------------\r\n");
  92.         state = false;
  93.         /* Configure the AES. */
  94.         g_aes_cfg.encrypt_mode = AES_ENCRYPTION;
  95.         g_aes_cfg.key_size = AES_KEY_SIZE_128;
  96.         g_aes_cfg.start_mode = AES_AUTO_START;
  97.         g_aes_cfg.opmode = AES_ECB_MODE;
  98.         g_aes_cfg.cfb_size = AES_CFB_SIZE_128;
  99.         g_aes_cfg.lod = false;
  100.         aes_set_config(&aes_instance,AES, &g_aes_cfg);
  101.         /* Set the cryptographic key. */
  102.         aes_write_key(&aes_instance, key128);
  103.         /* The initialization vector is not used by the ECB cipher mode. */
  104.         aes_set_new_message(&aes_instance);
  105.         /* Write the data to be ciphered to the input data registers. */
  106.         aes_write_input_data(&aes_instance, ref_plain_text);
  107.         aes_clear_new_message(&aes_instance);
  108.         /* Wait for the end of the encryption process. */
  109.         while (false == state) {
  110.         }
  111.         if ((ref_cipher_text_ecb[0] != output_data[0]) ||
  112.         (ref_cipher_text_ecb[1] != output_data[1]) ||
  113.         (ref_cipher_text_ecb[2] != output_data[2]) ||
  114.         (ref_cipher_text_ecb[3] != output_data[3])) {
  115.                 printf("\r\nKO!!!\r\n");
  116.                 } else {
  117.                 printf("\r\nOK!!!\r\n");
  118.         }
  119.         printf("\r\n-----------------------------------\r\n");
  120.         printf("- 128bit cryptographic key\r\n");
  121.         printf("- ECB decipher mode\r\n");
  122.         printf("- Auto start mode\r\n");
  123.         printf("- 4 32bit words\r\n");
  124.         printf("-----------------------------------\r\n");
  125.         state = false;
  126.         /* Configure the AES. */
  127.         g_aes_cfg.encrypt_mode = AES_DECRYPTION;
  128.         g_aes_cfg.key_size = AES_KEY_SIZE_128;
  129.         g_aes_cfg.start_mode = AES_AUTO_START;
  130.         g_aes_cfg.opmode = AES_ECB_MODE;
  131.         g_aes_cfg.cfb_size = AES_CFB_SIZE_128;
  132.         g_aes_cfg.lod = false;
  133.         aes_set_config(&aes_instance,AES, &g_aes_cfg);
  134.         /* Set the cryptographic key. */
  135.         aes_write_key(&aes_instance, key128);
  136.         /* The initialization vector is not used by the ECB cipher mode. */
  137.         aes_set_new_message(&aes_instance);
  138.         /* Write the data to be deciphered to the input data registers. */
  139.         aes_write_input_data(&aes_instance, ref_cipher_text_ecb);
  140.         aes_clear_new_message(&aes_instance);
  141.         /* Wait for the end of the decryption process. */
  142.         while (false == state) {
  143.         }
  144.         /* check the result. */
  145.         if ((ref_plain_text[0] != output_data[0]) ||
  146.         (ref_plain_text[1] != output_data[1]) ||
  147.         (ref_plain_text[2] != output_data[2]) ||
  148.         (ref_plain_text[3] != output_data[3])) {
  149.                 printf("\r\nKO!!!\r\n");
  150.                 } else {
  151.                 printf("\r\nOK!!!\r\n");
  152.         }
  153. }
  154. static void cbc_mode_test(void)
  155. {
  156.         printf("\r\n-----------------------------------\r\n");
  157.         printf("- 128bit cryptographic key\r\n");
  158.         printf("- CBC cipher mode\r\n");
  159.         printf("- Auto start mode\r\n");
  160.         printf("- 4 32bit words\r\n");
  161.         printf("-----------------------------------\r\n");
  162.         state = false;
  163.         /* Configure the AES. */
  164.         g_aes_cfg.encrypt_mode = AES_ENCRYPTION;
  165.         g_aes_cfg.key_size = AES_KEY_SIZE_128;
  166.         g_aes_cfg.start_mode = AES_MANUAL_START;
  167.         g_aes_cfg.opmode = AES_CBC_MODE;
  168.         g_aes_cfg.cfb_size = AES_CFB_SIZE_128;
  169.         g_aes_cfg.lod = false;
  170.         aes_set_config(&aes_instance,AES, &g_aes_cfg);
  171.         /* Set the cryptographic key. */
  172.         aes_write_key(&aes_instance, key128);
  173.         /* Set the initialization vector. */
  174.         aes_write_init_vector(&aes_instance, init_vector);
  175.         /* Write the data to be ciphered to the input data registers. */
  176.         aes_write_input_data(&aes_instance, ref_plain_text);
  177.         aes_set_new_message(&aes_instance);
  178.         aes_start(&aes_instance);
  179.         aes_clear_new_message(&aes_instance);
  180.         /* Wait for the end of the encryption process. */
  181.         while (false == state) {
  182.         }
  183.         if ((ref_cipher_text_cbc[0] != output_data[0]) ||
  184.         (ref_cipher_text_cbc[1] != output_data[1]) ||
  185.         (ref_cipher_text_cbc[2] != output_data[2]) ||
  186.         (ref_cipher_text_cbc[3] != output_data[3])) {
  187.                 printf("\r\nKO!!!\r\n");
  188.                 } else {
  189.                 printf("\r\nOK!!!\r\n");
  190.         }
  191.         printf("\r\n-----------------------------------\r\n");
  192.         printf("- 128bit cryptographic key\r\n");
  193.         printf("- CBC decipher mode\r\n");
  194.         printf("- Auto start mode\r\n");
  195.         printf("- 4 32bit words\r\n");
  196.         printf("-----------------------------------\r\n");
  197.         state = false;
  198.         /* Configure the AES. */
  199.         g_aes_cfg.encrypt_mode = AES_DECRYPTION;
  200.         g_aes_cfg.key_size = AES_KEY_SIZE_128;
  201.         g_aes_cfg.start_mode = AES_AUTO_START;
  202.         g_aes_cfg.opmode = AES_CBC_MODE;
  203.         g_aes_cfg.cfb_size = AES_CFB_SIZE_128;
  204.         g_aes_cfg.lod = false;
  205.         aes_set_config(&aes_instance,AES, &g_aes_cfg);
  206.         /* Set the cryptographic key. */
  207.         aes_write_key(&aes_instance, key128);
  208.         /* Set the initialization vector. */
  209.         aes_write_init_vector(&aes_instance, init_vector);
  210.         aes_set_new_message(&aes_instance);
  211.         /* Write the data to be deciphered to the input data registers. */
  212.         aes_write_input_data(&aes_instance, ref_cipher_text_cbc);
  213.         aes_clear_new_message(&aes_instance);
  214.         /* Wait for the end of the decryption process. */
  215.         while (false == state) {
  216.         }
  217.         if ((ref_plain_text[0] != output_data[0]) ||
  218.         (ref_plain_text[1] != output_data[1]) ||
  219.         (ref_plain_text[2] != output_data[2]) ||
  220.         (ref_plain_text[3] != output_data[3])) {
  221.                 printf("\r\nKO!!!\r\n");
  222.                 } else {
  223.                 printf("\r\nOK!!!\r\n");
  224.         }
  225. }
  226. static void cfb128_mode_test(void)
  227. {
  228.         printf("\r\n-----------------------------------\r\n");
  229.         printf("- 128bit cryptographic key\r\n");
  230.         printf("- CFB128 cipher mode\r\n");
  231.         printf("- Auto start mode\r\n");
  232.         printf("- 4 32bit words\r\n");
  233.         printf("-----------------------------------\r\n");
  234.         state = false;
  235.         /* Configure the AES. */
  236.         g_aes_cfg.encrypt_mode = AES_ENCRYPTION;
  237.         g_aes_cfg.key_size = AES_KEY_SIZE_128;
  238.         g_aes_cfg.start_mode = AES_MANUAL_START;
  239.         g_aes_cfg.opmode = AES_CFB_MODE;
  240.         g_aes_cfg.cfb_size = AES_CFB_SIZE_128;
  241.         g_aes_cfg.lod = false;
  242.         aes_set_config(&aes_instance,AES, &g_aes_cfg);
  243.         /* Set the cryptographic key. */
  244.         aes_write_key(&aes_instance, key128);
  245.         /* Set the initialization vector. */
  246.         aes_write_init_vector(&aes_instance, init_vector);
  247.         /* Write the data to be ciphered to the input data registers. */
  248.         aes_write_input_data(&aes_instance, ref_plain_text);
  249.         aes_set_new_message(&aes_instance);
  250.         aes_start(&aes_instance);
  251.         aes_clear_new_message(&aes_instance);
  252.         /* Wait for the end of the encryption process. */
  253.         while (false == state) {
  254.         }
  255.         /* check the result. */
  256.         if ((ref_cipher_text_cfb128[0] != output_data[0]) ||
  257.         (ref_cipher_text_cfb128[1] != output_data[1]) ||
  258.         (ref_cipher_text_cfb128[2] != output_data[2]) ||
  259.         (ref_cipher_text_cfb128[3] != output_data[3])) {
  260.                 printf("\r\nKO!!!\r\n");
  261.                 } else {
  262.                 printf("\r\nOK!!!\r\n");
  263.         }
  264.         printf("\r\n-----------------------------------\r\n");
  265.         printf("- 128bit cryptographic key\r\n");
  266.         printf("- CFB128 decipher mode\r\n");
  267.         printf("- Auto start mode\r\n");
  268.         printf("- 4 32bit words\r\n");
  269.         printf("-----------------------------------\r\n");
  270.         state = false;
  271.         /* Configure the AES. */
  272.         g_aes_cfg.encrypt_mode = AES_DECRYPTION;
  273.         g_aes_cfg.key_size = AES_KEY_SIZE_128;
  274.         g_aes_cfg.start_mode = AES_MANUAL_START;
  275.         g_aes_cfg.opmode = AES_CFB_MODE;
  276.         g_aes_cfg.cfb_size = AES_CFB_SIZE_128;
  277.         g_aes_cfg.lod = false;
  278.         aes_set_config(&aes_instance,AES, &g_aes_cfg);
  279.         /* Set the cryptographic key. */
  280.         aes_write_key(&aes_instance, key128);
  281.         /* Set the initialization vector. */
  282.         aes_write_init_vector(&aes_instance, init_vector);
  283.         /* Write the data to be deciphered to the input data registers. */
  284.         aes_write_input_data(&aes_instance, ref_cipher_text_cfb128);
  285.         aes_set_new_message(&aes_instance);
  286.         aes_start(&aes_instance);
  287.         aes_clear_new_message(&aes_instance);
  288.         /* Wait for the end of the decryption process. */
  289.         while (false == state) {
  290.         }
  291.         /* check the result. */
  292.         if ((ref_plain_text[0] != output_data[0]) ||
  293.         (ref_plain_text[1] != output_data[1]) ||
  294.         (ref_plain_text[2] != output_data[2]) ||
  295.         (ref_plain_text[3] != output_data[3])) {
  296.                 printf("\r\nKO!!!\r\n");
  297.                 } else {
  298.                 printf("\r\nOK!!!\r\n");
  299.         }
  300. }
  301. static void ofb_mode_test(void)
  302. {
  303.         printf("\r\n-----------------------------------\r\n");
  304.         printf("- 128bit cryptographic key\r\n");
  305.         printf("- OFB cipher mode\r\n");
  306.         printf("- Auto start mode\r\n");
  307.         printf("- 4 32bit words\r\n");
  308.         printf("-----------------------------------\r\n");
  309.         state = false;
  310.         /* Configure the AES. */
  311.         g_aes_cfg.encrypt_mode = AES_ENCRYPTION;
  312.         g_aes_cfg.key_size = AES_KEY_SIZE_128;
  313.         g_aes_cfg.start_mode = AES_AUTO_START;
  314.         g_aes_cfg.opmode = AES_OFB_MODE;
  315.         g_aes_cfg.cfb_size = AES_CFB_SIZE_128;
  316.         g_aes_cfg.lod = false;
  317.         aes_set_config(&aes_instance,AES, &g_aes_cfg);
  318.         /* Set the cryptographic key. */
  319.         aes_write_key(&aes_instance, key128);
  320.         /* Set the initialization vector. */
  321.         aes_write_init_vector(&aes_instance, init_vector);
  322.         aes_set_new_message(&aes_instance);
  323.         /* Write the data to be ciphered to the input data registers. */
  324.         aes_write_input_data(&aes_instance, ref_plain_text);
  325.         aes_clear_new_message(&aes_instance);
  326.         /* Wait for the end of the encryption process. */
  327.         while (false == state) {
  328.         }
  329.         /* check the result. */
  330.         if ((ref_cipher_text_ofb[0] != output_data[0]) ||
  331.         (ref_cipher_text_ofb[1] != output_data[1]) ||
  332.         (ref_cipher_text_ofb[2] != output_data[2]) ||
  333.         (ref_cipher_text_ofb[3] != output_data[3])) {
  334.                 printf("\r\nKO!!!\r\n");
  335.                 } else {
  336.                 printf("\r\nOK!!!\r\n");
  337.         }
  338.         printf("\r\n-----------------------------------\r\n");
  339.         printf("- 128bit cryptographic key\r\n");
  340.         printf("- OFB decipher mode\r\n");
  341.         printf("- Auto start mode\r\n");
  342.         printf("- 4 32bit words\r\n");
  343.         printf("-----------------------------------\r\n");
  344.         state = false;
  345.         /* Configure the AES. */
  346.         g_aes_cfg.encrypt_mode = AES_DECRYPTION;
  347.         g_aes_cfg.key_size = AES_KEY_SIZE_128;
  348.         g_aes_cfg.start_mode = AES_MANUAL_START;
  349.         g_aes_cfg.opmode = AES_OFB_MODE;
  350.         g_aes_cfg.cfb_size = AES_CFB_SIZE_128;
  351.         g_aes_cfg.lod = false;
  352.         aes_set_config(&aes_instance,AES, &g_aes_cfg);
  353.         /* Set the cryptographic key. */
  354.         aes_write_key(&aes_instance, key128);
  355.         /* Set the initialization vector. */
  356.         aes_write_init_vector(&aes_instance, init_vector);
  357.         /* Write the data to be deciphered to the input data registers. */
  358.         aes_write_input_data(&aes_instance, ref_cipher_text_ofb);
  359.         aes_set_new_message(&aes_instance);
  360.         aes_start(&aes_instance);
  361.         aes_clear_new_message(&aes_instance);
  362.         /* Wait for the end of the decryption process. */
  363.         while (false == state) {
  364.         }
  365.         /* check the result. */
  366.         if ((ref_plain_text[0] != output_data[0]) ||
  367.         (ref_plain_text[1] != output_data[1]) ||
  368.         (ref_plain_text[2] != output_data[2]) ||
  369.         (ref_plain_text[3] != output_data[3])) {
  370.                 printf("\r\nKO!!!\r\n");
  371.                 } else {
  372.                 printf("\r\nOK!!!\r\n");
  373.         }
  374. }
  375. static void ctr_mode_test(void)
  376. {
  377.         printf("\r\n-----------------------------------\r\n");
  378.         printf("- 128bit cryptographic key\r\n");
  379.         printf("- CTR cipher mode\r\n");
  380.         printf("- Auto start mode\r\n");
  381.         printf("- 4 32bit words\r\n");
  382.         printf("-----------------------------------\r\n");
  383.         state = false;
  384.         /* Configure the AES. */
  385.         g_aes_cfg.encrypt_mode = AES_ENCRYPTION;
  386.         g_aes_cfg.key_size = AES_KEY_SIZE_128;
  387.         g_aes_cfg.start_mode = AES_AUTO_START;
  388.         g_aes_cfg.opmode = AES_CTR_MODE;
  389.         g_aes_cfg.cfb_size = AES_CFB_SIZE_128;
  390.         g_aes_cfg.lod = false;
  391.         aes_set_config(&aes_instance,AES, &g_aes_cfg);
  392.         /* Set the cryptographic key. */
  393.         aes_write_key(&aes_instance, key128);
  394.         /* Set the initialization vector. */
  395.         aes_write_init_vector(&aes_instance, init_vector_ctr);
  396.         aes_set_new_message(&aes_instance);
  397.         /* Write the data to be ciphered to the input data registers. */
  398.         aes_write_input_data(&aes_instance, ref_plain_text);
  399.         aes_clear_new_message(&aes_instance);
  400.         /* Wait for the end of the encryption process. */
  401.         while (false == state) {
  402.         }
  403.         /* check the result. */
  404.         if ((ref_cipher_text_ctr[0] != output_data[0]) ||
  405.         (ref_cipher_text_ctr[1] != output_data[1]) ||
  406.         (ref_cipher_text_ctr[2] != output_data[2]) ||
  407.         (ref_cipher_text_ctr[3] != output_data[3])) {
  408.                 printf("\r\nKO!!!\r\n");
  409.                 } else {
  410.                 printf("\r\nOK!!!\r\n");
  411.         }
  412.         printf("\r\n-----------------------------------\r\n");
  413.         printf("- 128bit cryptographic key\r\n");
  414.         printf("- CTR decipher mode\r\n");
  415.         printf("- Auto start mode\r\n");
  416.         printf("- 4 32bit words\r\n");
  417.         printf("-----------------------------------\r\n");
  418.         state = false;
  419.         /* Configure the AES. */
  420.         g_aes_cfg.encrypt_mode = AES_DECRYPTION;
  421.         g_aes_cfg.key_size = AES_KEY_SIZE_128;
  422.         g_aes_cfg.start_mode = AES_MANUAL_START;
  423.         g_aes_cfg.opmode = AES_CTR_MODE;
  424.         g_aes_cfg.cfb_size = AES_CFB_SIZE_128;
  425.         g_aes_cfg.lod = false;
  426.         aes_set_config(&aes_instance,AES, &g_aes_cfg);
  427.         /* Set the cryptographic key. */
  428.         aes_write_key(&aes_instance, key128);
  429.         /* Set the initialization vector. */
  430.         aes_write_init_vector(&aes_instance, init_vector_ctr);
  431.         /* Write the data to be deciphered to the input data registers. */
  432.         aes_write_input_data(&aes_instance, ref_cipher_text_ctr);
  433.         aes_set_new_message(&aes_instance);
  434.         aes_start(&aes_instance);
  435.         aes_clear_new_message(&aes_instance);
  436.         /* Wait for the end of the decryption process. */
  437.         while (false == state) {
  438.         }
  439.         /* check the result. */
  440.         if ((ref_plain_text[0] != output_data[0]) ||
  441.         (ref_plain_text[1] != output_data[1]) ||
  442.         (ref_plain_text[2] != output_data[2]) ||
  443.         (ref_plain_text[3] != output_data[3])) {
  444.                 printf("\r\nKO!!!\r\n");
  445.                 } else {
  446.                 printf("\r\nOK!!!\r\n");
  447.         }
  448. }


  449. int main (void)
  450. {
  451.         
  452.            /* Initialize the system and console*/
  453.            system_init();
  454.            configure_usart();
  455.            aes_get_config_defaults(&g_aes_cfg);
  456.            aes_init(&aes_instance,AES, &g_aes_cfg);
  457.            aes_enable(&aes_instance);
  458.            /* Enable AES interrupt. */
  459.            aes_register_callback(aes_callback,AES_CALLBACK_ENCRYPTION_COMPLETE);
  460.            aes_enable_callback(&aes_instance,AES_CALLBACK_ENCRYPTION_COMPLETE);
  461.            
  462.             ecb_mode_test();
  463.             cbc_mode_test();
  464.             cfb128_mode_test();
  465.             ofb_mode_test();
  466.             ctr_mode_test();
  467.         
  468. }
执行的结果如下:


本帖子中包含更多资源

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

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

2404

主题

7006

帖子

68

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