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,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
+ })
@@ -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
- describe('for number', function () {
31
- const num = 16
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
- describe('AUTH process', function () {
64
- const id = 'id'
65
- const key = 'key'
66
-
67
- // client side
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
- describe('2. client send AUTH_REQ', function () {
107
- it('should return static size buffer', function () {
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
- describe('3. server verify AUTH_REQ pack', function () {
117
- it('3.1. should success unpack', function () {
118
- assert.ok(typeof unpack === 'object')
119
- })
120
-
121
- describe('3.2. verify_auth_req(obj) ', function () {
122
- // wrong key of id.
123
- it('3.2.1. should return false when wrong key', function () {
124
- assert.ok(!verify_auth_req_fail)
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
+ }
@@ -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'))
@@ -1,99 +0,0 @@
1
- // The key and authentication buffers are made on first use, as views into one block.
2
- import { describe, it } from 'node:test'
3
- import assert from 'node:assert/strict'
4
- import Boho from '../src/index.js'
5
-
6
- const FIELDS = ['_id8', '_otpSrc44', '_otp36', '_hmac', 'auth_salt12', 'localNonce', 'remoteNonce']
7
- const SIZES = [8, 44, 36, 32, 12, 4, 4]
8
-
9
- function authorizedPair() {
10
- const client = new Boho(), server = new Boho()
11
- client.set_id8('lazy-id'); client.set_key('lazy-key')
12
- const request = client.auth_req(server.server_time_nonce())
13
- server.set_key('lazy-key')
14
- assert.equal(client.verify_auth_res(server.verify_auth_req(request)), true)
15
- return { client, server }
16
- }
17
-
18
- describe('Boho key and authentication state', () => {
19
- it('holds no buffers until first used', () => {
20
- const boho = new Boho()
21
- assert.equal(boho._block, null); assert.equal(boho._state, null)
22
- boho.clearAuth()
23
- assert.equal(boho._block, null, 'clearAuth on an unused instance allocates nothing')
24
- assert.equal(boho.isAuthorized, false)
25
- })
26
-
27
- it('gives zeroed, separate views of the documented sizes', () => {
28
- const boho = new Boho()
29
- FIELDS.forEach((name, i) => {
30
- assert.equal(boho[name].length, SIZES[i], name)
31
- assert.ok(boho[name].every(byte => byte === 0), `${name} starts zeroed`)
32
- })
33
- FIELDS.forEach(name => boho[name].fill(0xff))
34
- FIELDS.forEach(name => assert.ok(boho[name].every(byte => byte === 0xff), `${name} not overwritten by a neighbour`))
35
- boho.clearAuth()
36
- FIELDS.forEach(name => assert.ok(boho[name].every(byte => byte === 0), `${name} cleared`))
37
- })
38
-
39
- it('keeps a replaced buffer and clears it', () => {
40
- const boho = new Boho()
41
- const nonce = Boho.Buffer.from([1, 2, 3, 4])
42
- boho.localNonce = nonce
43
- assert.equal(boho.localNonce, nonce)
44
- boho.clearAuth()
45
- assert.deepEqual([...nonce], [0, 0, 0, 0])
46
- })
47
-
48
- it('a login challenge alone writes the block without field views', () => {
49
- const server = new Boho(), before = Math.floor(Date.now() / 1000)
50
- const pack = server.server_time_nonce()
51
- assert.equal(server._state, null, 'no field views')
52
- assert.equal(server._block.length, 140)
53
- assert.equal(pack.length, 13)
54
- const salt = pack.subarray(1)
55
- assert.ok(Math.abs(salt.readUInt32LE(0) - before) <= 1, 'unix time')
56
- assert.ok(salt.readUInt16LE(4) < 1000, 'milliseconds')
57
- // Reading the fields later sees the same challenge.
58
- assert.deepEqual([...server.auth_salt12], [...salt])
59
- assert.deepEqual([...server.localNonce], [...salt.subarray(8, 12)])
60
- // Two challenges differ in their random bytes.
61
- const other = new Boho().server_time_nonce()
62
- assert.notDeepEqual([...other.subarray(7)], [...salt.subarray(6)])
63
- })
64
-
65
- it('a new challenge after a login replaces the fields', () => {
66
- const { server } = authorizedPair()
67
- const pack = server.server_time_nonce()
68
- assert.deepEqual([...server.auth_salt12], [...pack.subarray(1)])
69
- assert.deepEqual([...server.localNonce], [...pack.subarray(9, 13)])
70
- const client = new Boho()
71
- client.set_id8('lazy-id'); client.set_key('lazy-key')
72
- assert.equal(client.verify_auth_res(server.verify_auth_req(client.auth_req(pack))), true)
73
- })
74
-
75
- it('instances do not share state', () => {
76
- const a = new Boho(), b = new Boho()
77
- a._id8.fill(7)
78
- assert.ok(b._id8.every(byte => byte === 0))
79
- })
80
-
81
- it('authenticates and encrypts both ways, also after clearAuth and a new login', () => {
82
- for (let round = 0; round < 2; round++) {
83
- const { client, server } = authorizedPair()
84
- const fromClient = server.decrypt_488(client.encrypt_488(Boho.Buffer.from('hello server')))
85
- assert.equal(Boho.Buffer.from(fromClient).toString(), 'hello server')
86
- const fromServer = client.decrypt_488(server.encrypt_488(Boho.Buffer.from('hello client')))
87
- assert.equal(Boho.Buffer.from(fromServer).toString(), 'hello client')
88
- client.clearAuth(); server.clearAuth()
89
- assert.equal(client.isAuthorized, false)
90
- }
91
- })
92
-
93
- it('E2E on an instance that never authenticated', () => {
94
- const sender = new Boho(), receiver = new Boho()
95
- const pack = sender.encrypt_e2e(Boho.Buffer.from('payload'), 'e2e-key')
96
- assert.equal(Boho.Buffer.from(receiver.decrypt_e2e(pack, 'e2e-key').data).toString(), 'payload')
97
- assert.equal(receiver.decrypt_e2e(pack, 'wrong-key'), undefined)
98
- })
99
- })
@@ -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
- }