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.
- package/README.ko.md +92 -277
- package/README.md +95 -311
- 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 +265 -497
- 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 +3 -5
- 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/STATE_LAZY.js +0 -99
- 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
package/src/boho.js
CHANGED
|
@@ -1,582 +1,350 @@
|
|
|
1
|
-
// Copyright (c) 2024 Taeo Lee (sixgen@gmail.com)
|
|
1
|
+
// Copyright (c) 2024-2026 Taeo Lee (sixgen@gmail.com)
|
|
2
2
|
// MIT License
|
|
3
3
|
//
|
|
4
4
|
// https://github.com/remocons/boho
|
|
5
5
|
//
|
|
6
|
+
// Boho 3: shared-key authentication and encrypted messages on Ascon (NIST SP 800-232).
|
|
6
7
|
//
|
|
8
|
+
// login SERVER_TIME_NONCE (13 B) → AUTH_REQ (33 B) → AUTH_RES (17 B)
|
|
9
|
+
// session ENC / ENC_E2E: 21-byte header, Ascon-AEAD128 with an 8-byte tag
|
|
10
|
+
// pack/E2E ENC_PACK: 29-byte header (16-byte nonce with an 8-byte sender value)
|
|
7
11
|
//
|
|
8
|
-
|
|
12
|
+
// Keys: Ascon-CXOF128(Z = "boho/v2/key") of the key string, 16 bytes (deriveKey). A server
|
|
13
|
+
// stores this derived key, never the key string. Each login makes a new session key.
|
|
14
|
+
// Boho 2.x (SHA-256 based, classic/) is not supported: its message codes are not reused.
|
|
9
15
|
import MBP from 'meta-buffer-pack'
|
|
10
|
-
export { MBP }
|
|
11
|
-
import { BohoMsg, Meta, MetaSize } from './constants.js'
|
|
12
|
-
export { BohoMsg, Meta, MetaSize, sha256 }
|
|
13
16
|
import { Buffer } from 'buffer/index.js'
|
|
14
|
-
|
|
17
|
+
import { encrypt, decrypt, cxof128 } from './ascon.js'
|
|
18
|
+
import { BohoMsg, MetaSize } from './constants.js'
|
|
15
19
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
export { MBP, Buffer, BohoMsg, MetaSize }
|
|
21
|
+
|
|
22
|
+
const KEY_CUSTOM = 'boho/v2/key'
|
|
23
|
+
const SESSION_CUSTOM = 'boho/v2/session'
|
|
24
|
+
const TAG = 8
|
|
25
|
+
const KEY_SIZE = 16
|
|
26
|
+
const DIR = { client: 1, server: 2 } // the direction byte of what that side sends
|
|
27
|
+
const encoder = new TextEncoder()
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Random bytes from the platform's cryptographic generator.
|
|
31
|
+
* @param {number} size
|
|
32
|
+
* @returns {Buffer}
|
|
33
|
+
*/
|
|
34
|
+
export function RAND (size) {
|
|
35
|
+
const out = Buffer.alloc(size)
|
|
36
|
+
globalThis.crypto.getRandomValues(out)
|
|
37
|
+
return out
|
|
23
38
|
}
|
|
24
39
|
|
|
25
|
-
function
|
|
26
|
-
if (
|
|
27
|
-
|
|
28
|
-
if (buffer.length < size || buffer[0] !== type) return
|
|
29
|
-
if (variable ? buffer.readUInt32LE(1) !== buffer.length - size : buffer.length !== size) return
|
|
30
|
-
return MBP.unpack(buffer, meta)
|
|
40
|
+
function bytes (data) {
|
|
41
|
+
if (typeof data === 'string') return encoder.encode(data)
|
|
42
|
+
return MBP.B8(data)
|
|
31
43
|
}
|
|
32
44
|
|
|
33
|
-
function
|
|
34
|
-
|
|
45
|
+
function equal (a, b) {
|
|
46
|
+
if (!(a instanceof Uint8Array) || !(b instanceof Uint8Array) || a.length !== b.length) return false
|
|
47
|
+
let difference = 0
|
|
48
|
+
for (let i = 0; i < a.length; i++) difference |= a[i] ^ b[i]
|
|
49
|
+
return difference === 0
|
|
35
50
|
}
|
|
36
51
|
|
|
52
|
+
function wipe (b) { if (b) b.fill(0) }
|
|
37
53
|
|
|
38
54
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
55
|
+
* Key string (the part after "id." in id.key, or an E2E key) → the 16-byte key.
|
|
56
|
+
* Servers store it (for example base64) as the key of an ID.
|
|
57
|
+
* @param {string|Uint8Array} key
|
|
41
58
|
* @returns {Buffer}
|
|
42
59
|
*/
|
|
43
|
-
export function
|
|
44
|
-
|
|
60
|
+
export function deriveKey (key) {
|
|
61
|
+
const k = bytes(key)
|
|
62
|
+
if (!k.length) throw new RangeError('key must not be empty')
|
|
63
|
+
return Buffer.from(cxof128(k, KEY_SIZE, KEY_CUSTOM))
|
|
45
64
|
}
|
|
46
65
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
const STATE_BYTES = STATE_FIELDS.reduce((offset, [name, size]) => { OFFSET[name] = offset; return offset + size }, 0)
|
|
56
|
-
|
|
57
|
-
function createViews(block) {
|
|
58
|
-
const state = {}
|
|
59
|
-
for (const [name, size] of STATE_FIELDS) state[name] = block.subarray(OFFSET[name], OFFSET[name] + size)
|
|
60
|
-
return state
|
|
66
|
+
function readHeader (packet) {
|
|
67
|
+
const view = new DataView(packet.buffer, packet.byteOffset, packet.byteLength)
|
|
68
|
+
return {
|
|
69
|
+
length: view.getUint32(1, true),
|
|
70
|
+
sec: view.getUint32(5, true),
|
|
71
|
+
ms: view.getUint16(9, true),
|
|
72
|
+
counter: view.getUint16(11, true)
|
|
73
|
+
}
|
|
61
74
|
}
|
|
62
75
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
*/
|
|
70
|
-
constructor() {
|
|
71
|
-
this._block = null // see STATE_FIELDS
|
|
72
|
-
this._state = null // field views into _block, read and replaced through the accessors below
|
|
73
|
-
this.isAuthorized = false
|
|
74
|
-
this.counter = 0
|
|
75
|
-
this._hasKey = false
|
|
76
|
-
this._lastSendTime = 0
|
|
77
|
-
}
|
|
76
|
+
function sessionNonce (header, dir) {
|
|
77
|
+
const nonce = new Uint8Array(16)
|
|
78
|
+
nonce.set(header.subarray(5, 13))
|
|
79
|
+
nonce[8] = dir
|
|
80
|
+
return nonce
|
|
81
|
+
}
|
|
78
82
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
get _otpSrc44() { return this._views()._otpSrc44 }
|
|
84
|
-
set _otpSrc44(value) { this._views()._otpSrc44 = value }
|
|
85
|
-
get _otp36() { return this._views()._otp36 }
|
|
86
|
-
set _otp36(value) { this._views()._otp36 = value }
|
|
87
|
-
get _hmac() { return this._views()._hmac }
|
|
88
|
-
set _hmac(value) { this._views()._hmac = value }
|
|
89
|
-
get auth_salt12() { return this._views().auth_salt12 }
|
|
90
|
-
set auth_salt12(value) { this._views().auth_salt12 = value }
|
|
91
|
-
get localNonce() { return this._views().localNonce }
|
|
92
|
-
set localNonce(value) { this._views().localNonce = value }
|
|
93
|
-
get remoteNonce() { return this._views().remoteNonce }
|
|
94
|
-
set remoteNonce(value) { this._views().remoteNonce = value }
|
|
83
|
+
function validChallenge (c) {
|
|
84
|
+
return c instanceof Uint8Array && c.length === MetaSize.SERVER_TIME_NONCE &&
|
|
85
|
+
c[0] === BohoMsg.SERVER_TIME_NONCE && (c[5] | (c[6] << 8)) < 1000
|
|
86
|
+
}
|
|
95
87
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
clearAuth() {
|
|
100
|
-
// Nothing to clear before the first use. Replaced fields are not in the block: zero them too.
|
|
101
|
-
this._block?.fill(0)
|
|
102
|
-
if (this._state) for (const [name] of STATE_FIELDS) this._state[name].fill(0)
|
|
88
|
+
export class Boho {
|
|
89
|
+
constructor () {
|
|
90
|
+
/** True after a verified login (AUTH_RES on a client, AUTH_REQ on a server). */
|
|
103
91
|
this.isAuthorized = false
|
|
104
|
-
this.
|
|
105
|
-
this.
|
|
106
|
-
this.
|
|
92
|
+
this._id8 = new Uint8Array(8)
|
|
93
|
+
this._key = null
|
|
94
|
+
this._sessionKey = null
|
|
95
|
+
this._serverProof = null
|
|
96
|
+
this._challenge = null
|
|
97
|
+
this._role = null // 'client' or 'server': which direction byte this side sends
|
|
98
|
+
this._lastMs = -1
|
|
99
|
+
this._lastCounter = 0
|
|
100
|
+
this._sender = null
|
|
107
101
|
}
|
|
108
102
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
set_hash_id8(data) {
|
|
114
|
-
let idSum = MBP.B8(sha256.hash(data))
|
|
115
|
-
idSum.copy(this._id8, 0, 0, 8)
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* Sets the id8 value.
|
|
120
|
-
* @param {any} data
|
|
121
|
-
*/
|
|
122
|
-
set_id8(data) {
|
|
123
|
-
let encStr = MBP.B8(data)
|
|
124
|
-
this._id8.fill(0)
|
|
125
|
-
encStr.copy(this._id8, 0, 0, 8)
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Sets the key value by hashing and storing in otpSrc44.
|
|
130
|
-
* @param {any} data
|
|
131
|
-
*/
|
|
132
|
-
set_key(data) {
|
|
133
|
-
const bytes = MBP.B8(data)
|
|
134
|
-
if (!bytes || bytes.length === 0) throw new TypeError('Key must not be empty')
|
|
135
|
-
this.copy_key(MBP.B8(sha256.hash(bytes)))
|
|
103
|
+
clearAuth () {
|
|
104
|
+
wipe(this._id8); wipe(this._key); wipe(this._sessionKey); wipe(this._serverProof)
|
|
105
|
+
this._key = this._sessionKey = this._serverProof = this._role = null
|
|
106
|
+
this.isAuthorized = false
|
|
136
107
|
}
|
|
137
108
|
|
|
138
|
-
/**
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
set_id_key(id_key) {
|
|
143
|
-
let delimiterPosition = id_key.indexOf('.')
|
|
144
|
-
if (delimiterPosition == -1) return
|
|
145
|
-
let id = id_key.substring(0, delimiterPosition)
|
|
146
|
-
let key = id_key.substring(delimiterPosition + 1)
|
|
147
|
-
this.set_id8(id)
|
|
148
|
-
this.set_key(key)
|
|
109
|
+
/** The login ID: at most 8 bytes are used. */
|
|
110
|
+
set_id8 (id) {
|
|
111
|
+
this._id8 = new Uint8Array(8)
|
|
112
|
+
this._id8.set(bytes(id).subarray(0, 8))
|
|
149
113
|
}
|
|
150
114
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
* @param {Buffer} data
|
|
154
|
-
*/
|
|
155
|
-
copy_id8(data) {
|
|
156
|
-
data.copy(this._id8, 0, 0, 8)
|
|
115
|
+
copy_id8 (id8) {
|
|
116
|
+
this._id8 = Uint8Array.from(id8.subarray(0, 8))
|
|
157
117
|
}
|
|
158
118
|
|
|
159
|
-
/**
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if (!(data instanceof Uint8Array) || data.length !== 32) {
|
|
165
|
-
throw new TypeError('copy_key requires exactly 32 bytes')
|
|
166
|
-
}
|
|
167
|
-
this._otpSrc44.set(data, 0)
|
|
168
|
-
this._hasKey = true
|
|
169
|
-
|
|
119
|
+
/** Key string → key. A new key ends the session. */
|
|
120
|
+
set_key (key) {
|
|
121
|
+
this.isAuthorized = false
|
|
122
|
+
wipe(this._sessionKey); this._sessionKey = null
|
|
123
|
+
this._key = key ? deriveKey(key) : null
|
|
170
124
|
}
|
|
171
125
|
|
|
172
|
-
/**
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
sha256_n(srcData, n) {
|
|
179
|
-
let hashSum = sha256.hash(srcData)
|
|
180
|
-
for (let i = 0; i < n; i++) hashSum = sha256.hash(hashSum)
|
|
181
|
-
return hashSum
|
|
126
|
+
/** A stored 16-byte key (deriveKey), on a server. */
|
|
127
|
+
copy_key (key16) {
|
|
128
|
+
if (!(key16 instanceof Uint8Array) || key16.length !== KEY_SIZE) throw new RangeError('key must be 16 bytes')
|
|
129
|
+
this.isAuthorized = false
|
|
130
|
+
wipe(this._sessionKey); this._sessionKey = null
|
|
131
|
+
this._key = Uint8Array.from(key16)
|
|
182
132
|
}
|
|
183
133
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
this.
|
|
134
|
+
/** 'id.key' */
|
|
135
|
+
set_id_key (idKey) {
|
|
136
|
+
const dot = typeof idKey === 'string' ? idKey.indexOf('.') : -1
|
|
137
|
+
if (dot < 1 || dot === idKey.length - 1) { this.clearAuth(); return }
|
|
138
|
+
this.set_id8(idKey.slice(0, dot))
|
|
139
|
+
this.set_key(idKey.slice(dot + 1))
|
|
190
140
|
}
|
|
191
141
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
throw new RangeError('Clock is outside the protocol range')
|
|
202
|
-
}
|
|
203
|
-
this._lastSendTime = now
|
|
204
|
-
const salt12 = Buffer.concat([
|
|
205
|
-
MBP.NB('32L', Math.floor(now / 1000)),
|
|
206
|
-
MBP.NB('16L', now % 1000),
|
|
207
|
-
MBP.NB('16L', this.counter),
|
|
208
|
-
Buffer.from(nonce)
|
|
209
|
-
])
|
|
210
|
-
salt12.copy(this._otpSrc44, 32)
|
|
142
|
+
_session (challenge, id8, clientNonce) {
|
|
143
|
+
const input = new Uint8Array(KEY_SIZE + 13 + 8 + 8)
|
|
144
|
+
input.set(this._key, 0)
|
|
145
|
+
input.set(challenge, 16)
|
|
146
|
+
input.set(id8, 29)
|
|
147
|
+
input.set(clientNonce, 37)
|
|
148
|
+
const out = cxof128(input, 48, SESSION_CUSTOM)
|
|
149
|
+
wipe(input)
|
|
150
|
+
return out
|
|
211
151
|
}
|
|
212
152
|
|
|
213
|
-
|
|
214
|
-
if (!this._hasKey) throw new Error('Set a key before encryption or authentication')
|
|
215
|
-
}
|
|
153
|
+
// ---- server ----
|
|
216
154
|
|
|
217
155
|
/**
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
|
|
221
|
-
set_salt12(salt12 , caller) {
|
|
222
|
-
// console.log('boho.set_salt12', salt12 , caller )
|
|
223
|
-
if( salt12.byteLength != 12){
|
|
224
|
-
throw TypeError('set_salt12: Invalid salt12 byteLength.')
|
|
225
|
-
}
|
|
226
|
-
salt12.copy(this._otpSrc44, 32)
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Initializes OTP value.
|
|
156
|
+
* The server's challenge: type | unixTime4 | ms2 | random2 | random4 (13 bytes).
|
|
157
|
+
* Kept for verify_auth_req.
|
|
158
|
+
* @returns {Buffer}
|
|
231
159
|
*/
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
160
|
+
server_time_nonce () {
|
|
161
|
+
const now = Date.now()
|
|
162
|
+
const packet = Buffer.alloc(MetaSize.SERVER_TIME_NONCE)
|
|
163
|
+
packet[0] = BohoMsg.SERVER_TIME_NONCE
|
|
164
|
+
packet.writeUInt32LE(Math.floor(now / 1000), 1)
|
|
165
|
+
packet.writeUInt16LE(now % 1000, 5)
|
|
166
|
+
globalThis.crypto.getRandomValues(packet.subarray(7, 13))
|
|
167
|
+
this._challenge = Buffer.from(packet)
|
|
168
|
+
return packet
|
|
236
169
|
}
|
|
237
170
|
|
|
238
171
|
/**
|
|
239
|
-
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
172
|
+
* Checks a client's AUTH_REQ against the challenge this side sent (server_time_nonce, or the
|
|
173
|
+
* one given), with the ID's stored key (copy_key). On success the session is ready and
|
|
174
|
+
* AUTH_RES is returned.
|
|
175
|
+
* @returns {Buffer|false}
|
|
242
176
|
*/
|
|
243
|
-
|
|
244
|
-
this.
|
|
245
|
-
|
|
177
|
+
verify_auth_req (packet, challenge = this._challenge) {
|
|
178
|
+
if (!this._key || !validChallenge(challenge) || !(packet instanceof Uint8Array) ||
|
|
179
|
+
packet.length !== MetaSize.AUTH_REQ || packet[0] !== BohoMsg.AUTH_REQ) return false
|
|
180
|
+
const id8 = packet.subarray(1, 9)
|
|
181
|
+
const out = this._session(challenge, id8, packet.subarray(9, 17))
|
|
182
|
+
if (!equal(packet.subarray(17, 33), out.subarray(16, 32))) { wipe(out); return false }
|
|
183
|
+
this._id8 = Uint8Array.from(id8)
|
|
184
|
+
this._sessionKey = out.slice(0, 16)
|
|
185
|
+
this._role = 'server'
|
|
186
|
+
this.isAuthorized = true
|
|
187
|
+
const res = Buffer.alloc(MetaSize.AUTH_RES)
|
|
188
|
+
res[0] = BohoMsg.AUTH_RES
|
|
189
|
+
res.set(out.subarray(32, 48), 1)
|
|
190
|
+
wipe(out)
|
|
191
|
+
return res
|
|
246
192
|
}
|
|
247
193
|
|
|
248
|
-
|
|
249
|
-
* Generates the legacy SHA-256 prefix tag (not standard HMAC).
|
|
250
|
-
* @param {Buffer} data
|
|
251
|
-
*/
|
|
252
|
-
generateHMAC(data) {
|
|
253
|
-
this._requireKey()
|
|
254
|
-
let hmacSrc = Buffer.concat([this._otpSrc44, data])
|
|
255
|
-
this._hmac = MBP.B8(sha256.hash(hmacSrc))
|
|
256
|
-
}
|
|
194
|
+
// ---- client ----
|
|
257
195
|
|
|
258
196
|
/**
|
|
259
|
-
*
|
|
260
|
-
* @
|
|
261
|
-
* @returns {Buffer}
|
|
197
|
+
* AUTH_REQ for the server's SERVER_TIME_NONCE.
|
|
198
|
+
* @returns {Buffer|false}
|
|
262
199
|
*/
|
|
263
|
-
|
|
264
|
-
this.
|
|
265
|
-
|
|
266
|
-
this.
|
|
267
|
-
|
|
200
|
+
auth_req (serverTimeNonce, clientNonce = RAND(8)) {
|
|
201
|
+
if (!this._key || !validChallenge(serverTimeNonce)) return false
|
|
202
|
+
const out = this._session(serverTimeNonce, this._id8, clientNonce)
|
|
203
|
+
this._sessionKey = out.slice(0, 16)
|
|
204
|
+
this._serverProof = out.slice(32, 48)
|
|
205
|
+
this._role = 'client'
|
|
206
|
+
this.isAuthorized = false
|
|
207
|
+
const packet = Buffer.alloc(MetaSize.AUTH_REQ)
|
|
208
|
+
packet[0] = BohoMsg.AUTH_REQ
|
|
209
|
+
packet.set(this._id8, 1)
|
|
210
|
+
packet.set(clientNonce, 9)
|
|
211
|
+
packet.set(out.subarray(16, 32), 17)
|
|
212
|
+
wipe(out)
|
|
213
|
+
return packet
|
|
268
214
|
}
|
|
269
215
|
|
|
270
|
-
/**
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
xotp(data, otpStartIndex = 0, shareDataBuffer = false) {
|
|
278
|
-
|
|
279
|
-
data = MBP.B8(data, shareDataBuffer)
|
|
280
|
-
|
|
281
|
-
let len = data.byteLength
|
|
282
|
-
let otpIndex = otpStartIndex
|
|
283
|
-
let dataOffset = 0
|
|
284
|
-
let xorCalcLen = 0
|
|
285
|
-
|
|
286
|
-
while (len > 0) {
|
|
287
|
-
xorCalcLen = len < 32 ? len : 32
|
|
288
|
-
let iotp = this.getIndexOTP(++otpIndex);
|
|
289
|
-
for (let i = 0; i < xorCalcLen; i++) {
|
|
290
|
-
data[dataOffset++] ^= iotp[i]
|
|
291
|
-
}
|
|
292
|
-
len -= 32
|
|
293
|
-
}
|
|
294
|
-
return data
|
|
216
|
+
/** Checks the server's AUTH_RES. */
|
|
217
|
+
verify_auth_res (packet) {
|
|
218
|
+
if (!this._serverProof || !(packet instanceof Uint8Array) || packet.length !== MetaSize.AUTH_RES ||
|
|
219
|
+
packet[0] !== BohoMsg.AUTH_RES || !equal(packet.subarray(1), this._serverProof)) return
|
|
220
|
+
wipe(this._serverProof); this._serverProof = null
|
|
221
|
+
this.isAuthorized = true
|
|
222
|
+
return true
|
|
295
223
|
}
|
|
296
224
|
|
|
297
|
-
//
|
|
225
|
+
// ---- messages ----
|
|
298
226
|
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
const unixTime = Math.floor(now / 1000)
|
|
306
|
-
const milliseconds = now % 1000;
|
|
307
|
-
|
|
308
|
-
// salt12: unix time (32L), milliseconds (16L), 2 random bytes (the client's counter IV), nonce (4).
|
|
309
|
-
// localNonce holds its own copy of the nonce.
|
|
310
|
-
if (this._state) {
|
|
311
|
-
// Field views exist (an earlier login on this instance): replace them as fields.
|
|
312
|
-
this.localNonce = RAND(4)
|
|
313
|
-
this.auth_salt12 = Buffer.concat([MBP.NB('32L', unixTime), MBP.NB('16L', milliseconds), RAND(2), this.localNonce])
|
|
227
|
+
// The next outgoing (time, counter): strictly increasing, from the wall clock.
|
|
228
|
+
_nextClock () {
|
|
229
|
+
let ms = Date.now()
|
|
230
|
+
let counter
|
|
231
|
+
if (ms > this._lastMs) {
|
|
232
|
+
counter = (this._lastCounter + 1) & 0xffff
|
|
314
233
|
} else {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
block.writeUInt16LE(milliseconds, salt + 4)
|
|
319
|
-
globalThis.crypto.getRandomValues(block.subarray(salt + 6, salt + 12))
|
|
320
|
-
block.copy(block, OFFSET.localNonce, salt + 8, salt + 12)
|
|
234
|
+
ms = this._lastMs
|
|
235
|
+
counter = this._lastCounter + 1
|
|
236
|
+
if (counter > 0xffff) { ms++; counter = 0 }
|
|
321
237
|
}
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
MBP.NB('8', BohoMsg.SERVER_TIME_NONCE),
|
|
326
|
-
salt12
|
|
327
|
-
])
|
|
328
|
-
return infoPack
|
|
238
|
+
this._lastMs = ms
|
|
239
|
+
this._lastCounter = counter
|
|
240
|
+
return { sec: Math.floor(ms / 1000), ms: ms % 1000, counter }
|
|
329
241
|
}
|
|
330
242
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
let salt12 = Buffer.concat([
|
|
342
|
-
MBP.NB('32L', server_time_nonce.unixTime),
|
|
343
|
-
MBP.NB('16L', server_time_nonce.milTime),
|
|
344
|
-
MBP.NB('16L', server_time_nonce.counter),
|
|
345
|
-
server_time_nonce.nonce
|
|
346
|
-
])
|
|
347
|
-
|
|
348
|
-
this.set_salt12(salt12, 'auth_req')
|
|
349
|
-
|
|
350
|
-
this.localNonce = RAND(4)
|
|
351
|
-
this.generateHMAC(this.localNonce)
|
|
352
|
-
|
|
353
|
-
this.remoteNonce = server_time_nonce.nonce
|
|
354
|
-
|
|
355
|
-
let auth_hmac_buffer = MBP.pack(
|
|
356
|
-
MBP.MB('#header', '8', BohoMsg.AUTH_REQ),
|
|
357
|
-
MBP.MB('#id8', this._id8),
|
|
358
|
-
MBP.MB('#nonce', this.localNonce),
|
|
359
|
-
MBP.MB('#hmac32', this._hmac),
|
|
360
|
-
)
|
|
361
|
-
|
|
362
|
-
return auth_hmac_buffer
|
|
363
|
-
}
|
|
364
|
-
return false
|
|
243
|
+
_header (type, length) {
|
|
244
|
+
const clock = this._nextClock()
|
|
245
|
+
const h = new Uint8Array(13)
|
|
246
|
+
const view = new DataView(h.buffer)
|
|
247
|
+
h[0] = type
|
|
248
|
+
view.setUint32(1, length, true)
|
|
249
|
+
view.setUint32(5, clock.sec, true)
|
|
250
|
+
view.setUint16(9, clock.ms, true)
|
|
251
|
+
view.setUint16(11, clock.counter, true)
|
|
252
|
+
return h
|
|
365
253
|
}
|
|
366
254
|
|
|
367
|
-
/*
|
|
368
|
-
step 4. for server
|
|
369
|
-
*/
|
|
370
|
-
|
|
371
255
|
/**
|
|
372
|
-
*
|
|
373
|
-
*
|
|
374
|
-
* @returns {
|
|
256
|
+
* ENC (the whole message) or ENC_E2E (a routing header; the caller appends the E2E body).
|
|
257
|
+
* Only after a verified login.
|
|
258
|
+
* @returns {Buffer|undefined}
|
|
375
259
|
*/
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
if (
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
!(infoPack.nonce instanceof Uint8Array) || infoPack.nonce.length !== 4 ||
|
|
388
|
-
!(infoPack.hmac32 instanceof Uint8Array) || infoPack.hmac32.length !== 32 ||
|
|
389
|
-
(infoPack.$OTHERS && infoPack.$OTHERS.length !== 0)) return false
|
|
390
|
-
|
|
391
|
-
this.set_salt12(this.auth_salt12, 'verify_auth_req 1/2')
|
|
392
|
-
|
|
393
|
-
this.generateHMAC(infoPack.nonce)
|
|
394
|
-
let hmac32 = this._hmac
|
|
395
|
-
|
|
396
|
-
if (equalTag(infoPack.hmac32, hmac32)) {
|
|
397
|
-
this.remoteNonce = Buffer.from(infoPack.nonce)
|
|
398
|
-
|
|
399
|
-
let salt12 = Buffer.concat([
|
|
400
|
-
this.localNonce,
|
|
401
|
-
this.remoteNonce,
|
|
402
|
-
this.localNonce
|
|
403
|
-
])
|
|
404
|
-
|
|
405
|
-
this.set_salt12(salt12, 'verify_auth_req 2/2')
|
|
406
|
-
this.generateHMAC(infoPack.nonce)
|
|
407
|
-
let replyHMAC = this._hmac
|
|
408
|
-
|
|
409
|
-
let auth_res = MBP.rawPack(
|
|
410
|
-
MBP.MB('header', '8', BohoMsg.AUTH_RES),
|
|
411
|
-
MBP.MB('hmac32', replyHMAC)
|
|
412
|
-
)
|
|
413
|
-
this.isAuthorized = true
|
|
414
|
-
return auth_res
|
|
415
|
-
}
|
|
416
|
-
return false
|
|
260
|
+
encrypt_session (data, type = BohoMsg.ENC) {
|
|
261
|
+
if (!this.isAuthorized || !this._sessionKey) return
|
|
262
|
+
if (type !== BohoMsg.ENC && type !== BohoMsg.ENC_E2E) return
|
|
263
|
+
const plain = bytes(data)
|
|
264
|
+
const header = this._header(type, plain.length)
|
|
265
|
+
const sealed = encrypt(this._sessionKey, sessionNonce(header, DIR[this._role]), plain, header, TAG)
|
|
266
|
+
const packet = Buffer.alloc(MetaSize.ENC + plain.length)
|
|
267
|
+
packet.set(header, 0)
|
|
268
|
+
packet.set(sealed.subarray(plain.length), 13)
|
|
269
|
+
packet.set(sealed.subarray(0, plain.length), 21)
|
|
270
|
+
return packet
|
|
417
271
|
}
|
|
418
272
|
|
|
419
273
|
/**
|
|
420
|
-
*
|
|
421
|
-
* @
|
|
422
|
-
* @returns {boolean}
|
|
274
|
+
* Opens ENC (exact length) or the routing header of ENC_E2E (the body after it is not read).
|
|
275
|
+
* @returns {Buffer|undefined} the plaintext
|
|
423
276
|
*/
|
|
424
|
-
|
|
425
|
-
if (!this.
|
|
426
|
-
|
|
427
|
-
if (
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
return true
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
return
|
|
277
|
+
decrypt_session (packet) {
|
|
278
|
+
if (!this.isAuthorized || !this._sessionKey || !(packet instanceof Uint8Array) || packet.length < MetaSize.ENC) return
|
|
279
|
+
const type = packet[0]
|
|
280
|
+
if (type !== BohoMsg.ENC && type !== BohoMsg.ENC_E2E) return
|
|
281
|
+
const h = readHeader(packet)
|
|
282
|
+
const room = packet.length - MetaSize.ENC
|
|
283
|
+
if (h.length > room || (type === BohoMsg.ENC && h.length !== room) || h.ms >= 1000) return
|
|
284
|
+
const header = packet.subarray(0, 13)
|
|
285
|
+
const sealed = new Uint8Array(h.length + TAG)
|
|
286
|
+
sealed.set(packet.subarray(21, 21 + h.length))
|
|
287
|
+
sealed.set(packet.subarray(13, 21), h.length)
|
|
288
|
+
const other = this._role === 'client' ? DIR.server : DIR.client
|
|
289
|
+
const plain = decrypt(this._sessionKey, sessionNonce(header, other), sealed, header, TAG)
|
|
290
|
+
return plain ? Buffer.from(plain.buffer, plain.byteOffset, plain.byteLength) : undefined
|
|
442
291
|
}
|
|
443
292
|
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
let pack = MBP.pack(
|
|
464
|
-
MBP.MB('#type', '8', BohoMsg.ENC_488),
|
|
465
|
-
MBP.MB('#len', '32L', data.byteLength),
|
|
466
|
-
MBP.MB('#otpSrc8', this._otpSrc44.subarray(32, 40)),
|
|
467
|
-
MBP.MB('#hmac8', hmac8),
|
|
468
|
-
MBP.MB('#xdata', encData)
|
|
469
|
-
)
|
|
470
|
-
return pack
|
|
293
|
+
_seal (key, data) {
|
|
294
|
+
const plain = bytes(data)
|
|
295
|
+
this._sender ??= RAND(8)
|
|
296
|
+
const clock = this._nextClock()
|
|
297
|
+
const head = new Uint8Array(21)
|
|
298
|
+
const view = new DataView(head.buffer)
|
|
299
|
+
head[0] = BohoMsg.ENC_PACK
|
|
300
|
+
view.setUint32(1, plain.length, true)
|
|
301
|
+
view.setUint32(5, clock.sec, true)
|
|
302
|
+
view.setUint16(9, clock.ms, true)
|
|
303
|
+
view.setUint16(11, clock.counter, true)
|
|
304
|
+
head.set(this._sender, 13)
|
|
305
|
+
const sealed = encrypt(key, head.subarray(5, 21), plain, head.subarray(0, 5), TAG)
|
|
306
|
+
const packet = Buffer.alloc(MetaSize.ENC_PACK + plain.length)
|
|
307
|
+
packet.set(head, 0)
|
|
308
|
+
packet.set(sealed.subarray(plain.length), 21)
|
|
309
|
+
packet.set(sealed.subarray(0, plain.length), 29)
|
|
310
|
+
return packet
|
|
471
311
|
}
|
|
472
312
|
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
const headerLength = packet.readUInt32LE(1)
|
|
487
|
-
if (headerLength > packet.length - MetaSize.ENC_488) return
|
|
488
|
-
header = Buffer.from(packet.subarray(0, MetaSize.ENC_488 + headerLength))
|
|
489
|
-
header[0] = BohoMsg.ENC_488
|
|
313
|
+
_open (key, packet) {
|
|
314
|
+
if (!(packet instanceof Uint8Array) || packet.length < MetaSize.ENC_PACK || packet[0] !== BohoMsg.ENC_PACK) return
|
|
315
|
+
const h = readHeader(packet)
|
|
316
|
+
if (h.length !== packet.length - MetaSize.ENC_PACK || h.ms >= 1000) return
|
|
317
|
+
const sealed = new Uint8Array(h.length + TAG)
|
|
318
|
+
sealed.set(packet.subarray(29))
|
|
319
|
+
sealed.set(packet.subarray(21, 29), h.length)
|
|
320
|
+
const plain = decrypt(key, packet.subarray(5, 21), sealed, packet.subarray(0, 5), TAG)
|
|
321
|
+
if (!plain) return
|
|
322
|
+
return {
|
|
323
|
+
type: BohoMsg.ENC_PACK, len: h.length, sec: h.sec, ms: h.ms, counter: h.counter,
|
|
324
|
+
sender: Buffer.from(packet.subarray(13, 21)),
|
|
325
|
+
data: Buffer.from(plain.buffer, plain.byteOffset, plain.byteLength)
|
|
490
326
|
}
|
|
491
|
-
const pack = readPacket(header, BohoMsg.ENC_488, MetaSize.ENC_488, Meta.ENC_488, true)
|
|
492
|
-
if (!pack || !validClock(pack.otpSrc8)) return
|
|
493
|
-
this.set_salt12(Buffer.concat([pack.otpSrc8, this.localNonce]), 'decrypt_488')
|
|
494
|
-
this.resetOTP()
|
|
495
|
-
const decData = this.xotp(pack.$OTHERS ?? Buffer.alloc(0))
|
|
496
|
-
if (!equalTag(this.getHMAC8(decData), pack.hmac8)) return
|
|
497
|
-
// Session freshness/replay policy belongs to the caller (iosignal).
|
|
498
|
-
return decData
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
/**
|
|
502
|
-
* Generates encrypted packet for up to 2^32-1 bytes
|
|
503
|
-
* @param {Buffer} data
|
|
504
|
-
* @returns {Buffer}
|
|
505
|
-
*/
|
|
506
|
-
encryptPack(data) {
|
|
507
|
-
this._requireKey()
|
|
508
|
-
data = MBP.B8(data)
|
|
509
|
-
|
|
510
|
-
this.set_clock_rand()
|
|
511
|
-
this.resetOTP()
|
|
512
|
-
|
|
513
|
-
let hmac8 = this.getHMAC8(data)
|
|
514
|
-
let encData = this.xotp(data)
|
|
515
|
-
|
|
516
|
-
let pack = MBP.pack(
|
|
517
|
-
MBP.MB('#type', '8', BohoMsg.ENC_PACK),
|
|
518
|
-
MBP.MB('#len', '32L', data.byteLength),
|
|
519
|
-
MBP.MB('#salt12', this._otpSrc44.subarray(32)),
|
|
520
|
-
MBP.MB('#hmac8', hmac8),
|
|
521
|
-
MBP.MB('#xdata', encData)
|
|
522
|
-
)
|
|
523
|
-
return pack
|
|
524
327
|
}
|
|
525
328
|
|
|
526
|
-
/**
|
|
527
|
-
|
|
528
|
-
* @param {Buffer} data
|
|
529
|
-
* @returns {Buffer}
|
|
530
|
-
*/
|
|
531
|
-
decryptPack(data) {
|
|
532
|
-
if (!this._hasKey) return
|
|
533
|
-
const pack = readPacket(data, BohoMsg.ENC_PACK, MetaSize.ENC_PACK, Meta.ENC_PACK, true)
|
|
534
|
-
if (!pack) return
|
|
535
|
-
this.set_salt12(pack.salt12, 'decryptPack')
|
|
536
|
-
this.resetOTP()
|
|
537
|
-
const decData = this.xotp(pack.$OTHERS ?? Buffer.alloc(0))
|
|
538
|
-
if (!equalTag(pack.hmac, this.getHMAC8(decData))) return
|
|
539
|
-
pack.data = decData
|
|
540
|
-
return pack
|
|
541
|
-
}
|
|
329
|
+
/** ENC_PACK with this side's key. */
|
|
330
|
+
encryptPack (data) { if (this._key) return this._seal(this._key, data) }
|
|
542
331
|
|
|
543
|
-
/**
|
|
544
|
-
|
|
545
|
-
* @param {Buffer} data
|
|
546
|
-
* @param {Buffer} key
|
|
547
|
-
* @returns {Buffer}
|
|
548
|
-
*/
|
|
549
|
-
encrypt_e2e(data, key) {
|
|
550
|
-
return this._withKey(key, () => this.encryptPack(data))
|
|
551
|
-
}
|
|
332
|
+
/** Opens ENC_PACK with this side's key: { data, sec, ms, counter, sender, ... } or undefined. */
|
|
333
|
+
decryptPack (packet) { if (this._key) return this._open(this._key, packet) }
|
|
552
334
|
|
|
553
|
-
/**
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
*/
|
|
559
|
-
decrypt_e2e(data, key) {
|
|
560
|
-
return this._withKey(key, () => this.decryptPack(data))
|
|
335
|
+
/** ENC_PACK with an E2E key string. */
|
|
336
|
+
encrypt_e2e (data, key) {
|
|
337
|
+
if (!key) return
|
|
338
|
+
const k = deriveKey(key)
|
|
339
|
+
try { return this._seal(k, data) } finally { wipe(k) }
|
|
561
340
|
}
|
|
562
341
|
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
if (!
|
|
566
|
-
const
|
|
567
|
-
|
|
568
|
-
try {
|
|
569
|
-
this._otpSrc44.set(sha256.hash(bytes), 0)
|
|
570
|
-
this._hasKey = true
|
|
571
|
-
return operation()
|
|
572
|
-
} finally {
|
|
573
|
-
this._otpSrc44.set(state[0])
|
|
574
|
-
this._otp36.set(state[1])
|
|
575
|
-
this._hmac.set(state[2])
|
|
576
|
-
this._hasKey = hadKey
|
|
577
|
-
for (const buffer of state) buffer.fill(0)
|
|
578
|
-
}
|
|
342
|
+
/** Opens ENC_PACK with an E2E key string: { data, ... } or undefined. */
|
|
343
|
+
decrypt_e2e (packet, key) {
|
|
344
|
+
if (!key) return
|
|
345
|
+
const k = deriveKey(key)
|
|
346
|
+
try { return this._open(k, packet) } finally { wipe(k) }
|
|
579
347
|
}
|
|
580
|
-
|
|
581
348
|
}
|
|
582
349
|
|
|
350
|
+
Boho.deriveKey = deriveKey
|