boho 1.0.3 → 1.1.1
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 +96 -37
- package/dist/boho.cjs +10 -3
- package/dist/boho.d.ts +100 -0
- package/dist/boho.js +3 -5
- package/dist/boho.js.map +1 -1
- package/dist/boho.min.js +3 -5
- package/dist/boho.min.js.map +1 -1
- package/package.json +6 -5
- package/src/boho.js +144 -66
- package/src/constants.js +55 -8
- package/src/index.js +27 -0
- package/src/sha256-mbp.js +32 -0
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "boho",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Encryption
|
|
3
|
+
"version": "1.1.1",
|
|
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
8
|
"require": "./dist/boho.cjs"
|
|
9
9
|
},
|
|
10
|
+
"types": "dist/boho.d.ts",
|
|
10
11
|
"browser": "./dist/boho.min.js",
|
|
11
12
|
"jsdelivr": "./dist/boho.min.js",
|
|
12
13
|
"unpkg": "./dist/boho.min.js",
|
|
@@ -29,13 +30,13 @@
|
|
|
29
30
|
},
|
|
30
31
|
"license": "MIT",
|
|
31
32
|
"devDependencies": {
|
|
32
|
-
"@rollup/plugin-commonjs": "^
|
|
33
|
-
"@rollup/plugin-node-resolve": "^15.
|
|
33
|
+
"@rollup/plugin-commonjs": "^28.0.3",
|
|
34
|
+
"@rollup/plugin-node-resolve": "^15.3.1",
|
|
34
35
|
"@rollup/plugin-terser": "^0.4.4",
|
|
35
36
|
"rollup": "^4.36.0"
|
|
36
37
|
},
|
|
37
38
|
"dependencies": {
|
|
38
|
-
"meta-buffer-pack": "^2.0
|
|
39
|
+
"meta-buffer-pack": "^2.1.0"
|
|
39
40
|
},
|
|
40
41
|
"scripts": {
|
|
41
42
|
"build": "rollup -c",
|
package/src/boho.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
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 }
|
|
@@ -6,13 +13,22 @@ export { BohoMsg, Meta, MetaSize, sha256 }
|
|
|
6
13
|
import { Buffer } from 'buffer/index.js'
|
|
7
14
|
export { Buffer}
|
|
8
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Generates a random byte buffer.
|
|
18
|
+
* @param {number} size - Number of bytes to generate
|
|
19
|
+
* @returns {Buffer}
|
|
20
|
+
*/
|
|
9
21
|
export function RAND(size) {
|
|
10
22
|
return globalThis.crypto.getRandomValues(Buffer.alloc(size))
|
|
11
23
|
}
|
|
12
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Boho security protocol class
|
|
27
|
+
*/
|
|
13
28
|
export class Boho {
|
|
14
|
-
|
|
15
|
-
|
|
29
|
+
/**
|
|
30
|
+
* @constructor
|
|
31
|
+
*/
|
|
16
32
|
constructor() {
|
|
17
33
|
|
|
18
34
|
this._id8 = Buffer.alloc(8)
|
|
@@ -27,6 +43,9 @@ export class Boho {
|
|
|
27
43
|
|
|
28
44
|
}
|
|
29
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Initializes authentication state.
|
|
48
|
+
*/
|
|
30
49
|
clearAuth(){
|
|
31
50
|
this._id8.fill(0)
|
|
32
51
|
this._otpSrc44.fill(0)
|
|
@@ -38,24 +57,38 @@ export class Boho {
|
|
|
38
57
|
this.isAuthorized = false
|
|
39
58
|
}
|
|
40
59
|
|
|
41
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Sets id8 by hashing the input data.
|
|
62
|
+
* @param {any} data
|
|
63
|
+
*/
|
|
42
64
|
set_hash_id8(data) {
|
|
43
65
|
let idSum = MBP.B8(sha256.hash(data))
|
|
44
66
|
idSum.copy(this._id8, 0, 0, 8)
|
|
45
67
|
}
|
|
46
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Sets the id8 value.
|
|
71
|
+
* @param {any} data
|
|
72
|
+
*/
|
|
47
73
|
set_id8(data) {
|
|
48
74
|
let encStr = MBP.B8(data)
|
|
49
75
|
this._id8.fill(0)
|
|
50
76
|
encStr.copy(this._id8, 0, 0, 8)
|
|
51
77
|
}
|
|
52
78
|
|
|
79
|
+
/**
|
|
80
|
+
* Sets the key value by hashing and storing in otpSrc44.
|
|
81
|
+
* @param {any} data
|
|
82
|
+
*/
|
|
53
83
|
set_key(data) {
|
|
54
84
|
let keySum = MBP.B8(sha256.hash(data))
|
|
55
85
|
keySum.copy(this._otpSrc44, 0, 0, 32)
|
|
56
86
|
}
|
|
57
87
|
|
|
58
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Splits 'id.key' string and sets id8 and key.
|
|
90
|
+
* @param {string} id_key
|
|
91
|
+
*/
|
|
59
92
|
set_id_key(id_key) {
|
|
60
93
|
let delimiterPosition = id_key.indexOf('.')
|
|
61
94
|
if( delimiterPosition == -1 ) return
|
|
@@ -65,23 +98,37 @@ export class Boho {
|
|
|
65
98
|
this.set_key(key)
|
|
66
99
|
}
|
|
67
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Copies id8 value from external buffer.
|
|
103
|
+
* @param {Buffer} data
|
|
104
|
+
*/
|
|
68
105
|
copy_id8(data) {
|
|
69
106
|
data.copy(this._id8, 0, 0, 8)
|
|
70
107
|
}
|
|
71
108
|
|
|
109
|
+
/**
|
|
110
|
+
* Copies key value from external buffer.
|
|
111
|
+
* @param {Buffer} data
|
|
112
|
+
*/
|
|
72
113
|
copy_key(data) {
|
|
73
114
|
data.copy(this._otpSrc44, 0, 0, 32)
|
|
74
115
|
}
|
|
75
116
|
|
|
76
|
-
|
|
117
|
+
/**
|
|
118
|
+
* Applies sha256 hash n times.
|
|
119
|
+
* @param {any} srcData
|
|
120
|
+
* @param {number} n
|
|
121
|
+
* @returns {Uint8Array}
|
|
122
|
+
*/
|
|
77
123
|
sha256_n(srcData, n) {
|
|
78
124
|
let hashSum = sha256.hash(srcData)
|
|
79
125
|
for (let i = 0; i < n; i++) hashSum = sha256.hash(hashSum)
|
|
80
126
|
return hashSum
|
|
81
127
|
}
|
|
82
128
|
|
|
83
|
-
|
|
84
|
-
|
|
129
|
+
/**
|
|
130
|
+
* Sets random clock value (salt12) in otpSrc44.
|
|
131
|
+
*/
|
|
85
132
|
set_clock_rand() {
|
|
86
133
|
|
|
87
134
|
let milTime = Date.now()
|
|
@@ -96,7 +143,10 @@ export class Boho {
|
|
|
96
143
|
salt12.copy(this._otpSrc44, 32)
|
|
97
144
|
}
|
|
98
145
|
|
|
99
|
-
|
|
146
|
+
/**
|
|
147
|
+
* Sets otpSrc44 with given nonce and clock value.
|
|
148
|
+
* @param {Buffer} nonce
|
|
149
|
+
*/
|
|
100
150
|
set_clock_nonce(nonce) {
|
|
101
151
|
let milTime = Date.now()
|
|
102
152
|
let secTime = parseInt(milTime / 1000)
|
|
@@ -110,34 +160,59 @@ export class Boho {
|
|
|
110
160
|
salt12.copy(this._otpSrc44, 32)
|
|
111
161
|
}
|
|
112
162
|
|
|
113
|
-
|
|
163
|
+
/**
|
|
164
|
+
* Sets salt12 value in otpSrc44.
|
|
165
|
+
* @param {Buffer} salt12
|
|
166
|
+
*/
|
|
114
167
|
set_salt12(salt12) {
|
|
115
168
|
salt12.copy(this._otpSrc44, 32)
|
|
116
169
|
}
|
|
117
170
|
|
|
171
|
+
/**
|
|
172
|
+
* Initializes OTP value.
|
|
173
|
+
*/
|
|
118
174
|
resetOTP() {
|
|
119
175
|
let otp32 = MBP.B8(sha256.hash(this._otpSrc44))
|
|
120
176
|
otp32.copy(this._otp36, 0, 0, 32)
|
|
121
177
|
}
|
|
122
178
|
|
|
179
|
+
/**
|
|
180
|
+
* Returns OTP value for the given index.
|
|
181
|
+
* @param {number} otpIndex
|
|
182
|
+
* @returns {Uint8Array}
|
|
183
|
+
*/
|
|
123
184
|
getIndexOTP(otpIndex) {
|
|
124
185
|
this._otp36.writeUInt32LE(otpIndex, 32)
|
|
125
186
|
return sha256.hash(this._otp36)
|
|
126
187
|
}
|
|
127
188
|
|
|
128
|
-
|
|
189
|
+
/**
|
|
190
|
+
* Generates HMAC value.
|
|
191
|
+
* @param {Buffer} data
|
|
192
|
+
*/
|
|
129
193
|
generateHMAC(data) {
|
|
130
194
|
let hmacSrc = Buffer.concat([this._otpSrc44, data])
|
|
131
195
|
this._hmac = MBP.B8(sha256.hash(hmacSrc))
|
|
132
196
|
}
|
|
133
197
|
|
|
134
|
-
|
|
198
|
+
/**
|
|
199
|
+
* Returns 8-byte HMAC value.
|
|
200
|
+
* @param {Buffer} data
|
|
201
|
+
* @returns {Buffer}
|
|
202
|
+
*/
|
|
135
203
|
getHMAC8(data) {
|
|
136
204
|
let hmacSrc = Buffer.concat([this._otpSrc44, data])
|
|
137
205
|
this._hmac = MBP.B8(sha256.hash(hmacSrc))
|
|
138
206
|
return this._hmac.subarray(0, 8)
|
|
139
207
|
}
|
|
140
208
|
|
|
209
|
+
/**
|
|
210
|
+
* OTP-based XOR encryption/decryption
|
|
211
|
+
* @param {Buffer} data
|
|
212
|
+
* @param {number} [otpStartIndex=0]
|
|
213
|
+
* @param {boolean} [shareDataBuffer=false]
|
|
214
|
+
* @returns {Buffer}
|
|
215
|
+
*/
|
|
141
216
|
xotp(data, otpStartIndex = 0, shareDataBuffer = false) {
|
|
142
217
|
|
|
143
218
|
data = MBP.B8(data, shareDataBuffer)
|
|
@@ -160,8 +235,10 @@ export class Boho {
|
|
|
160
235
|
|
|
161
236
|
// B. AUTH process
|
|
162
237
|
|
|
163
|
-
|
|
164
|
-
|
|
238
|
+
/**
|
|
239
|
+
* Generates AUTH_REQ message
|
|
240
|
+
* @returns {Buffer}
|
|
241
|
+
*/
|
|
165
242
|
auth_req() {
|
|
166
243
|
return MBP.pack(
|
|
167
244
|
MBP.MB('#type', '8', BohoMsg.AUTH_REQ),
|
|
@@ -169,8 +246,10 @@ export class Boho {
|
|
|
169
246
|
)
|
|
170
247
|
}
|
|
171
248
|
|
|
172
|
-
|
|
173
|
-
|
|
249
|
+
/**
|
|
250
|
+
* Generates AUTH_NONCE message
|
|
251
|
+
* @returns {Buffer}
|
|
252
|
+
*/
|
|
174
253
|
auth_nonce() {
|
|
175
254
|
let now = Date.now()
|
|
176
255
|
let unixTime = Math.floor(now / 1000)
|
|
@@ -189,26 +268,14 @@ export class Boho {
|
|
|
189
268
|
return infoPack
|
|
190
269
|
}
|
|
191
270
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
271
|
+
/**
|
|
272
|
+
* Generates AUTH_HMAC message
|
|
273
|
+
* @param {Buffer} buffer
|
|
274
|
+
* @returns {Buffer|boolean}
|
|
275
|
+
*/
|
|
196
276
|
auth_hmac(buffer) {
|
|
197
277
|
let auth_nonce = MBP.unpack(buffer, Meta.AUTH_NONCE)
|
|
198
278
|
if (auth_nonce) {
|
|
199
|
-
// console.log(' auth nonce', auth_nonce )
|
|
200
|
-
|
|
201
|
-
// let now = Date.now()
|
|
202
|
-
// let localUTC= Math.floor( now/ 1000 )
|
|
203
|
-
// let localMilTime = now % 1000
|
|
204
|
-
|
|
205
|
-
// console.log('time server [sec]', auth_nonce.unixTime, auth_nonce.milTime )
|
|
206
|
-
// console.log('time client [sec]', localUTC , localMilTime )
|
|
207
|
-
// console.log('time diff client and server[sec]', auth_nonce.unixTime - localUTC )
|
|
208
|
-
|
|
209
|
-
// let serverSecMil = auth_nonce.unixTime * 1000 + auth_nonce.milTime
|
|
210
|
-
// console.log('time diff msec client and server[msec]', serverSecMil - now )
|
|
211
|
-
|
|
212
279
|
let salt12 = Buffer.concat([
|
|
213
280
|
MBP.NB('32L', auth_nonce.unixTime),
|
|
214
281
|
MBP.NB('32L', auth_nonce.milTime),
|
|
@@ -218,18 +285,15 @@ export class Boho {
|
|
|
218
285
|
this.set_salt12(salt12)
|
|
219
286
|
|
|
220
287
|
this.localNonce = RAND(4)
|
|
221
|
-
// hmac( key, sec,mil,serverNonce, localNonce)
|
|
222
288
|
this.generateHMAC(this.localNonce)
|
|
223
289
|
|
|
224
|
-
// let hmac8 = this._hmac.subarray(0, 8)
|
|
225
|
-
|
|
226
290
|
this.remoteNonce = auth_nonce.nonce
|
|
227
291
|
|
|
228
|
-
let auth_hmac_buffer = MBP.pack(
|
|
292
|
+
let auth_hmac_buffer = MBP.pack(
|
|
229
293
|
MBP.MB('#header', '8', BohoMsg.AUTH_HMAC),
|
|
230
294
|
MBP.MB('#id8', this._id8),
|
|
231
295
|
MBP.MB('#nonce', this.localNonce),
|
|
232
|
-
MBP.MB('#hmac32', this._hmac ),
|
|
296
|
+
MBP.MB('#hmac32', this._hmac ),
|
|
233
297
|
)
|
|
234
298
|
|
|
235
299
|
return auth_hmac_buffer
|
|
@@ -246,30 +310,29 @@ export class Boho {
|
|
|
246
310
|
or send AUTH_FAIL when fail.
|
|
247
311
|
*/
|
|
248
312
|
|
|
249
|
-
|
|
313
|
+
/**
|
|
314
|
+
* Verifies client's AUTH_HMAC.
|
|
315
|
+
* @param {Buffer|object} data
|
|
316
|
+
* @returns {boolean}
|
|
317
|
+
*/
|
|
250
318
|
check_auth_hmac(data) {
|
|
251
319
|
let infoPack
|
|
252
320
|
if (data instanceof Uint8Array) {
|
|
253
321
|
infoPack = MBP.unpack(data, Meta.AUTH_HMAC)
|
|
254
322
|
if (!infoPack) {
|
|
255
|
-
// console.log('auth_hamc unpack fail.')
|
|
256
323
|
return
|
|
257
324
|
}
|
|
258
325
|
} else {
|
|
259
326
|
infoPack = data;
|
|
260
327
|
|
|
261
328
|
}
|
|
262
|
-
// console.log('auth_hamc infoObj', infoPack )
|
|
263
329
|
|
|
264
330
|
this.set_salt12(this.auth_salt12)
|
|
265
331
|
|
|
266
|
-
// hmac( key, sec,mil,serverNonce, clientNonce)
|
|
267
332
|
this.generateHMAC(infoPack.nonce)
|
|
268
|
-
// let hmac8 = this._hmac.subarray(0, 8)
|
|
269
333
|
let hmac32 = this._hmac
|
|
270
334
|
|
|
271
335
|
if (MBP.equal(infoPack.hmac32, hmac32)) {
|
|
272
|
-
//Auth success then store client nonce.
|
|
273
336
|
this.remoteNonce = infoPack.nonce
|
|
274
337
|
|
|
275
338
|
let salt12 = Buffer.concat([
|
|
@@ -291,12 +354,12 @@ export class Boho {
|
|
|
291
354
|
return false
|
|
292
355
|
}
|
|
293
356
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
357
|
+
/**
|
|
358
|
+
* Verifies server's AUTH_ACK HMAC.
|
|
359
|
+
* @param {Buffer} buffer
|
|
360
|
+
* @returns {boolean}
|
|
361
|
+
*/
|
|
298
362
|
check_auth_ack_hmac(buffer) {
|
|
299
|
-
// server response has hmac ( key + clientNonce)
|
|
300
363
|
let auth_ack = MBP.unpack(buffer, Meta.AUTH_ACK)
|
|
301
364
|
if (auth_ack) {
|
|
302
365
|
let salt12 = Buffer.concat([
|
|
@@ -306,22 +369,24 @@ export class Boho {
|
|
|
306
369
|
])
|
|
307
370
|
this.set_salt12(salt12)
|
|
308
371
|
this.generateHMAC(this.localNonce)
|
|
309
|
-
// let hmac8 = this._hmac.subarray(0, 8)
|
|
310
372
|
let hmac32 = this._hmac
|
|
311
|
-
//server side hmac using client nonce.
|
|
312
373
|
if (MBP.equal(hmac32, auth_ack.hmac32)) {
|
|
313
374
|
this.isAuthorized = true
|
|
314
375
|
return true
|
|
315
376
|
}
|
|
316
377
|
}
|
|
317
|
-
// server hmac error
|
|
318
378
|
return
|
|
319
379
|
}
|
|
320
380
|
|
|
321
381
|
// C. Secure Communication
|
|
322
382
|
|
|
323
383
|
// Must AUTH first.
|
|
324
|
-
|
|
384
|
+
/**
|
|
385
|
+
* Generates encrypted 488 packet after authentication
|
|
386
|
+
* @param {Buffer} data
|
|
387
|
+
* @returns {Buffer|undefined}
|
|
388
|
+
*/
|
|
389
|
+
encrypt_488(data) {
|
|
325
390
|
if (!this.isAuthorized) return
|
|
326
391
|
|
|
327
392
|
data = MBP.B8(data)
|
|
@@ -339,11 +404,14 @@ export class Boho {
|
|
|
339
404
|
MBP.MB('#hmac8', hmac8),
|
|
340
405
|
MBP.MB('#xdata', encData)
|
|
341
406
|
)
|
|
342
|
-
// console.log('enc pack result', pack )
|
|
343
407
|
return pack
|
|
344
408
|
}
|
|
345
409
|
|
|
346
|
-
|
|
410
|
+
/**
|
|
411
|
+
* Decrypts 488 packet after authentication
|
|
412
|
+
* @param {Buffer} data
|
|
413
|
+
* @returns {Buffer|undefined}
|
|
414
|
+
*/
|
|
347
415
|
decrypt_488(data) {
|
|
348
416
|
data = MBP.B8(data)
|
|
349
417
|
|
|
@@ -366,14 +434,14 @@ export class Boho {
|
|
|
366
434
|
|
|
367
435
|
if (MBP.equal(hmac8, pack.hmac8)) return decData
|
|
368
436
|
|
|
369
|
-
// console.log('hmac dismatch', decData )
|
|
370
|
-
} else {
|
|
371
|
-
// console.log('unpack fail')
|
|
372
437
|
}
|
|
373
438
|
}
|
|
374
439
|
|
|
375
|
-
|
|
376
|
-
|
|
440
|
+
/**
|
|
441
|
+
* Generates encrypted packet for up to 2^32-1 bytes
|
|
442
|
+
* @param {Buffer} data
|
|
443
|
+
* @returns {Buffer}
|
|
444
|
+
*/
|
|
377
445
|
encryptPack(data) {
|
|
378
446
|
data = MBP.B8(data)
|
|
379
447
|
|
|
@@ -393,24 +461,24 @@ export class Boho {
|
|
|
393
461
|
return pack
|
|
394
462
|
}
|
|
395
463
|
|
|
396
|
-
|
|
464
|
+
/**
|
|
465
|
+
* Decrypts encrypted packet
|
|
466
|
+
* @param {Buffer} data
|
|
467
|
+
* @returns {Buffer}
|
|
468
|
+
*/
|
|
397
469
|
decryptPack(data) {
|
|
398
470
|
|
|
399
471
|
if (data[0] !== BohoMsg.ENC_PACK) {
|
|
400
|
-
// console.log('Boho: Invalid packType')
|
|
401
472
|
return
|
|
402
473
|
}
|
|
403
474
|
|
|
404
|
-
// packLength
|
|
405
475
|
let readPackLen = data.readUint32LE(1);
|
|
406
476
|
if (readPackLen != data.byteLength - MetaSize.ENC_PACK) {
|
|
407
|
-
// console.log('Boho: Invalid LEN data_len: data.byteLen' , readPackLen, data.byteLength)
|
|
408
477
|
return
|
|
409
478
|
}
|
|
410
479
|
|
|
411
480
|
try {
|
|
412
481
|
let pack = MBP.unpack(data, Meta.ENC_PACK)
|
|
413
|
-
// console.log('unpack result', pack )
|
|
414
482
|
if (!pack) return
|
|
415
483
|
|
|
416
484
|
this.set_salt12(pack.salt12)
|
|
@@ -424,14 +492,18 @@ export class Boho {
|
|
|
424
492
|
pack.data = decData
|
|
425
493
|
return pack
|
|
426
494
|
}
|
|
427
|
-
// console.log('Invalid HMAC', pack.hmac, hmac8 )
|
|
428
495
|
|
|
429
496
|
} catch (error) {
|
|
430
|
-
// console.log('Boho: unpack err', error )
|
|
431
497
|
|
|
432
498
|
}
|
|
433
499
|
}
|
|
434
500
|
|
|
501
|
+
/**
|
|
502
|
+
* End-to-end encryption
|
|
503
|
+
* @param {Buffer} data
|
|
504
|
+
* @param {Buffer} key
|
|
505
|
+
* @returns {Buffer}
|
|
506
|
+
*/
|
|
435
507
|
encrypt_e2e(data, key) {
|
|
436
508
|
let baseKey = Buffer.alloc(32)
|
|
437
509
|
baseKey.set(this._otpSrc44.subarray(0, 32))
|
|
@@ -441,6 +513,12 @@ export class Boho {
|
|
|
441
513
|
return pack;
|
|
442
514
|
}
|
|
443
515
|
|
|
516
|
+
/**
|
|
517
|
+
* End-to-end decryption
|
|
518
|
+
* @param {Buffer} data
|
|
519
|
+
* @param {Buffer} key
|
|
520
|
+
* @returns {Buffer}
|
|
521
|
+
*/
|
|
444
522
|
decrypt_e2e(data, key) {
|
|
445
523
|
let baseKey = Buffer.alloc(32)
|
|
446
524
|
baseKey.set(this._otpSrc44.subarray(0, 32))
|
package/src/constants.js
CHANGED
|
@@ -1,21 +1,53 @@
|
|
|
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
|
+
/**
|
|
12
|
+
* Boho message type constants
|
|
13
|
+
* @typedef {Object} BohoMsg
|
|
14
|
+
* @property {number} AUTH_REQ
|
|
15
|
+
* @property {number} AUTH_NONCE
|
|
16
|
+
* @property {number} AUTH_HMAC
|
|
17
|
+
* @property {number} AUTH_ACK
|
|
18
|
+
* @property {number} AUTH_FAIL
|
|
19
|
+
* @property {number} AUTH_EXT
|
|
20
|
+
* @property {number} ENC_PACK
|
|
21
|
+
* @property {number} ENC_E2E
|
|
22
|
+
* @property {number} ENC_488
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
// One-byte header for remote control message pack.
|
|
5
26
|
export let BohoMsg = {
|
|
6
|
-
AUTH_REQ : 0xB0,
|
|
27
|
+
AUTH_REQ : 0xB0,
|
|
7
28
|
AUTH_NONCE: 0xB1,
|
|
8
29
|
AUTH_HMAC: 0xB2,
|
|
9
30
|
AUTH_ACK: 0xB3,
|
|
10
31
|
AUTH_FAIL: 0xB4,
|
|
11
32
|
AUTH_EXT: 0xB5,
|
|
12
|
-
ENC_PACK : 0xB6,
|
|
13
|
-
ENC_E2E : 0xB7,
|
|
33
|
+
ENC_PACK : 0xB6,
|
|
34
|
+
ENC_E2E : 0xB7,
|
|
14
35
|
ENC_488 : 0xB8
|
|
15
36
|
}
|
|
16
37
|
|
|
17
38
|
for (let c in BohoMsg) { BohoMsg[BohoMsg[c]] = c }
|
|
18
39
|
|
|
40
|
+
/**
|
|
41
|
+
* Boho message meta information
|
|
42
|
+
* @typedef {Object} Meta
|
|
43
|
+
* @property {any} AUTH_REQ
|
|
44
|
+
* @property {any} AUTH_NONCE
|
|
45
|
+
* @property {any} AUTH_HMAC
|
|
46
|
+
* @property {any} AUTH_ACK
|
|
47
|
+
* @property {any} ENC_PACK
|
|
48
|
+
* @property {any} ENC_488
|
|
49
|
+
*/
|
|
50
|
+
|
|
19
51
|
export const Meta = {
|
|
20
52
|
|
|
21
53
|
AUTH_REQ: MBP.meta( // 2
|
|
@@ -36,16 +68,16 @@ export const Meta = {
|
|
|
36
68
|
MBP.MB('nonce', Buffer.alloc(4)),
|
|
37
69
|
MBP.MB('hmac32', Buffer.alloc(32))
|
|
38
70
|
),
|
|
39
|
-
|
|
71
|
+
|
|
40
72
|
AUTH_ACK: MBP.meta( // 33
|
|
41
73
|
MBP.MB('header','8', 0),
|
|
42
74
|
MBP.MB('hmac32', Buffer.alloc(32))
|
|
43
75
|
),
|
|
44
|
-
|
|
76
|
+
|
|
45
77
|
|
|
46
78
|
ENC_PACK: MBP.meta( //25 + payload
|
|
47
79
|
MBP.MB('type','8',0),
|
|
48
|
-
MBP.MB('len','32L',0), // pure xdata size.
|
|
80
|
+
MBP.MB('len','32L',0), // pure xdata size.
|
|
49
81
|
MBP.MB('salt12', Buffer.alloc(12)), // sec,mil,rand
|
|
50
82
|
MBP.MB('hmac',8,0)
|
|
51
83
|
// MBP.MB( 'xdata', encData )
|
|
@@ -57,7 +89,7 @@ export const Meta = {
|
|
|
57
89
|
MBP.MB('len','32L', 0 ),
|
|
58
90
|
MBP.MB('otpSrc8', Buffer.alloc(8) ),
|
|
59
91
|
MBP.MB('hmac8', Buffer.alloc(8) )
|
|
60
|
-
// MBP.MB('xdata', encData )
|
|
92
|
+
// MBP.MB('xdata', encData )
|
|
61
93
|
)
|
|
62
94
|
|
|
63
95
|
|
|
@@ -69,6 +101,21 @@ export const Meta = {
|
|
|
69
101
|
return lastItem[2] + lastItem[3]
|
|
70
102
|
}
|
|
71
103
|
|
|
104
|
+
/**
|
|
105
|
+
* Byte size information for each meta
|
|
106
|
+
* @typedef {Object} MetaSize
|
|
107
|
+
* @property {number} AUTH_REQ
|
|
108
|
+
* @property {number} AUTH_NONCE
|
|
109
|
+
* @property {number} AUTH_HMAC
|
|
110
|
+
* @property {number} AUTH_ACK
|
|
111
|
+
* @property {number} ENC_PACK
|
|
112
|
+
* @property {number} ENC_488
|
|
113
|
+
*/
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Byte size information for each meta
|
|
117
|
+
* @type {MetaSize}
|
|
118
|
+
*/
|
|
72
119
|
export const MetaSize = {
|
|
73
120
|
AUTH_REQ: getMetaSize( Meta.AUTH_REQ ),
|
|
74
121
|
AUTH_NONCE: getMetaSize( Meta.AUTH_NONCE ),
|
package/src/index.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
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 { Boho, RAND, BohoMsg, Meta, MetaSize, sha256, MBP, Buffer } from './boho.js'
|
|
2
9
|
|
|
3
10
|
Boho.RAND = RAND;
|
|
@@ -8,4 +15,24 @@ Boho.sha256 = sha256;
|
|
|
8
15
|
Boho.MBP = MBP;
|
|
9
16
|
Boho.Buffer = Buffer;
|
|
10
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Main entry point of the boho library
|
|
20
|
+
* @module boho
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @typedef {import('./boho.js').Boho} Boho
|
|
25
|
+
* @typedef {import('./boho.js').RAND} RAND
|
|
26
|
+
* @typedef {import('./boho.js').BohoMsg} BohoMsg
|
|
27
|
+
* @typedef {import('./boho.js').Meta} Meta
|
|
28
|
+
* @typedef {import('./boho.js').MetaSize} MetaSize
|
|
29
|
+
* @typedef {import('./boho.js').sha256} sha256
|
|
30
|
+
* @typedef {import('./boho.js').MBP} MBP
|
|
31
|
+
* @typedef {import('./boho.js').Buffer} Buffer
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Default export object of boho
|
|
36
|
+
* @type {Boho & { RAND: typeof RAND, BohoMsg: typeof BohoMsg, Meta: typeof Meta, MetaSize: typeof MetaSize, sha256: typeof sha256, MBP: typeof MBP, Buffer: typeof Buffer }}
|
|
37
|
+
*/
|
|
11
38
|
export default Boho;
|
package/src/sha256-mbp.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
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 { hash, hmac } from '../lib/fast-sha256.js'
|
|
2
9
|
import MBP from 'meta-buffer-pack'
|
|
3
10
|
|
|
@@ -10,20 +17,45 @@ Tip.
|
|
|
10
17
|
MBP.B8( any ) reutn Buffer instance
|
|
11
18
|
|
|
12
19
|
*/
|
|
20
|
+
/**
|
|
21
|
+
* sha256 및 hmac 유틸리티 함수 모음
|
|
22
|
+
* @namespace sha256
|
|
23
|
+
*/
|
|
13
24
|
const sha256 = {};
|
|
14
25
|
|
|
26
|
+
/**
|
|
27
|
+
* sha256 해시를 반환합니다.
|
|
28
|
+
* @param {any} data
|
|
29
|
+
* @returns {Uint8Array}
|
|
30
|
+
*/
|
|
15
31
|
sha256.hash = function (data) {
|
|
16
32
|
return hash(MBP.U8(data))
|
|
17
33
|
}
|
|
18
34
|
|
|
35
|
+
/**
|
|
36
|
+
* sha256 해시의 hex 문자열을 반환합니다.
|
|
37
|
+
* @param {any} data
|
|
38
|
+
* @returns {string}
|
|
39
|
+
*/
|
|
19
40
|
sha256.hex = function (data) {
|
|
20
41
|
return MBP.B8( hash(MBP.U8(data)) ).toString('hex')
|
|
21
42
|
}
|
|
22
43
|
|
|
44
|
+
/**
|
|
45
|
+
* sha256 해시의 base64 문자열을 반환합니다.
|
|
46
|
+
* @param {any} data
|
|
47
|
+
* @returns {string}
|
|
48
|
+
*/
|
|
23
49
|
sha256.base64= function (data) {
|
|
24
50
|
return MBP.B8( hash(MBP.U8(data)) ).toString('base64')
|
|
25
51
|
}
|
|
26
52
|
|
|
53
|
+
/**
|
|
54
|
+
* hmac 값을 반환합니다.
|
|
55
|
+
* @param {any} key
|
|
56
|
+
* @param {any} data
|
|
57
|
+
* @returns {Uint8Array}
|
|
58
|
+
*/
|
|
27
59
|
sha256.hmac = function (key, data) {
|
|
28
60
|
return hmac(MBP.U8(key), MBP.U8(data))
|
|
29
61
|
}
|