aegis-aead 0.1.1 → 0.2.1

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