/******************************************************************************
 * @file     main.c
 * @brief    Main program body
 * @version  V4.0.1
 * @date     30-July-2026
 *
 * @note
 * Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd.. 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<stdio.h>
#include<stdlib.h>

#if defined(SPC1158)
#include "spc1158.h"
#elif defined(SPD1148)
#include "spd1148.h"
#elif defined(SPD1178)
#include "spd1178.h"
#elif defined(SPD1188)
#include "spd1188.h"
#elif defined(SPD1163)
#include "spd1163.h"
#else
#include "spc1168.h"
#endif

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


uint8_t                u8AESTestData[INPUTDATALEN]       = "1234567890abcdef1234567890abcdef";
uint8_t                u8DecryptData[INPUTDATALEN]       = {0};
uint8_t                au8IV[16]                         = "1234567890abcdef";

uint8_t                au8Key[KEYSIZE]                   = "1234567890abcdef";
uint8_t                u8DataOut[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();

#if defined(SPC1158)
    FLASH_SetTiming(100000000);
    /* Disable flash write access after flash operation had done */
    FLASH_WDIS();

    CLOCK_InitWithRCO(100000000UL);
#else
    FLASH_SetTiming(200000000);
    /* Disable flash write access after flash operation had done */
    FLASH_WDIS();

    CLOCK_InitWithRCO(200000000UL);
#endif

    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);

    printf("AES test\n");

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

    status = AES_EncryptData(AES, (uint32_t *)u8AESTestData, (uint32_t *)u8DataOut, INPUTDATALEN / 4, OUTDATALEN / 4);
    if (status == SUCCESS)
    {
        printf("AES CBC mode Encrypt test PASS\n");
    }
    else
    {
        printf("AES CBC mode Encrypt test FAIL\n");
    }

    status = AES_DecryptData(AES, (uint32_t *)u8DataOut, (uint32_t *)u8DecryptData, OUTDATALEN / 4, INPUTDATALEN / 4);
    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 (u8DecryptData[count] != u8AESTestData[count])
        {
            printf("AES CBC En/De-crypt test FAIL\n");
            return 0;
        }
    }
    printf("AES CBC En/De-crypt test SUCCESS\n");

    while (1)
    {

    }
}
