π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Harden 2of3 share checksum
Manav Rathi6681d8370a37
diff --git a/docs/docs/2of3/faq.md b/docs/docs/2of3/faq.mdindex e545a7e010..b2d74d5ac2 100644--- a/docs/docs/2of3/faq.md+++ b/docs/docs/2of3/faq.md@@ -75,6 +75,10 @@ Open 2of3 at [2of3.ente.com](https://2of3.ente.com), or use the offline recoveryThe full walkthrough lives in [Getting started](/2of3/getting-started#recover-the-secret).+### The recovery page says "This share was created by a newer format". {#2of3-old-recovery-page}++Your saved `2of3-recovery.html` is older than the cards you are recovering. Recover on [2of3.ente.com](https://2of3.ente.com) instead. Learn more in [Format compatibility](/2of3/how-it-works#format-compatibility).+### Can I recover from a photo of a printed card? {#2of3-recover-from-photo}Yes. 2of3's QR reader tries several crops automatically, so a phone photo with the whole card in frame usually decodes on the first try. If a particular photo does not decode, take another with even lighting, no glare on the QR, and the whole card clearly in frame, or try a tighter crop on the QR.@@ -103,7 +107,7 @@ The two cards you uploaded share the same card number. You probably uploaded the### The recovery flow says "These shares did not reconstruct a valid secret". {#2of3-bad-reconstruct}-Both cards parsed fine but combined into bytes that did not match the checksum stored on each card. This usually means at least one card has been corrupted: a misread QR, a transcribed text code with a typo, or a faint print where the decoder picked up wrong bits.+Both cards parsed fine but combined into bytes that did not match the checksum recovered with the secret. This usually means at least one card has been corrupted: a misread QR, a transcribed text code with a typo, or a faint print where the decoder picked up wrong bits.Try a different pair of cards. With three cards in a set there are three possible pairs (1+2, 1+3, 2+3); if exactly one card is bad, the pair that avoids the bad card will still recover the secret.@@ -121,7 +125,7 @@ No. Your secret is split and recovered in your browser. There is no account, no### Can one card alone reveal my secret? {#2of3-one-card-leak}-No. A single card is mathematically harmless on its own. It looks like random data because that is what it is: the secret XOR-ed with a uniformly random byte (per byte). Knowing one card gives the same information about the original secret as knowing zero cards: none.+No. A single card's share data is mathematically harmless on its own. It looks random because that is what it is: the protected data XOR-ed with a uniformly random byte (per byte). The card reveals the secret's byte length, but nothing about its contents.You need any two of the three before the secret can be reconstructed.@@ -149,7 +153,7 @@ Yes. 2of3 is part of [Ente's open source repository](https://github.com/ente/ent- Share 2: secret byte XOR (r Γ 2 in GF(256))- Share 3: secret byte XOR (r Γ 3 in GF(256))-GF(256) multiplication uses the AES polynomial (`0x11b`). Any two shares plus their share numbers (1, 2, or 3) recover the secret byte by byte; one share alone is a uniformly random byte from the attacker's point of view.+GF(256) multiplication uses the AES polynomial (`0x11b`). Any two shares plus their share numbers (1, 2, or 3) recover the secret byte by byte; the data in one share is uniformly random from the attacker's point of view.The full walkthrough lives in [How it works](/2of3/how-it-works#shamir-secret-sharing-briefly).diff --git a/docs/docs/2of3/how-it-works.md b/docs/docs/2of3/how-it-works.mdindex 6b69859fb9..f5e5d72a4d 100644--- a/docs/docs/2of3/how-it-works.md+++ b/docs/docs/2of3/how-it-works.md@@ -9,7 +9,7 @@ This page explains what is actually happening when 2of3 splits a secret into thr## Shamir secret sharing, briefly-2of3 is built on **Shamir secret sharing**, a classic cryptographic scheme from 1979. The version 2of3 uses is the simplest interesting case: split a secret into three shares such that any two shares reconstruct the original, and any one share alone leaks nothing.+2of3 is built on **Shamir secret sharing**, a classic cryptographic scheme from 1979. The version 2of3 uses is the simplest interesting case: split a secret into three shares such that any two shares reconstruct the original, while one share reveals nothing about its contents beyond its byte length.> [!TIP]>@@ -47,28 +47,33 @@ Each card carries one share, encoded as a short text code (and rendered as a QRThe `2of3-` prefix is there so a reader can recognize the code at a glance and so 2of3 itself can reject pasted text that is not a share with a clear error message ("That code does not look like a 2of3 share.").-Everything after the prefix is a base64url-encoded byte payload. The payload has a 14-byte header followed by the share bytes.+Everything after the prefix is a base64url-encoded byte payload. The payload has a 10-byte header followed by the share bytes.### Share formatReading from the start, the payload is:-| Bytes | Field | What it means |-| ----- | ---------- | ------------------------------------------------------------------------------------------------------------------------ |-| 0 | Version | Always `1` in the current format. Future versions can use a different number to introduce a breaking change. |-| 1 | Card index | `1`, `2`, or `3`. This is the "x value" used during recovery to combine shares. |-| 2-3 | Length | A 16-bit big-endian length of the secret in bytes. Both shares in a recovery must agree on this. |-| 4-9 | Random ID | 6 bytes of `crypto.getRandomValues` output, generated fresh for each set. All three cards from one secret share this ID. |-| 10-13 | Checksum | A 32-bit checksum of the original secret bytes, computed before splitting. |-| 14+ | Share data | The share bytes themselves. The same length as the original secret. |+| Bytes | Field | What it means |+| ----- | ---------- | ---------------------------------------------------------------------------------------------------------------------------------- |+| 0 | Version | Always `2` in the current format. Recovery also supports cards created with version 1. |+| 1 | Card index | `1`, `2`, or `3`. This is the "x value" used during recovery to combine shares. |+| 2-3 | Length | A 16-bit big-endian length of the secret in bytes. Both shares in a recovery must agree on this. |+| 4-9 | Random ID | 6 bytes of `crypto.getRandomValues` output, generated fresh for each set. All three cards from one secret share this ID. |+| 10+ | Share data | A share of the secret bytes followed by a 4-byte checksum. The checksum is protected by the same two-card threshold as the secret. |-The ID and checksum are what let recovery be safe and helpful:+The ID and protected checksum are what let recovery be safe and helpful:- The **random ID** is used to detect mismatched cards. If you try to combine a card from one set with a card from a different set, both cards parse fine but their IDs disagree, and 2of3 stops with "These two cards are from different sets. Match the ID on both cards." The card's UI shows the first 8 characters of the base64url-encoded ID as the human-readable fingerprint (for example, `ID A1B2C3D4`) so you can match cards by eye too.-- The **checksum** is used to detect corrupted cards. After combining, 2of3 recomputes the checksum of the recovered bytes and compares it against the checksum stored on each card. If they disagree, you get "These shares did not reconstruct a valid secret." instead of silently returning garbage. The checksum is a simple FNV-1a 32-bit hash, not a cryptographic MAC, because the ID and the share data are not secret and there is no one for an attacker to lie to. The checksum is there to catch accidents like a misread QR or a transcription error.+- The **checksum** is used to detect corrupted cards. Before splitting, 2of3 appends a 32-bit FNV-1a checksum to the secret. After combining two cards, it recomputes the checksum and compares it with the recovered value. If they disagree, you get "These shares did not reconstruct a valid secret." instead of silently returning garbage. The checksum is protected by the split, so one card cannot use it to test guesses about the secret.The maximum secret size 2of3 will accept is 2048 bytes in principle, but the practical limit is whatever still fits as a readable QR plus four lines of printed text on a card, currently about 200 bytes of text. The byte counter under the **Secret** field in the UI shows the live limit.+### Format compatibility++Earlier releases created version 1 cards, which stored the checksum openly and allowed one card to confirm guesses of a low-entropy secret. Version 2 protects the checksum behind the two-card threshold.++Recovery supports both versions (if you have an older offline recovery page, generate a new set to get a current one, or recover on [2of3.ente.com](https://2of3.ente.com)). If a version 1 card may have been exposed and the secret is guessable, rotate the secret before creating new cards.+## Offline recovery fileWhen you click **Download all cards**, 2of3 includes one extra file alongside the three card PNGs: `2of3-recovery.html`. This is an offline copy of the recovery flow.diff --git a/docs/docs/2of3/index.md b/docs/docs/2of3/index.mdindex c843147109..964fc45cbd 100644--- a/docs/docs/2of3/index.md+++ b/docs/docs/2of3/index.md@@ -1,11 +1,11 @@---title: 2of3-description: Split one important secret into 3 recovery cards. Any 2 cards bring it back. One card alone reveals nothing.+description: Split one important secret into 3 recovery cards. Any 2 cards bring it back. One card alone cannot reconstruct it.---# 2of3-2of3 turns one important secret into three recovery cards. Any two of the three cards can reconstruct the secret. A single card by itself reveals nothing.+2of3 turns one important secret into three recovery cards. Any two of the three cards can reconstruct the secret. A single card cannot reconstruct it.It is a free, end-to-end private web app from Ente. Open it at [2of3.ente.com](https://2of3.ente.com). There is no account, nothing to install, and your secret never leaves your browser.@@ -44,7 +44,7 @@ When you enter a secret, 2of3 generates three cards in your browser. Each card cThe cards from one secret all share the same ID. The ID is how recovery tells "two cards from the same set" apart from "two cards that happen to look similar". The card number lets the recovery code know which two of the three you are using.-A single card is mathematically harmless on its own. It looks like random data, because that is exactly what it is. Knowing one card gives the same information about the original secret as knowing zero cards: none. You need any two of the three before the secret can be put back together.+A single card is mathematically harmless on its own. Its share data looks random, because that is exactly what it is. It reveals the secret's byte length, but nothing about its contents. You need any two of the three before the secret can be put back together.## What you do with the cardsdiff --git a/web/apps/twoof3/src/features/twoof3/utils/export.ts b/web/apps/twoof3/src/features/twoof3/utils/export.tsindex 967641e941..fe557dc522 100644--- a/web/apps/twoof3/src/features/twoof3/utils/export.ts+++ b/web/apps/twoof3/src/features/twoof3/utils/export.ts@@ -662,8 +662,11 @@ export const createOfflineRecoveryHtml = async () => {<script>${escapeScriptTagContent(OFFLINE_QR_DECODER_SOURCE)}</script><script>const GF_POLY = 0x11b;- const VERSION = 1;- const HEADER_LENGTH = 14;+ const VERSION = 2;+ const HEADER_LENGTH = 10;+ const LEGACY_HEADER_LENGTH = 14;+ const CHECKSUM_LENGTH = 4;+ const SHARE_OVERHEAD = HEADER_LENGTH + CHECKSUM_LENGTH;const gfMul = (left, right) => {let result = 0;let a = left;@@ -714,41 +717,45 @@ export const createOfflineRecoveryHtml = async () => {const encoded = input.replace(/\\s+/gu, "");if (!encoded.startsWith("2of3-")) throw new Error("That code does not look like a 2of3 share.");const payload = base64UrlDecode(encoded.slice(5));- if (payload.length <= HEADER_LENGTH) throw new Error("That share looks incomplete.");+ if (payload.length <= SHARE_OVERHEAD) throw new Error("That share looks incomplete.");const version = payload[0];const index = payload[1];const length = ((payload[2] || 0) << 8) | (payload[3] || 0);- if (version !== VERSION) throw new Error("This share was created by a newer format.");+ if (version !== 1 && version !== VERSION) throw new Error(version > VERSION ? "This card uses a newer format than this recovery page. Recover at 2of3.ente.com." : "This share uses an unsupported format version.");if (![1,2,3].includes(index)) throw new Error("This share number is invalid.");- if (payload.length !== HEADER_LENGTH + length) throw new Error("This share was cut off.");+ const headerLength = version === 1 ? LEGACY_HEADER_LENGTH : HEADER_LENGTH;+ const dataLength = length + (version === 1 ? 0 : CHECKSUM_LENGTH);+ if (payload.length !== headerLength + dataLength) throw new Error("This share was cut off.");return {- checksum: payload.slice(10, 14),- data: payload.slice(14),+ checksum: version === 1 ? payload.slice(10, 14) : null,+ data: payload.slice(headerLength),id: base64UrlEncode(payload.slice(4, 10)),index,length,+ version,};};const combineShares = (firstInput, secondInput) => {const first = parseShare(firstInput);const second = parseShare(secondInput);+ if (first.version !== second.version) throw new Error("These two cards use different formats.");if (first.id !== second.id || first.length !== second.length) throw new Error("These two cards are from different sets. Match the ID on both cards.");if (first.index === second.index) throw new Error("Use two different cards from the same set.");- const output = new Uint8Array(first.length);+ const output = new Uint8Array(first.data.length);const denominator = first.index ^ second.index;- for (let index = 0; index < first.length; index++) {+ for (let index = 0; index < output.length; index++) {const left = gfMul(first.data[index], gfDiv(second.index, denominator));const right = gfMul(second.data[index], gfDiv(first.index, denominator));output[index] = left ^ right;}- const expectedChecksum = checksumBytes(output);- for (let i = 0; i < expectedChecksum.length; i++) {- if (expectedChecksum[i] !== first.checksum[i] || expectedChecksum[i] !== second.checksum[i]) {- throw new Error("These shares did not reconstruct a valid secret.");- }- }+ const secretBytes = output.slice(0, first.length);+ const expectedChecksum = checksumBytes(secretBytes);+ const checksumMatches = first.version === 1+ ? expectedChecksum.every((byte, index) => byte === first.checksum[index] && byte === second.checksum[index])+ : expectedChecksum.every((byte, index) => byte === output[first.length + index]);+ if (!checksumMatches) throw new Error("These shares did not reconstruct a valid secret.");try {- return new TextDecoder("utf-8", { fatal: true }).decode(output);+ return new TextDecoder("utf-8", { fatal: true }).decode(secretBytes);} catch {throw new Error("These shares did not reconstruct readable text.");}diff --git a/web/apps/twoof3/src/features/twoof3/utils/shamir.ts b/web/apps/twoof3/src/features/twoof3/utils/shamir.tsindex e3136d0b5d..da23e9c617 100644--- a/web/apps/twoof3/src/features/twoof3/utils/shamir.ts+++ b/web/apps/twoof3/src/features/twoof3/utils/shamir.ts@@ -1,6 +1,9 @@const GF_POLY = 0x11b;-const VERSION = 1;-const HEADER_LENGTH = 14;+const VERSION = 2;+const HEADER_LENGTH = 10;+const LEGACY_HEADER_LENGTH = 14;+const CHECKSUM_LENGTH = 4;+const SHARE_OVERHEAD = HEADER_LENGTH + CHECKSUM_LENGTH;const ID_LENGTH = 6;const MAX_SECRET_BYTES = 2048;export const SHARE_PREFIX = "2of3-";@@ -104,16 +107,17 @@ export interface SplitShare {}export interface ParsedShare {- checksum: Uint8Array;+ checksum: Uint8Array | null;data: Uint8Array;encoded: string;id: string;index: 1 | 2 | 3;length: number;+ version: 1 | 2;}export const encodedShareLengthForSecretBytes = (secretByteLength: number) =>- SHARE_PREFIX.length + base64UrlLength(HEADER_LENGTH + secretByteLength);+ SHARE_PREFIX.length + base64UrlLength(SHARE_OVERHEAD + secretByteLength);export const maxSecretBytesForEncodedShareLength = (maxShareLength: number) => {let best = 0;@@ -146,18 +150,18 @@ export const splitSecret = (secret: string): SplitShare[] => {}const randomId = crypto.getRandomValues(new Uint8Array(ID_LENGTH));- const checksum = checksumBytes(secretBytes);+ const protectedBytes = joinBytes(secretBytes, checksumBytes(secretBytes));const coefficients = crypto.getRandomValues(- new Uint8Array(secretBytes.length),+ new Uint8Array(protectedBytes.length),);const shares = [- new Uint8Array(secretBytes.length),- new Uint8Array(secretBytes.length),- new Uint8Array(secretBytes.length),+ new Uint8Array(protectedBytes.length),+ new Uint8Array(protectedBytes.length),+ new Uint8Array(protectedBytes.length),];- for (let index = 0; index < secretBytes.length; index++) {- const secretByte = secretBytes[index]!;+ for (let index = 0; index < protectedBytes.length; index++) {+ const secretByte = protectedBytes[index]!;const coefficient = coefficients[index]!;shares[0]![index] = secretByte ^ coefficient;shares[1]![index] = secretByte ^ gfMul(coefficient, 2);@@ -173,7 +177,7 @@ export const splitSecret = (secret: string): SplitShare[] => {secretBytes.length & 0xff,]);- const payload = joinBytes(header, randomId, checksum, shareBytes);+ const payload = joinBytes(header, randomId, shareBytes);return { encoded: `${SHARE_PREFIX}${base64UrlEncode(payload)}`, index };});};@@ -185,7 +189,7 @@ export const parseShare = (input: string): ParsedShare => {}const payload = base64UrlDecode(encoded.slice(SHARE_PREFIX.length));- if (payload.length <= HEADER_LENGTH) {+ if (payload.length <= SHARE_OVERHEAD) {throw new Error("That share looks incomplete.");}@@ -193,21 +197,23 @@ export const parseShare = (input: string): ParsedShare => {const index = payload[1];const length = ((payload[2] ?? 0) << 8) | (payload[3] ?? 0);- if (version !== VERSION) {- throw new Error("This share was created by a newer format.");+ if (version !== 1 && version !== VERSION) {+ throw new Error("This share uses an unsupported format version.");}if (index !== 1 && index !== 2 && index !== 3) {throw new Error("This share number is invalid.");}- if (payload.length !== HEADER_LENGTH + length) {+ const headerLength = version === 1 ? LEGACY_HEADER_LENGTH : HEADER_LENGTH;+ const dataLength = length + (version === 1 ? 0 : CHECKSUM_LENGTH);+ if (payload.length !== headerLength + dataLength) {throw new Error("This share was cut off.");}const idBytes = payload.slice(4, 10);- const checksum = payload.slice(10, 14);- const data = payload.slice(14);+ const checksum = version === 1 ? payload.slice(10, 14) : null;+ const data = payload.slice(headerLength);return {checksum,@@ -216,6 +222,7 @@ export const parseShare = (input: string): ParsedShare => {id: base64UrlEncode(idBytes),index,length,+ version,};};@@ -223,6 +230,10 @@ export const combineShares = (firstInput: string, secondInput: string) => {const first = parseShare(firstInput);const second = parseShare(secondInput);+ if (first.version !== second.version) {+ throw new Error("These two cards use different formats.");+ }+if (first.id !== second.id || first.length !== second.length) {throw new Error("These two cards are from different sets. Match the ID on both cards.",@@ -233,10 +244,10 @@ export const combineShares = (firstInput: string, secondInput: string) => {throw new Error("Use two different cards from the same set.");}- const output = new Uint8Array(first.length);+ const output = new Uint8Array(first.data.length);const denominator = first.index ^ second.index;- for (let index = 0; index < first.length; index++) {+ for (let index = 0; index < output.length; index++) {const left = gfMul(first.data[index]!,gfDiv(second.index, denominator),@@ -248,19 +259,24 @@ export const combineShares = (firstInput: string, secondInput: string) => {output[index] = left ^ right;}- const expectedChecksum = checksumBytes(output);- if (- expectedChecksum.some(- (byte, index) =>- byte !== first.checksum[index] ||- byte !== second.checksum[index],- )- ) {+ const secretBytes = output.slice(0, first.length);+ const expectedChecksum = checksumBytes(secretBytes);+ const checksumMatches =+ first.version === 1+ ? expectedChecksum.every(+ (byte, index) =>+ byte === first.checksum![index] &&+ byte === second.checksum![index],+ )+ : expectedChecksum.every(+ (byte, index) => byte === output[first.length + index],+ );+ if (!checksumMatches) {throw new Error("These shares did not reconstruct a valid secret.");}try {- return new TextDecoder("utf-8", { fatal: true }).decode(output);+ return new TextDecoder("utf-8", { fatal: true }).decode(secretBytes);} catch {throw new Error("These shares did not reconstruct readable text.");}diff --git a/web/apps/twoof3/src/pages/index.tsx b/web/apps/twoof3/src/pages/index.tsxindex dbf5e7822f..f8dae6b8f8 100644--- a/web/apps/twoof3/src/pages/index.tsx+++ b/web/apps/twoof3/src/pages/index.tsx@@ -180,7 +180,7 @@ const HELP_FAQS = [},{question: "All this sounds magical, how does it work?",- answer: "It uses Shamir secret sharing. Your secret is split into three shares in a way where mathematically any two parts reconstruct it while one alone reveals nothing. Think of it like a lock which needs 2 of 3 keys to open.",+ answer: "It uses Shamir secret sharing. Your secret is split into three shares in a way where mathematically any two parts reconstruct it while one alone reveals nothing about its contents. Think of it like a lock which needs 2 of 3 keys to open.",},{question: "Should I test recovery before storing the cards?",