SecOcTxPduProcessing_EngTorqueCmd
48
64
16
TRUE
/CryptoConfig/CsmJobs/CsmJob_AES128_CMAC_Generate
/Com/ComConfig/ComIPdus/EngTorqueCmd_Authentic_PDU
SecOcRxPduProcessing_EngTorqueCmd
48
64
16
10
DROP_NEWEST
ARXML Configuration: SecOC in DaVinci Configurator
Freshness Value Management Implementation
/* SecOC FreshnessManagement callout implementation */
/* Two strategies: counter-based (NVM) and time-based (StbM) */
#include "SecOC.h"
#include "NvM.h"
#include "StbM.h"
/* Strategy A: Counter-based FV */
/* FV = TripCounter[16-bit] || MessageCounter[48-bit] */
static uint16 trip_counter; /* persisted in NVM; incremented on ECU reset */
static uint64 message_counter; /* RAM only; reset to 0 on ECU reset */
Std_ReturnType SecOC_GetTxFreshness(uint16 freshnessValueId,
uint8* freshnessValue, uint32* freshnessValueLength)
{
uint64 full_fv;
message_counter++; /* increment per transmission */
/* Build 64-bit FV: [TripCounter 16b][MessageCounter 48b] */
full_fv = ((uint64)trip_counter << 48) | (message_counter & 0xFFFFFFFFFFFFULL);
/* Store full FV big-endian */
for (int i = 7; i >= 0; i--) {
freshnessValue[i] = (uint8)(full_fv & 0xFF);
full_fv >>= 8;
}
*freshnessValueLength = 8; /* 64 bits */
return E_OK;
}
/* Called by ComM before power-off: persist trip counter to NVM */
void SecOC_PreSleep_PersistFV(void)
{
trip_counter++; /* increment for next ignition cycle */
NvM_WriteBlock(NVM_BLOCK_SECOC_TRIP_COUNTER, &trip_counter);
NvM_WriteBlock(NVM_BLOCK_SECOC_MSG_COUNTER, &message_counter);
}
/* Strategy B: Time-based FV using AUTOSAR StbM (nanosecond timestamp) */
Std_ReturnType SecOC_GetTxFreshness_TimeBased(uint16 freshnessValueId,
uint8* freshnessValue, uint32* freshnessValueLength)
{
StbM_TimeStampType timestamp;
StbM_UserDataType userData;
StbM_GetCurrentTime(STBM_SYNC_TIME_BASE_ETHERNET, ×tamp, &userData);
/* Use lower 64 bits of timestamp as FV */
uint64 fv_time = ((uint64)timestamp.seconds << 32) | timestamp.nanoseconds;
memcpy(freshnessValue, &fv_time, 8);
*freshnessValueLength = 8;
return E_OK;
}Key Provisioning in Test Environment
/* CANoe CAPL: SecOC integration test simulation */
/* Inject authentic frames and attack frames, verify ECU response */
variables {
byte test_key[16] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x10};
int authentic_frames_sent = 0;
int attack_frames_sent = 0;
int attack_frames_detected = 0;
}
/* Inject authentic frame with correct CMAC */
void InjectAuthenticFrame(long pdu_id, byte* payload, int len)
{
byte secured_pdu[64];
byte fv[2]; /* 16-bit truncated freshness value */
byte mac[6]; /* 48-bit truncated MAC */
/* Build secured PDU: payload || FV || MAC */
memcpy(secured_pdu, payload, len);
/* FV: current counter value (simplified for test) */
fv[0] = (authentic_frames_sent >> 8) & 0xFF;
fv[1] = authentic_frames_sent & 0xFF;
memcpy(secured_pdu + len, fv, 2);
/* Compute AES-128-CMAC using test key (SHE mock in CANoe) */
ComputeCMAC_AES128(test_key, secured_pdu, len + 2, mac, 6);
memcpy(secured_pdu + len + 2, mac, 6);
/* Transmit secured PDU on CAN bus */
CanOutputPDU(pdu_id, secured_pdu, len + 8);
authentic_frames_sent++;
}
/* Inject attack frame with random (wrong) MAC */
void InjectAttackFrame(long pdu_id, byte* payload, int len)
{
byte secured_pdu[64];
memcpy(secured_pdu, payload, len);
/* Random FV and MAC — will fail SecOC verification */
GetRandomBytes(secured_pdu + len, 8);
CanOutputPDU(pdu_id, secured_pdu, len + 8);
attack_frames_sent++;
}
/* Monitor DEM events: SecOC verification failure */
on envVar SecOC_VerificationStatus_EngTorqueCmd {
if (@this == FAILED) {
attack_frames_detected++;
write("SecOC ATTACK DETECTED: frame %d — DEM event logged", attack_frames_sent);
}
}
on start {
/* Test sequence: 10 authentic, 5 attack, 10 authentic */
byte eng_payload[8] = {0x01,0x02,0x00,0x00,0x00,0x00,0x00,0x00};
int i;
for (i = 0; i < 10; i++) InjectAuthenticFrame(0x100, eng_payload, 8);
for (i = 0; i < 5; i++) InjectAttackFrame(0x100, eng_payload, 8);
for (i = 0; i < 10; i++) InjectAuthenticFrame(0x100, eng_payload, 8);
}
on stop {
write("Test summary: %d authentic / %d attacks / %d detected (must = %d)",
authentic_frames_sent, attack_frames_sent, attack_frames_detected, attack_frames_sent);
}Verification and Debugging
| Failure Mode | Root Cause | Diagnosis | Fix |
|---|---|---|---|
| FV sync loss after ECU reset | FV counter not persisted to NVM before reset | Check NvM_WriteBlock call in ComM sleep notification | Write FV to NVM in SecOC_PreSleep_PersistFV() hook |
| Key mismatch (CMAC always FAILS) | Sender/receiver provisioned with different keys | Use SHE M4/M5 verification fields to confirm key identity in both ECUs | Re-provision both ECUs from same KMS key derivation |
| Acceptance window exceeded | Lost frames accumulate; receiver FV exceeds window | CANalyzer trace: count consecutive SECOC_FAILED events | Increase SecOCAcceptanceWindow or investigate bus errors causing loss |
| MAC verify fails after gateway re-route | Gateway not re-authenticating frames with destination key | SecOC gateway config: verify source key, re-sign with destination key | Configure gateway SecOC instance with both source and destination key slots |
Summary
SecOC implementation follows a clear path: configure ARXML with 48-bit minimum MAC truncation and 64-bit FV, implement FreshnessManagement using NVM-persisted trip counter, provision keys at EOL via SHE M1-M5, and validate with CANoe CAPL tests injecting both authentic and attack frames. The most common production issue is FV sync loss after ECU reset — always persist the FV counter to NVM in the pre-sleep ComM notification, not just on ignition-off.
🔬 Deep Dive — Core Concepts Expanded
This section builds on the foundational concepts covered above with additional technical depth, edge cases, and configuration nuances that separate competent engineers from experts. When working on production ECU projects, the details covered here are the ones most commonly responsible for integration delays and late-phase defects.
Key principles to reinforce:
- Configuration over coding: In AUTOSAR and automotive middleware environments, correctness is largely determined by ARXML configuration, not application code. A correctly implemented algorithm can produce wrong results due to a single misconfigured parameter.
- Traceability as a first-class concern: Every configuration decision should be traceable to a requirement, safety goal, or architecture decision. Undocumented configuration choices are a common source of regression defects when ECUs are updated.
- Cross-module dependencies: In tightly integrated automotive software stacks, changing one module's configuration often requires corresponding updates in dependent modules. Always perform a dependency impact analysis before submitting configuration changes.
🏭 How This Topic Appears in Production Projects
- Project integration phase: The concepts covered in this lesson are most commonly encountered during ECU integration testing — when multiple software components from different teams are combined for the first time. Issues that were invisible in unit tests frequently surface at this stage.
- Supplier/OEM interface: This is a topic that frequently appears in technical discussions between Tier-1 ECU suppliers and OEM system integrators. Engineers who can speak fluently about these details earn credibility and are often brought into critical design review meetings.
- Automotive tool ecosystem: Vector CANoe/CANalyzer, dSPACE tools, and ETAS INCA are the standard tools used to validate and measure the correct behaviour of the systems described in this lesson. Familiarity with these tools alongside the conceptual knowledge dramatically accelerates debugging in real projects.
⚠️ Common Mistakes and How to Avoid Them
- Assuming default configuration is correct: Automotive software tools ship with default configurations that are designed to compile and link, not to meet project-specific requirements. Every configuration parameter needs to be consciously set. 'It compiled' is not the same as 'it is correctly configured'.
- Skipping documentation of configuration rationale: In a 3-year ECU project with team turnover, undocumented configuration choices become tribal knowledge that disappears when engineers leave. Document why a parameter is set to a specific value, not just what it is set to.
- Testing only the happy path: Automotive ECUs must behave correctly under fault conditions, voltage variations, and communication errors. Always test the error handling paths as rigorously as the nominal operation. Many production escapes originate in untested error branches.
- Version mismatches between teams: In a multi-team project, the BSW team, SWC team, and system integration team may use different versions of the same ARXML file. Version management of all ARXML files in a shared repository is mandatory, not optional.
📊 Industry Note
Engineers who master both the theoretical concepts and the practical toolchain skills covered in this course are among the most sought-after professionals in the automotive software industry. The combination of AUTOSAR standards knowledge, safety engineering understanding, and hands-on configuration experience commands premium salaries at OEMs and Tier-1 suppliers globally.