boho 1.1.0 → 2.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.md +134 -38
- package/dist/boho.d.ts +163 -40
- package/dist/boho.js +3 -3
- package/dist/boho.js.map +1 -1
- package/dist/boho.min.js +3 -3
- package/dist/boho.min.js.map +1 -1
- package/package.json +2 -2
- package/rollup.config.js +0 -17
- package/src/boho.js +100 -99
- package/src/constants.js +43 -47
- package/src/index.js +9 -2
- package/src/sha256-mbp.js +7 -0
- package/test/AUTH_process.js +24 -25
- package/test-browser/index.html +26 -26
- package/dist/boho.cjs +0 -15
- package/test/ENC_PACK.cjs +0 -24
- package/test/RAND.cjs +0 -18
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "boho",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Secure Lightweight Encryption & Authentication Library for Node.js, Browsers and Arduino.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
7
7
|
"import": "./dist/boho.js",
|
|
8
|
-
"require":
|
|
8
|
+
"require": null
|
|
9
9
|
},
|
|
10
10
|
"types": "dist/boho.d.ts",
|
|
11
11
|
"browser": "./dist/boho.min.js",
|
package/rollup.config.js
CHANGED
|
@@ -25,23 +25,6 @@ export default [
|
|
|
25
25
|
commonjs()
|
|
26
26
|
, terser()
|
|
27
27
|
]
|
|
28
|
-
},
|
|
29
|
-
{
|
|
30
|
-
input: './src/index.js',
|
|
31
|
-
output: [
|
|
32
|
-
{
|
|
33
|
-
file: './dist/boho.cjs',
|
|
34
|
-
format: 'cjs',
|
|
35
|
-
exports: 'default'
|
|
36
|
-
}
|
|
37
|
-
],
|
|
38
|
-
plugins: [
|
|
39
|
-
resolve({
|
|
40
|
-
preferBuiltins: true
|
|
41
|
-
}),
|
|
42
|
-
commonjs()
|
|
43
|
-
, terser()
|
|
44
|
-
]
|
|
45
28
|
}
|
|
46
29
|
|
|
47
30
|
]
|
package/src/boho.js
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
|
+
// Copyright (c) 2024 Taeo Lee (sixgen@gmail.com)
|
|
2
|
+
// MIT License
|
|
3
|
+
//
|
|
4
|
+
// https://github.com/remocons/boho
|
|
5
|
+
//
|
|
6
|
+
//
|
|
7
|
+
//
|
|
1
8
|
import { sha256 } from './sha256-mbp.js'
|
|
2
9
|
import MBP from 'meta-buffer-pack'
|
|
3
10
|
export { MBP }
|
|
4
11
|
import { BohoMsg, Meta, MetaSize } from './constants.js'
|
|
5
12
|
export { BohoMsg, Meta, MetaSize, sha256 }
|
|
6
13
|
import { Buffer } from 'buffer/index.js'
|
|
7
|
-
export { Buffer}
|
|
14
|
+
export { Buffer }
|
|
8
15
|
|
|
9
16
|
/**
|
|
10
|
-
*
|
|
11
|
-
* @param {number} size -
|
|
17
|
+
* Generates a random byte buffer.
|
|
18
|
+
* @param {number} size - Number of bytes to generate
|
|
12
19
|
* @returns {Buffer}
|
|
13
20
|
*/
|
|
14
21
|
export function RAND(size) {
|
|
@@ -16,7 +23,7 @@ export function RAND(size) {
|
|
|
16
23
|
}
|
|
17
24
|
|
|
18
25
|
/**
|
|
19
|
-
* Boho
|
|
26
|
+
* Boho security protocol class
|
|
20
27
|
*/
|
|
21
28
|
export class Boho {
|
|
22
29
|
/**
|
|
@@ -33,13 +40,13 @@ export class Boho {
|
|
|
33
40
|
this.localNonce = Buffer.alloc(4)
|
|
34
41
|
this.remoteNonce = Buffer.alloc(4)
|
|
35
42
|
this.isAuthorized = false
|
|
36
|
-
|
|
43
|
+
this.counter = 0;
|
|
37
44
|
}
|
|
38
45
|
|
|
39
46
|
/**
|
|
40
|
-
*
|
|
47
|
+
* Initializes authentication state.
|
|
41
48
|
*/
|
|
42
|
-
clearAuth(){
|
|
49
|
+
clearAuth() {
|
|
43
50
|
this._id8.fill(0)
|
|
44
51
|
this._otpSrc44.fill(0)
|
|
45
52
|
this._otp36.fill(0)
|
|
@@ -48,10 +55,11 @@ export class Boho {
|
|
|
48
55
|
this.localNonce.fill(0)
|
|
49
56
|
this.remoteNonce.fill(0)
|
|
50
57
|
this.isAuthorized = false
|
|
58
|
+
this.counter = 0;
|
|
51
59
|
}
|
|
52
60
|
|
|
53
61
|
/**
|
|
54
|
-
*
|
|
62
|
+
* Sets id8 by hashing the input data.
|
|
55
63
|
* @param {any} data
|
|
56
64
|
*/
|
|
57
65
|
set_hash_id8(data) {
|
|
@@ -60,7 +68,7 @@ export class Boho {
|
|
|
60
68
|
}
|
|
61
69
|
|
|
62
70
|
/**
|
|
63
|
-
* id8
|
|
71
|
+
* Sets the id8 value.
|
|
64
72
|
* @param {any} data
|
|
65
73
|
*/
|
|
66
74
|
set_id8(data) {
|
|
@@ -70,7 +78,7 @@ export class Boho {
|
|
|
70
78
|
}
|
|
71
79
|
|
|
72
80
|
/**
|
|
73
|
-
*
|
|
81
|
+
* Sets the key value by hashing and storing in otpSrc44.
|
|
74
82
|
* @param {any} data
|
|
75
83
|
*/
|
|
76
84
|
set_key(data) {
|
|
@@ -79,12 +87,12 @@ export class Boho {
|
|
|
79
87
|
}
|
|
80
88
|
|
|
81
89
|
/**
|
|
82
|
-
* 'id.key'
|
|
90
|
+
* Splits 'id.key' string and sets id8 and key.
|
|
83
91
|
* @param {string} id_key
|
|
84
92
|
*/
|
|
85
93
|
set_id_key(id_key) {
|
|
86
94
|
let delimiterPosition = id_key.indexOf('.')
|
|
87
|
-
if(
|
|
95
|
+
if (delimiterPosition == -1) return
|
|
88
96
|
let id = id_key.substring(0, delimiterPosition)
|
|
89
97
|
let key = id_key.substring(delimiterPosition + 1)
|
|
90
98
|
this.set_id8(id)
|
|
@@ -92,7 +100,7 @@ export class Boho {
|
|
|
92
100
|
}
|
|
93
101
|
|
|
94
102
|
/**
|
|
95
|
-
*
|
|
103
|
+
* Copies id8 value from external buffer.
|
|
96
104
|
* @param {Buffer} data
|
|
97
105
|
*/
|
|
98
106
|
copy_id8(data) {
|
|
@@ -100,7 +108,7 @@ export class Boho {
|
|
|
100
108
|
}
|
|
101
109
|
|
|
102
110
|
/**
|
|
103
|
-
*
|
|
111
|
+
* Copies key value from external buffer.
|
|
104
112
|
* @param {Buffer} data
|
|
105
113
|
*/
|
|
106
114
|
copy_key(data) {
|
|
@@ -108,7 +116,7 @@ export class Boho {
|
|
|
108
116
|
}
|
|
109
117
|
|
|
110
118
|
/**
|
|
111
|
-
* sha256
|
|
119
|
+
* Applies sha256 hash n times.
|
|
112
120
|
* @param {any} srcData
|
|
113
121
|
* @param {number} n
|
|
114
122
|
* @returns {Uint8Array}
|
|
@@ -119,50 +127,54 @@ export class Boho {
|
|
|
119
127
|
return hashSum
|
|
120
128
|
}
|
|
121
129
|
|
|
130
|
+
|
|
122
131
|
/**
|
|
123
|
-
*
|
|
132
|
+
* Sets random clock value (salt12) in otpSrc44.
|
|
124
133
|
*/
|
|
125
134
|
set_clock_rand() {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
milTime = milTime % 0xffffffff
|
|
135
|
+
const now = Date.now()
|
|
136
|
+
const secTime = parseInt(now / 1000)
|
|
137
|
+
const milTime = now % 1000
|
|
130
138
|
const salt12 = Buffer.concat([
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
139
|
+
MBP.NB('32L', secTime),
|
|
140
|
+
MBP.NB('16L', milTime),
|
|
141
|
+
MBP.NB('16L', ++this.counter),
|
|
142
|
+
RAND(4) // JS: use crypto.getRandomValues(), Arduino: use micros()
|
|
134
143
|
])
|
|
135
|
-
|
|
136
144
|
salt12.copy(this._otpSrc44, 32)
|
|
137
145
|
}
|
|
138
146
|
|
|
139
147
|
/**
|
|
140
|
-
*
|
|
148
|
+
* Sets otpSrc44 with given nonce and clock value.
|
|
141
149
|
* @param {Buffer} nonce
|
|
142
150
|
*/
|
|
143
151
|
set_clock_nonce(nonce) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
milTime =
|
|
152
|
+
const now = Date.now()
|
|
153
|
+
const secTime = parseInt(now / 1000)
|
|
154
|
+
const milTime = now % 1000
|
|
147
155
|
const salt12 = Buffer.concat([
|
|
148
|
-
|
|
149
|
-
|
|
156
|
+
MBP.NB('32L', secTime),
|
|
157
|
+
MBP.NB('16L', milTime),
|
|
158
|
+
MBP.NB('16L', ++this.counter),
|
|
150
159
|
nonce
|
|
151
160
|
])
|
|
152
|
-
|
|
153
161
|
salt12.copy(this._otpSrc44, 32)
|
|
154
162
|
}
|
|
155
163
|
|
|
156
164
|
/**
|
|
157
|
-
* salt12
|
|
165
|
+
* Sets salt12 value in otpSrc44.
|
|
158
166
|
* @param {Buffer} salt12
|
|
159
167
|
*/
|
|
160
|
-
set_salt12(salt12) {
|
|
168
|
+
set_salt12(salt12 , caller) {
|
|
169
|
+
// console.log('boho.set_salt12', salt12 , caller )
|
|
170
|
+
if( salt12.byteLength != 12){
|
|
171
|
+
throw TypeError('set_salt12: Invalid salt12 byteLength.')
|
|
172
|
+
}
|
|
161
173
|
salt12.copy(this._otpSrc44, 32)
|
|
162
174
|
}
|
|
163
175
|
|
|
164
176
|
/**
|
|
165
|
-
*
|
|
177
|
+
* Initializes OTP value.
|
|
166
178
|
*/
|
|
167
179
|
resetOTP() {
|
|
168
180
|
let otp32 = MBP.B8(sha256.hash(this._otpSrc44))
|
|
@@ -170,7 +182,7 @@ export class Boho {
|
|
|
170
182
|
}
|
|
171
183
|
|
|
172
184
|
/**
|
|
173
|
-
*
|
|
185
|
+
* Returns OTP value for the given index.
|
|
174
186
|
* @param {number} otpIndex
|
|
175
187
|
* @returns {Uint8Array}
|
|
176
188
|
*/
|
|
@@ -180,7 +192,7 @@ export class Boho {
|
|
|
180
192
|
}
|
|
181
193
|
|
|
182
194
|
/**
|
|
183
|
-
* HMAC
|
|
195
|
+
* Generates HMAC value.
|
|
184
196
|
* @param {Buffer} data
|
|
185
197
|
*/
|
|
186
198
|
generateHMAC(data) {
|
|
@@ -189,7 +201,7 @@ export class Boho {
|
|
|
189
201
|
}
|
|
190
202
|
|
|
191
203
|
/**
|
|
192
|
-
* 8
|
|
204
|
+
* Returns 8-byte HMAC value.
|
|
193
205
|
* @param {Buffer} data
|
|
194
206
|
* @returns {Buffer}
|
|
195
207
|
*/
|
|
@@ -200,7 +212,7 @@ export class Boho {
|
|
|
200
212
|
}
|
|
201
213
|
|
|
202
214
|
/**
|
|
203
|
-
* OTP
|
|
215
|
+
* OTP-based XOR encryption/decryption
|
|
204
216
|
* @param {Buffer} data
|
|
205
217
|
* @param {number} [otpStartIndex=0]
|
|
206
218
|
* @param {boolean} [shareDataBuffer=false]
|
|
@@ -229,64 +241,57 @@ export class Boho {
|
|
|
229
241
|
// B. AUTH process
|
|
230
242
|
|
|
231
243
|
/**
|
|
232
|
-
*
|
|
244
|
+
* Generates server time and nonce signal
|
|
233
245
|
* @returns {Buffer}
|
|
234
246
|
*/
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
)
|
|
240
|
-
}
|
|
247
|
+
server_time_nonce() {
|
|
248
|
+
const now = Date.now()
|
|
249
|
+
const unixTime = Math.floor(now / 1000)
|
|
250
|
+
const milliseconds = now % 1000;
|
|
241
251
|
|
|
242
|
-
/**
|
|
243
|
-
* AUTH_NONCE 메시지 생성
|
|
244
|
-
* @returns {Buffer}
|
|
245
|
-
*/
|
|
246
|
-
auth_nonce() {
|
|
247
|
-
let now = Date.now()
|
|
248
|
-
let unixTime = Math.floor(now / 1000)
|
|
249
|
-
let milTime = now % 1000
|
|
250
252
|
this.localNonce = RAND(4)
|
|
253
|
+
//keep
|
|
251
254
|
this.auth_salt12 = Buffer.concat([
|
|
252
|
-
|
|
253
|
-
|
|
255
|
+
MBP.NB('32L', unixTime),
|
|
256
|
+
MBP.NB('16L', milliseconds),
|
|
257
|
+
RAND(2), // used for client's counter IV.
|
|
254
258
|
this.localNonce
|
|
255
259
|
])
|
|
256
260
|
|
|
257
261
|
let infoPack = Buffer.concat([
|
|
258
|
-
|
|
262
|
+
MBP.NB('8', BohoMsg.SERVER_TIME_NONCE),
|
|
259
263
|
this.auth_salt12
|
|
260
264
|
])
|
|
261
265
|
return infoPack
|
|
262
266
|
}
|
|
263
267
|
|
|
264
268
|
/**
|
|
265
|
-
*
|
|
266
|
-
* @param {Buffer} buffer
|
|
269
|
+
* Generates AUTH_REQ message
|
|
270
|
+
* @param {Buffer} buffer server's time nonce
|
|
267
271
|
* @returns {Buffer|boolean}
|
|
268
272
|
*/
|
|
269
|
-
|
|
270
|
-
let
|
|
271
|
-
if (
|
|
273
|
+
auth_req(buffer) {
|
|
274
|
+
let server_time_nonce = MBP.unpack(buffer, Meta.SERVER_TIME_NONCE)
|
|
275
|
+
if (server_time_nonce) {
|
|
272
276
|
let salt12 = Buffer.concat([
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
277
|
+
MBP.NB('32L', server_time_nonce.unixTime),
|
|
278
|
+
MBP.NB('16L', server_time_nonce.milTime),
|
|
279
|
+
MBP.NB('16L', server_time_nonce.counter),
|
|
280
|
+
server_time_nonce.nonce
|
|
276
281
|
])
|
|
277
282
|
|
|
278
|
-
this.set_salt12(salt12)
|
|
283
|
+
this.set_salt12(salt12, 'auth_req')
|
|
279
284
|
|
|
280
285
|
this.localNonce = RAND(4)
|
|
281
286
|
this.generateHMAC(this.localNonce)
|
|
282
287
|
|
|
283
|
-
this.remoteNonce =
|
|
288
|
+
this.remoteNonce = server_time_nonce.nonce
|
|
284
289
|
|
|
285
290
|
let auth_hmac_buffer = MBP.pack(
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
291
|
+
MBP.MB('#header', '8', BohoMsg.AUTH_REQ),
|
|
292
|
+
MBP.MB('#id8', this._id8),
|
|
293
|
+
MBP.MB('#nonce', this.localNonce),
|
|
294
|
+
MBP.MB('#hmac32', this._hmac),
|
|
290
295
|
)
|
|
291
296
|
|
|
292
297
|
return auth_hmac_buffer
|
|
@@ -296,31 +301,26 @@ export class Boho {
|
|
|
296
301
|
|
|
297
302
|
/*
|
|
298
303
|
step 4. for server
|
|
299
|
-
|
|
300
|
-
step 4-1. check client's auth_hmac
|
|
301
|
-
step 4-2. reply result
|
|
302
|
-
send AUTH_ACK with another HMAC for client.
|
|
303
|
-
or send AUTH_FAIL when fail.
|
|
304
304
|
*/
|
|
305
305
|
|
|
306
306
|
/**
|
|
307
|
-
*
|
|
307
|
+
* Verify client's AUTH_REQ.
|
|
308
308
|
* @param {Buffer|object} data
|
|
309
309
|
* @returns {boolean}
|
|
310
310
|
*/
|
|
311
|
-
|
|
311
|
+
verify_auth_req(data) {
|
|
312
|
+
|
|
312
313
|
let infoPack
|
|
313
314
|
if (data instanceof Uint8Array) {
|
|
314
|
-
infoPack = MBP.unpack(data, Meta.
|
|
315
|
+
infoPack = MBP.unpack(data, Meta.AUTH_REQ)
|
|
315
316
|
if (!infoPack) {
|
|
316
317
|
return
|
|
317
318
|
}
|
|
318
319
|
} else {
|
|
319
320
|
infoPack = data;
|
|
320
|
-
|
|
321
321
|
}
|
|
322
322
|
|
|
323
|
-
this.set_salt12(this.auth_salt12)
|
|
323
|
+
this.set_salt12(this.auth_salt12, 'verify_auth_req 1/2')
|
|
324
324
|
|
|
325
325
|
this.generateHMAC(infoPack.nonce)
|
|
326
326
|
let hmac32 = this._hmac
|
|
@@ -333,37 +333,38 @@ export class Boho {
|
|
|
333
333
|
this.remoteNonce,
|
|
334
334
|
this.localNonce
|
|
335
335
|
])
|
|
336
|
-
|
|
336
|
+
|
|
337
|
+
this.set_salt12(salt12, 'verify_auth_req 2/2')
|
|
337
338
|
this.generateHMAC(infoPack.nonce)
|
|
338
339
|
let replyHMAC = this._hmac
|
|
339
340
|
|
|
340
|
-
let
|
|
341
|
-
|
|
342
|
-
|
|
341
|
+
let auth_res = MBP.rawPack(
|
|
342
|
+
MBP.MB('header', '8', BohoMsg.AUTH_RES),
|
|
343
|
+
MBP.MB('hmac32', replyHMAC)
|
|
343
344
|
)
|
|
344
345
|
this.isAuthorized = true
|
|
345
|
-
return
|
|
346
|
+
return auth_res
|
|
346
347
|
}
|
|
347
348
|
return false
|
|
348
349
|
}
|
|
349
350
|
|
|
350
351
|
/**
|
|
351
|
-
*
|
|
352
|
+
* Verifies server's AUTH_RES HMAC.
|
|
352
353
|
* @param {Buffer} buffer
|
|
353
354
|
* @returns {boolean}
|
|
354
355
|
*/
|
|
355
|
-
|
|
356
|
-
let
|
|
357
|
-
if (
|
|
356
|
+
verify_auth_res(buffer) {
|
|
357
|
+
let auth_res = MBP.unpack(buffer, Meta.AUTH_RES)
|
|
358
|
+
if (auth_res) {
|
|
358
359
|
let salt12 = Buffer.concat([
|
|
359
360
|
this.remoteNonce,
|
|
360
361
|
this.localNonce,
|
|
361
362
|
this.remoteNonce,
|
|
362
363
|
])
|
|
363
|
-
this.set_salt12(salt12)
|
|
364
|
+
this.set_salt12(salt12, 'verify_auth_res')
|
|
364
365
|
this.generateHMAC(this.localNonce)
|
|
365
366
|
let hmac32 = this._hmac
|
|
366
|
-
if (MBP.equal(hmac32,
|
|
367
|
+
if (MBP.equal(hmac32, auth_res.hmac32)) {
|
|
367
368
|
this.isAuthorized = true
|
|
368
369
|
return true
|
|
369
370
|
}
|
|
@@ -375,7 +376,7 @@ export class Boho {
|
|
|
375
376
|
|
|
376
377
|
// Must AUTH first.
|
|
377
378
|
/**
|
|
378
|
-
*
|
|
379
|
+
* Generates encrypted 488 packet after authentication
|
|
379
380
|
* @param {Buffer} data
|
|
380
381
|
* @returns {Buffer|undefined}
|
|
381
382
|
*/
|
|
@@ -401,7 +402,7 @@ export class Boho {
|
|
|
401
402
|
}
|
|
402
403
|
|
|
403
404
|
/**
|
|
404
|
-
*
|
|
405
|
+
* Decrypts 488 packet after authentication
|
|
405
406
|
* @param {Buffer} data
|
|
406
407
|
* @returns {Buffer|undefined}
|
|
407
408
|
*/
|
|
@@ -417,7 +418,7 @@ export class Boho {
|
|
|
417
418
|
this.localNonce
|
|
418
419
|
])
|
|
419
420
|
|
|
420
|
-
this.set_salt12(salt12)
|
|
421
|
+
this.set_salt12(salt12 , 'decrypt_488')
|
|
421
422
|
this.resetOTP()
|
|
422
423
|
|
|
423
424
|
let xdata = pack.$OTHERS.subarray(0, pack.len)
|
|
@@ -431,7 +432,7 @@ export class Boho {
|
|
|
431
432
|
}
|
|
432
433
|
|
|
433
434
|
/**
|
|
434
|
-
*
|
|
435
|
+
* Generates encrypted packet for up to 2^32-1 bytes
|
|
435
436
|
* @param {Buffer} data
|
|
436
437
|
* @returns {Buffer}
|
|
437
438
|
*/
|
|
@@ -455,7 +456,7 @@ export class Boho {
|
|
|
455
456
|
}
|
|
456
457
|
|
|
457
458
|
/**
|
|
458
|
-
*
|
|
459
|
+
* Decrypts encrypted packet
|
|
459
460
|
* @param {Buffer} data
|
|
460
461
|
* @returns {Buffer}
|
|
461
462
|
*/
|
|
@@ -474,7 +475,7 @@ export class Boho {
|
|
|
474
475
|
let pack = MBP.unpack(data, Meta.ENC_PACK)
|
|
475
476
|
if (!pack) return
|
|
476
477
|
|
|
477
|
-
this.set_salt12(pack.salt12)
|
|
478
|
+
this.set_salt12(pack.salt12, 'decryptPack')
|
|
478
479
|
this.resetOTP()
|
|
479
480
|
|
|
480
481
|
let xdata = pack.$OTHERS
|
|
@@ -492,7 +493,7 @@ export class Boho {
|
|
|
492
493
|
}
|
|
493
494
|
|
|
494
495
|
/**
|
|
495
|
-
*
|
|
496
|
+
* End-to-end encryption
|
|
496
497
|
* @param {Buffer} data
|
|
497
498
|
* @param {Buffer} key
|
|
498
499
|
* @returns {Buffer}
|
|
@@ -507,7 +508,7 @@ export class Boho {
|
|
|
507
508
|
}
|
|
508
509
|
|
|
509
510
|
/**
|
|
510
|
-
*
|
|
511
|
+
* End-to-end decryption
|
|
511
512
|
* @param {Buffer} data
|
|
512
513
|
* @param {Buffer} key
|
|
513
514
|
* @returns {Buffer}
|
package/src/constants.js
CHANGED
|
@@ -1,76 +1,75 @@
|
|
|
1
|
+
// Copyright (c) 2024 Taeo Lee (sixgen@gmail.com)
|
|
2
|
+
// MIT License
|
|
3
|
+
//
|
|
4
|
+
// https://github.com/remocons/boho
|
|
5
|
+
//
|
|
6
|
+
//
|
|
7
|
+
//
|
|
1
8
|
import MBP from "meta-buffer-pack"
|
|
2
9
|
import { Buffer } from 'buffer/index.js'
|
|
3
10
|
|
|
4
11
|
/**
|
|
5
|
-
* Boho
|
|
12
|
+
* Boho message type constants
|
|
6
13
|
* @typedef {Object} BohoMsg
|
|
14
|
+
* @property {number} SERVER_TIME_NONCE
|
|
7
15
|
* @property {number} AUTH_REQ
|
|
8
|
-
* @property {number}
|
|
9
|
-
* @property {number} AUTH_HMAC
|
|
10
|
-
* @property {number} AUTH_ACK
|
|
16
|
+
* @property {number} AUTH_RES
|
|
11
17
|
* @property {number} AUTH_FAIL
|
|
12
|
-
* @property {number} AUTH_EXT
|
|
13
18
|
* @property {number} ENC_PACK
|
|
14
19
|
* @property {number} ENC_E2E
|
|
15
20
|
* @property {number} ENC_488
|
|
16
21
|
*/
|
|
17
22
|
|
|
18
|
-
//
|
|
23
|
+
// One-byte header for remote control message pack.
|
|
19
24
|
export let BohoMsg = {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
ENC_E2E : 0xB7,
|
|
25
|
+
SERVER_TIME_NONCE : 0xB0,
|
|
26
|
+
AUTH_REQ: 0xB1,
|
|
27
|
+
AUTH_RES: 0xB2,
|
|
28
|
+
AUTH_FAIL: 0xB3,
|
|
29
|
+
|
|
30
|
+
ENC_PACK : 0xB6,
|
|
31
|
+
ENC_E2E : 0xB7,
|
|
28
32
|
ENC_488 : 0xB8
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
for (let c in BohoMsg) { BohoMsg[BohoMsg[c]] = c }
|
|
32
36
|
|
|
33
37
|
/**
|
|
34
|
-
* Boho
|
|
38
|
+
* Boho message meta information
|
|
35
39
|
* @typedef {Object} Meta
|
|
40
|
+
* @property {any} SERVER_TIME_NONCE
|
|
36
41
|
* @property {any} AUTH_REQ
|
|
37
|
-
* @property {any}
|
|
38
|
-
* @property {any} AUTH_HMAC
|
|
39
|
-
* @property {any} AUTH_ACK
|
|
42
|
+
* @property {any} AUTH_RES
|
|
40
43
|
* @property {any} ENC_PACK
|
|
41
44
|
* @property {any} ENC_488
|
|
42
45
|
*/
|
|
43
46
|
|
|
44
47
|
export const Meta = {
|
|
45
48
|
|
|
46
|
-
|
|
47
|
-
MBP.MB('header','8', 0),
|
|
48
|
-
MBP.MB('reserved','8', 0)
|
|
49
|
-
),
|
|
50
|
-
|
|
51
|
-
AUTH_NONCE: MBP.meta( // 13
|
|
49
|
+
SERVER_TIME_NONCE: MBP.meta( // 13
|
|
52
50
|
MBP.MB('header','8', 0),
|
|
53
51
|
MBP.MB('unixTime','32L', 0),
|
|
54
|
-
MBP.MB('milTime','
|
|
52
|
+
MBP.MB('milTime','16L', 0 ),
|
|
53
|
+
MBP.MB('counter','16L', 0 ),
|
|
55
54
|
MBP.MB('nonce', Buffer.alloc(4))
|
|
56
55
|
),
|
|
57
56
|
|
|
58
|
-
|
|
57
|
+
AUTH_REQ: MBP.meta( // 45
|
|
59
58
|
MBP.MB('header','8', 0),
|
|
60
59
|
MBP.MB('id8',Buffer.alloc(8)),
|
|
61
60
|
MBP.MB('nonce', Buffer.alloc(4)),
|
|
62
61
|
MBP.MB('hmac32', Buffer.alloc(32))
|
|
63
62
|
),
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
|
|
64
|
+
AUTH_RES: MBP.meta( // 33
|
|
66
65
|
MBP.MB('header','8', 0),
|
|
67
66
|
MBP.MB('hmac32', Buffer.alloc(32))
|
|
68
67
|
),
|
|
69
|
-
|
|
68
|
+
|
|
70
69
|
|
|
71
70
|
ENC_PACK: MBP.meta( //25 + payload
|
|
72
71
|
MBP.MB('type','8',0),
|
|
73
|
-
MBP.MB('len','32L',0), // pure xdata size.
|
|
72
|
+
MBP.MB('len','32L',0), // pure xdata size.
|
|
74
73
|
MBP.MB('salt12', Buffer.alloc(12)), // sec,mil,rand
|
|
75
74
|
MBP.MB('hmac',8,0)
|
|
76
75
|
// MBP.MB( 'xdata', encData )
|
|
@@ -82,7 +81,7 @@ export const Meta = {
|
|
|
82
81
|
MBP.MB('len','32L', 0 ),
|
|
83
82
|
MBP.MB('otpSrc8', Buffer.alloc(8) ),
|
|
84
83
|
MBP.MB('hmac8', Buffer.alloc(8) )
|
|
85
|
-
// MBP.MB('xdata', encData )
|
|
84
|
+
// MBP.MB('xdata', encData )
|
|
86
85
|
)
|
|
87
86
|
|
|
88
87
|
|
|
@@ -95,35 +94,32 @@ export const Meta = {
|
|
|
95
94
|
}
|
|
96
95
|
|
|
97
96
|
/**
|
|
98
|
-
*
|
|
97
|
+
* Byte size information for each meta
|
|
99
98
|
* @typedef {Object} MetaSize
|
|
100
99
|
* @property {number} AUTH_REQ
|
|
101
|
-
* @property {number}
|
|
102
|
-
* @property {number}
|
|
103
|
-
* @property {number} AUTH_ACK
|
|
100
|
+
* @property {number} SERVER_TIME_NONCE
|
|
101
|
+
* @property {number} AUTH_RES
|
|
104
102
|
* @property {number} ENC_PACK
|
|
105
103
|
* @property {number} ENC_488
|
|
106
104
|
*/
|
|
107
105
|
|
|
108
106
|
/**
|
|
109
|
-
*
|
|
107
|
+
* Byte size information for each meta
|
|
110
108
|
* @type {MetaSize}
|
|
111
109
|
*/
|
|
112
110
|
export const MetaSize = {
|
|
111
|
+
SERVER_TIME_NONCE: getMetaSize( Meta.SERVER_TIME_NONCE ),
|
|
113
112
|
AUTH_REQ: getMetaSize( Meta.AUTH_REQ ),
|
|
114
|
-
|
|
115
|
-
AUTH_HMAC: getMetaSize( Meta.AUTH_HMAC ),
|
|
116
|
-
AUTH_ACK: getMetaSize( Meta.AUTH_ACK ),
|
|
113
|
+
AUTH_RES: getMetaSize( Meta.AUTH_RES ),
|
|
117
114
|
ENC_PACK: getMetaSize( Meta.ENC_PACK ),
|
|
118
115
|
ENC_488: getMetaSize( Meta.ENC_488 )
|
|
119
116
|
}
|
|
120
117
|
|
|
121
|
-
// console.log( 'boho MetaSize', MetaSize )
|
|
118
|
+
// console.log( 'boho 2.0.0 MetaSize', MetaSize )
|
|
122
119
|
// boho MetaSize {
|
|
123
|
-
//
|
|
124
|
-
//
|
|
125
|
-
//
|
|
126
|
-
//
|
|
127
|
-
//
|
|
128
|
-
//
|
|
129
|
-
// }
|
|
120
|
+
// SERVER_TIME_NONCE: 13,
|
|
121
|
+
// AUTH_REQ: 45,
|
|
122
|
+
// AUTH_RES: 33,
|
|
123
|
+
// ENC_PACK: 25,
|
|
124
|
+
// ENC_488: 21
|
|
125
|
+
// }
|