Section 7.24

Numeric comparison

Definition

Numeric comparison is an association model of BLE §7.1 (Bluetooth Low Energy) pairing §7.17: one of the alternative ways in which pairing checks that a new shared key is shared with the intended device. Each device displays a six-digit number computed from the public keys and random values it sent and received, and the user confirms on both devices that the numbers are equal. If an attacker altered the exchange, the numbers differ, except with probability about one in a million. Numeric comparison exists only in LE Secure Connections §7.21 pairing.

The idea

Under LE Secure Connections (LE stands for Low Energy), two devices start pairing by exchanging public keys for ECDH §19.1.5 (elliptic-curve Diffie–Hellman key agreement). Each device keeps a private number, sends a public key derived from it, and combines its own private number with the other’s public key; both arrive at the same shared secret, which a listener cannot compute. An active attacker can do more than listen. The attacker can replace each device’s public key with the attacker’s own, finish one exchange with each device, and relay traffic between them: a man-in-the-middle §7.26 (MITM) attack. Each device then holds a sound key that it shares with the attacker.

The devices cannot detect the substitution by radio, because any radio message may come from the attacker. They can detect it with the user’s help. Each device reduces the public keys and random values it saw to a short number. Without interference, both devices saw the same values and show the same number. After a substitution, device A saw its own key and the attacker’s, device B saw the attacker’s key and its own, and the numbers differ. The user’s eyes bring the two numbers together over a path the attacker cannot reach, much as two people read out a file’s checksum §4.32 on a telephone call to confirm that the file arrived unchanged. The comparison stops working if the attacker can choose the attacker’s own values after seeing the devices’ values, because a short number can then be steered to match; the protocol’s commitment step prevents exactly that.

How it works

Numeric comparison is selected in the first phase of pairing when both devices support LE Secure Connections, at least one of them requests MITM protection, neither has out-of-band data, and both can display a number and accept a yes or no answer. Let A be the initiating device, the one that started the pairing, and B the responding device.

  1. A and B exchange ECDH public keys on the elliptic curve named P-256. Write PAP_A and PBP_B for the public keys as each device sent or received them.

  2. Each device picks a nonce §5.18: a fresh 128-bit random value used for this pairing only. Call them NAN_A and NBN_B.

  3. B sends a commitment CBC_B to its nonce: a value computed from PBP_B, PAP_A and NBN_B with a keyed function built from the AES (Advanced Encryption Standard) block cipher §15.3. The commitment fixes NBN_B without revealing it.

  4. A sends NAN_A. Then B sends NBN_B, and A recomputes CBC_B and aborts if the result differs.

  5. Each device computes the function that the specification calls g2g_2 over PAP_A, PBP_B, NAN_A and NBN_B. Its output is a 32-bit number. The device displays the remainder of that number after division by 10610^6, as six decimal digits including leading zeros.

  6. The user compares the two numbers and presses yes on each device, or no if they differ. A no on either device fails the pairing.

  7. Both devices derive the long-term key §7.28 (LTK) from the ECDH shared secret, the nonces and their addresses, and exchange check values that prove each computed the same key.

The order of steps 3 and 4 defeats a clever attacker. An attacker in the middle runs one exchange with A and one with B. Toward A, the attacker must send a commitment before A reveals NAN_A. Toward B, the attacker must send a nonce before B reveals NBN_B. So in whichever exchange the attacker completes second, the attacker’s nonce was fixed before the honest device there revealed its nonce. The honest device’s nonce therefore makes the second displayed number random from the attacker’s point of view, and the attacker cannot steer it to equal the first.

Just Works §7.22 under LE Secure Connections sends the same messages but displays nothing and asks nothing. Numeric comparison differs from Just Works only by the user’s comparison.

Worked example

No attacker. Suppose both devices compute g2=𝟶𝚡𝟹𝙰𝙳𝙴𝟼𝟾𝙱𝟷g_2 = \texttt{0x3ADE68B1}, which is 987,654,321 in decimal. Dividing by 10610^6 gives 987 with remainder 654,321, so both screens show 654321. The user sees equal numbers and presses yes on both devices.

An attacker in the middle. The attacker M substituted M’s own public key PMP_M in both directions. A computed its number from PAP_A and PMP_M; B computed its number from PMP_M and PBP_B. A shows 654321 and B shows, say, 089217. A user who compares sees that the first digits already differ and presses no: the pairing fails and the attack is exposed. A user who presses yes on both devices without looking completes two pairings with M, and both devices record the key as authenticated.

The attacker’s chance. M’s only hope is that the second number happens to equal the first. A 32-bit value has 232=4,294,967,2962^{32} = 4{,}294{,}967{,}296 possible values, which is 4294×106+967,2964294 \times 10^6 + 967{,}296. So each six-digit result from 000000 to 967295 arises from 4295 of the 32-bit values, and each other result from 4294. If M cannot steer the value, it lands on one given result with probability at most 4295/4,294,967,296≈1.0000076×10−64295 / 4{,}294{,}967{,}296 \approx 1.0000076 \times 10^{-6}, about one in a million. The specification states the same figure: a simple MITM attack makes the two numbers differ with probability 0.999999.

Stated precisely

Let VAV_A and VBV_B be the numbers that A and B display. Each device computes V=g2(PA,PB,NA,NB)mod106V = g_2(P_A, P_B, N_A, N_B) \bmod 10^6 from the public keys and nonces as it saw them, where xmod106x \bmod 10^6 is the remainder of xx after division by 10610^6. The contract of numeric comparison is that a device marks the new key as authenticated only after its user has confirmed VA=VBV_A = V_B. The guarantee is that an active attacker who substitutes public keys in one pairing attempt goes undetected with probability about 10−610^{-6}. It rests on five assumptions, and an attacker can target each one separately:

  1. the user actually compares the two numbers and rejects a mismatch;

  2. the two numbers compared are on the two devices being paired, not on a third device;

  3. each display shows what its device computed, and each yes is pressed by the user, not by software or an attacker;

  4. the attacker cannot compute the ECDH shared secret from the public keys;

  5. the nonces are fresh 128-bit random values, and the commitment and g2g_2 behave like random functions to anyone who does not know all their inputs.

The six digits need integrity §1.9, not secrecy. An attacker who reads them gains nothing, because they are computed from values that crossed the radio unencrypted, and the attack is detected as soon as the two numbers differ.

Automating the comparison

On a test bench, or in a product with no person present, a program may take the user’s place. What matters is what the program checks and over which path.

In each case the attacker must not control the computer that runs the program. An attacker who does controls both paths at once.

Limits and common mistakes

Automatic acceptance turns numeric comparison into Just Works without changing the record. Each device still marks the key as authenticated, because it cannot tell whether the user looked. A later service that requires an authenticated key, which BLE expresses as security level §7.33 3 or 4, then trusts a key that may be shared with an attacker.

Numeric comparison authenticates the device whose screen the user looked at. If the user picks the attacker’s device from a list of nearby devices, the attacker’s screen shows the matching number, and the comparison correctly succeeds. The name shown in the list is not authenticated.

The six digits are not a password and not a signature. They are public and valid for one pairing only.

The first phase of pairing can be altered. An attacker who changes the unencrypted first messages to claim that one device has no display steers both devices to Just Works, which shows no number. This is a downgrade attack §1.27; the defense is that a device which needs an authenticated key refuses any pairing that does not produce one.

Numeric comparison needs both devices to support LE Secure Connections; LE legacy pairing §7.20 has no such model.

Each attempt gives the attacker about one chance in a million, but every new attempt needs a user to confirm a pairing; a user who accepts pairing requests they did not start gives the attacker more attempts.

Questions answered

What happens if a user automatically accepts without checking?

The pairing still completes, but it loses its protection against a man-in-the-middle (MITM) attacker, while both devices still record the key as authenticated. In numeric comparison, two BLE (Bluetooth Low Energy) devices each display a six-digit number computed from the public keys and random values they exchanged, and the user should confirm only if the numbers are equal. The comparison is the only step that catches an attacker who substituted public keys, because the substitution makes the numbers differ. Without it, the protocol is Just Works. A listener still cannot compute the key, because it comes from elliptic-curve Diffie–Hellman key agreement, but an active attacker in the middle ends up sharing one key with each device and can read and change all traffic.

The false record is the worse effect. Each device stores the key as authenticated, because it cannot see whether the user looked, so every later service that requires an authenticated key accepts the link. Software that presses yes automatically has the same effect. Automation keeps the protection only if it really compares the two numbers, read over paths that the attacker cannot alter.

Related entries: LE Secure Connections (§7.21), MITM attack (§7.26), Nonce (§5.18), Authentication (§1.6), Just Works (§7.22), Passkey entry (§7.23). Further reading: [18].