/**
*   @file       Crypto_Util.h

*   @version    1.0.0
*   @brief      AUTOSAR Crypto - Crypto interface
*   @details    API header for Crypto driver util functions.
*
*   @addtogroup  Crypto
*   @{
*/
/*==================================================================================================
*   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_UTIL_H
#define CRYPTO_UTIL_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 "Mcal.h"
#include "Csm_Types.h"
#include "Crypto_Cfg.h"
#include "Crypto_Private.h"
#include "SchM_Crypto.h"

/*==================================================================================================
*                              SOURCE FILE VERSION INFORMATION
==================================================================================================*/
#define CRYPTO_UTIL_VENDOR_ID                       43
#define CRYPTO_UTIL_MODULE_ID                       114
#define CRYPTO_UTIL_AR_RELEASE_MAJOR_VERSION        4
#define CRYPTO_UTIL_AR_RELEASE_MINOR_VERSION        3
#define CRYPTO_UTIL_AR_RELEASE_REVISION_VERSION     1
#define CRYPTO_UTIL_SW_MAJOR_VERSION                1
#define CRYPTO_UTIL_SW_MINOR_VERSION                0
#define CRYPTO_UTIL_SW_PATCH_VERSION                0

/*==================================================================================================
*                                    FILE VERSION CHECKS
==================================================================================================*/
/* Check if Crypto_Util header file and Crypto configuration header file are of the same vendor */
#if (CRYPTO_UTIL_VENDOR_ID != CRYPTO_VENDOR_ID_CFG)
    #error "Crypto_Util.h and Crypto_Cfg.h have different vendor ids"
#endif

/* Check if Crypto_Util header file and Crypto configuration header file are of the same Autosar version */
#if ((CRYPTO_UTIL_AR_RELEASE_MAJOR_VERSION    != CRYPTO_AR_RELEASE_MAJOR_VERSION_CFG) || \
     (CRYPTO_UTIL_AR_RELEASE_MINOR_VERSION    != CRYPTO_AR_RELEASE_MINOR_VERSION_CFG) || \
     (CRYPTO_UTIL_AR_RELEASE_REVISION_VERSION != CRYPTO_AR_RELEASE_REVISION_VERSION_CFG) \
    )
    #error "AutoSar Version Numbers of Crypto_Util.h and Crypto_Cfg.h are different"
#endif

/* Check if Crypto_Util header file and Crypto configuration header file are of the same Software version */
#if ((CRYPTO_UTIL_SW_MAJOR_VERSION != CRYPTO_SW_MAJOR_VERSION_CFG) || \
     (CRYPTO_UTIL_SW_MINOR_VERSION != CRYPTO_SW_MINOR_VERSION_CFG) || \
     (CRYPTO_UTIL_SW_PATCH_VERSION != CRYPTO_SW_PATCH_VERSION_CFG) \
    )
    #error "Software Version Numbers of Crypto_Util.h and Crypto_Cfg.h are different"
#endif

/* Check if Crypto_Util header file and Crypto private header file are of the same vendor */
#if (CRYPTO_UTIL_VENDOR_ID != CRYPTO_VENDOR_ID_PRIVATE)
    #error "Crypto_Util.h and Crypto_Private.h have different vendor ids"
#endif

/* Check if Crypto_Util header file and Crypto private header file are of the same Autosar version */
#if ((CRYPTO_UTIL_AR_RELEASE_MAJOR_VERSION    != CRYPTO_AR_RELEASE_MAJOR_VERSION_PRIVATE) || \
     (CRYPTO_UTIL_AR_RELEASE_MINOR_VERSION    != CRYPTO_AR_RELEASE_MINOR_VERSION_PRIVATE) || \
     (CRYPTO_UTIL_AR_RELEASE_REVISION_VERSION != CRYPTO_AR_RELEASE_REVISION_VERSION_PRIVATE) \
    )
    #error "AutoSar Version Numbers of Crypto_Util.h and Crypto_Private.h are different"
#endif

/* Check if Crypto_Util header file and Crypto private header file are of the same Software version */
#if ((CRYPTO_UTIL_SW_MAJOR_VERSION != CRYPTO_SW_MAJOR_VERSION_PRIVATE) || \
     (CRYPTO_UTIL_SW_MINOR_VERSION != CRYPTO_SW_MINOR_VERSION_PRIVATE) || \
     (CRYPTO_UTIL_SW_PATCH_VERSION != CRYPTO_SW_PATCH_VERSION_PRIVATE)    \
    )
    #error "Software Version Numbers of Crypto_Util.h and Crypto_Private.h are different"
#endif

#ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
    /* Check if Crypto_Util header file and Mcal header file are of the same AutoSar version */
    #if ((CRYPTO_UTIL_AR_RELEASE_MAJOR_VERSION != MCAL_AR_RELEASE_MAJOR_VERSION) || \
         (CRYPTO_UTIL_AR_RELEASE_MINOR_VERSION != MCAL_AR_RELEASE_MINOR_VERSION)    \
        )
        #error "AutoSar Version Numbers of Crypto_Util.h and Mcal.h are different"
    #endif 
    /* Check if Crypto_Util header file and Csm_Types header file are of the same Autosar version */
    #if ((CRYPTO_UTIL_AR_RELEASE_MAJOR_VERSION != CSM_AR_RELEASE_MAJOR_VERSION_TYPES) || \
         (CRYPTO_UTIL_AR_RELEASE_MINOR_VERSION != CSM_AR_RELEASE_MINOR_VERSION_TYPES)    \
        )
        #error "AutoSar Version Numbers of Crypto_Util.h and Csm_Types.h are different"
    #endif
#endif

/*==================================================================================================
*                                           CONSTANTS
==================================================================================================*/

/*==================================================================================================
*                                       DEFINES AND MACROS
==================================================================================================*/
/* Value used to mark the end of a job queue */
#define CRYPTO_JOB_QUEUE_NONE_U32               ((uint32)0xFFFFFFFFU)

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

/*==================================================================================================
*                                 STRUCTURES AND OTHER TYPEDEFS
==================================================================================================*/
/* Return structure used by Crypto_Util_VerifyKeyElementId() function */
typedef struct
{
    VAR (Std_ReturnType, AUTOMATIC) RetVal;
    VAR (uint32,         AUTOMATIC) u32Counter;
} Crypto_VerifyKeyElementIdType;

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

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

/**
* @brief        Memset function   
* @details      Memset function   
*
* @param[in]    pSource - Each byte will be set with the value of  u8Value
* @param[in]    u8Value - Value that will be written into pSource
* @param[inout] none   
* @param[out]   none   
* @returns      none   
*                  
* @api
*
* @pre            
*                   
*/
FUNC(void, CRYPTO_CODE) Crypto_Util_Memset
(
    P2VAR (void,   AUTOMATIC, CRYPTO_APP_DATA) pSource, 
    VAR   (uint8,  AUTOMATIC                 ) u8Value, 
    VAR   (uint32, AUTOMATIC                 ) u32Size
);

/**
* @brief           Memcopy function
* @details         Memcopy function
*
* @param[in]       pSource - address of the source
* @param[in]       pDest - address of the destination
* @param[in]       u32Size - size to be copied
*
* @returns         void
*                  
* @api
*
* @pre
*
*/
FUNC(void, CRYPTO_CODE) Crypto_Util_Memcpy 
(
    P2CONST(uint8,  AUTOMATIC, CRYPTO_APPL_CONST) pSource,
    P2VAR  (uint8,  AUTOMATIC, CRYPTO_APP_DATA  ) pDest,
    VAR    (uint32, AUTOMATIC                   ) u32Size
);

/**
* @brief           Memcopy function for a specific job
* @details         Memcopy function for a specific job
*
* @param[in]       pSource - address of the source
* @param[in]       pDest - address of the destination
* @param[inout]    none
* @param[out]      none
* @returns         void
*                  
* @api
*
* @pre            
*                   
*/
FUNC(void, CRYPTO_CODE) Crypto_Util_JobTypeMemcpy
(
    P2CONST(Crypto_JobType, AUTOMATIC, CRYPTO_APP_DATA) pSource, 
    P2VAR  (Crypto_JobType, AUTOMATIC, CRYPTO_APP_DATA) pDest
);

/**
* @brief           Crypto_Util_SwapArrayBytes Swap function for an array input
* @details         Crypto_Util_SwapArrayBytes Swap function will change endianness of input
*
* @param[in]       pSource - address of the source
* @param[in]       u32Len  - size of input in bytes
* @param[inout]    none
* @param[out]      none
* @returns         void
*                   E_OK: request successful
*                   E_NOT_OK: request unsuccessful              
* @api
*
* @pre            
*                   
*/
FUNC(Std_ReturnType, CRYPTO_CODE) Crypto_Util_SwapArrayBytes
(
    P2VAR  (uint8,  AUTOMATIC, CRYPTO_APP_DATA) pSource,
    VAR    (uint32, AUTOMATIC                 ) u32Len
);

/**
* @brief           Initialize the CDO job queues
* @details         Each CDO has 2 queues, one with free elements, the second containing jobs sorted by priority. 
*                  For each of the CDO queues, only the first element (head) is stored, as each queue element has a reference to the next one.
*                  This function initializes the queues heads and chains the elements in the free jobs queues
*
* @param[in]       void
* @returns         void
*
* @api
*
* @pre            
*                   
*/
FUNC(void, CRYPTO_CODE) Crypto_Util_InitJobQueues(void);

/**
* @brief           Adds a job in the jobs queue
* @details         Adds a job in the jobs queue, if at least one element is found in the queue of free jobs. The job is inserted in the queue
*                  based on its priority
*
* @param[in]       u32ObjectIdIdx: identifier of the Crypto Driver Object to be used for queuing the job
* @param[in]       pJob:        pointer to the job to be queued
* @returns         boolean
*                   TRUE  - job has been queued
*                   FALSE - job could not be queued
*
* @api
*
* @pre            
*                   
*/
FUNC(boolean, CRYPTO_CODE) Crypto_Util_QueueJob
(
    VAR    (uint32,         AUTOMATIC                  ) u32ObjectIdIdx,
    P2CONST(Crypto_JobType, AUTOMATIC, CRYPTO_APPL_DATA) pJob
);

/**
* @brief           Dequeues the first job in the queue
* @details         Removes the first queued job (head) and adds it in the queue of free jobs
*
* @param[in]       u32ObjectIdIdx: identifier of the Crypto Driver Object to be used for dequeuing the job
* @returns         void
*
* @api
*
* @pre            Assumes that at least one element exists in the queued jobs
*                   
*/
FUNC(void, CRYPTO_CODE) Crypto_Util_DequeueHeadJob
(
    VAR  (uint32, AUTOMATIC) u32ObjectIdIdx
);

/**
* @brief           Dequeues from the list of queued jobs the first found job with the given jobId
* @details         Searches the queued jobs for a job with a given jobId and if found, removes it
*
* @param[in]       u32ObjectIdIdx: identifier of the Crypto Driver Object to be used for dequeuing the job
* @param[in]       pJobInfo:       pointer to jobInfo structure containing the jobId to search for
* @returns         Std_ReturnType
*
* @api
*
* @pre            
*                   
*/
FUNC(Std_ReturnType, CRYPTO_CODE) Crypto_Util_DequeueJob
(
    VAR  (uint32,             AUTOMATIC                  ) u32ObjectIdIdx,
    P2VAR(Crypto_JobInfoType, AUTOMATIC, CRYPTO_APPL_DATA) pJobInfo
);

#if (STD_ON == CRYPTO_KEYS_EXIST)
FUNC(uint32, CRYPTO_CODE) Crypto_Util_GetKeyIdIdx
(
    VAR (uint32, AUTOMATIC) u32KeyId
);

FUNC(Crypto_VerifyKeyElementIdType, CRYPTO_CODE) Crypto_Util_VerifyKeyElementId
(
    VAR(uint32, AUTOMATIC) u32KeyIndex,
    VAR(uint32, AUTOMATIC) u32keyElementId
);
#endif /* (STD_ON == CRYPTO_KEYS_EXIST) */


#define CRYPTO_STOP_SEC_CODE
#include "Crypto_MemMap.h"

#ifdef __cplusplus
}
#endif

#endif /* CRYPTO_UTIL_H */

/** @} */
