boho 2.2.2 → 3.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
@@ -2,354 +2,138 @@
2
2
 
3
3
  [English](README.md) | [한국어](README.ko.md)
4
4
 
5
- **Lightweight encryption and authentication for Node.js, browsers and Arduino**
5
+ **Shared-key authentication and encrypted messages on standard Ascon, for Node.js, browsers and Arduino**
6
6
 
7
- Boho is a JavaScript library providing shared-key authentication and encrypted
8
- binary messages. It runs in Node.js and browsers; the Arduino implementation is
9
- available in a separate repository. The current protocol is a custom SHA-256-based
10
- construction and must not be treated as providing the same guarantees as standard
11
- AEAD or TLS.
7
+ Boho is a JavaScript library for shared-key login and binary encrypted messages. It runs in Node.js and
8
+ browsers. Since **3.0.0** it is built on **Ascon** (NIST SP 800-232), the lightweight cryptography standard,
9
+ instead of the earlier custom SHA-256 construction (2.x). The Arduino implementation is part of
10
+ [IOSignal Arduino](https://github.com/remocons/iosignal-arduino) 10.0.0; Kotlin and Python implementations
11
+ exist for the IOSignal clients. All of them produce the same bytes (`test/fixtures/boho-vectors.json`).
12
12
 
13
13
  - `boho` means **protection** in Korean.
14
14
 
15
+ > **3.0.0 is not compatible with 2.x.** The message codes and keys changed. A server that stored 2.x key
16
+ > hashes must register the keys again (it stores `Boho.deriveKey(key)` instead). IOSignal 10.0.0 uses boho 3.
17
+
15
18
  ---
16
19
 
17
20
  ## Why Boho?
18
21
 
19
- Boho is designed for environments where **you control the code and key provisioning
20
- at both endpoints**. With known peers such as an Arduino device, your own web app
21
- and a personal server, you can provision a secret securely before using it for
22
- authentication and encryption.
22
+ Boho is for environments where **you control the code and key provisioning at both endpoints**: an Arduino
23
+ device, your own web app, a personal server. A key installed over USB, entered by the user or delivered
24
+ through an existing SSH/TLS channel is enough to authenticate the peer and protect messages, without
25
+ certificates.
23
26
 
24
- Many encryption algorithms already exist, but practical development also needs
25
- peer authentication, packet formats, key provisioning and compatibility between
26
- devices and browsers. Boho combines a SHA-256-based keystream, XOR encryption,
27
- shared-key authentication and binary packets. Its distinction is **a common
28
- implementation and application model for Arduino, Node.js and browsers**, rather
29
- than novelty in XOR itself.
27
+ Boho does not invent cryptography. It puts a login handshake, session keys and compact binary packets on
28
+ Ascon, with compatible implementations for Arduino, JavaScript, Kotlin and Python.
30
29
 
31
- ### Where pre-shared symmetric keys fit
30
+ ## Algorithms
32
31
 
33
- | Environment | Example provisioning path |
32
+ | Purpose | Algorithm |
34
33
  | --- | --- |
35
- | Embedded devices you build | Provision device-specific keys over USB or serial during manufacture or installation |
36
- | Your own web app and personal device | Users enter a key obtained separately, or register it through trusted pairing |
37
- | Servers under common administration | Distribute keys through SSH, an existing TLS channel or a secret management system |
38
-
39
- If initial key sharing is handled, appropriate symmetric encryption and
40
- authentication can provide secure communication. Peers can authenticate through
41
- their shared key without a certificate authority. Trust rests on the provisioning
42
- path and endpoint software. Sharing one key across many devices expands both its
43
- authentication scope and the impact of a leak; plan device-specific keys,
44
- rotation and revocation.
45
-
46
- Writing a web app yourself does not automatically keep its key secret. A common
47
- secret included in a public JavaScript bundle is readable by visitors.
48
-
49
- ### XOR and one-time pads
50
-
51
- XOR is a simple operation: applying it twice with the same value restores the
52
- original value.
53
-
54
- ```text
55
- Encryption: C = P XOR S
56
- Decryption: P = C XOR S
57
-
58
- P: plaintext, C: ciphertext, S: keystream
59
- ```
60
-
61
- A uniformly random keystream that is independent of the plaintext, as long as the
62
- data, kept secret and used only once provides the perfect secrecy of a one-time
63
- pad (OTP). Under these conditions even simple XOR forms the basis of strong
64
- encryption. The practical difficulty is supplying a new 1 MB secret pad to both
65
- endpoints for every 1 MB of data. Repeating a short fixed key does not meet these
66
- conditions.
67
-
68
- Reusing a keystream exposes `C1 XOR C2 = P1 XOR P2`, revealing a relationship
69
- between the plaintexts. Security depends on keystream generation and avoiding
70
- reuse, not on the complexity of XOR.
71
-
72
- ### A “virtual OTP” generated with SHA-256
73
-
74
- Hashing sufficiently unpredictable secret input together with message-specific
75
- values, while varying a block index, can form a construction that generates a
76
- keystream of the required length. Both peers can reproduce the same output from
77
- the same input, without storing or transmitting the entire pad. Boho's usual
78
- `set_key` path is:
79
-
80
- ```text
81
- K = SHA256(input key)
82
- B = SHA256(K || salt12)
83
- S1 = SHA256(B || LE32(1))
84
- S2 = SHA256(B || LE32(2))
85
- …
86
- S = S1 || S2 || … // use only the plaintext length
87
- C = P XOR S
88
- ```
89
-
90
- `||` means byte concatenation; `LE32` is a four-byte little-endian integer.
91
- SHA-256 produces 32 bytes, so a 70-byte plaintext uses two full blocks and the
92
- first six bytes of the third. Decryption XORs with the same keystream rather than
93
- reversing the hash.
94
-
95
- `salt12` contains seconds (4 bytes), milliseconds (2), a counter (2) and a nonce
96
- (4). These values are not secret and are available to the receiver. **Anyone can
97
- compute a hash of public random values alone.** The shared secret key is the
98
- essential secret input; avoiding reuse of `salt12` under the same key is also
99
- important.
100
-
101
- “Virtual OTP” here describes a hash-based pseudorandom keystream. Expanding a short
102
- key into a long output does not provide the information-theoretic security of a
103
- true OTP. Using standard SHA-256 does not establish the security of the whole
104
- construction. Keystream generation followed by XOR also appears in
105
- [ChaCha20](https://www.rfc-editor.org/rfc/rfc8439.html#section-2.4), but Boho is a
106
- separate custom construction.
107
-
108
- XOR alone does not detect tampering. Boho data packets also contain a verification
109
- tag: the first eight bytes of `SHA256(K || salt12 || plaintext)`. Despite the
110
- legacy `HMAC` names in the code, this tag is not standard HMAC. Receivers must use
111
- data only after decryption and tag verification succeed.
112
-
113
- ### Comparing with TLS
114
-
115
- Typical certificate-based TLS authenticates peers and establishes keys without
116
- requiring a previously shared secret, then protects application data with
117
- symmetric encryption. Web services therefore do not need to distribute a secret
118
- to each visitor in advance. Even users who have not logged in can authenticate a
119
- server and establish an encrypted connection; this does not imply network
120
- anonymity.
121
-
122
- For small embedded projects, certificate chains, trust roots, validity and clock
123
- management, renewal, handshake work and buffers can be burdensome. Costs include
124
- code size, memory, power and operating time, not just certificate purchases.
125
- Systems that can already provision keys securely may be able to omit some of
126
- this machinery.
127
-
128
- However, **TLS also supports PSK-only and PSK with (EC)DHE**, so it should not be
129
- described as always requiring public-key exchange and third-party certificates.
130
- [TLS 1.3 pre-shared keys](https://www.rfc-editor.org/rfc/rfc8446.html#section-2.2)
131
-
132
- Boho's lightweight goal is to provide application-packet protection and shared-key
133
- authentication without public-key operations or certificate handling in its
134
- construction. This does not mean it is faster or uses less memory than every TLS
135
- or standard symmetric implementation; actual comparisons require measurement.
136
-
137
- ### Arduino, web apps and IOSignal
138
-
139
- Boho's Arduino and JavaScript implementations exchange encrypted packets between
140
- DIY devices, Node.js servers and browser apps. Boho handles encryption and
141
- authentication, while [IOSignal](https://github.com/remocons/iosignal) and
142
- [IOSignal for Arduino](https://github.com/remocons/iosignal-arduino) handle
143
- connections and message delivery.
144
-
145
- Connection credentials and end-to-end (E2E) data keys serve different purposes.
146
- To keep the body confidential from a relay, only the final endpoints should hold
147
- its separate data key. Ordinary connection encryption is not automatically E2E,
148
- and E2E does not hide all routing information or traffic sizes.
149
-
150
- Modified web app code can expose keys and plaintext, so normal web deployments
151
- can use HTTPS/WSS alongside Boho. Boho does not bypass browser mixed content
152
- rules, and E2E still requires trust in the provider of the web app code.
153
-
154
- ### Conditions to check when applying Boho
155
-
156
- - Use strong random keys. A single SHA-256 in `set_key` is not a slow password KDF.
157
- - JavaScript uses `crypto.getRandomValues()`, but Arduino currently uses `micros()` for standalone-packet nonces and the authentication client nonce. This is not a cryptographic random source; consider repeated key/`salt12` combinations across restarts, devices and communication directions.
158
- - A valid tag alone does not prove that a message is new. The caller manages challenge expiry, replay rejection, ordering and permission to execute commands.
159
- - Boho is a custom protocol. Do not assume standard AEAD/TLS guarantees or forward secrecy protecting past traffic after a long-term key leak.
160
-
161
- ---
162
-
163
- ## Features
164
-
165
- - General-purpose encryption and decryption
166
- - Mutual authentication protocol (see below)
167
- - Encrypted communication after authentication
168
- - End-to-end symmetric encryption
169
- - TypeScript declarations
170
- - JavaScript (Node.js, browsers) and C/C++ (Arduino) implementations
171
-
172
- ---
173
-
174
- ## Libraries
175
-
176
- - **JavaScript:** Node.js and browsers ([GitHub](https://github.com/remocons/boho))
177
- - **C/C++:** Arduino ([GitHub](https://github.com/remocons/boho-arduino))
178
-
179
- ---
180
-
181
- ## Typical use cases
182
-
183
- - WebSocket authentication and encrypted messaging
184
- - Encrypted TCP, serial and stream communication
185
- - Encrypted MQTT payloads
186
- - Local file encryption
187
-
188
- ---
189
-
190
- ## Main APIs
191
-
192
- - `encryptPack`, `decryptPack`: general-purpose encryption and decryption
193
- - `encrypt_488`, `decrypt_488`: encrypted communication after authentication
194
- - `encrypt_e2e`, `decrypt_e2e`: end-to-end encryption
195
- - `RAND(size)`: cryptographically secure random buffer generation
196
- - `sha256.hash`, `sha256.hex`, `sha256.base64`, `sha256.hmac`: hashing utilities
197
-
198
- ---
199
-
200
- ## Authentication protocol overview
201
-
202
- The Boho client–server authentication (AUTH) exchange follows these steps.
203
-
204
- ---
205
-
206
- ### 1. Server → client: SERVER_TIME_NONCE
207
-
208
- - The server sends `SERVER_TIME_NONCE` containing the current time (seconds/milliseconds) and a random nonce.
209
- - Message type: `BohoMsg.SERVER_TIME_NONCE`
210
- - Contents: unixTime, milTime, nonce
34
+ | Message encryption and tamper check | Ascon-AEAD128 (8-byte tag) |
35
+ | Key string → 16-byte key | Ascon-CXOF128 (customization `boho/v2/key`) |
36
+ | Login → session key and both proofs | Ascon-CXOF128 (customization `boho/v2/session`) |
211
37
 
212
- ### 2. Client → server: AUTH_REQ
38
+ Ascon is small and fast on 8-bit microcontrollers (one 320-bit state and one permutation do everything) and
39
+ uses no lookup tables, so it has no timing side channel. `Boho.ascon` exposes the primitives
40
+ (AEAD128, Hash256, XOF128, CXOF128).
213
41
 
214
- - The client constructs salt12 from the server's nonce and time, and creates its own nonce. JavaScript uses a random nonce; Arduino currently uses `micros()`.
215
- - It computes an authentication tag using salt12 and its nonce, then sends `AUTH_REQ`.
216
- - Message type: `BohoMsg.AUTH_REQ`
217
- - Contents: id8, clientNonce, hmac32
42
+ ## Messages
218
43
 
219
- ### 3. Server: verify AUTH_REQ and respond
44
+ | Type | Code | Size | Use |
45
+ | --- | --- | --- | --- |
46
+ | `SERVER_TIME_NONCE` | 0xB0 | 13 B | Server challenge (time and random bytes) |
47
+ | `AUTH_REQ` | 0xB4 | 33 B | Client: ID, client nonce, client proof |
48
+ | `AUTH_RES` | 0xB5 | 17 B | Server proof |
49
+ | `AUTH_FAIL` | 0xB3 | 1 B | Login refused |
50
+ | `ENC` | 0xBB | 21 B + data | Message of a logged-in session |
51
+ | `ENC_E2E` | 0xBA | 21 B + header + body | Encrypted routing header followed by an E2E body |
52
+ | `ENC_PACK` | 0xB9 | 29 B + data | Standalone or E2E data with its own key |
220
53
 
221
- - The server verifies the client's authentication tag.
222
- - On success, it generates its own tag and returns `AUTH_RES`.
223
- - On failure, it may send `AUTH_FAIL`.
54
+ Details: [docs/PROTOCOL.md](docs/PROTOCOL.md) (Korean).
224
55
 
225
- ### 4. Client: verify AUTH_RES
56
+ ## Install
226
57
 
227
- - The client verifies the authentication tag in `AUTH_RES`.
228
- - Once verification succeeds, both peers have `isAuthorized = true` and can exchange encrypted messages.
229
-
230
- ---
231
-
232
- ### Message flow
233
-
234
- ```mermaid
235
- sequenceDiagram
236
- participant Client
237
- participant Server
238
-
239
- Server->>Client: SERVER_TIME_NONCE (unixTime, milTime, nonce)
240
- Client->>Server: AUTH_REQ (id8, clientNonce, hmac32)
241
- Server->>Client: AUTH_RES (hmac32) / AUTH_FAIL
242
- Client->>Client: AUTH_RES verification
58
+ ```sh
59
+ npm install boho
243
60
  ```
244
61
 
245
- ---
246
-
247
- ### Purpose of each step
248
-
249
- - **SERVER_TIME_NONCE**: provides a per-connection server challenge; the caller manages its lifetime and reuse policy.
250
- - **AUTH_REQ**: the client generates an authentication tag from the server's information for server verification.
251
- - **AUTH_RES**: the server responds after verifying the client; the client verifies the server's tag for mutual authentication.
252
- - **AUTH_FAIL**: the server sends this on authentication failure.
253
-
254
- ---
255
-
256
- ### Properties and security scope
62
+ Browsers: `dist/boho.min.js` (UMD, global `Boho`) or `dist/boho.js` (ES module).
257
63
 
258
- - Authentication verifies against the challenge stored by the server. Challenge expiry and reuse restrictions, attempt limits and final connection approval belong to the caller, such as IOSignal.
259
- - Tag-based mutual authentication avoids transmitting the key itself.
260
- - `ENC_488` is used after mutual authentication. `ENC_PACK` and E2E operate with an explicitly configured key without requiring a prior authentication exchange.
64
+ ## Usage
261
65
 
262
- ---
263
-
264
- Boho exchanges nonces and time information between the server and client, then
265
- uses authentication tags for mutual authentication. The authenticated session can
266
- subsequently exchange encrypted messages.
267
-
268
- ---
269
-
270
- ## Usage example
271
-
272
- ### General-purpose encryption and decryption
66
+ ### Encrypt and decrypt data with a key
273
67
 
274
68
  ```js
275
69
  import Boho from 'boho'
276
70
 
277
- let boho = new Boho()
278
-
279
- // In real communication, use a random key securely shared with the peer.
280
- boho.set_key(Boho.RAND(32))
281
-
282
- let data = 'aaaaaaaa'
283
-
284
- let encData = boho.encryptPack( data )
285
- console.log('encData buffer:', encData )
286
- let result = boho.decryptPack( encData )
287
-
288
- if(result){
289
- console.log('result object:', result )
290
- printMessage(result.data) // decode to string.
291
- }else{
292
- console.log('decryption is fail')
293
- }
294
-
295
- function printMessage(data){
296
- let dataStr = new TextDecoder().decode( data )
297
- console.log('\n result string:',dataStr)
298
- }
71
+ const boho = new Boho()
72
+ boho.set_key('a long random key')
73
+ const packet = boho.encryptPack('hello') // ENC_PACK
74
+ console.log(boho.decryptPack(packet).data.toString()) // 'hello'
299
75
 
76
+ // E2E: a separate data key shared only by the final endpoints
77
+ const sealed = boho.encrypt_e2e('secret', 'e2e-key')
78
+ console.log(boho.decrypt_e2e(sealed, 'e2e-key').data.toString())
300
79
  ```
301
80
 
302
- ### Authentication and encrypted communication examples
303
-
304
- See `IOSignal` and the authentication example:
81
+ `decryptPack` and `decrypt_e2e` return `{ data, ... }`, or `undefined` for a wrong key or a damaged packet.
305
82
 
306
- - `iosignal` ([GitHub](https://github.com/remocons/iosignal))
307
- - test/AUTH_process.js
83
+ ### Login and session messages
308
84
 
309
- ---
310
-
311
- ## TypeScript support
312
-
313
- Declarations cover the default-exported `Boho` class and static properties such
314
- as `Boho.RAND`, `Boho.Buffer` and `Boho.MBP`. Use `import Boho from 'boho'`.
315
- Named value exports such as `import { Boho, RAND } from 'boho'` are not provided.
85
+ ```js
86
+ // Server: stores Boho.deriveKey(key) for the ID, never the key string
87
+ const server = new Boho()
88
+ server.copy_key(Boho.deriveKey('a long random key'))
89
+ const challenge = server.server_time_nonce() // send to the client
90
+
91
+ // Client
92
+ const client = new Boho()
93
+ client.set_id_key('dev1.a long random key')
94
+ const authReq = client.auth_req(challenge) // send to the server
95
+
96
+ // Server
97
+ const authRes = server.verify_auth_req(authReq) // false when the proof is wrong
98
+ // Client
99
+ client.verify_auth_res(authRes) // true: both sides are logged in
100
+
101
+ const up = client.encrypt_session('to server') // ENC
102
+ server.decrypt_session(up) // Buffer 'to server'
103
+ ```
316
104
 
317
- ## Receive policy and security scope
105
+ ## Main APIs
318
106
 
319
- - Internal `HMAC` names and packet `hmac` fields are retained for compatibility. Protocol tags actually use `SHA256(internal key || salt || data)`, not standard HMAC. The utility `Boho.sha256.hmac(key, data)` is standard HMAC.
320
- - `set_key` requires a nonempty key. `encryptPack` throws if no key is configured, and `clearAuth` also clears the key. `copy_key` accepts exactly 32 bytes.
321
- - Boho does not enforce a duplicate-receive cache or authentication-stage restrictions. Duplicate, ordering and freshness checks for `ENC_488`, challenge expiry and connection state belong to the caller, such as IOSignal. A valid tag alone does not prove freshness.
322
- - `set_key`/`copy_key` do not automatically change `isAuthorized`. The caller must explicitly manage state during reauthentication, key changes and shutdown.
323
- - JS sending maintains increasing clock/counter values even if the clock moves backward or the counter wraps within the same millisecond. Receiving permits existing Arduino clock adjustments and counter wrapping, without enforcing receive-history policy.
324
- - `ENC_PACK` and E2E support independent messages and stored data, permitting repeated decryption. Replay detection for these APIs belongs to the caller.
325
- - Decryption rejects truncated packets, invalid types/lengths and tag mismatches with `undefined`. `ENC_PACK`/`ENC_488` also reject extra bytes. IOSignal's `ENC_E2E` is an exception: only the routing header of the declared length is decrypted, and the delivery layer preserves the trailing E2E body. The final recipient verifies that body with `decrypt_e2e`.
326
- - A slow KDF for short passwords, forward secrecy and E2E key distribution are not provided. Cryptographic design changes, including nonce and tag sizes, require a new protocol version.
107
+ | API | Description |
108
+ | --- | --- |
109
+ | `Boho.deriveKey(key)` | Key string → 16-byte key (what a server stores) |
110
+ | `set_id8(id)`, `set_key(key)`, `set_id_key('id.key')`, `copy_key(key16)` | ID and key |
111
+ | `server_time_nonce()`, `verify_auth_req(packet[, challenge])` | Server side of the login |
112
+ | `auth_req(challenge)`, `verify_auth_res(packet)` | Client side of the login |
113
+ | `encrypt_session(data[, type])`, `decrypt_session(packet)` | `ENC` / `ENC_E2E` after login |
114
+ | `encryptPack(data)`, `decryptPack(packet)` | `ENC_PACK` with this side's key |
115
+ | `encrypt_e2e(data, key)`, `decrypt_e2e(packet, key)` | `ENC_PACK` with an E2E key |
116
+ | `Boho.BohoMsg`, `Boho.MetaSize` | Message codes and sizes |
117
+ | `Boho.ascon`, `Boho.sha256`, `Boho.RAND`, `Boho.MBP` | Primitives and helpers |
118
+
119
+ TypeScript definitions are in `dist/boho.d.ts`.
120
+
121
+ ## Security scope
122
+
123
+ - Use sufficiently random keys. Key derivation is not a slow password KDF.
124
+ - Every login makes a new session key; senders keep increasing the clock and counter so nonces never repeat.
125
+ - Boho does not reject replays or expire challenges by itself; the application (the IOSignal server) does.
126
+ - There is no forward secrecy: a leaked long-term key exposes the traffic it protected.
127
+ - Boho does not replace HTTPS for delivering browser code that handles keys.
327
128
 
328
129
  ## Development checks
329
130
 
330
- Open the [browser test page on GitHub Pages](https://remocons.github.io/boho/test-browser/) to run the random generation, encryption/decryption and authentication tests in your browser. Tests run automatically when the page loads.
331
-
332
- `npm test` first rebuilds distribution files, then runs regression tests for the
333
- source, ESM and browser UMD (in a Node VM), plus TypeScript checks. Actual browser
334
- and physical Arduino tests are separate.
335
-
336
- `npm run test:arduino` compiles the adjacent `../boho-arduino/src/Boho.cpp`
337
- unchanged with a host C++ compiler and checks bidirectional JS communication.
338
- Set `BOHO_ARDUINO_PATH` for a different location. macOS uses CommonCrypto; other
339
- hosts need OpenSSL development libraries. Test clock, Serial and SHA-256 adapters
340
- mean this does not replace physical device tests.
341
-
342
- `npm run test:iosignal` uses the actual sources in adjacent `../iosignal` and
343
- `../iosignal-arduino`. A loader injects the modified Boho into the JS server and
344
- client, and tests authentication plus ordinary/E2EE traffic in both directions
345
- using Arduino IOSignal's 80-byte RX buffer and an in-memory transport.
346
- `IOSIGNAL_PATH` and `IOSIGNAL_ARDUINO_PATH` override those locations. This does not
347
- test actual AVR SRAM usage or physical networking.
348
-
349
- See [security fixes](docs/SECURITY_FIXES.md) for the issue list and compatibility
350
- impact.
131
+ ```sh
132
+ npm test # Ascon KATs, shared vectors, class tests, types
133
+ BOHO_ARDUINO_SRC=../iosignal-arduino/src npm test # also JS ↔ Arduino C++ (host build)
134
+ ```
351
135
 
352
- ---
136
+ The earlier 2.x source is not part of the package; see the 2.x releases.
353
137
 
354
138
  ## License
355
139