boho 1.1.1 → 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/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "boho",
3
- "version": "1.1.1",
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": "./dist/boho.cjs"
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
@@ -11,7 +11,7 @@ export { MBP }
11
11
  import { BohoMsg, Meta, MetaSize } from './constants.js'
12
12
  export { BohoMsg, Meta, MetaSize, sha256 }
13
13
  import { Buffer } from 'buffer/index.js'
14
- export { Buffer}
14
+ export { Buffer }
15
15
 
16
16
  /**
17
17
  * Generates a random byte buffer.
@@ -40,13 +40,13 @@ export class Boho {
40
40
  this.localNonce = Buffer.alloc(4)
41
41
  this.remoteNonce = Buffer.alloc(4)
42
42
  this.isAuthorized = false
43
-
43
+ this.counter = 0;
44
44
  }
45
45
 
46
46
  /**
47
47
  * Initializes authentication state.
48
48
  */
49
- clearAuth(){
49
+ clearAuth() {
50
50
  this._id8.fill(0)
51
51
  this._otpSrc44.fill(0)
52
52
  this._otp36.fill(0)
@@ -55,6 +55,7 @@ export class Boho {
55
55
  this.localNonce.fill(0)
56
56
  this.remoteNonce.fill(0)
57
57
  this.isAuthorized = false
58
+ this.counter = 0;
58
59
  }
59
60
 
60
61
  /**
@@ -91,7 +92,7 @@ export class Boho {
91
92
  */
92
93
  set_id_key(id_key) {
93
94
  let delimiterPosition = id_key.indexOf('.')
94
- if( delimiterPosition == -1 ) return
95
+ if (delimiterPosition == -1) return
95
96
  let id = id_key.substring(0, delimiterPosition)
96
97
  let key = id_key.substring(delimiterPosition + 1)
97
98
  this.set_id8(id)
@@ -126,20 +127,20 @@ export class Boho {
126
127
  return hashSum
127
128
  }
128
129
 
130
+
129
131
  /**
130
132
  * Sets random clock value (salt12) in otpSrc44.
131
133
  */
132
134
  set_clock_rand() {
133
-
134
- let milTime = Date.now()
135
- let secTime = parseInt(milTime / 1000)
136
- milTime = milTime % 0xffffffff
135
+ const now = Date.now()
136
+ const secTime = parseInt(now / 1000)
137
+ const milTime = now % 1000
137
138
  const salt12 = Buffer.concat([
138
- MBP.NB('32L', secTime),
139
- MBP.NB('32L', milTime),
140
- RAND(4)
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()
141
143
  ])
142
-
143
144
  salt12.copy(this._otpSrc44, 32)
144
145
  }
145
146
 
@@ -148,15 +149,15 @@ export class Boho {
148
149
  * @param {Buffer} nonce
149
150
  */
150
151
  set_clock_nonce(nonce) {
151
- let milTime = Date.now()
152
- let secTime = parseInt(milTime / 1000)
153
- milTime = milTime % 0xffffffff
152
+ const now = Date.now()
153
+ const secTime = parseInt(now / 1000)
154
+ const milTime = now % 1000
154
155
  const salt12 = Buffer.concat([
155
- MBP.NB('32L', secTime),
156
- MBP.NB('32L', milTime),
156
+ MBP.NB('32L', secTime),
157
+ MBP.NB('16L', milTime),
158
+ MBP.NB('16L', ++this.counter),
157
159
  nonce
158
160
  ])
159
-
160
161
  salt12.copy(this._otpSrc44, 32)
161
162
  }
162
163
 
@@ -164,7 +165,11 @@ export class Boho {
164
165
  * Sets salt12 value in otpSrc44.
165
166
  * @param {Buffer} salt12
166
167
  */
167
- 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
+ }
168
173
  salt12.copy(this._otpSrc44, 32)
169
174
  }
170
175
 
@@ -236,64 +241,57 @@ export class Boho {
236
241
  // B. AUTH process
237
242
 
238
243
  /**
239
- * Generates AUTH_REQ message
244
+ * Generates server time and nonce signal
240
245
  * @returns {Buffer}
241
246
  */
242
- auth_req() {
243
- return MBP.pack(
244
- MBP.MB('#type', '8', BohoMsg.AUTH_REQ),
245
- MBP.MB('#reserved', '8', 0)
246
- )
247
- }
247
+ server_time_nonce() {
248
+ const now = Date.now()
249
+ const unixTime = Math.floor(now / 1000)
250
+ const milliseconds = now % 1000;
248
251
 
249
- /**
250
- * Generates AUTH_NONCE message
251
- * @returns {Buffer}
252
- */
253
- auth_nonce() {
254
- let now = Date.now()
255
- let unixTime = Math.floor(now / 1000)
256
- let milTime = now % 1000
257
252
  this.localNonce = RAND(4)
253
+ //keep
258
254
  this.auth_salt12 = Buffer.concat([
259
- MBP.NB('32L', unixTime),
260
- MBP.NB('32L', milTime),
255
+ MBP.NB('32L', unixTime),
256
+ MBP.NB('16L', milliseconds),
257
+ RAND(2), // used for client's counter IV.
261
258
  this.localNonce
262
259
  ])
263
260
 
264
261
  let infoPack = Buffer.concat([
265
- MBP.NB('8', BohoMsg.AUTH_NONCE),
262
+ MBP.NB('8', BohoMsg.SERVER_TIME_NONCE),
266
263
  this.auth_salt12
267
264
  ])
268
265
  return infoPack
269
266
  }
270
267
 
271
268
  /**
272
- * Generates AUTH_HMAC message
273
- * @param {Buffer} buffer
269
+ * Generates AUTH_REQ message
270
+ * @param {Buffer} buffer server's time nonce
274
271
  * @returns {Buffer|boolean}
275
272
  */
276
- auth_hmac(buffer) {
277
- let auth_nonce = MBP.unpack(buffer, Meta.AUTH_NONCE)
278
- if (auth_nonce) {
273
+ auth_req(buffer) {
274
+ let server_time_nonce = MBP.unpack(buffer, Meta.SERVER_TIME_NONCE)
275
+ if (server_time_nonce) {
279
276
  let salt12 = Buffer.concat([
280
- MBP.NB('32L', auth_nonce.unixTime),
281
- MBP.NB('32L', auth_nonce.milTime),
282
- auth_nonce.nonce
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
283
281
  ])
284
282
 
285
- this.set_salt12(salt12)
283
+ this.set_salt12(salt12, 'auth_req')
286
284
 
287
285
  this.localNonce = RAND(4)
288
286
  this.generateHMAC(this.localNonce)
289
287
 
290
- this.remoteNonce = auth_nonce.nonce
288
+ this.remoteNonce = server_time_nonce.nonce
291
289
 
292
290
  let auth_hmac_buffer = MBP.pack(
293
- MBP.MB('#header', '8', BohoMsg.AUTH_HMAC),
294
- MBP.MB('#id8', this._id8),
295
- MBP.MB('#nonce', this.localNonce),
296
- MBP.MB('#hmac32', this._hmac ),
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),
297
295
  )
298
296
 
299
297
  return auth_hmac_buffer
@@ -303,31 +301,26 @@ export class Boho {
303
301
 
304
302
  /*
305
303
  step 4. for server
306
-
307
- step 4-1. check client's auth_hmac
308
- step 4-2. reply result
309
- send AUTH_ACK with another HMAC for client.
310
- or send AUTH_FAIL when fail.
311
304
  */
312
305
 
313
306
  /**
314
- * Verifies client's AUTH_HMAC.
307
+ * Verify client's AUTH_REQ.
315
308
  * @param {Buffer|object} data
316
309
  * @returns {boolean}
317
310
  */
318
- check_auth_hmac(data) {
311
+ verify_auth_req(data) {
312
+
319
313
  let infoPack
320
314
  if (data instanceof Uint8Array) {
321
- infoPack = MBP.unpack(data, Meta.AUTH_HMAC)
315
+ infoPack = MBP.unpack(data, Meta.AUTH_REQ)
322
316
  if (!infoPack) {
323
317
  return
324
318
  }
325
319
  } else {
326
320
  infoPack = data;
327
-
328
321
  }
329
322
 
330
- this.set_salt12(this.auth_salt12)
323
+ this.set_salt12(this.auth_salt12, 'verify_auth_req 1/2')
331
324
 
332
325
  this.generateHMAC(infoPack.nonce)
333
326
  let hmac32 = this._hmac
@@ -340,37 +333,38 @@ export class Boho {
340
333
  this.remoteNonce,
341
334
  this.localNonce
342
335
  ])
343
- this.set_salt12(salt12)
336
+
337
+ this.set_salt12(salt12, 'verify_auth_req 2/2')
344
338
  this.generateHMAC(infoPack.nonce)
345
339
  let replyHMAC = this._hmac
346
340
 
347
- let auth_ack = MBP.rawPack(
348
- MBP.MB('header', '8', BohoMsg.AUTH_ACK),
349
- MBP.MB('hmac32', replyHMAC)
341
+ let auth_res = MBP.rawPack(
342
+ MBP.MB('header', '8', BohoMsg.AUTH_RES),
343
+ MBP.MB('hmac32', replyHMAC)
350
344
  )
351
345
  this.isAuthorized = true
352
- return auth_ack
346
+ return auth_res
353
347
  }
354
348
  return false
355
349
  }
356
350
 
357
351
  /**
358
- * Verifies server's AUTH_ACK HMAC.
352
+ * Verifies server's AUTH_RES HMAC.
359
353
  * @param {Buffer} buffer
360
354
  * @returns {boolean}
361
355
  */
362
- check_auth_ack_hmac(buffer) {
363
- let auth_ack = MBP.unpack(buffer, Meta.AUTH_ACK)
364
- if (auth_ack) {
356
+ verify_auth_res(buffer) {
357
+ let auth_res = MBP.unpack(buffer, Meta.AUTH_RES)
358
+ if (auth_res) {
365
359
  let salt12 = Buffer.concat([
366
360
  this.remoteNonce,
367
361
  this.localNonce,
368
362
  this.remoteNonce,
369
363
  ])
370
- this.set_salt12(salt12)
364
+ this.set_salt12(salt12, 'verify_auth_res')
371
365
  this.generateHMAC(this.localNonce)
372
366
  let hmac32 = this._hmac
373
- if (MBP.equal(hmac32, auth_ack.hmac32)) {
367
+ if (MBP.equal(hmac32, auth_res.hmac32)) {
374
368
  this.isAuthorized = true
375
369
  return true
376
370
  }
@@ -424,7 +418,7 @@ export class Boho {
424
418
  this.localNonce
425
419
  ])
426
420
 
427
- this.set_salt12(salt12)
421
+ this.set_salt12(salt12 , 'decrypt_488')
428
422
  this.resetOTP()
429
423
 
430
424
  let xdata = pack.$OTHERS.subarray(0, pack.len)
@@ -481,7 +475,7 @@ export class Boho {
481
475
  let pack = MBP.unpack(data, Meta.ENC_PACK)
482
476
  if (!pack) return
483
477
 
484
- this.set_salt12(pack.salt12)
478
+ this.set_salt12(pack.salt12, 'decryptPack')
485
479
  this.resetOTP()
486
480
 
487
481
  let xdata = pack.$OTHERS
package/src/constants.js CHANGED
@@ -11,12 +11,10 @@ import { Buffer } from 'buffer/index.js'
11
11
  /**
12
12
  * Boho message type constants
13
13
  * @typedef {Object} BohoMsg
14
+ * @property {number} SERVER_TIME_NONCE
14
15
  * @property {number} AUTH_REQ
15
- * @property {number} AUTH_NONCE
16
- * @property {number} AUTH_HMAC
17
- * @property {number} AUTH_ACK
16
+ * @property {number} AUTH_RES
18
17
  * @property {number} AUTH_FAIL
19
- * @property {number} AUTH_EXT
20
18
  * @property {number} ENC_PACK
21
19
  * @property {number} ENC_E2E
22
20
  * @property {number} ENC_488
@@ -24,12 +22,11 @@ import { Buffer } from 'buffer/index.js'
24
22
 
25
23
  // One-byte header for remote control message pack.
26
24
  export let BohoMsg = {
27
- AUTH_REQ : 0xB0,
28
- AUTH_NONCE: 0xB1,
29
- AUTH_HMAC: 0xB2,
30
- AUTH_ACK: 0xB3,
31
- AUTH_FAIL: 0xB4,
32
- AUTH_EXT: 0xB5,
25
+ SERVER_TIME_NONCE : 0xB0,
26
+ AUTH_REQ: 0xB1,
27
+ AUTH_RES: 0xB2,
28
+ AUTH_FAIL: 0xB3,
29
+
33
30
  ENC_PACK : 0xB6,
34
31
  ENC_E2E : 0xB7,
35
32
  ENC_488 : 0xB8
@@ -40,36 +37,31 @@ for (let c in BohoMsg) { BohoMsg[BohoMsg[c]] = c }
40
37
  /**
41
38
  * Boho message meta information
42
39
  * @typedef {Object} Meta
40
+ * @property {any} SERVER_TIME_NONCE
43
41
  * @property {any} AUTH_REQ
44
- * @property {any} AUTH_NONCE
45
- * @property {any} AUTH_HMAC
46
- * @property {any} AUTH_ACK
42
+ * @property {any} AUTH_RES
47
43
  * @property {any} ENC_PACK
48
44
  * @property {any} ENC_488
49
45
  */
50
46
 
51
47
  export const Meta = {
52
48
 
53
- AUTH_REQ: MBP.meta( // 2
54
- MBP.MB('header','8', 0),
55
- MBP.MB('reserved','8', 0)
56
- ),
57
-
58
- AUTH_NONCE: MBP.meta( // 13
49
+ SERVER_TIME_NONCE: MBP.meta( // 13
59
50
  MBP.MB('header','8', 0),
60
51
  MBP.MB('unixTime','32L', 0),
61
- MBP.MB('milTime','32L', 0 ),
52
+ MBP.MB('milTime','16L', 0 ),
53
+ MBP.MB('counter','16L', 0 ),
62
54
  MBP.MB('nonce', Buffer.alloc(4))
63
55
  ),
64
56
 
65
- AUTH_HMAC: MBP.meta( // 45
57
+ AUTH_REQ: MBP.meta( // 45
66
58
  MBP.MB('header','8', 0),
67
59
  MBP.MB('id8',Buffer.alloc(8)),
68
60
  MBP.MB('nonce', Buffer.alloc(4)),
69
61
  MBP.MB('hmac32', Buffer.alloc(32))
70
62
  ),
71
63
 
72
- AUTH_ACK: MBP.meta( // 33
64
+ AUTH_RES: MBP.meta( // 33
73
65
  MBP.MB('header','8', 0),
74
66
  MBP.MB('hmac32', Buffer.alloc(32))
75
67
  ),
@@ -105,9 +97,8 @@ export const Meta = {
105
97
  * Byte size information for each meta
106
98
  * @typedef {Object} MetaSize
107
99
  * @property {number} AUTH_REQ
108
- * @property {number} AUTH_NONCE
109
- * @property {number} AUTH_HMAC
110
- * @property {number} AUTH_ACK
100
+ * @property {number} SERVER_TIME_NONCE
101
+ * @property {number} AUTH_RES
111
102
  * @property {number} ENC_PACK
112
103
  * @property {number} ENC_488
113
104
  */
@@ -117,20 +108,18 @@ export const Meta = {
117
108
  * @type {MetaSize}
118
109
  */
119
110
  export const MetaSize = {
111
+ SERVER_TIME_NONCE: getMetaSize( Meta.SERVER_TIME_NONCE ),
120
112
  AUTH_REQ: getMetaSize( Meta.AUTH_REQ ),
121
- AUTH_NONCE: getMetaSize( Meta.AUTH_NONCE ),
122
- AUTH_HMAC: getMetaSize( Meta.AUTH_HMAC ),
123
- AUTH_ACK: getMetaSize( Meta.AUTH_ACK ),
113
+ AUTH_RES: getMetaSize( Meta.AUTH_RES ),
124
114
  ENC_PACK: getMetaSize( Meta.ENC_PACK ),
125
115
  ENC_488: getMetaSize( Meta.ENC_488 )
126
116
  }
127
117
 
128
- // console.log( 'boho MetaSize', MetaSize )
118
+ // console.log( 'boho 2.0.0 MetaSize', MetaSize )
129
119
  // boho MetaSize {
130
- // AUTH_REQ: 2,
131
- // AUTH_NONCE: 13,
132
- // AUTH_HMAC: 45,
133
- // AUTH_ACK: 33,
134
- // ENC_PACK: 25,
135
- // ENC_488: 21
136
- // }
120
+ // SERVER_TIME_NONCE: 13,
121
+ // AUTH_REQ: 45,
122
+ // AUTH_RES: 33,
123
+ // ENC_PACK: 25,
124
+ // ENC_488: 21
125
+ // }
@@ -19,74 +19,73 @@ describe('AUTH process ESM', function () {
19
19
 
20
20
  // server side
21
21
  const s = new Boho()
22
- const auth_nonce_buffer = s.auth_nonce()
23
- const auth_hmac_buffer = c.auth_hmac(auth_nonce_buffer)
22
+ const auth_nonce_buffer = s.server_time_nonce()
23
+ const auth_req_buffer = c.auth_req(auth_nonce_buffer)
24
24
 
25
- const unpack = MBP.unpack(auth_hmac_buffer, Meta.AUTH_HMAC)
25
+ const unpack = MBP.unpack(auth_req_buffer, Meta.AUTH_REQ)
26
26
 
27
27
  // Find hashID8 from DATABASE then set the id and key.
28
28
  s.set_id8(id)
29
29
 
30
30
  s.set_key('wrong-key') // if key is wrong.
31
- const check_auth_hmack_fail = s.check_auth_hmac(unpack)
31
+ const verify_auth_req_fail = s.verify_auth_req(unpack)
32
32
 
33
33
  s.set_key(key) // if correct.
34
- const auth_ack_buffer = s.check_auth_hmac(unpack)
34
+ const auth_res_buffer = s.verify_auth_req(unpack)
35
35
 
36
- const auth_ack_buffer_with_incorrect_hmac = Boho.Buffer.alloc(9)
37
- const isCorrectSeverHMAC = c.check_auth_ack_hmac(auth_ack_buffer)
36
+ const auth_res_buffer_with_incorrect_hmac = Boho.Buffer.alloc(9)
37
+ const isCorrectSeverHMAC = c.verify_auth_res(auth_res_buffer)
38
38
 
39
- auth_ack_buffer.copy(auth_ack_buffer_with_incorrect_hmac)
40
- auth_ack_buffer_with_incorrect_hmac[2] ^= 0x55 // change hmac
41
- const wrongSeverHMACResult = c.check_auth_ack_hmac(auth_ack_buffer_with_incorrect_hmac)
39
+ auth_res_buffer.copy(auth_res_buffer_with_incorrect_hmac)
40
+ auth_res_buffer_with_incorrect_hmac[2] ^= 0x55 // change hmac
41
+ const wrongSeverHMACResult = c.verify_auth_res(auth_res_buffer_with_incorrect_hmac)
42
42
 
43
- // 1. client send AUTH_REQ first
44
43
 
45
- describe('2. server reply AUTH_NONCE', function () {
44
+ describe('1. server send SERVER_TIME_NONCE', function () {
46
45
  it('should return static size buffer', function () {
47
- assert.equal(auth_nonce_buffer.byteLength, MetaSize.AUTH_NONCE)
46
+ assert.equal(auth_nonce_buffer.byteLength, MetaSize.SERVER_TIME_NONCE)
48
47
  })
49
48
 
50
49
  it('should pack[0] is header type ', function () {
51
- assert.ok(auth_nonce_buffer[0], BohoMsg.AUTH_NONCE)
50
+ assert.ok(auth_nonce_buffer[0], BohoMsg.SERVER_TIME_NONCE)
52
51
  })
53
52
  })
54
53
 
55
- describe('3. client reply AUTH_HMAC', function () {
54
+ describe('2. client send AUTH_REQ', function () {
56
55
  it('should return static size buffer', function () {
57
- assert.equal(auth_hmac_buffer.byteLength, MetaSize.AUTH_HMAC)
56
+ assert.equal(auth_req_buffer.byteLength, MetaSize.AUTH_REQ)
58
57
  })
59
58
 
60
59
  it('should pack[0] is header type ', function () {
61
- assert.ok(auth_hmac_buffer[0], BohoMsg.AUTH_HMAC)
60
+ assert.ok(auth_req_buffer[0], BohoMsg.AUTH_REQ)
62
61
  })
63
62
  })
64
63
 
65
- describe('3. server check AUTH_HMACK pack', function () {
64
+ describe('3. server verify AUTH_REQ pack', function () {
66
65
  it('3.1. should success unpack', function () {
67
66
  assert.ok(typeof unpack === 'object')
68
67
  })
69
68
 
70
- describe('3.2. check_auth_hmac(obj) ', function () {
69
+ describe('3.2. verify_auth_req(obj) ', function () {
71
70
  // wrong key of id.
72
71
  it('3.2.1. should return false when wrong key', function () {
73
- assert.ok(!check_auth_hmack_fail)
72
+ assert.ok(!verify_auth_req_fail)
74
73
  })
75
74
 
76
75
  // correct id and key
77
- it('3-2.2. should return AUTH_ACK buffer pack when correct key', function () {
78
- assert.equal(auth_ack_buffer[0], BohoMsg.AUTH_ACK)
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)
79
78
  })
80
79
  })
81
80
  })
82
81
 
83
82
  describe('4. client process', function () {
84
- describe('4.1 check_auth_ack_hmac()', function () {
85
- it('should return true when correct AUTH_ACK ', function () {
83
+ describe('4.1 verify_auth_res()', function () {
84
+ it('should return true when correct AUTH_RES ', function () {
86
85
  assert.ok(isCorrectSeverHMAC)
87
86
  })
88
87
 
89
- it('should return false when AUTH_ACK has wrong hmac', function () {
88
+ it('should return false when AUTH_RES has wrong hmac', function () {
90
89
  assert.ok(!wrongSeverHMACResult)
91
90
  })
92
91
  })
@@ -39,7 +39,7 @@
39
39
  })
40
40
  })
41
41
 
42
- describe('ENC_PACK CJS', function () {
42
+ describe('ENC_PACK', function () {
43
43
  describe('encryption and decryption', function () {
44
44
  const plainData = 'aaaaa'
45
45
  const key = 'key'
@@ -71,80 +71,80 @@
71
71
 
72
72
  // server side
73
73
  const s = new Boho()
74
- const auth_nonce_buffer = s.auth_nonce()
75
- const auth_hmac_buffer = c.auth_hmac(auth_nonce_buffer)
74
+ const auth_nonce_buffer = s.server_time_nonce()
75
+ const auth_req_buffer = c.auth_req(auth_nonce_buffer)
76
76
 
77
- const unpack = MBP.unpack(auth_hmac_buffer, Meta.AUTH_HMAC)
77
+ const unpack = MBP.unpack(auth_req_buffer, Meta.AUTH_REQ)
78
78
 
79
79
  // Find hashID8 from DATABASE then set the id and key.
80
80
  s.set_id8(id)
81
81
 
82
82
  s.set_key('wrong-key') // if key is wrong.
83
- const check_auth_hmack_fail = s.check_auth_hmac(unpack)
83
+ const verify_auth_req_fail = s.verify_auth_req(unpack)
84
84
 
85
85
  s.set_key(key) // if correct.
86
- const auth_ack_buffer = s.check_auth_hmac(unpack)
86
+ const auth_res_buffer = s.verify_auth_req(unpack)
87
87
 
88
- const auth_ack_buffer_with_incorrect_hmac = MBP.Buffer.alloc(9)
89
- const isCorrectSeverHMAC = c.check_auth_ack_hmac(auth_ack_buffer)
88
+ const auth_res_buffer_with_incorrect_hmac = Boho.Buffer.alloc(9)
89
+ const isCorrectSeverHMAC = c.verify_auth_res(auth_res_buffer)
90
90
 
91
- auth_ack_buffer.copy(auth_ack_buffer_with_incorrect_hmac)
92
- auth_ack_buffer_with_incorrect_hmac[2] ^= 0x55 // change hmac
93
- const wrongSeverHMACResult = c.check_auth_ack_hmac(auth_ack_buffer_with_incorrect_hmac)
91
+ auth_res_buffer.copy(auth_res_buffer_with_incorrect_hmac)
92
+ auth_res_buffer_with_incorrect_hmac[2] ^= 0x55 // change hmac
93
+ const wrongSeverHMACResult = c.verify_auth_res(auth_res_buffer_with_incorrect_hmac)
94
94
 
95
- // 1. client send AUTH_REQ first
96
95
 
97
- describe('2. server reply AUTH_NONCE', function () {
96
+ describe('1. server send SERVER_TIME_NONCE', function () {
98
97
  it('should return static size buffer', function () {
99
- assert.equal(auth_nonce_buffer.byteLength, MetaSize.AUTH_NONCE)
98
+ assert.equal(auth_nonce_buffer.byteLength, MetaSize.SERVER_TIME_NONCE)
100
99
  })
101
100
 
102
101
  it('should pack[0] is header type ', function () {
103
- assert.ok(auth_nonce_buffer[0], BohoMsg.AUTH_NONCE)
102
+ assert.ok(auth_nonce_buffer[0], BohoMsg.SERVER_TIME_NONCE)
104
103
  })
105
104
  })
106
105
 
107
- describe('3. client reply AUTH_HMAC', function () {
106
+ describe('2. client send AUTH_REQ', function () {
108
107
  it('should return static size buffer', function () {
109
- assert.equal(auth_hmac_buffer.byteLength, MetaSize.AUTH_HMAC)
108
+ assert.equal(auth_req_buffer.byteLength, MetaSize.AUTH_REQ)
110
109
  })
111
110
 
112
111
  it('should pack[0] is header type ', function () {
113
- assert.ok(auth_hmac_buffer[0], BohoMsg.AUTH_HMAC)
112
+ assert.ok(auth_req_buffer[0], BohoMsg.AUTH_REQ)
114
113
  })
115
114
  })
116
115
 
117
- describe('3. server check AUTH_HMACK pack', function () {
116
+ describe('3. server verify AUTH_REQ pack', function () {
118
117
  it('3.1. should success unpack', function () {
119
118
  assert.ok(typeof unpack === 'object')
120
119
  })
121
120
 
122
- describe('3.2. check_auth_hmac(obj) ', function () {
121
+ describe('3.2. verify_auth_req(obj) ', function () {
123
122
  // wrong key of id.
124
123
  it('3.2.1. should return false when wrong key', function () {
125
- assert.ok(!check_auth_hmack_fail)
124
+ assert.ok(!verify_auth_req_fail)
126
125
  })
127
126
 
128
127
  // correct id and key
129
- it('3-2.2. should return AUTH_ACK buffer pack when correct key', function () {
130
- assert.equal(auth_ack_buffer[0], BohoMsg.AUTH_ACK)
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)
131
130
  })
132
131
  })
133
132
  })
134
133
 
135
134
  describe('4. client process', function () {
136
- describe('4.1 check_auth_ack_hmac()', function () {
137
- it('should return true when correct AUTH_ACK ', function () {
135
+ describe('4.1 verify_auth_res()', function () {
136
+ it('should return true when correct AUTH_RES ', function () {
138
137
  assert.ok(isCorrectSeverHMAC)
139
138
  })
140
139
 
141
- it('should return false when AUTH_ACK has wrong hmac', function () {
140
+ it('should return false when AUTH_RES has wrong hmac', function () {
142
141
  assert.ok(!wrongSeverHMACResult)
143
142
  })
144
143
  })
145
144
  })
146
145
  })
147
146
 
147
+
148
148
  </script>
149
149
 
150
150
  <script class="mocha-exec">