/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V1.8.0
 * @date     31-Dec-2021
 *
 * @note
 * Copyright (C) 2021 Spintrol Limited Corporation. All rights reserved.
 *
 * @attention
 * THIS SOFTWARE JUST PROVIDES CUSTOMERS WITH CODING INFORMATION REGARDING
 * THEIR PRODUCTS, WHICH AIMS AT SAVING TIME FOR THEM. SPINTROL SHALL NOT BE
 * LIABLE FOR THE USE OF THE SOFTWARE. SPINTROL DOES NOT GUARANTEE THE
 * CORRECTNESS OF THIS SOFTWARE AND RESERVES THE RIGHT TO MODIFY THE SOFTWARE
 * WITHOUT NOTIFICATION.
 *
 ******************************************************************************/

#include"spc1168.h"
#include<stdio.h>
#include<stdlib.h>

#define                 INPUTDATALEN                       128                            /* Input data len */
#define                 KEYSIZE                            4                              /* Key size */
#define                 OUTDATALEN                         128                            /* Output data len */

uint32_t                u32AESTestData[INPUTDATALEN]       = {0};
uint32_t                u32DecryptData[INPUTDATALEN]       = {0};
uint8_t                 au8IV[16]                          = {0xCC, 0xAC, 0xCE, 0x55, 0xD0, 0xE5, 0x90, 0x0D};
uint32_t                au32Key[KEYSIZE]                   = {0};
uint32_t                u32DataOut[OUTDATALEN]             = {0};
uint32_t                count;
ErrorStatus             status                             = SUCCESS;


 /*************************************************************************************************************************
 *
 * @brief      In this case, we give a demo of how to use AES(CBC mode).
 *
 *************************************************************************************************************************/


int main()
{
  FLASH_WALLOW();
  FLASH_SetTiming(200000000);
  /* Disable flash write access after flash operation had done */
  FLASH_WDIS();

  CLOCK_InitWithRCO(CLOCK_HCLK_200MHZ);

  Delay_Init();

  /*
   * Init the UART
   *
   * 1.Set the GPIO34/35 as UART FUNC
   *
   * 2.Enable the UART CLK
   *
   * 3.Set the rest para
   */
  GPIO_SetPinChannel(GPIO_34,GPIO34_UART_TXD);
  GPIO_SetPinChannel(GPIO_35,GPIO35_UART_RXD);
  CLOCK_EnableModule(UART_MODULE);
  UART_Init(UART,38400);

  /* Get the input data (these are the datum you want to encryption) */
  for(count = 0; count < INPUTDATALEN; count ++)
      u32AESTestData[count] = abs(rand() % 0xFFFFFFFF);

  /* Init the key vector (you can define the key world, or randomize it) */
  for(count = 0; count < KEYSIZE; count++)
  {
      au32Key[count] = abs(rand() % 0xFFFFFFFF);
  }

  /* Set the AES as CBC mode */
  AES_CBCModeInit(AES, (uint32_t *)au8IV, au32Key, AES_KEYSIZE_16BYTE, (INPUTDATALEN * 4));

  status = AES_EncryptData(AES, u32AESTestData, u32DataOut, INPUTDATALEN, OUTDATALEN);
  if(status == SUCCESS)
  {
    printf("AES CBC mode Encrypt test PASS\n");
  }
  else
  {
    printf("AES CBC mode Encrypt test FAIL\n");
  }

  status = AES_DecryptData(AES, u32DataOut, u32DecryptData, OUTDATALEN, INPUTDATALEN);
  if(status == SUCCESS)
  {
    printf("AES CBC mode Decrypt test PASS\n");
  }
  else
  {
    printf("AES CBC mode Decrypt test FAIL\n");
  }

  /* Checking the raw data and the decrypted data */
  for(count = 0; count < INPUTDATALEN; count++)
  {
    if(u32DecryptData[count] != u32AESTestData[count])
    {
      printf("AES CBC En/De-crypt test FAIL\n");
      return 0;
    }
  }
  printf("AES CBC En/De-crypt test SUCCESS\n");

  while(1)
  {

  }
}
