/*!
* @file     Crypto_Cse.h
*
* @version  1.0.0
*
* @brief    AUTOSAR Crypto Driver header file
 *           
* @attention
*
* Project Name      : AUTOSAR MCAL
*
* Platform          : Arm
*
* Autosar Standard  : Classic Platform 4.4.0
*
* Compiled Version  : G32A10xx_MCAL_1_0_0_15-Nov-25
*
* 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 usefull 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.
*/

#ifndef CRYPTO_CSE_H
#define CRYPTO_CSE_H

#ifdef __cplusplus
extern "C"{
#endif
 
#include "Mcal.h"
#include "Reg_eSys.h"
#include "Crypto_Cfg.h"
#include "Crypto_Types.h"
/** @addtogroup G32A10xx_StdPeriphDriver
  @{
*/

/** @defgroup CRYPTO_Driver
  * @brief CRYPTO driver modules
  @{
*/

/** @defgroup CRYPTO_Macros
  @{
*/
/*******************************************************************************
                    VENDOR ID & AUTOSAR VERSION INFO
*******************************************************************************/
/** Vendor ID (vendorId) of the dedicated implementation of this module according to the AUTOSAR vendor list. */
#define CRYPTO_CSE_H_VENDOR_ID                          0x00C1U
/** Major version number of AUTOSAR release on which the appropriate implementation is based on. */
#define CRYPTO_CSE_H_AR_RELEASE_MAJOR_VERSION           0x04U
/** Minor version number of AUTOSAR release on which the appropriate implementation is based on. */
#define CRYPTO_CSE_H_AR_RELEASE_MINOR_VERSION           0x04U
/** Revision version number of AUTOSAR release on which the appropriate implementation is based on. */
#define CRYPTO_CSE_H_AR_RELEASE_REVISION_VERSION        0x00U
/** Major version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define CRYPTO_CSE_H_SW_MAJOR_VERSION                   0x01U
/** Minor version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define CRYPTO_CSE_H_SW_MINOR_VERSION                   0x00U
/** Patch level version number of the vendor specific implementation of the module. The numbering is vendor specific. */
#define CRYPTO_CSE_H_SW_PATCH_VERSION                   0x00U

/*******************************************************************************
                             VERSION CHECK
*******************************************************************************/
/* Make sure the vendor id is the same between Crypto_Cse.h and Crypto_Types.h */
#if (CRYPTO_CSE_H_VENDOR_ID != CRYPTO_TYPES_H_VENDOR_ID)
    #error "Vendor id mismatch between Crypto_Cse.h and Crypto_Types.h"
#endif
/* Make sure the AUTOSAR major version number is the same between Crypto_Cse.h and Crypto_Types.h */
#if (CRYPTO_CSE_H_AR_RELEASE_MAJOR_VERSION != CRYPTO_TYPES_H_AR_RELEASE_MAJOR_VERSION)
    #error "AUTOSAR major version number mismatch between Crypto_Cse.h and Crypto_Types.h"
#endif
/* Make sure the AUTOSAR minor version number is the same between Crypto_Cse.h and Crypto_Types.h */
#if (CRYPTO_CSE_H_AR_RELEASE_MINOR_VERSION != CRYPTO_TYPES_H_AR_RELEASE_MINOR_VERSION)
    #error "AUTOSAR minor version number mismatch between Crypto_Cse.h and Crypto_Types.h"
#endif
/* Make sure the AUTOSAR revision version number is the same between Crypto_Cse.h and Crypto_Types.h */
#if (CRYPTO_CSE_H_AR_RELEASE_REVISION_VERSION != CRYPTO_TYPES_H_AR_RELEASE_REVISION_VERSION)
    #error "AUTOSAR revision version number mismatch between Crypto_Cse.h and Crypto_Types.h"
#endif
/* Make sure the software major version number is the same between Crypto_Cse.h and Crypto_Types.h */
#if (CRYPTO_CSE_H_SW_MAJOR_VERSION != CRYPTO_TYPES_H_SW_MAJOR_VERSION)
    #error "Software major version number mismatch between Crypto_Cse.h and Crypto_Types.h"
#endif
/* Make sure the software minor version number is the same between Crypto_Cse.h and Crypto_Types.h */
#if (CRYPTO_CSE_H_SW_MINOR_VERSION != CRYPTO_TYPES_H_SW_MINOR_VERSION)
    #error "Software minor version number mismatch between Crypto_Cse.h and Crypto_Types.h"
#endif
/* Make sure the software patch version number is the same between Crypto_Cse.h and Crypto_Types.h */
#if (CRYPTO_CSE_H_SW_PATCH_VERSION != CRYPTO_TYPES_H_SW_PATCH_VERSION)
    #error "Software patch version number mismatch between Crypto_Cse.h and Crypto_Types.h"
#endif
/* Make sure the vendor id is the same between Crypto_Cse.h and Crypto_Cfg.h */
#if (CRYPTO_CSE_H_VENDOR_ID != CRYPTO_CFG_H_VENDOR_ID)
    #error "Vendor id mismatch between Crypto_Cse.h and Crypto_Cfg.h"
#endif
/* Make sure the AUTOSAR major version number is the same between Crypto_Cse.h and Crypto_Cfg.h */
#if (CRYPTO_CSE_H_AR_RELEASE_MAJOR_VERSION != CRYPTO_CFG_H_AR_RELEASE_MAJOR_VERSION)
    #error "AUTOSAR major version number mismatch between Crypto_Cse.h and Crypto_Cfg.h"
#endif
/* Make sure the AUTOSAR minor version number is the same between Crypto_Cse.h and Crypto_Cfg.h */
#if (CRYPTO_CSE_H_AR_RELEASE_MINOR_VERSION != CRYPTO_CFG_H_AR_RELEASE_MINOR_VERSION)
    #error "AUTOSAR minor version number mismatch between Crypto_Cse.h and Crypto_Cfg.h"
#endif
/* Make sure the AUTOSAR revision version number is the same between Crypto_Cse.h and Crypto_Cfg.h */
#if (CRYPTO_CSE_H_AR_RELEASE_REVISION_VERSION != CRYPTO_CFG_H_AR_RELEASE_REVISION_VERSION)
    #error "AUTOSAR revision version number mismatch between Crypto_Cse.h and Crypto_Cfg.h"
#endif
/* Make sure the software major version number is the same between Crypto_Cse.h and Crypto_Cfg.h */
#if (CRYPTO_CSE_H_SW_MAJOR_VERSION != CRYPTO_CFG_H_SW_MAJOR_VERSION)
    #error "Software major version number mismatch between Crypto_Cse.h and Crypto_Cfg.h"
#endif
/* Make sure the software minor version number is the same between Crypto_Cse.h and Crypto_Cfg.h */
#if (CRYPTO_CSE_H_SW_MINOR_VERSION != CRYPTO_CFG_H_SW_MINOR_VERSION)
    #error "Software minor version number mismatch between Crypto_Cse.h and Crypto_Cfg.h"
#endif
/* Make sure the software patch version number is the same between Crypto_Cse.h and Crypto_Cfg.h */
#if (CRYPTO_CSE_H_SW_PATCH_VERSION != CRYPTO_CFG_H_SW_PATCH_VERSION)
    #error "Software patch version number mismatch between Crypto_Cse.h and Crypto_Cfg.h"
#endif

/*******************************************************************************
                            DEFINES AND MACROS
*******************************************************************************/

/** RCM configuration register & flags */
#define RCM_AHBCLKEN_ADDR              ((uint32)(0x40021014UL))  /* Flash configuration register address */
/** RAM current state flag is not configured */
#define RCM_AHBRNGSHAAES_CONFIGURED    ((uint32)0x380UL)

/**@} end of group CRYPTO_Macros */
/*******************************************************************************
                                ENUMS
*******************************************************************************/
/** @defgroup CRYPTO_Enumerations
  @{
*/
/**
 * @brief    CSE error codes
 */
typedef enum  
{
    CSE_ERC_ERROR               = 0x0000U, //!< exit error 
    CSE_ERC_OK                  = 0x0001U, //!< No error 
    CSE_ERC_SEQUENCE_ERROR      = 0x0002U, //!< Invalid call sequence 
    CSE_ERC_KEY_NOT_AVAILABLE   = 0x0004U, //!< Key has DBG attached flag and debugger is active 
    CSE_ERC_KEY_INVALID         = 0x0008U, //!< Key is not allowed for the given operation 
    CSE_ERC_KEY_EMPTY           = 0x0010U, //!< Key slot is empty 
    CSE_ERC_NO_SECURE_BOOT      = 0x0020U,
    CSE_ERC_KEY_WRITE_PROTECTED = 0x0040U, //!< Key is write protected 
    CSE_ERC_KEY_UPDATE_ERROR    = 0x0080U, //!< Key update authentication failed 
    CSE_ERC_RNG_SEED            = 0x0100U, //!< PRNG seed has not yet been initialized 
    CSE_ERC_NO_DEBUGGING        = 0x0200U, //!< DEBUG command authentication failed 
    CSE_ERC_MEMORY_FAILURE      = 0x0400U, //!< General memory failure 
    CSE_ERC_GENERAL_ERROR       = 0x0800U, //!< Error that is not defined above 
    CSE_ERC_BUSY                = 0x1000U, //!< Detected by crypto driver: another command is in progress 
    CSE_ERC_DATA_LENGTH         = 0x2000U, //!< Detected by crypto driver: input data length is Overflow 
    CSE_ERC_SMALL_BUFFER        = 0x0FFFU, //!< Detected by crypto driver: buffer is too small 
    CSE_ERC_CANCELED            = 0xFFFFU  //!< Detected by crypto driver: command is canceled 
} Crypto_Cse_ErrorCodeType;
/**
 * @brief    Current working state
 */
typedef enum     
{
    CSE_BUSY    = 0x00, //!< Currently busy with a command 
    CSE_IDLE    = 0x01  //!< Idle state 
} Crypto_Cse_WorkingStateType;
/**
 * @brief    CSE commands 
 */
typedef enum  
{
    CSE_CMD_ENC_ECB         = 0x01U, //!< AES-256 encryption in ECB mode 
    CSE_CMD_ENC_CBC         = 0x02U, //!< AES-256 encryption in CBC mode 
    CSE_CMD_DEC_ECB         = 0x03U, //!< AES-256 decryption in ECB mode 
    CSE_CMD_DEC_CBC         = 0x04U, //!< AES-256 decryption in CBC mode 
    CSE_CMD_GENERATE_MAC    = 0x05U, //!< AES-256 based CMAC generation 
    CSE_CMD_VERIFY_MAC      = 0x06U, //!< AES-256 based CMAC verification 
    CSE_CMD_ENC_CTR         = 0x07U, //!< Update an internal key 
    CSE_CMD_DEC_CTR         = 0x08U, //!< Update RAM_KEY 
    CSE_CMD_EXPORT_RAM_KEY  = 0x09U, //!< Export RAM_KEY 
    CSE_CMD_INIT_RNG        = 0x0AU, //!< Initialize the PRNG 
    CSE_CMD_RND             = 0x0CU, //!< Generate random number 
    CSE_CMD_HASH            = 0x0DU, //!< Generate 256 bits HASH number 
    CSE_CMD_BOOT_FAILURE    = 0x0EU,
    CSE_CMD_BOOT_OK         = 0x0FU,
    CSE_CMD_GET_ID          = 0x10U,
    CSE_CMD_BOOT_DEFINE     = 0x11U,
    CSE_CMD_DBG_CHAL        = 0x12U,
    CSE_CMD_DBG_AUTH        = 0x13U,
    CSE_CMD_RESERVED_2      = 0x14U,
    CSE_CMD_RESERVED_3      = 0x15U,
    CSE_CMD_MP_COMPRESS     = 0x16U
} Crypto_Cse_CommandType;

/**
 * @brief State of the CSE hardware module
 */
typedef enum  
{
    CSE_HW_UNINIT   = 0x00U, //!< CSE module is not configured 
    CSE_HW_READY    = 0x01U  //!< CSE module is ready 
} Crypto_Cse_HardwareStateType;

/**
 * @brief CRYP Key Size (only AES)
 */
typedef enum
{
    AES256_KEYSIZE_128B  = 0,  //!< 128-bit key size 
    AES256_KEYSIZE_192B  = 1,  //!< 192-bit key size 
    AES256_KEYSIZE_256B  = 2   //!< 256-bit key size 
} Crypto_Aes256_KeySizeType;

/**
* @brief AES256 Algorithm Direction:Encryption/Decryption_mode
 */
typedef enum  
{
    AES256_ALGODIR_ENCRYPT   = 0x00,    //!< Encryption 
    AES256_ALGODIR_DECRYPT   = 0x01,    //!< Decryption 
    AES256_ALGODIR_KEYEXPAND = 0x02     //!< Key expand 
} Crypto_Aes256_OpcodeType;
/**@} end of group CRYPTO_Enumerations */
/*******************************************************************************
                            STRUCTURES AND TYPEDEFS
*******************************************************************************/
/** @defgroup CRYPTO_Structure
  @{
*/
/**
 * @brief    Export key configuration
 */
typedef struct  
{
    VAR(uint32, AUTOMATIC) keyId;
    VAR(uint32, AUTOMATIC) keyElementId;
    VAR(uint32, AUTOMATIC) labelLength;
    CONSTP2CONST(Crypto_KeyType, CRYPTO_VAR, CRYPTO_APPL_CONST) keyRef;
} Crypto_Cse_ExportKeyType;
/**
 * @brief    State of the current command
 */
typedef struct  
{
    VAR(uint32, AUTOMATIC) numberOfBlkn; //!< Number of Message Blocks to be Processed
    VAR(uint32, AUTOMATIC) location; //!< Location of the input and output data 
    VAR(uint32, AUTOMATIC) dataLength; //!< Length of the input data in byte to be process 
    VAR(uint32, AUTOMATIC) inputLength; //!< Length of the input data
    VAR(uint32, AUTOMATIC) secondarydataLength; //!< Length of the secondary Input Data data in byte  
    VAR(uint32, AUTOMATIC) refMacLength; //!< Length of the reference MAC
    VAR(uint16, AUTOMATIC) maxBlkn; //!< Number of Message Blocks available for data
    VAR(uint16, AUTOMATIC) BlknsWritten; //!< Number of Message Blocks have been written
    VAR(boolean, AUTOMATIC) refMacWritten; //!< Indicate if the reference MAC has been written
    P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) inputData; //!< Input data for the CSE command
    P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) secondaryInputData; //!< Secondary input data for the CSE command
    P2VAR(uint8, AUTOMATIC, CRYPTO_APPL_DATA) outputData; //!< Output data of the CSE command
    P2VAR(Crypto_VerifyResultType, AUTOMATIC, CRYPTO_APPL_DATA) verifyPtr; //!< MAC verification result
    VAR(Crypto_Cse_CommandType, AUTOMATIC) cmd; //!< CSE Command
    VAR(Crypto_Cse_ErrorCodeType, AUTOMATIC) response; //!< Result of the executed command
} Crypto_Cse_CmdStateType;
/**@} end of group CRYPTO_Structure */

/** @defgroup CRYPTO_Typedef
  @{
*/
/** 32-bit word that has to be written to trigger the execution of a command */
typedef uint32 Crypto_CseCmdType;
/**@} end of group CRYPTO_Typedef */
/*******************************************************************************
                            FUNCTION DECLARATIONS
*******************************************************************************/
/** @defgroup CRYPTO_Fuctions
  @{
*/
#define CRYPTO_START_SEC_CODE
#include "Crypto_MemMap.h"

ISR(SHA256_IRQHandler);
ISR(AES256_IRQHandler);
ISR(RNG_IRQHandler);

FUNC(Crypto_Cse_ErrorCodeType, CRYPTO_CODE) Crypto_Cse_GenerateRnd( P2VAR(uint8, AUTOMATIC, CRYPTO_APPL_DATA) RandomNumberPtr,
               VAR(Crypto_ProcessingType, AUTOMATIC) Mode);
               
FUNC(Crypto_Cse_ErrorCodeType, CRYPTO_CODE) Crypto_Cse_GenerateHash(P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) inBuffer, VAR(uint32, AUTOMATIC) lenBuffer, 
     P2VAR(uint8, AUTOMATIC, CRYPTO_APPL_DATA) outBuffer, VAR(Crypto_ProcessingType, AUTOMATIC) Mode);
     
/** Configuration */
FUNC(Crypto_Cse_ErrorCodeType, CRYPTO_CODE) Crypto_Sha256_HashGenerate(
    P2VAR(uint8, AUTOMATIC, CRYPTO_APPL_DATA) inBuffer, VAR(uint32, AUTOMATIC) lenBuffer, VAR(uint8, AUTOMATIC) outBuffer[32]);
    
FUNC(boolean, CRYPTO_CODE) Crypto_Cse_IsBusy(void);

FUNC(Std_ReturnType, CRYPTO_CODE) Crypto_Cse_Cancel(void);

FUNC(Crypto_Cse_ErrorCodeType, CRYPTO_CODE) Crypto_Cse_AesCtrEncryptDecrypt( VAR(Crypto_Aes256_OpcodeType, AUTOMATIC) mode, P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) key,
                       VAR(Crypto_Aes256_KeySizeType, AUTOMATIC) keysize, P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) IV, P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) input,
                       VAR(uint32, AUTOMATIC) length, P2VAR(uint8, AUTOMATIC, CRYPTO_APPL_DATA) output,VAR(Crypto_ProcessingType, AUTOMATIC) syncmode);

FUNC(Crypto_Cse_ErrorCodeType, CRYPTO_CODE) Crypto_Cse_AesCbcEncryptDecrypt( VAR(Crypto_Aes256_OpcodeType, AUTOMATIC) mode, P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) key,
                       VAR(Crypto_Aes256_KeySizeType, AUTOMATIC) keysize, P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) IV, P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) input,
                       VAR(uint32, AUTOMATIC) length, P2VAR(uint8, AUTOMATIC, CRYPTO_APPL_DATA) output,VAR(Crypto_ProcessingType, AUTOMATIC) syncmode);
                       
FUNC(Crypto_Cse_ErrorCodeType, CRYPTO_CODE) Crypto_Cse_AesEcbEncryptDecrypt( VAR(Crypto_Aes256_OpcodeType, AUTOMATIC) mode, P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) key,
                       VAR(Crypto_Aes256_KeySizeType, AUTOMATIC) keysize, P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) input,
                       VAR(uint32, AUTOMATIC) length, P2VAR(uint8, AUTOMATIC, CRYPTO_APPL_DATA) output,VAR(Crypto_ProcessingType, AUTOMATIC) syncmode);

FUNC(Crypto_Cse_ErrorCodeType, CRYPTO_CODE) Crypto_Cse_AesGenerateMpc(P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) input,VAR(uint32, AUTOMATIC) length, 
                       P2VAR(uint8, AUTOMATIC, CRYPTO_APPL_DATA) output,VAR(Crypto_ProcessingType, AUTOMATIC) syncmode);
                       
FUNC(Crypto_Cse_ErrorCodeType, CRYPTO_CODE) Crypto_Cse_AesGenerateMac( P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) key, VAR(Crypto_Aes256_KeySizeType, AUTOMATIC) keysize, 
                       P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) IV, P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) input,
                       VAR(uint32, AUTOMATIC) length, P2VAR(uint8, AUTOMATIC, CRYPTO_APPL_DATA) output,VAR(Crypto_ProcessingType, AUTOMATIC) syncmode);
                       
FUNC(Crypto_Cse_ErrorCodeType, CRYPTO_CODE) Crypto_Cse_MacVerify( P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) key, VAR(Crypto_Aes256_KeySizeType, AUTOMATIC) keysize, 
                       P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) IV, P2CONST(Crypto_JobType, AUTOMATIC, CRYPTO_APPL_CONST) JobPtr);
                       
#define CRYPTO_STOP_SEC_CODE
#include "Crypto_MemMap.h"
/**@} end of group CRYPTO_Fuctions */
#ifdef __cplusplus
}
#endif

#endif /* CRYPTO_CSE_H */
/**@} end of group CRYPTO_Driver */
/**@} end of group G32A10xx_StdPeriphDriver */
