/**
*   @file    Crypto_Cse.h
*
*   @version 1.0.0
*   @brief   AUTOSAR Crypto - Cryptographic (Crypto) interface
*   @details API header for Crypto driver.
*
*   @addtogroup  Crypto_Cse
*   @{
*/
/*==================================================================================================
*   Project              : AUTOSAR 4.3 MCAL
*   Platform             : PA
*   Peripheral           : Crypto
*   Dependencies         : none
*
*   Autosar Version      : 4.3.1
*   Autosar Revision     : ASR_REL_4_3_REV_0001
*   Autosar Conf.Variant :
*   SW Version           : 1.0.0
*   Build Version        : MPC5777C_MCAL_1_0_0_RTM_ASR_REL_4_3_REV_0001_20190315
*
*   (c) Copyright 2006-2016 Freescale Semiconductor, Inc. 
*       Copyright 2017-2019 NXP
*   All Rights Reserved.
==================================================================================================*/
/*==================================================================================================
==================================================================================================*/

#ifndef CRYPTO_CSE_H
#define CRYPTO_CSE_H

#ifdef __cplusplus
extern "C"{
#endif

/*==================================================================================================
*                                        INCLUDE FILES
* 1) system and project includes
* 2) needed interfaces from external units
* 3) internal and external interfaces from this unit
==================================================================================================*/
#include "Crypto_Cse_Cmd.h"

/*==================================================================================================
*                              SOURCE FILE VERSION INFORMATION
==================================================================================================*/
#define CRYPTO_CSE_VENDOR_ID                       43
#define CRYPTO_CSE_AR_RELEASE_MAJOR_VERSION        4
#define CRYPTO_CSE_AR_RELEASE_MINOR_VERSION        3
#define CRYPTO_CSE_AR_RELEASE_REVISION_VERSION     1
#define CRYPTO_CSE_SW_MAJOR_VERSION                1
#define CRYPTO_CSE_SW_MINOR_VERSION                0
#define CRYPTO_CSE_SW_PATCH_VERSION                0

/*==================================================================================================
*                                    FILE VERSION CHECKS
==================================================================================================*/
/* Check if Crypto_Cse header file and Crypto_Cse_Cmd header file are of the same vendor */
#if (CRYPTO_CSE_VENDOR_ID != CRYPTO_CSE_CMD_VENDOR_ID)
    #error "Crypto_Cse.h and Crypto_Cse_Cmd.h have different vendor ids"
#endif

/* Check if Crypto_Cse header file and Crypto_Cse_Cmd header file are of the same Autosar version */
#if ((CRYPTO_CSE_AR_RELEASE_MAJOR_VERSION    != CRYPTO_CSE_CMD_AR_RELEASE_MAJOR_VERSION) || \
     (CRYPTO_CSE_AR_RELEASE_MINOR_VERSION    != CRYPTO_CSE_CMD_AR_RELEASE_MINOR_VERSION) || \
     (CRYPTO_CSE_AR_RELEASE_REVISION_VERSION != CRYPTO_CSE_CMD_AR_RELEASE_REVISION_VERSION) \
    )
    #error "AutoSar Version Numbers of Crypto_Cse.h and Crypto_Cse_Cmd.h are different"
#endif

/* Check if Crypto_Cse header file and Crypto_Cse_Cmd header file are of the same Software version */
#if ((CRYPTO_CSE_SW_MAJOR_VERSION != CRYPTO_CSE_CMD_SW_MAJOR_VERSION) || \
     (CRYPTO_CSE_SW_MINOR_VERSION != CRYPTO_CSE_CMD_SW_MINOR_VERSION) || \
     (CRYPTO_CSE_SW_PATCH_VERSION != CRYPTO_CSE_CMD_SW_PATCH_VERSION)    \
    )
    #error "Software Version Numbers of Crypto_Cse.h and Crypto_Cse_Cmd.h are different"
#endif

/*==================================================================================================
*                                           CONSTANTS
==================================================================================================*/
#define     CRYPTO_LENGTH_OF_ARRAY_16                   (16U)

/* Macro to get the number of blocks of 128 bit length from a length given in bytes */
#define Crypto_Cse_Get128BitBuffBlockCount(len)         (((len) + (16U - 1U)) / 16U)

/*==================================================================================================
*                                       DEFINES AND MACROS
==================================================================================================*/
/* SHE keys Input Output */
#define CRYPTO_M1SIZE_U32                     ((uint32)0x10U)
#define CRYPTO_M2SIZE_U32                     ((uint32)0x20U)
#define CRYPTO_M3SIZE_U32                     ((uint32)0x10U)
#define CRYPTO_M4SIZE_U32                     ((uint32)0x20U)
#define CRYPTO_M5SIZE_U32                     ((uint32)0x10U)

#define CRYPTO_SHE_SIZE_OUT_U32                ((uint32)(CRYPTO_M1SIZE_U32 + CRYPTO_M2SIZE_U32 + CRYPTO_M3SIZE_U32 + CRYPTO_M4SIZE_U32 + CRYPTO_M5SIZE_U32))

#define CRYPTO_M1INDEX_U32                    ((uint32)(0U))
#define CRYPTO_M2INDEX_U32                    ((uint32)(CRYPTO_M1SIZE_U32))
#define CRYPTO_M3INDEX_U32                    ((uint32)(CRYPTO_M1SIZE_U32 + CRYPTO_M2SIZE_U32))
#define CRYPTO_M4INDEX_U32                    ((uint32)(CRYPTO_M1SIZE_U32 + CRYPTO_M2SIZE_U32 + CRYPTO_M3SIZE_U32))
#define CRYPTO_M5INDEX_U32                    ((uint32)(CRYPTO_M1SIZE_U32 + CRYPTO_M2SIZE_U32 + CRYPTO_M3SIZE_U32 + CRYPTO_M4SIZE_U32))

#define CRYPTO_MPCOMP_OUT_SIZE_U32              ((uint32)128U)

/*==================================================================================================
*                                             ENUMS
==================================================================================================*/

/*==================================================================================================
*                                 STRUCTURES AND OTHER TYPEDEFS
==================================================================================================*/

/*==================================================================================================
*                                GLOBAL VARIABLE DECLARATIONS
==================================================================================================*/

/*==================================================================================================
*                                     FUNCTION PROTOTYPES
==================================================================================================*/
#define CRYPTO_START_SEC_CODE
#include "Crypto_MemMap.h"

/**
* @brief           Initializes the driver.
* @details         Initializes the driver.
*
* @param[in]       None.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @api
*
* @pre            Driver must be initialized.
*
*/
FUNC(Std_ReturnType, CRYPTO_CODE)Crypto_Cse_Init(void);

/**
* @brief           Processes a job.
* @details         Tries to initiate a request to CSE to process a job. If CSE is busy and the job is async, it queues it.
*
* @param[in]       u32ObjectIdIdx    Ientifier of the Crypto Driver Object to be used for processing the job.
* @param[in]       pJob              Pointer to the job to be processed.
*
* @return          Result of the operation.
*                   Std_ReturnType
* @api
*
* @pre            Driver must be initialized.
*
*/
FUNC(Std_ReturnType, CRYPTO_CODE) Crypto_Cse_ProcessJob
(
    VAR  (uint32,         AUTOMATIC                  ) u32ObjectIdIdx,
    P2VAR(Crypto_JobType, AUTOMATIC, CRYPTO_APPL_DATA) pJob
);

/**
* @brief           Cancels a job.
* @details         Tries to cancel the currently processing job if the id is the same. Otherwise searches the queue of jobs in order to dequeue it.
*
* @param[in]       u32ObjectIdIdx    Identifier of the Crypto Driver Object to be used for canceling the job.
* @param[in]       pJobInfo          Pointer to the jobInfo of the job to be canceled.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @api
*
* @pre            Driver must be initialized.
*
*/
FUNC(Std_ReturnType, CRYPTO_CODE) Crypto_Cse_CancelJob
(
    VAR  (uint32,             AUTOMATIC                  ) u32ObjectIdIdx,
    P2VAR(Crypto_JobInfoType, AUTOMATIC, CRYPTO_APPL_DATA) pJobInfo
);

/**
* @brief           Main function.
* @details         Main function checks for async jobs, then send jobs from the queue.
*
* @param[in]       None.
*
* @return          Result of the operation.
*                   Void.
*
* @api
*
* @pre            Driver must be initialized.
*
*/
FUNC (void, CRYPTO_CODE) Crypto_Cse_MainFunction(void);

/**
* @brief           Generates a vector of 128 ture random bits.
* @details         The function returns a vector of 128 ture random bits. 
*                  CSE_CR[DIV] field must be properly configured before this command is executed.
*                  There is a very small probability that this command may return a TRNG error (EC = 0x12) even when the TRNG is operating properly.
*                  This function does not work for VLRP (Very Low Power).
*
* @param[out]      pTrueRandomValue: The address where the random value will be stored.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @api
*
* @pre            Driver must be initialized.
*
*/
FUNC(Std_ReturnType, CRYPTO_CODE) Crypto_Cse_GenerateTrueRandom
(
    P2VAR(uint8, AUTOMATIC, CRYPTO_APPL_CONST) pTrueRandomValue
);

/**
* @brief        Generates the internal seed state using the provided entropy source.
* @details      Update the seed state with new entropy.
*
* @param[in]    pEntropy          Pointer to a 128-bit buffer containing the entropy
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @pre          Driver must be initialized.
*
*/
FUNC(Std_ReturnType, CRYPTO_CODE) Crypto_Cse_GenerateSeed
(
    P2CONST(uint8,  AUTOMATIC, CRYPTO_APPL_CONST) pEntropy,
    VAR    (uint32, AUTOMATIC                   ) u32SeedLength,
    P2VAR  (uint8,  AUTOMATIC, CRYPTO_APPL_DATA ) pSeedState
);

/**
* @brief           Import key.
* @details         Loads the key from the pointer to RAM,NVM or using SHE specification.
*
* @param[in]       pKeyPtr             Pointer to the buffer containing the key that needs to be copied.
* @param[in]       u32CryptoKeyIdIdx   The index of the used key.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @api
*
* @pre            Driver must be initialized.
*
*/
FUNC (Std_ReturnType, CRYPTO_CODE) Crypto_Cse_LoadKey
(
    P2CONST (uint8,  AUTOMATIC, CRYPTO_APPL_CONST) pKeyPtr,
    VAR     (uint32, AUTOMATIC                   ) u32CryptoKeyIdIdx
);

/**
* @brief           Export key.
* @details         Exports the RAM key to the provided address.
*
* @param[in]       u32CryptoKeyIdIdx    The index of the used key.
* @param[out]      pResult              Address where the key will be stored.
* @param[in]       pResultLength        Length of the output pointer.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @api
*
* @pre            Driver must be initialized.
*
*/
FUNC(Std_ReturnType, CRYPTO_CODE) Crypto_Cse_ExportKey 
(
    VAR  (uint32, AUTOMATIC                  ) u32CryptoKeyElementIdIdx,  
    P2VAR(uint8,  AUTOMATIC, CRYPTO_APPL_DATA) resultPtr,
    P2VAR(uint32, AUTOMATIC, CRYPTO_APPL_DATA) resultLengthPtr
);

/**
* @brief           Generate Key.
* @details         Generates a key and loads it into RAM slot.
*
* @param[in]       u32KeyId    The slot where they key will be stored, it must be the RAM slot.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @api
*
* @pre            Driver must be initialized.
*
*/
FUNC(Std_ReturnType, CRYPTO_CODE) Crypto_Cse_GenerateKey
(
    VAR(uint32, AUTOMATIC) u32CryptoKeyElementIdIdx
);

/**
* @brief           Send debug challenge command.
* @details         Generates a 128-bit random challenge output value that is used in conjunction with the DEBUG_AUTH command.
*
* @param[out]      pOutChallengeAdress - Message start address where the output challenge will be stored.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @api
*
* @pre            Driver must be initialized.
*
*/
FUNC(Std_ReturnType, CRYPTO_CODE)Crypto_Cse_DebugChallenge
(
    P2VAR(uint8, AUTOMATIC, CRYPTO_APPL_DATA) pOutChallengeAdress
);

/**
* @brief           Send debug authorization command.
* @details         Erases all user keys and sets the CSE_SR[IDB] flag which enables internal debugging if the 128-bit authorization input value is valid
*                  and no memory slots are write protected.
*
* @param[in]       pAuthorization - Message start address where the output challenge was stored.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @api
*
* @pre            Driver must be initialized.
*
*/
FUNC(Std_ReturnType, CRYPTO_CODE)Crypto_Cse_DebugAuthorization
(
    P2VAR(uint8, AUTOMATIC, CRYPTO_APPL_DATA) pAuthorization
);

/**
* @brief           Miyaguchi-Preneel Compression.
* @details         One-way compression function used to derive a 128-bit output from a given message.
*
* @param[in]       pInputKey         Message start address.
* @param[in]       pInputKeyLen      Message length (bytes) address.
* @param[out]      pResult           Output address.
* @param[in]       eJobProcessing    The processing type used.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @api
*
* @pre            Driver must be initialized.
*
*/
FUNC(Std_ReturnType, CRYPTO_CODE) Crypto_Cse_MPCompression 
(
    P2CONST(uint8,                 AUTOMATIC, CRYPTO_APPL_CONST) pInputKey, 
    P2VAR  (uint64,                AUTOMATIC, CRYPTO_APPL_DATA ) pInputKeyLen,
    P2VAR  (uint8,                 AUTOMATIC, CRYPTO_APPL_DATA ) pResult,
    VAR    (Crypto_ProcessingType, AUTOMATIC                   ) eJobProcessing
);

/**
* @brief           Configure Secure Counter.
* @details         Using the provided ID and Rollover Value CSE will associate a secure counter with the given id and set its Rollover Value.
*                  A status will be returned after the execution of the command to inform if a new counter is used or an existing one is reused.
*
* @param[in]       u32CounterId        The id of the secure counter.
* @param[in]       u32RolloverValue    Defines the secure counter value at which point the RP is stored in flash memory.
* @param[out]      pStatus             Informs if a new secure counter is used.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @api
*
* @pre            Driver must be initialized.
*
*/
FUNC(Std_ReturnType, CRYPTO_CODE) Crypto_Cse_CfgSecCounter
(
    VAR(uint32,   AUTOMATIC                  ) u32CounterId,
    VAR(uint32,   AUTOMATIC                  ) u32RolloverValue,
    P2VAR(uint32, AUTOMATIC, CRYPTO_APPL_DATA) pStatus
);

/**
* @brief        Generates UID value.
* @details      UID will be written in pointer parameter.
*
* @param[out]    pUidResultAddr          Pointer to a 128-bit buffer where UID will be stored.
* @param[out]    pMacAddr                Pointer to a 128-bit buffer where MAC key will be stored.
* @param[out]    pSregAddr               Pointer to a 128-bit buffer where status register will be stored.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @pre          Driver must be initialized.
*
*/
FUNC(Std_ReturnType, CRYPTO_CODE) Crypto_Cse_GetID
(
    P2VAR(uint8, AUTOMATIC, CRYPTO_APPL_DATA) pUidResultAddr,
    P2VAR(uint8, AUTOMATIC, CRYPTO_APPL_DATA) pMacAddr,
    P2VAR(uint8, AUTOMATIC, CRYPTO_APPL_DATA) pSregAddr
);

/**
* @brief         Send BOOT OK command.
* @details       The BOOT_OK command sets the CSE_SR[BFN] flag and leaves the CSE_SR[BOK] flag set to confirm successful completion of the secure boot processes.
*                This enables the use of memory slots that have the BOOT_PROT flag set if they are not disabled for some other reason.
*
* @param[in]     None
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @pre          
*
*/
FUNC (Std_ReturnType, CRYPTO_CODE) Crypto_Cse_BootOk(void);

/**
* @brief         Send Boot Failure command.
* @details       The BOOT_FAILURE command sets the CSE_SR[BFN] flag and clears the CSE_SR[BOK] flag to disable use of memory slots that have the BOOT_PROT flag set.
*
* @param[in]     None.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @pre         
*
*/
FUNC (Std_ReturnType, CRYPTO_CODE) Crypto_Cse_BootFailure(void);

/**
* @brief           Send command for SHA-256.
* @details         The SHA-256_HASH command generates the hash over the input message based on the RSA SSA-PKCS1-V1_5 standard.
*
* @param[in]       pInputKey         Message start address.
* @param[in]       pInputKeyLen      Message length (bytes) address.
* @param[out]      pResult           Output address.
* @param[in]       eJobProcessing    The processing type used.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @api
*
* @pre            Driver must be initialized.
*
*/
FUNC(Std_ReturnType, CRYPTO_CODE) Crypto_Cse_SHA256
(
    P2CONST(uint8,                 AUTOMATIC, CRYPTO_APPL_CONST) pInputKey, 
    P2VAR  (uint64,                AUTOMATIC, CRYPTO_APPL_DATA ) pInputKeyLen,
    P2VAR  (uint8,                 AUTOMATIC, CRYPTO_APPL_DATA ) pResult,
    VAR    (Crypto_ProcessingType, AUTOMATIC                   ) eJobProcessing
);

/**
* @brief           Send session challenge command.
* @details         The SS_CHAL command outputs a 288-bit value consisting of a 128-bit random challenge value concatenated with the 120-bit UID,
*                  4-bit Certificate ID, 4-bit Challenge Mode, and 32 bits of zero padding.
*
* @param[out]      pOutChallengeAdress    Message start address where the output challenge will be stored.
* @param[in]       u8CertificateId        Public key specified.
* @param[in]       u8ChallengeMode        0 - No keys are erased.
*                                         1 - Erase all symmetric keys, excluding BOOT_MAC_KEY and BOOT_MAC.
*                                         2 - Erase all installed public key certificates, including ROOT_PK and CRL.
*                                         3 - Erase all installed public key certificates, including ROOT_PK and CRL.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @api
*
* @pre            Driver must be initialized.
*
*/
FUNC(Std_ReturnType, CRYPTO_CODE)Crypto_Cse_SessionChallenge
(
    P2VAR(uint8, AUTOMATIC, CRYPTO_APPL_DATA)pOutChallengeAdress,
    VAR  (uint8, AUTOMATIC                  )u8CertificateId,
    VAR  (uint8, AUTOMATIC                  )u8ChallengeMode
);

/**
* @brief         Send Install Certificate command.
* @details       The INSTALL_CERTIFICATE command authenticates a public key certificate stored in system flash memory and enables the public key to be used
*                for subsequent CSE operations.
*
* @param[in]     u8CertifId        Certificate ID.
* @param[in]     u8AuthCertifId    Authorization certificate ID.
* @param[in]     pCertifAddr       Certificate Address.
* @param[in]     u8CertifType      Certificate Type.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @pre         Driver must be initialized.
*
*/
FUNC (Std_ReturnType, CRYPTO_CODE) Crypto_Cse_InstallCertificate
(
    VAR    (uint8, AUTOMATIC                   ) u8CertifId,
    VAR    (uint8, AUTOMATIC                   ) u8AuthCertifId,
    P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) pCertifAddr,
    VAR    (uint8, AUTOMATIC                   ) u8CertifType
);

/**
* @brief         Send Delete plain text key command.
* @details       The DELETE_PT_KEY command clears plaintext key from RAM.
*
* @param[in]     u8KeyId    The input key ID.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @pre         Driver must be initialized.
*
*/
FUNC (Std_ReturnType, CRYPTO_CODE) Crypto_Cse_DeletePlainTextKey
(
    VAR (uint8, AUTOMATIC) u8KeyId
);

/**
* @brief            CMAC Generation with Secure Counter.
* @details          Calculates the MAC of a given input message concatenated with the incremented value of the selected secure counter, using the parameters provided.
*
* @param[in]    u32KeyId            KeyID used to perform the cryptographic operation.
* @param[in]    u8CounterId         The secure counter ID that will be used.
* @param[in]    u32MessageLength    Number of bytes of message on which CMAC is to be computed.
* @param[in]    pMessage            Pointer to the message buffer.
* @param[out]   pMac                Pointer to the 128-bit buffer containing the result of the CMAC generation.
* @param[out]   u32MacLen           The output buffer length for MAC.
* @param[out]   pVolatileCounter    Pointer to the the current VC.
*
* @return          Result of the operation.
*                   Std_ReturnType
* @api
*
* @pre            Driver must be initialized.
*
*/
FUNC(Std_ReturnType, CRYPTO_CODE) Crypto_Cse_MacGenerateSecCounter
( 
    VAR    (uint32, AUTOMATIC                   ) u32KeyId,
    VAR    (uint8,  AUTOMATIC                   ) u8CounterId,
    VAR    (uint32, AUTOMATIC                   ) u32MessageLength,
    P2CONST(uint8,  AUTOMATIC, CRYPTO_APPL_CONST) pMessage,
    P2VAR  (uint8,  AUTOMATIC, CRYPTO_APPL_DATA ) pMac,
    VAR    (uint32, AUTOMATIC                   ) u32MacLen,
    P2VAR  (uint32, AUTOMATIC, CRYPTO_APPL_DATA ) pVolatileCounter
);

/**
* @brief            CMAC Verification with Secure Counter.
* @details          Verifies the MAC of a given input message, concatenated with the implied value of the selected secure counter.
*
* @param[in]       u32KeyId             KeyID used to perform the cryptographic operation.
* @param[in]       u8CounterId          The secure counter ID that will be used.
* @param[in]       u8RlvPrtOffset       Rollover protection offset.
* @param[in]       u32MessageLength     Number of bytes of message on which CMAC is to be computed.
* @param[in]       pMessage             Pointer to the message buffer.
* @param[in]       pMac                 Pointer to the 128-bit buffer containing the the CMAC.
* @param[in]       u32MacLength         The length of the MAC.
* @param[in]       u32VolatileCounter   Implied value of the Volatile Counter.
* @param[out]      pVrfStatus           Verification status.
*
* @return          Result of the operation.
*                   Std_ReturnType
* @api
*
* @pre            Driver must be initialized.
*
*/
FUNC(Std_ReturnType, CRYPTO_CODE) Crypto_Cse_MacVerifySecCounter
(
    VAR    (uint32, AUTOMATIC                   ) u32KeyId,
    VAR    (uint8,  AUTOMATIC                   ) u8CounterId,
    VAR    (uint8,  AUTOMATIC                   ) u8RlvPrtOffset,
    VAR    (uint32, AUTOMATIC                   ) u32MessageLength,
    P2CONST(uint8,  AUTOMATIC, CRYPTO_APPL_CONST) pMessage,
    P2VAR  (uint8,  AUTOMATIC, CRYPTO_APPL_DATA ) pMac,
    VAR    (uint8,  AUTOMATIC                   ) u32MacLength,
    VAR    (uint32, AUTOMATIC                   ) u32VolatileCounter,
    P2VAR  (uint32, AUTOMATIC, CRYPTO_APPL_DATA ) pVrfStatus
);

/**
* @brief         Send Verify Signature command.
* @details       The VERIFY_SIGNATURE command verifies a signed message using the RSA SSA- PKCS1-V1_5 standard.
*
* @param[in]     u8AuthCertifId    Authorization certificate ID.
* @param[in]     pMessAddr         Message Address.
* @param[in]     u32MessLen        Message Length (bytes).
* @param[in]     pSignatureAddr    Signature Address.
* @param[out]    pVerifStatus      Verification Status.
* @param[in]     bClearBootMac     Clear BOOT_MAC( if set )
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @pre         Driver must be initialized.
*
*/
FUNC (Std_ReturnType, CRYPTO_CODE) Crypto_Cse_VerifySignature
(
    VAR    (uint8,  AUTOMATIC                   ) u8AuthCertifId,
    P2CONST(uint8,  AUTOMATIC, CRYPTO_APPL_CONST) pMessAddr,
    VAR    (uint32, AUTOMATIC                   ) u32MessLen,
    P2CONST(uint8,  AUTOMATIC, CRYPTO_APPL_CONST) pSignatureAddr,
    P2VAR  (uint32, AUTOMATIC, CRYPTO_APPL_DATA ) pVerifStatus,
    VAR    (boolean,AUTOMATIC                   ) bClearBootMac
);

/**
* @brief         Backup Restore Cleanup Status.
* @details       The BRCS command provides 4 separate functions based on the mode input value.
*
* @param[in]     eBrcsMode             Mode.
* @param[inout]  pImageAddress         Backup image address.
* @param[out]    pKeySlotStatus        Slot usage status.
* @param[out]    pCounterSlotStatus    Status of counter slots.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @pre         Driver must be initialized.
*
*/
FUNC (Std_ReturnType, CRYPTO_CODE) Crypto_Cse_BRCS
(
    VAR  (Crypto_BrcsModeType, AUTOMATIC                  ) eBrcsMode,
    P2VAR(uint32,              AUTOMATIC, CRYPTO_APPL_DATA) pImageAddress,
    P2VAR(uint32,              AUTOMATIC, CRYPTO_APPL_DATA) pKeySlotStatus,
    P2VAR(uint32,              AUTOMATIC, CRYPTO_APPL_DATA) pCounterSlotStatus
);

/**
* @brief         Update firmware.
* @details       The UPDATE_FIRMWARE command provides a secure method to update CSE firmware in the field.
*
* @param[in]     pFirmwareAddr          The firmware address input must point to the new encrypted firmware image.
* @param[in]     pSignatureAddr         Signature address.
* @param[in]     u8AuthCertifId         CSE_P3 selects the public key certificate to use for signature verification of the firmware update image.
* @param[out]    pNewFirmVersion        New firmware version is returned in CSE_P4.
* @param[out]    pCurrentFirmVersion    Old firmware version is returned in CSE_P5.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @pre         Driver must be initialized.
*
*/
FUNC (Std_ReturnType, CRYPTO_CODE) Crypto_Cse_UpdateFirmware
(
    P2VAR(uint32, AUTOMATIC, CRYPTO_APPL_DATA) pFirmwareAddr,
    P2VAR(uint32, AUTOMATIC, CRYPTO_APPL_DATA) pSignatureAddr,
    VAR  (uint8,  AUTOMATIC                  ) u8AuthCertifId,
    P2VAR(uint32, AUTOMATIC, CRYPTO_APPL_DATA) pNewFirmVersion,
    P2VAR(uint32, AUTOMATIC, CRYPTO_APPL_DATA) pCurrentFirmVersion
);

/**
* @brief           Send secure session authorization command.
* @details         Erases all user keys and sets the CSE_SR[IDB] flag which enables internal debugging if the 128-bit authorization input value is valid
*                  and no memory slots are write protected.
*
* @param[in]       pAuthorization - Message start address where the output challenge was stored.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @api
*
* @pre            Driver must be initialized.
*
*/
FUNC(Std_ReturnType, CRYPTO_CODE)Crypto_Cse_SecureSessionAuth
(
    P2VAR(uint8, AUTOMATIC, CRYPTO_APPL_DATA) pAuthorization
);

/**
* @brief           Select Key Bank.
* @details         Selects which set of AES application keys are visible for commands. This field is ignored
*                  for RSA commands when selecting a certificate ID, including SS_CHAL, INSTALL_CERTIFICATE,
*                  UPDATE_FIRMWARE, and VERIFY_SIGNATURE.
*
* @param[in]       pKeyId    Pointer to key id on which will be chosen the slot and key bank
*
* @return          Result of the operation.
*                   None
*
* @api
*
* @pre          Driver must be initialized.
*
*/
FUNC(void, CRYPTO_CODE) Crypto_Cse_KeyBankSelect
(
    P2VAR(Cse_KeyHandleType, AUTOMATIC, CRYPTO_APPL_DATA) pKeyId
);

/**
* @brief        Initializes the seed and derives a key for the PRNG.
* @details      This function initializes the seed and derives a key for the PRNG. 
*               The function must be called before CMD_RND after every power cycle or reset.
*
* @param[in]     None
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @pre         
*
*/
FUNC (Std_ReturnType, CRYPTO_CODE) Crypto_Cse_InitRng
(
    void
);

/**
* @brief        Extends the seed of the PRNG by compressing the former seed value and the supplied entropy into a new seed.
* @details      This function extends the seed of the PRNG by compressing the former seed value and the supplied 
*               entropy into a new seed which will be used to generate the following random numbers.
*
* @param[in]     pEntropy    Pointer to a 128-bit buffer containing the entropy.
*
* @return          Result of the operation.
*                   Std_ReturnType
*
* @pre         Driver must be initialized.
*
*/
FUNC (Std_ReturnType, CRYPTO_CODE) Crypto_Cse_ExtendPrngSeed
(
    P2CONST(uint8, AUTOMATIC, CRYPTO_APPL_CONST) pEntropy
);

#if (STD_ON == CRYPTO_ASYNC_PROCESS_INTERRUPT)
/**
* @brief           Interrupt handler for CSE IP.
* @details         This function shall manage the interrupt for CSE.
*
* @param[in]       None.
*
* @return          Result of the operation.
*                   Void.
*
* @api
*
* @pre            Driver must be initialized.
*
* @note           Internal driver function.
*/
FUNC(void, CRYPTO_CODE) Crypto_Cse_InterruptHandler(void);

#endif /* (STD_ON == CRYPTO_ASYNC_PROCESS_INTERRUPT) */

#define CRYPTO_STOP_SEC_CODE

/**
* @brief Include Memory mapping specification
* @violates @ref Crypto_Cse_H_REF_1 MISRA 2004 Required Rule 19.15 precautions to prevent the contents
*                of a header file being included twice
*/
#include "Crypto_MemMap.h"

#ifdef __cplusplus
}
#endif

#endif /* CRYPTO_CSE_H */

/** @} */
