aegis-aead 0.2.1 → 0.2.2

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/src/aes-bs.ts CHANGED
@@ -52,115 +52,116 @@ function rotl32(x: number, b: number): number {
52
52
  return ((x << b) | (x >>> (32 - b))) >>> 0;
53
53
  }
54
54
 
55
- /**
56
- * S-box implementation using bitsliced logic gates.
57
- * Maximov & Ekdahl circuit.
58
- */
59
55
  function sbox(st: Uint32Array, offset: number): void {
60
- let u0 = st[offset]!;
61
- let u1 = st[offset + 1]!;
62
- let u2 = st[offset + 2]!;
63
- let u3 = st[offset + 3]!;
64
- let u4 = st[offset + 4]!;
65
- let u5 = st[offset + 5]!;
66
- let u6 = st[offset + 6]!;
67
- let u7 = st[offset + 7]!;
68
-
69
- const z24 = (u3 ^ u4) >>> 0;
70
- const q17 = (u1 ^ u7) >>> 0;
71
- const q16 = (u5 ^ q17) >>> 0;
72
- const q0 = (z24 ^ q16) >>> 0;
73
- const q7 = (z24 ^ u1 ^ u6) >>> 0;
74
- const q2 = (u2 ^ q0) >>> 0;
75
- const q1 = (q7 ^ q2) >>> 0;
76
- const q3 = (u0 ^ q7) >>> 0;
77
- const q4 = (u0 ^ q2) >>> 0;
78
- const q5 = (u1 ^ q4) >>> 0;
79
- const q6 = (u2 ^ u3) >>> 0;
80
- const q10 = (q6 ^ q7) >>> 0;
81
- const q8 = (u0 ^ q10) >>> 0;
82
- const q9 = (q8 ^ q2) >>> 0;
83
- const q12 = (z24 ^ q17) >>> 0;
84
- const q15 = (u7 ^ q4) >>> 0;
85
- const q13 = (z24 ^ q15) >>> 0;
86
- const q14 = (q15 ^ q0) >>> 0;
56
+ const u0 = st[offset]!;
57
+ const u1 = st[offset + 1]!;
58
+ const u2 = st[offset + 2]!;
59
+ const u3 = st[offset + 3]!;
60
+ const u4 = st[offset + 4]!;
61
+ const u5 = st[offset + 5]!;
62
+ const u6 = st[offset + 6]!;
63
+ const u7 = st[offset + 7]!;
64
+
65
+ const z24 = u3 ^ u4;
66
+ const q17 = u1 ^ u7;
67
+ const q16 = u5 ^ q17;
68
+ const q0 = z24 ^ q16;
69
+ const q7 = z24 ^ u1 ^ u6;
70
+ const q2 = u2 ^ q0;
71
+ const q1 = q7 ^ q2;
72
+ const q3 = u0 ^ q7;
73
+ const q4 = u0 ^ q2;
74
+ const q5 = u1 ^ q4;
75
+ const q6 = u2 ^ u3;
76
+ const q10 = q6 ^ q7;
77
+ const q8 = u0 ^ q10;
78
+ const q9 = q8 ^ q2;
79
+ const q12 = z24 ^ q17;
80
+ const q15 = u7 ^ q4;
81
+ const m02 = u0 ^ u2;
82
+ const m15 = u1 ^ u5;
83
+ const q13 = m02 ^ m15;
84
+ const q14 = m02 ^ u7;
87
85
  const q11 = u5;
88
86
 
89
- // NAND(x, y) = ~(x & y)
90
- // NOR(x, y) = ~(x | y)
91
- // XNOR(x, y) = ~(x ^ y)
92
- // MUX(s, x, y) = (x & s) | (y & ~s)
93
- const nand = (x: number, y: number) => ~(x & y) >>> 0;
94
- const nor = (x: number, y: number) => ~(x | y) >>> 0;
95
- const xnor = (x: number, y: number) => ~(x ^ y) >>> 0;
96
- const mux = (s: number, x: number, y: number) =>
97
- (((x & s) >>> 0) | ((y & ~s) >>> 0)) >>> 0;
98
-
99
- const t20 = nand(q6, q12);
100
- const t21 = nand(q3, q14);
101
- const t22 = nand(q1, q16);
102
- const x0 = (nor(q3, q14) ^ nand(q0, q7) ^ (t20 ^ t22)) >>> 0;
103
- const x1 = (nor(q4, q13) ^ nand(q10, q11) ^ (t21 ^ t20)) >>> 0;
104
- const x2 = (nor(q2, q17) ^ nand(q5, q9) ^ (t21 ^ t22)) >>> 0;
105
- const x3 = (nor(q8, q15) ^ nand(q2, q17) ^ (t21 ^ nand(q4, q13))) >>> 0;
106
-
107
- const t2 = xnor(nand(x0, x2), nor(x1, x3));
108
- const y0 = mux(x2, t2, x3);
109
- const y2 = mux(x0, t2, x1);
110
- const y1 = mux(t2, x3, mux(x1, x2, 0xffffffff));
111
- const y3 = mux(t2, x1, mux(x3, x0, 0xffffffff));
112
- const y02 = (y2 ^ y0) >>> 0;
113
- const y13 = (y3 ^ y1) >>> 0;
114
- const y23 = (y3 ^ y2) >>> 0;
115
- const y01 = (y1 ^ y0) >>> 0;
116
- const y00 = (y02 ^ y13) >>> 0;
117
-
118
- const n0 = nand(y01, q11);
119
- const n1 = nand(y0, q12);
120
- const n2 = nand(y1, q0);
121
- const n3 = nand(y23, q17);
122
- const n4 = nand(y2, q5);
123
- const n5 = nand(y3, q15);
124
- const n6 = nand(y13, q14);
125
- const n7 = nand(y00, q16);
126
- const n8 = nand(y02, q13);
127
- const n9 = nand(y01, q7);
128
- const n10 = nand(y0, q10);
129
- const n11 = nand(y1, q6);
130
- const n12 = nand(y23, q2);
131
- const n13 = nand(y2, q9);
132
- const n14 = nand(y3, q8);
133
- const n15 = nand(y13, q3);
134
- const n16 = nand(y00, q1);
135
- const n17 = nand(y02, q4);
136
-
137
- const h1 = (n4 ^ n1 ^ n5) >>> 0;
138
- u2 = xnor(n2, h1);
139
- const h2 = (n9 ^ n15) >>> 0;
140
- u6 = xnor(h2, (n11 ^ n17) >>> 0);
141
- const h4 = (n11 ^ n14) >>> 0;
142
- const h5 = (n9 ^ n12) >>> 0;
143
- u5 = (h4 ^ h5) >>> 0;
144
- const h7 = (u2 ^ u6) >>> 0;
145
- const h8 = (n10 ^ h7) >>> 0;
146
- u7 = xnor((n16 ^ h2) >>> 0, h8);
147
- const h9 = (n8 ^ h1) >>> 0;
148
- const h10 = (n13 ^ h8) >>> 0;
149
- u3 = (h5 ^ h10) >>> 0;
150
- const h13 = (h4 ^ n7 ^ h9 ^ h10) >>> 0;
151
- u4 = (n1 ^ h13) >>> 0;
152
- const h14 = xnor(n0, u7);
153
- u1 = xnor(n6, (h7 ^ h9 ^ h14) >>> 0);
154
- u0 = (h13 ^ n3 ^ n4 ^ h14) >>> 0;
155
-
156
- st[offset] = u0;
157
- st[offset + 1] = u1;
158
- st[offset + 2] = u2;
159
- st[offset + 3] = u3;
160
- st[offset + 4] = u4;
161
- st[offset + 5] = u5;
162
- st[offset + 6] = u6;
163
- st[offset + 7] = u7;
87
+ const t20 = q6 & q12;
88
+ const t21 = q3 & q14;
89
+ const t22 = q1 & q16;
90
+ const t23 = q2 & q17;
91
+ const x0 = (q3 | q14) ^ (q0 & q7) ^ (t20 ^ t22);
92
+ const x1 = (q4 | q13) ^ (q10 & q11) ^ (t21 ^ t20);
93
+ const x2 = (q2 | q17) ^ (q5 & q9) ^ (t21 ^ t22);
94
+ const x3 = (q8 | q15) ^ t23 ^ (t21 ^ (q4 & q13));
95
+
96
+ const a = x1 & ~x3;
97
+ const b = x0 & ~x3;
98
+ const c = x3 & ~x1;
99
+ const d = x2 & ~x1;
100
+ const e = x0 ^ a;
101
+ const y0 = x3 ^ (x2 & ~e);
102
+ const f = x1 ^ b;
103
+ const y1 = c ^ (x2 & f);
104
+ const g = x2 ^ c;
105
+ const y2 = x1 ^ (x0 & ~g);
106
+ const h = x3 ^ d;
107
+ const y3 = a ^ (x0 & h);
108
+ const y02 = y2 ^ y0;
109
+ const y13 = y3 ^ y1;
110
+ const y23 = y3 ^ y2;
111
+ const y01 = y1 ^ y0;
112
+ const y00 = y02 ^ y13;
113
+
114
+ const a0 = y01 & q11;
115
+ const a1 = y0 & q12;
116
+ const a2 = y1 & q0;
117
+ const a3 = y23 & q17;
118
+ const a4 = y2 & q5;
119
+ const a5 = y3 & q15;
120
+ const a6 = y13 & q14;
121
+ const a7 = y00 & q16;
122
+ const a8 = y02 & q13;
123
+ const a9 = y01 & q7;
124
+ const a10 = y0 & q10;
125
+ const a11 = y1 & q6;
126
+ const a12 = y23 & q2;
127
+ const a13 = y2 & q9;
128
+ const a14 = y3 & q8;
129
+ const a15 = y13 & q3;
130
+ const a16 = y00 & q1;
131
+ const a17 = y02 & q4;
132
+
133
+ const r0 = a1 ^ a5;
134
+ const r1 = a9 ^ a15;
135
+ const r2 = a4 ^ r0;
136
+ const r3 = a2 ^ a10;
137
+ const r4 = a11 ^ a17;
138
+ const r5 = a8 ^ r1;
139
+ const r6 = a0 ^ a16;
140
+ const r7 = a7 ^ a13;
141
+ const r8 = a11 ^ a14;
142
+ const r9 = r3 ^ r4;
143
+ const r10 = r5 ^ r6;
144
+ const r11 = r2 ^ r9;
145
+ const r12 = a3 ^ r0;
146
+ const r13 = r7 ^ r8;
147
+ const r14 = r12 ^ r13;
148
+ st[offset] = r10 ^ r14;
149
+ const r15 = a6 ^ a10;
150
+ const r16 = r15 ^ r2;
151
+ st[offset + 1] = ~(r10 ^ r16);
152
+ st[offset + 2] = ~(a2 ^ r2);
153
+ const r17 = a12 ^ a13;
154
+ const r18 = a15 ^ r17;
155
+ st[offset + 3] = r18 ^ r11;
156
+ const r19 = a1 ^ a14;
157
+ const r20 = a17 ^ r3;
158
+ const r21 = r7 ^ r19;
159
+ const r22 = r5 ^ r20;
160
+ st[offset + 4] = r21 ^ r22;
161
+ const r23 = a9 ^ a12;
162
+ st[offset + 5] = r8 ^ r23;
163
+ st[offset + 6] = ~(r1 ^ r4);
164
+ st[offset + 7] = ~(a16 ^ r11);
164
165
  }
165
166
 
166
167
  /**
@@ -387,6 +388,76 @@ export function pack04(st: AesBlocks): void {
387
388
  }
388
389
  }
389
390
 
391
+ /**
392
+ * Inverse of pack04: unpack only blocks 0 and 4.
393
+ */
394
+ export function unpack04(st: AesBlocks): void {
395
+ for (let i = 0; i < 32; i += 8) {
396
+ swapmove(st, i + 7, i + 3, 0x0f0f0f0f, 4);
397
+ swapmove(st, i + 6, i + 2, 0x0f0f0f0f, 4);
398
+ swapmove(st, i + 5, i + 1, 0x0f0f0f0f, 4);
399
+ swapmove(st, i + 4, i, 0x0f0f0f0f, 4);
400
+ swapmove(st, i + 7, i + 5, 0x33333333, 2);
401
+ swapmove(st, i + 6, i + 4, 0x33333333, 2);
402
+ swapmove(st, i + 3, i + 1, 0x33333333, 2);
403
+ swapmove(st, i + 2, i, 0x33333333, 2);
404
+ swapmove(st, i + 5, i + 4, 0x55555555, 1);
405
+ swapmove(st, i + 1, i, 0x55555555, 1);
406
+ }
407
+
408
+ swapmove(st, 12, 12 + 16, 0x0000ffff, 16);
409
+ swapmove(st, 8, 8 + 16, 0x0000ffff, 16);
410
+ swapmove(st, 4, 4 + 16, 0x0000ffff, 16);
411
+ swapmove(st, 0, 0 + 16, 0x0000ffff, 16);
412
+
413
+ swapmove(st, 4 + 16, 4 + 24, 0x00ff00ff, 8);
414
+ swapmove(st, 4, 4 + 8, 0x00ff00ff, 8);
415
+ swapmove(st, 0 + 16, 0 + 24, 0x00ff00ff, 8);
416
+ swapmove(st, 0, 0 + 8, 0x00ff00ff, 8);
417
+ }
418
+
419
+ /**
420
+ * Pack only block 0 (used for constant inputs in AEGIS-256).
421
+ */
422
+ export function pack04_6(st: AesBlocks): void {
423
+ swapmove(st, 0, 0 + 8, 0x00ff00ff, 8);
424
+ swapmove(st, 0 + 16, 0 + 24, 0x00ff00ff, 8);
425
+
426
+ swapmove(st, 0, 0 + 16, 0x0000ffff, 16);
427
+ swapmove(st, 8, 8 + 16, 0x0000ffff, 16);
428
+
429
+ for (let i = 0; i < 32; i += 8) {
430
+ swapmove(st, i + 1, i, 0x55555555, 1);
431
+ swapmove(st, i + 2, i, 0x33333333, 2);
432
+ swapmove(st, i + 3, i + 1, 0x33333333, 2);
433
+ swapmove(st, i + 4, i, 0x0f0f0f0f, 4);
434
+ swapmove(st, i + 5, i + 1, 0x0f0f0f0f, 4);
435
+ swapmove(st, i + 6, i + 2, 0x0f0f0f0f, 4);
436
+ swapmove(st, i + 7, i + 3, 0x0f0f0f0f, 4);
437
+ }
438
+ }
439
+
440
+ /**
441
+ * Inverse of pack04_6: unpack only block 0.
442
+ */
443
+ export function unpack04_6(st: AesBlocks): void {
444
+ for (let i = 0; i < 32; i += 8) {
445
+ swapmove(st, i + 7, i + 3, 0x0f0f0f0f, 4);
446
+ swapmove(st, i + 6, i + 2, 0x0f0f0f0f, 4);
447
+ swapmove(st, i + 5, i + 1, 0x0f0f0f0f, 4);
448
+ swapmove(st, i + 4, i, 0x0f0f0f0f, 4);
449
+ swapmove(st, i + 3, i + 1, 0x33333333, 2);
450
+ swapmove(st, i + 2, i, 0x33333333, 2);
451
+ swapmove(st, i + 1, i, 0x55555555, 1);
452
+ }
453
+
454
+ swapmove(st, 8, 8 + 16, 0x0000ffff, 16);
455
+ swapmove(st, 0, 0 + 16, 0x0000ffff, 16);
456
+
457
+ swapmove(st, 0 + 16, 0 + 24, 0x00ff00ff, 8);
458
+ swapmove(st, 0, 0 + 8, 0x00ff00ff, 8);
459
+ }
460
+
390
461
  /**
391
462
  * Computes the index into the bitsliced state array.
392
463
  * @param block - Block index (0-7)
@@ -407,6 +478,16 @@ export function blocksRotr(st: AesBlocks): void {
407
478
  }
408
479
  }
409
480
 
481
+ /**
482
+ * Rotate within the bottom 6 lanes (for AEGIS-256, 6 blocks).
483
+ */
484
+ export function blocksRotr6(st: AesBlocks): void {
485
+ for (let i = 0; i < 32; i++) {
486
+ st[i] =
487
+ (((st[i]! & 0xf8f8f8f8) >>> 1) | ((st[i]! & 0x04040404) << 5)) >>> 0;
488
+ }
489
+ }
490
+
410
491
  /**
411
492
  * Put a single AES block into the bitsliced state at the given block position.
412
493
  */