/*----------------------------------------------------------------------------
* Copyright (C) 2009 Maxim Integrated Products Incorporated, All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED PRODUCTS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated Products
* shall not be used except as stated in the Maxim Integrated Products
* Branding Policy.
* ---------------------------------------------------------------------------
*/
/**
* This example show use of MAXQ1850 hardware with DES and 3DES both ECB and CBC modes
*/
#include "des.h"
#include <string.h> // Need for memcmp
#include <stdio.h>
#include "serial.h"
// Prototype
void hexdump(uint8_t *b, uint16_t len);
void main(void)
{
uint8_t DES_KEY[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
uint8_t DES_PT[] = "Now is the time for all ";
uint8_t DES_ECB_KACT[] = {0x3F, 0xA4, 0x0E, 0x8A, 0x98, 0x4D, 0x48, 0x15,
0x6A, 0x27, 0x17, 0x87, 0xAB, 0x88, 0x83, 0xF9,
0x89, 0x3D, 0x51, 0xEC, 0x4B, 0x56, 0x3B, 0x53};
uint8_t DES_CBC_KACT[] = {0xe5, 0xc7, 0xcd, 0xde, 0x87, 0x2b, 0xf2, 0x7c,
0x43, 0xe9, 0x34, 0x00, 0x8c, 0x38, 0x9c, 0x0f,
0x68, 0x37, 0x88, 0x49, 0x9a, 0x7c, 0x05, 0xf6};
uint8_t DES_IV[] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef};
uint8_t TDES_KEYS[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01,
0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23};
uint8_t TDES_PT[] = "The qufck brown fox jump";
uint8_t TDES_ECB_KACT[] = {0xA8, 0x26, 0xFD, 0x8C, 0xE5, 0x3B, 0x85, 0x5F,
0xCC, 0xE2, 0x1C, 0x81, 0x12, 0x25, 0x6F, 0xE6,
0x68, 0xD5, 0xC0, 0x5D, 0xD9, 0xB6, 0xB9, 0x00};
uint8_t DES_OUTPUT[256];
uint8_t DES_OUTPUT2[256];
serial_init(); // Setup serial port
printf("MAXQ1850 DES Example\n");
//************************************************************
//************************************************************
// Single DES ECB
// Known Answer Vector from FIPS PUB 81
//************************************************************
//************************************************************
printf("************************************************************\n");
printf("Single DES ECB\n");
printf("************************************************************\n");
printf("Original Plain Text:\n");
hexdump(DES_PT, 24);
des_encrypt_ecb(DES_PT, DES_OUTPUT, 24, DES_KEY, 1);
printf("ECB Vector Cipher Text:\n");
hexdump(DES_OUTPUT, 24);
if ( memcmp(DES_OUTPUT, DES_ECB_KACT, sizeof(DES_ECB_KACT)) != 0 ) // If result does not match known answer
printf("Error! ECB Cipher Text does not equal expected value!\n");
else
printf("ECB Cipher Text correctly matched known answer.\n");
des_decrypt_ecb(DES_OUTPUT, DES_OUTPUT, 24, DES_KEY, 1);
printf("ECB Decrypted Plain Text:\n");
hexdump(DES_OUTPUT, 24);
if ( memcmp(DES_OUTPUT, DES_PT, sizeof(DES_ECB_KACT)) != 0 ) // If result does not match Orig plain text
printf("Error! ECB Plain Text does not equal original!\n");
else
printf("ECB Plain Text correctly matched original.\n");
//************************************************************
//************************************************************
// Single DES CBC
// Known Answer Vector from FIPS PUB 81
//************************************************************
//************************************************************
printf("************************************************************\n");
printf("Single DES CBC\n");
printf("************************************************************\n");
printf("Original Plain Text:\n");
hexdump(DES_PT, 24);
des_encrypt_cbc(DES_PT, DES_OUTPUT, 24, DES_KEY, 1, DES_IV);
printf("CBC Vector Cipher Text:\n");
hexdump(DES_OUTPUT, 24);
if ( memcmp(DES_OUTPUT, DES_CBC_KACT, sizeof(DES_CBC_KACT)) != 0 ) // If result does not match known answer
printf("Error! CBC Cipher Text does not equal expected value!\n");
else
printf("CBC Cipher Text correctly matched known answer.\n");
des_decrypt_cbc(DES_OUTPUT, DES_OUTPUT2, 24, DES_KEY, 1, DES_IV);
printf("CBC Decrypted Plain Text:\n");
hexdump(DES_OUTPUT2, 24);
if ( memcmp(DES_OUTPUT2, DES_PT, sizeof(DES_CBC_KACT)) != 0 ) // If result does not match Orig plain text
printf("Error! CBC Plain Text does not equal original!\n");
else
printf("CBC Plain Text correctly matched original.\n");
//************************************************************
//************************************************************
// Triple DES ECB
// Known Answer Vector from NIST Pub 800-67
//************************************************************
//************************************************************
printf("************************************************************\n");
printf("Triple DES ECB\n");
printf("************************************************************\n");
printf("Original Plain Text:\n");
hexdump(TDES_PT, 24);
des_encrypt_ecb(TDES_PT, DES_OUTPUT, 24, TDES_KEYS, 3);
printf("Triple DES ECB Vector Cipher Text:\n");
hexdump(DES_OUTPUT, 24);
if ( memcmp(DES_OUTPUT, TDES_ECB_KACT, sizeof(TDES_ECB_KACT)) != 0 ) // If result does not match known answer
printf("Error! Triple DES ECB Cipher Text does not equal expected value!\n");
else
printf("Triple DES ECB Cipher Text correctly matched known answer.\n");
des_decrypt_ecb(DES_OUTPUT, DES_OUTPUT, 24, TDES_KEYS, 3);
printf("Triple DES ECB Decrypted Plain Text:\n");
hexdump(DES_OUTPUT, 24);
if ( memcmp(DES_OUTPUT, TDES_PT, sizeof(TDES_ECB_KACT)) != 0 ) // If result does not match Orig plain text
printf("Error! Triple DES ECB Plain Text does not equal original!\n");
else
printf("Triple DES ECB Plain Text correctly matched original.\n");
//************************************************************
//************************************************************
// Triple DES CBC
// This example is not a published known answer test
//************************************************************
//************************************************************
printf("************************************************************\n");
printf("Triple DES CBC\n");
printf("************************************************************\n");
printf("Original Plain Text:\n");
hexdump(TDES_PT, 24);
des_encrypt_cbc(TDES_PT, DES_OUTPUT, 24, TDES_KEYS, 3, DES_IV);
printf("Triple DES CBC Vector Cipher Text:\n");
h