nervur 0.19.2 → 0.20.1
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.
- package/README.md +156 -104
- package/dist/being/being.d.ts +8 -15
- package/dist/being/being.js +20 -67
- package/dist/being/digest.d.ts +1 -1
- package/dist/being/digest.js +7 -27
- package/dist/being/faculty.d.ts +10 -0
- package/dist/being/faculty.js +74 -0
- package/dist/being/index.d.ts +5 -5
- package/dist/being/index.js +6 -5
- package/dist/being/kind.d.ts +4 -0
- package/dist/being/kind.js +28 -0
- package/dist/being/types.d.ts +35 -63
- package/dist/being/types.js +3 -57
- package/dist/being/words.d.ts +13 -0
- package/dist/being/words.js +14 -0
- package/dist/cli/command.d.ts +13 -0
- package/dist/cli/command.js +194 -0
- package/dist/cli/harbor.d.ts +21 -0
- package/dist/cli/harbor.js +141 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +14 -0
- package/dist/contract/index.d.ts +39 -0
- package/dist/contract/index.js +33 -0
- package/dist/crypto/aes.d.ts +3 -0
- package/dist/crypto/aes.js +24 -0
- package/dist/crypto/bytes.d.ts +6 -0
- package/dist/crypto/bytes.js +33 -0
- package/dist/crypto/ed25519.d.ts +3 -0
- package/dist/crypto/ed25519.js +85 -0
- package/dist/crypto/hash.d.ts +2 -0
- package/dist/crypto/hash.js +11 -0
- package/dist/crypto/index.d.ts +7 -0
- package/dist/crypto/index.js +10 -0
- package/dist/crypto/json.d.ts +8 -0
- package/dist/crypto/json.js +250 -0
- package/dist/crypto/mlkem.d.ts +12 -0
- package/dist/crypto/mlkem.js +36 -0
- package/dist/crypto/subtle.d.ts +10 -0
- package/dist/crypto/subtle.js +29 -0
- package/dist/crypto/x25519.d.ts +2 -0
- package/dist/crypto/x25519.js +21 -0
- package/dist/folder/index.d.ts +14 -0
- package/dist/folder/index.js +87 -0
- package/dist/harbor/catalogue.d.ts +25 -0
- package/dist/harbor/catalogue.js +93 -0
- package/dist/harbor/dock.d.ts +29 -0
- package/dist/harbor/dock.js +122 -0
- package/dist/harbor/harbor.d.ts +31 -0
- package/dist/harbor/harbor.js +146 -0
- package/dist/harbor/index.d.ts +7 -10
- package/dist/harbor/index.js +9 -18
- package/dist/harbor/package.d.ts +10 -0
- package/dist/harbor/package.js +115 -0
- package/dist/harbor/registry.d.ts +9 -0
- package/dist/harbor/registry.js +78 -0
- package/dist/harbor/root-line.d.ts +9 -0
- package/dist/harbor/root-line.js +36 -0
- package/dist/harbor/terrain.d.ts +9 -0
- package/dist/harbor/terrain.js +2 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +14 -0
- package/dist/pointer/bodies.d.ts +26 -0
- package/dist/pointer/bodies.js +64 -0
- package/dist/pointer/index.d.ts +2 -0
- package/dist/pointer/index.js +4 -0
- package/dist/pointer/world.d.ts +35 -0
- package/dist/pointer/world.js +85 -0
- package/dist/quo/door.d.ts +34 -0
- package/dist/quo/door.js +172 -0
- package/dist/quo/index.d.ts +8 -0
- package/dist/quo/index.js +11 -0
- package/dist/quo/invitation.d.ts +7 -0
- package/dist/quo/invitation.js +15 -0
- package/dist/quo/keys.d.ts +40 -0
- package/dist/quo/keys.js +79 -0
- package/dist/quo/payload.d.ts +15 -0
- package/dist/quo/payload.js +55 -0
- package/dist/quo/relations.d.ts +40 -0
- package/dist/quo/relations.js +33 -0
- package/dist/quo/reply.d.ts +13 -0
- package/dist/quo/reply.js +35 -0
- package/dist/quo/seal.d.ts +42 -0
- package/dist/quo/seal.js +78 -0
- package/dist/quo/standing.d.ts +39 -0
- package/dist/quo/standing.js +90 -0
- package/dist/tcp/frame.d.ts +29 -0
- package/dist/tcp/frame.js +74 -0
- package/dist/tcp/index.d.ts +18 -0
- package/dist/tcp/index.js +164 -0
- package/dist/ward/allowance.d.ts +13 -9
- package/dist/ward/allowance.js +28 -67
- package/dist/ward/cells.d.ts +6 -6
- package/dist/ward/cells.js +67 -179
- package/dist/ward/index.d.ts +6 -10
- package/dist/ward/index.js +7 -13
- package/dist/ward/partition.d.ts +44 -60
- package/dist/ward/partition.js +56 -285
- package/dist/ward/pilot.d.ts +8 -0
- package/dist/ward/pilot.js +50 -0
- package/dist/ward/stance.d.ts +27 -20
- package/dist/ward/stance.js +174 -406
- package/dist/ward/ward-being.d.ts +35 -0
- package/dist/ward/ward-being.js +82 -0
- package/dist/ward/ward.d.ts +45 -11
- package/dist/ward/ward.js +239 -357
- package/package.json +24 -31
- package/src/being/being.ts +35 -78
- package/src/being/digest.ts +7 -35
- package/src/being/faculty.ts +73 -0
- package/src/being/index.ts +6 -6
- package/src/being/kind.ts +30 -0
- package/src/being/types.ts +50 -146
- package/src/being/words.ts +31 -0
- package/src/cli/command.ts +188 -0
- package/src/cli/harbor.ts +137 -0
- package/src/cli/main.ts +13 -0
- package/src/contract/index.ts +73 -0
- package/src/crypto/aes.ts +26 -0
- package/src/crypto/bytes.ts +37 -0
- package/src/crypto/ed25519.ts +84 -0
- package/src/crypto/hash.ts +14 -0
- package/src/crypto/index.ts +10 -0
- package/src/crypto/json.ts +241 -0
- package/src/crypto/mlkem.ts +38 -0
- package/src/crypto/subtle.ts +33 -0
- package/src/crypto/x25519.ts +21 -0
- package/src/folder/index.ts +92 -0
- package/src/harbor/catalogue.ts +110 -0
- package/src/harbor/dock.ts +137 -0
- package/src/harbor/harbor.ts +180 -0
- package/src/harbor/index.ts +9 -19
- package/src/harbor/package.ts +131 -0
- package/src/harbor/registry.ts +73 -0
- package/src/harbor/root-line.ts +36 -0
- package/src/harbor/terrain.ts +15 -0
- package/src/index.ts +20 -0
- package/src/pointer/bodies.ts +65 -0
- package/src/pointer/index.ts +4 -0
- package/src/pointer/world.ts +94 -0
- package/src/quo/door.ts +178 -0
- package/src/quo/index.ts +11 -0
- package/src/quo/invitation.ts +15 -0
- package/src/quo/keys.ts +91 -0
- package/src/quo/payload.ts +61 -0
- package/src/quo/relations.ts +62 -0
- package/src/quo/reply.ts +38 -0
- package/src/quo/seal.ts +101 -0
- package/src/quo/standing.ts +111 -0
- package/src/stand/main.ts +20 -0
- package/src/stand/stand.ts +220 -0
- package/src/tcp/frame.ts +78 -0
- package/src/tcp/index.ts +173 -0
- package/src/ward/allowance.ts +37 -75
- package/src/ward/cells.ts +63 -176
- package/src/ward/index.ts +7 -17
- package/src/ward/partition.ts +86 -326
- package/src/ward/pilot.ts +50 -0
- package/src/ward/stance.ts +186 -420
- package/src/ward/ward-being.ts +103 -0
- package/src/ward/ward.ts +256 -359
- package/dist/being/lent.d.ts +0 -32
- package/dist/being/lent.js +0 -72
- package/dist/being/silence.d.ts +0 -12
- package/dist/being/silence.js +0 -41
- package/dist/conformance/assert.d.ts +0 -11
- package/dist/conformance/assert.js +0 -106
- package/dist/conformance/beings.d.ts +0 -199
- package/dist/conformance/beings.js +0 -188
- package/dist/conformance/estate.d.ts +0 -5
- package/dist/conformance/estate.js +0 -388
- package/dist/conformance/index.d.ts +0 -80
- package/dist/conformance/index.js +0 -819
- package/dist/conformance/reach.d.ts +0 -10
- package/dist/conformance/reach.js +0 -72
- package/dist/conformance/store.d.ts +0 -5
- package/dist/conformance/store.js +0 -113
- package/dist/harbor/box.d.ts +0 -121
- package/dist/harbor/box.js +0 -121
- package/dist/harbor/core.d.ts +0 -55
- package/dist/harbor/core.js +0 -662
- package/dist/harbor/dial.d.ts +0 -9
- package/dist/harbor/dial.js +0 -81
- package/dist/harbor/memory.d.ts +0 -29
- package/dist/harbor/memory.js +0 -104
- package/dist/harbor/reach.d.ts +0 -36
- package/dist/harbor/reach.js +0 -199
- package/dist/harbor/store.d.ts +0 -35
- package/dist/harbor/store.js +0 -62
- package/dist/vector/cases.d.ts +0 -42
- package/dist/vector/cases.js +0 -223
- package/dist/vector/index.d.ts +0 -6
- package/dist/vector/index.js +0 -8
- package/dist/vector/stand.d.ts +0 -9
- package/dist/vector/stand.js +0 -77
- package/dist/vector/world.d.ts +0 -144
- package/dist/vector/world.js +0 -209
- package/dist/ward/arithmetic.d.ts +0 -31
- package/dist/ward/arithmetic.js +0 -249
- package/dist/ward/door.d.ts +0 -18
- package/dist/ward/door.js +0 -186
- package/dist/ward/ground.d.ts +0 -29
- package/dist/ward/ground.js +0 -56
- package/dist/ward/heirs.d.ts +0 -13
- package/dist/ward/heirs.js +0 -117
- package/dist/ward/json.d.ts +0 -2
- package/dist/ward/json.js +0 -163
- package/dist/ward/owner.d.ts +0 -13
- package/dist/ward/owner.js +0 -220
- package/dist/ward/seal.d.ts +0 -52
- package/dist/ward/seal.js +0 -150
- package/src/being/lent.ts +0 -72
- package/src/being/silence.ts +0 -46
- package/src/conformance/assert.ts +0 -100
- package/src/conformance/beings.ts +0 -188
- package/src/conformance/estate.ts +0 -412
- package/src/conformance/index.ts +0 -965
- package/src/conformance/reach.ts +0 -83
- package/src/conformance/store.ts +0 -125
- package/src/harbor/box.ts +0 -131
- package/src/harbor/core.ts +0 -699
- package/src/harbor/dial.ts +0 -112
- package/src/harbor/memory.ts +0 -123
- package/src/harbor/reach.ts +0 -221
- package/src/harbor/store.ts +0 -91
- package/src/vector/cases.ts +0 -257
- package/src/vector/index.ts +0 -11
- package/src/vector/stand.ts +0 -76
- package/src/vector/world.ts +0 -232
- package/src/ward/arithmetic.ts +0 -251
- package/src/ward/door.ts +0 -186
- package/src/ward/ground.ts +0 -142
- package/src/ward/heirs.ts +0 -116
- package/src/ward/json.ts +0 -144
- package/src/ward/owner.ts +0 -214
- package/src/ward/seal.ts +0 -178
package/dist/quo/door.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The door: an ask's box in, a reply's box out, judged by the thirteen cases
|
|
3
|
+
// of the spec in their order. A stranger hears one silence whatever the
|
|
4
|
+
// case. The case is handed back beside the bytes, for the kit's own eyes,
|
|
5
|
+
// with `heard`: whether a key the door holds or keeps spoke.
|
|
6
|
+
import { decapsulate, hex, unhex, utf8, x25519Public, zero } from '../crypto/index.js';
|
|
7
|
+
import { drawLock, SigningKey } from './keys.js';
|
|
8
|
+
import { readPayload } from './payload.js';
|
|
9
|
+
import { writeReply } from './reply.js';
|
|
10
|
+
import { checkBody, follow, HEAD, knockEdge, openBody, openHead, replyKey, sealReply, signBody, SIZE, splitBody, splitKnock, ZERO_EDGE } from './seal.js';
|
|
11
|
+
// The count numbers below the highest a door still honours: those within
|
|
12
|
+
// this span that it has not seen.
|
|
13
|
+
export const SPAN = 64;
|
|
14
|
+
export class Door {
|
|
15
|
+
key;
|
|
16
|
+
relations;
|
|
17
|
+
behind;
|
|
18
|
+
draw;
|
|
19
|
+
constructor(key, relations, behind, draw) {
|
|
20
|
+
this.key = key;
|
|
21
|
+
this.relations = relations;
|
|
22
|
+
this.behind = behind;
|
|
23
|
+
this.draw = draw;
|
|
24
|
+
}
|
|
25
|
+
// A new heir, held fresh, and its invitation. The lock is drawn at the
|
|
26
|
+
// first invitation that names an heir, before the heir's secret.
|
|
27
|
+
async invite() {
|
|
28
|
+
let lock = this.relations.lock();
|
|
29
|
+
if (!lock)
|
|
30
|
+
this.relations.setLock((lock = drawLock(this.draw)));
|
|
31
|
+
const heir = await SigningKey.draw(this.draw);
|
|
32
|
+
this.relations.set(heir.id, { state: 'fresh' });
|
|
33
|
+
return { heir: heir.id, invitation: { ward: this.key.pk, heir: heir.id, secret: hex(heir.secret), lock: hex(lock.ek) } };
|
|
34
|
+
}
|
|
35
|
+
// The door stops holding an heir. A fresh one leaves nothing, a spent one
|
|
36
|
+
// leaves the keys kept at removal.
|
|
37
|
+
remove(heir) {
|
|
38
|
+
const record = this.relations.get(heir);
|
|
39
|
+
if (record === undefined || record.state === 'kept')
|
|
40
|
+
return false;
|
|
41
|
+
if (record.state === 'fresh')
|
|
42
|
+
this.relations.delete(heir);
|
|
43
|
+
else
|
|
44
|
+
this.relations.set(heir, { state: 'kept', held: record.held, vouched: record.vouched, open: record.open, offered: record.offered });
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
async arrive(bytes) {
|
|
48
|
+
const stranger = async (n) => ({ bytes: await this.#silence(bytes), case: n, heard: false });
|
|
49
|
+
if (bytes.length > SIZE || bytes.length < HEAD)
|
|
50
|
+
return stranger(1);
|
|
51
|
+
const agreement = await this.key.open(bytes.subarray(0, HEAD));
|
|
52
|
+
const opened = agreement && (await openHead(bytes, agreement));
|
|
53
|
+
if (!opened)
|
|
54
|
+
return stranger(1);
|
|
55
|
+
const isZero = zero(opened.head);
|
|
56
|
+
const heir = hex(opened.head);
|
|
57
|
+
const record = isZero ? undefined : this.relations.get(heir);
|
|
58
|
+
const body = await this.#body(opened, record);
|
|
59
|
+
if (!body)
|
|
60
|
+
return stranger(1);
|
|
61
|
+
const read = readPayload(body.text);
|
|
62
|
+
if ('fault' in read)
|
|
63
|
+
return stranger(read.fault === 'not an object' ? 2 : 3);
|
|
64
|
+
const p = read.payload;
|
|
65
|
+
if (isZero ? p.to !== null : p.to !== heir)
|
|
66
|
+
return stranger(3);
|
|
67
|
+
if (isZero) {
|
|
68
|
+
if (!this.behind.zero)
|
|
69
|
+
return stranger(4);
|
|
70
|
+
if (!(await checkBody(unhex(p.by), body)))
|
|
71
|
+
return stranger(5);
|
|
72
|
+
const choice = await this.#choose(null, p);
|
|
73
|
+
return { bytes: await this.#sealed(opened, choice), case: 'object' in choice ? 12 : 13, heard: false };
|
|
74
|
+
}
|
|
75
|
+
if (record === undefined)
|
|
76
|
+
return stranger(6);
|
|
77
|
+
if (!admits(record, heir, p.by))
|
|
78
|
+
return stranger(7);
|
|
79
|
+
if (!(await checkBody(unhex(p.by), body)))
|
|
80
|
+
return stranger(8);
|
|
81
|
+
const reply = await replyKey(this.draw, opened.lid);
|
|
82
|
+
if (!reply)
|
|
83
|
+
return stranger(1);
|
|
84
|
+
const offered = await follow(body.edge, reply.agreement);
|
|
85
|
+
// Checked again, since another arrival may have moved the keys while
|
|
86
|
+
// this one was checking its signature.
|
|
87
|
+
const now = this.relations.get(heir);
|
|
88
|
+
if (now === undefined || !admits(now, heir, p.by) || !opensUnder(now, body.edge))
|
|
89
|
+
return stranger(7);
|
|
90
|
+
if (now.state === 'kept')
|
|
91
|
+
return this.#heard(reply, { quo: 'removed' }, 9);
|
|
92
|
+
if (now.state === 'fresh' && (p.next === null || p.next === heir))
|
|
93
|
+
return this.#heard(reply, { quo: 'unannounced' }, 10);
|
|
94
|
+
if (now.state === 'spent' && !honours(now, p.seq))
|
|
95
|
+
return this.#heard(reply, { quo: 'repeated' }, 11);
|
|
96
|
+
this.relations.set(heir, move(now, heir, p, hex(body.edge), hex(offered)));
|
|
97
|
+
const choice = await this.#choose(heir, p);
|
|
98
|
+
const kept = await this.relations.kept().catch(() => false);
|
|
99
|
+
return this.#heard(reply, kept ? choice : { silence: true }, kept && 'object' in choice ? 12 : 13);
|
|
100
|
+
}
|
|
101
|
+
async #heard(key, reply, n) {
|
|
102
|
+
return { bytes: await sealReply(key, await this.#signed(reply)), case: n, heard: true };
|
|
103
|
+
}
|
|
104
|
+
#signed(reply) {
|
|
105
|
+
return signBody(utf8(writeReply(reply)), this.key.signing);
|
|
106
|
+
}
|
|
107
|
+
async #choose(heir, p) {
|
|
108
|
+
try {
|
|
109
|
+
return await this.behind.answer(heir, p);
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
return { silence: true };
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async #sealed(opened, reply) {
|
|
116
|
+
const key = await replyKey(this.draw, opened.lid);
|
|
117
|
+
return key ? sealReply(key, await this.#signed(reply)) : this.#silence(opened.lid);
|
|
118
|
+
}
|
|
119
|
+
// The body, opened under the edge keys the door takes for this head.
|
|
120
|
+
async #body(opened, record) {
|
|
121
|
+
const tries = [];
|
|
122
|
+
if (record?.state === 'fresh') {
|
|
123
|
+
const knock = splitKnock(opened.rest);
|
|
124
|
+
const lock = this.relations.lock();
|
|
125
|
+
const shared = knock && lock ? decapsulate(lock.dk, knock.ciphertext) : null;
|
|
126
|
+
if (!knock || !shared)
|
|
127
|
+
return null;
|
|
128
|
+
tries.push({ edge: await knockEdge(shared), sealed: knock.sealedBody });
|
|
129
|
+
}
|
|
130
|
+
else if (record === undefined)
|
|
131
|
+
tries.push({ edge: ZERO_EDGE, sealed: opened.rest });
|
|
132
|
+
else
|
|
133
|
+
for (const edge of [record.open, record.offered])
|
|
134
|
+
tries.push({ edge: unhex(edge), sealed: opened.rest });
|
|
135
|
+
for (const { edge, sealed } of tries) {
|
|
136
|
+
const plain = await openBody(opened, edge, sealed);
|
|
137
|
+
if (plain === null)
|
|
138
|
+
continue;
|
|
139
|
+
const body = splitBody(plain);
|
|
140
|
+
return body && { edge, ...body };
|
|
141
|
+
}
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
// A stranger's silence: sealed to the first thirty-two bytes that arrived
|
|
145
|
+
// when they take a seal, and otherwise to a lid nobody holds.
|
|
146
|
+
async #silence(arrived) {
|
|
147
|
+
const text = await this.#signed({ silence: true });
|
|
148
|
+
const candidates = [arrived.length >= HEAD ? arrived.subarray(0, HEAD) : null, this.draw(HEAD)];
|
|
149
|
+
for (const lid of candidates) {
|
|
150
|
+
const key = lid && (await replyKey(this.draw, lid));
|
|
151
|
+
if (key)
|
|
152
|
+
return sealReply(key, text);
|
|
153
|
+
}
|
|
154
|
+
return sealReply((await replyKey(this.draw, await x25519Public(this.draw(HEAD)))), text);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const admits = (record, heir, by) => (record.state === 'fresh' ? by === heir : by === record.held || by === record.vouched);
|
|
158
|
+
const opensUnder = (record, edge) => record.state === 'fresh' || hex(edge) === record.open || hex(edge) === record.offered;
|
|
159
|
+
const honours = (record, seq) => seq > record.highest || (seq > record.highest - SPAN && seq !== record.highest && !record.honoured.includes(seq));
|
|
160
|
+
// The move tables: the keys and edge keys after a choice, and the count.
|
|
161
|
+
const move = (record, heir, p, edge, offered) => {
|
|
162
|
+
const announced = p.next !== null && p.next !== heir && p.next !== p.by ? p.next : null;
|
|
163
|
+
if (record.state === 'fresh')
|
|
164
|
+
return { state: 'spent', held: announced, vouched: null, open: edge, offered, highest: p.seq, honoured: [] };
|
|
165
|
+
if (record.state !== 'spent')
|
|
166
|
+
return record;
|
|
167
|
+
if (p.seq < record.highest)
|
|
168
|
+
return { ...record, honoured: [...record.honoured, p.seq] };
|
|
169
|
+
const keys = p.by === record.held ? { held: record.held, vouched: announced ?? record.vouched, open: edge, offered } : { held: p.by, vouched: announced, open: edge, offered };
|
|
170
|
+
const honoured = [...record.honoured, record.highest].filter((n) => n > p.seq - SPAN);
|
|
171
|
+
return { state: 'spent', ...keys, highest: p.seq, honoured };
|
|
172
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { Door, SPAN, type Behind, type Choice, type Judged } from './door.ts';
|
|
2
|
+
export { readInvitation, type Invitation } from './invitation.ts';
|
|
3
|
+
export { Ephemeral, SigningKey, WardKey, type Draw } from './keys.ts';
|
|
4
|
+
export { readPayload, writePayload, type Payload } from './payload.ts';
|
|
5
|
+
export { GONE, keptLock, lockOf, MemoryRelations, type HeirRecord, type KeptLock, type Relations } from './relations.ts';
|
|
6
|
+
export { readReply, SILENCE, WORDS, writeReply, type Reply, type Word } from './reply.ts';
|
|
7
|
+
export { SIZE } from './seal.ts';
|
|
8
|
+
export { freshStanding, Standing, type Read, type Sent, type StandingState } from './standing.ts';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Quo's wire: keys, the invitation, the payload, the reply, the boxes, the
|
|
3
|
+
// door and the standing. It knows no being, no ward and no terrain.
|
|
4
|
+
export { Door, SPAN } from './door.js';
|
|
5
|
+
export { readInvitation } from './invitation.js';
|
|
6
|
+
export { Ephemeral, SigningKey, WardKey } from './keys.js';
|
|
7
|
+
export { readPayload, writePayload } from './payload.js';
|
|
8
|
+
export { GONE, keptLock, lockOf, MemoryRelations } from './relations.js';
|
|
9
|
+
export { readReply, SILENCE, WORDS, writeReply } from './reply.js';
|
|
10
|
+
export { SIZE } from './seal.js';
|
|
11
|
+
export { freshStanding, Standing } from './standing.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The invitation: `{ ward, heir, secret, lock }`, a value with no route.
|
|
3
|
+
import { EK, encapsulate, isHex, unhex } from '../crypto/index.js';
|
|
4
|
+
// An invitation, or null for any other shape. A field beside the four is
|
|
5
|
+
// ignored, and the lock is held to the modulus check.
|
|
6
|
+
export const readInvitation = (value) => {
|
|
7
|
+
if (typeof value !== 'object' || value === null || Array.isArray(value))
|
|
8
|
+
return null;
|
|
9
|
+
const { ward, heir, secret, lock } = value;
|
|
10
|
+
if (!isHex(ward, 64) || !isHex(heir, 32) || !isHex(secret, 32) || !isHex(lock, EK))
|
|
11
|
+
return null;
|
|
12
|
+
if (encapsulate(unhex(lock), new Uint8Array(32)) === null)
|
|
13
|
+
return null;
|
|
14
|
+
return { ward, heir, secret, lock };
|
|
15
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type Lock } from '../crypto/index.ts';
|
|
2
|
+
export type Draw = (length: number) => Uint8Array;
|
|
3
|
+
export declare const LABEL: {
|
|
4
|
+
readonly seal: Uint8Array<ArrayBufferLike>;
|
|
5
|
+
readonly edgeSeal: Uint8Array<ArrayBufferLike>;
|
|
6
|
+
readonly lock: Uint8Array<ArrayBufferLike>;
|
|
7
|
+
readonly edge: Uint8Array<ArrayBufferLike>;
|
|
8
|
+
readonly wardSign: Uint8Array<ArrayBufferLike>;
|
|
9
|
+
readonly wardSeal: Uint8Array<ArrayBufferLike>;
|
|
10
|
+
};
|
|
11
|
+
export declare class SigningKey {
|
|
12
|
+
readonly secret: Uint8Array;
|
|
13
|
+
readonly pk: Uint8Array;
|
|
14
|
+
private constructor();
|
|
15
|
+
static from(secret: Uint8Array): Promise<SigningKey>;
|
|
16
|
+
static draw(draw: Draw): Promise<SigningKey>;
|
|
17
|
+
get id(): string;
|
|
18
|
+
}
|
|
19
|
+
export declare class Ephemeral {
|
|
20
|
+
readonly secret: Uint8Array;
|
|
21
|
+
readonly pk: Uint8Array;
|
|
22
|
+
private constructor();
|
|
23
|
+
static from(secret: Uint8Array): Promise<Ephemeral>;
|
|
24
|
+
static draw(draw: Draw): Promise<Ephemeral>;
|
|
25
|
+
agree(pk: Uint8Array): Promise<Uint8Array | null>;
|
|
26
|
+
}
|
|
27
|
+
export declare class WardKey {
|
|
28
|
+
readonly signing: SigningKey;
|
|
29
|
+
readonly sealSecret: Uint8Array;
|
|
30
|
+
readonly padlock: Uint8Array;
|
|
31
|
+
private constructor();
|
|
32
|
+
static from(seed: Uint8Array | string): Promise<WardKey>;
|
|
33
|
+
get pk(): string;
|
|
34
|
+
open(lid: Uint8Array): Promise<Uint8Array | null>;
|
|
35
|
+
}
|
|
36
|
+
export declare const wardHalves: (pk: string) => {
|
|
37
|
+
signing: string;
|
|
38
|
+
padlock: string;
|
|
39
|
+
};
|
|
40
|
+
export declare const drawLock: (draw: Draw) => Lock;
|
package/dist/quo/keys.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The keys of Quo: the ward key derived from a seed, and every other key
|
|
3
|
+
// drawn. A draw is the caller's, so this layer names no source of bytes.
|
|
4
|
+
import { agree, ed25519Public, hex, hkdf, lockFrom, sha256, utf8, x25519Public } from '../crypto/index.js';
|
|
5
|
+
export const LABEL = {
|
|
6
|
+
seal: utf8('quo-seal'),
|
|
7
|
+
edgeSeal: utf8('quo-edge-seal'),
|
|
8
|
+
lock: utf8('quo-lock'),
|
|
9
|
+
edge: utf8('quo-edge'),
|
|
10
|
+
wardSign: utf8('quo-ward-sign'),
|
|
11
|
+
wardSeal: utf8('quo-ward-seal'),
|
|
12
|
+
};
|
|
13
|
+
// An Ed25519 pair: an heir, or a standing's own key.
|
|
14
|
+
export class SigningKey {
|
|
15
|
+
secret;
|
|
16
|
+
pk;
|
|
17
|
+
constructor(secret, pk) {
|
|
18
|
+
this.secret = secret;
|
|
19
|
+
this.pk = pk;
|
|
20
|
+
}
|
|
21
|
+
static async from(secret) {
|
|
22
|
+
return new SigningKey(secret, await ed25519Public(secret));
|
|
23
|
+
}
|
|
24
|
+
static draw(draw) {
|
|
25
|
+
return SigningKey.from(draw(32));
|
|
26
|
+
}
|
|
27
|
+
get id() {
|
|
28
|
+
return hex(this.pk);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
// An X25519 pair used for one box.
|
|
32
|
+
export class Ephemeral {
|
|
33
|
+
secret;
|
|
34
|
+
pk;
|
|
35
|
+
constructor(secret, pk) {
|
|
36
|
+
this.secret = secret;
|
|
37
|
+
this.pk = pk;
|
|
38
|
+
}
|
|
39
|
+
static async from(secret) {
|
|
40
|
+
return new Ephemeral(secret, await x25519Public(secret));
|
|
41
|
+
}
|
|
42
|
+
static draw(draw) {
|
|
43
|
+
return Ephemeral.from(draw(32));
|
|
44
|
+
}
|
|
45
|
+
agree(pk) {
|
|
46
|
+
return agree(this.secret, pk);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// The ward key: a signing pair and a padlock, both from the seed.
|
|
50
|
+
export class WardKey {
|
|
51
|
+
signing;
|
|
52
|
+
sealSecret;
|
|
53
|
+
padlock;
|
|
54
|
+
constructor(signing, sealSecret, padlock) {
|
|
55
|
+
this.signing = signing;
|
|
56
|
+
this.sealSecret = sealSecret;
|
|
57
|
+
this.padlock = padlock;
|
|
58
|
+
}
|
|
59
|
+
// Thirty-two bytes are the seed. Text, and bytes of any other length, are
|
|
60
|
+
// hashed to it first.
|
|
61
|
+
static async from(seed) {
|
|
62
|
+
const bytes = typeof seed === 'string' ? await sha256(utf8(seed)) : seed.length === 32 ? seed : await sha256(seed);
|
|
63
|
+
const sealSecret = await hkdf(bytes, LABEL.wardSeal, 32);
|
|
64
|
+
return new WardKey(await SigningKey.from(await hkdf(bytes, LABEL.wardSign, 32)), sealSecret, await x25519Public(sealSecret));
|
|
65
|
+
}
|
|
66
|
+
get pk() {
|
|
67
|
+
return hex(this.signing.pk) + hex(this.padlock);
|
|
68
|
+
}
|
|
69
|
+
open(lid) {
|
|
70
|
+
return agree(this.sealSecret, lid);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// A ward pk's two halves.
|
|
74
|
+
export const wardHalves = (pk) => ({ signing: pk.slice(0, 64), padlock: pk.slice(64) });
|
|
75
|
+
// A lock from sixty-four drawn bytes.
|
|
76
|
+
export const drawLock = (draw) => {
|
|
77
|
+
const bytes = draw(64);
|
|
78
|
+
return lockFrom(bytes.subarray(0, 32), bytes.subarray(32));
|
|
79
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type Payload = {
|
|
2
|
+
readonly to: string | null;
|
|
3
|
+
readonly by: string;
|
|
4
|
+
readonly next: string | null;
|
|
5
|
+
readonly seq: number;
|
|
6
|
+
readonly method?: string;
|
|
7
|
+
readonly args?: string;
|
|
8
|
+
};
|
|
9
|
+
export declare const writePayload: (p: Payload) => string;
|
|
10
|
+
export type PayloadRead = {
|
|
11
|
+
payload: Payload;
|
|
12
|
+
} | {
|
|
13
|
+
fault: 'not an object' | 'not well formed';
|
|
14
|
+
};
|
|
15
|
+
export declare const readPayload: (bytes: Uint8Array) => PayloadRead;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The payload: `{ to, by, next, seq, method?, args? }`. `args` is carried as
|
|
3
|
+
// the text it was written in, since Quo reads nothing inside it.
|
|
4
|
+
import { isCount, isHex, readObject } from '../crypto/index.js';
|
|
5
|
+
const OWED = ['to', 'by', 'next', 'seq'];
|
|
6
|
+
const ZERO = '0'.repeat(64);
|
|
7
|
+
const isPk = (text) => isHex(text, 32) && text !== ZERO;
|
|
8
|
+
// A pk written as a JSON string, or null written as null.
|
|
9
|
+
const pkOrNull = (text, nullable) => {
|
|
10
|
+
if (nullable && text === 'null')
|
|
11
|
+
return null;
|
|
12
|
+
if (!/^"[0-9a-f]{64}"$/.test(text))
|
|
13
|
+
return undefined;
|
|
14
|
+
const pk = text.slice(1, -1);
|
|
15
|
+
return isPk(pk) ? pk : undefined;
|
|
16
|
+
};
|
|
17
|
+
export const writePayload = (p) => {
|
|
18
|
+
const parts = [`"to":${JSON.stringify(p.to)}`, `"by":"${p.by}"`, `"next":${JSON.stringify(p.next)}`, `"seq":${p.seq}`];
|
|
19
|
+
if (p.method !== undefined)
|
|
20
|
+
parts.push(`"method":${JSON.stringify(p.method)}`);
|
|
21
|
+
if (p.args !== undefined)
|
|
22
|
+
parts.push(`"args":${p.args}`);
|
|
23
|
+
return `{${parts.join(',')}}`;
|
|
24
|
+
};
|
|
25
|
+
// Cases 2 and 3 of the door, before the head is compared.
|
|
26
|
+
export const readPayload = (bytes) => {
|
|
27
|
+
const fields = readObject(bytes);
|
|
28
|
+
if (fields === null)
|
|
29
|
+
return { fault: 'not an object' };
|
|
30
|
+
const bad = { fault: 'not well formed' };
|
|
31
|
+
if (!OWED.every((f) => fields.has(f)))
|
|
32
|
+
return bad;
|
|
33
|
+
const to = pkOrNull(fields.get('to'), true);
|
|
34
|
+
const by = pkOrNull(fields.get('by'), false);
|
|
35
|
+
const next = pkOrNull(fields.get('next'), true);
|
|
36
|
+
const seq = fields.get('seq');
|
|
37
|
+
if (to === undefined || !by || next === undefined || !isCount(seq))
|
|
38
|
+
return bad;
|
|
39
|
+
const method = fields.get('method');
|
|
40
|
+
if (method !== undefined && !method.startsWith('"'))
|
|
41
|
+
return bad;
|
|
42
|
+
const args = fields.get('args');
|
|
43
|
+
if (args !== undefined && !args.startsWith('{'))
|
|
44
|
+
return bad;
|
|
45
|
+
return {
|
|
46
|
+
payload: {
|
|
47
|
+
to,
|
|
48
|
+
by,
|
|
49
|
+
next,
|
|
50
|
+
seq: Number(seq),
|
|
51
|
+
...(method === undefined ? {} : { method: JSON.parse(method) }),
|
|
52
|
+
...(args === undefined ? {} : { args }),
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Lock } from '../crypto/index.ts';
|
|
2
|
+
export type Keys = {
|
|
3
|
+
readonly held: string;
|
|
4
|
+
readonly vouched: string | null;
|
|
5
|
+
readonly open: string;
|
|
6
|
+
readonly offered: string;
|
|
7
|
+
};
|
|
8
|
+
export type HeirRecord = {
|
|
9
|
+
readonly state: 'fresh';
|
|
10
|
+
} | ({
|
|
11
|
+
readonly state: 'spent';
|
|
12
|
+
readonly highest: number;
|
|
13
|
+
readonly honoured: readonly number[];
|
|
14
|
+
} & Keys) | ({
|
|
15
|
+
readonly state: 'kept';
|
|
16
|
+
} & Keys);
|
|
17
|
+
export type KeptLock = {
|
|
18
|
+
readonly ek: string;
|
|
19
|
+
readonly dk: string;
|
|
20
|
+
};
|
|
21
|
+
export interface Relations {
|
|
22
|
+
lock(): Lock | undefined;
|
|
23
|
+
setLock(lock: Lock): void;
|
|
24
|
+
get(heir: string): HeirRecord | undefined;
|
|
25
|
+
set(heir: string, record: HeirRecord): void;
|
|
26
|
+
delete(heir: string): void;
|
|
27
|
+
kept(): Promise<boolean>;
|
|
28
|
+
}
|
|
29
|
+
export declare const lockOf: (kept: KeptLock | undefined) => Lock | undefined;
|
|
30
|
+
export declare const keptLock: (lock: Lock) => KeptLock;
|
|
31
|
+
export declare const GONE = 256;
|
|
32
|
+
export declare class MemoryRelations implements Relations {
|
|
33
|
+
#private;
|
|
34
|
+
lock(): Lock | undefined;
|
|
35
|
+
setLock(lock: Lock): void;
|
|
36
|
+
get(heir: string): HeirRecord | undefined;
|
|
37
|
+
set(heir: string, record: HeirRecord): void;
|
|
38
|
+
delete(heir: string): void;
|
|
39
|
+
kept(): Promise<boolean>;
|
|
40
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { hex, unhex } from '../crypto/index.js';
|
|
2
|
+
export const lockOf = (kept) => (kept ? { ek: unhex(kept.ek), dk: unhex(kept.dk) } : undefined);
|
|
3
|
+
export const keptLock = (lock) => ({ ek: hex(lock.ek), dk: hex(lock.dk) });
|
|
4
|
+
// A bound on the kept records of removed relations, oldest out.
|
|
5
|
+
export const GONE = 256;
|
|
6
|
+
export class MemoryRelations {
|
|
7
|
+
#lock;
|
|
8
|
+
#heirs = new Map();
|
|
9
|
+
#gone = [];
|
|
10
|
+
lock() {
|
|
11
|
+
return lockOf(this.#lock);
|
|
12
|
+
}
|
|
13
|
+
setLock(lock) {
|
|
14
|
+
this.#lock = keptLock(lock);
|
|
15
|
+
}
|
|
16
|
+
get(heir) {
|
|
17
|
+
return this.#heirs.get(heir);
|
|
18
|
+
}
|
|
19
|
+
set(heir, record) {
|
|
20
|
+
this.#heirs.set(heir, record);
|
|
21
|
+
if (record.state !== 'kept')
|
|
22
|
+
return;
|
|
23
|
+
this.#gone.push(heir);
|
|
24
|
+
while (this.#gone.length > GONE)
|
|
25
|
+
this.#heirs.delete(this.#gone.shift());
|
|
26
|
+
}
|
|
27
|
+
delete(heir) {
|
|
28
|
+
this.#heirs.delete(heir);
|
|
29
|
+
}
|
|
30
|
+
kept() {
|
|
31
|
+
return Promise.resolve(true);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const WORDS: readonly ['removed', 'unannounced', 'repeated'];
|
|
2
|
+
export type Word = (typeof WORDS)[number];
|
|
3
|
+
export type Reply = {
|
|
4
|
+
readonly object: string;
|
|
5
|
+
readonly seen: string | null;
|
|
6
|
+
} | {
|
|
7
|
+
readonly silence: true;
|
|
8
|
+
} | {
|
|
9
|
+
readonly quo: Word;
|
|
10
|
+
};
|
|
11
|
+
export declare const SILENCE = "{\"silence\":true}";
|
|
12
|
+
export declare const writeReply: (reply: Reply) => string;
|
|
13
|
+
export declare const readReply: (bytes: Uint8Array) => Reply | null;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The reply text: `{ object, seen }`, `{ silence: true }` or `{ quo: word }`.
|
|
3
|
+
// `object` is carried as the text it was written in.
|
|
4
|
+
import { readObject } from '../crypto/index.js';
|
|
5
|
+
export const WORDS = ['removed', 'unannounced', 'repeated'];
|
|
6
|
+
export const SILENCE = '{"silence":true}';
|
|
7
|
+
export const writeReply = (reply) => {
|
|
8
|
+
if ('silence' in reply)
|
|
9
|
+
return SILENCE;
|
|
10
|
+
if ('quo' in reply)
|
|
11
|
+
return `{"quo":"${reply.quo}"}`;
|
|
12
|
+
return `{"object":${reply.object},"seen":${JSON.stringify(reply.seen)}}`;
|
|
13
|
+
};
|
|
14
|
+
const exactly = (fields, ...keys) => fields.size === keys.length && keys.every((k) => fields.has(k));
|
|
15
|
+
// A reply text of one of the three shapes, or null for anything else, which
|
|
16
|
+
// the reader takes as silence.
|
|
17
|
+
export const readReply = (bytes) => {
|
|
18
|
+
const fields = readObject(bytes);
|
|
19
|
+
if (fields === null)
|
|
20
|
+
return null;
|
|
21
|
+
if (exactly(fields, 'silence'))
|
|
22
|
+
return fields.get('silence') === 'true' ? { silence: true } : null;
|
|
23
|
+
if (exactly(fields, 'quo')) {
|
|
24
|
+
const text = fields.get('quo');
|
|
25
|
+
const word = text.startsWith('"') ? JSON.parse(text) : null;
|
|
26
|
+
return WORDS.find((w) => w === word) ? { quo: word } : null;
|
|
27
|
+
}
|
|
28
|
+
if (exactly(fields, 'object', 'seen')) {
|
|
29
|
+
const seen = fields.get('seen');
|
|
30
|
+
if (seen === 'null')
|
|
31
|
+
return { object: fields.get('object'), seen: null };
|
|
32
|
+
return seen.startsWith('"') ? { object: fields.get('object'), seen: JSON.parse(seen) } : null;
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Ephemeral, type Draw, type SigningKey } from './keys.ts';
|
|
2
|
+
export declare const SIZE = 1048576;
|
|
3
|
+
export declare const HEAD = 32;
|
|
4
|
+
export declare const SIGNATURE = 64;
|
|
5
|
+
export declare const SEALED_HEAD: number;
|
|
6
|
+
export declare const ZERO_EDGE: Uint8Array<ArrayBuffer>;
|
|
7
|
+
export declare const ZERO_HEAD: Uint8Array<ArrayBuffer>;
|
|
8
|
+
export declare const knockEdge: (shared: Uint8Array) => Promise<Uint8Array>;
|
|
9
|
+
export declare const follow: (edge: Uint8Array, agreement: Uint8Array) => Promise<Uint8Array>;
|
|
10
|
+
export declare const signBody: (text: Uint8Array, key: SigningKey) => Promise<Uint8Array>;
|
|
11
|
+
export declare const splitBody: (body: Uint8Array) => {
|
|
12
|
+
text: Uint8Array;
|
|
13
|
+
signature: Uint8Array;
|
|
14
|
+
} | null;
|
|
15
|
+
export declare const checkBody: (pk: Uint8Array, body: {
|
|
16
|
+
text: Uint8Array;
|
|
17
|
+
signature: Uint8Array;
|
|
18
|
+
}) => Promise<boolean>;
|
|
19
|
+
export declare const sealAsk: (lid: Ephemeral, padlock: Uint8Array, head: Uint8Array, edge: Uint8Array, body: Uint8Array, ciphertext?: Uint8Array) => Promise<Uint8Array | null>;
|
|
20
|
+
export type OpenedHead = {
|
|
21
|
+
lid: Uint8Array;
|
|
22
|
+
agreement: Uint8Array;
|
|
23
|
+
head: Uint8Array;
|
|
24
|
+
rest: Uint8Array;
|
|
25
|
+
};
|
|
26
|
+
export declare const openHead: (bytes: Uint8Array, agreement: Uint8Array) => Promise<OpenedHead | null>;
|
|
27
|
+
export declare const splitKnock: (rest: Uint8Array) => {
|
|
28
|
+
ciphertext: Uint8Array;
|
|
29
|
+
sealedBody: Uint8Array;
|
|
30
|
+
} | null;
|
|
31
|
+
export declare const openBody: (opened: OpenedHead, edge: Uint8Array, sealedBody: Uint8Array) => Promise<Uint8Array | null>;
|
|
32
|
+
export type SealedReply = {
|
|
33
|
+
bytes: Uint8Array;
|
|
34
|
+
agreement: Uint8Array;
|
|
35
|
+
};
|
|
36
|
+
export type ReplyKey = {
|
|
37
|
+
ephemeral: Ephemeral;
|
|
38
|
+
agreement: Uint8Array;
|
|
39
|
+
};
|
|
40
|
+
export declare const replyKey: (draw: Draw, lid: Uint8Array) => Promise<ReplyKey | null>;
|
|
41
|
+
export declare const sealReply: (key: ReplyKey, body: Uint8Array) => Promise<Uint8Array>;
|
|
42
|
+
export declare const openReply: (bytes: Uint8Array, lid: Ephemeral) => Promise<SealedReply | null>;
|
package/dist/quo/seal.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The three boxes and the keys between them.
|
|
3
|
+
//
|
|
4
|
+
// ask box lid (32) || AES( head ) || AES( body )
|
|
5
|
+
// knock box lid (32) || AES( head ) || ML-KEM ciphertext (1088) || AES( body )
|
|
6
|
+
// reply box ephemeral pk (32) || AES( body )
|
|
7
|
+
// body JSON text || signature (64)
|
|
8
|
+
//
|
|
9
|
+
// The head of an ask and the body of a reply are sealed under `quo-seal`
|
|
10
|
+
// over the agreement. The body of an ask is sealed under `quo-edge-seal`
|
|
11
|
+
// over the agreement and then the edge key. Every ciphertext's additional
|
|
12
|
+
// data is its own box's ephemeral pk.
|
|
13
|
+
import { CIPHERTEXT, concat, decrypt, encrypt, hkdf, sign, TAG, verify } from '../crypto/index.js';
|
|
14
|
+
import { Ephemeral, LABEL } from './keys.js';
|
|
15
|
+
export const SIZE = 1_048_576;
|
|
16
|
+
export const HEAD = 32;
|
|
17
|
+
export const SIGNATURE = 64;
|
|
18
|
+
export const SEALED_HEAD = HEAD + TAG;
|
|
19
|
+
export const ZERO_EDGE = new Uint8Array(32);
|
|
20
|
+
export const ZERO_HEAD = new Uint8Array(HEAD);
|
|
21
|
+
const cipher = async (label, ...material) => {
|
|
22
|
+
const out = await hkdf(concat(...material), label, 44);
|
|
23
|
+
return { key: out.subarray(0, 32), nonce: out.subarray(32) };
|
|
24
|
+
};
|
|
25
|
+
const seal = async (label, material, lid, plaintext) => {
|
|
26
|
+
const { key, nonce } = await cipher(label, ...material);
|
|
27
|
+
return encrypt(key, nonce, lid, plaintext);
|
|
28
|
+
};
|
|
29
|
+
const open = async (label, material, lid, ciphertext) => {
|
|
30
|
+
const { key, nonce } = await cipher(label, ...material);
|
|
31
|
+
return decrypt(key, nonce, lid, ciphertext);
|
|
32
|
+
};
|
|
33
|
+
// A knock's edge key, from the shared secret the lock decapsulates.
|
|
34
|
+
export const knockEdge = (shared) => hkdf(shared, LABEL.lock, 32);
|
|
35
|
+
// The edge key that follows `edge` after a reply of this agreement.
|
|
36
|
+
export const follow = (edge, agreement) => hkdf(concat(edge, agreement), LABEL.edge, 32);
|
|
37
|
+
export const signBody = async (text, key) => concat(text, await sign(key.secret, text));
|
|
38
|
+
// The text and signature of a body, or null for a body of sixty-four bytes
|
|
39
|
+
// or fewer.
|
|
40
|
+
export const splitBody = (body) => body.length <= SIGNATURE ? null : { text: body.subarray(0, -SIGNATURE), signature: body.subarray(-SIGNATURE) };
|
|
41
|
+
export const checkBody = (pk, body) => verify(pk, body.text, body.signature);
|
|
42
|
+
// An ask box sealed under `lid` to a padlock, or null for a padlock that
|
|
43
|
+
// takes no seal. `ciphertext` makes it a knock box.
|
|
44
|
+
export const sealAsk = async (lid, padlock, head, edge, body, ciphertext) => {
|
|
45
|
+
const agreement = await lid.agree(padlock);
|
|
46
|
+
if (agreement === null)
|
|
47
|
+
return null;
|
|
48
|
+
const sealedHead = await seal(LABEL.seal, [agreement], lid.pk, head);
|
|
49
|
+
const sealedBody = await seal(LABEL.edgeSeal, [agreement, edge], lid.pk, body);
|
|
50
|
+
return concat(lid.pk, sealedHead, ciphertext ?? new Uint8Array(0), sealedBody);
|
|
51
|
+
};
|
|
52
|
+
export const openHead = async (bytes, agreement) => {
|
|
53
|
+
if (bytes.length < HEAD + SEALED_HEAD)
|
|
54
|
+
return null;
|
|
55
|
+
const lid = bytes.subarray(0, HEAD);
|
|
56
|
+
const head = await open(LABEL.seal, [agreement], lid, bytes.subarray(HEAD, HEAD + SEALED_HEAD));
|
|
57
|
+
return head === null ? null : { lid, agreement, head, rest: bytes.subarray(HEAD + SEALED_HEAD) };
|
|
58
|
+
};
|
|
59
|
+
// A knock's rest split into its ciphertext and sealed body.
|
|
60
|
+
export const splitKnock = (rest) => rest.length < CIPHERTEXT ? null : { ciphertext: rest.subarray(0, CIPHERTEXT), sealedBody: rest.subarray(CIPHERTEXT) };
|
|
61
|
+
export const openBody = (opened, edge, sealedBody) => open(LABEL.edgeSeal, [opened.agreement, edge], opened.lid, sealedBody);
|
|
62
|
+
export const replyKey = async (draw, lid) => {
|
|
63
|
+
const ephemeral = await Ephemeral.draw(draw);
|
|
64
|
+
const agreement = await ephemeral.agree(lid);
|
|
65
|
+
return agreement === null ? null : { ephemeral, agreement };
|
|
66
|
+
};
|
|
67
|
+
export const sealReply = async (key, body) => concat(key.ephemeral.pk, await seal(LABEL.seal, [key.agreement], key.ephemeral.pk, body));
|
|
68
|
+
// A reply's body and agreement, opened with the lid's secret, or null.
|
|
69
|
+
export const openReply = async (bytes, lid) => {
|
|
70
|
+
if (bytes.length > SIZE || bytes.length < HEAD + TAG)
|
|
71
|
+
return null;
|
|
72
|
+
const pk = bytes.subarray(0, HEAD);
|
|
73
|
+
const agreement = await lid.agree(pk);
|
|
74
|
+
if (agreement === null)
|
|
75
|
+
return null;
|
|
76
|
+
const body = await open(LABEL.seal, [agreement], pk, bytes.subarray(HEAD));
|
|
77
|
+
return body === null ? null : { bytes: body, agreement };
|
|
78
|
+
};
|