Xmega Application Note


DES_example.c

Go to the documentation of this file.
00001 /* This file has been prepared for Doxygen automatic documentation generation.*/
00053 #include "avr_compiler.h"
00054 #include "DES_driver.h"
00055 
00056 #define DES_BLOCK_LENGTH  8
00057 #define DES_BLOCK_COUNT   3
00058 
00063 uint8_t data[DES_BLOCK_LENGTH] = {0xAB, 0xBA, 0x00, 0xBE, 0xEF, 0x00, 0xDE, 0xAD};
00064 
00066 uint8_t single_ans[DES_BLOCK_LENGTH];
00067 
00072 uint8_t keys[DES_BLOCK_LENGTH * DES_BLOCK_COUNT] =
00073                     {0x94, 0x74, 0xB8, 0xE8, 0xC7, 0x3B, 0xCA, 0x7D,
00074                      0x28, 0x34, 0x76, 0xAB, 0x38, 0xCF, 0x37, 0xC2,
00075                      0xFE, 0x98, 0x6C, 0x38, 0x23, 0xFC, 0x2D, 0x23};
00076 
00078 uint8_t init[DES_BLOCK_LENGTH] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
00079 
00081 uint8_t data_block[DES_BLOCK_LENGTH * DES_BLOCK_COUNT]=
00082                     {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
00083                      0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
00084                      0xAB, 0xBA, 0x00, 0x00, 0xDE, 0xAD, 0x00, 0x00};
00085 
00087 uint8_t cipher_block_ans[DES_BLOCK_LENGTH * DES_BLOCK_COUNT];
00088 
00096 uint8_t block_ans[DES_BLOCK_LENGTH * DES_BLOCK_COUNT];
00097 
00098 
00099 
00102 int main( void )
00103 {
00104         bool success = true;
00105 
00106         /* Example of how to use Single DES encryption and decryption functions. */
00107         DES_Encrypt(data, single_ans, keys);
00108         DES_Decrypt(single_ans, single_ans, keys);
00109 
00110         /* Check if decrypted answer is equal to plaintext. */
00111         for (uint8_t i = 0; i < DES_BLOCK_LENGTH ; i++ ){
00112                 if (data[i] != single_ans[i]){
00113                         success = false;
00114                         break;
00115                 }
00116         }
00117         
00118         if (success){
00119         
00120                 /* Example of how to use 3DES encryption and decryption functions. */
00121                 DES_3DES_Encrypt(data, single_ans, keys);
00122                 DES_3DES_Decrypt(single_ans, single_ans, keys);
00123 
00124                 /* Check if decrypted answer is equal to plaintext. */
00125                 for (uint8_t i = 0; i < DES_BLOCK_LENGTH ; i++ ){
00126                         if (data[i] != single_ans[i]){
00127                                 success = false;
00128                                 break;
00129                         }
00130                 }
00131         }
00132 
00133         if (success){
00134                 /* Example of how to use DES Cipher Block Chaining encryption and
00135                  * decryption functions.
00136                  */
00137                 DES_CBC_Encrypt(data_block, cipher_block_ans, keys, init, true, DES_BLOCK_COUNT);
00138                 DES_CBC_Decrypt(cipher_block_ans, block_ans, keys, init, true, DES_BLOCK_COUNT);
00139 
00140                 /* Check if decrypted answer is equal to plaintext. */
00141                 for (uint8_t i = 1; i < (DES_BLOCK_LENGTH * DES_BLOCK_COUNT); i++ ){
00142                         if (data_block[i] != block_ans[i]){
00143                                 success = false;
00144                                 break;
00145                         }
00146                 }
00147         }
00148 
00149         if (success){
00150                 while (true){
00151                         /* If the example ends up here everything is ok. */
00152                         nop();
00153                 }
00154         }else{
00155                 while (true){
00156                         /* If the example ends up here something is wrong. */
00157                         nop();
00158                 }
00159         }
00160 }
@DOC_TITLE@
Generated on Wed Apr 23 08:31:41 2008 for AVR1317 Using the XMEGA bulit in DES accelerator by doxygen 1.5.5