//------------------------------------------------------------------------------
// File:          des_hardware.h
// Function:      Firmware encryption using DES hardware acceleration
// Supported chip(s):
//    - AT91SAM7XC128
//    - AT91SAM7XC256
// Supported toolchain(s):
//    - IAR Embedded Workbench
// Date created:  09 June 2006
// Created by:    JJo
//------------------------------------------------------------------------------

#ifndef BOOTLOADER_DES_HARDWARE_H
#define BOOTLOADER_DES_HARDWARE_H

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

#include "common.h"
#include "debug.h"
#include "board.h"

#if defined(USE_ENCRYPTION) && defined(ENCRYPTION_3DES_HARD)

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

#if (ENCRYPTION_KEY_LENGTH != 16) && (ENCRYPTION_KEY_LENGTH != 24)
  #error Triple-DES hardware acceleration only supports 128 and 192 bits keys.
#endif

#if (ENCRYPTION_BLOCK_LENGTH != 8)
  #error Triple-DES hardware acceleration only supports 64 bits blocks.
#endif

#if !defined(ENCRYPTION_ECB) && \
    !defined(ENCRYPTION_CBC) && \
    !defined(ENCRYPTION_CTR)
  #error Only ECB, CBC & CTR mode are supported.
#endif

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

// Functions
#define encryption_init     des_hard_init
#define encryption_cleanup  des_hard_cleanup
#define encryption_decrypt  des_hard_decrypt

#if defined(ENCRYPTION_ECB)
  #define TDES_MODE         AT91C_TDES_OPMOD_ECB
  #define TDES_CIPHER       0
#elif defined(ENCRYPTION_CBC)
  #define TDES_MODE         AT91C_TDES_OPMOD_CBC
  #define TDES_CIPHER       0
#elif defined(ENCRYPTION_CTR)
  #define TDES_MODE         AT91C_TDES_OPMOD_ECB
  #define TDES_CIPHER       AT91C_TDES_CIPHER
#endif

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

void des_hard_init(void);
void des_hard_cleanup(void);
int des_hard_decrypt(const unsigned char *, unsigned char *, unsigned int);

#endif // defined(USE_ENCRYPTION) && defined(ENCRYPTION_3DES_HARD)
#endif // BOOTLOADER_DES_HARDWARE_H
