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.
@@ -1,89 +0,0 @@
1
- import { before, after, describe, it } from 'node:test'
2
- import assert from 'node:assert/strict'
3
- import { spawn, spawnSync } from 'node:child_process'
4
- import { mkdtempSync, rmSync, readFileSync } from 'node:fs'
5
- import { tmpdir } from 'node:os'
6
- import { join, resolve } from 'node:path'
7
- import { fileURLToPath } from 'node:url'
8
- import readline from 'node:readline'
9
- import vm from 'node:vm'
10
- import Source from '../../src/index.js'
11
- import Distribution from '../../dist/boho.js'
12
-
13
- const directory = fileURLToPath(new URL('.', import.meta.url))
14
- const arduino = process.env.BOHO_ARDUINO_PATH || resolve(directory, '../../../boho-arduino')
15
- let build, executable
16
- before(() => {
17
- build = mkdtempSync(join(tmpdir(), 'boho-arduino-'))
18
- executable = join(build, 'interop')
19
- const args = ['-std=c++11', '-Wno-deprecated-declarations', '-I', join(directory, 'shims'), '-I', join(arduino, 'src'),
20
- join(directory, 'host.cpp'), join(arduino, 'src/Boho.cpp'), '-o', executable]
21
- if (process.platform !== 'darwin') args.push('-lcrypto')
22
- const result = spawnSync('c++', args, { encoding: 'utf8', timeout: 30000 })
23
- assert.equal(result.status, 0, result.stderr || String(result.error))
24
- })
25
- after(() => { if (build) rmSync(build, { recursive: true, force: true }) })
26
-
27
- async function withPeer(run) {
28
- const child = spawn(executable, [], { stdio: ['pipe', 'pipe', 'pipe'] })
29
- const lines = readline.createInterface({ input: child.stdout })[Symbol.asyncIterator]()
30
- const ask = async (command, value = '') => {
31
- const reply = lines.next()
32
- child.stdin.write(`${command} ${typeof value === 'string' ? value : Buffer.from(value).toString('hex')}\n`)
33
- const line = await reply
34
- assert.equal(line.done, false, 'C++ peer closed unexpectedly')
35
- return line.value
36
- }
37
- try { await run(ask) } finally { child.stdin.end(); child.kill(); await lines.return() }
38
- }
39
-
40
- const browser = { crypto: globalThis.crypto, TextEncoder, TextDecoder, Uint8Array, ArrayBuffer }
41
- vm.runInNewContext(readFileSync(new URL('../../dist/boho.min.js', import.meta.url), 'utf8'), browser)
42
- for (const [name, B] of [['source', Source], ['ESM', Distribution], ['UMD', browser.Boho]]) {
43
- describe(`${name} ↔ unchanged Arduino Boho.cpp`, { timeout: 10000 }, () => {
44
- it('exchanges normal and E2E packets in both directions', async () => withPeer(async ask => {
45
- const b = new B(); b.set_key('interop-shared-key')
46
- for (const size of [1, 31, 32, 33, 257]) {
47
- const plain = B.Buffer.from(Array.from({length: size}, (_, i) => i & 255))
48
- const encoded = B.Buffer.from(await ask('PACK', plain), 'hex')
49
- assert.equal(b.decryptPack(encoded).data.toString('hex'), plain.toString('hex'))
50
- assert.equal(await ask('UNPACK', b.encryptPack(plain)), plain.toString('hex'))
51
- const e2e = B.Buffer.from(await ask('E2E', plain), 'hex')
52
- assert.equal(b.decrypt_e2e(e2e, 'interop-e2e-key').data.toString('hex'), plain.toString('hex'))
53
- assert.equal(await ask('DE2E', b.encrypt_e2e(plain, 'interop-e2e-key')), plain.toString('hex'))
54
- }
55
- }))
56
- it('authenticates the Arduino client and exchanges 488 packets', async () => withPeer(async ask => {
57
- const server = new B(); server.set_key('interop-shared-key')
58
- const request = B.Buffer.from(await ask('AUTH', server.server_time_nonce()), 'hex')
59
- const response = server.verify_auth_req(request)
60
- assert.ok(response)
61
- assert.equal(await ask('VERIFY', response), '1')
62
- for (const size of [1, 31, 32, 33, 257]) {
63
- const plain = B.Buffer.alloc(size, 0x5a)
64
- const packet = B.Buffer.from(await ask('SEND', plain), 'hex')
65
- assert.equal(server.decrypt_488(packet).toString('hex'), plain.toString('hex'))
66
- assert.equal(server.decrypt_488(packet).toString('hex'), plain.toString('hex'))
67
- assert.equal(await ask('RECV', server.encrypt_488(plain)), plain.toString('hex'))
68
- }
69
- }))
70
- it('accepts Arduino counter wrap and clock resynchronization', async () => withPeer(async ask => {
71
- const server = new B(); server.set_key('interop-shared-key')
72
- const challenge = server.server_time_nonce()
73
- // The challenge counter seeds the Arduino sender; force wrap on send 2.
74
- challenge.writeUInt16LE(65534, 7)
75
- server.auth_salt12 = B.Buffer.from(challenge.subarray(1))
76
- const request = B.Buffer.from(await ask('AUTH', challenge), 'hex')
77
- assert.equal(await ask('VERIFY', server.verify_auth_req(request)), '1')
78
- const plain = B.Buffer.from('counter wrap')
79
- for (const expected of [65535, 0, 1]) {
80
- const packet = B.Buffer.from(await ask('SEND', plain), 'hex')
81
- assert.equal(packet.readUInt16LE(11), expected)
82
- assert.equal(server.decrypt_488(packet).toString(), plain.toString())
83
- }
84
- await ask('TIME', '100 0')
85
- const oldClockPacket = B.Buffer.from(await ask('SEND', plain), 'hex')
86
- assert.equal(server.decrypt_488(oldClockPacket).toString(), plain.toString())
87
- }))
88
- })
89
- }
@@ -1 +0,0 @@
1
- #pragma once
@@ -1,46 +0,0 @@
1
- #pragma once
2
- #include <cstring>
3
- #if defined(__APPLE__)
4
- #include <CommonCrypto/CommonDigest.h>
5
- #else
6
- #include <openssl/sha.h>
7
- #endif
8
- class Hash {
9
- public:
10
- virtual ~Hash() = default;
11
- virtual void reset() = 0;
12
- virtual void update(const void*, size_t) = 0;
13
- virtual void finalize(void*, size_t) = 0;
14
- };
15
- // Host adapter only: production Boho.cpp calls the same SHA-256 interface.
16
- class SHA256 : public Hash {
17
- #if defined(__APPLE__)
18
- CC_SHA256_CTX context;
19
- #else
20
- SHA256_CTX context;
21
- #endif
22
- public:
23
- void reset() override {
24
- #if defined(__APPLE__)
25
- CC_SHA256_Init(&context);
26
- #else
27
- SHA256_Init(&context);
28
- #endif
29
- }
30
- void update(const void* data, size_t size) override {
31
- #if defined(__APPLE__)
32
- CC_SHA256_Update(&context, data, static_cast<CC_LONG>(size));
33
- #else
34
- SHA256_Update(&context, data, size);
35
- #endif
36
- }
37
- void finalize(void* output, size_t size) override {
38
- unsigned char digest[32];
39
- #if defined(__APPLE__)
40
- CC_SHA256_Final(digest, &context);
41
- #else
42
- SHA256_Final(digest, &context);
43
- #endif
44
- std::memcpy(output, digest, size < 32 ? size : 32);
45
- }
46
- };
@@ -1,5 +0,0 @@
1
- {
2
- "key": "compatibility-test-key",
3
- "plaintext": "Boho 2.1.3 compatibility",
4
- "packet": "b618000000eec5b46ab80201006b0e83fa12895eb8eaf71eec9fb0b90539ce674dd94110824f8374b15a225069a0ce500f"
5
- }
@@ -1,196 +0,0 @@
1
- import { describe, it } from 'node:test'
2
- import assert from 'node:assert/strict'
3
- import { readFileSync } from 'node:fs'
4
- import vm from 'node:vm'
5
- import Source from '../src/index.js'
6
- import Distribution from '../dist/boho.js'
7
-
8
- const browser = { crypto: globalThis.crypto, TextEncoder, TextDecoder, Uint8Array, ArrayBuffer }
9
- vm.runInNewContext(readFileSync(new URL('../dist/boho.min.js', import.meta.url), 'utf8'), browser)
10
- const fixture = JSON.parse(readFileSync(new URL('./fixtures/legacy-pack.json', import.meta.url)))
11
-
12
- for (const [name, B] of [['source', Source], ['ESM', Distribution], ['browser UMD', browser.Boho]]) {
13
- const bytes = value => B.Buffer.from(value)
14
- function pair() {
15
- const client = new B(), server = new B()
16
- client.set_id8('test-id'); client.set_key('shared-test-key')
17
- const request = client.auth_req(server.server_time_nonce())
18
- server.set_key('shared-test-key')
19
- assert.equal(client.verify_auth_res(server.verify_auth_req(request)), true)
20
- return { client, server, request }
21
- }
22
- describe(`${name}: security regressions`, () => {
23
- it('rejects missing, cleared, empty and incorrectly sized keys', () => {
24
- const b = new B()
25
- assert.throws(() => b.encryptPack('hello'), /key/i)
26
- assert.equal(b.auth_req(new B().server_time_nonce()), false)
27
- assert.throws(() => b.set_key(''), /empty/i)
28
- assert.throws(() => b.copy_key(bytes([1])), /32/)
29
- b.set_key('key'); b.clearAuth()
30
- assert.throws(() => b.encryptPack('hello'), /key/i)
31
- assert.equal(b.isAuthorized, false)
32
- })
33
- it('decrypts legacy 2.1.3 packet bytes', () => {
34
- const b = new B(); b.set_key(fixture.key)
35
- assert.equal(b.decryptPack(B.Buffer.from(fixture.packet, 'hex')).data.toString(), fixture.plaintext)
36
- })
37
- it('round trips empty and multi-block binary messages', () => {
38
- const b = new B(); b.set_key('key')
39
- for (const size of [0, 1, 31, 32, 33, 1024]) {
40
- const plain = B.Buffer.alloc(size, 0xa5)
41
- const packet = b.encryptPack(plain)
42
- assert.equal(packet.length, size + B.MetaSize.ENC_PACK)
43
- assert.deepEqual(Array.from(b.decryptPack(new Uint8Array(packet)).data), Array.from(plain))
44
- }
45
- const {client, server} = pair()
46
- assert.equal(server.decrypt_488(client.encrypt_488('')).length, 0)
47
- })
48
- it('rejects all truncated lengths, bad types, lengths and trailing bytes', () => {
49
- const b = new B(); b.set_key('key')
50
- const packet = b.encryptPack('test message')
51
- for (let length = 0; length < packet.length; length++) {
52
- assert.equal(b.decryptPack(packet.subarray(0, length)), undefined)
53
- }
54
- for (const invalid of [undefined, null, {}, 'text', [], bytes([B.BohoMsg.ENC_PACK])]) {
55
- assert.equal(b.decryptPack(invalid), undefined)
56
- }
57
- const wrongType = bytes(packet); wrongType[0] = B.BohoMsg.ENC_488
58
- const wrongLength = bytes(packet); wrongLength.writeUInt32LE(0, 1)
59
- for (const invalid of [wrongType, wrongLength, B.Buffer.concat([packet, bytes([0])])]) {
60
- assert.equal(b.decryptPack(invalid), undefined)
61
- }
62
- })
63
- it('rejects tampering in every byte of a normal packet', () => {
64
- const b = new B(); b.set_key('key')
65
- const packet = b.encryptPack('payload crossing a thirty two byte boundary')
66
- for (let i = 0; i < packet.length; i++) {
67
- const modified = bytes(packet); modified[i] ^= 1
68
- assert.equal(b.decryptPack(modified), undefined, `byte ${i}`)
69
- }
70
- })
71
- it('rejects wrong keys', () => {
72
- const b = new B(), other = new B(); b.set_key('key'); other.set_key('other')
73
- assert.equal(other.decryptPack(b.encryptPack('secret')), undefined)
74
- })
75
- it('requires authorization on both 488 entry points', () => {
76
- const {client, server} = pair(), packet = client.encrypt_488('hello')
77
- server.isAuthorized = false
78
- assert.equal(server.decrypt_488(packet), undefined)
79
- assert.equal(server.encrypt_488('hello'), undefined)
80
- })
81
- it('rejects malformed 488 packets without consuming replay state', () => {
82
- const {client, server} = pair(), packet = client.encrypt_488('hello')
83
- for (let length = 0; length < packet.length; length++) {
84
- assert.equal(server.decrypt_488(packet.subarray(0, length)), undefined)
85
- }
86
- for (let i = 0; i < packet.length; i++) {
87
- const modified = bytes(packet); modified[i] ^= 1
88
- assert.equal(server.decrypt_488(modified), undefined)
89
- }
90
- assert.equal(server.decrypt_488(B.Buffer.concat([packet, bytes([0])])), undefined)
91
- assert.equal(server.decrypt_488(packet).toString(), 'hello')
92
- })
93
- it('accepts iosignal E2E routing headers without decrypting the opaque tail', () => {
94
- const {client, server} = pair()
95
- const routingHeader = bytes([0x42, 1, 120, 0])
96
- const encryptedHeader = client.encrypt_488(routingHeader)
97
- encryptedHeader[0] = B.BohoMsg.ENC_E2E
98
- const opaqueBody = client.encrypt_e2e('body', 'separate-key')
99
- const wire = B.Buffer.concat([encryptedHeader, opaqueBody])
100
- const originalWire = bytes(wire)
101
- const truncated = wire.subarray(0, B.MetaSize.ENC_488 - 1)
102
- assert.equal(server.decrypt_488(truncated), undefined)
103
- const badLength = bytes(wire); badLength.writeUInt32LE(wire.length, 1)
104
- assert.equal(server.decrypt_488(badLength), undefined)
105
- const badTag = bytes(wire); badTag[13] ^= 1
106
- assert.equal(server.decrypt_488(badTag), undefined)
107
- assert.deepEqual(Array.from(server.decrypt_488(wire)), Array.from(routingHeader))
108
- assert.deepEqual(Array.from(wire), Array.from(originalWire))
109
- assert.deepEqual(Array.from(server.decrypt_488(wire)), Array.from(routingHeader))
110
- assert.equal(server.decrypt_e2e(opaqueBody, 'separate-key').data.toString(), 'body')
111
- })
112
- it('leaves replay and delivery-order policy to iosignal', () => {
113
- const {client, server} = pair()
114
- const first = client.encrypt_488('first'), second = client.encrypt_488('second')
115
- assert.equal(server.decrypt_488(second).toString(), 'second')
116
- assert.equal(server.decrypt_488(first).toString(), 'first')
117
- assert.equal(server.decrypt_488(second).toString(), 'second')
118
- })
119
- it('leaves authentication request reuse policy to iosignal', () => {
120
- const {server, request, client} = pair()
121
- server.set_key('shared-test-key')
122
- const response = server.verify_auth_req(request)
123
- assert.ok(response)
124
- assert.equal(client.verify_auth_res(response), true)
125
- assert.equal(server.isAuthorized, true)
126
- })
127
- it('rejects malformed auth messages and full-length forged responses', () => {
128
- const c = new B(), s = new B(); c.set_key('key'); s.set_key('key')
129
- const challenge = s.server_time_nonce()
130
- const wrongChallenge = bytes(challenge); wrongChallenge[0] ^= 1
131
- assert.equal(c.auth_req(wrongChallenge), false)
132
- assert.equal(c.auth_req(challenge.subarray(0, 12)), false)
133
- const request = c.auth_req(challenge)
134
- for (const bad of [null, {}, request.subarray(0, 44), B.Buffer.concat([request, bytes([0])])]) {
135
- assert.equal(s.verify_auth_req(bad), false)
136
- }
137
- const wrongRequest = bytes(request); wrongRequest[0] ^= 1
138
- assert.equal(s.verify_auth_req(wrongRequest), false)
139
- const response = s.verify_auth_req(request)
140
- const wrongResponse = bytes(response); wrongResponse[1] ^= 1
141
- assert.ok(!c.verify_auth_res(wrongResponse)); assert.equal(c.isAuthorized, false)
142
- const wrongType = bytes(response); wrongType[0] ^= 1
143
- assert.ok(!c.verify_auth_res(wrongType))
144
- assert.equal(c.verify_auth_res(response), true)
145
- })
146
- it('preserves authorization when iosignal reloads the connection key', () => {
147
- const {client, server} = pair()
148
- client.set_key('shared-test-key')
149
- assert.equal(client.isAuthorized, true)
150
- assert.equal(server.decrypt_488(client.encrypt_488('reloaded')).toString(), 'reloaded')
151
- server.copy_key(B.sha256.hash('shared-test-key'))
152
- assert.equal(server.isAuthorized, true)
153
- assert.equal(client.decrypt_488(server.encrypt_488('reply')).toString(), 'reply')
154
- // Caller starts reauthentication explicitly, just as it does on reconnect.
155
- client.isAuthorized = false; server.isAuthorized = false
156
- const request = client.auth_req(server.server_time_nonce())
157
- assert.equal(client.verify_auth_res(server.verify_auth_req(request)), true)
158
- })
159
- it('restores E2E state on success, rejection and thrown errors', () => {
160
- const {client, server} = pair()
161
- const before = Array.from(client._otpSrc44)
162
- const packet = client.encrypt_e2e('e2e payload', 'e2e key')
163
- assert.equal(client.decrypt_e2e(packet, 'e2e key').data.toString(), 'e2e payload')
164
- assert.equal(client.decrypt_e2e(bytes([B.BohoMsg.ENC_PACK]), 'e2e key'), undefined)
165
- const original = client.decryptPack
166
- client.decryptPack = () => { throw new Error('forced failure') }
167
- assert.throws(() => client.decrypt_e2e(packet, 'e2e key'), /forced failure/)
168
- client.decryptPack = original
169
- assert.deepEqual(Array.from(client._otpSrc44), before)
170
- assert.equal(client.isAuthorized, true)
171
- assert.equal(server.decrypt_488(client.encrypt_488('still works')).toString(), 'still works')
172
- const keyless = new B(); keyless.encrypt_e2e('hello', 'temporary')
173
- assert.throws(() => keyless.encryptPack('hello'), /key/i)
174
- })
175
- it('keeps sending clocks monotonic through rollback and counter wrap', () => {
176
- const b = new B(); b.set_key('key')
177
- b._lastSendTime = Date.now() + 10000; b.counter = 65535
178
- const lastTime = b._lastSendTime
179
- b.set_clock_nonce(bytes([1, 2, 3, 4]))
180
- assert.equal(b._lastSendTime, lastTime + 1)
181
- assert.equal(b.counter, 0)
182
- const before = b._lastSendTime
183
- b.set_clock_nonce(bytes([1, 2, 3, 4]))
184
- assert.equal(b._lastSendTime, before)
185
- assert.equal(b.counter, 1)
186
- })
187
- })
188
- }
189
-
190
- it('both distribution source maps include the installed meta-buffer-pack build', () => {
191
- const installed = readFileSync(new URL('../node_modules/meta-buffer-pack/dist/meta-buffer-pack.js', import.meta.url), 'utf8')
192
- for (const file of ['boho.js.map', 'boho.min.js.map']) {
193
- const map = JSON.parse(readFileSync(new URL(`../dist/${file}`, import.meta.url)))
194
- assert.ok(map.sourcesContent.includes(installed), file)
195
- }
196
- })