/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @date        2026-02-25
 *
 * @attention
 *
 *  Copyright (C) 2026 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the Geehy Semiconductor Software License Agreement for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "Board.h"
#include "stdio.h"
#include "g32a10xx_eint.h"
#include "g32a10xx_syscfg.h"
#include "g32a10xx_aes256.h"
#include "g32a10xx_misc.h"
#include "user_aes256_config.h"
#include <string.h>

/** @addtogroup Examples
  @{
  */

/** @addtogroup AES256_Examples
  @{
  */

/** @addtogroup AES256_AllModes
  @{
  */

/** @defgroup AES256_AllModes_Macros Macros
  @{
*/

/* printf function configs to USART1 */
#define DEBUG_USART  USART1
#define  TIMEOUT_VALUE 0xFF

/**@} end of group AES256_AllModes_Macros */

/** @defgroup AES256_AllModes_Functions Functions
  @{
  */

/* Aes256_Delay */
void Aes256_Delay(uint32_t Count);

/*!
 * @brief    main program
 *
 * @param    None
 *
 * @retval   None
 *
 * @note
 */
int main(void)
{
    uint8_t cntTrue = 0;
    

    Board_LedInit(LED2);
    Board_LedInit(LED3);
    Board_ComInit(COM1);

    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_AES256);
    printf("\r\nSystem is Init\r\n");
    
    /* AES256 Mode: ECB */
    Rcm_EnableAhbPeriphReset(RCM_AHB_PERIPH_AES256);
    Rcm_DisableAhbPeriphReset(RCM_AHB_PERIPH_AES256);
    Aes256_Init(&g_aes256EcbHandle);
    
    /* Encryption, result in  g_aes256Encryptedtext buffer */
    Aes256_Encrypt(&g_aes256EcbHandle, g_aes256Plaintext, sizeof(g_aes256Plaintext) / sizeof(g_aes256Plaintext[0]), g_aes256Encryptedtext, TIMEOUT_VALUE);

    Rcm_EnableAhbPeriphReset(RCM_AHB_PERIPH_AES256);
    Rcm_DisableAhbPeriphReset(RCM_AHB_PERIPH_AES256);
    Aes256_Init(&g_aes256EcbHandle);

    memset(g_aes256Decryptedtext, 0, sizeof(g_aes256Decryptedtext));
    /* Decryption, result in  g_aes256Decryptedtext buffer */
    Aes256_Decrypt(&g_aes256EcbHandle, g_aes256Encryptedtext , sizeof(g_aes256Plaintext) / sizeof(g_aes256Plaintext[0]), g_aes256Decryptedtext, TIMEOUT_VALUE);

    /*compare with expected result */
    if(memcmp(g_aes256Decryptedtext, g_aes256Plaintext, sizeof(g_aes256Plaintext)) != 0)
    {
        printf("\r\nAES256 Mode: ECB Encrypt or Decrypt is false.\r\n");
    }
    else
    {
        cntTrue += 2U;
    }

    /* AES256 Mode: CBC */
    Rcm_EnableAhbPeriphReset(RCM_AHB_PERIPH_AES256);
    Rcm_DisableAhbPeriphReset(RCM_AHB_PERIPH_AES256);
    Aes256_Init(&g_aes256CbcHandle);

    /* Encryption, result in  g_aes256Encryptedtext buffer */
    Aes256_Encrypt(&g_aes256CbcHandle, g_aes256Plaintext, sizeof(g_aes256Plaintext) / sizeof(g_aes256Plaintext[0]), g_aes256Encryptedtext, TIMEOUT_VALUE);

    Rcm_EnableAhbPeriphReset(RCM_AHB_PERIPH_AES256);
    Rcm_DisableAhbPeriphReset(RCM_AHB_PERIPH_AES256);
    Aes256_Init(&g_aes256CbcHandle);
    
    memset(g_aes256Decryptedtext, 0, sizeof(g_aes256Decryptedtext));
    /* Decryption, result in  g_aes256Decryptedtext buffer */
    Aes256_Decrypt(&g_aes256CbcHandle, g_aes256Encryptedtext , sizeof(g_aes256Plaintext) / sizeof(g_aes256Plaintext[0]), g_aes256Decryptedtext, TIMEOUT_VALUE);

    /*compare with expected result */
    if(memcmp(g_aes256Decryptedtext, g_aes256Plaintext, sizeof(g_aes256Plaintext)) != 0)
    {
        printf("\r\nAES256 Mode: CBC Encrypt or Decrypt is false.\r\n");
    }
    else
    {
        cntTrue += 2U;
    }

    /* AES256 Mode: CTR */
    Rcm_EnableAhbPeriphReset(RCM_AHB_PERIPH_AES256);
    Rcm_DisableAhbPeriphReset(RCM_AHB_PERIPH_AES256);
    Aes256_Init(&g_aes256CtrHandle);

    /* Encryption, result in  g_aes256Encryptedtext buffer */
    Aes256_Encrypt(&g_aes256CtrHandle, g_aes256Plaintext, sizeof(g_aes256Plaintext) / sizeof(g_aes256Plaintext[0]), g_aes256Encryptedtext, TIMEOUT_VALUE);

    Rcm_EnableAhbPeriphReset(RCM_AHB_PERIPH_AES256);
    Rcm_DisableAhbPeriphReset(RCM_AHB_PERIPH_AES256);
    Aes256_Init(&g_aes256CtrHandle);
    
    memset(g_aes256Decryptedtext, 0, sizeof(g_aes256Decryptedtext));
    /* Decryption, result in  g_aes256Decryptedtext buffer */
    Aes256_Decrypt(&g_aes256CtrHandle, g_aes256Encryptedtext , sizeof(g_aes256Plaintext) / sizeof(g_aes256Plaintext[0]), g_aes256Decryptedtext, TIMEOUT_VALUE);
    
    /*compare with expected result */
    if(memcmp(g_aes256Decryptedtext, g_aes256Plaintext, sizeof(g_aes256Plaintext)) != 0)
    {
        printf("\r\nAES256 Mode: CTR Encrypt or Decrypt is false.\r\n");
    }
    else
    {
        cntTrue += 2U;
    }

    /* AES256 Mode: CMAC */
    Rcm_EnableAhbPeriphReset(RCM_AHB_PERIPH_AES256);
    Rcm_DisableAhbPeriphReset(RCM_AHB_PERIPH_AES256);
    Aes256_Init(&g_aes256CmacHandle);

    /* CMAC Generate, result in  g_aes256Encryptedtext buffer */
    Aes256_GenerateCmac(&g_aes256CmacHandle, AES256_PLAINTEXT_CMAC, sizeof(AES256_PLAINTEXT_CMAC) / sizeof(AES256_PLAINTEXT_CMAC[0]), g_aes256Encryptedtext, TIMEOUT_VALUE);

    /*compare with expected result */
    if(memcmp(g_aes256Encryptedtext, g_aes256CiphertextCmac256, sizeof(g_aes256CiphertextCmac256)) != 0)
    {
        printf("\r\nAES256 Mode: CMAC is false.\r\n");
    }
    else
    {
        cntTrue++;
    }

    /* AES256 Mode: MPC */
    Rcm_EnableAhbPeriphReset(RCM_AHB_PERIPH_AES256);
    Rcm_DisableAhbPeriphReset(RCM_AHB_PERIPH_AES256);
    Aes256_Init(&g_aes256MpcHandle);
    
    /* MPC Calculate, result in  g_aes256Encryptedtext buffer */
    Aes256_CalculateMpc(&g_aes256MpcHandle, g_aes256PlaintextMpc, sizeof(g_aes256PlaintextMpc) / sizeof(g_aes256PlaintextMpc[0]), g_aes256Encryptedtext, TIMEOUT_VALUE);

    /*compare with expected result */
    if(memcmp(g_aes256Encryptedtext, g_aes256CiphertextMpc, sizeof(g_aes256CiphertextMpc)) != 0)
    {
        printf("\r\nAES256 Mode: MPC is false.\r\n");
    }
    else
    {
        cntTrue++;
    }

    if (cntTrue == 8U)
    {
        Board_LedOn(LED2);
        printf("\r\nAES256 all test is true.\r\n");
    }
    else
    {
        Board_LedOff(LED2);
        printf("\r\nAES256 test is false.\r\n");
    }
    
    for (;;)
    {
        Aes256_Delay(0xfffff);
    }
}

/*!
 * @brief       Aes256_Delay
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void Aes256_Delay(uint32_t Count)
{
    volatile uint32_t delay = Count;

    while (delay != 0U)
    {
        delay = delay - 1U;
    }
}

#if defined (__CC_ARM) || defined (__ICCARM__) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @param       *f:  pointer to a FILE that can recording all information
*              needed to control a stream
*
* @retval      The characters that need to be send.
*
* @note
*/
int fputc(int ch, FILE* f)
{
    /* send a byte of data to the serial port */
    Usart_TxData(DEBUG_USART, (uint8_t)ch);

    /* wait for the data to be send  */
    while (Usart_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == (uint8_t)RESET)
    {
        /*  nothing */
    }

    return (ch);
}

#elif defined (__GNUC__)

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @retval      The characters that need to be send.
*
* @note
*/
int __io_putchar(int ch)
{
    /* send a byte of data to the serial port */
    Usart_TxData(DEBUG_USART, ch);

    /* wait for the data to be send  */
    while (Usart_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);

    return ch;
}

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       file:  Meaningless in this function.
*
* @param       *ptr:  Buffer pointer for data to be sent.
*
* @param       len:  Length of data to be sent.
*
* @retval      The characters that need to be send.
*
* @note
*/
int _write(int file, char* ptr, int len)
{
    int i;
    for (i = 0; i < len; i++)
    {
        __io_putchar(*ptr++);
    }

    return len;
}

#else
#pragma message("Not supported compiler type")
#endif

/**@} end of group AES256_AllModes_Functions */
/**@} end of group AES256_AllModes */
/**@} end of group AES256_Examples */
/**@} end of group Examples */
