//------------------------------------------------------------------------------
// File:          libtomcrypt.h
// Function:      Firmware encryption using libTomCrypt
// Supported chip(s):
//    - AT91SAM7XC128
//    - AT91SAM7XC256
// Supported toolchain(s):
//    - IAR Embedded Workbench
// Date created:  05 June 2006
// Created by:    JJo
//------------------------------------------------------------------------------

#ifndef BOOTLOADER_LIBTOMCRYPT_H
#define BOOTLOADER_LIBTOMCRYPT_H

//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------

#include <tomcrypt.h>
#include "common.h"
#include "debug.h"

#if defined(USE_ENCRYPTION) && (defined(ENCRYPTION_AES_LTC) || defined(ENCRYPTION_3DES_LTC))

//------------------------------------------------------------------------------
// Check configuration
//------------------------------------------------------------------------------

// Supported modes
#if !defined(ENCRYPTION_CTR) && \
    !defined(ENCRYPTION_CBC) && \
    !defined(ENCRYPTION_ECB)
  #error No other mode than ECB, CBC & CTR are supported.
#endif

// Supported key length
#if defined(ENCRYPTION_AES_LTC)
  #if (ENCRYPTION_KEY_LENGTH != 16) && \
      (ENCRYPTION_KEY_LENGTH != 24) && \
      (ENCRYPTION_KEY_LENGTH != 32) 
    #error Only a key length of 128, 192 or 256 bits are supported with AES.
  #endif
#elif defined(ENCRYPTION_3DES_LTC)
  #if (ENCRYPTION_KEY_LENGTH != 16) && \
      (ENCRYPTION_KEY_LENGTH != 24)
    #error Only a key length of 128 or 192 bits are supported with Triple-DES.
  #endif
#endif

// Supported block length
#if defined(ENCRYPTION_AES_LTC)
  #if (ENCRYPTION_BLOCK_LENGTH != 16)
    #error Only a block length of 128 bits is supported with AES.
  #endif
#elif defined(ENCRYPTION_3DES_LTC)
  #if (ENCRYPTION_BLOCK_LENGTH != 8)
    #error Only a block length of 64 bits is supported with Triple-DES.
  #endif
#endif

//------------------------------------------------------------------------------
// Definitions
//------------------------------------------------------------------------------

// Functions
#define encryption_init     ltc_init
#define encryption_cleanup  ltc_cleanup
#define encryption_decrypt  ltc_decrypt

#if defined(ENCRYPTION_AES_LTC)
  #define CIPHER_NAME       "rijndael"
  #define CIPHER_DESC       rijndael_desc
#elif defined(ENCRYPTION_3DES_LTC)
  #define CIPHER_NAME       "3des"
  #define CIPHER_DESC       des3_desc
#endif

//------------------------------------------------------------------------------
// Prototypes
//------------------------------------------------------------------------------

void ltc_init(void);
void ltc_cleanup(void);
int ltc_decrypt(const unsigned char *, unsigned char *, unsigned int);

#endif // defined(USE_ENCRYPTION) && (defined(ENCRYPTION_AES_LTC) || defined(ENCRYPTION_3DES_LTC))
#endif // BOOTLOADER_AES_LIBTOMCRYPT_H
