Xmega Application Note


DES_example.c File Reference


Detailed Description

XMEGA DES driver example source.

This file contains an example application that demonstrates the DES driver. It shows how to do a DES encryption and decryption, a 3DES encryption and decryption and a DES cipher block chaining encryption and decryption. It can be used with both the driver optimized for speed and the driver optimized for size.

Application note:
AVR1317: Using the XMEGA built in DES accelerator
Documentation
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision
1544
Date
2008-04-21 08:36:12 +0200 (ma, 21 apr 2008)

Copyright (c) 2008, Atmel Corporation All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file DES_example.c.

#include "avr_compiler.h"
#include "DES_driver.h"

Include dependency graph for DES_example.c:

Go to the source code of this file.

Defines

#define DES_BLOCK_COUNT   3
#define DES_BLOCK_LENGTH   8

Functions

int main (void)
 Main example doing DES encryption/decryption.

Variables

uint8_t block_ans [DES_BLOCK_LENGTH *DES_BLOCK_COUNT]
 Variable used to store decrypted plaintext from DES Cipher Block Chaining.
uint8_t cipher_block_ans [DES_BLOCK_LENGTH *DES_BLOCK_COUNT]
 Variable used to store ciphertext from DES Cipher Block Chaining.
uint8_t data [DES_BLOCK_LENGTH] = {0xAB, 0xBA, 0x00, 0xBE, 0xEF, 0x00, 0xDE, 0xAD}
 Plaintext block used by DES and 3DES.
uint8_t data_block [DES_BLOCK_LENGTH *DES_BLOCK_COUNT]
 Plain text used during DES Cipher Block Chaining.
uint8_t init [DES_BLOCK_LENGTH] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}
 Initial vector used during DES Cipher Block Chaining.
uint8_t keys [DES_BLOCK_LENGTH *DES_BLOCK_COUNT]
 Keys used by all DES operations. (single DES only uses the first 64-bit key).
uint8_t single_ans [DES_BLOCK_LENGTH]
 Variable used to store DES and tripleDES results.


Define Documentation

#define DES_BLOCK_COUNT   3

Definition at line 57 of file DES_example.c.

Referenced by main().

#define DES_BLOCK_LENGTH   8

Definition at line 56 of file DES_example.c.

Referenced by main().


Function Documentation

int main ( void   ) 

Main example doing DES encryption/decryption.

Definition at line 102 of file DES_example.c.

References block_ans, cipher_block_ans, data, data_block, DES_3DES_Decrypt(), DES_3DES_Encrypt(), DES_BLOCK_COUNT, DES_BLOCK_LENGTH, DES_CBC_Decrypt(), DES_CBC_Encrypt(), DES_Decrypt(), DES_Encrypt(), init, keys, and single_ans.

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 }

Here is the call graph for this function:


Variable Documentation

uint8_t block_ans[DES_BLOCK_LENGTH *DES_BLOCK_COUNT]

Variable used to store decrypted plaintext from DES Cipher Block Chaining.

Note:
It is not possible to use the same variable to store the plaintext after a Cipher Block Chaining operation, as the previous cipher block is used to decode the current data block.

Definition at line 96 of file DES_example.c.

Referenced by main().

uint8_t cipher_block_ans[DES_BLOCK_LENGTH *DES_BLOCK_COUNT]

Variable used to store ciphertext from DES Cipher Block Chaining.

Definition at line 87 of file DES_example.c.

Referenced by main().

uint8_t data[DES_BLOCK_LENGTH] = {0xAB, 0xBA, 0x00, 0xBE, 0xEF, 0x00, 0xDE, 0xAD}

Plaintext block used by DES and 3DES.

Note:
The MSB in the block is byte 0, and LSB is byte 7.

Definition at line 63 of file DES_example.c.

Referenced by main().

uint8_t data_block[DES_BLOCK_LENGTH *DES_BLOCK_COUNT]

Initial value:

                    {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
                     0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00,
                     0xAB, 0xBA, 0x00, 0x00, 0xDE, 0xAD, 0x00, 0x00}
Plain text used during DES Cipher Block Chaining.

Definition at line 81 of file DES_example.c.

Referenced by main().

uint8_t init[DES_BLOCK_LENGTH] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88}

Initial vector used during DES Cipher Block Chaining.

Definition at line 78 of file DES_example.c.

Referenced by main().

uint8_t keys[DES_BLOCK_LENGTH *DES_BLOCK_COUNT]

Initial value:

                    {0x94, 0x74, 0xB8, 0xE8, 0xC7, 0x3B, 0xCA, 0x7D,
                     0x28, 0x34, 0x76, 0xAB, 0x38, 0xCF, 0x37, 0xC2,
                     0xFE, 0x98, 0x6C, 0x38, 0x23, 0xFC, 0x2D, 0x23}
Keys used by all DES operations. (single DES only uses the first 64-bit key).

Note:
The MSB of the 3 keys is byte 0,8 and 16. The LSB of each key is byte 7, 15 and 23.

Definition at line 72 of file DES_example.c.

Referenced by main().

uint8_t single_ans[DES_BLOCK_LENGTH]

Variable used to store DES and tripleDES results.

Definition at line 66 of file DES_example.c.

Referenced by main().

@DOC_TITLE@
Generated on Wed Apr 23 08:31:42 2008 for AVR1317 Using the XMEGA bulit in DES accelerator by doxygen 1.5.5