Xmega Application Note


AES_example_interrupt.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00050 #include "avr_compiler.h"
00051 #include "AES_driver.h"
00052 
00053 #define BLOCK_LENGTH  16
00054 #define BLOCK_COUNT   9
00055 
00056 /* Interrupt driver */
00057 AES_interrupt_driver_t interrupt_driver;
00058 
00059 /* Key used when AES encryption is done operations. (single AES only uses the first 8 values). */
00060 uint8_t  key[BLOCK_LENGTH] = { 0x94, 0x74, 0xB8, 0xE8, 0xC7, 0x3B, 0xCA, 0x7D,
00061                                0x28, 0x34, 0x76, 0xAB, 0x38, 0xCF, 0x37, 0xC2};
00062 
00063 /* \brief Key used when the AES shall decrypt. This key is a modified version of the key. */
00064 uint8_t  lastsubkey[BLOCK_LENGTH];
00065 
00067 uint8_t  init[BLOCK_LENGTH] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
00068                                0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
00069 
00074 uint8_t  data_block[BLOCK_LENGTH * BLOCK_COUNT] =
00075 {
00076   "AVR1318: Using the XMEGA built-in AES accelerator."
00077   "This is a little textstring which will be encrypted"
00078   "and decrypted with CBC in the AES module."
00079 };
00080 
00082 uint8_t cipher_block_ans[BLOCK_LENGTH * BLOCK_COUNT];
00083 
00085 uint8_t block_ans[BLOCK_LENGTH * BLOCK_COUNT];
00086 
00087 /* Variable used to check if decrypted answer is equal original data*/
00088 bool success;
00089 
00090 int main(void)
00091 {
00092         /* Assume that everything is ok*/
00093         success = true;
00094 
00095         /* Before using the AES it is recommended to do an AES software reset to put
00096          * the module in known state, in case other parts of your code has accessed
00097          * the AES module. */
00098         AES_software_reset();
00099 
00100         /* Initialize AES interrupt driver for encryption. */
00101         AES_interrupt_driver_init(&interrupt_driver,
00102                                   data_block,
00103                                   cipher_block_ans,
00104                                   key,
00105                                   init,
00106                                   BLOCK_COUNT,
00107                                   false);
00108 
00109         /* Enable PMIC interrupt to level low. */
00110         PMIC.CTRL |= PMIC_LOLVLEN_bm;
00111 
00112         /* Enable global interrupts. */
00113         sei();
00114 
00115         /* Start interrupt driver. */
00116         success = AES_interrupt_driver_start(&interrupt_driver, AES_INTLVL_LO_gc);
00117 
00118         if(success){
00119                 do{
00120                 /* Wait until all the encryption is finished. */
00121                 }while(!AES_interrupt_driver_finished(&interrupt_driver));
00122         }else{
00123                 while(true){
00124                         /* If the example ends up here something is wrong. */
00125                         nop();
00126                 }
00127         }
00128 
00129         /* Before doing a decryption generate and store the last sub key of the
00130          * extended key. */
00131         success = AES_lastsubkey_generate(key, lastsubkey);
00132 
00133         /* Initialize AES interrupt driver for decryption. */
00134         AES_interrupt_driver_init(&interrupt_driver,
00135                                   cipher_block_ans,
00136                                   block_ans,
00137                                   lastsubkey,
00138                                   init,
00139                                   BLOCK_COUNT,
00140                                   true);
00141 
00142         /* Start interrupt driver. */
00143         success = AES_interrupt_driver_start(&interrupt_driver, AES_INTLVL_LO_gc);
00144 
00145         if(success){
00146                 do{
00147                         /* Wait until the decryption is finished. */
00148                 }while(!AES_interrupt_driver_finished(&interrupt_driver));
00149         }
00150 
00151         /* Check if decrypted answer is equal to plaintext. */
00152         for(uint16_t i = 0; i < BLOCK_LENGTH * BLOCK_COUNT ; i++ ){
00153                 if (data_block[i] != block_ans[i]){
00154                         success = false;
00155                 }
00156         }
00157 
00158         if(success){
00159                 while(true){
00160                         /* If the example ends up here every thing is ok. */
00161                         nop();
00162                 }
00163         }else{
00164                 while(true){
00165                         /* If the example ends up here something is wrong. */
00166                         nop();
00167                 }
00168         }
00169 }
00170 
00171 
00173 ISR(AES_INT_vect)
00174 {
00175         /* Run AES interrupts according to interrupt_driver. */
00176         AES_interrupt_handler(&interrupt_driver);
00177 }
@DOC_TITLE@
Generated on Wed Apr 23 08:53:40 2008 for AVR1318 Using the XMEGA built in AES accelerator by doxygen 1.5.5