boho 1.0.2 → 1.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,12 +1,13 @@
1
1
  {
2
2
  "name": "boho",
3
- "version": "1.0.2",
4
- "description": "Encryption, authentication, Secure communication",
3
+ "version": "1.1.0",
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",
@@ -24,18 +25,18 @@
24
25
  "url": "git+https://github.com/remocons/boho.git"
25
26
  },
26
27
  "author": {
27
- "name": "Lee Taeo",
28
- "email": "taeo.mocha@gmail.com"
28
+ "name": "Taeo Lee",
29
+ "email": "sixgen@gmail.com"
29
30
  },
30
31
  "license": "MIT",
31
32
  "devDependencies": {
32
- "@rollup/plugin-commonjs": "^26.0.1",
33
- "@rollup/plugin-node-resolve": "^15.2.3",
33
+ "@rollup/plugin-commonjs": "^28.0.3",
34
+ "@rollup/plugin-node-resolve": "^15.3.1",
34
35
  "@rollup/plugin-terser": "^0.4.4",
35
- "rollup": "^4.21.2"
36
+ "rollup": "^4.36.0"
36
37
  },
37
38
  "dependencies": {
38
- "meta-buffer-pack": "^2.0.3"
39
+ "meta-buffer-pack": "^2.1.0"
39
40
  },
40
41
  "scripts": {
41
42
  "build": "rollup -c",
package/src/boho.js CHANGED
@@ -6,13 +6,22 @@ export { BohoMsg, Meta, MetaSize, sha256 }
6
6
  import { Buffer } from 'buffer/index.js'
7
7
  export { Buffer}
8
8
 
9
+ /**
10
+ * 랜덤 바이트 버퍼를 생성합니다.
11
+ * @param {number} size - 생성할 바이트 수
12
+ * @returns {Buffer}
13
+ */
9
14
  export function RAND(size) {
10
15
  return globalThis.crypto.getRandomValues(Buffer.alloc(size))
11
16
  }
12
17
 
18
+ /**
19
+ * Boho 보안 프로토콜 클래스
20
+ */
13
21
  export class Boho {
14
-
15
- // A. Core
22
+ /**
23
+ * @constructor
24
+ */
16
25
  constructor() {
17
26
 
18
27
  this._id8 = Buffer.alloc(8)
@@ -27,6 +36,9 @@ export class Boho {
27
36
 
28
37
  }
29
38
 
39
+ /**
40
+ * 인증 상태를 초기화합니다.
41
+ */
30
42
  clearAuth(){
31
43
  this._id8.fill(0)
32
44
  this._otpSrc44.fill(0)
@@ -38,24 +50,38 @@ export class Boho {
38
50
  this.isAuthorized = false
39
51
  }
40
52
 
41
- // for the self
53
+ /**
54
+ * 입력 데이터를 해시하여 id8에 설정합니다.
55
+ * @param {any} data
56
+ */
42
57
  set_hash_id8(data) {
43
58
  let idSum = MBP.B8(sha256.hash(data))
44
59
  idSum.copy(this._id8, 0, 0, 8)
45
60
  }
46
61
 
62
+ /**
63
+ * id8 값을 설정합니다.
64
+ * @param {any} data
65
+ */
47
66
  set_id8(data) {
48
67
  let encStr = MBP.B8(data)
49
68
  this._id8.fill(0)
50
69
  encStr.copy(this._id8, 0, 0, 8)
51
70
  }
52
71
 
72
+ /**
73
+ * 키 값을 해시하여 otpSrc44에 설정합니다.
74
+ * @param {any} data
75
+ */
53
76
  set_key(data) {
54
77
  let keySum = MBP.B8(sha256.hash(data))
55
78
  keySum.copy(this._otpSrc44, 0, 0, 32)
56
79
  }
57
80
 
58
- // id_key == 'id' + '.' + 'key'
81
+ /**
82
+ * 'id.key' 문자열을 분리하여 id8과 key를 설정합니다.
83
+ * @param {string} id_key
84
+ */
59
85
  set_id_key(id_key) {
60
86
  let delimiterPosition = id_key.indexOf('.')
61
87
  if( delimiterPosition == -1 ) return
@@ -65,23 +91,37 @@ export class Boho {
65
91
  this.set_key(key)
66
92
  }
67
93
 
94
+ /**
95
+ * 외부에서 id8 값을 복사합니다.
96
+ * @param {Buffer} data
97
+ */
68
98
  copy_id8(data) {
69
99
  data.copy(this._id8, 0, 0, 8)
70
100
  }
71
101
 
102
+ /**
103
+ * 외부에서 key 값을 복사합니다.
104
+ * @param {Buffer} data
105
+ */
72
106
  copy_key(data) {
73
107
  data.copy(this._otpSrc44, 0, 0, 32)
74
108
  }
75
109
 
76
-
110
+ /**
111
+ * sha256 해시를 n번 반복 적용합니다.
112
+ * @param {any} srcData
113
+ * @param {number} n
114
+ * @returns {Uint8Array}
115
+ */
77
116
  sha256_n(srcData, n) {
78
117
  let hashSum = sha256.hash(srcData)
79
118
  for (let i = 0; i < n; i++) hashSum = sha256.hash(hashSum)
80
119
  return hashSum
81
120
  }
82
121
 
83
-
84
- // useful general encryption i.e. enc_pack
122
+ /**
123
+ * 랜덤 시계값(salt12)을 otpSrc44에 설정합니다.
124
+ */
85
125
  set_clock_rand() {
86
126
 
87
127
  let milTime = Date.now()
@@ -96,7 +136,10 @@ export class Boho {
96
136
  salt12.copy(this._otpSrc44, 32)
97
137
  }
98
138
 
99
- // for secure communication sender.
139
+ /**
140
+ * 주어진 nonce와 시계값을 otpSrc44에 설정합니다.
141
+ * @param {Buffer} nonce
142
+ */
100
143
  set_clock_nonce(nonce) {
101
144
  let milTime = Date.now()
102
145
  let secTime = parseInt(milTime / 1000)
@@ -110,34 +153,59 @@ export class Boho {
110
153
  salt12.copy(this._otpSrc44, 32)
111
154
  }
112
155
 
113
-
156
+ /**
157
+ * salt12 값을 otpSrc44에 설정합니다.
158
+ * @param {Buffer} salt12
159
+ */
114
160
  set_salt12(salt12) {
115
161
  salt12.copy(this._otpSrc44, 32)
116
162
  }
117
163
 
164
+ /**
165
+ * otp 값을 초기화합니다.
166
+ */
118
167
  resetOTP() {
119
168
  let otp32 = MBP.B8(sha256.hash(this._otpSrc44))
120
169
  otp32.copy(this._otp36, 0, 0, 32)
121
170
  }
122
171
 
172
+ /**
173
+ * 인덱스에 해당하는 OTP 값을 반환합니다.
174
+ * @param {number} otpIndex
175
+ * @returns {Uint8Array}
176
+ */
123
177
  getIndexOTP(otpIndex) {
124
178
  this._otp36.writeUInt32LE(otpIndex, 32)
125
179
  return sha256.hash(this._otp36)
126
180
  }
127
181
 
128
-
182
+ /**
183
+ * HMAC 값을 생성합니다.
184
+ * @param {Buffer} data
185
+ */
129
186
  generateHMAC(data) {
130
187
  let hmacSrc = Buffer.concat([this._otpSrc44, data])
131
188
  this._hmac = MBP.B8(sha256.hash(hmacSrc))
132
189
  }
133
190
 
134
- // return 8 bytes of hash
191
+ /**
192
+ * 8바이트 HMAC 값을 반환합니다.
193
+ * @param {Buffer} data
194
+ * @returns {Buffer}
195
+ */
135
196
  getHMAC8(data) {
136
197
  let hmacSrc = Buffer.concat([this._otpSrc44, data])
137
198
  this._hmac = MBP.B8(sha256.hash(hmacSrc))
138
199
  return this._hmac.subarray(0, 8)
139
200
  }
140
201
 
202
+ /**
203
+ * OTP 기반 XOR 암호화/복호화
204
+ * @param {Buffer} data
205
+ * @param {number} [otpStartIndex=0]
206
+ * @param {boolean} [shareDataBuffer=false]
207
+ * @returns {Buffer}
208
+ */
141
209
  xotp(data, otpStartIndex = 0, shareDataBuffer = false) {
142
210
 
143
211
  data = MBP.B8(data, shareDataBuffer)
@@ -160,8 +228,10 @@ export class Boho {
160
228
 
161
229
  // B. AUTH process
162
230
 
163
- // step 1
164
- // client send AUTH_REQ
231
+ /**
232
+ * AUTH_REQ 메시지 생성
233
+ * @returns {Buffer}
234
+ */
165
235
  auth_req() {
166
236
  return MBP.pack(
167
237
  MBP.MB('#type', '8', BohoMsg.AUTH_REQ),
@@ -169,8 +239,10 @@ export class Boho {
169
239
  )
170
240
  }
171
241
 
172
- // step 2
173
- // server send AUTH_NONCE
242
+ /**
243
+ * AUTH_NONCE 메시지 생성
244
+ * @returns {Buffer}
245
+ */
174
246
  auth_nonce() {
175
247
  let now = Date.now()
176
248
  let unixTime = Math.floor(now / 1000)
@@ -189,26 +261,14 @@ export class Boho {
189
261
  return infoPack
190
262
  }
191
263
 
192
-
193
- // step 3
194
- // client send AUTH_HMAC
195
- // input : auth_nonce buffer
264
+ /**
265
+ * AUTH_HMAC 메시지 생성
266
+ * @param {Buffer} buffer
267
+ * @returns {Buffer|boolean}
268
+ */
196
269
  auth_hmac(buffer) {
197
270
  let auth_nonce = MBP.unpack(buffer, Meta.AUTH_NONCE)
198
271
  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
272
  let salt12 = Buffer.concat([
213
273
  MBP.NB('32L', auth_nonce.unixTime),
214
274
  MBP.NB('32L', auth_nonce.milTime),
@@ -218,18 +278,15 @@ export class Boho {
218
278
  this.set_salt12(salt12)
219
279
 
220
280
  this.localNonce = RAND(4)
221
- // hmac( key, sec,mil,serverNonce, localNonce)
222
281
  this.generateHMAC(this.localNonce)
223
282
 
224
- // let hmac8 = this._hmac.subarray(0, 8)
225
-
226
283
  this.remoteNonce = auth_nonce.nonce
227
284
 
228
- let auth_hmac_buffer = MBP.pack( // 21 -> 45
285
+ let auth_hmac_buffer = MBP.pack(
229
286
  MBP.MB('#header', '8', BohoMsg.AUTH_HMAC),
230
287
  MBP.MB('#id8', this._id8),
231
288
  MBP.MB('#nonce', this.localNonce),
232
- MBP.MB('#hmac32', this._hmac ), //full 32bytes hash
289
+ MBP.MB('#hmac32', this._hmac ),
233
290
  )
234
291
 
235
292
  return auth_hmac_buffer
@@ -246,30 +303,29 @@ export class Boho {
246
303
  or send AUTH_FAIL when fail.
247
304
  */
248
305
 
249
- // input: unpack object or buffer of auth_hmac
306
+ /**
307
+ * 클라이언트의 AUTH_HMAC을 검증합니다.
308
+ * @param {Buffer|object} data
309
+ * @returns {boolean}
310
+ */
250
311
  check_auth_hmac(data) {
251
312
  let infoPack
252
313
  if (data instanceof Uint8Array) {
253
314
  infoPack = MBP.unpack(data, Meta.AUTH_HMAC)
254
315
  if (!infoPack) {
255
- // console.log('auth_hamc unpack fail.')
256
316
  return
257
317
  }
258
318
  } else {
259
319
  infoPack = data;
260
320
 
261
321
  }
262
- // console.log('auth_hamc infoObj', infoPack )
263
322
 
264
323
  this.set_salt12(this.auth_salt12)
265
324
 
266
- // hmac( key, sec,mil,serverNonce, clientNonce)
267
325
  this.generateHMAC(infoPack.nonce)
268
- // let hmac8 = this._hmac.subarray(0, 8)
269
326
  let hmac32 = this._hmac
270
327
 
271
328
  if (MBP.equal(infoPack.hmac32, hmac32)) {
272
- //Auth success then store client nonce.
273
329
  this.remoteNonce = infoPack.nonce
274
330
 
275
331
  let salt12 = Buffer.concat([
@@ -291,12 +347,12 @@ export class Boho {
291
347
  return false
292
348
  }
293
349
 
294
-
295
-
296
- // step 5. cross check
297
- // client check server's hmac.
350
+ /**
351
+ * 서버의 AUTH_ACK HMAC을 검증합니다.
352
+ * @param {Buffer} buffer
353
+ * @returns {boolean}
354
+ */
298
355
  check_auth_ack_hmac(buffer) {
299
- // server response has hmac ( key + clientNonce)
300
356
  let auth_ack = MBP.unpack(buffer, Meta.AUTH_ACK)
301
357
  if (auth_ack) {
302
358
  let salt12 = Buffer.concat([
@@ -306,22 +362,24 @@ export class Boho {
306
362
  ])
307
363
  this.set_salt12(salt12)
308
364
  this.generateHMAC(this.localNonce)
309
- // let hmac8 = this._hmac.subarray(0, 8)
310
365
  let hmac32 = this._hmac
311
- //server side hmac using client nonce.
312
366
  if (MBP.equal(hmac32, auth_ack.hmac32)) {
313
367
  this.isAuthorized = true
314
368
  return true
315
369
  }
316
370
  }
317
- // server hmac error
318
371
  return
319
372
  }
320
373
 
321
374
  // C. Secure Communication
322
375
 
323
376
  // Must AUTH first.
324
- encrypt_488(data) { // payload max about 2^32 bytes.
377
+ /**
378
+ * 인증 후 488 암호화 패킷 생성
379
+ * @param {Buffer} data
380
+ * @returns {Buffer|undefined}
381
+ */
382
+ encrypt_488(data) {
325
383
  if (!this.isAuthorized) return
326
384
 
327
385
  data = MBP.B8(data)
@@ -339,11 +397,14 @@ export class Boho {
339
397
  MBP.MB('#hmac8', hmac8),
340
398
  MBP.MB('#xdata', encData)
341
399
  )
342
- // console.log('enc pack result', pack )
343
400
  return pack
344
401
  }
345
402
 
346
-
403
+ /**
404
+ * 인증 후 488 암호화 패킷 복호화
405
+ * @param {Buffer} data
406
+ * @returns {Buffer|undefined}
407
+ */
347
408
  decrypt_488(data) {
348
409
  data = MBP.B8(data)
349
410
 
@@ -366,14 +427,14 @@ export class Boho {
366
427
 
367
428
  if (MBP.equal(hmac8, pack.hmac8)) return decData
368
429
 
369
- // console.log('hmac dismatch', decData )
370
- } else {
371
- // console.log('unpack fail')
372
430
  }
373
431
  }
374
432
 
375
-
376
- // maxium data size is 2**32 -1 bytes.
433
+ /**
434
+ * 최대 2^32-1 바이트 데이터 암호화 패킷 생성
435
+ * @param {Buffer} data
436
+ * @returns {Buffer}
437
+ */
377
438
  encryptPack(data) {
378
439
  data = MBP.B8(data)
379
440
 
@@ -393,24 +454,24 @@ export class Boho {
393
454
  return pack
394
455
  }
395
456
 
396
-
457
+ /**
458
+ * 암호화 패킷 복호화
459
+ * @param {Buffer} data
460
+ * @returns {Buffer}
461
+ */
397
462
  decryptPack(data) {
398
463
 
399
464
  if (data[0] !== BohoMsg.ENC_PACK) {
400
- // console.log('Boho: Invalid packType')
401
465
  return
402
466
  }
403
467
 
404
- // packLength
405
468
  let readPackLen = data.readUint32LE(1);
406
469
  if (readPackLen != data.byteLength - MetaSize.ENC_PACK) {
407
- // console.log('Boho: Invalid LEN data_len: data.byteLen' , readPackLen, data.byteLength)
408
470
  return
409
471
  }
410
472
 
411
473
  try {
412
474
  let pack = MBP.unpack(data, Meta.ENC_PACK)
413
- // console.log('unpack result', pack )
414
475
  if (!pack) return
415
476
 
416
477
  this.set_salt12(pack.salt12)
@@ -424,14 +485,18 @@ export class Boho {
424
485
  pack.data = decData
425
486
  return pack
426
487
  }
427
- // console.log('Invalid HMAC', pack.hmac, hmac8 )
428
488
 
429
489
  } catch (error) {
430
- // console.log('Boho: unpack err', error )
431
490
 
432
491
  }
433
492
  }
434
493
 
494
+ /**
495
+ * E2E 암호화
496
+ * @param {Buffer} data
497
+ * @param {Buffer} key
498
+ * @returns {Buffer}
499
+ */
435
500
  encrypt_e2e(data, key) {
436
501
  let baseKey = Buffer.alloc(32)
437
502
  baseKey.set(this._otpSrc44.subarray(0, 32))
@@ -441,6 +506,12 @@ export class Boho {
441
506
  return pack;
442
507
  }
443
508
 
509
+ /**
510
+ * E2E 복호화
511
+ * @param {Buffer} data
512
+ * @param {Buffer} key
513
+ * @returns {Buffer}
514
+ */
444
515
  decrypt_e2e(data, key) {
445
516
  let baseKey = Buffer.alloc(32)
446
517
  baseKey.set(this._otpSrc44.subarray(0, 32))
package/src/constants.js CHANGED
@@ -1,6 +1,20 @@
1
1
  import MBP from "meta-buffer-pack"
2
2
  import { Buffer } from 'buffer/index.js'
3
3
 
4
+ /**
5
+ * Boho 메시지 타입 상수
6
+ * @typedef {Object} BohoMsg
7
+ * @property {number} AUTH_REQ
8
+ * @property {number} AUTH_NONCE
9
+ * @property {number} AUTH_HMAC
10
+ * @property {number} AUTH_ACK
11
+ * @property {number} AUTH_FAIL
12
+ * @property {number} AUTH_EXT
13
+ * @property {number} ENC_PACK
14
+ * @property {number} ENC_E2E
15
+ * @property {number} ENC_488
16
+ */
17
+
4
18
  // remocon message pack one byte header.
5
19
  export let BohoMsg = {
6
20
  AUTH_REQ : 0xB0,
@@ -16,6 +30,17 @@ export let BohoMsg = {
16
30
 
17
31
  for (let c in BohoMsg) { BohoMsg[BohoMsg[c]] = c }
18
32
 
33
+ /**
34
+ * Boho 메시지 메타 정보
35
+ * @typedef {Object} Meta
36
+ * @property {any} AUTH_REQ
37
+ * @property {any} AUTH_NONCE
38
+ * @property {any} AUTH_HMAC
39
+ * @property {any} AUTH_ACK
40
+ * @property {any} ENC_PACK
41
+ * @property {any} ENC_488
42
+ */
43
+
19
44
  export const Meta = {
20
45
 
21
46
  AUTH_REQ: MBP.meta( // 2
@@ -69,6 +94,21 @@ export const Meta = {
69
94
  return lastItem[2] + lastItem[3]
70
95
  }
71
96
 
97
+ /**
98
+ * 각 메타의 바이트 크기 정보
99
+ * @typedef {Object} MetaSize
100
+ * @property {number} AUTH_REQ
101
+ * @property {number} AUTH_NONCE
102
+ * @property {number} AUTH_HMAC
103
+ * @property {number} AUTH_ACK
104
+ * @property {number} ENC_PACK
105
+ * @property {number} ENC_488
106
+ */
107
+
108
+ /**
109
+ * 각 메타의 바이트 크기 정보
110
+ * @type {MetaSize}
111
+ */
72
112
  export const MetaSize = {
73
113
  AUTH_REQ: getMetaSize( Meta.AUTH_REQ ),
74
114
  AUTH_NONCE: getMetaSize( Meta.AUTH_NONCE ),
package/src/index.js CHANGED
@@ -8,4 +8,24 @@ Boho.sha256 = sha256;
8
8
  Boho.MBP = MBP;
9
9
  Boho.Buffer = Buffer;
10
10
 
11
+ /**
12
+ * boho 라이브러리의 메인 엔트리포인트
13
+ * @module boho
14
+ */
15
+
16
+ /**
17
+ * @typedef {import('./boho.js').Boho} Boho
18
+ * @typedef {import('./boho.js').RAND} RAND
19
+ * @typedef {import('./boho.js').BohoMsg} BohoMsg
20
+ * @typedef {import('./boho.js').Meta} Meta
21
+ * @typedef {import('./boho.js').MetaSize} MetaSize
22
+ * @typedef {import('./boho.js').sha256} sha256
23
+ * @typedef {import('./boho.js').MBP} MBP
24
+ * @typedef {import('./boho.js').Buffer} Buffer
25
+ */
26
+
27
+ /**
28
+ * boho의 기본 export 객체
29
+ * @type {Boho & { RAND: typeof RAND, BohoMsg: typeof BohoMsg, Meta: typeof Meta, MetaSize: typeof MetaSize, sha256: typeof sha256, MBP: typeof MBP, Buffer: typeof Buffer }}
30
+ */
11
31
  export default Boho;
package/src/sha256-mbp.js CHANGED
@@ -10,20 +10,45 @@ Tip.
10
10
  MBP.B8( any ) reutn Buffer instance
11
11
 
12
12
  */
13
+ /**
14
+ * sha256 및 hmac 유틸리티 함수 모음
15
+ * @namespace sha256
16
+ */
13
17
  const sha256 = {};
14
18
 
19
+ /**
20
+ * sha256 해시를 반환합니다.
21
+ * @param {any} data
22
+ * @returns {Uint8Array}
23
+ */
15
24
  sha256.hash = function (data) {
16
25
  return hash(MBP.U8(data))
17
26
  }
18
27
 
28
+ /**
29
+ * sha256 해시의 hex 문자열을 반환합니다.
30
+ * @param {any} data
31
+ * @returns {string}
32
+ */
19
33
  sha256.hex = function (data) {
20
34
  return MBP.B8( hash(MBP.U8(data)) ).toString('hex')
21
35
  }
22
36
 
37
+ /**
38
+ * sha256 해시의 base64 문자열을 반환합니다.
39
+ * @param {any} data
40
+ * @returns {string}
41
+ */
23
42
  sha256.base64= function (data) {
24
43
  return MBP.B8( hash(MBP.U8(data)) ).toString('base64')
25
44
  }
26
45
 
46
+ /**
47
+ * hmac 값을 반환합니다.
48
+ * @param {any} key
49
+ * @param {any} data
50
+ * @returns {Uint8Array}
51
+ */
27
52
  sha256.hmac = function (key, data) {
28
53
  return hmac(MBP.U8(key), MBP.U8(data))
29
54
  }