aegis-aead 0.1.1 → 0.2.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.
Files changed (48) hide show
  1. package/README.md +241 -60
  2. package/dist/aegis128l-bs.d.ts +162 -0
  3. package/dist/aegis128l-bs.d.ts.map +1 -0
  4. package/dist/aegis128l-bs.js +470 -0
  5. package/dist/aegis128l-bs.js.map +1 -0
  6. package/dist/aegis128l.d.ts +42 -5
  7. package/dist/aegis128l.d.ts.map +1 -1
  8. package/dist/aegis128l.js +79 -5
  9. package/dist/aegis128l.js.map +1 -1
  10. package/dist/aegis128x.d.ts +67 -12
  11. package/dist/aegis128x.d.ts.map +1 -1
  12. package/dist/aegis128x.js +102 -9
  13. package/dist/aegis128x.js.map +1 -1
  14. package/dist/aegis256-bs.d.ts +151 -0
  15. package/dist/aegis256-bs.d.ts.map +1 -0
  16. package/dist/aegis256-bs.js +398 -0
  17. package/dist/aegis256-bs.js.map +1 -0
  18. package/dist/aegis256.d.ts +42 -5
  19. package/dist/aegis256.d.ts.map +1 -1
  20. package/dist/aegis256.js +79 -5
  21. package/dist/aegis256.js.map +1 -1
  22. package/dist/aegis256x.d.ts +67 -12
  23. package/dist/aegis256x.d.ts.map +1 -1
  24. package/dist/aegis256x.js +102 -9
  25. package/dist/aegis256x.js.map +1 -1
  26. package/dist/aes-bs.d.ts +71 -0
  27. package/dist/aes-bs.d.ts.map +1 -0
  28. package/dist/aes-bs.js +399 -0
  29. package/dist/aes-bs.js.map +1 -0
  30. package/dist/index.d.ts +6 -4
  31. package/dist/index.d.ts.map +1 -1
  32. package/dist/index.js +6 -4
  33. package/dist/index.js.map +1 -1
  34. package/dist/random.d.ts +22 -0
  35. package/dist/random.d.ts.map +1 -0
  36. package/dist/random.js +36 -0
  37. package/dist/random.js.map +1 -0
  38. package/package.json +1 -1
  39. package/src/aegis128l-bs.ts +602 -0
  40. package/src/aegis128l.ts +112 -5
  41. package/src/aegis128x.ts +174 -15
  42. package/src/aegis256-bs.ts +518 -0
  43. package/src/aegis256.ts +112 -5
  44. package/src/aegis256x.ts +174 -15
  45. package/src/aes-bs.ts +459 -0
  46. package/src/index.ts +66 -0
  47. package/src/random.ts +41 -0
  48. package/README.md~ +0 -154
@@ -0,0 +1,602 @@
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
+ * Decrypts a partial (final) ciphertext block smaller than 32 bytes.
275
+ * @param cn - Partial ciphertext block (1-31 bytes)
276
+ * @returns Decrypted plaintext of the same length
277
+ */
278
+ decPartial(cn: Uint8Array): Uint8Array {
279
+ const st = this.st;
280
+ const msg0 = this.tmp0;
281
+ const msg1 = this.tmp1;
282
+
283
+ const padded = zeroPad(cn, RATE);
284
+ blockFromBytes(msg0, padded.subarray(0, 16));
285
+ blockFromBytes(msg1, padded.subarray(16, 32));
286
+
287
+ for (let i = 0; i < 4; i++) {
288
+ msg0[i] =
289
+ (msg0[i]! ^
290
+ st[wordIdx(6, i)]! ^
291
+ st[wordIdx(1, i)]! ^
292
+ (st[wordIdx(2, i)]! & st[wordIdx(3, i)]!)) >>>
293
+ 0;
294
+ }
295
+ for (let i = 0; i < 4; i++) {
296
+ msg1[i] =
297
+ (msg1[i]! ^
298
+ st[wordIdx(2, i)]! ^
299
+ st[wordIdx(5, i)]! ^
300
+ (st[wordIdx(6, i)]! & st[wordIdx(7, i)]!)) >>>
301
+ 0;
302
+ }
303
+
304
+ const pad = new Uint8Array(RATE);
305
+ blockToBytes(pad.subarray(0, 16), msg0);
306
+ blockToBytes(pad.subarray(16, 32), msg1);
307
+
308
+ const xn = new Uint8Array(pad.subarray(0, cn.length));
309
+
310
+ pad.fill(0, cn.length);
311
+ blockFromBytes(msg0, pad.subarray(0, 16));
312
+ blockFromBytes(msg1, pad.subarray(16, 32));
313
+
314
+ this.aegisRound();
315
+ this.absorbRate(msg0, msg1);
316
+
317
+ return xn;
318
+ }
319
+
320
+ /**
321
+ * Finalizes encryption/decryption and produces an authentication tag.
322
+ * @param adLen - Associated data length in bytes
323
+ * @param msgLen - Message length in bytes
324
+ * @param tagLen - Tag length (16 or 32 bytes)
325
+ * @returns Authentication tag
326
+ */
327
+ finalize(adLen: number, msgLen: number, tagLen: 16 | 32 = 16): Uint8Array {
328
+ const st = this.st;
329
+ const tmp = this.tmp0;
330
+
331
+ tmp[0] = ((adLen * 8) & 0xffffffff) >>> 0;
332
+ tmp[1] = Math.floor((adLen * 8) / 0x100000000) >>> 0;
333
+ tmp[2] = ((msgLen * 8) & 0xffffffff) >>> 0;
334
+ tmp[3] = Math.floor((msgLen * 8) / 0x100000000) >>> 0;
335
+
336
+ tmp[0] = (tmp[0]! ^ st[wordIdx(2, 0)]!) >>> 0;
337
+ tmp[1] = (tmp[1]! ^ st[wordIdx(2, 1)]!) >>> 0;
338
+ tmp[2] = (tmp[2]! ^ st[wordIdx(2, 2)]!) >>> 0;
339
+ tmp[3] = (tmp[3]! ^ st[wordIdx(2, 3)]!) >>> 0;
340
+
341
+ const constantInput = createAesBlocks();
342
+ this.packConstantInput(constantInput, tmp, tmp);
343
+ pack(this.st);
344
+ for (let i = 0; i < 7; i++) {
345
+ this.aegisRoundPacked(constantInput);
346
+ }
347
+ unpack(this.st);
348
+
349
+ if (tagLen === 16) {
350
+ const tag = new Uint8Array(16);
351
+ const tagBlock = createAesBlock();
352
+ for (let i = 0; i < 4; i++) {
353
+ tagBlock[i] =
354
+ (st[wordIdx(0, i)]! ^
355
+ st[wordIdx(1, i)]! ^
356
+ st[wordIdx(2, i)]! ^
357
+ st[wordIdx(3, i)]! ^
358
+ st[wordIdx(4, i)]! ^
359
+ st[wordIdx(5, i)]! ^
360
+ st[wordIdx(6, i)]!) >>>
361
+ 0;
362
+ }
363
+ blockToBytes(tag, tagBlock);
364
+ return tag;
365
+ } else {
366
+ const tag = new Uint8Array(32);
367
+ const tagBlock0 = createAesBlock();
368
+ const tagBlock1 = createAesBlock();
369
+ for (let i = 0; i < 4; i++) {
370
+ tagBlock0[i] =
371
+ (st[wordIdx(0, i)]! ^
372
+ st[wordIdx(1, i)]! ^
373
+ st[wordIdx(2, i)]! ^
374
+ st[wordIdx(3, i)]!) >>>
375
+ 0;
376
+ }
377
+ for (let i = 0; i < 4; i++) {
378
+ tagBlock1[i] =
379
+ (st[wordIdx(4, i)]! ^
380
+ st[wordIdx(5, i)]! ^
381
+ st[wordIdx(6, i)]! ^
382
+ st[wordIdx(7, i)]!) >>>
383
+ 0;
384
+ }
385
+ blockToBytes(tag.subarray(0, 16), tagBlock0);
386
+ blockToBytes(tag.subarray(16, 32), tagBlock1);
387
+ return tag;
388
+ }
389
+ }
390
+ }
391
+
392
+ /**
393
+ * Encrypts a message using bitsliced AEGIS-128L (detached mode).
394
+ * @param msg - Plaintext message
395
+ * @param ad - Associated data (authenticated but not encrypted)
396
+ * @param key - 16-byte encryption key
397
+ * @param nonce - 16-byte nonce (must be unique per message with the same key)
398
+ * @param tagLen - Authentication tag length: 16 or 32 bytes (default: 16)
399
+ * @returns Object containing ciphertext and authentication tag separately
400
+ */
401
+ export function aegis128LBsEncryptDetached(
402
+ msg: Uint8Array,
403
+ ad: Uint8Array,
404
+ key: Uint8Array,
405
+ nonce: Uint8Array,
406
+ tagLen: 16 | 32 = 16,
407
+ ): { ciphertext: Uint8Array; tag: Uint8Array } {
408
+ const state = new Aegis128LBsState();
409
+ state.init(key, nonce);
410
+
411
+ const adPadded = zeroPad(ad, RATE);
412
+ for (let i = 0; i + RATE <= adPadded.length; i += RATE) {
413
+ state.absorb(adPadded.subarray(i, i + RATE));
414
+ }
415
+
416
+ const ciphertext = new Uint8Array(msg.length);
417
+ const fullBlocks = Math.floor(msg.length / RATE) * RATE;
418
+
419
+ for (let i = 0; i < fullBlocks; i += RATE) {
420
+ state.encTo(msg.subarray(i, i + RATE), ciphertext.subarray(i, i + RATE));
421
+ }
422
+
423
+ if (msg.length > fullBlocks) {
424
+ const lastBlock = zeroPad(msg.subarray(fullBlocks), RATE);
425
+ const encBlock = state.enc(lastBlock);
426
+ ciphertext.set(encBlock.subarray(0, msg.length - fullBlocks), fullBlocks);
427
+ }
428
+
429
+ const tag = state.finalize(ad.length, msg.length, tagLen);
430
+
431
+ return { ciphertext, tag };
432
+ }
433
+
434
+ /**
435
+ * Decrypts a message using bitsliced AEGIS-128L (detached mode).
436
+ * @param ct - Ciphertext
437
+ * @param tag - Authentication tag (16 or 32 bytes)
438
+ * @param ad - Associated data (must match what was used during encryption)
439
+ * @param key - 16-byte encryption key
440
+ * @param nonce - 16-byte nonce (must match what was used during encryption)
441
+ * @returns Decrypted plaintext, or null if authentication fails
442
+ */
443
+ export function aegis128LBsDecryptDetached(
444
+ ct: Uint8Array,
445
+ tag: Uint8Array,
446
+ ad: Uint8Array,
447
+ key: Uint8Array,
448
+ nonce: Uint8Array,
449
+ ): Uint8Array | null {
450
+ const tagLen = tag.length as 16 | 32;
451
+ const state = new Aegis128LBsState();
452
+ state.init(key, nonce);
453
+
454
+ const adPadded = zeroPad(ad, RATE);
455
+ for (let i = 0; i + RATE <= adPadded.length; i += RATE) {
456
+ state.absorb(adPadded.subarray(i, i + RATE));
457
+ }
458
+
459
+ const msg = new Uint8Array(ct.length);
460
+ const fullBlocks = Math.floor(ct.length / RATE) * RATE;
461
+
462
+ for (let i = 0; i < fullBlocks; i += RATE) {
463
+ state.decTo(ct.subarray(i, i + RATE), msg.subarray(i, i + RATE));
464
+ }
465
+
466
+ if (ct.length > fullBlocks) {
467
+ msg.set(state.decPartial(ct.subarray(fullBlocks)), fullBlocks);
468
+ }
469
+
470
+ const expectedTag = state.finalize(ad.length, msg.length, tagLen);
471
+
472
+ if (!constantTimeEqual(tag, expectedTag)) {
473
+ msg.fill(0);
474
+ return null;
475
+ }
476
+
477
+ return msg;
478
+ }
479
+
480
+ export const AEGIS_128L_BS_NONCE_SIZE = 16;
481
+ export const AEGIS_128L_BS_KEY_SIZE = 16;
482
+
483
+ /**
484
+ * Encrypts a message using bitsliced AEGIS-128L.
485
+ * Returns a single buffer containing nonce || ciphertext || tag.
486
+ * @param msg - Plaintext message
487
+ * @param ad - Associated data (authenticated but not encrypted)
488
+ * @param key - 16-byte encryption key
489
+ * @param nonce - 16-byte nonce (optional, generates random nonce if not provided)
490
+ * @param tagLen - Authentication tag length: 16 or 32 bytes (default: 16)
491
+ * @returns Concatenated nonce || ciphertext || tag
492
+ */
493
+ export function aegis128LBsEncrypt(
494
+ msg: Uint8Array,
495
+ ad: Uint8Array,
496
+ key: Uint8Array,
497
+ nonce: Uint8Array | null = null,
498
+ tagLen: 16 | 32 = 16,
499
+ ): Uint8Array {
500
+ const actualNonce = nonce ?? randomBytes(AEGIS_128L_BS_NONCE_SIZE);
501
+ const { ciphertext, tag } = aegis128LBsEncryptDetached(
502
+ msg,
503
+ ad,
504
+ key,
505
+ actualNonce,
506
+ tagLen,
507
+ );
508
+
509
+ const result = new Uint8Array(
510
+ AEGIS_128L_BS_NONCE_SIZE + ciphertext.length + tagLen,
511
+ );
512
+ result.set(actualNonce, 0);
513
+ result.set(ciphertext, AEGIS_128L_BS_NONCE_SIZE);
514
+ result.set(tag, AEGIS_128L_BS_NONCE_SIZE + ciphertext.length);
515
+
516
+ return result;
517
+ }
518
+
519
+ /**
520
+ * Decrypts a message using bitsliced AEGIS-128L.
521
+ * Expects input as nonce || ciphertext || tag.
522
+ * @param sealed - Concatenated nonce || ciphertext || tag
523
+ * @param ad - Associated data (must match what was used during encryption)
524
+ * @param key - 16-byte encryption key
525
+ * @param tagLen - Authentication tag length: 16 or 32 bytes (default: 16)
526
+ * @returns Decrypted plaintext, or null if authentication fails
527
+ */
528
+ export function aegis128LBsDecrypt(
529
+ sealed: Uint8Array,
530
+ ad: Uint8Array,
531
+ key: Uint8Array,
532
+ tagLen: 16 | 32 = 16,
533
+ ): Uint8Array | null {
534
+ const nonceSize = AEGIS_128L_BS_NONCE_SIZE;
535
+ if (sealed.length < nonceSize + tagLen) {
536
+ return null;
537
+ }
538
+ const nonce = sealed.subarray(0, nonceSize);
539
+ const ct = sealed.subarray(nonceSize, sealed.length - tagLen);
540
+ const tag = sealed.subarray(sealed.length - tagLen);
541
+ return aegis128LBsDecryptDetached(ct, tag, ad, key, nonce);
542
+ }
543
+
544
+ /**
545
+ * Computes a MAC (Message Authentication Code) using bitsliced AEGIS-128L.
546
+ * @param data - Data to authenticate
547
+ * @param key - 16-byte key
548
+ * @param nonce - 16-byte nonce (optional, uses zero nonce if null)
549
+ * @param tagLen - Tag length: 16 or 32 bytes (default: 16)
550
+ * @returns Authentication tag
551
+ */
552
+ export function aegis128LBsMac(
553
+ data: Uint8Array,
554
+ key: Uint8Array,
555
+ nonce: Uint8Array | null = null,
556
+ tagLen: 16 | 32 = 16,
557
+ ): Uint8Array {
558
+ const state = new Aegis128LBsState();
559
+ state.init(key, nonce ?? new Uint8Array(16));
560
+
561
+ const dataPadded = zeroPad(data, RATE);
562
+ for (let i = 0; i + RATE <= dataPadded.length; i += RATE) {
563
+ state.absorb(dataPadded.subarray(i, i + RATE));
564
+ }
565
+
566
+ return state.finalize(data.length, tagLen, tagLen);
567
+ }
568
+
569
+ /**
570
+ * Verifies a MAC computed using bitsliced AEGIS-128L.
571
+ * @param data - Data to verify
572
+ * @param tag - Expected authentication tag (16 or 32 bytes)
573
+ * @param key - 16-byte key
574
+ * @param nonce - 16-byte nonce (optional, uses zero nonce if null)
575
+ * @returns True if the tag is valid, false otherwise
576
+ */
577
+ export function aegis128LBsMacVerify(
578
+ data: Uint8Array,
579
+ tag: Uint8Array,
580
+ key: Uint8Array,
581
+ nonce: Uint8Array | null = null,
582
+ ): boolean {
583
+ const tagLen = tag.length as 16 | 32;
584
+ const expectedTag = aegis128LBsMac(data, key, nonce, tagLen);
585
+ return constantTimeEqual(tag, expectedTag);
586
+ }
587
+
588
+ /**
589
+ * Generates a random 16-byte key for bitsliced AEGIS-128L.
590
+ * @returns 16-byte encryption key
591
+ */
592
+ export function aegis128LBsCreateKey(): Uint8Array {
593
+ return randomBytes(AEGIS_128L_BS_KEY_SIZE);
594
+ }
595
+
596
+ /**
597
+ * Generates a random 16-byte nonce for bitsliced AEGIS-128L.
598
+ * @returns 16-byte nonce
599
+ */
600
+ export function aegis128LBsCreateNonce(): Uint8Array {
601
+ return randomBytes(AEGIS_128L_BS_NONCE_SIZE);
602
+ }