Xmega Application Note


AES_example_backtoback.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   3
00055 
00056 /* Global Plaintext variables containing one block. */
00057 uint8_t data1[BLOCK_LENGTH] ={0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
00058                               0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
00059 
00060 uint8_t data2[BLOCK_LENGTH] ={0xAA, 0xAA, 0xAA, 0xBB, 0xBB, 0xBB, 0xCC, 0xCC,
00061                               0xCC, 0xDD, 0xDD, 0xDD, 0xEE, 0xEE, 0xEE, 0x0F};
00062 
00063 /* Variables used to store the result from a single AES encryption/decryption .*/
00064 uint8_t single_ans1[BLOCK_LENGTH];
00065 uint8_t single_ans2[BLOCK_LENGTH];
00066 
00067 /* Key used when AES encryption is done operations. */
00068 uint8_t key[BLOCK_LENGTH] = {0x94, 0x74, 0xB8, 0xE8, 0xC7, 0x3B, 0xCA, 0x7D,
00069                              0x28, 0x34, 0x76, 0xAB, 0x38, 0xCF, 0x37, 0xC2};
00070 
00071 
00072 uint8_t lastsubkey[BLOCK_LENGTH];
00073 uint8_t read_key[BLOCK_LENGTH];
00074 
00075 /* Variable used to check if decrypted answer is equal original data. */
00076 bool success;
00077 
00078 
00079 int main( void )
00080 {
00081         /* Assume that everything is ok*/
00082         success = true;
00083 
00084         /* Before using the AES it is recommended to do an AES software reset to put
00085          * the module in known state, in case other parts of your code has accessed
00086          * the AES module. */
00087         AES_software_reset();
00088 
00089         /* Do AES encryption and decryption every other time with the same key on
00090          * two data blocks. */
00091         success = AES_encrypt(data1, single_ans1, key);
00092         success = AES_decrypt_backtoback(single_ans1, single_ans1);
00093         success = AES_encrypt_backtoback(data2, single_ans2);
00094         success = AES_decrypt_backtoback(single_ans2, single_ans2);
00095 
00096         /* Check if decrypted answers is equal to plaintext. */
00097         for(uint8_t i = 0; i < BLOCK_LENGTH ; i++ ){
00098                 if (data1[i] != single_ans1[i]){
00099                         success = false;
00100                 }
00101                 if (data2[i] != single_ans2[i]){
00102                         success = false;
00103                 }
00104         }
00105 
00106         if(success){
00107                 while(true){
00108                         /* If the example ends up here every thing is ok. */
00109                         nop();
00110                 }
00111         }else{
00112                 while(true){
00113                         /* If the example ends up here something is wrong. */
00114                         nop();
00115                 }
00116         }
00117 }
00118 
@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