The circle: joining a device
The circle is the set of a user’s devices that hold the same account key, the AEK. The first device creates it from the
recovery phrase, and each later device joins in four steps. These functions do the
cryptography only: your app moves every payload between the devices, through its backend or through CircleClient
(see Sync). The idea behind the steps is on The circle.
All four functions need a Secret Key.
The steps
Section titled “The steps”- The founding device, once per account.
initCircle(mnemonic, passphrase, secretKey)returns{ wrappedAEK, aekCommitment, deviceId }. StorewrappedAEKandaekCommitmenton the server. - The new device, after it has taken the circle’s commitment.
createJoinRequest(aekCommitment, deviceMeta?)returns{ request, ephemeralPrivateKey, verificationCode }. Sendrequest, and keep the private key and the commitment in memory only. - A trusted device, once both codes match.
authorizeJoin(joinRequest, wrappedAEK, passphrase, secretKey, serverReceivedAt)returns aSealedAEK. Send it to the new device. - The new device, once both codes match.
finalizeJoin(sealedAEK, ephemeralPrivateKey, passphrase, secretKey, aekCommitment)returns{ wrappedAEK }for this device. It throws if the key doesn’t match the commitment.
deriveVerificationCode(ephemeralPublicKey, aekCommitment) gives the trusted device the code to show for a request.
Signatures
Section titled “Signatures”initCircle(mnemonic: string, passphrase: string, secretKey: Uint8Array): Promise<{ wrappedAEK: WrappedAEK aekCommitment: ArrayBuffer deviceId: string}>
createJoinRequest(aekCommitment: ArrayBuffer | Uint8Array, deviceMeta?: { name?: string, platform?: string }): Promise<{ request: JoinRequest ephemeralPrivateKey: CryptoKey verificationCode: string}>
authorizeJoin(joinRequest: JoinRequest, wrappedAEK: WrappedAEK, passphrase: string, secretKey: Uint8Array, serverReceivedAt: number): Promise<SealedAEK>
finalizeJoin(sealedAEK: SealedAEK, ephemeralPrivateKey: CryptoKey, passphrase: string, secretKey: Uint8Array, aekCommitment: ArrayBuffer): Promise<{ wrappedAEK: WrappedAEK }>
deriveVerificationCode(ephemeralPublicKey: JsonWebKey, aekCommitment: ArrayBuffer | Uint8Array): Promise<string>interface WrappedAEK { wrappedKey: ArrayBuffer salt: Uint8Array}
interface JoinRequest { deviceId: string ephemeralPublicKey: JsonWebKey createdAt: string // set by the joining device, for display only deviceMeta?: { name?: string, platform?: string }}
interface SealedAEK { ciphertext: ArrayBuffer iv: Uint8Array ephemeralPublicKey: JsonWebKey // the sealing's own ephemeral key, not the joining device's}createdAt is set by the joining device, so it can be forged: authorizeJoin doesn’t read it.
Example
Section titled “Example”import { generateMnemonic, generateSecretKey, initCircle, createJoinRequest, deriveVerificationCode, authorizeJoin, finalizeJoin,} from '@consealdev/conseal'
// Founding device, once per accountconst mnemonic = generateMnemonic() // show it once, never store itconst secretKey = generateSecretKey() // keep it on this device, with an offline copyconst { wrappedAEK, aekCommitment } = await initCircle(mnemonic, passphrase, secretKey)// Send wrappedAEK and aekCommitment to the server
// New device: it has the commitment from the server before it makes its requestconst { request, ephemeralPrivateKey, verificationCode } = await createJoinRequest(aekCommitment, { name: 'Phone' })// Send request to the server. Show verificationCode. Keep ephemeralPrivateKey and aekCommitment in memory.
// Trusted device: the code for that request, from its own commitmentconst code = await deriveVerificationCode(request.ephemeralPublicKey, aekCommitment)// Show code. Only once the user confirms it matches the new device's:const sealedAEK = await authorizeJoin(request, wrappedAEK, passphrase, secretKey, serverReceivedAt)// Send sealedAEK to the new device
// New device, once the user has confirmed the codes matchconst { wrappedAEK: thisDevice } = await finalizeJoin(sealedAEK, ephemeralPrivateKey, newPassphrase, newSecretKey, aekCommitment)// Store thisDevice. Discard ephemeralPrivateKey.The verification code
Section titled “The verification code”The code is the first 80 bits of a SHA-256 hash over a fixed label, the circle’s commitment and the new device’s
ephemeral public key, written as 16 Crockford base32 characters in four groups, such as 7KQ2-M9XD-4RTF-B8NW. The
alphabet has no I, L, O or U.
The new device takes the commitment before its key pair exists, so whoever supplied the commitment can’t choose it to fit the key. The trusted device uses its own commitment. A server that relays the join can put a key pair of its own in place of the new device’s, or give the new device the commitment of a key of its own, and either one changes a code. To get a match it would need about 2^80 tries, which is out of reach within the five minutes a request lives.
What your app must do
Section titled “What your app must do”The library can’t enforce these:
- Show the code on both devices and require the user to confirm it matches, before
authorizeJoinand again beforefinalizeJoin. Without that confirmation the defence against a server in the middle is gone. - Give the new device the circle’s commitment before
createJoinRequest, and passfinalizeJointhat same commitment. The trusted device derives its code from its own commitment, the oneinitCirclereturned or the one its own join or recovery checked, never one the server supplies with the request. - Pass
serverReceivedAtas the server’s receipt time in Unix milliseconds, neverjoinRequest.createdAt. A request older than 5 minutes is refused. - Treat
aekCommitmentas immutable once it is stored. - Keep the ephemeral private key and the commitment in memory only, and discard the key once
finalizeJoinis done. - Base64-encode the binary fields on the wire:
wrappedKey,salt,ciphertext,ivandaekCommitment.
finalizeJoin throws if the key doesn’t match the commitment. Don’t store or use anything from that call then.
Running the circle on these functions, without CircleClient, has no passphrase check: a server can answer a join with
a key of its own, and the new device finishes only once the user has confirmed matching codes on a trusted device. The
threat model goes through what each layer enforces.