20 #include "csec_hw_access.h"
65 #define CSEC_PAGE_SIZE_IN_BYTES (16U)
69 #define CSEC_DATA_PAGES_AVAILABLE (7U)
72 #define CSEC_DATA_BYTES_AVAILABLE (112U)
75 #define CSEC_BYTES_TO_FROM_PAGES_SHIFT (4U)
77 #define CSEC_BYTES_TO_FROM_BITS_SHIFT (3U)
80 #define CSEC_M1_SIZE_IN_BYTES (16U)
82 #define CSEC_M2_SIZE_IN_BYTES (32U)
84 #define CSEC_M3_SIZE_IN_BYTES (16U)
86 #define CSEC_M4_SIZE_IN_BYTES (32U)
88 #define CSEC_M5_SIZE_IN_BYTES (16U)
102 const uint8_t * inBuff,
110 return (value + (roundTo - 1U)) & ~(roundTo - 1U);
138 g_csecStatePtr = state;
156 g_csecStatePtr = NULL;
170 const uint8_t * plainText,
172 uint8_t * cipherText,
179 uint32_t startTime = 0;
180 uint32_t crtTime = 0;
197 CSEC_WaitCommandCompletion();
200 if (crtTime > (startTime + timeout))
211 return g_csecStatePtr->
errCode;
223 const uint8_t * cipherText,
232 uint32_t startTime = 0;
233 uint32_t crtTime = 0;
249 CSEC_WaitCommandCompletion();
252 if (crtTime > (startTime + timeout))
263 return g_csecStatePtr->
errCode;
275 const uint8_t * plainText,
278 uint8_t * cipherText,
286 uint32_t startTime = 0;
287 uint32_t crtTime = 0;
296 g_csecStatePtr->
iv = iv;
305 CSEC_WaitCommandCompletion();
308 if (crtTime > (startTime + timeout))
319 return g_csecStatePtr->
errCode;
331 const uint8_t * cipherText,
342 uint32_t startTime = 0;
343 uint32_t crtTime = 0;
352 g_csecStatePtr->
iv = iv;
361 CSEC_WaitCommandCompletion();
364 if (crtTime > (startTime + timeout))
375 return g_csecStatePtr->
errCode;
396 uint32_t startTime = 0;
397 uint32_t crtTime = 0;
406 g_csecStatePtr->
msgLen = msgLen;
415 CSEC_WaitCommandCompletion();
418 if (crtTime > (startTime + timeout))
429 return g_csecStatePtr->
errCode;
467 stat = CSEC_ReadErrorBits();
500 uint32_t startTime = 0;
501 uint32_t crtTime = 0;
510 g_csecStatePtr->
msgLen = msgLen;
513 g_csecStatePtr->
mac = mac;
514 g_csecStatePtr->
macLen = macLen;
523 CSEC_WaitCommandCompletion();
526 if (crtTime > (startTime + timeout))
537 return g_csecStatePtr->
errCode;
582 stat = CSEC_ReadErrorBits();
633 CSEC_WaitCommandCompletion();
636 stat = CSEC_ReadErrorBits();
676 CSEC_WaitCommandCompletion();
679 stat = CSEC_ReadErrorBits();
719 CSEC_WaitCommandCompletion();
722 stat = CSEC_ReadErrorBits();
762 CSEC_WaitCommandCompletion();
765 stat = CSEC_ReadErrorBits();
802 CSEC_WaitCommandCompletion();
805 stat = CSEC_ReadErrorBits();
838 CSEC_WaitCommandCompletion();
841 stat = CSEC_ReadErrorBits();
877 CSEC_WaitCommandCompletion();
880 stat = CSEC_ReadErrorBits();
911 CSEC_WaitCommandCompletion();
914 stat = CSEC_ReadErrorBits();
934 uint8_t flavor = (uint8_t)bootFlavor;
950 CSEC_WaitCommandCompletion();
953 stat = CSEC_ReadErrorBits();
993 CSEC_WaitCommandCompletion();
996 stat = CSEC_ReadErrorBits();
1037 CSEC_WaitCommandCompletion();
1040 stat = CSEC_ReadErrorBits();
1079 CSEC_WaitCommandCompletion();
1082 stat = CSEC_ReadErrorBits();
1099 uint8_t * mpCompress,
1106 uint32_t startTime = 0;
1107 uint32_t crtTime = 0;
1111 uint16_t numPagesLeft = msgLen;
1121 while (numPagesLeft > 0U)
1135 CSEC_WaitCommandCompletion();
1138 if (crtTime > (startTime + timeout))
1148 stat = CSEC_ReadErrorBits();
1156 numPagesLeft = (uint16_t)(numPagesLeft - numPages);
1157 index = (uint32_t)(index + numBytes);
1182 const uint8_t * plainText,
1184 uint8_t * cipherText)
1200 CSEC_SetInterrupt(
true);
1214 const uint8_t * cipherText,
1216 uint8_t * plainText)
1232 CSEC_SetInterrupt(
true);
1246 const uint8_t * plainText,
1249 uint8_t * cipherText)
1262 g_csecStatePtr->
iv = iv;
1267 CSEC_SetInterrupt(
true);
1281 const uint8_t * cipherText,
1284 uint8_t * plainText)
1297 g_csecStatePtr->
iv = iv;
1302 CSEC_SetInterrupt(
true);
1316 const uint8_t * msg,
1330 g_csecStatePtr->
msgLen = msgLen;
1335 CSEC_SetInterrupt(
true);
1349 const uint8_t * msg,
1351 const uint8_t * mac,
1366 g_csecStatePtr->
msgLen = msgLen;
1369 g_csecStatePtr->
mac = mac;
1370 g_csecStatePtr->
macLen = macLen;
1375 CSEC_SetInterrupt(
true);
1394 return g_csecStatePtr->
errCode;
1410 const uint8_t * inBuff,
1415 g_csecStatePtr->
cmd = cmd;
1418 g_csecStatePtr->
keyId = keyId;
1420 g_csecStatePtr->
index = 0U;
1463 CSEC_SetInterrupt(
false);
1465 if (g_csecStatePtr->
callback != NULL)
1491 g_csecStatePtr->
partSize = numBytes;
1494 CSEC_WriteCommandHeader(g_csecStatePtr->
cmd, CSEC_FUNC_FORMAT_COPY, g_csecStatePtr->
seq, g_csecStatePtr->
keyId);
1529 g_csecStatePtr->
partSize = numBytes;
1532 CSEC_WriteCommandHeader(g_csecStatePtr->
cmd, CSEC_FUNC_FORMAT_COPY, g_csecStatePtr->
seq, g_csecStatePtr->
keyId);
1544 uint8_t numBytes = (uint8_t)(((g_csecStatePtr->
fullSize - g_csecStatePtr->
index) >
1552 g_csecStatePtr->
partSize = numBytes;
1555 CSEC_WriteCommandHeader(g_csecStatePtr->
cmd, CSEC_FUNC_FORMAT_COPY, g_csecStatePtr->
seq, g_csecStatePtr->
keyId);
1567 uint8_t numBytes = (uint8_t)(((g_csecStatePtr->
fullSize - g_csecStatePtr->
index) >
1586 g_csecStatePtr->
partSize = numBytes;
1589 CSEC_WriteCommandHeader(g_csecStatePtr->
cmd, CSEC_FUNC_FORMAT_COPY, g_csecStatePtr->
seq, g_csecStatePtr->
keyId);
1603 g_csecStatePtr->
errCode = CSEC_ReadErrorBits();
1639 g_csecStatePtr->
errCode = CSEC_ReadErrorBits();
1651 (uint8_t)g_csecStatePtr->
partSize);
1657 (uint8_t)g_csecStatePtr->
partSize);
1686 g_csecStatePtr->
errCode = CSEC_ReadErrorBits();
1726 g_csecStatePtr->
errCode = CSEC_ReadErrorBits();
1767 g_csecStatePtr->
callback = callbackFunc;
1784 CSEC_SetInterrupt(
false);
1787 CSEC_WaitCommandCompletion();
1799 CSEC_WaitCommandCompletion();
status_t CSEC_DRV_LoadPlainKey(const uint8_t *plainKey)
Updates the RAM key memory slot with a 128-bit plaintext.
status_t CSEC_DRV_GetAsyncCmdStatus()
Checks the status of the execution of an asynchronous command.
#define FEATURE_CSEC_FLASH_START_ADDRESS_OFFSET
CSE_PRAM offset of the Flash start address parameter used by the following commands: CMD_GENERATE_MAC...
#define CSEC_DATA_PAGES_AVAILABLE
static void CSEC_DRV_ContinueEncDecECBCmd(void)
static void CSEC_DRV_ContinueEncDecCBCCmd(void)
security_callback_t callback
#define CSEC_M3_SIZE_IN_BYTES
void CSEC_DRV_InstallCallback(security_callback_t callbackFunc, void *callbackParam)
Installs a callback function which will be invoked when an asynchronous command finishes its executio...
#define FEATURE_CSEC_PAGE_5_OFFSET
CSE_PRAM offset of page 5.
void OSIF_TimeDelay(const uint32_t delay)
Delays execution for a number of milliseconds.
status_t CSEC_DRV_GetID(const uint8_t *challenge, uint8_t *uid, uint8_t *sreg, uint8_t *mac)
Returns the identity (UID) and the value of the status register protected by a MAC over a challenge a...
Internal driver state information.
static void CSEC_DRV_StartEncDecECBCmd(void)
#define FTFC_FSTAT_CCIF_MASK
#define CSEC_BYTES_TO_FROM_PAGES_SHIFT
status_t CSEC_DRV_DecryptCBC(csec_key_id_t keyId, const uint8_t *cipherText, uint32_t length, const uint8_t *iv, uint8_t *plainText, uint32_t timeout)
Performs the AES-128 decryption in CBC mode.
void CSEC_DRV_Init(csec_state_t *state)
Initializes the internal state of the driver and enables the FTFC interrupt.
status_t CSEC_DRV_DbgAuth(const uint8_t *authorization)
Erases all keys (actual and outdated) stored in NVM Memory if the authorization is confirmed by CSEc...
status_t CSEC_DRV_VerifyMACAsync(csec_key_id_t keyId, const uint8_t *msg, uint32_t msgLen, const uint8_t *mac, uint16_t macLen, bool *verifStatus)
Asynchronously verifies the MAC of a given message using CMAC with AES-128.
#define CSEC_M1_SIZE_IN_BYTES
csec_key_id_t
Specify the KeyID to be used to implement the requested cryptographic operation.
status_t CSEC_DRV_BootOK()
Marks a successful boot verification during later stages of the boot process.
#define FEATURE_CSEC_VERIFICATION_STATUS_OFFSET
CSE_PRAM offset of the verification status parameter used by the following commands: CMD_VERIFY_MAC (...
uint32_t OSIF_GetMilliseconds(void)
Returns the number of miliseconds elapsed since starting the internal timer or starting the scheduler...
#define CSEC_BYTES_TO_FROM_BITS_SHIFT
status_t CSEC_DRV_EncryptCBCAsync(csec_key_id_t keyId, const uint8_t *plainText, uint32_t length, const uint8_t *iv, uint8_t *cipherText)
Asynchronously performs the AES-128 encryption in CBC mode.
status_t CSEC_DRV_VerifyMAC(csec_key_id_t keyId, const uint8_t *msg, uint32_t msgLen, const uint8_t *mac, uint16_t macLen, bool *verifStatus, uint32_t timeout)
Verifies the MAC of a given message using CMAC with AES-128.
status_t CSEC_DRV_VerifyMACAddrMode(csec_key_id_t keyId, const uint8_t *msg, uint32_t msgLen, const uint8_t *mac, uint16_t macLen, bool *verifStatus)
Verifies the MAC of a given message (located in Flash) using CMAC with AES-128.
status_t CSEC_DRV_InitRNG()
Initializes the seed and derives a key for the PRNG.
static void CSEC_DRV_StartVerifMACCmd(void)
void INT_SYS_DisableIRQ(IRQn_Type irqNumber)
Disables an interrupt for a given IRQ number.
#define CSEC_M2_SIZE_IN_BYTES
#define CSEC_DATA_BYTES_AVAILABLE
status_t CSEC_DRV_ExtendSeed(const uint8_t *entropy)
Extends the seed of the PRNG.
static void CSEC_DRV_StartEncDecCBCCmd(void)
#define FEATURE_CSEC_PAGE_7_OFFSET
CSE_PRAM offset of page 7.
#define FEATURE_CSEC_PAGE_LENGTH_OFFSET
CSE_PRAM offset of the page length parameter used by the following commands: CMD_ENC_ECB, CMD_ENC_CBC, CMD_DEC_ECB, CMD_DEC_CBC, CMD_MP_COMPRESS.
status_t CSEC_DRV_EncryptECB(csec_key_id_t keyId, const uint8_t *plainText, uint32_t length, uint8_t *cipherText, uint32_t timeout)
Performs the AES-128 encryption in ECB mode.
status_t CSEC_DRV_BootFailure()
Signals a failure detected during later stages of the boot process.
status_t CSEC_DRV_DecryptECBAsync(csec_key_id_t keyId, const uint8_t *cipherText, uint32_t length, uint8_t *plainText)
Asynchronously performs the AES-128 decryption in ECB mode.
static void CSEC_DRV_InitState(csec_key_id_t keyId, csec_cmd_t cmd, const uint8_t *inBuff, uint8_t *outBuff, uint32_t length)
#define FEATURE_CSEC_PAGE_3_OFFSET
CSE_PRAM offset of page 3.
status_t CSEC_DRV_EncryptECBAsync(csec_key_id_t keyId, const uint8_t *plainText, uint32_t length, uint8_t *cipherText)
Asynchronously performs the AES-128 encryption in ECB mode.
#define FEATURE_CSEC_BOOT_SIZE_OFFSET
CSE_PRAM offset of the boot size parameter used by the following commands: CMD_BOOT_DEFINE.
status_t CSEC_DRV_DecryptECB(csec_key_id_t keyId, const uint8_t *cipherText, uint32_t length, uint8_t *plainText, uint32_t timeout)
Performs the AES-128 decryption in ECB mode.
#define CSEC_PAGE_SIZE_IN_BYTES
status_t
Status return codes. Common error codes will be a unified enumeration (C enum) that will contain all ...
#define FEATURE_CSEC_MAC_LENGTH_OFFSET
CSE_PRAM offset of the MAC length parameter used by the following commands: CMD_VERIFY_MAC (both copy...
void(* security_callback_t)(uint32_t completedCmd, void *callbackParam)
Callback for security modules.
status_t CSEC_DRV_EncryptCBC(csec_key_id_t keyId, const uint8_t *plainText, uint32_t length, const uint8_t *iv, uint8_t *cipherText, uint32_t timeout)
Performs the AES-128 encryption in CBC mode.
status_t CSEC_DRV_GenerateMACAddrMode(csec_key_id_t keyId, const uint8_t *msg, uint32_t msgLen, uint8_t *cmac)
Calculates the MAC of a given message (located in Flash) using CMAC with AES-128. ...
static void CSEC_DRV_ContinueVerifMACCmd(void)
void FTFC_IRQHandler(void)
static void CSEC_DRV_ContinueGenMACCmd(void)
void CSEC_DRV_Deinit()
Clears the internal state of the driver and disables the FTFC interrupt.
#define FEATURE_CSEC_PAGE_4_OFFSET
CSE_PRAM offset of page 4.
status_t CSEC_DRV_LoadKey(csec_key_id_t keyId, const uint8_t *m1, const uint8_t *m2, const uint8_t *m3, uint8_t *m4, uint8_t *m5)
Updates an internal key per the SHE specification.
#define FEATURE_CSEC_BOOT_FLAVOR_OFFSET
CSE_PRAM offset of the boot flavor parameter used by the following commands: CMD_BOOT_DEFINE.
status_t CSEC_DRV_MPCompress(const uint8_t *msg, uint16_t msgLen, uint8_t *mpCompress, uint32_t timeout)
Compresses the given messages by accessing the Miyaguchi-Prenell compression feature with in the CSEc...
void CSEC_DRV_CancelCommand(void)
Cancels a previously launched asynchronous command.
csec_boot_flavor_t
Specifies the boot type for the BOOT_DEFINE command.
status_t CSEC_DRV_DecryptCBCAsync(csec_key_id_t keyId, const uint8_t *cipherText, uint32_t length, const uint8_t *iv, uint8_t *plainText)
Asynchronously performs the AES-128 decryption in CBC mode.
csec_call_sequence_t
Specifies if the information is the first or a following function call.
void INT_SYS_EnableIRQ(IRQn_Type irqNumber)
Enables an interrupt for a given IRQ number.
csec_cmd_t
CSEc commands which follow the same values as the SHE command definition.
status_t CSEC_DRV_ExportRAMKey(uint8_t *m1, uint8_t *m2, uint8_t *m3, uint8_t *m4, uint8_t *m5)
Exports the RAM_KEY into a format protected by SECRET_KEY.
status_t CSEC_DRV_GenerateRND(uint8_t *rnd)
Generates a vector of 128 random bits.
#define FEATURE_CSEC_SREG_OFFSET
CSE_PRAM offset of the SREG parameter used by the following commands: CMD_GET_ID. ...
#define FEATURE_CSEC_PAGE_1_OFFSET
CSE_PRAM offset of page 1.
#define CSEC_M4_SIZE_IN_BYTES
status_t CSEC_DRV_GenerateMAC(csec_key_id_t keyId, const uint8_t *msg, uint32_t msgLen, uint8_t *cmac, uint32_t timeout)
Calculates the MAC of a given message using CMAC with AES-128.
static uint32_t CSEC_DRV_RoundTo(uint32_t value, uint32_t roundTo)
#define FEATURE_CSEC_MESSAGE_LENGTH_OFFSET
CSE_PRAM offset of the message length parameter used by the following commands: CMD_GENERATE_MAC, CMD_VERIFY_MAC (both copy and pointer methods)
const uint8_t * inputBuff
status_t CSEC_DRV_DbgChal(uint8_t *challenge)
Obtains a random number which the user shall use along with the MASTER_ECU_KEY and UID to return an a...
static csec_state_t * g_csecStatePtr
static void CSEC_DRV_StartGenMACCmd(void)
#define FEATURE_CSEC_PAGE_2_OFFSET
CSE_PRAM offset of page 2.
#define CSEC_M5_SIZE_IN_BYTES
status_t CSEC_DRV_BootDefine(uint32_t bootSize, csec_boot_flavor_t bootFlavor)
Implements an extension of the SHE standard to define both the user boot size and boot method...
status_t CSEC_DRV_GenerateMACAsync(csec_key_id_t keyId, const uint8_t *msg, uint32_t msgLen, uint8_t *cmac)
Asynchronously calculates the MAC of a given message using CMAC with AES-128.