@li0ard/dstu 0.1.9 → 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 (55) hide show
  1. package/README.md +1 -1
  2. package/const.d.ts +5 -14
  3. package/const.js +47 -1010
  4. package/dstu4145/const.js +15 -15
  5. package/dstu4145/ec/index.d.ts +4 -0
  6. package/dstu4145/ec/index.js +28 -27
  7. package/dstu4145/ecies.d.ts +2 -2
  8. package/dstu4145/ecies.js +13 -2
  9. package/dstu4145/gf2m/utils.js +1 -1
  10. package/dstu4145/index.js +26 -17
  11. package/dstu9041/curve.d.ts +1 -1
  12. package/dstu9041/curve.js +16 -29
  13. package/dstu9041/index.js +21 -25
  14. package/dstu9311/const.js +12 -12
  15. package/dstu9311/index.d.ts +1 -2
  16. package/dstu9311/index.js +37 -34
  17. package/gf/index.d.ts +5 -0
  18. package/gf/index.js +62 -0
  19. package/gost34311/index.d.ts +55 -0
  20. package/gost34311/index.js +167 -0
  21. package/index.d.ts +1 -0
  22. package/index.js +1 -0
  23. package/iso15946.d.ts +1 -1
  24. package/iso15946.js +11 -6
  25. package/kalyna/index.d.ts +3 -5
  26. package/kalyna/index.js +119 -98
  27. package/keywrap.js +11 -13
  28. package/kupyna/index.d.ts +100 -133
  29. package/kupyna/index.js +192 -180
  30. package/modes/_guard.d.ts +4 -0
  31. package/modes/_guard.js +6 -0
  32. package/modes/cbc.d.ts +1 -1
  33. package/modes/cbc.js +6 -7
  34. package/modes/ccm.js +13 -4
  35. package/modes/cfb.js +43 -51
  36. package/modes/cmac.js +6 -2
  37. package/modes/ctr.js +6 -4
  38. package/modes/ecb.js +2 -2
  39. package/modes/gcm.d.ts +4 -1
  40. package/modes/gcm.js +33 -5
  41. package/modes/gmac.js +8 -9
  42. package/modes/index.d.ts +1 -1
  43. package/modes/index.js +1 -1
  44. package/modes/kw.js +10 -17
  45. package/modes/ofb.js +5 -4
  46. package/modes/xts.js +15 -25
  47. package/package.json +12 -9
  48. package/strumok/index.d.ts +4 -5
  49. package/strumok/index.js +132 -87
  50. package/utils.d.ts +7 -13
  51. package/utils.js +31 -61
  52. package/dstu9311/hash.d.ts +0 -41
  53. package/dstu9311/hash.js +0 -98
  54. package/kupyna/kmac.d.ts +0 -46
  55. package/kupyna/kmac.js +0 -125
package/kalyna/index.js CHANGED
@@ -1,21 +1,24 @@
1
- import { IS, IT, T, S } from "../const.js";
2
- import { bytesToUint64sLE, uint64sToBytesLE, byte } from "../utils.js";
3
- const swap_block = (k, N) => {
4
- if (N <= 1)
1
+ import { abool, abytes, anumber } from "@noble/hashes/utils.js";
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)
5
6
  return;
6
- const t = k[0];
7
- for (let i = 0; i < N - 1; i++)
8
- k[i] = k[i + 1];
9
- 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;
10
11
  };
11
- export const column = (x, i, N, c1, c2, c3, c4, c5, c6, c7, table = T, sign = -1) => (table[0][byte(x[(i + N) % N])] ^
12
- table[1][byte(x[(i + sign * c1 + N) % N] >> 8n)] ^
13
- table[2][byte(x[(i + sign * c2 + N) % N] >> 16n)] ^
14
- table[3][byte(x[(i + sign * c3 + N) % N] >> 24n)] ^
15
- table[4][byte(x[(i + sign * c4 + N) % N] >> 32n)] ^
16
- table[5][byte(x[(i + sign * c5 + N) % N] >> 40n)] ^
17
- table[6][byte(x[(i + sign * c6 + N) % N] >> 48n)] ^
18
- 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);
19
22
  export class Kalyna {
20
23
  N;
21
24
  /** Block size */
@@ -30,14 +33,15 @@ export class Kalyna {
30
33
  /** Kalyna abstract class */
31
34
  constructor(key, N, isDouble = false) {
32
35
  this.N = N;
36
+ anumber(N, "N");
37
+ abool(isDouble, "isDouble");
33
38
  if (N < 2 || (N & (N - 1)) !== 0)
34
39
  throw new Error("N must be power of 2 and >= 2");
35
40
  this.blockSize = N << 3;
36
41
  this.keySize = this.blockSize;
37
42
  if (isDouble)
38
43
  this.keySize *= 2;
39
- if (key.length !== this.keySize)
40
- throw new Error("Invalid key length");
44
+ abytes(key, this.keySize, "key");
41
45
  this.wordOffsets = Array.from({ length: 8 }, (_, j) => Math.floor(j * this.N / 8));
42
46
  const X = 6 + 4 * Math.log2(N) + (isDouble ? 4 : 0);
43
47
  this.numRounds = X - (N > 2 || isDouble ? 1 : 0);
@@ -45,142 +49,159 @@ export class Kalyna {
45
49
  this.expandKey(key);
46
50
  }
47
51
  expandKey(key) {
48
- 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);
49
- t1[0] = isDoubleKey ? BigInt(2 * this.N + 2 * log2N + 1) : BigInt(2 * this.N + 1);
50
- const keys = bytesToUint64sLE(key);
51
- let k = new BigUint64Array(isDoubleKey ? this.N * 2 : this.N);
52
- 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);
53
56
  if (isDoubleKey) {
54
- const ka = keys.subarray(0, this.N);
57
+ const ka = keys.subarray(0, N2);
55
58
  this.addkey(t1, t2, ka);
56
- this.G(t2, t1, keys.subarray(this.N));
59
+ this.G(t2, t1, keys.subarray(N2));
57
60
  this.GL(t1, t2, ka);
58
61
  this.G(t2, ks, _0);
59
62
  k.set(keys);
60
63
  }
61
64
  else {
62
- k.set(keys.subarray(0, this.N));
65
+ k.set(keys.subarray(0, N2));
63
66
  this.addkey(t1, t2, keys);
64
67
  this.G(t2, t1, keys);
65
68
  this.GL(t1, t2, keys);
66
69
  this.G(t2, ks, _0);
67
70
  }
68
- let constant = 0x0001000100010001n;
71
+ let cl = 0x00010001, ch = 0x00010001;
69
72
  for (let i = 0; i < R; i++) {
70
- const offset = i * (this.N * 2);
73
+ const offset = i * N2;
71
74
  if (i > 0) {
72
75
  if (!isDoubleKey)
73
76
  swap_block(k, this.N);
74
77
  else if (i % 2 === 0)
75
- swap_block(k, this.N * 2);
78
+ swap_block(k, N2);
76
79
  }
77
80
  const keySource = isDoubleKey
78
- ? (i % 2 === 0 ? k.subarray(0, this.N) : k.subarray(this.N))
81
+ ? (i % 2 === 0 ? k.subarray(0, N2) : k.subarray(N2))
79
82
  : k;
80
- for (let i = 0; i < this.N; i++)
81
- 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
+ }
82
88
  this.addkey(keySource, t2, ksc);
83
89
  this.G(t2, t1, ksc);
84
- this.GL(t1, rk.subarray(offset), ksc);
90
+ this.GL(t1, rk.subarray(offset * 2), ksc);
85
91
  if (i < R - 1)
86
- this.makeOddKey(rk.subarray(offset), rk.subarray(offset + this.N));
87
- 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;
88
95
  }
89
96
  this.erk = rk.slice();
90
97
  for (let i = ((R * 2 - 3) * this.N); i > 0; i -= this.N)
91
- this.IMC(rk.subarray(i));
98
+ this.IMC(rk.subarray(i * 2));
92
99
  this.drk = rk;
93
100
  }
94
101
  makeOddKey(evenkey, oddkey) {
95
- const offset = 2 * this.N + 3;
96
- const evenkeys = uint64sToBytesLE(evenkey);
97
- const oddkeys = uint64sToBytesLE(oddkey);
98
- oddkeys.set(evenkeys.subarray(offset, this.blockSize));
99
- oddkeys.set(evenkeys.subarray(0, offset), this.blockSize - offset);
100
- 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));
101
106
  }
102
- addkey(x, y, k) {
103
- for (let i = 0; i < this.N; i++)
104
- y[i] = x[i] + k[i];
105
- }
106
- subkey(x, y, k) {
107
- for (let i = 0; i < this.N; i++)
108
- 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
+ }
109
113
  }
110
- column(x, i) {
111
- 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
+ }
112
120
  }
113
- invColumn(x, i) {
114
- 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
+ }
115
128
  }
116
- G(x, y, k) {
117
- for (let i = 0; i < this.N; i++)
118
- 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
+ }
119
137
  }
120
- GL(x, y, k) {
121
- for (let i = 0; i < this.N; i++)
122
- 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
+ }
123
145
  }
124
146
  IMC(x) {
125
- for (let i = 0; i < this.N; i++)
126
- x[i] = IT[0][S[0][byte(x[i])]] ^
127
- IT[1][S[1][byte(x[i] >> 8n)]] ^
128
- IT[2][S[2][byte(x[i] >> 16n)]] ^
129
- IT[3][S[3][byte(x[i] >> 24n)]] ^
130
- IT[4][S[0][byte(x[i] >> 32n)]] ^
131
- IT[5][S[1][byte(x[i] >> 40n)]] ^
132
- IT[6][S[2][byte(x[i] >> 48n)]] ^
133
- IT[7][S[3][byte(x[i] >> 56n)]];
134
- }
135
- IG(x, y, k) {
136
- for (let i = 0; i < this.N; i++)
137
- 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
+ }
138
156
  }
139
157
  IGL(x, y) {
158
+ const m = this.N - 1;
159
+ const [is0, is1, is2, is3] = IS;
140
160
  for (let i = 0; i < this.N; i++) {
141
- const result = BigInt(IS[0][byte(x[i % this.N])]) ^
142
- (BigInt(IS[1][byte(x[(i + this.wordOffsets[1]) % this.N] >> 8n)]) << 8n) ^
143
- (BigInt(IS[2][byte(x[(i + this.wordOffsets[2]) % this.N] >> 16n)]) << 16n) ^
144
- (BigInt(IS[3][byte(x[(i + this.wordOffsets[3]) % this.N] >> 24n)]) << 24n) ^
145
- (BigInt(IS[0][byte(x[(i + this.wordOffsets[4]) % this.N] >> 32n)]) << 32n) ^
146
- (BigInt(IS[1][byte(x[(i + this.wordOffsets[5]) % this.N] >> 40n)]) << 40n) ^
147
- (BigInt(IS[2][byte(x[(i + this.wordOffsets[6]) % this.N] >> 48n)]) << 48n) ^
148
- (BigInt(IS[3][byte(x[(i + this.wordOffsets[7]) % this.N] >> 56n)]) << 56n);
149
- 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;
150
173
  }
151
174
  }
152
175
  /** Encrypt block */
153
176
  encrypt(block) {
154
- if (block.length != this.blockSize)
155
- throw new Error(`Incorrect length (need - ${this.blockSize}, got - ${block.length})`);
156
- const t1 = new BigUint64Array(this.N), t2 = new BigUint64Array(this.N);
157
- this.addkey(bytesToUint64sLE(block), t1, this.erk);
177
+ abytes(block, this.blockSize, "block");
178
+ const t1 = new Uint32Array(this.N * 2), t2 = new Uint32Array(this.N * 2);
179
+ this.addkey(bytesToU32LE(block), t1, this.erk);
158
180
  for (let i = 0; i < this.numRounds; i++) {
159
- const roundKey = this.erk.subarray(this.N + i * this.N);
181
+ const ko = (this.N + i * this.N) * 2;
160
182
  if (i % 2 === 0)
161
- this.G(t1, t2, roundKey);
183
+ this.G(t1, t2, this.erk, ko);
162
184
  else
163
- this.G(t2, t1, roundKey);
185
+ this.G(t2, t1, this.erk, ko);
164
186
  }
165
- this.GL(t2, t1, this.erk.subarray(this.glOffset));
166
- return uint64sToBytesLE(t1);
187
+ this.GL(t2, t1, this.erk, this.glOffset * 2);
188
+ return u32ToBytesLE(t1);
167
189
  }
168
190
  /** Decrypt block */
169
191
  decrypt(block) {
170
- if (block.length != this.blockSize)
171
- throw new Error(`Incorrect length (need - ${this.blockSize}, got - ${block.length})`);
172
- const t1 = new BigUint64Array(this.N), t2 = new BigUint64Array(this.N);
173
- this.subkey(bytesToUint64sLE(block), t1, this.drk.subarray(this.glOffset));
192
+ abytes(block, this.blockSize, "block");
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
@@ -1,37 +1,35 @@
1
- import { equalBytes, concatBytes, randomBytes } from "@noble/curves/utils.js";
1
+ import { equalBytes, concatBytes, randomBytes, abytes, abool } from "@noble/curves/utils.js";
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`) */
13
13
  export const keyWrap = (kek, useDstu9311 = false) => {
14
14
  if (kek.length != 32)
15
15
  throw new Error("Invalid key length");
16
+ abytes(kek, 32, "kek");
17
+ abool(useDstu9311, "useDstu9311");
16
18
  const cipher = new (useDstu9311 ? Dstu9311 : Kalyna256)(kek);
17
19
  const ivLength = useDstu9311 ? 8 : 32, macLength = useDstu9311 ? 4 : 32;
18
- const macMode = cmac(cipher, macLength);
19
- 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);
20
21
  return Object.freeze({
21
22
  wrap: (key, iv) => {
23
+ abytes(key, undefined, "key");
22
24
  iv ??= randomBytes(ivLength);
23
- if (iv.length != ivLength)
24
- throw new Error("Invalid IV length");
25
+ abytes(iv, ivLength, "iv");
25
26
  const mac = macMode.compute(key);
26
27
  return finalMode.encrypt(concatBytes(iv, cfb(cipher, iv, 32).encrypt(concatBytes(key, mac))).reverse());
27
28
  },
28
29
  unwrap: (wrappedKey) => {
29
- const dec_wrapped = finalMode.decrypt(wrappedKey).reverse();
30
- const iv = dec_wrapped.subarray(0, ivLength);
31
- const key_mac = cfb(cipher, iv, 32).decrypt(dec_wrapped.subarray(ivLength));
32
- const key = key_mac.slice(0, -macLength), mac = key_mac.subarray(-macLength);
33
- const actualMac = macMode.compute(key);
34
- if (!equalBytes(actualMac, mac))
30
+ abytes(wrappedKey, undefined, "wrappedKey");
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)))
35
33
  throw new Error("Invalid MAC");
36
34
  return key;
37
35
  }
package/kupyna/index.d.ts CHANGED
@@ -1,162 +1,129 @@
1
- import { 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;
1
+ import { type CHash, type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
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
- abstract clone(): Kupyna<T>;
25
- abstract _cloneInto(): Kupyna<T>;
23
+ _cloneInto(to?: T): T;
26
24
  }
27
25
  declare abstract class KupynaDerived<T extends Kupyna<T>> implements Hash<KupynaDerived<T>> {
28
- hash: () => Kupyna<T>;
29
26
  private readonly slice;
30
27
  readonly outputLen: number;
31
28
  readonly blockLen: number;
32
29
  readonly canXOF = false;
33
- buffer: Uint8Array;
34
- constructor(hash: () => Kupyna<T>, slice: number);
30
+ protected h: Hash<T>;
31
+ constructor(hash: CHash, slice: number);
35
32
  destroy(): void;
36
- abstract clone(): KupynaDerived<T>;
37
- abstract _cloneInto(): KupynaDerived<T>;
33
+ _cloneInto(to?: KupynaDerived<T>): KupynaDerived<T>;
34
+ clone(): KupynaDerived<T>;
38
35
  update(data: TArg<Uint8Array>): this;
39
36
  digest(): TRet<Uint8Array>;
40
37
  digestInto(buffer: TArg<Uint8Array>): void;
41
38
  }
42
- /** Kupyna 256 bit version */
43
- export declare class Kupyna256 extends Kupyna<Kupyna256> {
44
- /** Kupyna 256 bit version */
39
+ /** Internal Kupyna-256 hash class */
40
+ export declare class _Kupyna256 extends Kupyna<_Kupyna256> {
45
41
  constructor();
46
- _cloneInto(to?: Kupyna256): Kupyna256;
47
- clone(): Kupyna256;
48
- /** Create hash instance */
49
- static create(): Kupyna256;
50
42
  }
51
- /** Kupyna 512 bit version */
52
- export declare class Kupyna512 extends Kupyna<Kupyna512> {
53
- /** Kupyna 512 bit version */
43
+ /** Internal Kupyna-512 hash class */
44
+ export declare class _Kupyna512 extends Kupyna<_Kupyna512> {
54
45
  constructor();
55
- _cloneInto(to?: Kupyna512): Kupyna512;
56
- clone(): Kupyna512;
57
- /** Create hash instance */
58
- static create(): Kupyna512;
59
46
  }
60
- /** Kupyna 48 bit version */
61
- export declare class Kupyna48 extends KupynaDerived<Kupyna256> {
62
- /** Kupyna 48 bit version */
47
+ /**
48
+ * Kupyna-256 hash function
49
+ *
50
+ * @param msg - message bytes to hash.
51
+ * @returns Digest bytes.
52
+ * @example
53
+ * ```ts
54
+ * import { kupyna256 } from "@li0ard/dstu/kupyna.js";
55
+ *
56
+ * kupyna256(new Uint8Array([97, 98, 99]));
57
+ * kupyna256.create().update(new Uint8Array([97, 98, 99])).digest();
58
+ * ```
59
+ */
60
+ export declare const kupyna256: TRet<CHash<_Kupyna256>>;
61
+ /**
62
+ * Kupyna-512 hash function
63
+ *
64
+ * @param msg - message bytes to hash.
65
+ * @returns Digest bytes.
66
+ * @example
67
+ * ```ts
68
+ * import { kupyna512 } from "@li0ard/dstu/kupyna.js";
69
+ *
70
+ * kupyna512(new Uint8Array([97, 98, 99]));
71
+ * kupyna512.create().update(new Uint8Array([97, 98, 99])).digest();
72
+ * ```
73
+ */
74
+ export declare const kupyna512: TRet<CHash<_Kupyna512>>;
75
+ /** Internal Kupyna-48 hash class */
76
+ export declare class _Kupyna48 extends KupynaDerived<_Kupyna256> {
63
77
  constructor();
64
- _cloneInto(to?: Kupyna48): Kupyna48;
65
- clone(): Kupyna48;
66
- /** Create hash instance */
67
- static create(): Kupyna48;
68
78
  }
69
- /** Kupyna 304 bit version */
70
- export declare class Kupyna304 extends KupynaDerived<Kupyna512> {
71
- /** Kupyna 304 bit version */
79
+ /** Internal Kupyna-304 hash class */
80
+ export declare class _Kupyna304 extends KupynaDerived<_Kupyna512> {
72
81
  constructor();
73
- _cloneInto(to?: Kupyna304): Kupyna304;
74
- clone(): Kupyna304;
75
- /** Create hash instance */
76
- static create(): Kupyna304;
77
82
  }
78
- /** Kupyna 384 bit version */
79
- export declare class Kupyna384 extends KupynaDerived<Kupyna512> {
80
- /** Kupyna 384 bit version */
83
+ /** Internal Kupyna-384 hash class */
84
+ export declare class _Kupyna384 extends KupynaDerived<_Kupyna512> {
81
85
  constructor();
82
- _cloneInto(to?: Kupyna384): Kupyna384;
83
- clone(): Kupyna384;
84
- /** Create hash instance */
85
- static create(): Kupyna384;
86
86
  }
87
- /** Kupyna 48 bit version */
88
- export declare const kupyna48: {
89
- outputLen: number;
90
- blockLen: number;
91
- canXOF: boolean;
92
- } & import("@noble/hashes/utils.js").HashInfo & {
93
- (msg: TArg<Uint8Array>): TRet<Uint8Array>;
94
- create(): Kupyna48;
95
- } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
96
- outputLen: number;
97
- blockLen: number;
98
- canXOF: boolean;
99
- oid?: TRet<Uint8Array> | undefined;
100
- create: () => Kupyna48;
101
- };
102
- /** Kupyna 256 bit version */
103
- export declare const kupyna256: {
104
- outputLen: number;
105
- blockLen: number;
106
- canXOF: boolean;
107
- } & import("@noble/hashes/utils.js").HashInfo & {
108
- (msg: TArg<Uint8Array>): TRet<Uint8Array>;
109
- create(): Kupyna256;
110
- } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
111
- outputLen: number;
112
- blockLen: number;
113
- canXOF: boolean;
114
- oid?: TRet<Uint8Array> | undefined;
115
- create: () => Kupyna256;
116
- };
117
- /** Kupyna 304 bit version */
118
- export declare const kupyna304: {
119
- outputLen: number;
120
- blockLen: number;
121
- canXOF: boolean;
122
- } & import("@noble/hashes/utils.js").HashInfo & {
123
- (msg: TArg<Uint8Array>): TRet<Uint8Array>;
124
- create(): Kupyna304;
125
- } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
126
- outputLen: number;
127
- blockLen: number;
128
- canXOF: boolean;
129
- oid?: TRet<Uint8Array> | undefined;
130
- create: () => Kupyna304;
131
- };
132
- /** Kupyna 384 bit version */
133
- export declare const kupyna384: {
134
- outputLen: number;
135
- blockLen: number;
136
- canXOF: boolean;
137
- } & import("@noble/hashes/utils.js").HashInfo & {
138
- (msg: TArg<Uint8Array>): TRet<Uint8Array>;
139
- create(): Kupyna384;
140
- } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
141
- outputLen: number;
142
- blockLen: number;
143
- canXOF: boolean;
144
- oid?: TRet<Uint8Array> | undefined;
145
- create: () => Kupyna384;
146
- };
147
- /** Kupyna 512 bit version */
148
- export declare const kupyna512: {
149
- outputLen: number;
150
- blockLen: number;
151
- canXOF: boolean;
152
- } & import("@noble/hashes/utils.js").HashInfo & {
153
- (msg: TArg<Uint8Array>): TRet<Uint8Array>;
154
- create(): Kupyna512;
155
- } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
156
- outputLen: number;
157
- blockLen: number;
158
- canXOF: boolean;
159
- oid?: TRet<Uint8Array> | undefined;
160
- create: () => Kupyna512;
161
- };
162
- export * from "./kmac.js";
87
+ /**
88
+ * Kupyna-48 hash function
89
+ *
90
+ * @param msg - message bytes to hash.
91
+ * @returns Digest bytes.
92
+ * @example
93
+ * ```ts
94
+ * import { kupyna48 } from "@li0ard/dstu/kupyna.js";
95
+ *
96
+ * kupyna48(new Uint8Array([97, 98, 99]));
97
+ * kupyna48.create().update(new Uint8Array([97, 98, 99])).digest();
98
+ * ```
99
+ */
100
+ export declare const kupyna48: TRet<CHash<_Kupyna48>>;
101
+ /**
102
+ * Kupyna-304 hash function
103
+ *
104
+ * @param msg - message bytes to hash.
105
+ * @returns Digest bytes.
106
+ * @example
107
+ * ```ts
108
+ * import { kupyna304 } from "@li0ard/dstu/kupyna.js";
109
+ *
110
+ * kupyna304(new Uint8Array([97, 98, 99]));
111
+ * kupyna304.create().update(new Uint8Array([97, 98, 99])).digest();
112
+ * ```
113
+ */
114
+ export declare const kupyna304: TRet<CHash<_Kupyna304>>;
115
+ /**
116
+ * Kupyna-384 hash function
117
+ *
118
+ * @param msg - message bytes to hash.
119
+ * @returns Digest bytes.
120
+ * @example
121
+ * ```ts
122
+ * import { kupyna384 } from "@li0ard/dstu/kupyna.js";
123
+ *
124
+ * kupyna384(new Uint8Array([97, 98, 99]));
125
+ * kupyna384.create().update(new Uint8Array([97, 98, 99])).digest();
126
+ * ```
127
+ */
128
+ export declare const kupyna384: TRet<CHash<_Kupyna384>>;
129
+ export {};