@li0ard/dstu 0.2.0 → 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 (49) hide show
  1. package/const.d.ts +5 -14
  2. package/const.js +47 -1010
  3. package/dstu4145/const.js +15 -15
  4. package/dstu4145/ec/index.js +9 -27
  5. package/dstu4145/ecies.d.ts +2 -2
  6. package/dstu4145/ecies.js +2 -1
  7. package/dstu4145/gf2m/utils.js +1 -1
  8. package/dstu4145/index.js +12 -12
  9. package/dstu9041/curve.d.ts +1 -1
  10. package/dstu9041/curve.js +14 -27
  11. package/dstu9041/index.js +12 -20
  12. package/dstu9311/const.js +12 -12
  13. package/dstu9311/index.d.ts +1 -2
  14. package/dstu9311/index.js +34 -30
  15. package/gf/index.d.ts +5 -0
  16. package/gf/index.js +62 -0
  17. package/gost34311/index.d.ts +55 -0
  18. package/gost34311/index.js +167 -0
  19. package/index.d.ts +1 -0
  20. package/index.js +1 -0
  21. package/iso15946.js +4 -6
  22. package/kalyna/index.d.ts +2 -4
  23. package/kalyna/index.js +113 -92
  24. package/keywrap.js +5 -10
  25. package/kupyna/index.d.ts +21 -103
  26. package/kupyna/index.js +104 -153
  27. package/modes/_guard.d.ts +4 -0
  28. package/modes/_guard.js +6 -0
  29. package/modes/cbc.js +2 -1
  30. package/modes/ccm.js +1 -1
  31. package/modes/cfb.js +2 -1
  32. package/modes/cmac.js +2 -1
  33. package/modes/ctr.js +2 -1
  34. package/modes/gcm.js +2 -1
  35. package/modes/gmac.js +4 -8
  36. package/modes/kw.js +1 -1
  37. package/modes/ofb.js +2 -1
  38. package/modes/xts.js +11 -25
  39. package/package.json +12 -9
  40. package/padding.d.ts +1 -1
  41. package/padding.js +0 -5
  42. package/strumok/index.d.ts +3 -4
  43. package/strumok/index.js +128 -85
  44. package/utils.d.ts +6 -13
  45. package/utils.js +27 -70
  46. package/dstu9311/hash.d.ts +0 -75
  47. package/dstu9311/hash.js +0 -159
  48. package/kupyna/kmac.d.ts +0 -46
  49. package/kupyna/kmac.js +0 -130
package/kalyna/index.js CHANGED
@@ -1,22 +1,24 @@
1
1
  import { abool, abytes, anumber } from "@noble/hashes/utils.js";
2
- import { IS, IT, T, S } from "../const.js";
3
- import { bytesToUint64sLE, uint64sToBytesLE, byte } from "../utils.js";
4
- const swap_block = (k, N) => {
5
- if (N <= 1)
2
+ import { T, S, MDS_MATRIX_inv, genTable } from "../const.js";
3
+ import { _U32, u32ToBytesLE, bytesToU32LE } from "../utils.js";
4
+ const swap_block = (k, words) => {
5
+ if (words <= 1)
6
6
  return;
7
- const t = k[0];
8
- for (let i = 0; i < N - 1; i++)
9
- k[i] = k[i + 1];
10
- k[N - 1] = t;
7
+ const l = k[0], h = k[1];
8
+ k.copyWithin(0, 2, words * 2);
9
+ k[words * 2 - 2] = l;
10
+ k[words * 2 - 1] = h;
11
11
  };
12
- export const column = (x, i, N, c1, c2, c3, c4, c5, c6, c7, table = T, sign = -1) => (table[0][byte(x[(i + N) % N])] ^
13
- table[1][byte(x[(i + sign * c1 + N) % N] >> 8n)] ^
14
- table[2][byte(x[(i + sign * c2 + N) % N] >> 16n)] ^
15
- table[3][byte(x[(i + sign * c3 + N) % N] >> 24n)] ^
16
- table[4][byte(x[(i + sign * c4 + N) % N] >> 32n)] ^
17
- table[5][byte(x[(i + sign * c5 + N) % N] >> 40n)] ^
18
- table[6][byte(x[(i + sign * c6 + N) % N] >> 48n)] ^
19
- table[7][byte(x[(i + sign * c7 + N) % N] >> 56n)]);
12
+ export const column = (x, i, N, offs, out, o, table = T, sign = -1) => {
13
+ const m = N - 1;
14
+ const j0 = ((i + sign * offs[0]) & m) << 1, j1 = ((i + sign * offs[1]) & m) << 1, j2 = ((i + sign * offs[2]) & m) << 1, j3 = ((i + sign * offs[3]) & m) << 1, j4 = (((i + sign * offs[4]) & m) << 1) | 1, j5 = (((i + sign * offs[5]) & m) << 1) | 1, j6 = (((i + sign * offs[6]) & m) << 1) | 1, j7 = (((i + sign * offs[7]) & m) << 1) | 1;
15
+ const b0 = (x[j0] & 0xFF) << 1, b1 = ((x[j1] >>> 8) & 0xFF) << 1, b2 = ((x[j2] >>> 16) & 0xFF) << 1, b3 = (x[j3] >>> 24) << 1, b4 = (x[j4] & 0xFF) << 1, b5 = ((x[j5] >>> 8) & 0xFF) << 1, b6 = ((x[j6] >>> 16) & 0xFF) << 1, b7 = (x[j7] >>> 24) << 1;
16
+ const t0 = table[0], t1 = table[1], t2 = table[2], t3 = table[3], t4 = table[4], t5 = table[5], t6 = table[6], t7 = table[7];
17
+ out[o] = t0[b0] ^ t1[b1] ^ t2[b2] ^ t3[b3] ^ t4[b4] ^ t5[b5] ^ t6[b6] ^ t7[b7];
18
+ out[o + 1] = t0[b0 | 1] ^ t1[b1 | 1] ^ t2[b2 | 1] ^ t3[b3 | 1] ^
19
+ t4[b4 | 1] ^ t5[b5 | 1] ^ t6[b6 | 1] ^ t7[b7 | 1];
20
+ };
21
+ const IS = /* @__PURE__ */ S.map(i => i.map((_, j) => i.indexOf(j))), IT = /* @__PURE__ */ genTable(MDS_MATRIX_inv, IS);
20
22
  export class Kalyna {
21
23
  N;
22
24
  /** Block size */
@@ -47,140 +49,159 @@ export class Kalyna {
47
49
  this.expandKey(key);
48
50
  }
49
51
  expandKey(key) {
50
- const log2N = Math.log2(this.N), isDoubleKey = (this.keySize === this.blockSize * 2), R = isDoubleKey ? (6 + 2 * log2N) : (4 + 2 * log2N), rk = new BigUint64Array(R * this.N * 2), ks = new BigUint64Array(this.N), ksc = new BigUint64Array(this.N), t1 = new BigUint64Array(this.N), t2 = new BigUint64Array(this.N);
51
- t1[0] = isDoubleKey ? BigInt(2 * this.N + 2 * log2N + 1) : BigInt(2 * this.N + 1);
52
- const keys = bytesToUint64sLE(key);
53
- let k = new BigUint64Array(isDoubleKey ? this.N * 2 : this.N);
54
- const _0 = new BigUint64Array(this.N);
52
+ const N2 = this.N * 2, log2N = Math.log2(this.N), isDoubleKey = (this.keySize === this.blockSize * 2), R = isDoubleKey ? (6 + 2 * log2N) : (4 + 2 * log2N), rk = new Uint32Array(R * this.N * 4), ks = new Uint32Array(N2), ksc = new Uint32Array(N2), t1 = new Uint32Array(N2), t2 = new Uint32Array(N2);
53
+ t1[0] = isDoubleKey ? (N2 + 2 * log2N + 1) : (N2 + 1);
54
+ const keys = bytesToU32LE(key), _0 = new Uint32Array(N2);
55
+ const k = new Uint32Array(isDoubleKey ? N2 * 2 : N2);
55
56
  if (isDoubleKey) {
56
- const ka = keys.subarray(0, this.N);
57
+ const ka = keys.subarray(0, N2);
57
58
  this.addkey(t1, t2, ka);
58
- this.G(t2, t1, keys.subarray(this.N));
59
+ this.G(t2, t1, keys.subarray(N2));
59
60
  this.GL(t1, t2, ka);
60
61
  this.G(t2, ks, _0);
61
62
  k.set(keys);
62
63
  }
63
64
  else {
64
- k.set(keys.subarray(0, this.N));
65
+ k.set(keys.subarray(0, N2));
65
66
  this.addkey(t1, t2, keys);
66
67
  this.G(t2, t1, keys);
67
68
  this.GL(t1, t2, keys);
68
69
  this.G(t2, ks, _0);
69
70
  }
70
- let constant = 0x0001000100010001n;
71
+ let cl = 0x00010001, ch = 0x00010001;
71
72
  for (let i = 0; i < R; i++) {
72
- const offset = i * (this.N * 2);
73
+ const offset = i * N2;
73
74
  if (i > 0) {
74
75
  if (!isDoubleKey)
75
76
  swap_block(k, this.N);
76
77
  else if (i % 2 === 0)
77
- swap_block(k, this.N * 2);
78
+ swap_block(k, N2);
78
79
  }
79
80
  const keySource = isDoubleKey
80
- ? (i % 2 === 0 ? k.subarray(0, this.N) : k.subarray(this.N))
81
+ ? (i % 2 === 0 ? k.subarray(0, N2) : k.subarray(N2))
81
82
  : k;
82
- for (let i = 0; i < this.N; i++)
83
- ksc[i] = ks[i] + constant;
83
+ for (let j = 0; j < N2; j += 2) {
84
+ const lo = ks[j] + cl;
85
+ ksc[j] = lo;
86
+ ksc[j + 1] = ks[j + 1] + ch + (lo > _U32 ? 1 : 0);
87
+ }
84
88
  this.addkey(keySource, t2, ksc);
85
89
  this.G(t2, t1, ksc);
86
- this.GL(t1, rk.subarray(offset), ksc);
90
+ this.GL(t1, rk.subarray(offset * 2), ksc);
87
91
  if (i < R - 1)
88
- this.makeOddKey(rk.subarray(offset), rk.subarray(offset + this.N));
89
- constant <<= 1n;
92
+ this.makeOddKey(rk.subarray(offset * 2), rk.subarray((offset + this.N) * 2));
93
+ ch = ((ch << 1) | (cl >>> 31)) >>> 0;
94
+ cl = (cl << 1) >>> 0;
90
95
  }
91
96
  this.erk = rk.slice();
92
97
  for (let i = ((R * 2 - 3) * this.N); i > 0; i -= this.N)
93
- this.IMC(rk.subarray(i));
98
+ this.IMC(rk.subarray(i * 2));
94
99
  this.drk = rk;
95
100
  }
96
101
  makeOddKey(evenkey, oddkey) {
97
- const offset = 2 * this.N + 3;
98
- const evenkeys = uint64sToBytesLE(evenkey);
99
- const oddkeys = uint64sToBytesLE(oddkey);
100
- oddkeys.set(evenkeys.subarray(offset, this.blockSize));
101
- oddkeys.set(evenkeys.subarray(0, offset), this.blockSize - offset);
102
- oddkey.set(bytesToUint64sLE(oddkeys));
102
+ const offset = 2 * this.N + 3, ev = u32ToBytesLE(evenkey.subarray(0, this.N * 2)), od = new Uint8Array(this.blockSize);
103
+ od.set(ev.subarray(offset, this.blockSize));
104
+ od.set(ev.subarray(0, offset), this.blockSize - offset);
105
+ oddkey.set(bytesToU32LE(od));
103
106
  }
104
- addkey(x, y, k) {
105
- for (let i = 0; i < this.N; i++)
106
- y[i] = x[i] + k[i];
107
- }
108
- subkey(x, y, k) {
109
- for (let i = 0; i < this.N; i++)
110
- y[i] = x[i] - k[i];
107
+ addkey(x, y, k, ko = 0) {
108
+ for (let j = 0, e = this.N * 2; j < e; j += 2) {
109
+ const lo = x[j] + k[ko + j];
110
+ y[j + 1] = x[j + 1] + k[ko + j + 1] + (lo > _U32 ? 1 : 0);
111
+ y[j] = lo;
112
+ }
111
113
  }
112
- column(x, i) {
113
- return column(x, i, this.N, this.wordOffsets[1], this.wordOffsets[2], this.wordOffsets[3], this.wordOffsets[4], this.wordOffsets[5], this.wordOffsets[6], this.wordOffsets[7]);
114
+ subkey(x, y, k, ko = 0) {
115
+ for (let j = 0, e = this.N * 2; j < e; j += 2) {
116
+ const lo = x[j] - k[ko + j];
117
+ y[j + 1] = x[j + 1] - k[ko + j + 1] - (lo < 0 ? 1 : 0);
118
+ y[j] = lo;
119
+ }
114
120
  }
115
- invColumn(x, i) {
116
- return column(x, i, this.N, this.wordOffsets[1], this.wordOffsets[2], this.wordOffsets[3], this.wordOffsets[4], this.wordOffsets[5], this.wordOffsets[6], this.wordOffsets[7], IT, 1);
121
+ G(x, y, k, ko = 0) {
122
+ for (let i = 0; i < this.N; i++) {
123
+ const j = i << 1;
124
+ column(x, i, this.N, this.wordOffsets, y, j);
125
+ y[j] ^= k[ko + j];
126
+ y[j + 1] ^= k[ko + j + 1];
127
+ }
117
128
  }
118
- G(x, y, k) {
119
- for (let i = 0; i < this.N; i++)
120
- y[i] = k[i] ^ this.column(x, i);
129
+ GL(x, y, k, ko = 0) {
130
+ for (let i = 0; i < this.N; i++) {
131
+ const j = i << 1;
132
+ column(x, i, this.N, this.wordOffsets, y, j);
133
+ const lo = y[j] + k[ko + j];
134
+ y[j + 1] += k[ko + j + 1] + (lo > _U32 ? 1 : 0);
135
+ y[j] = lo;
136
+ }
121
137
  }
122
- GL(x, y, k) {
123
- for (let i = 0; i < this.N; i++)
124
- y[i] = k[i] + this.column(x, i);
138
+ IG(x, y, k, ko = 0) {
139
+ for (let i = 0; i < this.N; i++) {
140
+ const j = i << 1;
141
+ column(x, i, this.N, this.wordOffsets, y, j, IT, 1);
142
+ y[j] ^= k[ko + j];
143
+ y[j + 1] ^= k[ko + j + 1];
144
+ }
125
145
  }
126
146
  IMC(x) {
127
- for (let i = 0; i < this.N; i++)
128
- x[i] = IT[0][S[0][byte(x[i])]] ^
129
- IT[1][S[1][byte(x[i] >> 8n)]] ^
130
- IT[2][S[2][byte(x[i] >> 16n)]] ^
131
- IT[3][S[3][byte(x[i] >> 24n)]] ^
132
- IT[4][S[0][byte(x[i] >> 32n)]] ^
133
- IT[5][S[1][byte(x[i] >> 40n)]] ^
134
- IT[6][S[2][byte(x[i] >> 48n)]] ^
135
- IT[7][S[3][byte(x[i] >> 56n)]];
136
- }
137
- IG(x, y, k) {
138
- for (let i = 0; i < this.N; i++)
139
- y[i] = k[i] ^ this.invColumn(x, i);
147
+ const [s0, s1, s2, s3] = S;
148
+ const [i0, i1, i2, i3, i4, i5, i6, i7] = IT;
149
+ for (let j = 0, e = this.N * 2; j < e; j += 2) {
150
+ const l = x[j], h = x[j + 1];
151
+ const a0 = s0[l & 0xFF] << 1, a1 = s1[(l >>> 8) & 0xFF] << 1, a2 = s2[(l >>> 16) & 0xFF] << 1, a3 = s3[l >>> 24] << 1, a4 = s0[h & 0xFF] << 1, a5 = s1[(h >>> 8) & 0xFF] << 1, a6 = s2[(h >>> 16) & 0xFF] << 1, a7 = s3[h >>> 24] << 1;
152
+ x[j] = i0[a0] ^ i1[a1] ^ i2[a2] ^ i3[a3] ^ i4[a4] ^ i5[a5] ^ i6[a6] ^ i7[a7];
153
+ x[j + 1] = i0[a0 | 1] ^ i1[a1 | 1] ^ i2[a2 | 1] ^ i3[a3 | 1] ^
154
+ i4[a4 | 1] ^ i5[a5 | 1] ^ i6[a6 | 1] ^ i7[a7 | 1];
155
+ }
140
156
  }
141
157
  IGL(x, y) {
158
+ const m = this.N - 1;
159
+ const [is0, is1, is2, is3] = IS;
142
160
  for (let i = 0; i < this.N; i++) {
143
- const result = BigInt(IS[0][byte(x[i % this.N])]) ^
144
- (BigInt(IS[1][byte(x[(i + this.wordOffsets[1]) % this.N] >> 8n)]) << 8n) ^
145
- (BigInt(IS[2][byte(x[(i + this.wordOffsets[2]) % this.N] >> 16n)]) << 16n) ^
146
- (BigInt(IS[3][byte(x[(i + this.wordOffsets[3]) % this.N] >> 24n)]) << 24n) ^
147
- (BigInt(IS[0][byte(x[(i + this.wordOffsets[4]) % this.N] >> 32n)]) << 32n) ^
148
- (BigInt(IS[1][byte(x[(i + this.wordOffsets[5]) % this.N] >> 40n)]) << 40n) ^
149
- (BigInt(IS[2][byte(x[(i + this.wordOffsets[6]) % this.N] >> 48n)]) << 48n) ^
150
- (BigInt(IS[3][byte(x[(i + this.wordOffsets[7]) % this.N] >> 56n)]) << 56n);
151
- y[i] = result - this.drk[i];
161
+ const j0 = ((i + this.wordOffsets[0]) & m) << 1, j1 = ((i + this.wordOffsets[1]) & m) << 1, j2 = ((i + this.wordOffsets[2]) & m) << 1, j3 = ((i + this.wordOffsets[3]) & m) << 1, j4 = (((i + this.wordOffsets[4]) & m) << 1) | 1, j5 = (((i + this.wordOffsets[5]) & m) << 1) | 1, j6 = (((i + this.wordOffsets[6]) & m) << 1) | 1, j7 = (((i + this.wordOffsets[7]) & m) << 1) | 1;
162
+ const lo = is0[x[j0] & 0xFF] |
163
+ (is1[(x[j1] >>> 8) & 0xFF] << 8) |
164
+ (is2[(x[j2] >>> 16) & 0xFF] << 16) |
165
+ (is3[x[j3] >>> 24] << 24);
166
+ const hi = is0[x[j4] & 0xFF] |
167
+ (is1[(x[j5] >>> 8) & 0xFF] << 8) |
168
+ (is2[(x[j6] >>> 16) & 0xFF] << 16) |
169
+ (is3[x[j7] >>> 24] << 24);
170
+ const d = i << 1, l = (lo >>> 0) - this.drk[d];
171
+ y[d + 1] = (hi >>> 0) - this.drk[d + 1] - (l < 0 ? 1 : 0);
172
+ y[d] = l;
152
173
  }
153
174
  }
154
175
  /** Encrypt block */
155
176
  encrypt(block) {
156
177
  abytes(block, this.blockSize, "block");
157
- const t1 = new BigUint64Array(this.N), t2 = new BigUint64Array(this.N);
158
- this.addkey(bytesToUint64sLE(block), t1, this.erk);
178
+ const t1 = new Uint32Array(this.N * 2), t2 = new Uint32Array(this.N * 2);
179
+ this.addkey(bytesToU32LE(block), t1, this.erk);
159
180
  for (let i = 0; i < this.numRounds; i++) {
160
- const roundKey = this.erk.subarray(this.N + i * this.N);
181
+ const ko = (this.N + i * this.N) * 2;
161
182
  if (i % 2 === 0)
162
- this.G(t1, t2, roundKey);
183
+ this.G(t1, t2, this.erk, ko);
163
184
  else
164
- this.G(t2, t1, roundKey);
185
+ this.G(t2, t1, this.erk, ko);
165
186
  }
166
- this.GL(t2, t1, this.erk.subarray(this.glOffset));
167
- return uint64sToBytesLE(t1);
187
+ this.GL(t2, t1, this.erk, this.glOffset * 2);
188
+ return u32ToBytesLE(t1);
168
189
  }
169
190
  /** Decrypt block */
170
191
  decrypt(block) {
171
192
  abytes(block, this.blockSize, "block");
172
- const t1 = new BigUint64Array(this.N), t2 = new BigUint64Array(this.N);
173
- this.subkey(bytesToUint64sLE(block), t1, this.drk.subarray(this.glOffset));
193
+ const t1 = new Uint32Array(this.N * 2), t2 = new Uint32Array(this.N * 2);
194
+ this.subkey(bytesToU32LE(block), t1, this.drk, this.glOffset * 2);
174
195
  this.IMC(t1);
175
196
  for (let i = 0; i < this.numRounds; i++) {
176
- const roundKey = this.drk.subarray(this.glOffset - this.N - i * this.N);
197
+ const ko = (this.glOffset - this.N - i * this.N) * 2;
177
198
  if (i % 2 === 0)
178
- this.IG(t1, t2, roundKey);
199
+ this.IG(t1, t2, this.drk, ko);
179
200
  else
180
- this.IG(t2, t1, roundKey);
201
+ this.IG(t2, t1, this.drk, ko);
181
202
  }
182
203
  this.IGL(t2, t1);
183
- return uint64sToBytesLE(t1);
204
+ return u32ToBytesLE(t1);
184
205
  }
185
206
  }
186
207
  /** Kalyna 128/128 bit version */
package/keywrap.js CHANGED
@@ -2,11 +2,11 @@ import { equalBytes, concatBytes, randomBytes, abytes, abool } from "@noble/curv
2
2
  import { Kalyna256 } from "./kalyna/index.js";
3
3
  import { cfb, cmac } from "./modes/index.js";
4
4
  import { Dstu9311 } from "./dstu9311/index.js";
5
- const IV1_KALYNA = new Uint8Array([
5
+ const IV1_KALYNA = /* @__PURE__ */ new Uint8Array([
6
6
  0x69, 0x73, 0x27, 0x1D, 0x6E, 0x61, 0x1D, 0x06, 0x61, 0x67, 0x15, 0x04, 0x6C, 0x65, 0x50, 0x4C,
7
7
  0x20, 0x20, 0x00, 0x4F, 0x6D, 0x68, 0x01, 0x1F, 0x65, 0x61, 0x0C, 0x0C, 0x73, 0x73, 0x47, 0x14
8
8
  ]);
9
- const IV1_9311 = new Uint8Array([
9
+ const IV1_9311 = /* @__PURE__ */ new Uint8Array([
10
10
  0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05
11
11
  ]);
12
12
  /** Key wrap (`Dstu7624Wrap`/`GOST28147Wrap`) */
@@ -17,8 +17,7 @@ export const keyWrap = (kek, useDstu9311 = false) => {
17
17
  abool(useDstu9311, "useDstu9311");
18
18
  const cipher = new (useDstu9311 ? Dstu9311 : Kalyna256)(kek);
19
19
  const ivLength = useDstu9311 ? 8 : 32, macLength = useDstu9311 ? 4 : 32;
20
- const macMode = cmac(cipher, macLength);
21
- const finalMode = cfb(cipher, useDstu9311 ? IV1_9311 : IV1_KALYNA, 32);
20
+ const macMode = cmac(cipher, macLength), finalMode = cfb(cipher, useDstu9311 ? IV1_9311 : IV1_KALYNA, 32);
22
21
  return Object.freeze({
23
22
  wrap: (key, iv) => {
24
23
  abytes(key, undefined, "key");
@@ -29,12 +28,8 @@ export const keyWrap = (kek, useDstu9311 = false) => {
29
28
  },
30
29
  unwrap: (wrappedKey) => {
31
30
  abytes(wrappedKey, undefined, "wrappedKey");
32
- const dec_wrapped = finalMode.decrypt(wrappedKey).reverse();
33
- const iv = dec_wrapped.subarray(0, ivLength);
34
- const key_mac = cfb(cipher, iv, 32).decrypt(dec_wrapped.subarray(ivLength));
35
- const key = key_mac.slice(0, -macLength), mac = key_mac.subarray(-macLength);
36
- const actualMac = macMode.compute(key);
37
- if (!equalBytes(actualMac, mac))
31
+ const dec_wrapped = finalMode.decrypt(wrappedKey).reverse(), iv = dec_wrapped.subarray(0, ivLength), key_mac = cfb(cipher, iv, 32).decrypt(dec_wrapped.subarray(ivLength)), key = key_mac.slice(0, -macLength);
32
+ if (!equalBytes(macMode.compute(key), key_mac.subarray(-macLength)))
38
33
  throw new Error("Invalid MAC");
39
34
  return key;
40
35
  }
package/kupyna/index.d.ts CHANGED
@@ -1,28 +1,26 @@
1
1
  import { type CHash, type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
2
- declare abstract class Kupyna<T extends Kupyna<T>> implements Hash<Kupyna<T>> {
3
- readonly blockLen: number;
4
- readonly outputLen: number;
5
- readonly canXOF = false;
2
+ import { HashMD } from "@noble/hashes/_md.js";
3
+ declare abstract class Kupyna<T extends Kupyna<T>> extends HashMD<T> {
6
4
  private readonly rounds;
7
5
  private readonly stSize;
8
- private readonly threshold;
9
- protected s: BigUint64Array;
10
- protected x: Uint8Array;
11
- protected nx: number;
12
- protected len: bigint;
6
+ private readonly offs;
7
+ protected s: Uint32Array;
8
+ private readonly m;
9
+ private readonly ap;
10
+ private readonly aq;
11
+ private readonly tmp;
13
12
  constructor(blockLen: number);
13
+ protected get(): number[];
14
+ protected set(..._args: number[]): void;
15
+ protected process(buf: DataView, offset?: number): void;
16
+ protected roundClean(): void;
14
17
  destroy(): void;
15
- update(data: TArg<Uint8Array>): this;
16
- digest(): TRet<Uint8Array>;
17
- digestInto(buffer: TArg<Uint8Array>): void;
18
- private column;
18
+ digestInto(out: TArg<Uint8Array>): void;
19
19
  private G;
20
20
  private P;
21
21
  private Q;
22
22
  private outputTransform;
23
- private F;
24
- clone(): Kupyna<T>;
25
- abstract _cloneInto(to?: Kupyna<T>): Kupyna<T>;
23
+ _cloneInto(to?: T): T;
26
24
  }
27
25
  declare abstract class KupynaDerived<T extends Kupyna<T>> implements Hash<KupynaDerived<T>> {
28
26
  private readonly slice;
@@ -32,7 +30,7 @@ declare abstract class KupynaDerived<T extends Kupyna<T>> implements Hash<Kupyna
32
30
  protected h: Hash<T>;
33
31
  constructor(hash: CHash, slice: number);
34
32
  destroy(): void;
35
- abstract _cloneInto(to?: KupynaDerived<T>): KupynaDerived<T>;
33
+ _cloneInto(to?: KupynaDerived<T>): KupynaDerived<T>;
36
34
  clone(): KupynaDerived<T>;
37
35
  update(data: TArg<Uint8Array>): this;
38
36
  digest(): TRet<Uint8Array>;
@@ -41,16 +39,10 @@ declare abstract class KupynaDerived<T extends Kupyna<T>> implements Hash<Kupyna
41
39
  /** Internal Kupyna-256 hash class */
42
40
  export declare class _Kupyna256 extends Kupyna<_Kupyna256> {
43
41
  constructor();
44
- _cloneInto(to?: _Kupyna256): _Kupyna256;
45
- /** Create hash instance */
46
- static create(): _Kupyna256;
47
42
  }
48
43
  /** Internal Kupyna-512 hash class */
49
44
  export declare class _Kupyna512 extends Kupyna<_Kupyna512> {
50
45
  constructor();
51
- _cloneInto(to?: _Kupyna512): _Kupyna512;
52
- /** Create hash instance */
53
- static create(): _Kupyna512;
54
46
  }
55
47
  /**
56
48
  * Kupyna-256 hash function
@@ -65,20 +57,7 @@ export declare class _Kupyna512 extends Kupyna<_Kupyna512> {
65
57
  * kupyna256.create().update(new Uint8Array([97, 98, 99])).digest();
66
58
  * ```
67
59
  */
68
- export declare const kupyna256: {
69
- outputLen: number;
70
- blockLen: number;
71
- canXOF: boolean;
72
- } & import("@noble/hashes/utils.js").HashInfo & {
73
- (msg: TArg<Uint8Array>): TRet<Uint8Array>;
74
- create(): _Kupyna256;
75
- } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
76
- outputLen: number;
77
- blockLen: number;
78
- canXOF: boolean;
79
- oid?: TRet<Uint8Array> | undefined;
80
- create: () => _Kupyna256;
81
- };
60
+ export declare const kupyna256: TRet<CHash<_Kupyna256>>;
82
61
  /**
83
62
  * Kupyna-512 hash function
84
63
  *
@@ -92,40 +71,18 @@ export declare const kupyna256: {
92
71
  * kupyna512.create().update(new Uint8Array([97, 98, 99])).digest();
93
72
  * ```
94
73
  */
95
- export declare const kupyna512: {
96
- outputLen: number;
97
- blockLen: number;
98
- canXOF: boolean;
99
- } & import("@noble/hashes/utils.js").HashInfo & {
100
- (msg: TArg<Uint8Array>): TRet<Uint8Array>;
101
- create(): _Kupyna512;
102
- } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
103
- outputLen: number;
104
- blockLen: number;
105
- canXOF: boolean;
106
- oid?: TRet<Uint8Array> | undefined;
107
- create: () => _Kupyna512;
108
- };
74
+ export declare const kupyna512: TRet<CHash<_Kupyna512>>;
109
75
  /** Internal Kupyna-48 hash class */
110
76
  export declare class _Kupyna48 extends KupynaDerived<_Kupyna256> {
111
77
  constructor();
112
- _cloneInto(to?: _Kupyna48): _Kupyna48;
113
- /** Create hash instance */
114
- static create(): _Kupyna48;
115
78
  }
116
79
  /** Internal Kupyna-304 hash class */
117
80
  export declare class _Kupyna304 extends KupynaDerived<_Kupyna512> {
118
81
  constructor();
119
- _cloneInto(to?: _Kupyna304): _Kupyna304;
120
- /** Create hash instance */
121
- static create(): _Kupyna304;
122
82
  }
123
83
  /** Internal Kupyna-384 hash class */
124
84
  export declare class _Kupyna384 extends KupynaDerived<_Kupyna512> {
125
85
  constructor();
126
- _cloneInto(to?: _Kupyna384): _Kupyna384;
127
- /** Create hash instance */
128
- static create(): _Kupyna384;
129
86
  }
130
87
  /**
131
88
  * Kupyna-48 hash function
@@ -140,20 +97,7 @@ export declare class _Kupyna384 extends KupynaDerived<_Kupyna512> {
140
97
  * kupyna48.create().update(new Uint8Array([97, 98, 99])).digest();
141
98
  * ```
142
99
  */
143
- export declare const kupyna48: {
144
- outputLen: number;
145
- blockLen: number;
146
- canXOF: boolean;
147
- } & import("@noble/hashes/utils.js").HashInfo & {
148
- (msg: TArg<Uint8Array>): TRet<Uint8Array>;
149
- create(): _Kupyna48;
150
- } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
151
- outputLen: number;
152
- blockLen: number;
153
- canXOF: boolean;
154
- oid?: TRet<Uint8Array> | undefined;
155
- create: () => _Kupyna48;
156
- };
100
+ export declare const kupyna48: TRet<CHash<_Kupyna48>>;
157
101
  /**
158
102
  * Kupyna-304 hash function
159
103
  *
@@ -167,20 +111,7 @@ export declare const kupyna48: {
167
111
  * kupyna304.create().update(new Uint8Array([97, 98, 99])).digest();
168
112
  * ```
169
113
  */
170
- export declare const kupyna304: {
171
- outputLen: number;
172
- blockLen: number;
173
- canXOF: boolean;
174
- } & import("@noble/hashes/utils.js").HashInfo & {
175
- (msg: TArg<Uint8Array>): TRet<Uint8Array>;
176
- create(): _Kupyna304;
177
- } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
178
- outputLen: number;
179
- blockLen: number;
180
- canXOF: boolean;
181
- oid?: TRet<Uint8Array> | undefined;
182
- create: () => _Kupyna304;
183
- };
114
+ export declare const kupyna304: TRet<CHash<_Kupyna304>>;
184
115
  /**
185
116
  * Kupyna-384 hash function
186
117
  *
@@ -194,18 +125,5 @@ export declare const kupyna304: {
194
125
  * kupyna384.create().update(new Uint8Array([97, 98, 99])).digest();
195
126
  * ```
196
127
  */
197
- export declare const kupyna384: {
198
- outputLen: number;
199
- blockLen: number;
200
- canXOF: boolean;
201
- } & import("@noble/hashes/utils.js").HashInfo & {
202
- (msg: TArg<Uint8Array>): TRet<Uint8Array>;
203
- create(): _Kupyna384;
204
- } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
205
- outputLen: number;
206
- blockLen: number;
207
- canXOF: boolean;
208
- oid?: TRet<Uint8Array> | undefined;
209
- create: () => _Kupyna384;
210
- };
211
- export * from "./kmac.js";
128
+ export declare const kupyna384: TRet<CHash<_Kupyna384>>;
129
+ export {};