boho 2.2.1 → 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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "boho",
3
- "version": "2.2.1",
4
- "description": "Secure Lightweight Encryption & Authentication Library for Node.js, Browsers and Arduino.",
3
+ "version": "3.0.0",
4
+ "description": "Shared-key authentication and encrypted messages on standard Ascon (NIST SP 800-232) for Node.js, browsers and Arduino.",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  "types": "./dist/boho.d.ts",
@@ -18,7 +18,10 @@
18
18
  "crypto",
19
19
  "encryption",
20
20
  "보호",
21
- "암호"
21
+ "암호",
22
+ "ascon",
23
+ "aead",
24
+ "authentication"
22
25
  ],
23
26
  "homepage": "https://github.com/remocons/boho#readme",
24
27
  "repository": {
@@ -43,10 +46,9 @@
43
46
  },
44
47
  "scripts": {
45
48
  "build": "rollup -c",
46
- "test": "node --test test/*.js && npm run test:types",
49
+ "test": "node tools/make-vectors.mjs --check && node --test test/*.test.js && npm run test:types",
47
50
  "pretest": "npm run build",
48
51
  "test:types": "tsc -p test/types/tsconfig.json",
49
- "test:arduino": "npm run build && node --test test/arduino/interop.test.js",
50
52
  "test:iosignal": "npm run build && node --import ./test/iosignal/register.js --test test/iosignal/full-flow.test.js"
51
53
  }
52
54
  }
package/src/ascon.js ADDED
@@ -0,0 +1,262 @@
1
+ /*
2
+ * ascon.js — Ascon (NIST SP 800-232) for Boho v2
3
+ *
4
+ * Ascon-AEAD128, Ascon-Hash256, Ascon-XOF128 and Ascon-CXOF128 on one 320-bit permutation.
5
+ * JS partner of the Arduino BohoAscon.cpp. Used by BohoV2 (src/boho-v2.js).
6
+ *
7
+ * Each 64-bit state word is kept as two 32-bit halves (lo, hi) so that plain
8
+ * 32-bit integer operations work in every JS engine without BigInt. A byte view
9
+ * of the same buffer gives the little-endian byte order SP 800-232 uses.
10
+ *
11
+ * Author: Taeo Lee <sixgen@gmail.com>
12
+ */
13
+
14
+ if (new Uint8Array(new Uint32Array([1]).buffer)[0] !== 1) {
15
+ throw new Error('ascon.js needs a little-endian platform')
16
+ }
17
+
18
+ export const KEY_SIZE = 16
19
+ export const NONCE_SIZE = 16
20
+ export const TAG_SIZE = 16
21
+ export const HASH_SIZE = 32
22
+
23
+ const encoder = new TextEncoder()
24
+
25
+ function bytes (value, name) {
26
+ if (value === undefined || value === null) return new Uint8Array(0)
27
+ if (typeof value === 'string') return encoder.encode(value)
28
+ if (value instanceof Uint8Array) return value
29
+ if (ArrayBuffer.isView(value)) return new Uint8Array(value.buffer, value.byteOffset, value.byteLength)
30
+ if (value instanceof ArrayBuffer) return new Uint8Array(value)
31
+ throw new TypeError(`${name} must be a string, Uint8Array, ArrayBuffer or typed array`)
32
+ }
33
+
34
+ function fixed (value, size, name) {
35
+ const b = bytes(value, name)
36
+ if (b.length !== size) throw new RangeError(`${name} must be ${size} bytes`)
37
+ return b
38
+ }
39
+
40
+ function tagSize (value) {
41
+ if (!Number.isInteger(value) || value < 4 || value > 16) throw new RangeError('tagLength must be 4..16')
42
+ return value
43
+ }
44
+
45
+ class State {
46
+ constructor () {
47
+ this.w = new Uint32Array(10) // word i: lo = w[2i], hi = w[2i+1]
48
+ this.b = new Uint8Array(this.w.buffer)
49
+ }
50
+
51
+ setWord (i, hi, lo) { this.w[2 * i] = lo; this.w[2 * i + 1] = hi }
52
+
53
+ xor (offset, data, start, length) {
54
+ const b = this.b
55
+ for (let i = 0; i < length; i++) b[offset + i] ^= data[start + i]
56
+ }
57
+
58
+ // The last `rounds` of 12 rounds (12 or 8).
59
+ permute (rounds) {
60
+ const w = this.w
61
+ let x0l = w[0], x0h = w[1], x1l = w[2], x1h = w[3], x2l = w[4], x2h = w[5]
62
+ let x3l = w[6], x3h = w[7], x4l = w[8], x4h = w[9]
63
+ let tl, th, al, ah, bl, bh, cl, ch, dl, dh, el, eh
64
+ for (let i = 12 - rounds; i < 12; i++) {
65
+ x2l ^= ((0x0f - i) << 4) | i
66
+ // Substitution layer
67
+ x0l ^= x4l; x0h ^= x4h
68
+ x4l ^= x3l; x4h ^= x3h
69
+ x2l ^= x1l; x2h ^= x1h
70
+ al = ~x0l & x1l; ah = ~x0h & x1h
71
+ bl = ~x1l & x2l; bh = ~x1h & x2h
72
+ cl = ~x2l & x3l; ch = ~x2h & x3h
73
+ dl = ~x3l & x4l; dh = ~x3h & x4h
74
+ el = ~x4l & x0l; eh = ~x4h & x0h
75
+ x0l ^= bl; x0h ^= bh
76
+ x1l ^= cl; x1h ^= ch
77
+ x2l ^= dl; x2h ^= dh
78
+ x3l ^= el; x3h ^= eh
79
+ x4l ^= al; x4h ^= ah
80
+ x1l ^= x0l; x1h ^= x0h
81
+ x0l ^= x4l; x0h ^= x4h
82
+ x3l ^= x2l; x3h ^= x2h
83
+ x2l = ~x2l; x2h = ~x2h
84
+ // Linear layer: x ^= ror(x, a) ^ ror(x, b)
85
+ // x0: 19, 28
86
+ tl = x0l; th = x0h
87
+ x0l = tl ^ ((tl >>> 19) | (th << 13)) ^ ((tl >>> 28) | (th << 4))
88
+ x0h = th ^ ((th >>> 19) | (tl << 13)) ^ ((th >>> 28) | (tl << 4))
89
+ // x1: 61, 39 (rotations over 32 swap halves: 29, 7)
90
+ tl = x1l; th = x1h
91
+ x1l = tl ^ ((th >>> 29) | (tl << 3)) ^ ((th >>> 7) | (tl << 25))
92
+ x1h = th ^ ((tl >>> 29) | (th << 3)) ^ ((tl >>> 7) | (th << 25))
93
+ // x2: 1, 6
94
+ tl = x2l; th = x2h
95
+ x2l = tl ^ ((tl >>> 1) | (th << 31)) ^ ((tl >>> 6) | (th << 26))
96
+ x2h = th ^ ((th >>> 1) | (tl << 31)) ^ ((th >>> 6) | (tl << 26))
97
+ // x3: 10, 17
98
+ tl = x3l; th = x3h
99
+ x3l = tl ^ ((tl >>> 10) | (th << 22)) ^ ((tl >>> 17) | (th << 15))
100
+ x3h = th ^ ((th >>> 10) | (tl << 22)) ^ ((th >>> 17) | (tl << 15))
101
+ // x4: 7, 41 (41 swaps halves: 9)
102
+ tl = x4l; th = x4h
103
+ x4l = tl ^ ((tl >>> 7) | (th << 25)) ^ ((th >>> 9) | (tl << 23))
104
+ x4h = th ^ ((th >>> 7) | (tl << 25)) ^ ((tl >>> 9) | (th << 23))
105
+ }
106
+ w[0] = x0l; w[1] = x0h; w[2] = x1l; w[3] = x1h; w[4] = x2l; w[5] = x2h
107
+ w[6] = x3l; w[7] = x3h; w[8] = x4l; w[9] = x4h
108
+ }
109
+
110
+ wipe () { this.w.fill(0) }
111
+ }
112
+
113
+ function start (key, nonce, ad) {
114
+ const s = new State()
115
+ s.setWord(0, 0x00001000, 0x808c0001)
116
+ s.b.set(key, 8)
117
+ s.b.set(nonce, 24)
118
+ s.permute(12)
119
+ s.xor(24, key, 0, 16)
120
+ if (ad.length) {
121
+ let i = 0
122
+ for (; ad.length - i >= 16; i += 16) {
123
+ s.xor(0, ad, i, 16)
124
+ s.permute(8)
125
+ }
126
+ s.xor(0, ad, i, ad.length - i)
127
+ s.b[ad.length - i] ^= 0x01
128
+ s.permute(8)
129
+ }
130
+ s.b[39] ^= 0x80
131
+ return s
132
+ }
133
+
134
+ function finish (s, key) {
135
+ s.xor(16, key, 0, 16)
136
+ s.permute(12)
137
+ s.xor(24, key, 0, 16) // tag = s.b[24..39]
138
+ }
139
+
140
+ /**
141
+ * Ascon-AEAD128 encryption.
142
+ * @returns {Uint8Array} ciphertext followed by the tag (tagLength bytes, default 16).
143
+ * The (key, nonce) pair must never be used for two different messages.
144
+ */
145
+ export function encrypt (key, nonce, plaintext, ad, tagLength = TAG_SIZE) {
146
+ key = fixed(key, KEY_SIZE, 'key')
147
+ nonce = fixed(nonce, NONCE_SIZE, 'nonce')
148
+ const m = bytes(plaintext, 'plaintext')
149
+ const t = tagSize(tagLength)
150
+ const s = start(key, nonce, bytes(ad, 'ad'))
151
+ const out = new Uint8Array(m.length + t)
152
+ let i = 0
153
+ for (; m.length - i >= 16; i += 16) {
154
+ s.xor(0, m, i, 16)
155
+ out.set(s.b.subarray(0, 16), i)
156
+ s.permute(8)
157
+ }
158
+ const rest = m.length - i
159
+ s.xor(0, m, i, rest)
160
+ out.set(s.b.subarray(0, rest), i)
161
+ s.b[rest] ^= 0x01
162
+ finish(s, key)
163
+ out.set(s.b.subarray(24, 24 + t), m.length)
164
+ s.wipe()
165
+ return out
166
+ }
167
+
168
+ /**
169
+ * Ascon-AEAD128 decryption of ciphertext followed by a tagLength-byte tag.
170
+ * @returns {Uint8Array|null} plaintext, or null when the tag does not match.
171
+ */
172
+ export function decrypt (key, nonce, sealed, ad, tagLength = TAG_SIZE) {
173
+ key = fixed(key, KEY_SIZE, 'key')
174
+ nonce = fixed(nonce, NONCE_SIZE, 'nonce')
175
+ const c = bytes(sealed, 'ciphertext')
176
+ const t = tagSize(tagLength)
177
+ if (c.length < t) return null
178
+ const length = c.length - t
179
+ const s = start(key, nonce, bytes(ad, 'ad'))
180
+ const out = new Uint8Array(length)
181
+ const b = s.b
182
+ let i = 0
183
+ while (true) {
184
+ const n = Math.min(16, length - i)
185
+ for (let j = 0; j < n; j++) {
186
+ const v = c[i + j]
187
+ out[i + j] = b[j] ^ v
188
+ b[j] = v
189
+ }
190
+ i += n
191
+ if (n < 16) {
192
+ b[n] ^= 0x01
193
+ break
194
+ }
195
+ s.permute(8)
196
+ }
197
+ finish(s, key)
198
+ let diff = 0
199
+ for (let j = 0; j < t; j++) diff |= b[24 + j] ^ c[length + j]
200
+ s.wipe()
201
+ if (diff) {
202
+ out.fill(0)
203
+ return null
204
+ }
205
+ return out
206
+ }
207
+
208
+ // Absorb one padded string in 8-byte blocks, 12 rounds after each block.
209
+ function absorb8 (s, data) {
210
+ let i = 0
211
+ for (; data.length - i >= 8; i += 8) {
212
+ s.xor(0, data, i, 8)
213
+ s.permute(12)
214
+ }
215
+ s.xor(0, data, i, data.length - i)
216
+ s.b[data.length - i] ^= 0x01
217
+ s.permute(12)
218
+ }
219
+
220
+ // One hash/XOF/CXOF run. A customization string (CXOF) goes in after its bit length.
221
+ function hashRun (ivHi, ivLo, data, outLength, custom) {
222
+ const s = new State()
223
+ s.setWord(0, ivHi, ivLo)
224
+ s.permute(12)
225
+ if (custom) {
226
+ const bits = custom.length * 8 // at most 2048: fits the low half
227
+ s.w[0] ^= bits
228
+ s.permute(12)
229
+ absorb8(s, custom)
230
+ }
231
+ absorb8(s, data)
232
+ const out = new Uint8Array(outLength)
233
+ for (let o = 0; o < outLength; o += 8) {
234
+ if (o) s.permute(12)
235
+ out.set(s.b.subarray(0, Math.min(8, outLength - o)), o)
236
+ }
237
+ s.wipe()
238
+ return out
239
+ }
240
+
241
+ /** Ascon-Hash256: 32-byte digest. */
242
+ export function hash256 (data) {
243
+ return hashRun(0x00000801, 0x00cc0002, bytes(data, 'data'), HASH_SIZE)
244
+ }
245
+
246
+ /** Ascon-XOF128: outLength bytes. */
247
+ export function xof128 (data, outLength) {
248
+ if (!Number.isInteger(outLength) || outLength < 0) throw new RangeError('outLength must be a non-negative integer')
249
+ return hashRun(0x00000800, 0x00cc0003, bytes(data, 'data'), outLength)
250
+ }
251
+
252
+ export const CXOF_MAX_CUSTOM = 256
253
+
254
+ /** Ascon-CXOF128: outLength bytes, with a customization string of at most 256 bytes. */
255
+ export function cxof128 (data, outLength, custom) {
256
+ if (!Number.isInteger(outLength) || outLength < 0) throw new RangeError('outLength must be a non-negative integer')
257
+ const z = bytes(custom, 'custom')
258
+ if (z.length > CXOF_MAX_CUSTOM) throw new RangeError(`custom must be at most ${CXOF_MAX_CUSTOM} bytes`)
259
+ return hashRun(0x00000800, 0x00cc0004, bytes(data, 'data'), outLength, z)
260
+ }
261
+
262
+ export default { encrypt, decrypt, hash256, xof128, cxof128, CXOF_MAX_CUSTOM, KEY_SIZE, NONCE_SIZE, TAG_SIZE, HASH_SIZE }