aegis-aead 0.2.1 → 0.2.3

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.
Files changed (55) hide show
  1. package/README.md +29 -22
  2. package/dist/aegis128l.d.ts +33 -33
  3. package/dist/aegis128l.js +225 -235
  4. package/dist/aegis128x.d.ts +35 -35
  5. package/dist/aegis128x.js +348 -282
  6. package/dist/aegis256.d.ts +34 -51
  7. package/dist/aegis256.js +215 -206
  8. package/dist/aegis256x.d.ts +32 -33
  9. package/dist/aegis256x.js +333 -265
  10. package/dist/aes-bs.d.ts +46 -23
  11. package/dist/aes-bs.js +2273 -331
  12. package/dist/index.d.ts +4 -7
  13. package/dist/index.js +4 -7
  14. package/dist/random.d.ts +0 -1
  15. package/dist/random.js +0 -1
  16. package/dist/utils.d.ts +14 -0
  17. package/dist/utils.js +31 -0
  18. package/package.json +6 -6
  19. package/dist/aegis128l-bs.d.ts +0 -194
  20. package/dist/aegis128l-bs.d.ts.map +0 -1
  21. package/dist/aegis128l-bs.js +0 -549
  22. package/dist/aegis128l-bs.js.map +0 -1
  23. package/dist/aegis128l.d.ts.map +0 -1
  24. package/dist/aegis128l.js.map +0 -1
  25. package/dist/aegis128x.d.ts.map +0 -1
  26. package/dist/aegis128x.js.map +0 -1
  27. package/dist/aegis256-bs.d.ts +0 -183
  28. package/dist/aegis256-bs.d.ts.map +0 -1
  29. package/dist/aegis256-bs.js +0 -477
  30. package/dist/aegis256-bs.js.map +0 -1
  31. package/dist/aegis256.d.ts.map +0 -1
  32. package/dist/aegis256.js.map +0 -1
  33. package/dist/aegis256x.d.ts.map +0 -1
  34. package/dist/aegis256x.js.map +0 -1
  35. package/dist/aes-bs.d.ts.map +0 -1
  36. package/dist/aes-bs.js.map +0 -1
  37. package/dist/aes.d.ts +0 -81
  38. package/dist/aes.d.ts.map +0 -1
  39. package/dist/aes.js +0 -232
  40. package/dist/aes.js.map +0 -1
  41. package/dist/index.d.ts.map +0 -1
  42. package/dist/index.js.map +0 -1
  43. package/dist/random.d.ts.map +0 -1
  44. package/dist/random.js.map +0 -1
  45. package/src/aegis128l-bs.ts +0 -709
  46. package/src/aegis128l.ts +0 -653
  47. package/src/aegis128x.ts +0 -932
  48. package/src/aegis256-bs.ts +0 -625
  49. package/src/aegis256.ts +0 -597
  50. package/src/aegis256x.ts +0 -893
  51. package/src/aes-bs.ts +0 -459
  52. package/src/aes.ts +0 -271
  53. package/src/index.ts +0 -126
  54. package/src/random.ts +0 -41
  55. package/src/typed-array.d.ts +0 -18
@@ -1,709 +0,0 @@
1
- /**
2
- * Bitsliced AEGIS-128L implementation.
3
- * Provides constant-time operation by processing all 8 state blocks simultaneously.
4
- */
5
-
6
- import { constantTimeEqual, zeroPad } from "./aes.js";
7
- import {
8
- type AesBlock,
9
- type AesBlocks,
10
- aesRound,
11
- blockFromBytes,
12
- blocksPut,
13
- blocksRotr,
14
- blocksXor,
15
- blockToBytes,
16
- blockXor,
17
- createAesBlock,
18
- createAesBlocks,
19
- pack,
20
- pack04,
21
- unpack,
22
- wordIdx,
23
- } from "./aes-bs.js";
24
- import { randomBytes } from "./random.js";
25
-
26
- const RATE = 32;
27
-
28
- const C0: AesBlock = new Uint32Array([
29
- 0x02010100, 0x0d080503, 0x59372215, 0x6279e990,
30
- ]);
31
- const C1: AesBlock = new Uint32Array([
32
- 0x55183ddb, 0xf12fc26d, 0x42311120, 0xdd28b573,
33
- ]);
34
-
35
- /**
36
- * Bitsliced AEGIS-128L cipher state.
37
- * Uses 8 AES blocks (128 bytes) stored in bitsliced form (32 uint32 words).
38
- */
39
- export class Aegis128LBsState {
40
- private st: AesBlocks;
41
- private st1: AesBlocks;
42
- private tmp0: AesBlock;
43
- private tmp1: AesBlock;
44
- private z0: AesBlock;
45
- private z1: AesBlock;
46
-
47
- constructor() {
48
- this.st = createAesBlocks();
49
- this.st1 = createAesBlocks();
50
- this.tmp0 = createAesBlock();
51
- this.tmp1 = createAesBlock();
52
- this.z0 = createAesBlock();
53
- this.z1 = createAesBlock();
54
- }
55
-
56
- /**
57
- * AEGIS round function: applies AES round to all blocks and rotates.
58
- * st[i] = AES(st[i]) ^ st[(i-1) mod 8]
59
- */
60
- private aegisRound(): void {
61
- const st = this.st;
62
- const st1 = this.st1;
63
-
64
- st1.set(st);
65
- pack(st1);
66
- aesRound(st1);
67
- unpack(st1);
68
-
69
- for (let i = 0; i < 8; i++) {
70
- const prev = (i + 7) % 8;
71
- st[wordIdx(i, 0)] = (st[wordIdx(i, 0)]! ^ st1[wordIdx(prev, 0)]!) >>> 0;
72
- st[wordIdx(i, 1)] = (st[wordIdx(i, 1)]! ^ st1[wordIdx(prev, 1)]!) >>> 0;
73
- st[wordIdx(i, 2)] = (st[wordIdx(i, 2)]! ^ st1[wordIdx(prev, 2)]!) >>> 0;
74
- st[wordIdx(i, 3)] = (st[wordIdx(i, 3)]! ^ st1[wordIdx(prev, 3)]!) >>> 0;
75
- }
76
- }
77
-
78
- /**
79
- * AEGIS round function with constant input (used in packed mode).
80
- */
81
- private aegisRoundPacked(constantInput: AesBlocks): void {
82
- const st = this.st;
83
- const st1 = this.st1;
84
-
85
- st1.set(st);
86
- aesRound(st1);
87
- blocksRotr(st1);
88
- blocksXor(st, st1);
89
- blocksXor(st, constantInput);
90
- }
91
-
92
- /**
93
- * Pack constant input for blocks 0 and 4.
94
- */
95
- private packConstantInput(out: AesBlocks, m0: AesBlock, m1: AesBlock): void {
96
- out.fill(0);
97
- blocksPut(out, m0, 0);
98
- blocksPut(out, m1, 4);
99
- pack04(out);
100
- }
101
-
102
- /**
103
- * Absorb rate: XOR message blocks into state positions 0 and 4.
104
- */
105
- private absorbRate(m0: AesBlock, m1: AesBlock): void {
106
- const st = this.st;
107
- st[wordIdx(0, 0)] = (st[wordIdx(0, 0)]! ^ m0[0]!) >>> 0;
108
- st[wordIdx(0, 1)] = (st[wordIdx(0, 1)]! ^ m0[1]!) >>> 0;
109
- st[wordIdx(0, 2)] = (st[wordIdx(0, 2)]! ^ m0[2]!) >>> 0;
110
- st[wordIdx(0, 3)] = (st[wordIdx(0, 3)]! ^ m0[3]!) >>> 0;
111
-
112
- st[wordIdx(4, 0)] = (st[wordIdx(4, 0)]! ^ m1[0]!) >>> 0;
113
- st[wordIdx(4, 1)] = (st[wordIdx(4, 1)]! ^ m1[1]!) >>> 0;
114
- st[wordIdx(4, 2)] = (st[wordIdx(4, 2)]! ^ m1[2]!) >>> 0;
115
- st[wordIdx(4, 3)] = (st[wordIdx(4, 3)]! ^ m1[3]!) >>> 0;
116
- }
117
-
118
- /**
119
- * Update state with two message blocks.
120
- */
121
- private update(m0: AesBlock, m1: AesBlock): void {
122
- this.aegisRound();
123
- this.absorbRate(m0, m1);
124
- }
125
-
126
- /**
127
- * Initializes the state with a key and nonce.
128
- * @param key - 16-byte encryption key
129
- * @param nonce - 16-byte nonce (must be unique per message)
130
- */
131
- init(key: Uint8Array, nonce: Uint8Array): void {
132
- const k = createAesBlock();
133
- const n = createAesBlock();
134
- const kn = createAesBlock();
135
- const kc0 = createAesBlock();
136
- const kc1 = createAesBlock();
137
-
138
- blockFromBytes(k, key);
139
- blockFromBytes(n, nonce);
140
- blockXor(kn, k, n);
141
- blockXor(kc0, k, C0);
142
- blockXor(kc1, k, C1);
143
-
144
- blocksPut(this.st, kn, 0);
145
- blocksPut(this.st, C1, 1);
146
- blocksPut(this.st, C0, 2);
147
- blocksPut(this.st, C1, 3);
148
- blocksPut(this.st, kn, 4);
149
- blocksPut(this.st, kc0, 5);
150
- blocksPut(this.st, kc1, 6);
151
- blocksPut(this.st, kc0, 7);
152
-
153
- const constantInput = createAesBlocks();
154
- this.packConstantInput(constantInput, n, k);
155
- pack(this.st);
156
- for (let i = 0; i < 10; i++) {
157
- this.aegisRoundPacked(constantInput);
158
- }
159
- unpack(this.st);
160
- }
161
-
162
- /**
163
- * Absorbs a 32-byte associated data block into the state.
164
- * @param ai - 32-byte associated data block
165
- */
166
- absorb(ai: Uint8Array): void {
167
- const msg0 = this.tmp0;
168
- const msg1 = this.tmp1;
169
- blockFromBytes(msg0, ai.subarray(0, 16));
170
- blockFromBytes(msg1, ai.subarray(16, 32));
171
- this.update(msg0, msg1);
172
- }
173
-
174
- /**
175
- * Encrypts a 32-byte plaintext block and writes to output buffer.
176
- * @param xi - 32-byte plaintext block
177
- * @param out - 32-byte output buffer
178
- */
179
- encTo(xi: Uint8Array, out: Uint8Array): void {
180
- const st = this.st;
181
- const z0 = this.z0;
182
- const z1 = this.z1;
183
- const t0 = this.tmp0;
184
- const t1 = this.tmp1;
185
-
186
- for (let i = 0; i < 4; i++) {
187
- z0[i] =
188
- (st[wordIdx(6, i)]! ^
189
- st[wordIdx(1, i)]! ^
190
- (st[wordIdx(2, i)]! & st[wordIdx(3, i)]!)) >>>
191
- 0;
192
- }
193
- for (let i = 0; i < 4; i++) {
194
- z1[i] =
195
- (st[wordIdx(2, i)]! ^
196
- st[wordIdx(5, i)]! ^
197
- (st[wordIdx(6, i)]! & st[wordIdx(7, i)]!)) >>>
198
- 0;
199
- }
200
-
201
- blockFromBytes(t0, xi.subarray(0, 16));
202
- blockFromBytes(t1, xi.subarray(16, 32));
203
-
204
- const out0 = createAesBlock();
205
- const out1 = createAesBlock();
206
- blockXor(out0, t0, z0);
207
- blockXor(out1, t1, z1);
208
-
209
- blockToBytes(out.subarray(0, 16), out0);
210
- blockToBytes(out.subarray(16, 32), out1);
211
-
212
- this.update(t0, t1);
213
- }
214
-
215
- /**
216
- * Encrypts a 32-byte plaintext block.
217
- * @param xi - 32-byte plaintext block
218
- * @returns 32-byte ciphertext block
219
- */
220
- enc(xi: Uint8Array): Uint8Array {
221
- const out = new Uint8Array(32);
222
- this.encTo(xi, out);
223
- return out;
224
- }
225
-
226
- /**
227
- * Decrypts a 32-byte ciphertext block and writes to output buffer.
228
- * @param ci - 32-byte ciphertext block
229
- * @param out - 32-byte output buffer
230
- */
231
- decTo(ci: Uint8Array, out: Uint8Array): void {
232
- const st = this.st;
233
- const msg0 = this.tmp0;
234
- const msg1 = this.tmp1;
235
-
236
- blockFromBytes(msg0, ci.subarray(0, 16));
237
- blockFromBytes(msg1, ci.subarray(16, 32));
238
-
239
- for (let i = 0; i < 4; i++) {
240
- msg0[i] =
241
- (msg0[i]! ^
242
- st[wordIdx(6, i)]! ^
243
- st[wordIdx(1, i)]! ^
244
- (st[wordIdx(2, i)]! & st[wordIdx(3, i)]!)) >>>
245
- 0;
246
- }
247
- for (let i = 0; i < 4; i++) {
248
- msg1[i] =
249
- (msg1[i]! ^
250
- st[wordIdx(2, i)]! ^
251
- st[wordIdx(5, i)]! ^
252
- (st[wordIdx(6, i)]! & st[wordIdx(7, i)]!)) >>>
253
- 0;
254
- }
255
-
256
- this.update(msg0, msg1);
257
-
258
- blockToBytes(out.subarray(0, 16), msg0);
259
- blockToBytes(out.subarray(16, 32), msg1);
260
- }
261
-
262
- /**
263
- * Decrypts a 32-byte ciphertext block.
264
- * @param ci - 32-byte ciphertext block
265
- * @returns 32-byte plaintext block
266
- */
267
- dec(ci: Uint8Array): Uint8Array {
268
- const out = new Uint8Array(32);
269
- this.decTo(ci, out);
270
- return out;
271
- }
272
-
273
- /**
274
- * Encrypts a 32-byte plaintext block in-place.
275
- * @param block - 32-byte buffer (plaintext in, ciphertext out)
276
- */
277
- encInPlace(block: Uint8Array): void {
278
- this.encTo(block, block);
279
- }
280
-
281
- /**
282
- * Decrypts a 32-byte ciphertext block in-place.
283
- * @param block - 32-byte buffer (ciphertext in, plaintext out)
284
- */
285
- decInPlace(block: Uint8Array): void {
286
- this.decTo(block, block);
287
- }
288
-
289
- /**
290
- * Decrypts a partial (final) ciphertext block smaller than 32 bytes.
291
- * @param cn - Partial ciphertext block (1-31 bytes)
292
- * @returns Decrypted plaintext of the same length
293
- */
294
- decPartial(cn: Uint8Array): Uint8Array {
295
- const st = this.st;
296
- const msg0 = this.tmp0;
297
- const msg1 = this.tmp1;
298
-
299
- const padded = zeroPad(cn, RATE);
300
- blockFromBytes(msg0, padded.subarray(0, 16));
301
- blockFromBytes(msg1, padded.subarray(16, 32));
302
-
303
- for (let i = 0; i < 4; i++) {
304
- msg0[i] =
305
- (msg0[i]! ^
306
- st[wordIdx(6, i)]! ^
307
- st[wordIdx(1, i)]! ^
308
- (st[wordIdx(2, i)]! & st[wordIdx(3, i)]!)) >>>
309
- 0;
310
- }
311
- for (let i = 0; i < 4; i++) {
312
- msg1[i] =
313
- (msg1[i]! ^
314
- st[wordIdx(2, i)]! ^
315
- st[wordIdx(5, i)]! ^
316
- (st[wordIdx(6, i)]! & st[wordIdx(7, i)]!)) >>>
317
- 0;
318
- }
319
-
320
- const pad = new Uint8Array(RATE);
321
- blockToBytes(pad.subarray(0, 16), msg0);
322
- blockToBytes(pad.subarray(16, 32), msg1);
323
-
324
- const xn = new Uint8Array(pad.subarray(0, cn.length));
325
-
326
- pad.fill(0, cn.length);
327
- blockFromBytes(msg0, pad.subarray(0, 16));
328
- blockFromBytes(msg1, pad.subarray(16, 32));
329
-
330
- this.aegisRound();
331
- this.absorbRate(msg0, msg1);
332
-
333
- return xn;
334
- }
335
-
336
- /**
337
- * Finalizes encryption/decryption and produces an authentication tag.
338
- * @param adLen - Associated data length in bytes
339
- * @param msgLen - Message length in bytes
340
- * @param tagLen - Tag length (16 or 32 bytes)
341
- * @returns Authentication tag
342
- */
343
- finalize(adLen: number, msgLen: number, tagLen: 16 | 32 = 16): Uint8Array {
344
- const st = this.st;
345
- const tmp = this.tmp0;
346
-
347
- tmp[0] = ((adLen * 8) & 0xffffffff) >>> 0;
348
- tmp[1] = Math.floor((adLen * 8) / 0x100000000) >>> 0;
349
- tmp[2] = ((msgLen * 8) & 0xffffffff) >>> 0;
350
- tmp[3] = Math.floor((msgLen * 8) / 0x100000000) >>> 0;
351
-
352
- tmp[0] = (tmp[0]! ^ st[wordIdx(2, 0)]!) >>> 0;
353
- tmp[1] = (tmp[1]! ^ st[wordIdx(2, 1)]!) >>> 0;
354
- tmp[2] = (tmp[2]! ^ st[wordIdx(2, 2)]!) >>> 0;
355
- tmp[3] = (tmp[3]! ^ st[wordIdx(2, 3)]!) >>> 0;
356
-
357
- const constantInput = createAesBlocks();
358
- this.packConstantInput(constantInput, tmp, tmp);
359
- pack(this.st);
360
- for (let i = 0; i < 7; i++) {
361
- this.aegisRoundPacked(constantInput);
362
- }
363
- unpack(this.st);
364
-
365
- if (tagLen === 16) {
366
- const tag = new Uint8Array(16);
367
- const tagBlock = createAesBlock();
368
- for (let i = 0; i < 4; i++) {
369
- tagBlock[i] =
370
- (st[wordIdx(0, i)]! ^
371
- st[wordIdx(1, i)]! ^
372
- st[wordIdx(2, i)]! ^
373
- st[wordIdx(3, i)]! ^
374
- st[wordIdx(4, i)]! ^
375
- st[wordIdx(5, i)]! ^
376
- st[wordIdx(6, i)]!) >>>
377
- 0;
378
- }
379
- blockToBytes(tag, tagBlock);
380
- return tag;
381
- } else {
382
- const tag = new Uint8Array(32);
383
- const tagBlock0 = createAesBlock();
384
- const tagBlock1 = createAesBlock();
385
- for (let i = 0; i < 4; i++) {
386
- tagBlock0[i] =
387
- (st[wordIdx(0, i)]! ^
388
- st[wordIdx(1, i)]! ^
389
- st[wordIdx(2, i)]! ^
390
- st[wordIdx(3, i)]!) >>>
391
- 0;
392
- }
393
- for (let i = 0; i < 4; i++) {
394
- tagBlock1[i] =
395
- (st[wordIdx(4, i)]! ^
396
- st[wordIdx(5, i)]! ^
397
- st[wordIdx(6, i)]! ^
398
- st[wordIdx(7, i)]!) >>>
399
- 0;
400
- }
401
- blockToBytes(tag.subarray(0, 16), tagBlock0);
402
- blockToBytes(tag.subarray(16, 32), tagBlock1);
403
- return tag;
404
- }
405
- }
406
- }
407
-
408
- /**
409
- * Encrypts a message using bitsliced AEGIS-128L (detached mode).
410
- * @param msg - Plaintext message
411
- * @param ad - Associated data (authenticated but not encrypted)
412
- * @param key - 16-byte encryption key
413
- * @param nonce - 16-byte nonce (must be unique per message with the same key)
414
- * @param tagLen - Authentication tag length: 16 or 32 bytes (default: 16)
415
- * @returns Object containing ciphertext and authentication tag separately
416
- */
417
- export function aegis128LBsEncryptDetached(
418
- msg: Uint8Array,
419
- ad: Uint8Array,
420
- key: Uint8Array,
421
- nonce: Uint8Array,
422
- tagLen: 16 | 32 = 16,
423
- ): { ciphertext: Uint8Array; tag: Uint8Array } {
424
- const state = new Aegis128LBsState();
425
- state.init(key, nonce);
426
-
427
- const adPadded = zeroPad(ad, RATE);
428
- for (let i = 0; i + RATE <= adPadded.length; i += RATE) {
429
- state.absorb(adPadded.subarray(i, i + RATE));
430
- }
431
-
432
- const ciphertext = new Uint8Array(msg.length);
433
- const fullBlocks = Math.floor(msg.length / RATE) * RATE;
434
-
435
- for (let i = 0; i < fullBlocks; i += RATE) {
436
- state.encTo(msg.subarray(i, i + RATE), ciphertext.subarray(i, i + RATE));
437
- }
438
-
439
- if (msg.length > fullBlocks) {
440
- const lastBlock = zeroPad(msg.subarray(fullBlocks), RATE);
441
- const encBlock = state.enc(lastBlock);
442
- ciphertext.set(encBlock.subarray(0, msg.length - fullBlocks), fullBlocks);
443
- }
444
-
445
- const tag = state.finalize(ad.length, msg.length, tagLen);
446
-
447
- return { ciphertext, tag };
448
- }
449
-
450
- /**
451
- * Decrypts a message using bitsliced AEGIS-128L (detached mode).
452
- * @param ct - Ciphertext
453
- * @param tag - Authentication tag (16 or 32 bytes)
454
- * @param ad - Associated data (must match what was used during encryption)
455
- * @param key - 16-byte encryption key
456
- * @param nonce - 16-byte nonce (must match what was used during encryption)
457
- * @returns Decrypted plaintext, or null if authentication fails
458
- */
459
- export function aegis128LBsDecryptDetached(
460
- ct: Uint8Array,
461
- tag: Uint8Array,
462
- ad: Uint8Array,
463
- key: Uint8Array,
464
- nonce: Uint8Array,
465
- ): Uint8Array | null {
466
- const tagLen = tag.length as 16 | 32;
467
- const state = new Aegis128LBsState();
468
- state.init(key, nonce);
469
-
470
- const adPadded = zeroPad(ad, RATE);
471
- for (let i = 0; i + RATE <= adPadded.length; i += RATE) {
472
- state.absorb(adPadded.subarray(i, i + RATE));
473
- }
474
-
475
- const msg = new Uint8Array(ct.length);
476
- const fullBlocks = Math.floor(ct.length / RATE) * RATE;
477
-
478
- for (let i = 0; i < fullBlocks; i += RATE) {
479
- state.decTo(ct.subarray(i, i + RATE), msg.subarray(i, i + RATE));
480
- }
481
-
482
- if (ct.length > fullBlocks) {
483
- msg.set(state.decPartial(ct.subarray(fullBlocks)), fullBlocks);
484
- }
485
-
486
- const expectedTag = state.finalize(ad.length, msg.length, tagLen);
487
-
488
- if (!constantTimeEqual(tag, expectedTag)) {
489
- msg.fill(0);
490
- return null;
491
- }
492
-
493
- return msg;
494
- }
495
-
496
- /**
497
- * Encrypts a message in-place using bitsliced AEGIS-128L (detached mode).
498
- * The input buffer is modified to contain the ciphertext.
499
- * @param data - Buffer containing plaintext (will be overwritten with ciphertext)
500
- * @param ad - Associated data (authenticated but not encrypted)
501
- * @param key - 16-byte encryption key
502
- * @param nonce - 16-byte nonce (must be unique per message with the same key)
503
- * @param tagLen - Authentication tag length: 16 or 32 bytes (default: 16)
504
- * @returns Authentication tag
505
- */
506
- export function aegis128LBsEncryptDetachedInPlace(
507
- data: Uint8Array,
508
- ad: Uint8Array,
509
- key: Uint8Array,
510
- nonce: Uint8Array,
511
- tagLen: 16 | 32 = 16,
512
- ): Uint8Array {
513
- const state = new Aegis128LBsState();
514
- state.init(key, nonce);
515
-
516
- const adPadded = zeroPad(ad, RATE);
517
- for (let i = 0; i + RATE <= adPadded.length; i += RATE) {
518
- state.absorb(adPadded.subarray(i, i + RATE));
519
- }
520
-
521
- const msgLen = data.length;
522
- const fullBlocksLen = Math.floor(msgLen / RATE) * RATE;
523
-
524
- for (let i = 0; i < fullBlocksLen; i += RATE) {
525
- state.encInPlace(data.subarray(i, i + RATE));
526
- }
527
-
528
- if (msgLen > fullBlocksLen) {
529
- const lastPartial = data.subarray(fullBlocksLen);
530
- const lastBlock = zeroPad(lastPartial, RATE);
531
- const encBlock = state.enc(lastBlock);
532
- lastPartial.set(encBlock.subarray(0, lastPartial.length));
533
- }
534
-
535
- return state.finalize(ad.length, msgLen, tagLen);
536
- }
537
-
538
- /**
539
- * Decrypts a message in-place using bitsliced AEGIS-128L (detached mode).
540
- * The input buffer is modified to contain the plaintext (or zeroed on failure).
541
- * @param data - Buffer containing ciphertext (will be overwritten with plaintext)
542
- * @param tag - Authentication tag (16 or 32 bytes)
543
- * @param ad - Associated data (must match what was used during encryption)
544
- * @param key - 16-byte encryption key
545
- * @param nonce - 16-byte nonce (must match what was used during encryption)
546
- * @returns True if authentication succeeds, false otherwise
547
- */
548
- export function aegis128LBsDecryptDetachedInPlace(
549
- data: Uint8Array,
550
- tag: Uint8Array,
551
- ad: Uint8Array,
552
- key: Uint8Array,
553
- nonce: Uint8Array,
554
- ): boolean {
555
- const tagLen = tag.length as 16 | 32;
556
- const state = new Aegis128LBsState();
557
- state.init(key, nonce);
558
-
559
- const adPadded = zeroPad(ad, RATE);
560
- for (let i = 0; i + RATE <= adPadded.length; i += RATE) {
561
- state.absorb(adPadded.subarray(i, i + RATE));
562
- }
563
-
564
- const msgLen = data.length;
565
- const fullBlocksLen = Math.floor(msgLen / RATE) * RATE;
566
-
567
- for (let i = 0; i < fullBlocksLen; i += RATE) {
568
- state.decInPlace(data.subarray(i, i + RATE));
569
- }
570
-
571
- if (msgLen > fullBlocksLen) {
572
- const lastPartial = data.subarray(fullBlocksLen);
573
- const decrypted = state.decPartial(lastPartial);
574
- lastPartial.set(decrypted);
575
- }
576
-
577
- const expectedTag = state.finalize(ad.length, msgLen, tagLen);
578
-
579
- if (!constantTimeEqual(tag, expectedTag)) {
580
- data.fill(0);
581
- return false;
582
- }
583
-
584
- return true;
585
- }
586
-
587
- export const AEGIS_128L_BS_NONCE_SIZE = 16;
588
- export const AEGIS_128L_BS_KEY_SIZE = 16;
589
-
590
- /**
591
- * Encrypts a message using bitsliced AEGIS-128L.
592
- * Returns a single buffer containing nonce || ciphertext || tag.
593
- * @param msg - Plaintext message
594
- * @param ad - Associated data (authenticated but not encrypted)
595
- * @param key - 16-byte encryption key
596
- * @param nonce - 16-byte nonce (optional, generates random nonce if not provided)
597
- * @param tagLen - Authentication tag length: 16 or 32 bytes (default: 16)
598
- * @returns Concatenated nonce || ciphertext || tag
599
- */
600
- export function aegis128LBsEncrypt(
601
- msg: Uint8Array,
602
- ad: Uint8Array,
603
- key: Uint8Array,
604
- nonce: Uint8Array | null = null,
605
- tagLen: 16 | 32 = 16,
606
- ): Uint8Array {
607
- const actualNonce = nonce ?? randomBytes(AEGIS_128L_BS_NONCE_SIZE);
608
- const { ciphertext, tag } = aegis128LBsEncryptDetached(
609
- msg,
610
- ad,
611
- key,
612
- actualNonce,
613
- tagLen,
614
- );
615
-
616
- const result = new Uint8Array(
617
- AEGIS_128L_BS_NONCE_SIZE + ciphertext.length + tagLen,
618
- );
619
- result.set(actualNonce, 0);
620
- result.set(ciphertext, AEGIS_128L_BS_NONCE_SIZE);
621
- result.set(tag, AEGIS_128L_BS_NONCE_SIZE + ciphertext.length);
622
-
623
- return result;
624
- }
625
-
626
- /**
627
- * Decrypts a message using bitsliced AEGIS-128L.
628
- * Expects input as nonce || ciphertext || tag.
629
- * @param sealed - Concatenated nonce || ciphertext || tag
630
- * @param ad - Associated data (must match what was used during encryption)
631
- * @param key - 16-byte encryption key
632
- * @param tagLen - Authentication tag length: 16 or 32 bytes (default: 16)
633
- * @returns Decrypted plaintext, or null if authentication fails
634
- */
635
- export function aegis128LBsDecrypt(
636
- sealed: Uint8Array,
637
- ad: Uint8Array,
638
- key: Uint8Array,
639
- tagLen: 16 | 32 = 16,
640
- ): Uint8Array | null {
641
- const nonceSize = AEGIS_128L_BS_NONCE_SIZE;
642
- if (sealed.length < nonceSize + tagLen) {
643
- return null;
644
- }
645
- const nonce = sealed.subarray(0, nonceSize);
646
- const ct = sealed.subarray(nonceSize, sealed.length - tagLen);
647
- const tag = sealed.subarray(sealed.length - tagLen);
648
- return aegis128LBsDecryptDetached(ct, tag, ad, key, nonce);
649
- }
650
-
651
- /**
652
- * Computes a MAC (Message Authentication Code) using bitsliced AEGIS-128L.
653
- * @param data - Data to authenticate
654
- * @param key - 16-byte key
655
- * @param nonce - 16-byte nonce (optional, uses zero nonce if null)
656
- * @param tagLen - Tag length: 16 or 32 bytes (default: 16)
657
- * @returns Authentication tag
658
- */
659
- export function aegis128LBsMac(
660
- data: Uint8Array,
661
- key: Uint8Array,
662
- nonce: Uint8Array | null = null,
663
- tagLen: 16 | 32 = 16,
664
- ): Uint8Array {
665
- const state = new Aegis128LBsState();
666
- state.init(key, nonce ?? new Uint8Array(16));
667
-
668
- const dataPadded = zeroPad(data, RATE);
669
- for (let i = 0; i + RATE <= dataPadded.length; i += RATE) {
670
- state.absorb(dataPadded.subarray(i, i + RATE));
671
- }
672
-
673
- return state.finalize(data.length, tagLen, tagLen);
674
- }
675
-
676
- /**
677
- * Verifies a MAC computed using bitsliced AEGIS-128L.
678
- * @param data - Data to verify
679
- * @param tag - Expected authentication tag (16 or 32 bytes)
680
- * @param key - 16-byte key
681
- * @param nonce - 16-byte nonce (optional, uses zero nonce if null)
682
- * @returns True if the tag is valid, false otherwise
683
- */
684
- export function aegis128LBsMacVerify(
685
- data: Uint8Array,
686
- tag: Uint8Array,
687
- key: Uint8Array,
688
- nonce: Uint8Array | null = null,
689
- ): boolean {
690
- const tagLen = tag.length as 16 | 32;
691
- const expectedTag = aegis128LBsMac(data, key, nonce, tagLen);
692
- return constantTimeEqual(tag, expectedTag);
693
- }
694
-
695
- /**
696
- * Generates a random 16-byte key for bitsliced AEGIS-128L.
697
- * @returns 16-byte encryption key
698
- */
699
- export function aegis128LBsCreateKey(): Uint8Array {
700
- return randomBytes(AEGIS_128L_BS_KEY_SIZE);
701
- }
702
-
703
- /**
704
- * Generates a random 16-byte nonce for bitsliced AEGIS-128L.
705
- * @returns 16-byte nonce
706
- */
707
- export function aegis128LBsCreateNonce(): Uint8Array {
708
- return randomBytes(AEGIS_128L_BS_NONCE_SIZE);
709
- }