Blog

Secure pairing: how Timmy connects two devices

How ECDH, the security number and encrypted signaling work together.

Before Baby Monitor Timmy can stream audio or video, the two devices need to find each other and check who they are connecting to. Here I explain how Timmy pairs two phones, the cryptography involved, and how comparing the security numbers can help reveal tampering with the key exchange.

The challenge: who is my device talking to?

When two devices connect for the first time, one question matters: is device A really communicating with device B, or is someone acting as an intermediary? In cryptography, this is known as a man-in-the-middle attack (MITM).

Timmy combines an Elliptic Curve Diffie–Hellman (ECDH) key exchange through Firebase with a security number that you compare on both devices.

This diagram gives an overview of the pairing process:

01From discovery to confirmation
Pairing from device discovery and public-key exchange to comparing the security numbers and confirming on each device.
The pairing process at a glance. Private keys and the shared secret stay on the devices.Open diagram ↗

Step 1: each device creates a key pair

At the start of a new pairing attempt, each device generates an ephemeral ECDH key pair on the P-256 curve (secp256r1):

Timmy uses Random.secure() to initialize the random number generator used to create the keys. Each ECDH key pair belongs to that particular pairing attempt.

Step 2: exchange public keys through Firebase

Timmy uses a four-character code as a rendezvous point for the devices. Automatic discovery finds the code on the local network using DNS-SD/mDNS; you can also enter it manually. The code determines which Firestore document the devices use for the key exchange. The cryptographic keys are generated separately.

Once both devices know the code, each writes its public ECDH key to a shared Firestore document. Each device then reads the other device’s public key from that document.

Only the public key is sent during the key exchange. The private key stays on the device. Under the security assumptions of ECDH, knowing the public keys alone does not make it computationally feasible to derive the shared secret.

Step 3: calculate a shared secret

After reading each other’s public key from Firebase, both devices independently calculate the same shared secret in a normal exchange:

sharedSecret = ECDH(myPrivateKey, theirPublicKey)
             → 32 bytes (the same on both devices)

The mathematics of elliptic curves makes both calculations produce the same result, even though each device knows only its own private key and the other device’s public key.

Step 4: compare the security number (SAS)

Each device derives a Short Authentication String (SAS) from the shared secret and both public keys. Timmy displays it as a two-digit security number.

[pubkey1, pubkey2] = sort(publicKeysAsStrings)
prefix = UTF-8("sas:" + pubkey1 + ":" + pubkey2 + ":")
hash = SHA-256(prefix + sharedSecret)
number = (hash[0] × 256 + hash[1]) mod 100   → 00 to 99

In a normal exchange, both devices display the same number, such as 42. Compare the numbers on the actual screens. Confirm separately on each device if they match and you intended to pair these devices.

How the security number helps detect tampering

If an attacker can replace the public keys used in the exchange, they could try to establish a separate secret with each device:

  1. Intercept and replace the genuine public keys in Firebase
  2. Use their own key pairs to perform a separate exchange with each device
02What a tampered key exchange can look like
An attacker replaces public keys. In this example the two phones display different security numbers.
An example with different security numbers. Different exchanges can also produce matching numbers.Open diagram ↗

In this example, the attacker establishes secret S_A with device A and a different secret, S_B, with device B. The devices display 73 and 18. Comparing the screens can reveal the mismatch.

Two different exchanges can produce the same two-digit number. The number is therefore an additional check. Cancel pairing if the numbers differ or the request is unexpected.

Step 5: complete pairing

Each device completes its part after you confirm on that device:

  1. Derive a pairing key represented as 64 hexadecimal characters from the shared secret: SHA-256("pair:" + sharedSecret) → pairingKey
  2. Derive the Firestore document key from the pairing key: SHA-256("doc:" + pairingKey) → documentKey
  3. Use another hash to derive the AES-256-GCM key for encrypted signaling: SHA-256("enc:" + pairingKey) → encryptionKey
  4. Each device stores its derived pairing data locally. In a normal exchange, both devices derive the same pairing key after you confirm on each one.

For later sessions, AES-256-GCM protects the SDP payloads and ICE candidate text stored in Firestore. In the flow described here, the pairing key stays on the device. A separate document identifier is derived from it for routing. WebRTC protects media traffic independently.

How pairing and streaming fit together

Local discovery, Firestore and WebRTC perform different jobs. This diagram separates the data paths, from finding the pairing code and exchanging keys to streaming audio and video.

03Three jobs, separate data paths
Separate paths for local discovery, pairing data and encrypted signaling through Firestore, and protected WebRTC media traffic.
Device discovery, signaling and media transport serve different purposes.Open diagram ↗

The communication paths in more detail:

Which components work together

The same architecture can also be viewed in terms of its components. Both phones use Firebase for anonymous authentication, exchanges through Firestore, and requests for temporary TURN credentials. STUN helps discover address mappings; when needed, a TURN relay forwards the protected WebRTC traffic. The component diagram below shows how these pieces fit together.

04Two devices and the services they use
Baby and parent devices, Firebase Authentication, Firestore, Cloud Functions, STUN and TURN, with separate paths for signaling and protected media.
The components and their connections. Authentication and connection setup are separate from the media path.Open diagram ↗

If discovery fails: enter the code manually

If automatic discovery does not find the other device, you can enter the four-character code manually. The devices then use the same ECDH exchange through Firestore and display a security number for comparison. What changes is how the code reaches the second device.

The security mechanisms at a glance

Mechanism Purpose
ECDH key exchange (P-256) Derive a shared secret from the local private key and the received public key
Ephemeral key pairs Use a new ECDH key pair for each new pairing attempt
Security number (SAS) Compare the screens as an additional check
SHA-256 hash as a document identifier Address a document using a derived identifier
AES-256-GCM encryption Protect SDP payloads and ICE candidate text
Confirmation on each device Store pairing data on each device after confirmation
DTLS-SRTP (WebRTC) Protect audio and video in transit

The code brings the devices together. ECDH provides the shared secret used to derive keys, and comparing the security numbers helps check who is at the other end. AES-256-GCM then protects selected signaling data, while WebRTC protects the media separately.


More from the blog