boho 1.1.1 → 2.1.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.1.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,21 @@ 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
138
+ if (++this.counter > 65535) this.counter = 0;
137
139
  const salt12 = Buffer.concat([
138
- MBP.NB('32L', secTime),
139
- MBP.NB('32L', milTime),
140
- RAND(4)
140
+ MBP.NB('32L', secTime),
141
+ MBP.NB('16L', milTime),
142
+ MBP.NB('16L', this.counter),
143
+ RAND(4) // JS: use crypto.getRandomValues(), Arduino: use micros()
141
144
  ])
142
-
143
145
  salt12.copy(this._otpSrc44, 32)
144
146
  }
145
147
 
@@ -148,15 +150,16 @@ export class Boho {
148
150
  * @param {Buffer} nonce
149
151
  */
150
152
  set_clock_nonce(nonce) {
151
- let milTime = Date.now()
152
- let secTime = parseInt(milTime / 1000)
153
- milTime = milTime % 0xffffffff
153
+ const now = Date.now()
154
+ const secTime = parseInt(now / 1000)
155
+ const milTime = now % 1000
156
+ if (++this.counter > 65535) this.counter = 0;
154
157
  const salt12 = Buffer.concat([
155
- MBP.NB('32L', secTime),
156
- MBP.NB('32L', milTime),
158
+ MBP.NB('32L', secTime),
159
+ MBP.NB('16L', milTime),
160
+ MBP.NB('16L', this.counter),
157
161
  nonce
158
162
  ])
159
-
160
163
  salt12.copy(this._otpSrc44, 32)
161
164
  }
162
165
 
@@ -164,7 +167,11 @@ export class Boho {
164
167
  * Sets salt12 value in otpSrc44.
165
168
  * @param {Buffer} salt12
166
169
  */
167
- set_salt12(salt12) {
170
+ set_salt12(salt12 , caller) {
171
+ // console.log('boho.set_salt12', salt12 , caller )
172
+ if( salt12.byteLength != 12){
173
+ throw TypeError('set_salt12: Invalid salt12 byteLength.')
174
+ }
168
175
  salt12.copy(this._otpSrc44, 32)
169
176
  }
170
177
 
@@ -236,64 +243,57 @@ export class Boho {
236
243
  // B. AUTH process
237
244
 
238
245
  /**
239
- * Generates AUTH_REQ message
246
+ * Generates server time and nonce signal
240
247
  * @returns {Buffer}
241
248
  */
242
- auth_req() {
243
- return MBP.pack(
244
- MBP.MB('#type', '8', BohoMsg.AUTH_REQ),
245
- MBP.MB('#reserved', '8', 0)
246
- )
247
- }
249
+ server_time_nonce() {
250
+ const now = Date.now()
251
+ const unixTime = Math.floor(now / 1000)
252
+ const milliseconds = now % 1000;
248
253
 
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
254
  this.localNonce = RAND(4)
255
+ //keep
258
256
  this.auth_salt12 = Buffer.concat([
259
- MBP.NB('32L', unixTime),
260
- MBP.NB('32L', milTime),
257
+ MBP.NB('32L', unixTime),
258
+ MBP.NB('16L', milliseconds),
259
+ RAND(2), // used for client's counter IV.
261
260
  this.localNonce
262
261
  ])
263
262
 
264
263
  let infoPack = Buffer.concat([
265
- MBP.NB('8', BohoMsg.AUTH_NONCE),
264
+ MBP.NB('8', BohoMsg.SERVER_TIME_NONCE),
266
265
  this.auth_salt12
267
266
  ])
268
267
  return infoPack
269
268
  }
270
269
 
271
270
  /**
272
- * Generates AUTH_HMAC message
273
- * @param {Buffer} buffer
271
+ * Generates AUTH_REQ message
272
+ * @param {Buffer} buffer server's time nonce
274
273
  * @returns {Buffer|boolean}
275
274
  */
276
- auth_hmac(buffer) {
277
- let auth_nonce = MBP.unpack(buffer, Meta.AUTH_NONCE)
278
- if (auth_nonce) {
275
+ auth_req(buffer) {
276
+ let server_time_nonce = MBP.unpack(buffer, Meta.SERVER_TIME_NONCE)
277
+ if (server_time_nonce) {
279
278
  let salt12 = Buffer.concat([
280
- MBP.NB('32L', auth_nonce.unixTime),
281
- MBP.NB('32L', auth_nonce.milTime),
282
- auth_nonce.nonce
279
+ MBP.NB('32L', server_time_nonce.unixTime),
280
+ MBP.NB('16L', server_time_nonce.milTime),
281
+ MBP.NB('16L', server_time_nonce.counter),
282
+ server_time_nonce.nonce
283
283
  ])
284
284
 
285
- this.set_salt12(salt12)
285
+ this.set_salt12(salt12, 'auth_req')
286
286
 
287
287
  this.localNonce = RAND(4)
288
288
  this.generateHMAC(this.localNonce)
289
289
 
290
- this.remoteNonce = auth_nonce.nonce
290
+ this.remoteNonce = server_time_nonce.nonce
291
291
 
292
292
  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 ),
293
+ MBP.MB('#header', '8', BohoMsg.AUTH_REQ),
294
+ MBP.MB('#id8', this._id8),
295
+ MBP.MB('#nonce', this.localNonce),
296
+ MBP.MB('#hmac32', this._hmac),
297
297
  )
298
298
 
299
299
  return auth_hmac_buffer
@@ -303,31 +303,26 @@ export class Boho {
303
303
 
304
304
  /*
305
305
  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
306
  */
312
307
 
313
308
  /**
314
- * Verifies client's AUTH_HMAC.
309
+ * Verify client's AUTH_REQ.
315
310
  * @param {Buffer|object} data
316
311
  * @returns {boolean}
317
312
  */
318
- check_auth_hmac(data) {
313
+ verify_auth_req(data) {
314
+
319
315
  let infoPack
320
316
  if (data instanceof Uint8Array) {
321
- infoPack = MBP.unpack(data, Meta.AUTH_HMAC)
317
+ infoPack = MBP.unpack(data, Meta.AUTH_REQ)
322
318
  if (!infoPack) {
323
319
  return
324
320
  }
325
321
  } else {
326
322
  infoPack = data;
327
-
328
323
  }
329
324
 
330
- this.set_salt12(this.auth_salt12)
325
+ this.set_salt12(this.auth_salt12, 'verify_auth_req 1/2')
331
326
 
332
327
  this.generateHMAC(infoPack.nonce)
333
328
  let hmac32 = this._hmac
@@ -340,37 +335,38 @@ export class Boho {
340
335
  this.remoteNonce,
341
336
  this.localNonce
342
337
  ])
343
- this.set_salt12(salt12)
338
+
339
+ this.set_salt12(salt12, 'verify_auth_req 2/2')
344
340
  this.generateHMAC(infoPack.nonce)
345
341
  let replyHMAC = this._hmac
346
342
 
347
- let auth_ack = MBP.rawPack(
348
- MBP.MB('header', '8', BohoMsg.AUTH_ACK),
349
- MBP.MB('hmac32', replyHMAC)
343
+ let auth_res = MBP.rawPack(
344
+ MBP.MB('header', '8', BohoMsg.AUTH_RES),
345
+ MBP.MB('hmac32', replyHMAC)
350
346
  )
351
347
  this.isAuthorized = true
352
- return auth_ack
348
+ return auth_res
353
349
  }
354
350
  return false
355
351
  }
356
352
 
357
353
  /**
358
- * Verifies server's AUTH_ACK HMAC.
354
+ * Verifies server's AUTH_RES HMAC.
359
355
  * @param {Buffer} buffer
360
356
  * @returns {boolean}
361
357
  */
362
- check_auth_ack_hmac(buffer) {
363
- let auth_ack = MBP.unpack(buffer, Meta.AUTH_ACK)
364
- if (auth_ack) {
358
+ verify_auth_res(buffer) {
359
+ let auth_res = MBP.unpack(buffer, Meta.AUTH_RES)
360
+ if (auth_res) {
365
361
  let salt12 = Buffer.concat([
366
362
  this.remoteNonce,
367
363
  this.localNonce,
368
364
  this.remoteNonce,
369
365
  ])
370
- this.set_salt12(salt12)
366
+ this.set_salt12(salt12, 'verify_auth_res')
371
367
  this.generateHMAC(this.localNonce)
372
368
  let hmac32 = this._hmac
373
- if (MBP.equal(hmac32, auth_ack.hmac32)) {
369
+ if (MBP.equal(hmac32, auth_res.hmac32)) {
374
370
  this.isAuthorized = true
375
371
  return true
376
372
  }
@@ -424,7 +420,7 @@ export class Boho {
424
420
  this.localNonce
425
421
  ])
426
422
 
427
- this.set_salt12(salt12)
423
+ this.set_salt12(salt12 , 'decrypt_488')
428
424
  this.resetOTP()
429
425
 
430
426
  let xdata = pack.$OTHERS.subarray(0, pack.len)
@@ -481,7 +477,7 @@ export class Boho {
481
477
  let pack = MBP.unpack(data, Meta.ENC_PACK)
482
478
  if (!pack) return
483
479
 
484
- this.set_salt12(pack.salt12)
480
+ this.set_salt12(pack.salt12, 'decryptPack')
485
481
  this.resetOTP()
486
482
 
487
483
  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">