/* SecOC symmetric key provisioning via SHE (Secure Hardware Extension) */
/* Production EOL: KMS generates M1-M5 for each ECU; provisioning station loads key */
#include "SheDriver.h"
#include "SecOC.h"
/* Key hierarchy: Master Secret → KDF → per-ECU, per-bus, per-direction key */
/* VMS = HKDF(RootKey, VIN)
ESK_CAN0_TX = HKDF(VMS, ECU_ID || "CAN0" || "TX") */
Std_ReturnType SecOC_ProvisionKey(const uint8* m1, const uint8* m2,
const uint8* m3, uint8* m4_out, uint8* m5_out)
{
SHE_StatusType status;
/* SHE CMD_LOAD_KEY: load AES-128 key into KEY_1 slot using M1-M5 protocol */
/* M1: ECU_ID + KEY_ID + count (prevents replay of old key) */
/* M2: encrypted key + flags (AES-128-CBC with MASTER_ECU_KEY) */
/* M3: authentication (AES-128-CMAC over M1+M2) */
status = SHE_LoadKey(SHE_KEY_SLOT_1, m1, m2, m3, m4_out, m5_out);
/* M4+M5 returned by SHE serve as provisioning proof */
/* Archive M4+M5 in KMS for audit: proves correct key was loaded in this ECU */
if (status != SHE_STATUS_OK) {
/* Provisioning failure: ECU cannot authenticate CAN messages */
/* Log DEM error; ECU enters fail-safe without SecOC */
Dem_ReportErrorStatus(DEM_EVENT_SECOC_KEY_PROV_FAIL, DEM_EVENT_STATUS_FAILED);
}
return (status == SHE_STATUS_OK) ? E_OK : E_NOT_OK;
}
/* Key rotation: new key delivered via UDS WriteDataByIdentifier 0x2E */
/* New key encrypted with current ESK; activated on next ignition cycle */
/* Version counter checked: new_count >= current_count (anti-rollback) */