Skip to content
Snippets Groups Projects
BQP.md 10.6 KiB
Newer Older
akwizgran's avatar
akwizgran committed
# Bramble QR Code Protocol, version 1

## 1 Introduction

Bramble QR Code Protocol (BQP) is a key agreement protocol that establishes an ephemeral symmetric key between a pair of devices. The devices must be in proximity to each other and equipped with screens and cameras. They must also have a short-range transport over which they can communicate, such as Bluetooth or Wi-Fi. The transport is not required to provide any security properties.

### 1.1 Outline

Each device displays a QR code containing a commitment to an ephemeral public key and information about how to connect to the device over various short-range transports. The devices scan each other's codes and use the transport information to establish an insecure connection. The devices then exchange public keys matching their commitments over the insecure connection.

Each device derives a shared secret from its own private key and the received public key, and a master key is derived from the shared secret. The master key may be used to derive keys for communicating securely over the transport connection, or for other purposes.

### 1.2 Adversary Model 

We assume the adversary can read, modify, delete and insert traffic on all transports at will.

### 1.3 Man-in-the-Middle Attacks

The initial exchange of QR codes is assumed to be secure against man-in-the-middle attacks because the users can see which devices they are scanning. This allows each user to be sure that the content of the QR code they have scanned was provided by the person with whom they intend to exchange keys.

Man-in-the-middle attacks against the subsequent exchange of public keys over the insecure connection can be detected by comparing the keys to the commitments contained in the QR codes.

### 1.4 Cryptographic Primitives

**Notation:** || denotes concatenation, double quotes denote an ASCII string, and len(x) denotes the length of x in bytes, represented as a 32-bit integer. All integers in BQP are big-endian.

BQP uses the following cryptographic primitives:

1. A **cryptographic hash function**, H(m)

2. A **key agreement function**, DH(pri, pub), where pri is one party's private key and pub is the other party's public key

3. A **message authentication code**, MAC(k, m), which must be a pseudo-random function

We use H(m) to define a multi-argument hash function:

- HASH(x\_1, ..., x\_n) = H(len(x\_1) || x\_1 || ... || len(x\_n) || x\_n)

We use MAC(k, m) to define a key derivation function:

- KDF(k, x\_1, ..., x\_n) = MAC(k, len(x\_1) || x\_1 || ... || len(x\_n) || x\_n)

All hashes are HASH\_LEN bytes, all symmetric keys are KEY\_LEN bytes, and the output of MAC(k, m) is MAC\_LEN bytes. For simplicity we require that HASH\_LEN = KEY\_LEN = MAC\_LEN.

(*Note:* The current version of the protocol uses BLAKE2s as the hash function and keyed BLAKE2s as the message authentication code. This gives HASH\_LEN = KEY\_LEN = MAC\_LEN = 32. The key agreement function is elliptic curve Diffie-Hellman with cofactor multiplication (ECDHC) using the brainpoolp256r1 curve.)

### 1.5 Protocol Phases

Each run of the protocol has four phases: preparation, connection establishment, key agreement, and master key derivation. The phases are described in the following sections.

## 2 Preparation Phase

### 2.1 Key Generation

Each device starts by generating a fresh **ephemeral key pair** (pri, pub). The device's **public key commitment** is the first COMMIT\_LEN bytes of HASH("COMMIT", pub). We require that COMMIT\_LEN ≤ HASH\_LEN.

(*Note:* In the current version of the protocol, COMMIT\_LEN = 16. This is sufficient for 128-bit security because the adversary cannot make use of the birthday paradox: a successful man-in-the-middle attack requires a public key with a commitment that matches the victim's commitment, rather than two public keys with commitments that match each other.)

### 2.2 Scan Payloads

BQP uses **Bramble Data Format (BDF)** to represent structured data. BDF has six primitive types (null, boolean, integer, float, string, raw) and two container types (list, dictionary). BDF is specified in a separate document.

Each device creates a **scan payload**, which is a BDF list with three elements. The first element is the protocol version, represented as an integer. The second element is the public key commitment, represented as a raw. The third element is a list of **transport descriptors**, each of which is a list with two elements: a **transport identifier**, represented as a string, and a set of **transport properties**, represented as a dictionary.

The current version of the protocol is 1. If a device scans a QR code with any other protocol version, it must abort the protocol.

Each transport descriptor describes how to connect to the device over a short-range transport. Descriptors for the currently supported transports are defined below.

The device encodes the scan payload using Base64 and uses the resulting string to create a QR code, which it displays.

## 3 Connection Establishment Phase

Each device scans the other device's QR code and extracts the scan payload.

The device may immediately connect to the other device over any transports supported by both devices, simultaneously or in any order. This may result in one or more transport connections being established by either or both of the devices.

If no incoming or outgoing connection is established within CONNECTION\_TIMEOUT seconds of scanning the QR code, the device aborts the protocol. (*Note:* The current version of BQP uses CONNECTION\_TIMEOUT = 20 seconds, which provides a reasonable tradeoff between reliability and responsiveness.)

In the remainder of the protocol, the devices are assigned roles according to the lexicographic order of their public key commitments: the device with the earlier commitment plays the role of **Alice**, while the device with the later commitment plays the role of **Bob**. Each device can determine its role after decoding the other device's scan payload. The roles are identical except for some key derivation constants. It does not matter which device plays which role, as long as one is Alice and the other Bob.

If multiple transport connections are established, Alice decides which connection to use and closes any other connections. Bob waits for Alice to start communicating over one of the connections and then closes any other connections.

## 4 Key Agreement Phase

### 4.1 Records

Alice and Bob exchange a series of **records** over the connection chosen by Alice. Each record starts with a four-byte header with the following format:

- Bits 0-7: Protocol version

- Bits 8-15: Record type

- Bits 16-31: Length of the payload in bytes as a 16-bit integer

The current version of the protocol is 1, which has three record types:

**0: KEY** - The payload consists of the sender's ephemeral public key.

**1: CONFIRM** - The payload consists of a confirmation code.

**2: ABORT** - The payload is empty.

When a device receives an ABORT record it must respond with an ABORT record (unless it has already sent one) and abort the protocol.

Devices must ignore any records with unrecognised protocol versions or record types without aborting the protocol.

### 4.2 Key Exchange

Alice begins by sending a KEY record containing her ephemeral public key, pub\_a. Bob compares the first COMMIT\_LEN bytes of HASH("COMMIT", pub\_a) to the commitment in Alice's scan payload. If the key does not match the commitment, Bob sends an ABORT record and aborts the protocol.

Bob sends a KEY record containing his ephemeral public key, pub\_b. Alice compares the first COMMIT\_LEN bytes of HASH("COMMIT", pub\_b) to the commitment in Bob's scan payload code. If the key does not match the commitment, Alice sends an ABORT record and aborts the protocol.

Alice and Bob calculate the shared secret as follows:

- Alice calculates s = HASH("SHARED\_SECRET", DH(pri\_a, pub\_b), pub\_a, pub\_b)

- Bob calculates s = HASH("SHARED\_SECRET", DH(pri\_b, pub\_a), pub\_a, pub\_b)

If the adversary has not modified the KEY records, both parties will calculate the same shared secret.

### 4.3 Confirmation

Each device knows it has received the correct public key because it has compared the received public key to the commitment in the other device's scan payload. However, each device also needs to know that the other device has received the correct public key. To confirm this, both devices derive a **confirmation key** from the shared secret and use it to calculate two message authentication codes over the binary scan payloads, q\_a and q\_b, and the public keys, pub\_a and pub\_b:

- confirmation_key = KDF(s, "CONFIRMATION\_KEY")

- confirm\_a = KDF(confirmation_key, q\_a, pub\_a, q\_b, pub\_b)

- confirm\_b = KDF(confirmation_key, q\_b, pub\_b, q\_a, pub\_a)

Alice sends a CONFIRM record containing confirm\_a. Bob compares the received confirm\_a to the confirm\_a he calculated. If the values do not match, Bob sends an ABORT record and aborts the protocol.

Bob sends a CONFIRM record containing confirm\_b. Alice compares the received confirm\_b to the confirm\_b she calculated. If the values do not match, Alice sends an ABORT record and aborts the protocol.

## 5 Master Key Derivation Phase

Finally, each device derives the master key from the shared secret:

- master_key = KDF(s, "MASTER\_KEY")

The devices must then erase the shared secret.

The master key is retured to the calling application, together with the transport connection and a flag indicating whether the device played the role of Alice or Bob. The application may use the master key and flag to derive keys for communicating securely over the transport connection, or for any other purpose.

## 6 Transport Descriptors

The current version of the protocol supports the following transports:

### 6.1 Bluetooth

The device registers a Bluetooth service to accept RFCOMM connections. The service UUID is generated by converting the first 16 bytes of the device's public key commitment into a UUID, as specified in section 4.4 of RFC 4122. The service is unregistered when the protocol terminates.

The transport identifier is "bt". The descriptor contains one property:

- "address": The device's Bluetooth address in colon-separated hex format

### 6.2 Local Area Network

The device connects to a local area network and opens a port to accept TCP connections. The port is closed when the protocol terminates.

The transport identifier is "lan". The descriptor contains one property:

- "ipPort": The device's IP address and port number, separated by a colon. The address must be a link-local or site-local IPv4 address in dotted-quad format

Devices must ignore any transport descriptors with unrecognised identifiers, and any unrecognised properties of descriptors with recognised identifiers, without aborting the protocol.