sigelo-contact 0.1.13 → 0.1.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/README.md +13 -10
  2. package/call.mjs +727 -0
  3. package/carrier.mjs +1204 -137
  4. package/control.mjs +81 -22
  5. package/device.mjs +1 -1
  6. package/enrol.mjs +239 -43
  7. package/gen-carrier.mjs +4 -4
  8. package/gen-device.mjs +2 -2
  9. package/gen-envelope.mjs +3 -3
  10. package/gen-invite.mjs +3 -3
  11. package/gen-short.mjs +3 -3
  12. package/group.mjs +616 -0
  13. package/invite.mjs +63 -13
  14. package/kdf.mjs +1 -1
  15. package/link.mjs +24 -16
  16. package/package.json +49 -7
  17. package/page.mjs +1172 -70
  18. package/photo.mjs +37 -0
  19. package/platform-node.mjs +125 -18
  20. package/prekeys.mjs +10 -2
  21. package/public.mjs +93 -0
  22. package/relays.mjs +1 -1
  23. package/restore.mjs +3 -3
  24. package/rtc-web.mjs +95 -0
  25. package/short.mjs +31 -3
  26. package/store.mjs +147 -12
  27. package/test-carrier.mjs +138 -21
  28. package/test-device.mjs +1 -1
  29. package/test-enrol.mjs +157 -8
  30. package/test-envelope.mjs +16 -3
  31. package/test-file.mjs +5 -5
  32. package/test-group.mjs +413 -0
  33. package/test-invite.mjs +15 -2
  34. package/test-link.mjs +11 -8
  35. package/test-platform.mjs +5 -5
  36. package/test-presence.mjs +169 -0
  37. package/test-push.mjs +136 -0
  38. package/test-relay-lane.mjs +248 -0
  39. package/test-short.mjs +38 -12
  40. package/test-tier.mjs +6 -6
  41. package/test-transfer-web.mjs +246 -0
  42. package/test-transfer.mjs +463 -0
  43. package/test-voice.mjs +158 -0
  44. package/test-ws.mjs +5 -5
  45. package/transfer-engine.mjs +800 -0
  46. package/transfer-node.mjs +449 -0
  47. package/transfer-web.mjs +80 -0
  48. package/transfer.mjs +388 -0
  49. package/voice-web.mjs +113 -0
  50. package/voice.mjs +65 -0
  51. package/words-lock.mjs +115 -0
package/words-lock.mjs ADDED
@@ -0,0 +1,115 @@
1
+ // SPDX-License-Identifier: MIT
2
+ // The words password (design/apps-seed.md §8): the 25 words kept on a phone, encrypted under a key made from
3
+ // a password the human chose, so Me → Security can show them again (Monerujo-style, but optional). WebCrypto
4
+ // only, no import: it runs as it is in the Android app's WebView, in the GTK app's WebKit and in Node.
5
+ //
6
+ // lockWords(words, password, { iterations }) → { blob, check } blob: the bytes to seal in the keystore
7
+ // openWords(blob, password) → { words, check } a wrong password throws check words_password
8
+ // proofOf(blob, password) → check, the same refusal: what a shell compares (GTK)
9
+ // wordsMeta(blob) → { v, kdf, iter } (no secret)
10
+ // checkPassword(password, repeat) → the password, or throws check words_password_rule
11
+ // wrongGate({ base, sleep }), gate the delay after a wrong password: 2 s, doubling per failure (at most
12
+ // 256 s) in this page's life, none after a right one; no lockout
13
+ // lockBytes(label, bytes, password, o) → { iter, salt, iv, ct, check }: any bytes, the same KDF and cipher, `label`
14
+ // openBytes(label, sealed, password, aad) → bytes (o.aad: what the caller binds; apps/web/backup.mjs, label sigelo-backup/0)
15
+ //
16
+ // blob: JSON { v: "sigelo-words/0", kdf: "PBKDF2-SHA256", iter, salt, iv, ct } (base64). The password (NFC)
17
+ // goes through PBKDF2-HMAC-SHA256 with a random 16-byte salt and `iter` rounds (ITERATIONS: 1,000,000; about
18
+ // 0.45 s in WebKit on a 2018 phone), 32 bytes out; HKDF-SHA256 of those makes the AES-256-GCM key (info
19
+ // "sigelo-words/0 key") and the check (info "sigelo-words/0 check", 32 bytes, hex). The AAD names the version
20
+ // and the KDF with its rounds. The check is never stored in the blob: a shell that keeps it beside the blob
21
+ // (the GTK app's keyring item) knows the password was typed without seeing the words.
22
+ export const V = 'sigelo-words/0', KDF = 'PBKDF2-SHA256', ITERATIONS = 1_000_000, MIN_ITERATIONS = 1_000, PASSWORD_MIN = 8, WORD_COUNT = 25;
23
+ const te = new TextEncoder(), td = new TextDecoder();
24
+ const refuse = (check, message) => { throw Object.assign(new Error(message), { check }); };
25
+ const b64 = (b) => { let s = ''; for (const x of b) s += String.fromCharCode(x); return btoa(s); };
26
+ const unb64 = (s) => Uint8Array.from(atob(s), (c) => c.charCodeAt(0));
27
+ const hex = (b) => Array.from(b, (x) => x.toString(16).padStart(2, '0')).join('');
28
+ const subtle = () => globalThis.crypto?.subtle ?? refuse('words_crypto', 'this page has no WebCrypto');
29
+ const aad = (iter) => te.encode(`${V} ${KDF} ${iter}`);
30
+
31
+ /** The words as one line: lowercase, one space between, exactly 25 (they are checked by the restore before this). */
32
+ function cleanWords(words) {
33
+ const w = (Array.isArray(words) ? words.join(' ') : String(words ?? '')).trim().toLowerCase().split(/\s+/);
34
+ if (w.length !== WORD_COUNT || w.some((x) => !/^[a-z]+$/.test(x))) refuse('words', `${WORD_COUNT} words`);
35
+ return w.join(' ');
36
+ }
37
+ /** A password as set: at least 8 characters, the repeat the same. → the password. */
38
+ export function checkPassword(password, repeat = password) {
39
+ const p = String(password ?? '');
40
+ if ([...p].length < PASSWORD_MIN) refuse('words_password_rule', `A password of at least ${PASSWORD_MIN} characters.`);
41
+ if (p !== String(repeat ?? '')) refuse('words_password_rule', 'The two passwords are not the same.');
42
+ return p;
43
+ }
44
+ async function keys(password, salt, iter, label = V) {
45
+ const s = subtle();
46
+ const pw = await s.importKey('raw', te.encode(String(password).normalize('NFC')), 'PBKDF2', false, ['deriveBits']);
47
+ const master = new Uint8Array(await s.deriveBits({ name: 'PBKDF2', hash: 'SHA-256', salt, iterations: iter }, pw, 256));
48
+ try {
49
+ const h = await s.importKey('raw', master, 'HKDF', false, ['deriveKey', 'deriveBits']);
50
+ const hk = (info) => ({ name: 'HKDF', hash: 'SHA-256', salt: new Uint8Array(0), info: te.encode(`${label} ${info}`) });
51
+ const key = await s.deriveKey(hk('key'), h, { name: 'AES-GCM', length: 256 }, false, ['encrypt', 'decrypt']);
52
+ const check = hex(new Uint8Array(await s.deriveBits(hk('check'), h, 256)));
53
+ return { key, check };
54
+ } finally { master.fill(0); }
55
+ }
56
+ /** The blob's fields, checked: { v, kdf, iter, salt, iv, ct } (bytes for the last three). */
57
+ function parse(blob) {
58
+ let o;
59
+ try { o = JSON.parse(td.decode(blob)); } catch { refuse('words_blob', 'the kept words are damaged'); }
60
+ if (o?.v !== V || o.kdf !== KDF || !Number.isSafeInteger(o.iter) || o.iter < MIN_ITERATIONS) refuse('words_blob', 'the kept words are of an unknown kind');
61
+ try { return { ...o, salt: unb64(o.salt), iv: unb64(o.iv), ct: unb64(o.ct) }; } catch { return refuse('words_blob', 'the kept words are damaged'); }
62
+ }
63
+ export function wordsMeta(blob) { const { v, kdf, iter } = parse(blob); return { v, kdf, iter }; }
64
+
65
+ /**
66
+ * Bytes encrypted under the password, as the words are: PBKDF2 with a fresh 16-byte salt, HKDF info "<label> key" and
67
+ * "<label> check", AES-256-GCM over aad → { iter, salt, iv, ct (bytes), check (hex) }.
68
+ */
69
+ export async function lockBytes(label, bytes, password, { iterations = ITERATIONS, aad: ad = new Uint8Array(0) } = {}) {
70
+ if (!Number.isSafeInteger(iterations) || iterations < MIN_ITERATIONS) refuse('words_kdf', `at least ${MIN_ITERATIONS} rounds`);
71
+ const salt = globalThis.crypto.getRandomValues(new Uint8Array(16)), iv = globalThis.crypto.getRandomValues(new Uint8Array(12));
72
+ const { key, check } = await keys(password, salt, iterations, label);
73
+ const ct = new Uint8Array(await subtle().encrypt({ name: 'AES-GCM', iv, additionalData: ad }, key, bytes));
74
+ return { iter: iterations, salt, iv, ct, check };
75
+ }
76
+ /** lockBytes's bytes back → { plain, check }; a wrong password (or a changed aad): check words_password. */
77
+ export async function openBytes(label, { iter, salt, iv, ct }, password, ad = new Uint8Array(0)) {
78
+ if (!Number.isSafeInteger(iter) || iter < MIN_ITERATIONS) refuse('words_kdf', `at least ${MIN_ITERATIONS} rounds`);
79
+ const { key, check } = await keys(String(password ?? ''), salt, iter, label);
80
+ try { return { plain: new Uint8Array(await subtle().decrypt({ name: 'AES-GCM', iv, additionalData: ad }, key, ct)), check }; } catch { return refuse('words_password', 'Wrong password.'); }
81
+ }
82
+
83
+ /** The 25 words encrypted under the password → { blob (bytes), check (hex) }. */
84
+ export async function lockWords(words, password, { iterations = ITERATIONS } = {}) {
85
+ const line = te.encode(cleanWords(words)), p = checkPassword(password);
86
+ try {
87
+ const { iter, salt, iv, ct, check } = await lockBytes(V, line, p, { iterations, aad: aad(iterations) });
88
+ return { blob: te.encode(JSON.stringify({ v: V, kdf: KDF, iter, salt: b64(salt), iv: b64(iv), ct: b64(ct) })), check };
89
+ } finally { line.fill(0); }
90
+ }
91
+ /** The words back → { words: [25], check }; a wrong password: check words_password. */
92
+ export async function openWords(blob, password) {
93
+ const o = parse(blob), { plain, check } = await openBytes(V, o, password, aad(o.iter));
94
+ try { return { words: td.decode(plain).split(' '), check }; } finally { plain.fill(0); }
95
+ }
96
+ /** The check for a password, after the words opened under it (so a wrong one is refused here too). */
97
+ export async function proofOf(blob, password) { return (await openWords(blob, password)).check; }
98
+
99
+ /** A wrong password costs 2 s, then 4, 8, … up to 256 s (in this gate's life: the page's); a right one resets it. Never a lockout. */
100
+ export function wrongGate({ base = 2000, sleep = (ms) => new Promise((r) => setTimeout(r, ms)) } = {}) {
101
+ let fails = 0, last = Promise.resolve();
102
+ const attempt = async (fn) => {
103
+ try { const r = await fn(); fails = 0; return r; } catch (e) {
104
+ if (e?.check === 'words_password') { fails++; await sleep(base * 2 ** Math.min(fails - 1, 7)); }
105
+ throw e;
106
+ }
107
+ };
108
+ return {
109
+ get fails() { return fails; },
110
+ /** One attempt at a time: a second tap waits for the first's delay, so taps in parallel do not skip it. */
111
+ run(fn) { const r = last.then(() => attempt(fn)); last = r.catch(() => {}); return r; },
112
+ };
113
+ }
114
+ /** The page's one gate: every place that asks the password counts in it. */
115
+ export const gate = wrongGate();