UDS: security access, routines and reset
Seed and key, attempt counters and delays, RoutineControl, ECUReset, and what real security needs.
Why an ECU locks itself
The OBD port is under the dashboard of every car, and anything plugged into it can send diagnostic requests. Reading trouble codes is harmless. Rewriting the odometer, disabling the immobiliser, changing an engine map or flashing new firmware is not. UDS therefore lets a server lock services, identifiers and routines behind a security level, and gives two services to unlock one: the original SecurityAccess (0x27) and, since the 2020 edition, Authentication (0x29).
SecurityAccess (0x27): seed and key
It is a challenge and response:
tester 27 01 requestSeed, level 1
ECU 67 01 29 5D the seed: 0x295D
tester 27 02 7D 4B sendKey: 0x7D4B, computed from the seed
ECU 67 02 unlocked
- The tester asks for a seed. The ECU generates a number, remembers it, and sends it.
- Both sides run the same secret algorithm on the seed. The tester sends its result, the key.
- The ECU compares. If they match, the level is unlocked until the session changes.
The secret is the algorithm (or a key inside it), which never crosses the bus. Because the seed differs each time, a recorded exchange cannot be replayed.
Sub-functions come in pairs: an odd value requests the seed for a level and the next even value sends its key. 01/02 is level 1, 03/04 level 2, and so on; manufacturers use different levels for, say, end-of-line configuration and reprogramming. The seed and key lengths are the manufacturer's choice: 2 bytes in old ECUs, 4 to 16 or more in current ones.
The rules a server must follow
| Situation | Response |
|---|---|
| requestSeed when that level is already unlocked | positive, with a seed of all zeros: "nothing to do" |
| sendKey with no seed requested first, or a second key for the same seed | 7F 27 24 requestSequenceError |
| wrong key | 7F 27 35 invalidKey, and the attempt counter goes up |
| the wrong key that reaches the attempt limit | 7F 27 36 exceededNumberOfAttempts, and a delay timer starts |
| requestSeed while the delay timer runs | 7F 27 37 requiredTimeDelayNotExpired |
| any session change, the S3 timeout, a reset | locked again |
The attempt counter and the delay timer are what stand between a short key and an attacker. Without them a 16-bit key falls to 65 536 tries in minutes. With three attempts and a 10 s delay it takes days; with a 32-bit key, centuries. Three implementation details decide whether that protection is real:
- The delay must survive a reset. If cycling power clears the counter, the attacker cycles power. The standard's answer: store the attempt counter (or a "locked out" flag) in non-volatile memory, and if it is non-zero at power-up, start the delay timer at boot. Many manufacturers require the delay after every power-up regardless.
- A seed is good for one answer. After any key, right or wrong, the seed is spent. Otherwise an attacker requests one seed and tries every key against it at bus speed.
- The seed must be unpredictable. A seed from a counter, or from a pseudo-random generator seeded with the same value at every boot, repeats; an attacker who has seen one valid seed and key pair waits for that seed to come round. Use a hardware random number generator, or at least mix in something that varies (ADC noise, a free-running timer captured on a bus event) and never reuse a seed.
#include <stdio.h>
#include <stdint.h>
/* A toy key algorithm: every real one is a manufacturer's secret. */
static uint16_t key_for(uint16_t seed) {
uint16_t x = (uint16_t)(seed ^ 0xA55A);
x = (uint16_t)((x << 3) | (x >> 13));
return (uint16_t)(x + 0x1D0F);
}
/* Compares two keys in the same time whatever they hold. */
static int same_key(const uint8_t *a, const uint8_t *b, unsigned n) {
uint8_t diff = 0;
for (unsigned i = 0; i < n; i++) diff |= (uint8_t)(a[i] ^ b[i]);
return diff == 0;
}
typedef struct { uint16_t seed; uint8_t seed_valid, unlocked, wrong; } security_t;
static uint8_t send_key(security_t *s, uint16_t key) { /* returns 0, or the NRC */
if (!s->seed_valid) return 0x24;
s->seed_valid = 0; /* one answer a seed */
uint16_t want = key_for(s->seed);
const uint8_t a[2] = { (uint8_t)(key >> 8), (uint8_t)key }, b[2] = { (uint8_t)(want >> 8), (uint8_t)want };
if (same_key(a, b, 2)) { s->unlocked = 1; s->wrong = 0; return 0; }
if (++s->wrong >= 3) return 0x36; /* the caller starts the delay timer */
return 0x35;
}
int main(void) {
security_t s = { 0x295D, 1, 0, 0 };
printf("key for 0x%04X is 0x%04X\n", s.seed, key_for(s.seed));
printf("wrong key: NRC 0x%02X\n", send_key(&s, 0x0000));
printf("right key, late: NRC 0x%02X\n", send_key(&s, 0x7D4B)); /* the seed was spent */
s.seed = 0x8BFA; s.seed_valid = 1; /* a new seed */
printf("right key: NRC 0x%02X, unlocked %u\n", send_key(&s, key_for(0x8BFA)), s.unlocked);
return 0;
}
same_key deserves a second look. A comparison that returns at the first differing byte takes slightly longer the more leading bytes are right, and on a quiet bus that difference in response time can be measured, giving the key away byte by byte. Comparing every byte and deciding at the end removes the leak. It costs nothing, and is the kind of detail a security review looks for first.
What seed and key does not give you
Be honest about this in an interview; it is what separates someone who has implemented the service from someone who has thought about it.
- The secret is shared. The same algorithm is in every ECU of that type and in every tester. Extract it from one ECU's flash, or one copy of the tester software, and every car is open. Algorithms with 2-byte keys have been published for many older vehicles.
- It authenticates nothing but "knows the algorithm". There is no identity, no revocation, no audit of who unlocked what.
- It does not protect the conversation. After unlocking, requests travel in the clear; a device on the bus can inject its own.
Hence the newer mechanisms: Authentication (0x29), where the tester proves its identity with a certificate and a signature (asymmetric cryptography, with the ECU holding only public keys and roles granted per certificate); a hardware security module in the MCU to hold keys where a flash dump cannot reach; secure onboard communication (SecOC) to authenticate normal bus messages; and signed firmware, checked by the bootloader, so that even an unlocked ECU accepts only the manufacturer's images. Regulation (UN R155) now requires manufacturers to manage these risks, which is why this is a growing area of work.
RoutineControl (0x31)
A routine is a named procedure the ECU can run on request: erase a memory area, check a downloaded image, run an actuator self-test, learn a sensor's end stops. It is addressed by a 16-bit routine identifier (RID).
| Request | Meaning |
|---|---|
31 01 <RID> [options] | startRoutine |
31 02 <RID> | stopRoutine |
31 03 <RID> | requestRoutineResults |
tester 31 01 02 03 start routine 0x0203
ECU 7F 31 78 it takes longer than P2
ECU 71 01 02 03 00 done; one byte of result
Two designs, and the specification of each routine says which it uses:
- Synchronous: the positive response to start is sent when the routine has finished, with its result, using
78while it runs. Simple for the tester; right for jobs of up to a few seconds. - Asynchronous: start is answered at once ("started"); the routine runs in the background; the tester polls with requestRoutineResults and may stop it. Needed for long jobs, such as a ten-minute bleed of a brake system, during which the tester must stay able to talk.
The standard reserves a few RIDs: FF00 eraseMemory, FF01 checkProgrammingDependencies, 0202 and 0203 for checks of memory and programming preconditions. Everything else is the manufacturer's.
Routines that move things are where diagnostics meets functional safety. An actuator test must check its preconditions (22 conditionsNotCorrect: vehicle stationary, gear in park, voltage in range), must be limited in time and effect, and must stop if the session ends. A routine that could open a door lock or apply a brake while the car is moving is a hazard, and the checks that prevent it are safety requirements, tested and reviewed as such.
ECUReset (0x11)
| Request | Reset type |
|---|---|
11 01 | hardReset: as if power had been removed |
11 02 | keyOffOnReset: as if the ignition had been cycled |
11 03 | softReset: restart the application |
The one thing to get right: respond, then reset. The positive response 51 01 must be on the bus before the MCU restarts. Calling the reset function straight after queuing the response loses the frame in the transmit mailbox. Wait for the transmit confirmation (or a short fixed time), then reset; if non-volatile data is pending, finish writing it first.
How the pieces fit: a reprogramming session
Every service in this module appears in the sequence a tester runs to flash an ECU. You should be able to narrate it:
| Step | Request | Why |
|---|---|---|
| 1 | 10 03 to all | extended session |
| 2 | 31 01 02 03 | check programming preconditions: voltage, vehicle state |
| 3 | 85 02, 28 03 01 to all | stop DTC storage and normal messages on every ECU |
| 4 | 10 02 to the target | programming session: the ECU resets into its bootloader |
| 5 | 27 .. | unlock the programming security level |
| 6 | 2E F1 5A .. | write a "fingerprint": who is programming, and when |
| 7 | 31 01 FF 00 .. | erase the memory area |
| 8 | 34, 36 × n, 37 | RequestDownload, TransferData blocks, RequestTransferExit |
| 9 | 31 01 FF 01 | check the image's integrity, signature and dependencies |
| 10 | 11 01, then 28 00, 85 01, 14 FF FF FF | reset into the new software, restore the other ECUs, clear faults |
Through all of it the tester sends 3E 80 functionally every two seconds, so that no ECU's S3 timer returns it to normal operation half-way.
Interview question: Explain UDS SecurityAccess and how it resists brute-force guessing.
Answer: The tester requests a seed with an odd sub-function; the ECU returns a fresh random value. Both compute a key from it with a secret algorithm, and the tester sends the key with the next even sub-function. If it matches, that security level is unlocked until the session changes. Guessing is limited by an attempt counter and a delay timer: after a configured number of wrong keys the ECU answers exceededNumberOfAttempts and refuses to hand out a seed until the delay has passed, answering requiredTimeDelayNotExpired meanwhile. A seed is valid for one key only, the counter is kept across resets with the delay enforced at power-up, and the seed must be unpredictable so that recorded seed and key pairs cannot be reused.
Interview question: What are the weaknesses of seed and key, and what replaces it?
Answer: Its security rests on a shared secret algorithm that exists in every ECU of a type and in every tester, so one extraction opens them all, and short keys can be brute-forced or have been published. It gives no identity, no roles and no revocation, and it does not protect later requests. Newer vehicles use the Authentication service 0x29 with certificates and challenge signatures, so the ECU needs only public keys and can grant roles per certificate; they keep keys in a hardware security module, verify firmware signatures in the bootloader so that an unlocked ECU still rejects unsigned images, and authenticate normal bus traffic with SecOC.
Going deeper
Interview question: Where should the attempt counter for SecurityAccess live, and what happens at power-up?
Answer: In non-volatile memory, written when a wrong key is received, before the negative response is sent, so that removing power between the attempt and the write does not give a free try. At power-up the server reads it. If the counter is at its limit, or by many manufacturers' rules if it is non-zero or simply always, the server starts the delay timer and answers requestSeed with requiredTimeDelayNotExpired until it expires. A successful unlock clears the counter. Because flash wears, the counter is stored with wear levelling or in EEPROM-like storage, and its integrity is protected with a checksum, defaulting to "locked out" if the record is corrupt.
Interview question: An actuator test routine opens and closes the door locks. Which checks belong in the RoutineControl handler?
Answer: The protocol checks first: the routine is available only in the extended session, with the required security level, and the request has the right length and options. Then the preconditions, answered with conditionsNotCorrect if they fail: vehicle speed zero from a trusted source, no crash signal, supply voltage in range, no other routine using the actuators. The routine itself is bounded in duration and repetitions, monitors the same safety limits the normal function does, and stops if the session ends, the tester asks, or a precondition goes false. Because it can affect people's safety, these checks are derived from the safety analysis and verified with fault-injection tests, not left to the diagnostic developer's judgement.
Interview question: Why does entering the programming session usually reset the ECU?
Answer: Reprogramming erases the application, so the code that receives the new image cannot be part of it. It lives in a bootloader in a protected flash region. On a request for the programming session the application validates preconditions, writes a flag that survives reset, optionally sends response pending, and resets. The bootloader starts, sees the flag, enters the programming session directly and sends the positive response to the original request, so the tester sees one continuous exchange. The bootloader implements just the diagnostic services needed for download, with its own transport layer, and jumps to the application only when a valid image is present.
Explain it: In your own words, explain why a seed must be used for one key attempt only, and why it must be unpredictable.
Answer: If a seed stayed valid after a wrong key, an attacker could request one seed and then try key after key against it, limited only by bus speed, and the attempt counter could not be tied to fresh challenges. Spending the seed on every attempt forces a new request each time, which the counter and delay then throttle. Unpredictability matters because the key depends only on the seed: if seeds repeat or can be forced, for example a generator that starts from the same value after every reset, an attacker who has recorded one valid seed and key pair just waits for or provokes that seed and replays the key, without ever knowing the algorithm.