@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.
- package/README.md +1 -1
- package/const.d.ts +5 -14
- package/const.js +47 -1010
- package/dstu4145/const.js +15 -15
- package/dstu4145/ec/index.d.ts +4 -0
- package/dstu4145/ec/index.js +28 -27
- package/dstu4145/ecies.d.ts +2 -2
- package/dstu4145/ecies.js +13 -2
- package/dstu4145/gf2m/utils.js +1 -1
- package/dstu4145/index.js +26 -17
- package/dstu9041/curve.d.ts +1 -1
- package/dstu9041/curve.js +16 -29
- package/dstu9041/index.js +21 -25
- package/dstu9311/const.js +12 -12
- package/dstu9311/index.d.ts +1 -2
- package/dstu9311/index.js +37 -34
- package/gf/index.d.ts +5 -0
- package/gf/index.js +62 -0
- package/gost34311/index.d.ts +55 -0
- package/gost34311/index.js +167 -0
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/iso15946.d.ts +1 -1
- package/iso15946.js +11 -6
- package/kalyna/index.d.ts +3 -5
- package/kalyna/index.js +119 -98
- package/keywrap.js +11 -13
- package/kupyna/index.d.ts +100 -133
- package/kupyna/index.js +192 -180
- package/modes/_guard.d.ts +4 -0
- package/modes/_guard.js +6 -0
- package/modes/cbc.d.ts +1 -1
- package/modes/cbc.js +6 -7
- package/modes/ccm.js +13 -4
- package/modes/cfb.js +43 -51
- package/modes/cmac.js +6 -2
- package/modes/ctr.js +6 -4
- package/modes/ecb.js +2 -2
- package/modes/gcm.d.ts +4 -1
- package/modes/gcm.js +33 -5
- package/modes/gmac.js +8 -9
- package/modes/index.d.ts +1 -1
- package/modes/index.js +1 -1
- package/modes/kw.js +10 -17
- package/modes/ofb.js +5 -4
- package/modes/xts.js +15 -25
- package/package.json +12 -9
- package/strumok/index.d.ts +4 -5
- package/strumok/index.js +132 -87
- package/utils.d.ts +7 -13
- package/utils.js +31 -61
- package/dstu9311/hash.d.ts +0 -41
- package/dstu9311/hash.js +0 -98
- package/kupyna/kmac.d.ts +0 -46
- package/kupyna/kmac.js +0 -125
package/kalyna/index.js
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
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
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
k[
|
|
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,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
table[
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
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
|
|
49
|
-
t1[0] = isDoubleKey ?
|
|
50
|
-
const keys =
|
|
51
|
-
|
|
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,
|
|
57
|
+
const ka = keys.subarray(0, N2);
|
|
55
58
|
this.addkey(t1, t2, ka);
|
|
56
|
-
this.G(t2, t1, keys.subarray(
|
|
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,
|
|
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
|
|
71
|
+
let cl = 0x00010001, ch = 0x00010001;
|
|
69
72
|
for (let i = 0; i < R; i++) {
|
|
70
|
-
const offset = i *
|
|
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,
|
|
78
|
+
swap_block(k, N2);
|
|
76
79
|
}
|
|
77
80
|
const keySource = isDoubleKey
|
|
78
|
-
? (i % 2 === 0 ? k.subarray(0,
|
|
81
|
+
? (i % 2 === 0 ? k.subarray(0, N2) : k.subarray(N2))
|
|
79
82
|
: k;
|
|
80
|
-
for (let
|
|
81
|
-
|
|
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
|
-
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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
|
-
|
|
111
|
-
|
|
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
|
-
|
|
114
|
-
|
|
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
|
-
|
|
117
|
-
for (let i = 0; i < this.N; i++)
|
|
118
|
-
|
|
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
|
-
|
|
121
|
-
for (let i = 0; i < this.N; i++)
|
|
122
|
-
|
|
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
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
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
|
|
142
|
-
|
|
143
|
-
(
|
|
144
|
-
(
|
|
145
|
-
(
|
|
146
|
-
|
|
147
|
-
(
|
|
148
|
-
(
|
|
149
|
-
|
|
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
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
|
181
|
+
const ko = (this.N + i * this.N) * 2;
|
|
160
182
|
if (i % 2 === 0)
|
|
161
|
-
this.G(t1, t2,
|
|
183
|
+
this.G(t1, t2, this.erk, ko);
|
|
162
184
|
else
|
|
163
|
-
this.G(t2, t1,
|
|
185
|
+
this.G(t2, t1, this.erk, ko);
|
|
164
186
|
}
|
|
165
|
-
this.GL(t2, t1, this.erk
|
|
166
|
-
return
|
|
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
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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
|
|
197
|
+
const ko = (this.glOffset - this.N - i * this.N) * 2;
|
|
177
198
|
if (i % 2 === 0)
|
|
178
|
-
this.IG(t1, t2,
|
|
199
|
+
this.IG(t1, t2, this.drk, ko);
|
|
179
200
|
else
|
|
180
|
-
this.IG(t2, t1,
|
|
201
|
+
this.IG(t2, t1, this.drk, ko);
|
|
181
202
|
}
|
|
182
203
|
this.IGL(t2, t1);
|
|
183
|
-
return
|
|
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
|
-
|
|
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
|
-
|
|
30
|
-
const iv = dec_wrapped.subarray(0, ivLength);
|
|
31
|
-
|
|
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
|
-
|
|
3
|
-
|
|
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
|
|
9
|
-
protected s:
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
34
|
-
constructor(hash:
|
|
30
|
+
protected h: Hash<T>;
|
|
31
|
+
constructor(hash: CHash, slice: number);
|
|
35
32
|
destroy(): void;
|
|
36
|
-
|
|
37
|
-
|
|
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
|
|
43
|
-
export declare class
|
|
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
|
|
52
|
-
export declare class
|
|
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
|
-
/**
|
|
61
|
-
|
|
62
|
-
|
|
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
|
|
70
|
-
export declare class
|
|
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
|
|
79
|
-
export declare class
|
|
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
|
-
/**
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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 {};
|