Security access with a lockout
The server side of SecurityAccess: one key a seed, three attempts, then a delay. A hard embedded C problem (UDS: security access, routines and reset), run…
hard · UDS: security access, routines and reset · uds · security · automotive · state-machine · timers
Write the ECU's side of SecurityAccess (0x27). Each function returns NRC_NONE for a positive response, or the negative response code. now is a millisecond counter that wraps around.
request_seed(s, now, seed_out)
- While the delay timer runs (fewer than
DELAY_MSsince it started):NRC_DELAY_NOT_EXPIRED. Once the time has passed, the delay is over. - Already unlocked: the seed is 0, and nothing else changes (do not call
random_seed). - Otherwise: take a seed from
random_seed(), remember it as outstanding, and give it in*seed_out.
send_key(s, now, key)
- No seed outstanding:
NRC_SEQUENCE_ERROR. - A seed is good for one answer: right or wrong, it is no longer outstanding afterwards.
- Right key (
key_for(seed)): unlocked, and the count of wrong keys returns to 0. - Wrong key: count it. If that makes
MAX_ATTEMPTS, start the delay timer atnow, reset the count, and returnNRC_EXCEEDED_ATTEMPTS; otherwiseNRC_INVALID_KEY.
lock(s): a session change. Locked again, and no seed outstanding; the wrong-key count and the delay are untouched.
s starts zeroed.
Starter code
uint8_t request_seed(security_t *s, uint32_t now, uint16_t *seed_out) {
(void)now;
s->seed = random_seed();
s->seed_valid = true;
*seed_out = s->seed;
return NRC_NONE;
}
uint8_t send_key(security_t *s, uint32_t now, uint16_t key) {
(void)now;
if (key == key_for(s->seed)) {
s->unlocked = true;
return NRC_NONE;
}
return NRC_INVALID_KEY;
}
void lock(security_t *s) {
s->unlocked = false;
}Example tests
Unlock with the right key
seed(100);
key(150, true);
seed(200);
printf("seeds generated: %d\n", seeds_made);Expected output:
t=100 seed: given
t=150 key: accepted, unlocked
t=200 seed: 0000
seeds generated: 1A wrong key, then a second try needs a new seed
seed(100);
key(150, false);
key(200, true);
seed(250);
key(300, true);Expected output:
t=100 seed: given
t=150 key: NRC 35
t=200 key: NRC 24
t=250 seed: given
t=300 key: accepted, unlockedLearn it first
- UDS: security access, routines and reset · Seed and key, attempt counters and delays, RoutineControl, ECUReset, and what real security needs.
More UDS: security access, routines and reset problems
- Compute the key for a seed · Implement a shift-and-XOR seed and key algorithm of the kind ECUs use for SecurityAccess.