aka: a command on a logical channel from 4 to 19 carries the extended class byte

The AUTHENTICATE and the GET RESPONSE went out with the channel number
as their class byte. That is the coding of the standard logical
channels, 0 to 3 (ETSI TS 102 221 10.1.1, table 10.3: the channel in
bits b2 and b1). For the extended ones, 4 to 19, table 10.4a has
'01x0 xxxx' with the channel less four in the low four bits: a command
for channel 4 starts with 40. As 04 it is a command for channel 0 with
a secure-messaging bit set.

The channel is the one the modem returns when imsd opens a logical
channel for the AKA run; imsd does not log it, so whether a
registration ever got one above 3 is not on record.

tests/Aka pins the class byte of channels 0, 1, 3, 4, 5 and 19, the
AUTHENTICATE on channels 3 and 4 and the GET RESPONSE on channel 7.
This commit is contained in:
Jorijn van der Graaf 2026-11-23 14:47:32 +01:00
commit 460343e578
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
3 changed files with 32 additions and 6 deletions

View file

@ -253,7 +253,10 @@ What the suites pin:
form (the RFC's §4.1–4.2.3 examples, idempotence on `inet_ntop`-shaped
text, what is refused), the address-list parser, hex/base64, MD5.
- `Aka` — the AKAv1-MD5 digest, identity derivation, the `qmicli`/`mmcli`
text parsers, the AUTHENTICATE APDU and its response parser.
text parsers, the AUTHENTICATE APDU and its response parser, the class
byte of a command on a logical channel (the channel itself for 0 to 3,
`4X` for 4 to 19 — TS 102 221 10.1.1; AUTHENTICATE on channels 3 and
4, GET RESPONSE on channel 7).
- `Ipsec` — the exact `ip xfrm` argv lists for the SA pairs, the `EALG`
setting, the Security-Server mechanism selection against one offered
cipher and against two (KPN's entry and Odido's, O2's six with the SPIs

View file

@ -248,20 +248,29 @@ export namespace imsd::aka {
// ---- AUTHENTICATE(AKA) APDU (ISO 7816-4 over the qmicli UIM channel) ----
// The class byte of a command on a logical channel (ETSI TS 102 221
// 10.1.1): '0X' with the channel in its two low bits for the standard
// channels 0 to 3, '4X' with the channel less four in its low nibble for
// the extended ones, 4 to 19. Before 1.2.3 the channel number itself was
// the class byte, which is the first form only: a command for channel 4
// went out as '04', channel 0 with a secure-messaging bit set.
inline int ClassByte(int channel) {
return channel < 4 ? channel & 0x03 : 0x40 | ((channel - 4) & 0x0F);
}
// The command APDU (hex, no separators) for a 3G AKA authentication:
// CLA=<channel byte> INS=88 P1=00 P2=81 Lc [10 RAND(16) 10 AUTN(16)]
// `channel` is the logical-channel number qmicli returned; the CLA low
// nibble carries it (Python: cla = "%02x" % ch).
// CLA=<class byte> INS=88 P1=00 P2=81 Lc [10 RAND(16) 10 AUTN(16)]
// `channel` is the logical-channel number qmicli returned.
std::string BuildAkaApdu(int channel, std::span<const std::uint8_t> rand16, std::span<const std::uint8_t> autn16) {
std::string data = std::format("10{}10{}", imsd::util::ToHex(rand16), imsd::util::ToHex(autn16));
std::size_t lc = data.size() / 2;
// CLA INS=88 P1=00 P2=81 Lc <data>
return std::format("{:02x}880081{:02x}{}", channel, lc, data);
return std::format("{:02x}880081{:02x}{}", ClassByte(channel), lc, data);
}
// GET RESPONSE APDU for when the card answers 61xx (xx bytes available).
std::string BuildGetResponseApdu(int channel, int le) {
return std::format("{:02x}c00000{:02x}", channel, le & 0xFF);
return std::format("{:02x}c00000{:02x}", ClassByte(channel), le & 0xFF);
}
struct AkaResult {

View file

@ -98,6 +98,20 @@ int main() {
Check(apdu == "0188008122" "10" "11111111111111111111111111111111" "10" "22222222222222222222222222222222", "APDU byte-exact");
Check(BuildGetResponseApdu(0x01, 0x2E) == "01c000002e", "GET RESPONSE APDU");
}
{
// the class byte (TS 102 221 10.1.1, tables 10.3 and 10.4a): the
// channel itself for 0 to 3, '4X' with the channel less four for 4
// to 19
Check(ClassByte(0) == 0x00 && ClassByte(1) == 0x01 && ClassByte(3) == 0x03, "class byte of a standard channel is the channel");
Check(ClassByte(4) == 0x40, "class byte of channel 4 is 40");
Check(ClassByte(5) == 0x41, "class byte of channel 5 is 41");
Check(ClassByte(19) == 0x4F, "class byte of channel 19 is 4f");
std::vector<std::uint8_t> rnd(16, 0x11), autn(16, 0x22);
Check(BuildAkaApdu(3, rnd, autn).starts_with("0388008122"), "AUTHENTICATE on channel 3 starts 03");
Check(BuildAkaApdu(4, rnd, autn).starts_with("4088008122"), "AUTHENTICATE on channel 4 starts 40, not 04");
Check(BuildAkaApdu(4, rnd, autn).substr(2) == BuildAkaApdu(1, rnd, autn).substr(2), "the channel changes the class byte and nothing else");
Check(BuildGetResponseApdu(7, 0x35) == "43c0000035", "GET RESPONSE on channel 7 starts 43");
}
{
// DB | 08 RES | 10 CK | 10 IK | SW1 SW2(9000)
std::vector<std::uint8_t> body = {0xDB, 0x08};