boho 2.2.1 → 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.
- package/.github/workflows/pages.yml +51 -0
- package/README.ko.md +92 -275
- package/README.md +95 -309
- package/dist/boho.d.ts +71 -186
- package/dist/boho.js +5 -5
- package/dist/boho.js.map +1 -1
- package/dist/boho.min.js +5 -5
- package/dist/boho.min.js.map +1 -1
- package/package.json +7 -5
- package/src/ascon.js +262 -0
- package/src/boho.js +268 -471
- package/src/constants.js +23 -107
- package/src/index.js +11 -20
- package/test/ascon.test.js +225 -0
- package/test/boho.test.js +190 -0
- package/test/fixtures/boho-vectors.json +370 -0
- package/test/iosignal/full-flow.test.js +4 -4
- package/test/iosignal/host.cpp +1 -1
- package/test/reference.js +175 -0
- package/test/types/api.mts +17 -13
- package/test/vectors.test.js +75 -0
- package/test-browser/index.html +25 -114
- package/tools/make-vectors.mjs +131 -0
- package/test/AUTH_process.js +0 -93
- package/test/ENC_PACK.js +0 -26
- package/test/RAND.js +0 -18
- package/test/arduino/host.cpp +0 -51
- package/test/arduino/interop.test.js +0 -89
- package/test/arduino/shims/Crypto.h +0 -1
- package/test/arduino/shims/SHA256.h +0 -46
- package/test/fixtures/legacy-pack.json +0 -5
- package/test/security-regressions.js +0 -196
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// The independent reference (test/reference.js) against the shared vectors (fixtures/boho-vectors.json).
|
|
2
|
+
import { describe, it } from 'node:test'
|
|
3
|
+
import assert from 'node:assert/strict'
|
|
4
|
+
import { readFileSync } from 'node:fs'
|
|
5
|
+
import * as ref from './reference.js'
|
|
6
|
+
import { makeVectors } from '../tools/make-vectors.mjs'
|
|
7
|
+
|
|
8
|
+
const hex = b => Buffer.from(b).toString('hex')
|
|
9
|
+
const unhex = h => new Uint8Array(Buffer.from(h, 'hex'))
|
|
10
|
+
const vectors = JSON.parse(readFileSync(new URL('./fixtures/boho-vectors.json', import.meta.url), 'utf8'))
|
|
11
|
+
|
|
12
|
+
describe('Boho vectors', () => {
|
|
13
|
+
it('the file matches the reference (run tools/make-vectors.mjs after a change)', () => {
|
|
14
|
+
assert.deepEqual(vectors, JSON.parse(JSON.stringify(makeVectors())))
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
it('keys', () => {
|
|
18
|
+
for (const k of vectors.keys) assert.equal(hex(ref.deriveKey(k.key)), k.derived)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
it('logins: both sides agree and check each other', () => {
|
|
22
|
+
for (const l of vectors.logins) {
|
|
23
|
+
const key = ref.deriveKey(l.key), challenge = unhex(l.challenge)
|
|
24
|
+
const req = ref.authReq(key, challenge, ref.id8(l.id), unhex(l.clientNonce))
|
|
25
|
+
assert.equal(hex(req.packet), l.authReq)
|
|
26
|
+
assert.equal(hex(req.session.sessionKey), l.sessionKey)
|
|
27
|
+
const res = ref.verifyAuthReq(key, challenge, unhex(l.authReq))
|
|
28
|
+
assert.equal(hex(res.packet), l.authRes)
|
|
29
|
+
assert.ok(ref.verifyAuthRes(req.session, unhex(l.authRes)))
|
|
30
|
+
}
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('messages open to their plaintext, clock and E2E body', () => {
|
|
34
|
+
for (const m of vectors.messages) {
|
|
35
|
+
const opened = ref.open(unhex(m.sessionKey), m.dir, unhex(m.packet))
|
|
36
|
+
assert.ok(opened, m.packet)
|
|
37
|
+
assert.equal(opened.type, ref.TYPE[m.type])
|
|
38
|
+
assert.deepEqual([opened.sec, opened.ms, opened.counter], [m.sec, m.ms, m.counter])
|
|
39
|
+
assert.equal(hex(opened.data), m.plaintext)
|
|
40
|
+
assert.equal(hex(opened.e2eBody), m.e2eBody)
|
|
41
|
+
}
|
|
42
|
+
// Same clock, other direction: different bytes.
|
|
43
|
+
assert.notEqual(vectors.messages[1].packet, vectors.messages[2].packet)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('packs open to their plaintext and sender', () => {
|
|
47
|
+
for (const p of vectors.packs) {
|
|
48
|
+
const opened = ref.openPack(ref.deriveKey(p.key), unhex(p.packet))
|
|
49
|
+
assert.ok(opened, p.packet)
|
|
50
|
+
assert.equal(hex(opened.data), p.plaintext)
|
|
51
|
+
assert.equal(hex(opened.sender), p.sender)
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('invalid packets are rejected', () => {
|
|
56
|
+
const sessions = vectors.logins.map(l => ref.session(ref.deriveKey(l.key), unhex(l.challenge), ref.id8(l.id), unhex(l.clientNonce)))
|
|
57
|
+
for (const c of vectors.invalid) {
|
|
58
|
+
const packet = unhex(c.packet)
|
|
59
|
+
let result
|
|
60
|
+
if (c.kind === 'message') result = ref.open(unhex(c.sessionKey), c.dir, packet)
|
|
61
|
+
else if (c.kind === 'pack') result = ref.openPack(ref.deriveKey(c.key), packet)
|
|
62
|
+
else if (c.kind === 'authReq') result = ref.verifyAuthReq(ref.deriveKey(c.key), unhex(c.challenge), packet)
|
|
63
|
+
else if (c.kind === 'authRes') result = ref.verifyAuthRes(sessions[c.sessionOf], packet) || null
|
|
64
|
+
assert.equal(result, null, `${c.kind}: ${c.reason}`)
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('sizes follow the design', () => {
|
|
69
|
+
const l = vectors.logins[0]
|
|
70
|
+
assert.equal(l.authReq.length / 2, 33)
|
|
71
|
+
assert.equal(l.authRes.length / 2, 17)
|
|
72
|
+
for (const m of vectors.messages) assert.equal(m.packet.length / 2, 21 + (m.plaintext.length + m.e2eBody.length) / 2)
|
|
73
|
+
for (const p of vectors.packs) assert.equal(p.packet.length / 2, 29 + p.plaintext.length / 2)
|
|
74
|
+
})
|
|
75
|
+
})
|
package/test-browser/index.html
CHANGED
|
@@ -20,132 +20,43 @@
|
|
|
20
20
|
|
|
21
21
|
let assert = chai.assert;
|
|
22
22
|
const RAND = Boho.RAND
|
|
23
|
-
const MBP = Boho.MBP
|
|
24
|
-
const MetaSize = Boho.MetaSize
|
|
25
|
-
const BohoMsg = Boho.BohoMsg
|
|
26
|
-
const Meta = Boho.Meta
|
|
27
23
|
const Buffer = Boho.Buffer;
|
|
24
|
+
const expect = chai.expect
|
|
28
25
|
|
|
29
26
|
describe('RAND browser', function () {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
it('should return Uint8Array', function () {
|
|
33
|
-
assert.ok(RAND(num) instanceof Uint8Array)
|
|
34
|
-
})
|
|
35
|
-
|
|
36
|
-
it('should return number-bytes size of Uint8Array', function () {
|
|
37
|
-
assert.ok(RAND(num).byteLength === num)
|
|
38
|
-
})
|
|
39
|
-
})
|
|
40
|
-
})
|
|
41
|
-
|
|
42
|
-
describe('ENC_PACK', function () {
|
|
43
|
-
describe('encryption and decryption', function () {
|
|
44
|
-
const plainData = 'aaaaa'
|
|
45
|
-
const key = 'key'
|
|
46
|
-
|
|
47
|
-
it('should decryptPack.data property has same buffer of origin.', function () {
|
|
48
|
-
const boho = new Boho()
|
|
49
|
-
boho.set_key(key)
|
|
50
|
-
|
|
51
|
-
const encData = boho.encryptPack(plainData)
|
|
52
|
-
const decObj = boho.decryptPack(encData)
|
|
53
|
-
|
|
54
|
-
const srcBuffer = MBP.B8(plainData)
|
|
55
|
-
const resultBuffer = MBP.B8(decObj.data)
|
|
56
|
-
|
|
57
|
-
assert.ok(MBP.equal(srcBuffer, resultBuffer))
|
|
58
|
-
})
|
|
27
|
+
it('returns size bytes', function () {
|
|
28
|
+
expect(RAND(8).length).to.equal(8)
|
|
59
29
|
})
|
|
60
30
|
})
|
|
61
31
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const c = new Boho()
|
|
69
|
-
c.set_id8(id)
|
|
70
|
-
c.set_key(key)
|
|
71
|
-
|
|
72
|
-
// server side
|
|
73
|
-
const s = new Boho()
|
|
74
|
-
const auth_nonce_buffer = s.server_time_nonce()
|
|
75
|
-
const auth_req_buffer = c.auth_req(auth_nonce_buffer)
|
|
76
|
-
|
|
77
|
-
const unpack = MBP.unpack(auth_req_buffer, Meta.AUTH_REQ)
|
|
78
|
-
|
|
79
|
-
// Find hashID8 from DATABASE then set the id and key.
|
|
80
|
-
s.set_id8(id)
|
|
81
|
-
|
|
82
|
-
s.set_key('wrong-key') // if key is wrong.
|
|
83
|
-
const verify_auth_req_fail = s.verify_auth_req(unpack)
|
|
84
|
-
|
|
85
|
-
s.set_key(key) // if correct.
|
|
86
|
-
const auth_res_buffer = s.verify_auth_req(unpack)
|
|
87
|
-
|
|
88
|
-
const auth_res_buffer_with_incorrect_hmac = Boho.Buffer.alloc(MetaSize.AUTH_RES)
|
|
89
|
-
|
|
90
|
-
auth_res_buffer.copy(auth_res_buffer_with_incorrect_hmac)
|
|
91
|
-
auth_res_buffer_with_incorrect_hmac[2] ^= 0x55 // change hmac
|
|
92
|
-
const wrongSeverHMACResult = c.verify_auth_res(auth_res_buffer_with_incorrect_hmac)
|
|
93
|
-
const isCorrectSeverHMAC = c.verify_auth_res(auth_res_buffer)
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
describe('1. server send SERVER_TIME_NONCE', function () {
|
|
97
|
-
it('should return static size buffer', function () {
|
|
98
|
-
assert.equal(auth_nonce_buffer.byteLength, MetaSize.SERVER_TIME_NONCE)
|
|
99
|
-
})
|
|
100
|
-
|
|
101
|
-
it('should pack[0] is header type ', function () {
|
|
102
|
-
assert.equal(auth_nonce_buffer[0], BohoMsg.SERVER_TIME_NONCE)
|
|
103
|
-
})
|
|
32
|
+
describe('ENC_PACK (Ascon)', function () {
|
|
33
|
+
it('decryptPack().data is the original data', function () {
|
|
34
|
+
const boho = new Boho()
|
|
35
|
+
boho.set_key('key')
|
|
36
|
+
const decObj = boho.decryptPack(boho.encryptPack('aaaaa'))
|
|
37
|
+
expect(Buffer.from(decObj.data).toString()).to.equal('aaaaa')
|
|
104
38
|
})
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
assert.equal(auth_req_buffer.byteLength, MetaSize.AUTH_REQ)
|
|
109
|
-
})
|
|
110
|
-
|
|
111
|
-
it('should pack[0] is header type ', function () {
|
|
112
|
-
assert.equal(auth_req_buffer[0], BohoMsg.AUTH_REQ)
|
|
113
|
-
})
|
|
39
|
+
it('decrypt_e2e with another key fails', function () {
|
|
40
|
+
const boho = new Boho()
|
|
41
|
+
expect(boho.decrypt_e2e(boho.encrypt_e2e('x', 'a'), 'b')).to.equal(undefined)
|
|
114
42
|
})
|
|
43
|
+
})
|
|
115
44
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
// correct id and key
|
|
128
|
-
it('3-2.2. should return AUTH_RES buffer pack when correct key', function () {
|
|
129
|
-
assert.equal(auth_res_buffer[0], BohoMsg.AUTH_RES)
|
|
130
|
-
})
|
|
131
|
-
})
|
|
132
|
-
})
|
|
133
|
-
|
|
134
|
-
describe('4. client process', function () {
|
|
135
|
-
describe('4.1 verify_auth_res()', function () {
|
|
136
|
-
it('should return true when correct AUTH_RES ', function () {
|
|
137
|
-
assert.ok(isCorrectSeverHMAC)
|
|
138
|
-
})
|
|
139
|
-
|
|
140
|
-
it('should return false when AUTH_RES has wrong hmac', function () {
|
|
141
|
-
assert.ok(!wrongSeverHMACResult)
|
|
142
|
-
})
|
|
143
|
-
})
|
|
45
|
+
describe('AUTH process (Ascon)', function () {
|
|
46
|
+
it('client and server log in and exchange messages', function () {
|
|
47
|
+
const server = new Boho(); server.copy_key(Boho.deriveKey('secret'))
|
|
48
|
+
const client = new Boho(); client.set_id_key('dev1.secret')
|
|
49
|
+
const res = server.verify_auth_req(client.auth_req(server.server_time_nonce()))
|
|
50
|
+
expect(res).to.be.ok
|
|
51
|
+
expect(client.verify_auth_res(res)).to.equal(true)
|
|
52
|
+
const up = client.encrypt_session('hello')
|
|
53
|
+
expect(Buffer.from(server.decrypt_session(up)).toString()).to.equal('hello')
|
|
54
|
+
const down = server.encrypt_session('world')
|
|
55
|
+
expect(Buffer.from(client.decrypt_session(down)).toString()).to.equal('world')
|
|
144
56
|
})
|
|
145
57
|
})
|
|
146
58
|
|
|
147
|
-
|
|
148
|
-
</script>
|
|
59
|
+
</script>
|
|
149
60
|
|
|
150
61
|
<script class="mocha-exec">
|
|
151
62
|
mocha.run();
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// Makes boho-vectors.json: fixed-input test vectors for the Boho message formats that
|
|
2
|
+
// every implementation (JS, C++, Kotlin, Python) must reproduce byte for byte.
|
|
3
|
+
// node tools/make-vectors.mjs write test/fixtures/boho-vectors.json
|
|
4
|
+
// node tools/make-vectors.mjs --check fail if the file differs
|
|
5
|
+
import { readFileSync, writeFileSync } from 'node:fs'
|
|
6
|
+
import * as ref from '../test/reference.js'
|
|
7
|
+
|
|
8
|
+
const hex = b => Buffer.from(b).toString('hex')
|
|
9
|
+
const unhex = h => new Uint8Array(Buffer.from(h, 'hex'))
|
|
10
|
+
const seq = (n, start = 0) => Uint8Array.from({ length: n }, (_, i) => (start + i) & 255)
|
|
11
|
+
const text = s => new TextEncoder().encode(s)
|
|
12
|
+
const signal = s => Uint8Array.from([0xd0, ...text(s)]) // a SIGNAL-like plaintext
|
|
13
|
+
|
|
14
|
+
function challenge (sec, ms, counter, nonceHex) {
|
|
15
|
+
const c = new Uint8Array(13)
|
|
16
|
+
c[0] = ref.TYPE.SERVER_TIME_NONCE
|
|
17
|
+
new DataView(c.buffer).setUint32(1, sec, true)
|
|
18
|
+
new DataView(c.buffer).setUint16(5, ms, true)
|
|
19
|
+
new DataView(c.buffer).setUint16(7, counter, true)
|
|
20
|
+
c.set(unhex(nonceHex), 9)
|
|
21
|
+
return c
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function makeVectors () {
|
|
25
|
+
const keys = ['secret', 'interop-shared-key', '비밀키-한글', 'k'.repeat(100)]
|
|
26
|
+
.map(key => ({ key, derived: hex(ref.deriveKey(key)) }))
|
|
27
|
+
|
|
28
|
+
const logins = [
|
|
29
|
+
{ key: 'secret', id: 'dev1', challenge: challenge(1764000000, 123, 0x1234, 'a1b2c3d4'), clientNonce: '0001020304050607' },
|
|
30
|
+
{ key: 'interop-shared-key', id: '12345678', challenge: challenge(1764000001, 999, 0, '00000000'), clientNonce: 'f0e1d2c3b4a59687' },
|
|
31
|
+
{ key: '비밀키-한글', id: 'uno', challenge: challenge(0, 0, 0xffff, 'ffffffff'), clientNonce: 'ffffffffffffffff' }
|
|
32
|
+
].map(({ key, id, challenge: c, clientNonce }) => {
|
|
33
|
+
const k = ref.deriveKey(key)
|
|
34
|
+
const req = ref.authReq(k, c, ref.id8(id), unhex(clientNonce))
|
|
35
|
+
const res = ref.verifyAuthReq(k, c, req.packet)
|
|
36
|
+
return {
|
|
37
|
+
key, id, challenge: hex(c), clientNonce,
|
|
38
|
+
transcript: hex(req.session.transcript),
|
|
39
|
+
sessionKey: hex(req.session.sessionKey),
|
|
40
|
+
clientProof: hex(req.session.clientProof),
|
|
41
|
+
serverProof: hex(req.session.serverProof),
|
|
42
|
+
authReq: hex(req.packet),
|
|
43
|
+
authRes: hex(res.packet)
|
|
44
|
+
}
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
const ks = unhex(logins[0].sessionKey)
|
|
48
|
+
const clock = (sec, ms, counter) => ({ sec, ms, counter })
|
|
49
|
+
const messageInputs = [
|
|
50
|
+
['ENC', ref.DIR.CLIENT_TO_SERVER, clock(1764000000, 124, 0x1235), new Uint8Array(0)],
|
|
51
|
+
['ENC', ref.DIR.CLIENT_TO_SERVER, clock(1764000000, 125, 0x1236), signal('hello')],
|
|
52
|
+
['ENC', ref.DIR.SERVER_TO_CLIENT, clock(1764000000, 125, 0x1236), signal('hello')],
|
|
53
|
+
['ENC', ref.DIR.CLIENT_TO_SERVER, clock(1764000002, 0, 1), seq(15)],
|
|
54
|
+
['ENC', ref.DIR.CLIENT_TO_SERVER, clock(1764000002, 0, 2), seq(16)],
|
|
55
|
+
['ENC', ref.DIR.CLIENT_TO_SERVER, clock(1764000002, 0, 3), seq(17)],
|
|
56
|
+
['ENC', ref.DIR.SERVER_TO_CLIENT, clock(4294967295, 999, 65535), seq(59, 7)],
|
|
57
|
+
['ENC_E2E', ref.DIR.CLIENT_TO_SERVER, clock(1764000003, 500, 9), seq(10, 0xd2), unhex('b9' + 'aa'.repeat(31))]
|
|
58
|
+
]
|
|
59
|
+
const messages = messageInputs.map(([type, dir, c, plaintext, e2eBody]) => ({
|
|
60
|
+
sessionKey: hex(ks), type, dir, ...c, plaintext: hex(plaintext),
|
|
61
|
+
e2eBody: e2eBody ? hex(e2eBody) : '',
|
|
62
|
+
packet: hex(ref.seal(ref.TYPE[type], ks, dir, c, plaintext, e2eBody))
|
|
63
|
+
}))
|
|
64
|
+
|
|
65
|
+
const e2eKey = 'e2e-shared-key'
|
|
66
|
+
const packs = [
|
|
67
|
+
[clock(1764000000, 1, 1), '0102030405060708', new Uint8Array(0)],
|
|
68
|
+
[clock(1764000000, 1, 2), '0102030405060708', text('on=1;level=70')],
|
|
69
|
+
[clock(1764000000, 1, 2), '1112131415161718', text('on=1;level=70')],
|
|
70
|
+
[clock(1764000009, 999, 65535), 'fffefdfcfbfaf9f8', seq(40)]
|
|
71
|
+
].map(([c, sender, plaintext]) => ({
|
|
72
|
+
key: e2eKey, ...c, sender, plaintext: hex(plaintext),
|
|
73
|
+
packet: hex(ref.sealPack(ref.deriveKey(e2eKey), c, unhex(sender), plaintext))
|
|
74
|
+
}))
|
|
75
|
+
|
|
76
|
+
// Packets every implementation must reject.
|
|
77
|
+
const flip = (h, i, bit = 1) => { const b = unhex(h); b[i] ^= bit; return hex(b) }
|
|
78
|
+
const set = (h, i, v) => { const b = unhex(h); b[i] = v; return hex(b) }
|
|
79
|
+
const m = messages[1], p = packs[1], l = logins[0]
|
|
80
|
+
const invalid = [
|
|
81
|
+
{ kind: 'message', reason: 'tag bit flipped', sessionKey: m.sessionKey, dir: m.dir, packet: flip(m.packet, 13) },
|
|
82
|
+
{ kind: 'message', reason: 'ciphertext bit flipped', sessionKey: m.sessionKey, dir: m.dir, packet: flip(m.packet, 21) },
|
|
83
|
+
{ kind: 'message', reason: 'clock changed', sessionKey: m.sessionKey, dir: m.dir, packet: flip(m.packet, 11) },
|
|
84
|
+
{ kind: 'message', reason: 'ENC relabelled ENC_E2E', sessionKey: m.sessionKey, dir: m.dir, packet: set(m.packet, 0, ref.TYPE.ENC_E2E) },
|
|
85
|
+
{ kind: 'message', reason: 'other direction', sessionKey: m.sessionKey, dir: ref.DIR.SERVER_TO_CLIENT, packet: m.packet },
|
|
86
|
+
{ kind: 'message', reason: 'other session key', sessionKey: logins[1].sessionKey, dir: m.dir, packet: m.packet },
|
|
87
|
+
{ kind: 'message', reason: 'length one short', sessionKey: m.sessionKey, dir: m.dir, packet: set(m.packet, 1, unhex(m.packet)[1] - 1) },
|
|
88
|
+
{ kind: 'message', reason: 'trailing byte on ENC', sessionKey: m.sessionKey, dir: m.dir, packet: m.packet + '00' },
|
|
89
|
+
{ kind: 'message', reason: 'milliseconds 1000', sessionKey: m.sessionKey, dir: m.dir, packet: set(set(m.packet, 9, 0xe8), 10, 0x03) },
|
|
90
|
+
{ kind: 'message', reason: 'shorter than header', sessionKey: m.sessionKey, dir: m.dir, packet: m.packet.slice(0, 40) },
|
|
91
|
+
{ kind: 'message', reason: 'classic ENC_488 type', sessionKey: m.sessionKey, dir: m.dir, packet: set(m.packet, 0, 0xb8) },
|
|
92
|
+
{ kind: 'pack', reason: 'tag bit flipped', key: p.key, packet: flip(p.packet, 21) },
|
|
93
|
+
{ kind: 'pack', reason: 'sender changed', key: p.key, packet: flip(p.packet, 13) },
|
|
94
|
+
{ kind: 'pack', reason: 'ciphertext bit flipped', key: p.key, packet: flip(p.packet, 29, 0x80) },
|
|
95
|
+
{ kind: 'pack', reason: 'other key', key: 'secret', packet: p.packet },
|
|
96
|
+
{ kind: 'pack', reason: 'classic ENC_PACK type', key: p.key, packet: set(p.packet, 0, 0xb6) },
|
|
97
|
+
{ kind: 'authReq', reason: 'proof bit flipped', key: l.key, challenge: l.challenge, packet: flip(l.authReq, 32) },
|
|
98
|
+
{ kind: 'authReq', reason: 'other challenge', key: l.key, challenge: logins[1].challenge, packet: l.authReq },
|
|
99
|
+
{ kind: 'authReq', reason: 'other key', key: 'Secret', challenge: l.challenge, packet: l.authReq },
|
|
100
|
+
{ kind: 'authReq', reason: 'id changed', key: l.key, challenge: l.challenge, packet: flip(l.authReq, 1) },
|
|
101
|
+
{ kind: 'authRes', reason: 'proof bit flipped', sessionOf: 0, packet: flip(l.authRes, 16) },
|
|
102
|
+
{ kind: 'authRes', reason: 'client proof echoed', sessionOf: 0, packet: '' + hex([ref.TYPE.AUTH_RES]) + l.clientProof }
|
|
103
|
+
]
|
|
104
|
+
|
|
105
|
+
return {
|
|
106
|
+
format: 'boho-vectors',
|
|
107
|
+
version: 1,
|
|
108
|
+
design: 'docs/PROTOCOL.md',
|
|
109
|
+
notes: 'All byte strings are lowercase hex. Integers are little-endian on the wire. Ascon is NIST SP 800-232.',
|
|
110
|
+
constants: {
|
|
111
|
+
types: Object.fromEntries(Object.entries(ref.TYPE).map(([k, v]) => [k, v])),
|
|
112
|
+
dir: ref.DIR,
|
|
113
|
+
custom: ref.CUSTOM,
|
|
114
|
+
sizes: ref.SIZE
|
|
115
|
+
},
|
|
116
|
+
keys, logins, messages, packs, invalid
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const isMain = process.argv[1] && import.meta.url === new URL(`file://${process.argv[1]}`).href
|
|
121
|
+
if (isMain) {
|
|
122
|
+
const file = new URL('../test/fixtures/boho-vectors.json', import.meta.url)
|
|
123
|
+
const text = JSON.stringify(makeVectors(), null, 2) + '\n'
|
|
124
|
+
if (process.argv.includes('--check')) {
|
|
125
|
+
if (readFileSync(file, 'utf8') !== text) { console.error('boho-vectors.json is out of date'); process.exit(1) }
|
|
126
|
+
console.log('boho-vectors.json is up to date')
|
|
127
|
+
} else {
|
|
128
|
+
writeFileSync(file, text)
|
|
129
|
+
console.log('wrote', file.pathname)
|
|
130
|
+
}
|
|
131
|
+
}
|
package/test/AUTH_process.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import { describe, it } from 'node:test';
|
|
2
|
-
import assert from 'node:assert';
|
|
3
|
-
import Boho from 'boho'
|
|
4
|
-
|
|
5
|
-
const MBP = Boho.MBP
|
|
6
|
-
const MetaSize = Boho.MetaSize
|
|
7
|
-
const BohoMsg = Boho.BohoMsg
|
|
8
|
-
const Meta = Boho.Meta
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
describe('AUTH process ESM', function () {
|
|
12
|
-
const id = 'id'
|
|
13
|
-
const key = 'key'
|
|
14
|
-
|
|
15
|
-
// client side
|
|
16
|
-
const c = new Boho()
|
|
17
|
-
c.set_id8(id)
|
|
18
|
-
c.set_key(key)
|
|
19
|
-
|
|
20
|
-
// server side
|
|
21
|
-
const s = new Boho()
|
|
22
|
-
const auth_nonce_buffer = s.server_time_nonce()
|
|
23
|
-
const auth_req_buffer = c.auth_req(auth_nonce_buffer)
|
|
24
|
-
|
|
25
|
-
const unpack = MBP.unpack(auth_req_buffer, Meta.AUTH_REQ)
|
|
26
|
-
|
|
27
|
-
// Find hashID8 from DATABASE then set the id and key.
|
|
28
|
-
s.set_id8(id)
|
|
29
|
-
|
|
30
|
-
s.set_key('wrong-key') // if key is wrong.
|
|
31
|
-
const verify_auth_req_fail = s.verify_auth_req(unpack)
|
|
32
|
-
|
|
33
|
-
s.set_key(key) // if correct.
|
|
34
|
-
const auth_res_buffer = s.verify_auth_req(unpack)
|
|
35
|
-
|
|
36
|
-
const auth_res_buffer_with_incorrect_hmac = Boho.Buffer.alloc(MetaSize.AUTH_RES)
|
|
37
|
-
|
|
38
|
-
auth_res_buffer.copy(auth_res_buffer_with_incorrect_hmac)
|
|
39
|
-
auth_res_buffer_with_incorrect_hmac[2] ^= 0x55 // change hmac
|
|
40
|
-
const wrongSeverHMACResult = c.verify_auth_res(auth_res_buffer_with_incorrect_hmac)
|
|
41
|
-
const isCorrectSeverHMAC = c.verify_auth_res(auth_res_buffer)
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
describe('1. server send SERVER_TIME_NONCE', function () {
|
|
45
|
-
it('should return static size buffer', function () {
|
|
46
|
-
assert.equal(auth_nonce_buffer.byteLength, MetaSize.SERVER_TIME_NONCE)
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
it('should pack[0] is header type ', function () {
|
|
50
|
-
assert.equal(auth_nonce_buffer[0], BohoMsg.SERVER_TIME_NONCE)
|
|
51
|
-
})
|
|
52
|
-
})
|
|
53
|
-
|
|
54
|
-
describe('2. client send AUTH_REQ', function () {
|
|
55
|
-
it('should return static size buffer', function () {
|
|
56
|
-
assert.equal(auth_req_buffer.byteLength, MetaSize.AUTH_REQ)
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
it('should pack[0] is header type ', function () {
|
|
60
|
-
assert.equal(auth_req_buffer[0], BohoMsg.AUTH_REQ)
|
|
61
|
-
})
|
|
62
|
-
})
|
|
63
|
-
|
|
64
|
-
describe('3. server verify AUTH_REQ pack', function () {
|
|
65
|
-
it('3.1. should success unpack', function () {
|
|
66
|
-
assert.ok(typeof unpack === 'object')
|
|
67
|
-
})
|
|
68
|
-
|
|
69
|
-
describe('3.2. verify_auth_req(obj) ', function () {
|
|
70
|
-
// wrong key of id.
|
|
71
|
-
it('3.2.1. should return false when wrong key', function () {
|
|
72
|
-
assert.ok(!verify_auth_req_fail)
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
// correct id and key
|
|
76
|
-
it('3-2.2. should return AUTH_RES buffer pack when correct key', function () {
|
|
77
|
-
assert.equal(auth_res_buffer[0], BohoMsg.AUTH_RES)
|
|
78
|
-
})
|
|
79
|
-
})
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
describe('4. client process', function () {
|
|
83
|
-
describe('4.1 verify_auth_res()', function () {
|
|
84
|
-
it('should return true when correct AUTH_RES ', function () {
|
|
85
|
-
assert.ok(isCorrectSeverHMAC)
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
it('should return false when AUTH_RES has wrong hmac', function () {
|
|
89
|
-
assert.ok(!wrongSeverHMACResult)
|
|
90
|
-
})
|
|
91
|
-
})
|
|
92
|
-
})
|
|
93
|
-
})
|
package/test/ENC_PACK.js
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { describe, it } from 'node:test';
|
|
2
|
-
import assert from 'node:assert';
|
|
3
|
-
import Boho from 'boho'
|
|
4
|
-
|
|
5
|
-
const MBP = Boho.MBP
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
describe('ENC_PACK ESM', function () {
|
|
9
|
-
describe('encryption and decryption', function () {
|
|
10
|
-
const plainData = 'aaaaa'
|
|
11
|
-
const key = 'key'
|
|
12
|
-
|
|
13
|
-
it('should decryptPack.data property has same buffer of origin.', function () {
|
|
14
|
-
const boho = new Boho()
|
|
15
|
-
boho.set_key(key)
|
|
16
|
-
|
|
17
|
-
const encData = boho.encryptPack(plainData)
|
|
18
|
-
const decObj = boho.decryptPack(encData)
|
|
19
|
-
|
|
20
|
-
const srcBuffer = MBP.B8(plainData)
|
|
21
|
-
const resultBuffer = MBP.B8(decObj.data)
|
|
22
|
-
|
|
23
|
-
assert.ok(MBP.equal(srcBuffer, resultBuffer))
|
|
24
|
-
})
|
|
25
|
-
})
|
|
26
|
-
})
|
package/test/RAND.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { describe, it } from 'node:test';
|
|
2
|
-
import assert from 'node:assert';
|
|
3
|
-
import Boho from 'boho'
|
|
4
|
-
|
|
5
|
-
describe('RAND ESM', function () {
|
|
6
|
-
describe('for number', function () {
|
|
7
|
-
const num = 16
|
|
8
|
-
it('should return Uint8Array', function () {
|
|
9
|
-
assert.ok(Boho.RAND(num) instanceof Uint8Array)
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
it('should return number-bytes size of Uint8Array', function () {
|
|
13
|
-
assert.ok(Boho.RAND(num).byteLength === num)
|
|
14
|
-
})
|
|
15
|
-
})
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
console.log( 'buffer: hello', Boho.Buffer.from('hello'))
|
package/test/arduino/host.cpp
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
#include <iostream>
|
|
2
|
-
#include <sstream>
|
|
3
|
-
#include <vector>
|
|
4
|
-
#include <string>
|
|
5
|
-
#include "Boho.h"
|
|
6
|
-
uint32_t clockMs = 1234, clockUs = 1234567;
|
|
7
|
-
uint32_t millis() { return clockMs; }
|
|
8
|
-
uint32_t micros() { return clockUs; }
|
|
9
|
-
TestSerial Serial;
|
|
10
|
-
std::vector<uint8_t> fromHex(const std::string& text) {
|
|
11
|
-
std::vector<uint8_t> out;
|
|
12
|
-
for (size_t i = 0; i < text.size(); i += 2) out.push_back(std::stoul(text.substr(i, 2), nullptr, 16));
|
|
13
|
-
return out;
|
|
14
|
-
}
|
|
15
|
-
void printHex(const std::vector<uint8_t>& data, size_t size) {
|
|
16
|
-
const char* hex = "0123456789abcdef";
|
|
17
|
-
for (size_t i = 0; i < size; i++) std::cout << hex[data[i] >> 4] << hex[data[i] & 15];
|
|
18
|
-
std::cout << std::endl;
|
|
19
|
-
}
|
|
20
|
-
int main() {
|
|
21
|
-
const uint16_t endian = 1;
|
|
22
|
-
if (*reinterpret_cast<const uint8_t*>(&endian) != 1) return 2;
|
|
23
|
-
Boho boho;
|
|
24
|
-
boho.set_id8("test-id"); boho.set_key("interop-shared-key");
|
|
25
|
-
std::string line;
|
|
26
|
-
while (std::getline(std::cin, line)) {
|
|
27
|
-
std::istringstream input(line);
|
|
28
|
-
std::string command, arg; input >> command >> arg;
|
|
29
|
-
if (command == "CLOCK") {
|
|
30
|
-
clockMs = std::stoul(arg); input >> clockUs; std::cout << "ok" << std::endl; continue;
|
|
31
|
-
}
|
|
32
|
-
if (command == "TIME") {
|
|
33
|
-
unsigned ms = 0; input >> ms; boho.setTime(std::stoul(arg), ms); std::cout << "ok" << std::endl; continue;
|
|
34
|
-
}
|
|
35
|
-
auto data = fromHex(arg);
|
|
36
|
-
std::vector<uint8_t> out(data.size() + 128);
|
|
37
|
-
size_t length = 0;
|
|
38
|
-
if (command == "AUTH") length = boho.auth_req(out.data(), data.data(), data.size());
|
|
39
|
-
else if (command == "VERIFY") {
|
|
40
|
-
std::cout << (boho.verify_auth_res(data.data(), data.size()) ? "1" : "0") << std::endl; continue;
|
|
41
|
-
}
|
|
42
|
-
else if (command == "PACK") length = boho.encryptPack(out.data(), data.data(), data.size());
|
|
43
|
-
else if (command == "UNPACK") length = boho.decryptPack(out.data(), data.data(), data.size());
|
|
44
|
-
else if (command == "E2E") length = boho.encrypt_e2e(out.data(), data.data(), data.size(), "interop-e2e-key");
|
|
45
|
-
else if (command == "DE2E") length = boho.decrypt_e2e(out.data(), data.data(), data.size(), "interop-e2e-key");
|
|
46
|
-
else if (command == "SEND") length = boho.encrypt_488(out.data(), data.data(), data.size());
|
|
47
|
-
else if (command == "RECV") length = boho.decrypt_488(out.data(), data.data(), data.size());
|
|
48
|
-
else return 3;
|
|
49
|
-
printHex(out, length);
|
|
50
|
-
}
|
|
51
|
-
}
|
|
@@ -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
|