Simvik

Compute the key for a seed

Implement a shift-and-XOR seed and key algorithm of the kind ECUs use for SecurityAccess. An easy embedded C problem (UDS: security access, routines and…

easy · UDS: security access, routines and reset · uds · security · automotive · bits · shifts

SecurityAccess is a challenge and response: the ECU sends a seed, and the tester must answer with the key that a secret algorithm makes from it. The ECU runs the same algorithm to check the answer.

Implement this ECU's algorithm, key_for(seed), on 32-bit values:

  1. Start with key = seed.
  2. Repeat 35 times: if the top bit of key is set, shift key left by one and XOR it with the constant 0x5F3A9C17; otherwise only shift it left by one.
  3. The result is the key.

Everything stays in 32 bits: bits shifted out of the top are lost.

Starter code

uint32_t key_for(uint32_t seed) {
  uint32_t key = seed;
  for (int i = 0; i < 35; i++) {
    key = (key << 1) ^ 0x5F3A9C17u;
  }
  return key;
}

Example tests

Two seeds

show(0x00000001);
show(0x12345678);

Expected output:

seed 00000001 -> key 47A1D896
seed 12345678 -> key 1AFC069E

Learn it first

More UDS: security access, routines and reset problems