boho 1.1.0 → 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/README.md CHANGED
@@ -1,56 +1,152 @@
1
+
1
2
  # Boho
2
3
 
3
- Secure Lightweight Encryption & Authentication Library for Node.js, Browsers and Arduino.
4
+ **Node.js, 브라우저, Arduino를 위한 안전하고 가벼운 암호화 및 인증 라이브러리**
5
+
6
+ Boho는 강력한 암호화, 인증 및 보안 통신을 위한 경량 JavaScript 라이브러리입니다. Node.js, 브라우저, 그리고 Arduino와 같은 임베디드 시스템을 위해 설계되었으며, 최소한의 오버헤드로 강력한 보안이 필요한 IoT, 임베디드 및 웹 애플리케이션에 이상적입니다.
7
+
8
+ - `boho`는 **보호**를 의미합니다.
9
+
10
+ ---
11
+
12
+ ## 주요 기능
13
+ - 범용 암호화 및 복호화
14
+ - 상호 인증 프로토콜 (아래 참조)
15
+ - 인증 후 보안 통신
16
+ - 종단간(End-to-End) 대칭 암호화
17
+ - TypeScript 타입 정의 포함
18
+ - JavaScript (Node.js, 브라우저) 및 C/C++ (Arduino) 지원
19
+
20
+ ---
21
+
22
+ ## 라이브러리
23
+ - **JavaScript:** Node.js, 웹 브라우저 ([GitHub](https://github.com/remocons/boho))
24
+ - **C/C++:** Arduino ([GitHub](https://github.com/remocons/boho-arduino))
25
+
26
+ ---
27
+
28
+ ## 대표적인 사용 사례
29
+ - WebSocket 인증 및 보안 메시징
30
+ - 보안 TCP/직렬/스트림 통신
31
+ - 보안 MQTT 페이로드
32
+ - 로컬 파일 암호화
33
+
34
+ ---
35
+
36
+ ## 주요 API
37
+ - `encryptPack`, `decryptPack`: 범용 암호화/복호화
38
+ - `encrypt_488`, `decrypt_488`: 인증 후 보안 통신
39
+ - `encrypt_e2e`, `decrypt_e2e`: 종단간 암호화
40
+ - `RAND(size)`: 안전한 랜덤 버퍼 생성
41
+ - `sha256.hash`, `sha256.hex`, `sha256.base64`, `sha256.hmac`: 해싱 유틸리티
42
+
43
+ ---
44
+
45
+ ## 인증 프로토콜 (요약)
46
+
47
+ boho 라이브러리의 서버-클라이언트 인증(AUTH) 프로세스는 다음과 같은 단계로 이루어집니다.
48
+
49
+ ---
50
+
51
+ ### 1. 서버 → 클라이언트: SERVER_TIME_NONCE
52
+ - 서버는 현재 시각(초/밀리초)과 랜덤 nonce를 포함한 `SERVER_TIME_NONCE` 메시지를 클라이언트에 보냅니다.
53
+ - 메시지 타입: `BohoMsg.SERVER_TIME_NONCE`
54
+ - 내용: unixTime, milTime, nonce
55
+
56
+ ### 2. 클라이언트 → 서버: AUTH_REQ
57
+ - 클라이언트는 서버로부터 받은 nonce와 시각 정보를 바탕으로 salt12를 생성하고, 자신의 랜덤 nonce를 새로 만듭니다.
58
+ - salt12와 자신의 nonce를 합쳐 HMAC을 생성한 뒤, `AUTH_REQ` 메시지로 서버에 전송합니다.
59
+ - 메시지 타입: `BohoMsg.AUTH_REQ`
60
+ - 내용: id8, clientNonce, hmac32
61
+
62
+ ### 3. 서버: AUTH_REQ 검증 및 응답
63
+ - 서버는 클라이언트의 HMAC이 올바른지 검증합니다.
64
+ - 검증 성공 시, 서버는 자신의 HMAC을 생성하여 `AUTH_RES` 메시지로 클라이언트에 응답합니다.
65
+ - 실패 시, `AUTH_FAIL` 메시지를 보낼 수 있습니다.
66
+
67
+ ### 4. 클라이언트: AUTH_RES 검증
68
+ - 클라이언트는 서버의 `AUTH_RES` 메시지에 포함된 HMAC이 올바른지 검증합니다.
69
+ - 검증이 성공하면, 양쪽 모두 `isAuthorized = true` 상태가 되어 이후 암호화 통신이 가능합니다.
70
+
71
+ ---
72
+
73
+ ### 메시지 플로우 요약
74
+
75
+ ```mermaid
76
+ sequenceDiagram
77
+ participant Client
78
+ participant Server
79
+
80
+ Server->>Client: SERVER_TIME_NONCE (unixTime, milTime, nonce)
81
+ Client->>Server: AUTH_REQ (id8, clientNonce, hmac32)
82
+ Server->>Client: AUTH_RES (hmac32) / AUTH_FAIL
83
+ Client->>Client: AUTH_RES verification
84
+ ```
85
+
86
+ ---
87
+
88
+ ### 각 단계의 목적
89
+
90
+ - **SERVER_TIME_NONCE**: 서버가 시간/nonce를 제공하여 리플레이 공격 방지
91
+ - **AUTH_REQ**: 클라이언트가 서버의 정보를 바탕으로 HMAC 생성(서버가 검증)
92
+ - **AUTH_RES**: 서버가 클라이언트의 인증 성공을 알리고, 클라이언트는 서버의 HMAC 검증 상호 인증
93
+ - **AUTH_FAIL**: 인증 실패 시 서버가 전송
94
+
95
+ ---
96
+
97
+ ### 특징 및 보안성
98
+
99
+ - 서버와 클라이언트 모두 랜덤 nonce와 시각 정보를 사용하여, 리플레이 공격에 강함
100
+ - HMAC 기반 상호 인증으로, 키를 노출하지 않고 인증 가능
101
+ - 인증이 완료되어야만 이후 암호화 통신(ENC_488, ENC_PACK, E2E )이 가능
4
102
 
5
- Boho is a lightweight JavaScript library that provides easy-to-use encryption, authentication, and secure communication features. Designed for both Node.js and browser environments, Boho offers simple APIs for generating random values, hashing, HMAC, and secure message packing, making it ideal for IoT, embedded, and web applications that require robust security with minimal overhead.
103
+ ---
6
104
 
7
- - ``boho`` means Protection
105
+ boho의 인증 프로토콜은 서버와 클라이언트가 nonce와 시각 정보를 교환하고, HMAC을 통해 상호 인증하는 구조입니다. 이 과정을 통해 안전하게 세션을 설정하고, 이후 암호화된 데이터 통신이 가능합니다.
8
106
 
9
- ## features
10
- - general encryption.
11
- - authentication.
12
- - secure communication.
13
- - End-to-End Encryption with Symmetric.
14
- - support JS & C/C++ for Arduino.
107
+ ---
15
108
 
16
- ## libraries
17
- - JavaScript: Node.js , Web Browser. [ [github](https://github.com/remocons/boho) ]
18
- - C/C++: Arduino [ [github](https://github.com/remocons/boho-arduino) ]
109
+ ## 사용 예제
19
110
 
20
- ## applications
111
+ ### 일반 데이터 암호화 및 복호화
112
+ ```js
113
+ import Boho from 'boho'
21
114
 
22
- - Websocket authentication, secure communication.
23
- - secure TCP/Serial/Stream communication, authentication.
24
- - secure MQTT payload.
25
- - local file encryption.
115
+ let boho = new Boho()
26
116
 
27
- ## core
28
- - Using SHA256
29
- - xotp
30
- - generateOTP
31
- - generateHMAC
117
+ boho.set_key('abc' )
32
118
 
33
- ## general purpose encryption
119
+ let data = 'aaaaaaaa'
34
120
 
35
- - encryptPack
36
- - decryptPack
121
+ let encData = boho.encryptPack( data )
122
+ console.log('encData buffer:', encData )
123
+ let result = boho.decryptPack( encData )
124
+
125
+ if(result){
126
+ console.log('result object:', result )
127
+ printMessage(result.data) // decode to string.
128
+ }else{
129
+ console.log('decryption is fail')
130
+ }
37
131
 
132
+ function printMessage(data){
133
+ let dataStr = new TextDecoder().decode( data )
134
+ console.log('
135
+ result string:',dataStr)
136
+ }
38
137
 
39
- ## authentication protocol.
138
+ ```
40
139
 
41
- - AUTH_REQ
42
- - AUTH_NONCE
43
- - AUTH_HMAC
44
- - check_auth_hmac
45
- - AUTH_ACK
46
- - check_auth_ack_hamc
47
- - AUTH_FAIL
140
+ ### Boho를 사용한 인증 및 보안 통신 예제는 `IOSignal`을 참고하세요.
141
+ - `iosignal` ([GitHub](https://github.com/remocons/iosignal))
142
+ - test/AUTH_process.js
48
143
 
49
- ## secure communication after auth.
144
+ ---
50
145
 
51
- - ENC_488
52
- - ENC_E2E
146
+ ## TypeScript 지원
147
+ Boho는 완전한 TypeScript 타입 정의를 제공합니다. 간단히 설치하고 가져오기만 하면 TypeScript 프로젝트에서 자동 완성 및 타입 검사를 받을 수 있습니다.
53
148
 
149
+ ---
54
150
 
55
- ## license
56
- - MIT
151
+ ## 라이선스
152
+ MIT
package/dist/boho.d.ts CHANGED
@@ -1,37 +1,32 @@
1
1
  // TypeScript type definitions for boho
2
+ // Project: https://github.com/remocons/boho
3
+ // Definitions by: Taeo Lee (sixgen@gmail.com)
4
+
2
5
  import { Buffer } from 'buffer';
6
+ import MBP from 'meta-buffer-pack';
3
7
 
4
8
  export function RAND(size: number): Buffer;
5
9
 
10
+ // Interfaces based on src/constants.js and usage in boho.js
6
11
  export interface BohoMsgType {
12
+ SERVER_TIME_NONCE: number;
7
13
  AUTH_REQ: number;
8
- AUTH_NONCE: number;
9
- AUTH_HMAC: number;
10
- AUTH_ACK: number;
11
- AUTH_FAIL: number;
12
- AUTH_EXT: number;
14
+ AUTH_RES: number;
13
15
  ENC_PACK: number;
14
- ENC_E2E: number;
15
16
  ENC_488: number;
16
17
  [key: string]: number | string;
17
18
  }
18
19
 
19
20
  export interface MetaType {
21
+ SERVER_TIME_NONCE: any;
20
22
  AUTH_REQ: any;
21
- AUTH_NONCE: any;
22
- AUTH_HMAC: any;
23
- AUTH_ACK: any;
23
+ AUTH_RES: any;
24
24
  ENC_PACK: any;
25
25
  ENC_488: any;
26
26
  }
27
27
 
28
28
  export interface MetaSizeType {
29
- AUTH_REQ: number;
30
- AUTH_NONCE: number;
31
- AUTH_HMAC: number;
32
- AUTH_ACK: number;
33
29
  ENC_PACK: number;
34
- ENC_488: number;
35
30
  }
36
31
 
37
32
  export interface Sha256 {
@@ -42,59 +37,187 @@ export interface Sha256 {
42
37
  }
43
38
 
44
39
  export class Boho {
45
- _id8: Buffer;
46
- _otpSrc44: Buffer;
47
- _otp36: Buffer;
48
- _hmac: Buffer;
40
+ protected _id8: Buffer;
41
+ protected _otpSrc44: Buffer;
42
+ protected _otp36: Buffer;
43
+ protected _hmac: Buffer;
44
+
49
45
  auth_salt12: Buffer;
50
46
  localNonce: Buffer;
51
47
  remoteNonce: Buffer;
52
48
  isAuthorized: boolean;
49
+ counter: number;
53
50
 
54
51
  constructor();
52
+
53
+ /**
54
+ * Initializes authentication state.
55
+ */
55
56
  clearAuth(): void;
57
+
58
+ /**
59
+ * Sets id8 by hashing the input data.
60
+ * @param data
61
+ */
56
62
  set_hash_id8(data: any): void;
63
+
64
+ /**
65
+ * Sets the id8 value.
66
+ * @param data
67
+ */
57
68
  set_id8(data: any): void;
69
+
70
+ /**
71
+ * Sets the key value by hashing and storing in otpSrc44.
72
+ * @param data
73
+ */
58
74
  set_key(data: any): void;
75
+
76
+ /**
77
+ * Splits 'id.key' string and sets id8 and key.
78
+ * @param id_key
79
+ */
59
80
  set_id_key(id_key: string): void;
81
+
82
+ /**
83
+ * Copies id8 value from external buffer.
84
+ * @param data
85
+ */
60
86
  copy_id8(data: Buffer): void;
87
+
88
+ /**
89
+ * Copies key value from external buffer.
90
+ * @param data
91
+ */
61
92
  copy_key(data: Buffer): void;
93
+
94
+ /**
95
+ * Applies sha256 hash n times.
96
+ * @param srcData
97
+ * @param n
98
+ */
62
99
  sha256_n(srcData: any, n: number): Uint8Array;
100
+
101
+ /**
102
+ * Sets random clock value (salt12) in otpSrc44.
103
+ */
63
104
  set_clock_rand(): void;
105
+
106
+ /**
107
+ * Sets otpSrc44 with given nonce and clock value.
108
+ * @param nonce
109
+ */
64
110
  set_clock_nonce(nonce: Buffer): void;
65
- set_salt12(salt12: Buffer): void;
111
+
112
+ /**
113
+ * Sets salt12 value in otpSrc44.
114
+ * @param salt12
115
+ * @param caller - for debug
116
+ */
117
+ set_salt12(salt12: Buffer, caller?: string): void;
118
+
119
+ /**
120
+ * Initializes OTP value.
121
+ */
66
122
  resetOTP(): void;
123
+
124
+ /**
125
+ * Returns OTP value for the given index.
126
+ * @param otpIndex
127
+ */
67
128
  getIndexOTP(otpIndex: number): Uint8Array;
129
+
130
+ /**
131
+ * Generates HMAC value.
132
+ * @param data
133
+ */
68
134
  generateHMAC(data: Buffer): void;
135
+
136
+ /**
137
+ * Returns 8-byte HMAC value.
138
+ * @param data
139
+ */
69
140
  getHMAC8(data: Buffer): Buffer;
141
+
142
+ /**
143
+ * OTP-based XOR encryption/decryption
144
+ * @param data
145
+ * @param otpStartIndex
146
+ * @param shareDataBuffer
147
+ */
70
148
  xotp(data: Buffer, otpStartIndex?: number, shareDataBuffer?: boolean): Buffer;
71
- auth_req(): Buffer;
72
- auth_nonce(): Buffer;
73
- auth_hmac(buffer: Buffer): Buffer | boolean;
74
- check_auth_hmac(data: Buffer | object): boolean;
75
- check_auth_ack_hmac(buffer: Buffer): boolean;
149
+
150
+ // B. AUTH process
151
+
152
+ /**
153
+ * Generates server time and nonce signal
154
+ */
155
+ server_time_nonce(): Buffer;
156
+
157
+ /**
158
+ * Generates AUTH_REQ message
159
+ * @param buffer server's time nonce
160
+ */
161
+ auth_req(buffer: Buffer): Buffer | boolean;
162
+
163
+ /**
164
+ * Verify client's AUTH_REQ.
165
+ * @param data
166
+ * @returns auth_res packet or false
167
+ */
168
+ verify_auth_req(data: Buffer | object): Buffer | false;
169
+
170
+ /**
171
+ * Verifies server's AUTH_RES HMAC.
172
+ * @param buffer
173
+ */
174
+ verify_auth_res(buffer: Buffer): boolean | undefined;
175
+
176
+ // C. Secure Communication
177
+
178
+ /**
179
+ * Generates encrypted 488 packet after authentication
180
+ * @param data
181
+ */
76
182
  encrypt_488(data: Buffer): Buffer | undefined;
183
+
184
+ /**
185
+ * Decrypts 488 packet after authentication
186
+ * @param data
187
+ */
77
188
  decrypt_488(data: Buffer): Buffer | undefined;
189
+
190
+ /**
191
+ * Generates encrypted packet for up to 2^32-1 bytes
192
+ * @param data
193
+ */
78
194
  encryptPack(data: Buffer): Buffer;
79
- decryptPack(data: Buffer): Buffer;
80
- encrypt_e2e(data: Buffer, key: Buffer): Buffer;
81
- decrypt_e2e(data: Buffer, key: Buffer): Buffer;
195
+
196
+ /**
197
+ * Decrypts encrypted packet
198
+ * @param data
199
+ * @returns The decrypted data buffer inside a pack object, or undefined.
200
+ */
201
+ decryptPack(data: Buffer): { data: Buffer, [key: string]: any } | undefined;
202
+
203
+ /**
204
+ * End-to-end encryption
205
+ * @param data
206
+ * @param key
207
+ */
208
+ encrypt_e2e(data: Buffer, key: any): Buffer;
209
+
210
+ /**
211
+ * End-to-end decryption
212
+ * @param data
213
+ * @param key
214
+ */
215
+ decrypt_e2e(data: Buffer, key: any): { data: Buffer, [key: string]: any } | undefined;
82
216
  }
83
217
 
84
218
  export const BohoMsg: BohoMsgType;
85
219
  export const Meta: MetaType;
86
220
  export const MetaSize: MetaSizeType;
87
221
  export const sha256: Sha256;
88
- export const MBP: any;
89
- export { Buffer };
90
-
91
- declare const _default: typeof Boho & {
92
- RAND: typeof RAND;
93
- BohoMsg: typeof BohoMsg;
94
- Meta: typeof Meta;
95
- MetaSize: typeof MetaSize;
96
- sha256: typeof sha256;
97
- MBP: typeof MBP;
98
- Buffer: typeof Buffer;
99
- };
100
- export default _default;
222
+
223
+ export { MBP, Buffer };