Xmega Application Note


AES_example_polled.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00051 #include "avr_compiler.h"
00052 #include "AES_driver.h"
00053 
00054 #define BLOCK_LENGTH  16
00055 #define BLOCK_COUNT   3
00056 
00057 /* Global Plaintext variable containing one block. */
00058 uint8_t data[BLOCK_LENGTH] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
00059                               0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
00060 
00061 /* Variable used to store the result from a single AES encryption/decryption .*/
00062 uint8_t single_ans[BLOCK_LENGTH];
00063 
00064 /* Key used when AES encryption is done operations. */
00065 uint8_t key[BLOCK_LENGTH] = {0x94, 0x74, 0xB8, 0xE8, 0xC7, 0x3B, 0xCA, 0x7D,
00066                              0x28, 0x34, 0x76, 0xAB, 0x38, 0xCF, 0x37, 0xC2};
00067 
00068 /* Key used when the AES shall decrypt. This key is a modified version of the key. */
00069 uint8_t  lastsubkey[BLOCK_LENGTH];
00070 
00071 /* Initialisation vector used during Cipher Block Chaining. */
00072 uint8_t init[BLOCK_LENGTH] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
00073                               0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
00074 
00075 /* Plain text used during Cipher Block Chaining(contains 3 blocks). */
00076 uint8_t data_block[BLOCK_LENGTH * BLOCK_COUNT] =
00077                         {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
00078                          0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
00079                          0xAB, 0xBA, 0x00, 0x00, 0xDE, 0xAD, 0x00, 0x00,
00080                          0xAB, 0xBA, 0x00, 0x00, 0xDE, 0xAD, 0x00, 0x00,
00081                          0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
00082                          0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00};
00083 
00084 /* Variable used to store ciphertext from Cipher Block Chaining. */
00085 uint8_t  cipher_block_ans[BLOCK_LENGTH * BLOCK_COUNT];
00086 
00087 /* Variable used to store decrypted plaintext from Cipher Block Chaining. */
00088 uint8_t  block_ans[BLOCK_LENGTH * BLOCK_COUNT];
00089 
00090 /* Variable used to check if decrypted answer is equal original data. */
00091 bool success;
00092 
00093 int main( void )
00094 {
00095         /* Assume that everything is ok*/
00096         success = true;
00097 
00098         /* Before using the AES it is recommended to do an AES software reset to put
00099          * the module in known state, in case other parts of your code has accessed
00100          * the AES module. */
00101         AES_software_reset();
00102 
00103         /* Generate last subkey. */
00104         AES_lastsubkey_generate(key, lastsubkey);
00105 
00106         /* Do AES encryption and decryption of a single block. */
00107         success = AES_encrypt(data, single_ans, key);
00108         success = AES_decrypt(single_ans, single_ans, lastsubkey);
00109 
00110         /* Check if decrypted answer is equal to plaintext. */
00111         for(uint8_t i = 0; i < BLOCK_LENGTH ; i++ ){
00112                 if (data[i] != single_ans[i]){
00113                         success = false;
00114                 }
00115         }
00116 
00117         /* Do AES Cipher Block Chaining encryption and decryption on three blocks. */
00118         success = AES_CBC_encrypt(data_block, cipher_block_ans, key, init,
00119                                 BLOCK_COUNT);
00120         success = AES_CBC_decrypt(cipher_block_ans, block_ans, lastsubkey,
00121                                 init, BLOCK_COUNT);
00122 
00123         /* Check if decrypted answer is equal to plaintext. */
00124         for(uint8_t i = 0; i < BLOCK_LENGTH * BLOCK_COUNT ; i++ ){
00125                 if (data_block[i] != block_ans[i]){
00126                         success = false;
00127                 }
00128         }
00129 
00130         if(success){
00131                 while(true){
00132                         /* If the example ends up here every thing is ok. */
00133                         nop();
00134                 }
00135         }else{
00136                 while(true){
00137                         /* If the example ends up here something is wrong. */
00138                         nop();
00139                 }
00140         }
00141 }
@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