boho 2.2.2 → 3.0.0

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.
@@ -0,0 +1,370 @@
1
+ {
2
+ "format": "boho-vectors",
3
+ "version": 1,
4
+ "design": "docs/PROTOCOL.md",
5
+ "notes": "All byte strings are lowercase hex. Integers are little-endian on the wire. Ascon is NIST SP 800-232.",
6
+ "constants": {
7
+ "types": {
8
+ "SERVER_TIME_NONCE": 176,
9
+ "AUTH_FAIL": 179,
10
+ "AUTH_REQ": 180,
11
+ "AUTH_RES": 181,
12
+ "ENC_PACK": 185,
13
+ "ENC_E2E": 186,
14
+ "ENC": 187
15
+ },
16
+ "dir": {
17
+ "CLIENT_TO_SERVER": 1,
18
+ "SERVER_TO_CLIENT": 2
19
+ },
20
+ "custom": {
21
+ "KEY": "boho/v2/key",
22
+ "SESSION": "boho/v2/session"
23
+ },
24
+ "sizes": {
25
+ "SERVER_TIME_NONCE": 13,
26
+ "AUTH_REQ": 33,
27
+ "AUTH_RES": 17,
28
+ "TRANSCRIPT": 29,
29
+ "TAG": 8,
30
+ "PROOF": 16,
31
+ "ENC_HEADER": 21,
32
+ "ENC_AD": 13,
33
+ "ENC_PACK_HEADER": 29,
34
+ "ENC_PACK_AD": 5
35
+ }
36
+ },
37
+ "keys": [
38
+ {
39
+ "key": "secret",
40
+ "derived": "0205c25bd8a0ff8939d3a66b562f85da"
41
+ },
42
+ {
43
+ "key": "interop-shared-key",
44
+ "derived": "09f5d47848d018906ff990f3858cb544"
45
+ },
46
+ {
47
+ "key": "비밀키-한글",
48
+ "derived": "2d15722fecfb11d1a4e3a3e1806fe861"
49
+ },
50
+ {
51
+ "key": "kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk",
52
+ "derived": "7c7213ef3dd6488488291d2c584dd43e"
53
+ }
54
+ ],
55
+ "logins": [
56
+ {
57
+ "key": "secret",
58
+ "id": "dev1",
59
+ "challenge": "b0008124697b003412a1b2c3d4",
60
+ "clientNonce": "0001020304050607",
61
+ "transcript": "b0008124697b003412a1b2c3d464657631000000000001020304050607",
62
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
63
+ "clientProof": "62bc01ab5d82448e87ebc9634b6ed509",
64
+ "serverProof": "c6ff42959c567faa9f2138bbc46cf02c",
65
+ "authReq": "b46465763100000000000102030405060762bc01ab5d82448e87ebc9634b6ed509",
66
+ "authRes": "b5c6ff42959c567faa9f2138bbc46cf02c"
67
+ },
68
+ {
69
+ "key": "interop-shared-key",
70
+ "id": "12345678",
71
+ "challenge": "b001812469e703000000000000",
72
+ "clientNonce": "f0e1d2c3b4a59687",
73
+ "transcript": "b001812469e7030000000000003132333435363738f0e1d2c3b4a59687",
74
+ "sessionKey": "45f73ddb32d058a3da3e9f72db4bc53e",
75
+ "clientProof": "11bc6bb674f394d52a9c7a7c8f935ed2",
76
+ "serverProof": "6e1a58058927a1ce5fa0fbbf70c99873",
77
+ "authReq": "b43132333435363738f0e1d2c3b4a5968711bc6bb674f394d52a9c7a7c8f935ed2",
78
+ "authRes": "b56e1a58058927a1ce5fa0fbbf70c99873"
79
+ },
80
+ {
81
+ "key": "비밀키-한글",
82
+ "id": "uno",
83
+ "challenge": "b0000000000000ffffffffffff",
84
+ "clientNonce": "ffffffffffffffff",
85
+ "transcript": "b0000000000000ffffffffffff756e6f0000000000ffffffffffffffff",
86
+ "sessionKey": "31f9fe5b6fdc1c3fa5a5f45dc483e5eb",
87
+ "clientProof": "3495e8ffaea837ec6f4cad4f033dfa61",
88
+ "serverProof": "b207eddb5cb89e4222cad32c4e3d6eeb",
89
+ "authReq": "b4756e6f0000000000ffffffffffffffff3495e8ffaea837ec6f4cad4f033dfa61",
90
+ "authRes": "b5b207eddb5cb89e4222cad32c4e3d6eeb"
91
+ }
92
+ ],
93
+ "messages": [
94
+ {
95
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
96
+ "type": "ENC",
97
+ "dir": 1,
98
+ "sec": 1764000000,
99
+ "ms": 124,
100
+ "counter": 4661,
101
+ "plaintext": "",
102
+ "e2eBody": "",
103
+ "packet": "bb00000000008124697c003512b706d62ac02cb5a2"
104
+ },
105
+ {
106
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
107
+ "type": "ENC",
108
+ "dir": 1,
109
+ "sec": 1764000000,
110
+ "ms": 125,
111
+ "counter": 4662,
112
+ "plaintext": "d068656c6c6f",
113
+ "e2eBody": "",
114
+ "packet": "bb06000000008124697d0036120979acf524da1a41ab37b9c4776f"
115
+ },
116
+ {
117
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
118
+ "type": "ENC",
119
+ "dir": 2,
120
+ "sec": 1764000000,
121
+ "ms": 125,
122
+ "counter": 4662,
123
+ "plaintext": "d068656c6c6f",
124
+ "e2eBody": "",
125
+ "packet": "bb06000000008124697d003612c12ee3ccc1a178112a5423f7e47b"
126
+ },
127
+ {
128
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
129
+ "type": "ENC",
130
+ "dir": 1,
131
+ "sec": 1764000002,
132
+ "ms": 0,
133
+ "counter": 1,
134
+ "plaintext": "000102030405060708090a0b0c0d0e",
135
+ "e2eBody": "",
136
+ "packet": "bb0f0000000281246900000100ebd83778ccead0a84e7733931f1e6709c3e750f98b4aff"
137
+ },
138
+ {
139
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
140
+ "type": "ENC",
141
+ "dir": 1,
142
+ "sec": 1764000002,
143
+ "ms": 0,
144
+ "counter": 2,
145
+ "plaintext": "000102030405060708090a0b0c0d0e0f",
146
+ "e2eBody": "",
147
+ "packet": "bb100000000281246900000200516b20125acee70fdcd289ae7ed0548a78cb7d89efffabed"
148
+ },
149
+ {
150
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
151
+ "type": "ENC",
152
+ "dir": 1,
153
+ "sec": 1764000002,
154
+ "ms": 0,
155
+ "counter": 3,
156
+ "plaintext": "000102030405060708090a0b0c0d0e0f10",
157
+ "e2eBody": "",
158
+ "packet": "bb110000000281246900000300bafee49163e629598398a78031ae6d2d999905f24f73833e49"
159
+ },
160
+ {
161
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
162
+ "type": "ENC",
163
+ "dir": 2,
164
+ "sec": 4294967295,
165
+ "ms": 999,
166
+ "counter": 65535,
167
+ "plaintext": "0708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f4041",
168
+ "e2eBody": "",
169
+ "packet": "bb3b000000ffffffffe703ffffb3f757b649b93bfda284e65b77f92c14c63d77c28e33fd48572a50b8332f799cf11822fee54d34e9d5da7a960da60c69d10d02114991f3a37d2cbfdd8f9cb95489bef8"
170
+ },
171
+ {
172
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
173
+ "type": "ENC_E2E",
174
+ "dir": 1,
175
+ "sec": 1764000003,
176
+ "ms": 500,
177
+ "counter": 9,
178
+ "plaintext": "d2d3d4d5d6d7d8d9dadb",
179
+ "e2eBody": "b9aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
180
+ "packet": "ba0a00000003812469f4010900b7c8467ee4d52df58afa9c523657bf47afdbb9aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
181
+ }
182
+ ],
183
+ "packs": [
184
+ {
185
+ "key": "e2e-shared-key",
186
+ "sec": 1764000000,
187
+ "ms": 1,
188
+ "counter": 1,
189
+ "sender": "0102030405060708",
190
+ "plaintext": "",
191
+ "packet": "b900000000008124690100010001020304050607084049dbb40720a796"
192
+ },
193
+ {
194
+ "key": "e2e-shared-key",
195
+ "sec": 1764000000,
196
+ "ms": 1,
197
+ "counter": 2,
198
+ "sender": "0102030405060708",
199
+ "plaintext": "6f6e3d313b6c6576656c3d3730",
200
+ "packet": "b90d00000000812469010002000102030405060708906f57b3b809fef346d437e233c31819f814da9f7c"
201
+ },
202
+ {
203
+ "key": "e2e-shared-key",
204
+ "sec": 1764000000,
205
+ "ms": 1,
206
+ "counter": 2,
207
+ "sender": "1112131415161718",
208
+ "plaintext": "6f6e3d313b6c6576656c3d3730",
209
+ "packet": "b90d00000000812469010002001112131415161718615eadc83b1942522b56efeaabad60fe2fd0b137e8"
210
+ },
211
+ {
212
+ "key": "e2e-shared-key",
213
+ "sec": 1764000009,
214
+ "ms": 999,
215
+ "counter": 65535,
216
+ "sender": "fffefdfcfbfaf9f8",
217
+ "plaintext": "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2021222324252627",
218
+ "packet": "b92800000009812469e703fffffffefdfcfbfaf9f805e08c87adfa8387f16f6d04fdaf2bfeff79971faa8b390c63bd12bfbfecd749df084e06d487a75fe1174f70f837e54f"
219
+ }
220
+ ],
221
+ "invalid": [
222
+ {
223
+ "kind": "message",
224
+ "reason": "tag bit flipped",
225
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
226
+ "dir": 1,
227
+ "packet": "bb06000000008124697d0036120879acf524da1a41ab37b9c4776f"
228
+ },
229
+ {
230
+ "kind": "message",
231
+ "reason": "ciphertext bit flipped",
232
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
233
+ "dir": 1,
234
+ "packet": "bb06000000008124697d0036120979acf524da1a41aa37b9c4776f"
235
+ },
236
+ {
237
+ "kind": "message",
238
+ "reason": "clock changed",
239
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
240
+ "dir": 1,
241
+ "packet": "bb06000000008124697d0037120979acf524da1a41ab37b9c4776f"
242
+ },
243
+ {
244
+ "kind": "message",
245
+ "reason": "ENC relabelled ENC_E2E",
246
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
247
+ "dir": 1,
248
+ "packet": "ba06000000008124697d0036120979acf524da1a41ab37b9c4776f"
249
+ },
250
+ {
251
+ "kind": "message",
252
+ "reason": "other direction",
253
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
254
+ "dir": 2,
255
+ "packet": "bb06000000008124697d0036120979acf524da1a41ab37b9c4776f"
256
+ },
257
+ {
258
+ "kind": "message",
259
+ "reason": "other session key",
260
+ "sessionKey": "45f73ddb32d058a3da3e9f72db4bc53e",
261
+ "dir": 1,
262
+ "packet": "bb06000000008124697d0036120979acf524da1a41ab37b9c4776f"
263
+ },
264
+ {
265
+ "kind": "message",
266
+ "reason": "length one short",
267
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
268
+ "dir": 1,
269
+ "packet": "bb05000000008124697d0036120979acf524da1a41ab37b9c4776f"
270
+ },
271
+ {
272
+ "kind": "message",
273
+ "reason": "trailing byte on ENC",
274
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
275
+ "dir": 1,
276
+ "packet": "bb06000000008124697d0036120979acf524da1a41ab37b9c4776f00"
277
+ },
278
+ {
279
+ "kind": "message",
280
+ "reason": "milliseconds 1000",
281
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
282
+ "dir": 1,
283
+ "packet": "bb0600000000812469e80336120979acf524da1a41ab37b9c4776f"
284
+ },
285
+ {
286
+ "kind": "message",
287
+ "reason": "shorter than header",
288
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
289
+ "dir": 1,
290
+ "packet": "bb06000000008124697d0036120979acf524da1a"
291
+ },
292
+ {
293
+ "kind": "message",
294
+ "reason": "classic ENC_488 type",
295
+ "sessionKey": "96a166689a2145db1b08864174f54e10",
296
+ "dir": 1,
297
+ "packet": "b806000000008124697d0036120979acf524da1a41ab37b9c4776f"
298
+ },
299
+ {
300
+ "kind": "pack",
301
+ "reason": "tag bit flipped",
302
+ "key": "e2e-shared-key",
303
+ "packet": "b90d00000000812469010002000102030405060708916f57b3b809fef346d437e233c31819f814da9f7c"
304
+ },
305
+ {
306
+ "kind": "pack",
307
+ "reason": "sender changed",
308
+ "key": "e2e-shared-key",
309
+ "packet": "b90d00000000812469010002000002030405060708906f57b3b809fef346d437e233c31819f814da9f7c"
310
+ },
311
+ {
312
+ "kind": "pack",
313
+ "reason": "ciphertext bit flipped",
314
+ "key": "e2e-shared-key",
315
+ "packet": "b90d00000000812469010002000102030405060708906f57b3b809fef3c6d437e233c31819f814da9f7c"
316
+ },
317
+ {
318
+ "kind": "pack",
319
+ "reason": "other key",
320
+ "key": "secret",
321
+ "packet": "b90d00000000812469010002000102030405060708906f57b3b809fef346d437e233c31819f814da9f7c"
322
+ },
323
+ {
324
+ "kind": "pack",
325
+ "reason": "classic ENC_PACK type",
326
+ "key": "e2e-shared-key",
327
+ "packet": "b60d00000000812469010002000102030405060708906f57b3b809fef346d437e233c31819f814da9f7c"
328
+ },
329
+ {
330
+ "kind": "authReq",
331
+ "reason": "proof bit flipped",
332
+ "key": "secret",
333
+ "challenge": "b0008124697b003412a1b2c3d4",
334
+ "packet": "b46465763100000000000102030405060762bc01ab5d82448e87ebc9634b6ed508"
335
+ },
336
+ {
337
+ "kind": "authReq",
338
+ "reason": "other challenge",
339
+ "key": "secret",
340
+ "challenge": "b001812469e703000000000000",
341
+ "packet": "b46465763100000000000102030405060762bc01ab5d82448e87ebc9634b6ed509"
342
+ },
343
+ {
344
+ "kind": "authReq",
345
+ "reason": "other key",
346
+ "key": "Secret",
347
+ "challenge": "b0008124697b003412a1b2c3d4",
348
+ "packet": "b46465763100000000000102030405060762bc01ab5d82448e87ebc9634b6ed509"
349
+ },
350
+ {
351
+ "kind": "authReq",
352
+ "reason": "id changed",
353
+ "key": "secret",
354
+ "challenge": "b0008124697b003412a1b2c3d4",
355
+ "packet": "b46565763100000000000102030405060762bc01ab5d82448e87ebc9634b6ed509"
356
+ },
357
+ {
358
+ "kind": "authRes",
359
+ "reason": "proof bit flipped",
360
+ "sessionOf": 0,
361
+ "packet": "b5c6ff42959c567faa9f2138bbc46cf02d"
362
+ },
363
+ {
364
+ "kind": "authRes",
365
+ "reason": "client proof echoed",
366
+ "sessionOf": 0,
367
+ "packet": "b562bc01ab5d82448e87ebc9634b6ed509"
368
+ }
369
+ ]
370
+ }
@@ -12,7 +12,6 @@ import PatchedBoho from '../../dist/boho.js'
12
12
  const dir = fileURLToPath(new URL('.', import.meta.url))
13
13
  const iosignal = process.env.IOSIGNAL_PATH || resolve(dir, '../../../iosignal')
14
14
  const arduino = process.env.IOSIGNAL_ARDUINO_PATH || resolve(dir, '../../../iosignal-arduino')
15
- const bohoArduino = process.env.BOHO_ARDUINO_PATH || resolve(dir, '../../../boho-arduino')
16
15
  const load = path => import(pathToFileURL(join(iosignal, path)))
17
16
  const {Manager} = await load('src/server/Manager.js')
18
17
  const {serverOption} = await load('src/server/serverOption.js')
@@ -27,11 +26,10 @@ it('Arduino IOSignal (80-byte RX) ↔ real iosignal server ↔ JS client: auth,
27
26
  let child, lines, io, manager
28
27
  try {
29
28
  const args=['-std=c++11','-ffunction-sections','-fdata-sections','-Wno-deprecated-declarations',
30
- '-I',join(dir,'shims'),'-I',join(dir,'../arduino/shims'),'-I',join(arduino,'src'),'-I',join(bohoArduino,'src'),
31
- join(dir,'host.cpp'),join(bohoArduino,'src/Boho.cpp'),...['CongPacket.cpp','IOSignal.cpp'].map(f=>join(arduino,'src',f)),
29
+ '-I',join(dir,'shims'),'-I',join(dir,'../arduino/shims'),'-I',join(arduino,'src'),
30
+ join(dir,'host.cpp'),...['Boho.cpp','BohoAscon.cpp','CongPacket.cpp','IOSignal.cpp'].map(f=>join(arduino,'src',f)),
32
31
  '-o',executable]
33
32
  args.push(process.platform==='darwin'?'-Wl,-dead_strip':'-Wl,--gc-sections')
34
- if(process.platform!=='darwin')args.push('-lcrypto')
35
33
  const compiled=spawnSync('c++',args,{encoding:'utf8',timeout:20000})
36
34
  assert.equal(compiled.status,0,compiled.stderr||String(compiled.error))
37
35
  child=spawn(executable,[],{stdio:['pipe','pipe','inherit']})
@@ -84,7 +82,7 @@ it('Arduino IOSignal (80-byte RX) ↔ real iosignal server ↔ JS client: auth,
84
82
  io.signal('arduino@t',Buffer.from('world'));await pump()
85
83
  assert.equal(await ask('MESSAGES'),'@t:776f726c64;')
86
84
  aSocket.emit('data',Buffer.from(await ask('E2E js@t 736563726574'),'hex'));await pump()
87
- const secret=received.pop();assert.equal(secret.data[0],0xb6)
85
+ const secret=received.pop();assert.equal(secret.data[0],PatchedBoho.BohoMsg.ENC_PACK)
88
86
  assert.equal(io.decrypt_e2e(secret.data,'interop-e2e-key').data.toString(),'secret')
89
87
  assert.equal(io.decrypt_e2e(secret.data,'wrong-key'),undefined)
90
88
  io.signal_e2e('arduino@t',Buffer.from('reply'),'interop-e2e-key');await pump()
@@ -38,7 +38,7 @@ std::vector<uint8_t> unhex(const std::string& text) {
38
38
  void onMessage(char* tag, uint8_t, uint8_t* data, size_t size) {
39
39
  if (size && data[0]==Boho::ENC_PACK) {
40
40
  std::vector<uint8_t> plain(size);
41
- auto length=io.decrypt_e2e(plain.data(),data,size,"interop-e2e-key");
41
+ uint32_t length=0; io.decrypt_e2e(plain.data(),data,size,"interop-e2e-key",length);
42
42
  received.push_back(std::string(tag)+":"+hex(plain.data(),length));
43
43
  } else received.push_back(std::string(tag)+":"+hex(data,size));
44
44
  }
@@ -0,0 +1,175 @@
1
+ /*
2
+ * reference.js — independent byte-level reference of the Boho message formats on Ascon
3
+ *
4
+ * Written apart from src/boho.js (functions, no state). Used to
5
+ * make the shared test vectors (boho-vectors.json); not the production API.
6
+ *
7
+ * Author: Taeo Lee <sixgen@gmail.com>
8
+ */
9
+
10
+ import { encrypt, decrypt, cxof128 } from '../src/ascon.js'
11
+
12
+ export const TYPE = {
13
+ SERVER_TIME_NONCE: 0xB0,
14
+ AUTH_FAIL: 0xB3,
15
+ AUTH_REQ: 0xB4,
16
+ AUTH_RES: 0xB5,
17
+ ENC_PACK: 0xB9,
18
+ ENC_E2E: 0xBA,
19
+ ENC: 0xBB
20
+ }
21
+ export const DIR = { CLIENT_TO_SERVER: 1, SERVER_TO_CLIENT: 2 }
22
+ export const CUSTOM = { KEY: 'boho/v2/key', SESSION: 'boho/v2/session' }
23
+ export const SIZE = {
24
+ SERVER_TIME_NONCE: 13, AUTH_REQ: 33, AUTH_RES: 17, TRANSCRIPT: 29,
25
+ TAG: 8, PROOF: 16, ENC_HEADER: 21, ENC_AD: 13, ENC_PACK_HEADER: 29, ENC_PACK_AD: 5
26
+ }
27
+
28
+ const encoder = new TextEncoder()
29
+ const bytes = v => (typeof v === 'string' ? encoder.encode(v) : new Uint8Array(v))
30
+ const concat = (...parts) => {
31
+ const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0))
32
+ let o = 0
33
+ for (const p of parts) { out.set(p, o); o += p.length }
34
+ return out
35
+ }
36
+ const u32 = (b, o) => (b[o] | (b[o + 1] << 8) | (b[o + 2] << 16) | (b[o + 3] << 24)) >>> 0
37
+ const u16 = (b, o) => b[o] | (b[o + 1] << 8)
38
+ const put32 = (b, o, v) => { b[o] = v; b[o + 1] = v >>> 8; b[o + 2] = v >>> 16; b[o + 3] = v >>> 24 }
39
+ const put16 = (b, o, v) => { b[o] = v; b[o + 1] = v >>> 8 }
40
+ const equal = (a, b) => {
41
+ if (a.length !== b.length) return false
42
+ let d = 0
43
+ for (let i = 0; i < a.length; i++) d |= a[i] ^ b[i]
44
+ return d === 0
45
+ }
46
+
47
+ function clockOk (c) {
48
+ return Number.isInteger(c.sec) && c.sec >= 0 && c.sec <= 0xffffffff &&
49
+ Number.isInteger(c.ms) && c.ms >= 0 && c.ms < 1000 &&
50
+ Number.isInteger(c.counter) && c.counter >= 0 && c.counter <= 0xffff
51
+ }
52
+
53
+ /** Key string (the part after "id." in id.key) or E2E key string → 16-byte key. */
54
+ export function deriveKey (keyString) {
55
+ const k = bytes(keyString)
56
+ if (!k.length) throw new RangeError('key must not be empty')
57
+ return cxof128(k, 16, CUSTOM.KEY)
58
+ }
59
+
60
+ /** ID string → id8 (first 8 bytes, zero padded), as v1 set_id8. */
61
+ export function id8 (id) {
62
+ const b = bytes(id)
63
+ if (!b.length || b.length > 8) throw new RangeError('id must be 1..8 bytes')
64
+ const out = new Uint8Array(8)
65
+ out.set(b)
66
+ return out
67
+ }
68
+
69
+ /** Transcript, session key and both proofs of one login. */
70
+ export function session (key, challenge, id, clientNonce) {
71
+ if (challenge.length !== SIZE.SERVER_TIME_NONCE || challenge[0] !== TYPE.SERVER_TIME_NONCE) throw new RangeError('bad challenge')
72
+ if (id.length !== 8 || clientNonce.length !== 8) throw new RangeError('id8 and clientNonce must be 8 bytes')
73
+ const transcript = concat(challenge, id, clientNonce)
74
+ const out = cxof128(concat(key, transcript), 48, CUSTOM.SESSION)
75
+ return {
76
+ transcript,
77
+ sessionKey: out.subarray(0, 16),
78
+ clientProof: out.subarray(16, 32),
79
+ serverProof: out.subarray(32, 48)
80
+ }
81
+ }
82
+
83
+ /** Client: AUTH_REQ = type | id8 | clientNonce8 | clientProof16. */
84
+ export function authReq (key, challenge, id, clientNonce) {
85
+ const s = session(key, challenge, id, clientNonce)
86
+ return { packet: concat([TYPE.AUTH_REQ], id, clientNonce, s.clientProof), session: s }
87
+ }
88
+
89
+ /** Server: check AUTH_REQ with the key of its id8. Returns AUTH_RES and the session, or null. */
90
+ export function verifyAuthReq (key, challenge, packet) {
91
+ if (packet.length !== SIZE.AUTH_REQ || packet[0] !== TYPE.AUTH_REQ) return null
92
+ const s = session(key, challenge, packet.subarray(1, 9), packet.subarray(9, 17))
93
+ if (!equal(packet.subarray(17, 33), s.clientProof)) return null
94
+ return { packet: concat([TYPE.AUTH_RES], s.serverProof), session: s }
95
+ }
96
+
97
+ /** Client: check AUTH_RES = type | serverProof16. */
98
+ export function verifyAuthRes (s, packet) {
99
+ return packet.length === SIZE.AUTH_RES && packet[0] === TYPE.AUTH_RES && equal(packet.subarray(1), s.serverProof)
100
+ }
101
+
102
+ function header (type, length, clock) {
103
+ const h = new Uint8Array(SIZE.ENC_AD)
104
+ h[0] = type
105
+ put32(h, 1, length)
106
+ put32(h, 5, clock.sec)
107
+ put16(h, 9, clock.ms)
108
+ put16(h, 11, clock.counter)
109
+ return h
110
+ }
111
+
112
+ function sessionNonce (h, dir) {
113
+ const n = new Uint8Array(16)
114
+ n.set(h.subarray(5, 13))
115
+ n[8] = dir
116
+ return n
117
+ }
118
+
119
+ /**
120
+ * ENC / ENC_E2E = type | len4 | sec4 | ms2 | counter2 | tag8 | ciphertext [| e2eBody].
121
+ * For ENC_E2E the plaintext is the routing header and e2eBody (an ENC_PACK) follows unchanged.
122
+ */
123
+ export function seal (type, sessionKey, dir, clock, plaintext, e2eBody = new Uint8Array(0)) {
124
+ if (type !== TYPE.ENC && type !== TYPE.ENC_E2E) throw new RangeError('type must be ENC or ENC_E2E')
125
+ if (type === TYPE.ENC && e2eBody.length) throw new RangeError('ENC has no E2E body')
126
+ if (!clockOk(clock)) throw new RangeError('bad clock')
127
+ const h = header(type, plaintext.length, clock)
128
+ const sealed = encrypt(sessionKey, sessionNonce(h, dir), plaintext, h, SIZE.TAG)
129
+ const ct = sealed.subarray(0, plaintext.length), tag = sealed.subarray(plaintext.length)
130
+ return concat(h, tag, ct, e2eBody)
131
+ }
132
+
133
+ /** Opens ENC / ENC_E2E. Returns { type, sec, ms, counter, data, e2eBody } or null. */
134
+ export function open (sessionKey, dir, packet) {
135
+ if (packet.length < SIZE.ENC_HEADER) return null
136
+ const type = packet[0]
137
+ if (type !== TYPE.ENC && type !== TYPE.ENC_E2E) return null
138
+ const length = u32(packet, 1)
139
+ const room = packet.length - SIZE.ENC_HEADER
140
+ if (length > room || (type === TYPE.ENC && length !== room)) return null
141
+ const clock = { sec: u32(packet, 5), ms: u16(packet, 9), counter: u16(packet, 11) }
142
+ if (clock.ms >= 1000) return null
143
+ const h = packet.subarray(0, SIZE.ENC_AD)
144
+ const sealed = concat(packet.subarray(SIZE.ENC_HEADER, SIZE.ENC_HEADER + length), packet.subarray(13, 21))
145
+ const data = decrypt(sessionKey, sessionNonce(h, dir), sealed, h, SIZE.TAG)
146
+ if (!data) return null
147
+ return { type, ...clock, data, e2eBody: packet.slice(SIZE.ENC_HEADER + length) }
148
+ }
149
+
150
+ /** ENC_PACK = type | len4 | nonce16 (sec4 | ms2 | counter2 | sender8) | tag8 | ciphertext. */
151
+ export function sealPack (key, clock, sender, plaintext) {
152
+ if (!clockOk(clock)) throw new RangeError('bad clock')
153
+ if (sender.length !== 8) throw new RangeError('sender must be 8 bytes')
154
+ const ad = new Uint8Array(SIZE.ENC_PACK_AD)
155
+ ad[0] = TYPE.ENC_PACK
156
+ put32(ad, 1, plaintext.length)
157
+ const nonce = new Uint8Array(16)
158
+ put32(nonce, 0, clock.sec); put16(nonce, 4, clock.ms); put16(nonce, 6, clock.counter)
159
+ nonce.set(sender, 8)
160
+ const sealed = encrypt(key, nonce, plaintext, ad, SIZE.TAG)
161
+ return concat(ad, nonce, sealed.subarray(plaintext.length), sealed.subarray(0, plaintext.length))
162
+ }
163
+
164
+ /** Opens ENC_PACK. Returns { sec, ms, counter, sender, data } or null. */
165
+ export function openPack (key, packet) {
166
+ if (packet.length < SIZE.ENC_PACK_HEADER || packet[0] !== TYPE.ENC_PACK) return null
167
+ const length = u32(packet, 1)
168
+ if (length !== packet.length - SIZE.ENC_PACK_HEADER) return null
169
+ const nonce = packet.subarray(5, 21)
170
+ if (u16(nonce, 4) >= 1000) return null
171
+ const sealed = concat(packet.subarray(SIZE.ENC_PACK_HEADER), packet.subarray(21, 29))
172
+ const data = decrypt(key, nonce, sealed, packet.subarray(0, 5), SIZE.TAG)
173
+ if (!data) return null
174
+ return { sec: u32(nonce, 0), ms: u16(nonce, 4), counter: u16(nonce, 6), sender: nonce.slice(8), data }
175
+ }
@@ -1,17 +1,21 @@
1
- import Boho, { type MetaSizeType } from 'boho'
1
+ import Boho, { type MetaSizeType, type OpenedPack } from 'boho'
2
2
  // @ts-expect-error Runtime exports only the default class.
3
3
  import { RAND } from 'boho'
4
4
 
5
- const boho = new Boho()
6
- boho.set_key(Boho.RAND(32))
7
- const packet = boho.encryptPack('hello')
8
- const clear: Uint8Array | undefined = boho.decryptPack(packet)?.data
5
+ const server = new Boho()
6
+ server.copy_key(Boho.deriveKey('secret'))
7
+ const challenge = server.server_time_nonce()
8
+ const client = new Boho()
9
+ client.set_id_key('dev1.secret')
10
+ const request = client.auth_req(challenge)
11
+ const response = request ? server.verify_auth_req(request) : false
12
+ if (response) client.verify_auth_res(response)
13
+ const packet = client.encrypt_session('hello')
14
+ const plain: Uint8Array | undefined = packet ? server.decrypt_session(packet) : undefined
15
+ const header = client.encrypt_session(new Uint8Array([0xd2, 1, 0x78]), Boho.BohoMsg.ENC_E2E)
16
+ const body = client.encrypt_e2e('hello', 'e2e-key')
17
+ const opened: OpenedPack | undefined = body ? server.decrypt_e2e(body, 'e2e-key') : undefined
9
18
  const sizes: MetaSizeType = Boho.MetaSize
10
- const challengeSize: number = sizes.SERVER_TIME_NONCE
11
- const request = boho.auth_req(new Uint8Array(challengeSize))
12
- if (request) boho.verify_auth_req(request)
13
- boho.encrypt_e2e('hello', 'key')
14
- Boho.sha256.hmac(Boho.RAND(32), packet)
15
- Boho.Buffer.from('hello')
16
- Boho.MBP.unpack(packet, Boho.Meta.ENC_PACK)
17
- void clear
19
+ const tag: Uint8Array = Boho.ascon.encrypt(new Uint8Array(16), new Uint8Array(16), 'x')
20
+ Boho.sha256.hmac(Boho.RAND(32), Boho.Buffer.from('hello'))
21
+ void plain; void header; void opened; void sizes.ENC; void tag