boho 0.2.0 → 0.5.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/src/boho.js ADDED
@@ -0,0 +1,457 @@
1
+ import { sha256 } from './sha256-mbp.js'
2
+ import { MBP, Buffer } from 'meta-buffer-pack'
3
+ export { sha256, MBP, Buffer }
4
+
5
+ import { webcrypto } from 'crypto'
6
+ import { BohoMsg, Meta, MetaSize } from "./constants.js"
7
+ export { BohoMsg, Meta, MetaSize }
8
+
9
+ let isNode = false
10
+ try {
11
+ isNode = Object.prototype.toString.call(global.process) === '[object process]'
12
+ } catch (e) { }
13
+
14
+
15
+ export function RAND(size) {
16
+ if( isNode ){
17
+ return webcrypto.getRandomValues(Buffer.alloc(size))
18
+ }else{
19
+ return self.crypto.getRandomValues(Buffer.alloc(size))
20
+ }
21
+ }
22
+
23
+
24
+
25
+ export class Boho {
26
+ // A. Core
27
+ constructor() {
28
+
29
+ this._id8 = Buffer.alloc(8)
30
+ this._otpSrc44 = Buffer.alloc(44)
31
+ this._otp36 = Buffer.alloc(36)
32
+ this._hmac = Buffer.alloc(32)
33
+
34
+ this.auth_salt12 = Buffer.alloc(12)
35
+ this.localNonce = Buffer.alloc(4)
36
+ this.remoteNonce = Buffer.alloc(4)
37
+ this.isAuthorized = false
38
+
39
+ }
40
+
41
+ clearAuth(){
42
+ this._id8.fill(0)
43
+ this._otpSrc44.fill(0)
44
+ this._otp36.fill(0)
45
+ this._hmac.fill(0)
46
+ this.auth_salt12.fill(0)
47
+ this.localNonce.fill(0)
48
+ this.remoteNonce.fill(0)
49
+ this.isAuthorized = false
50
+ }
51
+
52
+ // for the self
53
+ set_id8(data) {
54
+ let encStr = MBP.B8(data)
55
+ this._id8.fill(0)
56
+ encStr.copy(this._id8, 0, 0, 8)
57
+ }
58
+
59
+ set_hash_id8(data) {
60
+ let idSum = MBP.B8(sha256.hash(data))
61
+ idSum.copy(this._id8, 0, 0, 8)
62
+ }
63
+
64
+
65
+ set_key(data) {
66
+ let keySum = MBP.B8(sha256.hash(data))
67
+ keySum.copy(this._otpSrc44, 0, 0, 32)
68
+ }
69
+
70
+ // for the remote client.
71
+ copy_id8(data) {
72
+ data.copy(this._id8, 0, 0, 8)
73
+ }
74
+
75
+ // for the remote client.
76
+ copy_key(data) {
77
+ data.copy(this._otpSrc44, 0, 0, 32)
78
+ }
79
+
80
+
81
+ sha256_n(srcData, n) {
82
+ let hashSum = sha256.hash(srcData)
83
+ for (let i = 0; i < n; i++) hashSum = sha256.hash(hashSum)
84
+ return hashSum
85
+ }
86
+
87
+
88
+ // useful general encryption i.e. enc_pack
89
+ set_clock_rand() {
90
+
91
+ let milTime = Date.now()
92
+ let secTime = parseInt(milTime / 1000)
93
+ milTime = milTime % 0xffffffff
94
+ const salt12 = Buffer.concat([
95
+ MBP.NB('32L', secTime),
96
+ MBP.NB('32L', milTime),
97
+ RAND(4)
98
+ ])
99
+
100
+ salt12.copy(this._otpSrc44, 32)
101
+ }
102
+
103
+ // for secure communication sender.
104
+ set_clock_nonce(nonce) {
105
+ let milTime = Date.now()
106
+ let secTime = parseInt(milTime / 1000)
107
+ milTime = milTime % 0xffffffff
108
+ const salt12 = Buffer.concat([
109
+ MBP.NB('32L', secTime),
110
+ MBP.NB('32L', milTime),
111
+ nonce
112
+ ])
113
+
114
+ salt12.copy(this._otpSrc44, 32)
115
+ }
116
+
117
+
118
+ set_salt12(salt12) {
119
+ salt12.copy(this._otpSrc44, 32)
120
+ }
121
+
122
+ resetOTP() {
123
+ let otp32 = MBP.B8(sha256.hash(this._otpSrc44))
124
+ otp32.copy(this._otp36, 0, 0, 32)
125
+ }
126
+
127
+ getIndexOTP(otpIndex) {
128
+ this._otp36.writeUInt32LE(otpIndex, 32)
129
+ return sha256.hash(this._otp36)
130
+ }
131
+
132
+
133
+ generateHMAC(data) {
134
+ let hmacSrc = Buffer.concat([this._otpSrc44, data])
135
+ this._hmac = MBP.B8(sha256.hash(hmacSrc))
136
+ }
137
+
138
+ // return 8 bytes of hash
139
+ getHMAC8(data) {
140
+ let hmacSrc = Buffer.concat([this._otpSrc44, data])
141
+ this._hmac = MBP.B8(sha256.hash(hmacSrc))
142
+ return this._hmac.subarray(0, 8)
143
+ }
144
+
145
+ xotp(data, otpStartIndex = 0, shareDataBuffer = false) {
146
+
147
+ data = MBP.B8(data, shareDataBuffer)
148
+
149
+ let len = data.byteLength
150
+ let otpIndex = otpStartIndex
151
+ let dataOffset = 0
152
+ let xorCalcLen = 0
153
+
154
+ while (len > 0) {
155
+ xorCalcLen = len < 32 ? len : 32
156
+ let iotp = this.getIndexOTP(++otpIndex);
157
+ for (let i = 0; i < xorCalcLen; i++) {
158
+ data[dataOffset++] ^= iotp[i]
159
+ }
160
+ len -= 32
161
+ }
162
+ return data
163
+ }
164
+
165
+ // B. AUTH process
166
+
167
+ // step 1
168
+ // client send AUTH_REQ
169
+ auth_req() {
170
+ return MBP.pack(
171
+ MBP.MB('#type', '8', BohoMsg.AUTH_REQ),
172
+ MBP.MB('#reserved', '8', 0)
173
+ )
174
+ }
175
+
176
+ // step 2
177
+ // server send AUTH_NONCE
178
+ auth_nonce() {
179
+ let now = Date.now()
180
+ let unixTime = Math.floor(now / 1000)
181
+ let milTime = now % 1000
182
+ this.localNonce = RAND(4)
183
+ this.auth_salt12 = Buffer.concat([
184
+ MBP.NB('32L', unixTime),
185
+ MBP.NB('32L', milTime),
186
+ this.localNonce
187
+ ])
188
+
189
+ let infoPack = Buffer.concat([
190
+ MBP.NB('8', BohoMsg.AUTH_NONCE),
191
+ this.auth_salt12
192
+ ])
193
+ return infoPack
194
+ }
195
+
196
+
197
+ // step 3
198
+ // client send AUTH_HMAC
199
+ // input : auth_nonce buffer
200
+ auth_hmac(buffer) {
201
+ let auth_nonce = MBP.unpack(buffer, Meta.AUTH_NONCE)
202
+ if (auth_nonce) {
203
+ // console.log(' auth nonce', auth_nonce )
204
+
205
+ // let now = Date.now()
206
+ // let localUTC= Math.floor( now/ 1000 )
207
+ // let localMilTime = now % 1000
208
+
209
+ // console.log('time server [sec]', auth_nonce.unixTime, auth_nonce.milTime )
210
+ // console.log('time client [sec]', localUTC , localMilTime )
211
+ // console.log('time diff client and server[sec]', auth_nonce.unixTime - localUTC )
212
+
213
+ // let serverSecMil = auth_nonce.unixTime * 1000 + auth_nonce.milTime
214
+ // console.log('time diff msec client and server[msec]', serverSecMil - now )
215
+
216
+ let salt12 = Buffer.concat([
217
+ MBP.NB('32L', auth_nonce.unixTime),
218
+ MBP.NB('32L', auth_nonce.milTime),
219
+ auth_nonce.nonce
220
+ ])
221
+
222
+ this.set_salt12(salt12)
223
+
224
+ this.localNonce = RAND(4)
225
+ // hmac( key, sec,mil,serverNonce, localNonce)
226
+ this.generateHMAC(this.localNonce)
227
+
228
+ // let hmac8 = this._hmac.subarray(0, 8)
229
+
230
+ this.remoteNonce = auth_nonce.nonce
231
+
232
+ let auth_hmac_buffer = MBP.pack( // 21 -> 45
233
+ MBP.MB('#header', '8', BohoMsg.AUTH_HMAC),
234
+ MBP.MB('#id8', this._id8),
235
+ MBP.MB('#nonce', this.localNonce),
236
+ MBP.MB('#hmac32', this._hmac ), //full 32bytes hash
237
+ )
238
+
239
+ return auth_hmac_buffer
240
+ }
241
+ return false
242
+ }
243
+
244
+ /*
245
+ step 4. for server
246
+
247
+ step 4-1. check client's auth_hmac
248
+ step 4-2. reply result
249
+ send AUTH_ACK with another HMAC for client.
250
+ or send AUTH_FAIL when fail.
251
+ */
252
+
253
+ // input: unpack object or buffer of auth_hmac
254
+ check_auth_hmac(data) {
255
+ let infoPack
256
+ if (data instanceof Uint8Array) {
257
+ infoPack = MBP.unpack(data, Meta.AUTH_HMAC)
258
+ if (!infoPack) {
259
+ // console.log('auth_hamc unpack fail.')
260
+ return
261
+ }
262
+ } else {
263
+ infoPack = data;
264
+
265
+ }
266
+ // console.log('auth_hamc infoObj', infoPack )
267
+
268
+ this.set_salt12(this.auth_salt12)
269
+
270
+ // hmac( key, sec,mil,serverNonce, clientNonce)
271
+ this.generateHMAC(infoPack.nonce)
272
+ // let hmac8 = this._hmac.subarray(0, 8)
273
+ let hmac32 = this._hmac
274
+
275
+ if (MBP.equal(infoPack.hmac32, hmac32)) {
276
+ //Auth success then store client nonce.
277
+ this.remoteNonce = infoPack.nonce
278
+
279
+ let salt12 = Buffer.concat([
280
+ this.localNonce,
281
+ this.remoteNonce,
282
+ this.localNonce
283
+ ])
284
+ this.set_salt12(salt12)
285
+ this.generateHMAC(infoPack.nonce)
286
+ let replyHMAC = this._hmac
287
+
288
+ let auth_ack = MBP.rawPack(
289
+ MBP.MB('header', '8', BohoMsg.AUTH_ACK),
290
+ MBP.MB('hmac32', replyHMAC)
291
+ )
292
+ this.isAuthorized = true
293
+ return auth_ack
294
+ }
295
+ return false
296
+ }
297
+
298
+
299
+
300
+ // step 5. cross check
301
+ // client check server's hmac.
302
+ check_auth_ack_hmac(buffer) {
303
+ // server response has hmac ( key + clientNonce)
304
+ let auth_ack = MBP.unpack(buffer, Meta.AUTH_ACK)
305
+ if (auth_ack) {
306
+ let salt12 = Buffer.concat([
307
+ this.remoteNonce,
308
+ this.localNonce,
309
+ this.remoteNonce,
310
+ ])
311
+ this.set_salt12(salt12)
312
+ this.generateHMAC(this.localNonce)
313
+ // let hmac8 = this._hmac.subarray(0, 8)
314
+ let hmac32 = this._hmac
315
+ //server side hmac using client nonce.
316
+ if (MBP.equal(hmac32, auth_ack.hmac32)) {
317
+ this.isAuthorized = true
318
+ return true
319
+ }
320
+ }
321
+ // server hmac error
322
+ return
323
+ }
324
+
325
+ // C. Secure Communication
326
+
327
+ // Must AUTH first.
328
+ encrypt_488(data) { // payload max about 2^32 bytes.
329
+ if (!this.isAuthorized) return
330
+
331
+ data = MBP.B8(data)
332
+
333
+ this.set_clock_nonce(this.remoteNonce)
334
+ this.resetOTP()
335
+
336
+ let hmac8 = this.getHMAC8(data)
337
+ let encData = this.xotp(data)
338
+
339
+ let pack = MBP.pack(
340
+ MBP.MB('#type', '8', BohoMsg.ENC_488),
341
+ MBP.MB('#len', '32L', data.byteLength),
342
+ MBP.MB('#otpSrc8', this._otpSrc44.subarray(32, 40)),
343
+ MBP.MB('#hmac8', hmac8),
344
+ MBP.MB('#xdata', encData)
345
+ )
346
+ // console.log('enc pack result', pack )
347
+ return pack
348
+ }
349
+
350
+
351
+ decrypt_488(data) {
352
+ data = MBP.B8(data)
353
+
354
+ let pack = MBP.unpack(data, Meta.ENC_488)
355
+
356
+ if (pack) {
357
+
358
+ let salt12 = Buffer.concat([
359
+ pack.otpSrc8,
360
+ this.localNonce
361
+ ])
362
+
363
+ this.set_salt12(salt12)
364
+ this.resetOTP()
365
+
366
+ let xdata = pack.$OTHERS.subarray(0, pack.len)
367
+ let decData = this.xotp(xdata)
368
+
369
+ let hmac8 = this.getHMAC8(decData)
370
+
371
+ if (MBP.equal(hmac8, pack.hmac8)) return decData
372
+
373
+ // console.log('hmac dismatch', decData )
374
+ } else {
375
+ // console.log('unpack fail')
376
+ }
377
+ }
378
+
379
+
380
+ // maxium data size is 2**32 -1 bytes.
381
+ encryptPack(data) {
382
+ data = MBP.B8(data)
383
+
384
+ this.set_clock_rand()
385
+ this.resetOTP()
386
+
387
+ let hmac8 = this.getHMAC8(data)
388
+ let encData = this.xotp(data)
389
+
390
+ let pack = MBP.pack(
391
+ MBP.MB('#type', '8', BohoMsg.ENC_PACK),
392
+ MBP.MB('#len', '32L', data.byteLength),
393
+ MBP.MB('#salt12', this._otpSrc44.subarray(32)),
394
+ MBP.MB('#hmac8', hmac8),
395
+ MBP.MB('#xdata', encData)
396
+ )
397
+ return pack
398
+ }
399
+
400
+
401
+ decryptPack(data) {
402
+
403
+ if (data[0] !== BohoMsg.ENC_PACK) {
404
+ // console.log('Boho: Invalid packType')
405
+ return
406
+ }
407
+
408
+ // packLength
409
+ let readPackLen = data.readUint32LE(1);
410
+ if (readPackLen != data.byteLength - MetaSize.ENC_PACK) {
411
+ // console.log('Boho: Invalid LEN data_len: data.byteLen' , readPackLen, data.byteLength)
412
+ return
413
+ }
414
+
415
+ try {
416
+ let pack = MBP.unpack(data, Meta.ENC_PACK)
417
+ // console.log('unpack result', pack )
418
+ if (!pack) return
419
+
420
+ this.set_salt12(pack.salt12)
421
+ this.resetOTP()
422
+
423
+ let xdata = pack.$OTHERS
424
+ let decData = this.xotp(xdata)
425
+ let hmac8 = this.getHMAC8(decData)
426
+
427
+ if (MBP.equal(pack.hmac, hmac8)) {
428
+ pack.data = decData
429
+ return pack
430
+ }
431
+ // console.log('Invalid HMAC', pack.hmac, hmac8 )
432
+
433
+ } catch (error) {
434
+ // console.log('Boho: unpack err', error )
435
+
436
+ }
437
+ }
438
+
439
+ encrypt_e2e(data, key) {
440
+ let baseKey = Buffer.alloc(32)
441
+ baseKey.set(this._otpSrc44.subarray(0, 32))
442
+ this.set_key(key)
443
+ let pack = this.encryptPack(data)
444
+ this._otpSrc44.set(baseKey)
445
+ return pack;
446
+ }
447
+
448
+ decrypt_e2e(data, key) {
449
+ let baseKey = Buffer.alloc(32)
450
+ baseKey.set(this._otpSrc44.subarray(0, 32))
451
+ this.set_key(key)
452
+ let decPack = this.decryptPack(data)
453
+ this._otpSrc44.set(baseKey)
454
+ return decPack
455
+ }
456
+
457
+ }
@@ -0,0 +1,11 @@
1
+ import { Boho, RAND, MBP, BohoMsg, Meta, MetaSize , sha256, Buffer } from './boho.js'
2
+
3
+ Boho.RAND = RAND;
4
+ Boho.BohoMsg = BohoMsg;
5
+ Boho.Meta = Meta;
6
+ Boho.MetaSize = MetaSize;
7
+ Boho.sha256 = sha256;
8
+ Boho.MBP = MBP;
9
+ Boho.Buffer = Buffer;
10
+
11
+ export default Boho;
@@ -0,0 +1,90 @@
1
+ import{ MBP, Buffer} from "meta-buffer-pack"
2
+ const MB = MBP.MB
3
+
4
+
5
+ // remocon message pack one byte header.
6
+ export let BohoMsg = {
7
+ AUTH_REQ : 0xB0,
8
+ AUTH_NONCE: 0xB1,
9
+ AUTH_HMAC: 0xB2,
10
+ AUTH_ACK: 0xB3,
11
+ AUTH_FAIL: 0xB4,
12
+ AUTH_EXT: 0xB5,
13
+ ENC_PACK : 0xB6,
14
+ ENC_E2E : 0xB7,
15
+ ENC_488 : 0xB8
16
+ }
17
+
18
+ for (let c in BohoMsg) { BohoMsg[BohoMsg[c]] = c }
19
+
20
+ export const Meta = {
21
+
22
+ AUTH_REQ: MBP.meta( // 2
23
+ MB('header','8', 0),
24
+ MB('reserved','8', 0)
25
+ ),
26
+
27
+ AUTH_NONCE: MBP.meta( // 13
28
+ MB('header','8', 0),
29
+ MB('unixTime','32L', 0),
30
+ MB('milTime','32L', 0 ),
31
+ MB('nonce', Buffer.alloc(4))
32
+ ),
33
+
34
+ AUTH_HMAC: MBP.meta( // 45
35
+ MB('header','8', 0),
36
+ MB('id8',Buffer.alloc(8)),
37
+ MB('nonce', Buffer.alloc(4)),
38
+ MB('hmac32', Buffer.alloc(32))
39
+ ),
40
+
41
+ AUTH_ACK: MBP.meta( // 33
42
+ MB('header','8', 0),
43
+ MB('hmac32', Buffer.alloc(32))
44
+ ),
45
+
46
+
47
+ ENC_PACK: MBP.meta( //25 + payload
48
+ MB('type','8',0),
49
+ MB('len','32L',0), // pure xdata size.
50
+ MB('salt12', Buffer.alloc(12)), // sec,mil,rand
51
+ MB('hmac',8,0)
52
+ // MB( 'xdata', encData )
53
+ ),
54
+
55
+
56
+ ENC_488: MBP.meta( // 21 + payload
57
+ MB('type','8', 0 ),
58
+ MB('len','32L', 0 ),
59
+ MB('otpSrc8', Buffer.alloc(8) ),
60
+ MB('hmac8', Buffer.alloc(8) )
61
+ // MB('xdata', encData )
62
+ )
63
+
64
+
65
+ }
66
+
67
+
68
+ function getMetaSize(meta){
69
+ let lastItem = meta[ meta.length - 1]
70
+ return lastItem[2] + lastItem[3]
71
+ }
72
+
73
+ export const MetaSize = {
74
+ AUTH_REQ: getMetaSize( Meta.AUTH_REQ ),
75
+ AUTH_NONCE: getMetaSize( Meta.AUTH_NONCE ),
76
+ AUTH_HMAC: getMetaSize( Meta.AUTH_HMAC ),
77
+ AUTH_ACK: getMetaSize( Meta.AUTH_ACK ),
78
+ ENC_PACK: getMetaSize( Meta.ENC_PACK ),
79
+ ENC_488: getMetaSize( Meta.ENC_488 )
80
+ }
81
+
82
+ // console.log( 'boho MetaSize', MetaSize )
83
+ // boho MetaSize {
84
+ // AUTH_REQ: 2,
85
+ // AUTH_NONCE: 13,
86
+ // AUTH_HMAC: 45,
87
+ // AUTH_ACK: 33,
88
+ // ENC_PACK: 25,
89
+ // ENC_488: 21
90
+ // }
@@ -0,0 +1,16 @@
1
+ import { hash as sha256, hmac } from '../lib/fast-sha256.js'
2
+ import { MBP } from 'meta-buffer-pack'
3
+
4
+ sha256.hash = function (data) {
5
+ return sha256(MBP.U8(data))
6
+ }
7
+
8
+ sha256.hex = function (data) {
9
+ return MBP.hex(sha256.hash(data))
10
+ }
11
+
12
+ sha256.hmac = function (key, data) {
13
+ return hmac(MBP.U8(key), MBP.U8(data))
14
+ }
15
+
16
+ export { sha256 }