/** * @file Crypto_TC_FCT_0070.c * @version M4_SRC_SW_VERSION_MAJOR.M4_SRC_SW_VERSION_MINOR.M4_SRC_SW_VERSION_PATCH * * @brief AUTOSAR M4_SRC_MODULE_NAME test source file * @details Test source file * * @addtogroup CRYPTO_TESTS * @{ */ /*================================================================================================== * Project : M4_SRC_PROJECT_NAME * Platform : M4_SRC_MCU_FAMILY * Peripheral : M4_SRC_USED_PERIPHERAL * Dependencies : M4_SRC_AR_MODULE_DEPENDENCY * * Autosar Version : M4_SRC_AR_SPEC_VERSION_MAJOR.M4_SRC_AR_SPEC_VERSION_MINOR.M4_SRC_AR_SPEC_VERSION_PATCH * Autosar Revision : M4_SRC_AR_RELEASE_REVISION * Autosar Conf.Variant : * SW Version : M4_SRC_SW_VERSION_MAJOR.M4_SRC_SW_VERSION_MINOR.M4_SRC_SW_VERSION_PATCH * Build Version : M4_SRC_BUILD_ID * * (c) Copyright M4_SRC_YEAR_ID M4_SRC_COPYRIGHTED_TO * All Rights Reserved. ==================================================================================================*/ #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_TC_FCT_0070.h" #include "Crypto.h" /*================================================================================================== * LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS) ==================================================================================================*/ /*================================================================================================== * LOCAL MACROS ==================================================================================================*/ /* Take the generated values of the SHE keys from the configuration file */ #define SHE_MASTER_ECU_KEY_ID (CryptoConf_CryptoKey_CryptoKey_SHE_MASTER_ECU_KEY) #define SHE_BOOT_MAC_KEY_ID (CryptoConf_CryptoKey_CryptoKey_SHE_BOOT_MAC_KEY) #define SHE_KEY1_ID (CryptoConf_CryptoKey_CryptoKey_SHE_Key1) #define SHE_KEY2_ID (CryptoConf_CryptoKey_CryptoKey_SHE_Key2) #define SHE_KEY3_ID (CryptoConf_CryptoKey_CryptoKey_SHE_Key3) #define SHE_KEY4_ID (CryptoConf_CryptoKey_CryptoKey_SHE_Key4) #define SHE_KEY5_ID (CryptoConf_CryptoKey_CryptoKey_SHE_Key5) #define SHE_KEY6_ID (CryptoConf_CryptoKey_CryptoKey_SHE_Key6) #define SHE_KEY7_ID (CryptoConf_CryptoKey_CryptoKey_SHE_Key7) #define SHE_KEY8_ID (CryptoConf_CryptoKey_CryptoKey_SHE_Key8) #define SHE_KEY9_ID (CryptoConf_CryptoKey_CryptoKey_SHE_Key9) #define SHE_KEY10_ID (CryptoConf_CryptoKey_CryptoKey_SHE_Key10) #define SHE_RAM_KEY_ID (CryptoConf_CryptoKey_CryptoKey_SHE_RamKey) #define SHE_RAM_KEY_ENCRYPT_ID (CryptoConf_CryptoKey_CryptoKey_SHE_Ramkey_Encrypt) /* Size in bytes of the SHE AES128 encrypt key */ #define SHE_KEY_SIZE ((uint32)16U) /* Size in bytes of the SHE MAC */ #define SHE_MAC_SIZE ((uint32)16U) /* Id of the key material element for the SHE keys */ #define SHE_KEY_MATERIAL_ELEMENT_ID ((uint32)1U) /* Defines related to sizes of M1, M2, M3, M4 and M5 SHE specific values */ #define SHE_M1_SIZE ((uint8)16U) #define SHE_M2_SIZE ((uint8)32U) #define SHE_M3_SIZE ((uint8)16U) #define SHE_M4_SIZE ((uint8)32U) #define SHE_M5_SIZE ((uint8)16U) #define SHE_M1M2M3_SIZE ((uint32)(SHE_M1_SIZE + SHE_M2_SIZE + SHE_M3_SIZE)) #define START_INDEX_M4 (uint32)(SHE_M1_SIZE + SHE_M2_SIZE + SHE_M3_SIZE) #define START_INDEX_M5 (uint32)(SHE_M1_SIZE + SHE_M2_SIZE + SHE_M3_SIZE + SHE_M4_SIZE) /*================================================================================================== * LOCAL CONSTANTS ==================================================================================================*/ static const Crypto_JobInfoType Job_CMAC_JobInfo = { 0U, /* jobId - The identifier of the job */ 0U /* jobPriority - Specifies the importance of the job (the higher, the more important) */ }; static const Crypto_PrimitiveInfoType Job_CMAC_PrimitiveInfo = { SHE_MAC_SIZE, /* resultLength - Contains the result length in bytes. */ CRYPTO_MACGENERATE, /* service - Contains the enum of the used service, e.g. Encrypt */ { CRYPTO_ALGOFAM_AES, /* family - The family of the algorithm */ CRYPTO_ALGOFAM_NOT_SET, /* secondaryFamily - The secondary family of the algorithm */ SHE_KEY_SIZE, /* keyLength - The key length in bits to be used with that algorithm */ CRYPTO_ALGOMODE_CMAC /* mode - The operation mode to be used with that algorithm */ } }; static const Crypto_JobInfoType Job_Enc_JobInfo = { 0U, /* jobId - The identifier of the job */ 0U /* jobPriority - Specifies the importance of the job (the higher, the more important) */ }; static const Crypto_PrimitiveInfoType Job_Enc_PrimitiveInfo = { 32, /* resultLength - Contains the result length in bytes. */ CRYPTO_ENCRYPT, /* service - Contains the enum of the used service, e.g. Encrypt */ { CRYPTO_ALGOFAM_AES, /* family - The family of the algorithm */ CRYPTO_ALGOFAM_NOT_SET, /* secondaryFamily - The secondary family of the algorithm */ SHE_KEY_SIZE, /* keyLength - The key length in bits to be used with that algorithm */ CRYPTO_ALGOMODE_CBC /* mode - The operation mode to be used with that algorithm */ } }; /*================================================================================================== * LOCAL VARIABLES ==================================================================================================*/ #define CRYPTO_START_SEC_VAR_CLEARED_8_NO_CACHEABLE #include "Crypto_MemMap.h" /* Array containing the concatenation of master key and debug key SHE constant */ static uint8 aKeyPlain[32]; static VAR(uint8, AUTOMATIC) aM2Plain[32]; static VAR(uint8, AUTOMATIC) aK1[16]; static VAR(uint8, AUTOMATIC) aK2[16]; static VAR(uint8, AUTOMATIC) aM1[16]; static VAR(uint8, AUTOMATIC) aM2[32]; static VAR(uint8, AUTOMATIC) aM3[16]; static VAR(uint8, AUTOMATIC) aM1M2[48]; static uint8 KeyUpdatePram[64]; static uint8 M5Mac[16]; static uint8 tempM4[32]; static uint8 tempM5[16]; static uint8 M4Cipher[32]; static uint8 OutResult[112]; #define CRYPTO_STOP_SEC_VAR_CLEARED_8_NO_CACHEABLE #include "Crypto_MemMap.h" #define CRYPTO_START_SEC_VAR_CLEARED_32_NO_CACHEABLE #include "Crypto_MemMap.h" static uint32 u32ResultLen; #define CRYPTO_STOP_SEC_VAR_CLEARED_32_NO_CACHEABLE #include "Crypto_MemMap.h" #define CRYPTO_START_SEC_VAR_INIT_32_NO_CACHEABLE #include "Crypto_MemMap.h" static uint32 u32M3Len = SHE_MAC_SIZE; static uint32 u32aM2Len = 32U; static uint32 u32aM4Len = 32U; static uint32 u32aM5Len = 16U; static uint32 KeyLength = 64; static uint32 KeyLengthExp = 112U; #define CRYPTO_STOP_SEC_VAR_INIT_32_NO_CACHEABLE #include "Crypto_MemMap.h" #define CRYPTO_START_SEC_VAR_INIT_8_NO_CACHEABLE #include "Crypto_MemMap.h" static VAR(uint8, AUTOMATIC) aK1Plain[32] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x53, 0x48, 0x45, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0 }; static VAR(uint8, AUTOMATIC) aK2Plain[32] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x53, 0x48, 0x45, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb0, }; static uint8 aMasterEcuKey[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; static uint8 aNewKey[16] = { 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 }; static uint8 au8EmptyKeyIV[SHE_KEY_SIZE] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; #ifdef CSEC static uint8 au8EmptyKey[SHE_KEY_SIZE] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; #else /* Array to be passed to Crypto driver during various operations as output buffer */ static uint8 au8EmptyKey[SHE_KEY_SIZE] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; #endif static CONST(uint8, AUTOMATIC) aEmptyUID[15] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; #define CRYPTO_STOP_SEC_VAR_INIT_8_NO_CACHEABLE #include "Crypto_MemMap.h" static Crypto_JobPrimitiveInfoType Job_CMAC_JobPrimitiveInfo = { 0U, /* callbackId - Identifier of the callback function, to be called, if the configured service finished. */ &Job_CMAC_PrimitiveInfo, /* primitiveInfo - Pointer to a structure containing further configuration of the crypto primitives */ SHE_RAM_KEY_ID, /* cryIfKeyId - Identifier of the CryIf key. */ CRYPTO_PROCESSING_SYNC, /* processingType - Determines the synchronous or asynchronous behavior. */ (boolean)FALSE /* callbackUpdateNotification - Indicates, whether the callback function shall be called, if the UPDATE operation has finished. */ }; static Crypto_JobPrimitiveInfoType Job_Enc_JobPrimitiveInfo = { 0U, /* callbackId - Identifier of the callback function, to be called, if the configured service finished. */ &Job_Enc_PrimitiveInfo, /* primitiveInfo - Pointer to a structure containing further configuration of the crypto primitives */ SHE_RAM_KEY_ID, /* cryIfKeyId - Identifier of the CryIf key. */ CRYPTO_PROCESSING_SYNC, /* processingType - Determines the synchronous or asynchronous behavior. */ (boolean)FALSE /* callbackUpdateNotification - Indicates, whether the callback function shall be called, if the UPDATE operation has finished. */ }; static Crypto_JobType Job_GenM3 = { 1U, /* jobId - Identifier for the job structure */ CRYPTO_JOBSTATE_IDLE, /* jobState - Determines the current job state */ { aM1M2, /* inputPtr - Pointer to the input data. */ 48, /* inputLength - Contains the input length in bytes. */ NULL_PTR, /* secondaryInputPtr - Pointer to the secondary input data (for MacVerify, SignatureVerify). */ 0U, /* secondaryInputLength - Contains the secondary input length in bytes. */ NULL_PTR, /* tertiaryInputPtr - Pointer to the tertiary input data (for MacVerify, SignatureVerify). */ 0U, /* tertiaryInputLength - Contains the tertiary input length in bytes. */ aM3, /* outputPtr - Pointer to the output data. */ &u32M3Len, /* outputLengthPtr - Holds a pointer to a memory location containing the output length in bytes. */ NULL_PTR, /* secondaryOutputPtr - Pointer to the secondary output data. */ NULL_PTR, /* secondaryOutputLengthPtr - Holds a pointer to a memory location containing the secondary output length in bytes. */ 0U, /* input64 - Versatile input parameter */ NULL_PTR, /* verifyPtr - Output pointer to a memory location holding a Crypto_VerifyResultType */ NULL_PTR, /* output64Ptr - Output pointer to a memory location holding an uint64. */ CRYPTO_OPERATIONMODE_SINGLECALL, /* mode - Indicator of the mode(s)/operation(s) to be performed */ 0U, /* cryIfKeyId - Holds the CryIf key id for key operation services. */ 0U /* targetCryIfKeyId - Holds the target CryIf key id for key operation services. */ }, &Job_CMAC_JobPrimitiveInfo, /* jobPrimitiveInfo - Pointer to a structure containing further information, which depends on the job and the crypto primitive */ &Job_CMAC_JobInfo, /* jobInfo - Pointer to a structure containing further information, which depends on the job and the crypto primitive */ NULL_PTR /* jobRedirectionInfoRef - Pointer to a structure containing further information on the usage of keys as input and output for jobs. */ }; /* Structure of the job to be passed to Crypto driver, requesting Encrypt in single call and streaming mode with one job */ static Crypto_JobType Job_Encrypt = { 1U, /* jobId - Identifier for the job structure */ CRYPTO_JOBSTATE_IDLE, /* jobState - Determines the current job state */ { aM2Plain, /* inputPtr - Pointer to the input data. */ 32U, /* inputLength - Contains the input length in bytes. */ NULL_PTR, /* secondaryInputPtr - Pointer to the secondary input data (for MacVerify, SignatureVerify). */ 0U, /* secondaryInputLength - Contains the secondary input length in bytes. */ NULL_PTR, /* tertiaryInputPtr - Pointer to the tertiary input data (for MacVerify, SignatureVerify). */ 0U, /* tertiaryInputLength - Contains the tertiary input length in bytes. */ aM2, /* outputPtr - Pointer to the output data. */ &u32aM2Len, /* outputLengthPtr - Holds a pointer to a memory location containing the output length in bytes. */ NULL_PTR, /* secondaryOutputPtr - Pointer to the secondary output data. */ NULL_PTR, /* secondaryOutputLengthPtr - Holds a pointer to a memory location containing the secondary output length in bytes. */ 0U, /* input64 - Versatile input parameter */ NULL_PTR, /* verifyPtr - Output pointer to a memory location holding a Crypto_VerifyResultType */ NULL_PTR, /* output64Ptr - Output pointer to a memory location holding an uint64. */ CRYPTO_OPERATIONMODE_SINGLECALL, /* mode - Indicator of the mode(s)/operation(s) to be performed */ 0U, /* cryIfKeyId - Holds the CryIf key id for key operation services. */ 0U /* targetCryIfKeyId - Holds the target CryIf key id for key operation services. */ }, &Job_Enc_JobPrimitiveInfo, /* jobPrimitiveInfo - Pointer to a structure containing further information, which depends on the job and the crypto primitive */ &Job_Enc_JobInfo, /* jobInfo - Pointer to a structure containing further information, which depends on the job and the crypto primitive */ NULL_PTR /* jobRedirectionInfoRef - Pointer to a structure containing further information on the usage of keys as input and output for jobs. */ }; /*================================================================================================== * GLOBAL CONSTANTS ==================================================================================================*/ /*================================================================================================== * GLOBAL VARIABLES ==================================================================================================*/ /*================================================================================================== * LOCAL FUNCTION PROTOTYPES ==================================================================================================*/ void Crypto_CalculatedM1M2M3(CONST(uint8, AUTOMATIC) authKey[16], uint8 authKeyId, uint8 keyId, CONST(uint8, AUTOMATIC) key[16], uint32 counter, CONST(uint8, AUTOMATIC) uid[15], uint8 flags, VAR(uint8, AUTOMATIC) KeyPram[64]); /*================================================================================================== * LOCAL FUNCTIONS ==================================================================================================*/ void Crypto_CalculatedM1M2M3(CONST(uint8, AUTOMATIC) authKey[16], uint8 authKeyId, uint8 keyId, CONST(uint8, AUTOMATIC) key[16], uint32 counter, CONST(uint8, AUTOMATIC) uid[15], uint8 flags, VAR(uint8, AUTOMATIC) KeyPram[64]) { uint8 u8Idx; Std_ReturnType RetVal; if ((keyId > 0x43)) { aK1Plain[17] = 0xB1U; aK2Plain[17] = 0xB2U; } else if ((keyId > 0x33)&&(keyId < 0x3E)) { aK1Plain[17] = 0xA1U; aK2Plain[17] = 0xA2U; } else if ((keyId > 0x23)&&(keyId < 0x2E)) { aK1Plain[17] = 0x91U; aK2Plain[17] = 0x92U; } else if ((keyId > 0x13)&&(keyId < 0x1E)) { /* Constants defined by the SHE spec for bank 1 */ aK1Plain[17] = 0x81U; aK2Plain[17] = 0x82U; } else { /* Constants defined by the SHE spec for bank 0 */ aK1Plain[17] = 0x01U; aK2Plain[17] = 0x02U; } for (u8Idx = 0; u8Idx < 16U; u8Idx++) { aK1Plain[u8Idx] = authKey[u8Idx]; aK2Plain[u8Idx] = authKey[u8Idx]; } /* Generate KDF :KDF (K, constant) = AES-MP(K | constant) */ u32ResultLen = SHE_KEY_SIZE; RetVal = Crypto_Exts_MPCompression(aK1Plain, 32, aK1, &u32ResultLen); EU_ASSERT((Std_ReturnType)E_OK == RetVal); RetVal = Crypto_Exts_MPCompression(aK2Plain, 32, aK2, &u32ResultLen); EU_ASSERT((Std_ReturnType)E_OK == RetVal); /* Prepare M1 */ for (u8Idx = 0; u8Idx < 15U; u8Idx++) { aM1[u8Idx] = uid[u8Idx]; } aM1[15] = (authKeyId & 0x0F) | ((keyId & 0x0F) << 4); /* Generate M2 */ for (u8Idx = 0; u8Idx < 16; u8Idx++) { aM2Plain[u8Idx + 16U] = key[u8Idx]; } aM2Plain[0] = ((counter << 0x04) & 0xff000000) >> 0x18; aM2Plain[1] = ((counter << 0x04) & 0x00ff0000) >> 0x10; aM2Plain[2] = ((counter << 0x04) & 0x0000ff00) >> 0x08; aM2Plain[3] = ((counter << 0x04) & 0x000000ff) >> 0x00; aM2Plain[3] |= (flags & 0x1e) >> 1U; aM2Plain[4] |= (flags & 0x01) << 7U; /* Load Ram key with K1 key in plain */ RetVal = Crypto_KeyElementSet(SHE_RAM_KEY_ID, SHE_KEY_MATERIAL_ELEMENT_ID, aK1, SHE_KEY_SIZE); EU_ASSERT((Std_ReturnType)E_OK == RetVal); RetVal = Crypto_KeyElementSet(SHE_RAM_KEY_ID, 5, au8EmptyKeyIV, SHE_KEY_SIZE); EU_ASSERT((Std_ReturnType)E_OK == RetVal); RetVal = Crypto_KeySetValid(SHE_RAM_KEY_ID); EU_ASSERT((Std_ReturnType)E_OK == RetVal); /* Encrypt M2 */ RetVal = Crypto_ProcessJob(0U, &Job_Encrypt); EU_ASSERT((Std_ReturnType)E_OK == RetVal); /* Generate M3 */ for (u8Idx = 0; u8Idx < 16U; u8Idx++) { aM1M2[u8Idx] = aM1[u8Idx]; aM1M2[u8Idx + 16U] = aM2[u8Idx]; aM1M2[u8Idx + 32U] = aM2[u8Idx + 16U]; } /* Load Ram key with K1 key in plain */ RetVal = Crypto_KeyElementSet(SHE_RAM_KEY_ID, SHE_KEY_MATERIAL_ELEMENT_ID, aK2, SHE_KEY_SIZE); EU_ASSERT((Std_ReturnType)E_OK == RetVal); RetVal = Crypto_KeySetValid(SHE_RAM_KEY_ID); EU_ASSERT((Std_ReturnType)E_OK == RetVal); /* Generate M3 */ RetVal = Crypto_ProcessJob(0U, &Job_GenM3); EU_ASSERT((Std_ReturnType)E_OK == RetVal); for(u8Idx = 0; u8Idx < 16; u8Idx++ ) { KeyPram[u8Idx] = aM1[u8Idx]; KeyPram[u8Idx+16] = aM2[u8Idx]; KeyPram[u8Idx+32] = aM2[u8Idx+16]; KeyPram[u8Idx+48] = aM3[u8Idx]; } } /*================================================================================================== * GLOBAL FUNCTIONS ==================================================================================================*/ /*================================================================================================*/ /** * @test_id Crypto_TC_FCT_0070 * @brief Load Master Ecu Key and User key follow all the SHE spec steps * @details Load Master Ecu Key and User key follow all the SHE spec steps * @pre N/A * @post N/A * @test_level ComponentValidation * @test_type Functional * @test_technique BlackBox * @test_procedure Steps: * -# Call Crypto_Init() to initialize the driver * -# Call Crypto_Exts_FormatKeyCatalogs() to format key catalogs * -# Calculate M1,M2,M3 to load SHE_MASTER_ECU_KEY_ID * -# Call Crypto_KeyElementSet to update MASTER_ECU_KEY for the first time * -# Verification Point: function returns E_OK * -# Calculate M1,M2,M3 to load user key use SHE_MASTER_ECU_KEY_ID is authention key * -# Call Crypto_KeyElementSet to update User key * -# Verification Point: function returns E_OK * -# Calculate M1,M2,M3 to load user key use itself is authention key * -# Call Crypto_KeyElementSet to update User key * -# Verification Point: function returns E_OK * -# Call Crypto_KeyElementSet() to import RAM key * -# Verification Point:the function returns E_OK * -# Call Crypto_KeyElementGet() to export the RAM key in protected format * -# Verification Point:the function returns E_OK * -# Call Crypto_KeyElementSet() to imported the encrypted key exported from RAM * -# Verification Point:the function returns E_OK * -# Call Crypto_KeyElementGet() to export M4 cipher proof * -# Verification Point:the function returns E_OK and the result mathces expected data * -# Call Crypto_KeyElementGet() to export M5 MAC proof * -# Verification Point:the function returns E_OK and the result mathces expected data * @pass_criteria All verification points passed. * @requirements SWS_Crypto_00148 * @traceability N/A * @execution_type Automated * @hw_depend N/A * @sw_depend N/A * @boundary_test N/A * @defects N/A * @test_priority Mid * @note N/A * @keywords CryptoLoadMasterEcuKey */ void Crypto_TC_FCT_0070(void) { /* Variable storing the return values of the called APIs */ Std_ReturnType RetVal; uint8 u8Idx,i,y; uint32 u32Counter1 = 0x00000001U; uint32 u32Counter2 = 0x00000002U; uint8 u8Flags = 0x00; /* =============================================================================================================================== */ /* Initialization */ /* =============================================================================================================================== */ /* Initialize driver */ Crypto_Init(NULL_PTR); #if (STD_ON == CRYPTO_SPT_FORMATKEYCATALOGS) /* Format HSE key catalogs (RAM + NVM) */ RetVal = Crypto_Exts_FormatKeyCatalogs(); EU_ASSERT((Std_ReturnType)E_OK == RetVal); #endif /* (STD_ON == CRYPTO_SPT_FORMATKEYCATALOGS) */ /* Calculate M1,M2,M3 to load SHE_MASTER_ECU_KEY_ID */ Crypto_CalculatedM1M2M3(au8EmptyKey,1,1,aMasterEcuKey, u32Counter1, aEmptyUID, u8Flags,KeyUpdatePram); /* Call Crypto_KeyElementSet to update MASTER_ECU_KEY for the first time */ RetVal = Crypto_KeyElementSet(SHE_MASTER_ECU_KEY_ID, SHE_KEY_MATERIAL_ELEMENT_ID, KeyUpdatePram, SHE_M1M2M3_SIZE); /* Verification Point: function returns E_OK */ EU_ASSERT((Std_ReturnType)E_OK == RetVal); for ( u8Idx = 0;u8Idx < 10; u8Idx++) { Crypto_Util_Memset(KeyUpdatePram, 0, ARRAY_SIZE(KeyUpdatePram)); /* Calculate M1,M2,M3 to load user key use SHE_MASTER_ECU_KEY_ID is authention key*/ Crypto_CalculatedM1M2M3(aMasterEcuKey,1 ,u8Idx + 0x04,aMasterEcuKey, u32Counter1, aEmptyUID, u8Flags,KeyUpdatePram); /* Call Crypto_KeyElementSet to update User key */ RetVal = Crypto_KeyElementSet(SHE_KEY1_ID + u8Idx, SHE_KEY_MATERIAL_ELEMENT_ID, KeyUpdatePram, SHE_M1M2M3_SIZE); /* Verification Point: function returns E_OK */ EU_ASSERT((Std_ReturnType)E_OK == RetVal); /* Calculate M1,M2,M3 to load user key use itself is authention key */ Crypto_CalculatedM1M2M3(aMasterEcuKey,u8Idx + 0x04 ,u8Idx + 0x04,aNewKey, u32Counter2, aEmptyUID, u8Flags,KeyUpdatePram); /* Call Crypto_KeyElementSet to update User key */ RetVal = Crypto_KeyElementSet(SHE_KEY1_ID + u8Idx, SHE_KEY_MATERIAL_ELEMENT_ID, KeyUpdatePram, SHE_M1M2M3_SIZE); /* Verification Point: function returns E_OK */ EU_ASSERT((Std_ReturnType)E_OK == RetVal); } RetVal = Crypto_KeyElementSet(SHE_RAM_KEY_ID, SHE_KEY_MATERIAL_ELEMENT_ID, aNewKey, SHE_KEY_SIZE); EU_ASSERT(E_OK == RetVal); /* --------------------------------------Export Plain Key test------------------------------------------*/ /*A RAM_KEY can only be exported if it was written into SHE in plaintext*/ RetVal = Crypto_KeyElementGet(SHE_RAM_KEY_ID, SHE_KEY_MATERIAL_ELEMENT_ID,OutResult,&KeyLengthExp); EU_ASSERT(E_OK == RetVal); /* Copy M4 and M5 in order to check them in the next encrypted load */ for (y = 0; y < SHE_M4_SIZE; y++) { tempM4[y] = OutResult[START_INDEX_M4 + y]; OutResult[START_INDEX_M4 + y] = 0U; } for (i = 0; i < SHE_M5_SIZE; i++) { tempM5[i] = OutResult[START_INDEX_M5 + i]; OutResult[START_INDEX_M5 + i] = 0U; } /* Load encripted key */ RetVal = Crypto_KeyElementSet(SHE_RAM_KEY_ENCRYPT_ID, SHE_KEY_MATERIAL_ELEMENT_ID, OutResult, KeyLength); EU_ASSERT(E_OK == RetVal); /* Get computed M4 cipher proof */ RetVal = Crypto_KeyElementGet(SHE_RAM_KEY_ENCRYPT_ID, CRYPTO_KE_CIPHER_PROOF, M4Cipher,&u32aM4Len); EU_ASSERT(E_OK == RetVal); /* Get the computed M5 MAC proof and compare with gM5 */ RetVal = Crypto_KeyElementGet(SHE_RAM_KEY_ENCRYPT_ID, CRYPTO_KE_MAC_PROOF, M5Mac,&u32aM5Len); EU_ASSERT(E_OK == RetVal); EU_ASSERT(TRUE == Crypto_Util_MemCmp(M4Cipher, tempM4, 32U)); EU_ASSERT(TRUE == Crypto_Util_MemCmp(M5Mac, tempM5, 16U)); } #ifdef __cplusplus } #endif /** @} */