/* UDS SecurityAccess SID 0x27 — seed-key authentication */
/* AUTOSAR DCM: SecurityAccess service handler */
#include "Dcm.h"
#include "Csm.h"
static uint8 sa_seed[32]; /* 256-bit random seed per ISO/SAE 21434 */
static uint8 sa_attempt_count; /* failed attempt counter per level */
static uint32 sa_lockout_timer; /* exponential backoff timer (ms) */
Dcm_StatusType DCM_SecurityAccess_RequestSeed(uint8 security_level,
uint8* seed_out, uint8* seed_len)
{
/* Check lockout: 3 failures → exponential backoff */
if (sa_attempt_count >= 3 && sa_lockout_timer > 0) {
return DCM_E_REQUEST_OUT_OF_RANGE; /* NRC 0x31 */
}
/* Generate 256-bit cryptographically secure random seed */
Csm_RandomGenerate(CSM_JOB_RANDOM_256BIT, sa_seed, 32);
memcpy(seed_out, sa_seed, 32);
*seed_len = 32;
return DCM_E_OK;
}
Dcm_StatusType DCM_SecurityAccess_CompareKey(uint8 security_level,
const uint8* received_key, uint8 key_len)
{
uint8 expected_key[32];
/* Expected key = HMAC-SHA256(sa_seed || ECU_ID, secret_key) truncated to 32B */
/* secret_key stored in HSM KEY_2 slot — never exported */
Csm_MacGenerate(CSM_JOB_HMAC_SHA256_KEY2,
sa_seed, 32, expected_key);
/* CRITICAL: constant-time comparison — no timing oracle */
uint8 diff = 0;
for (uint8 i = 0; i < 32; i++) {
diff |= expected_key[i] ^ received_key[i];
}
if (diff != 0) {
sa_attempt_count++;
if (sa_attempt_count >= 3) {
sa_lockout_timer = 10000u * (1u << (sa_attempt_count - 3u)); /* exp backoff */
}
return DCM_E_INVALID_KEY; /* NRC 0x35 */
}
sa_attempt_count = 0;
DCM_SetSecurityLevel(security_level);
return DCM_E_OK;
}UDS SecurityAccess (0x27) Seed-Key Mechanism
Certificate-Based Authentication for High-Security Access
| Access Level | Authentication Method | Use Case |
|---|---|---|
| Programming session | mTLS client certificate (OEM-issued to workshop tool) | ECU reflash; only authorised workshop tools can reprogram |
| Engineering/factory | OEM engineering certificate (restricted distribution) | Full calibration write; DTC clear; all diagnostic levels |
| Workshop (standard) | OEM workshop certificate (VIN-bound or tool-bound) | DTC read/clear, routine control, some ReadDataByIdentifier |
| End-user diagnostics | No certificate; UDS default session only | ReadDataByIdentifier for public data; no security-sensitive access |
Securing DoIP (ISO 13400) with TLS
#!/usr/bin/env python3
# DoIP over TLS: secure routing activation with mTLS client cert verification
import ssl, socket, struct
def create_doip_tls_connection(vehicle_ip: str, doip_port: int = 13400,
client_cert: str = "workshop_tool.pem",
client_key: str = "workshop_tool.key",
ca_cert: str = "oem_root_ca.pem") -> ssl.SSLSocket:
"""Establish mTLS-secured DoIP TCP connection per ISO 13400-2"""
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ctx.minimum_version = ssl.TLSVersion.TLSv1_3 # enforce TLS 1.3 minimum
ctx.verify_mode = ssl.CERT_REQUIRED
ctx.load_verify_locations(ca_cert) # verify vehicle's ECU cert
ctx.load_cert_chain(client_cert, client_key) # present workshop tool cert
sock = socket.create_connection((vehicle_ip, doip_port), timeout=5)
tls_sock = ctx.wrap_socket(sock, server_hostname=vehicle_ip)
# DoIP RoutingActivation (0x0005) — authentication token extension
routing_req = struct.pack(">HHIB",
0xFFFE, # Source address (test equipment)
0x0005, # RoutingActivation payload type
0x00000001, # Length
0x00 # Default activation type (client cert already validated by TLS)
)
tls_sock.sendall(routing_req)
response = tls_sock.recv(1024)
print(f"DoIP RoutingActivation response: {response.hex()}")
return tls_sockDiagnostic Data Confidentiality
| Data Type | Protection Required | Implementation |
|---|---|---|
| VIN, mileage, DTC history | Session-level encryption under TLS | TLS provides transport encryption; no additional layer needed |
| Encryption counters, FV values | Encrypt for OEM-level access only | SecurityAccess level 3 (OEM cert) before ReadDataByIdentifier |
| Calibration parameters | Read requires workshop cert; write requires OEM cert | Role-based access in DCM SecurityLevel configuration |
| SecurityAccess audit log | Tamper-evident NVM log; read only with OEM access | Log format: timestamp + tester cert serial + service ID + NRC; CRC protected |
Summary
UDS security architecture requires three elements working together: SecurityAccess seed-key with cryptographically secure 256-bit random seeds and exponential-backoff lockout; certificate-based authentication for high-security levels (reflash, calibration); and DoIP over mTLS to ensure the diagnostic transport itself is authenticated and encrypted. Constant-time MAC comparison in the key verification function is essential — timing attacks on naive memcmp() implementations have been demonstrated to recover seed-key secrets in automotive systems.
🔬 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.