@li0ard/dstu 0.0.1 → 0.1.0
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/LICENSE +21 -0
- package/README.md +37 -45
- package/const.d.ts +14 -0
- package/const.js +1084 -0
- package/dstu4145/const.d.ts +22 -0
- package/dstu4145/const.js +36 -0
- package/dstu4145/ec/curve.d.ts +17 -0
- package/dstu4145/ec/curve.js +82 -0
- package/dstu4145/ec/field.d.ts +30 -0
- package/dstu4145/ec/field.js +135 -0
- package/dstu4145/ec/index.d.ts +3 -0
- package/dstu4145/ec/index.js +3 -0
- package/dstu4145/ec/point.d.ts +12 -0
- package/dstu4145/ec/point.js +79 -0
- package/dstu4145/index.d.ts +28 -0
- package/dstu4145/index.js +70 -0
- package/index.d.ts +6 -0
- package/index.js +6 -0
- package/kalyna/index.d.ts +50 -0
- package/kalyna/index.js +211 -0
- package/kupyna/index.d.ts +162 -0
- package/kupyna/index.js +241 -0
- package/kupyna/kmac.d.ts +46 -0
- package/kupyna/kmac.js +124 -0
- package/modes/cbc.d.ts +5 -0
- package/modes/cbc.js +10 -0
- package/modes/cfb.d.ts +5 -0
- package/modes/cfb.js +72 -0
- package/modes/ctr.d.ts +5 -0
- package/modes/ctr.js +24 -0
- package/modes/ecb.d.ts +4 -0
- package/modes/ecb.js +17 -0
- package/modes/gcm.d.ts +5 -0
- package/modes/gcm.js +20 -0
- package/modes/index.d.ts +7 -0
- package/modes/index.js +7 -0
- package/modes/mac.d.ts +6 -0
- package/modes/mac.js +65 -0
- package/modes/ofb.d.ts +5 -0
- package/modes/ofb.js +11 -0
- package/package.json +47 -7
- package/padding.d.ts +3 -0
- package/padding.js +22 -0
- package/strumok/index.d.ts +24 -0
- package/strumok/index.js +360 -0
- package/types.d.ts +41 -0
- package/types.js +1 -0
- package/utils.d.ts +14 -0
- package/utils.js +81 -0
package/kalyna/index.js
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
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)
|
|
5
|
+
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;
|
|
10
|
+
};
|
|
11
|
+
export class Kalyna {
|
|
12
|
+
N;
|
|
13
|
+
/** Block size */
|
|
14
|
+
blockSize;
|
|
15
|
+
/** Key size */
|
|
16
|
+
keySize;
|
|
17
|
+
erk;
|
|
18
|
+
drk;
|
|
19
|
+
numRounds;
|
|
20
|
+
glOffset;
|
|
21
|
+
wordOffsets;
|
|
22
|
+
/** Kalyna abstract class */
|
|
23
|
+
constructor(key, N, isDouble = false) {
|
|
24
|
+
this.N = N;
|
|
25
|
+
if (N < 2 || (N & (N - 1)) !== 0)
|
|
26
|
+
throw new Error("N must be power of 2 and >= 2");
|
|
27
|
+
this.blockSize = N << 3;
|
|
28
|
+
this.keySize = this.blockSize;
|
|
29
|
+
if (isDouble)
|
|
30
|
+
this.keySize *= 2;
|
|
31
|
+
if (key.length !== this.keySize)
|
|
32
|
+
throw new Error("Invalid key length");
|
|
33
|
+
this.wordOffsets = Array.from({ length: 8 }, (_, j) => Math.floor(j * this.N / 8));
|
|
34
|
+
const X = 6 + 4 * Math.log2(N) + (isDouble ? 4 : 0);
|
|
35
|
+
this.numRounds = X - (N > 2 || isDouble ? 1 : 0);
|
|
36
|
+
this.glOffset = X * N;
|
|
37
|
+
this.expandKey(key);
|
|
38
|
+
}
|
|
39
|
+
expandKey(key) {
|
|
40
|
+
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);
|
|
41
|
+
t1[0] = isDoubleKey ? BigInt(2 * this.N + 2 * log2N + 1) : BigInt(2 * this.N + 1);
|
|
42
|
+
const keys = bytesToUint64sLE(key);
|
|
43
|
+
let k = new BigUint64Array(isDoubleKey ? this.N * 2 : this.N);
|
|
44
|
+
const _0 = new BigUint64Array(this.N);
|
|
45
|
+
if (isDoubleKey) {
|
|
46
|
+
const ka = keys.subarray(0, this.N);
|
|
47
|
+
this.addkey(t1, t2, ka);
|
|
48
|
+
this.G(t2, t1, keys.subarray(this.N));
|
|
49
|
+
this.GL(t1, t2, ka);
|
|
50
|
+
this.G(t2, ks, _0);
|
|
51
|
+
k.set(keys);
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
k.set(keys.subarray(0, this.N));
|
|
55
|
+
this.addkey(t1, t2, keys);
|
|
56
|
+
this.G(t2, t1, keys);
|
|
57
|
+
this.GL(t1, t2, keys);
|
|
58
|
+
this.G(t2, ks, _0);
|
|
59
|
+
}
|
|
60
|
+
let constant = 0x0001000100010001n;
|
|
61
|
+
for (let i = 0; i < R; i++) {
|
|
62
|
+
const offset = i * (this.N * 2);
|
|
63
|
+
if (i > 0) {
|
|
64
|
+
if (!isDoubleKey)
|
|
65
|
+
swap_block(k, this.N);
|
|
66
|
+
else if (i % 2 === 0)
|
|
67
|
+
swap_block(k, this.N * 2);
|
|
68
|
+
}
|
|
69
|
+
const keySource = isDoubleKey
|
|
70
|
+
? (i % 2 === 0 ? k.subarray(0, this.N) : k.subarray(this.N))
|
|
71
|
+
: k;
|
|
72
|
+
for (let i = 0; i < this.N; i++)
|
|
73
|
+
ksc[i] = ks[i] + constant;
|
|
74
|
+
this.addkey(keySource, t2, ksc);
|
|
75
|
+
this.G(t2, t1, ksc);
|
|
76
|
+
this.GL(t1, rk.subarray(offset), ksc);
|
|
77
|
+
if (i < R - 1)
|
|
78
|
+
this.makeOddKey(rk.subarray(offset), rk.subarray(offset + this.N));
|
|
79
|
+
constant <<= 1n;
|
|
80
|
+
}
|
|
81
|
+
this.erk = rk.slice();
|
|
82
|
+
for (let i = ((R * 2 - 3) * this.N); i > 0; i -= this.N)
|
|
83
|
+
this.IMC(rk.subarray(i));
|
|
84
|
+
this.drk = rk;
|
|
85
|
+
}
|
|
86
|
+
makeOddKey(evenkey, oddkey) {
|
|
87
|
+
const offset = 2 * this.N + 3;
|
|
88
|
+
const evenkeys = uint64sToBytesLE(evenkey);
|
|
89
|
+
const oddkeys = uint64sToBytesLE(oddkey);
|
|
90
|
+
oddkeys.set(evenkeys.subarray(offset, this.blockSize));
|
|
91
|
+
oddkeys.set(evenkeys.subarray(0, offset), this.blockSize - offset);
|
|
92
|
+
oddkey.set(bytesToUint64sLE(oddkeys));
|
|
93
|
+
}
|
|
94
|
+
addkey(x, y, k) {
|
|
95
|
+
for (let i = 0; i < this.N; i++)
|
|
96
|
+
y[i] = x[i] + k[i];
|
|
97
|
+
}
|
|
98
|
+
subkey(x, y, k) {
|
|
99
|
+
for (let i = 0; i < this.N; i++)
|
|
100
|
+
y[i] = x[i] - k[i];
|
|
101
|
+
}
|
|
102
|
+
column(x, i) {
|
|
103
|
+
return T[0][byte(x[(i - this.wordOffsets[0] + this.N) % this.N])] ^
|
|
104
|
+
T[1][byte(x[(i - this.wordOffsets[1] + this.N) % this.N] >> 8n)] ^
|
|
105
|
+
T[2][byte(x[(i - this.wordOffsets[2] + this.N) % this.N] >> 16n)] ^
|
|
106
|
+
T[3][byte(x[(i - this.wordOffsets[3] + this.N) % this.N] >> 24n)] ^
|
|
107
|
+
T[4][byte(x[(i - this.wordOffsets[4] + this.N) % this.N] >> 32n)] ^
|
|
108
|
+
T[5][byte(x[(i - this.wordOffsets[5] + this.N) % this.N] >> 40n)] ^
|
|
109
|
+
T[6][byte(x[(i - this.wordOffsets[6] + this.N) % this.N] >> 48n)] ^
|
|
110
|
+
T[7][byte(x[(i - this.wordOffsets[7] + this.N) % this.N] >> 56n)];
|
|
111
|
+
}
|
|
112
|
+
G(x, y, k) {
|
|
113
|
+
for (let i = 0; i < this.N; i++)
|
|
114
|
+
y[i] = k[i] ^ this.column(x, i);
|
|
115
|
+
}
|
|
116
|
+
GL(x, y, k) {
|
|
117
|
+
for (let i = 0; i < this.N; i++)
|
|
118
|
+
y[i] = k[i] + this.column(x, i);
|
|
119
|
+
}
|
|
120
|
+
IMC(x) {
|
|
121
|
+
for (let i = 0; i < this.N; i++)
|
|
122
|
+
x[i] = IT[0][S[0][byte(x[i])]] ^
|
|
123
|
+
IT[1][S[1][byte(x[i] >> 8n)]] ^
|
|
124
|
+
IT[2][S[2][byte(x[i] >> 16n)]] ^
|
|
125
|
+
IT[3][S[3][byte(x[i] >> 24n)]] ^
|
|
126
|
+
IT[4][S[0][byte(x[i] >> 32n)]] ^
|
|
127
|
+
IT[5][S[1][byte(x[i] >> 40n)]] ^
|
|
128
|
+
IT[6][S[2][byte(x[i] >> 48n)]] ^
|
|
129
|
+
IT[7][S[3][byte(x[i] >> 56n)]];
|
|
130
|
+
}
|
|
131
|
+
IG(x, y, k) {
|
|
132
|
+
for (let i = 0; i < this.N; i++)
|
|
133
|
+
y[i] = k[i] ^
|
|
134
|
+
IT[0][byte(x[(i + this.wordOffsets[0]) % this.N])] ^
|
|
135
|
+
IT[1][byte(x[(i + this.wordOffsets[1]) % this.N] >> 8n)] ^
|
|
136
|
+
IT[2][byte(x[(i + this.wordOffsets[2]) % this.N] >> 16n)] ^
|
|
137
|
+
IT[3][byte(x[(i + this.wordOffsets[3]) % this.N] >> 24n)] ^
|
|
138
|
+
IT[4][byte(x[(i + this.wordOffsets[4]) % this.N] >> 32n)] ^
|
|
139
|
+
IT[5][byte(x[(i + this.wordOffsets[5]) % this.N] >> 40n)] ^
|
|
140
|
+
IT[6][byte(x[(i + this.wordOffsets[6]) % this.N] >> 48n)] ^
|
|
141
|
+
IT[7][byte(x[(i + this.wordOffsets[7]) % this.N] >> 56n)];
|
|
142
|
+
}
|
|
143
|
+
IGL(x, y, k) {
|
|
144
|
+
for (let i = 0; i < this.N; i++) {
|
|
145
|
+
let result = BigInt(IS[0][byte(x[(i + this.wordOffsets[0]) % this.N])]);
|
|
146
|
+
result ^= BigInt(IS[1][byte(x[(i + this.wordOffsets[1]) % this.N] >> 8n)]) << 8n;
|
|
147
|
+
result ^= BigInt(IS[2][byte(x[(i + this.wordOffsets[2]) % this.N] >> 16n)]) << 16n;
|
|
148
|
+
result ^= BigInt(IS[3][byte(x[(i + this.wordOffsets[3]) % this.N] >> 24n)]) << 24n;
|
|
149
|
+
result ^= BigInt(IS[0][byte(x[(i + this.wordOffsets[4]) % this.N] >> 32n)]) << 32n;
|
|
150
|
+
result ^= BigInt(IS[1][byte(x[(i + this.wordOffsets[5]) % this.N] >> 40n)]) << 40n;
|
|
151
|
+
result ^= BigInt(IS[2][byte(x[(i + this.wordOffsets[6]) % this.N] >> 48n)]) << 48n;
|
|
152
|
+
result ^= BigInt(IS[3][byte(x[(i + this.wordOffsets[7]) % this.N] >> 56n)]) << 56n;
|
|
153
|
+
y[i] = result - k[i];
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/** Encrypt block */
|
|
157
|
+
encrypt(block) {
|
|
158
|
+
if (block.length != this.blockSize)
|
|
159
|
+
throw new Error(`Incorrect length (need - ${this.blockSize}, got - ${block.length})`);
|
|
160
|
+
const t1 = new BigUint64Array(this.N), t2 = new BigUint64Array(this.N);
|
|
161
|
+
const rk = this.erk.slice();
|
|
162
|
+
this.addkey(bytesToUint64sLE(block), t1, rk);
|
|
163
|
+
for (let i = 0; i < this.numRounds; i++) {
|
|
164
|
+
const roundKey = rk.subarray(this.N + i * this.N);
|
|
165
|
+
if (i % 2 === 0)
|
|
166
|
+
this.G(t1, t2, roundKey);
|
|
167
|
+
else
|
|
168
|
+
this.G(t2, t1, roundKey);
|
|
169
|
+
}
|
|
170
|
+
this.GL(t2, t1, rk.subarray(this.glOffset));
|
|
171
|
+
return uint64sToBytesLE(t1);
|
|
172
|
+
}
|
|
173
|
+
/** Decrypt block */
|
|
174
|
+
decrypt(block) {
|
|
175
|
+
if (block.length != this.blockSize)
|
|
176
|
+
throw new Error(`Incorrect length (need - ${this.blockSize}, got - ${block.length})`);
|
|
177
|
+
const t1 = new BigUint64Array(this.N), t2 = new BigUint64Array(this.N);
|
|
178
|
+
const rk = this.drk.slice();
|
|
179
|
+
this.subkey(bytesToUint64sLE(block), t1, rk.subarray(this.glOffset));
|
|
180
|
+
this.IMC(t1);
|
|
181
|
+
for (let i = 0; i < this.numRounds; i++) {
|
|
182
|
+
const roundKey = rk.subarray(this.glOffset - this.N - i * this.N);
|
|
183
|
+
if (i % 2 === 0)
|
|
184
|
+
this.IG(t1, t2, roundKey);
|
|
185
|
+
else
|
|
186
|
+
this.IG(t2, t1, roundKey);
|
|
187
|
+
}
|
|
188
|
+
this.IGL(t2, t1, rk);
|
|
189
|
+
return uint64sToBytesLE(t1);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/** Kalyna 128/128 bit version */
|
|
193
|
+
export class Kalyna128 extends Kalyna {
|
|
194
|
+
constructor(key) { super(key, 2); }
|
|
195
|
+
}
|
|
196
|
+
/** Kalyna 128/256 bit version */
|
|
197
|
+
export class Kalyna128_256 extends Kalyna {
|
|
198
|
+
constructor(key) { super(key, 2, true); }
|
|
199
|
+
}
|
|
200
|
+
/** Kalyna 256 bit version */
|
|
201
|
+
export class Kalyna256 extends Kalyna {
|
|
202
|
+
constructor(key) { super(key, 4); }
|
|
203
|
+
}
|
|
204
|
+
/** Kalyna 256/512 bit version */
|
|
205
|
+
export class Kalyna256_512 extends Kalyna {
|
|
206
|
+
constructor(key) { super(key, 4, true); }
|
|
207
|
+
}
|
|
208
|
+
/** Kalyna 512 bit version */
|
|
209
|
+
export class Kalyna512 extends Kalyna {
|
|
210
|
+
constructor(key) { super(key, 8); }
|
|
211
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
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;
|
|
6
|
+
private readonly rounds;
|
|
7
|
+
private readonly stSize;
|
|
8
|
+
private readonly threshold;
|
|
9
|
+
protected s: BigUint64Array;
|
|
10
|
+
protected x: Uint8Array;
|
|
11
|
+
protected nx: number;
|
|
12
|
+
protected len: bigint;
|
|
13
|
+
constructor(blockLen: number);
|
|
14
|
+
destroy(): void;
|
|
15
|
+
update(data: TArg<Uint8Array>): this;
|
|
16
|
+
digest(): TRet<Uint8Array>;
|
|
17
|
+
digestInto(buffer: TArg<Uint8Array>): void;
|
|
18
|
+
private column;
|
|
19
|
+
private G;
|
|
20
|
+
private P;
|
|
21
|
+
private Q;
|
|
22
|
+
private outputTransform;
|
|
23
|
+
private transform;
|
|
24
|
+
abstract clone(): Kupyna<T>;
|
|
25
|
+
abstract _cloneInto(): Kupyna<T>;
|
|
26
|
+
}
|
|
27
|
+
declare abstract class KupynaDerived<T extends Kupyna<T>> implements Hash<KupynaDerived<T>> {
|
|
28
|
+
hash: () => Kupyna<T>;
|
|
29
|
+
private readonly slice;
|
|
30
|
+
readonly outputLen: number;
|
|
31
|
+
readonly blockLen: number;
|
|
32
|
+
readonly canXOF = false;
|
|
33
|
+
buffer: Uint8Array;
|
|
34
|
+
constructor(hash: () => Kupyna<T>, slice: number);
|
|
35
|
+
destroy(): void;
|
|
36
|
+
abstract clone(): KupynaDerived<T>;
|
|
37
|
+
abstract _cloneInto(): KupynaDerived<T>;
|
|
38
|
+
update(data: TArg<Uint8Array>): this;
|
|
39
|
+
digest(): TRet<Uint8Array>;
|
|
40
|
+
digestInto(buffer: TArg<Uint8Array>): void;
|
|
41
|
+
}
|
|
42
|
+
/** Kupyna 256 bit version */
|
|
43
|
+
export declare class Kupyna256 extends Kupyna<Kupyna256> {
|
|
44
|
+
/** Kupyna 256 bit version */
|
|
45
|
+
constructor();
|
|
46
|
+
_cloneInto(to?: Kupyna256): Kupyna256;
|
|
47
|
+
clone(): Kupyna256;
|
|
48
|
+
/** Create hash instance */
|
|
49
|
+
static create(): Kupyna256;
|
|
50
|
+
}
|
|
51
|
+
/** Kupyna 512 bit version */
|
|
52
|
+
export declare class Kupyna512 extends Kupyna<Kupyna512> {
|
|
53
|
+
/** Kupyna 512 bit version */
|
|
54
|
+
constructor();
|
|
55
|
+
_cloneInto(to?: Kupyna512): Kupyna512;
|
|
56
|
+
clone(): Kupyna512;
|
|
57
|
+
/** Create hash instance */
|
|
58
|
+
static create(): Kupyna512;
|
|
59
|
+
}
|
|
60
|
+
/** Kupyna 48 bit version */
|
|
61
|
+
export declare class Kupyna48 extends KupynaDerived<Kupyna256> {
|
|
62
|
+
/** Kupyna 48 bit version */
|
|
63
|
+
constructor();
|
|
64
|
+
/** Create hash instance */
|
|
65
|
+
static create(): Kupyna48;
|
|
66
|
+
_cloneInto(to?: Kupyna48): Kupyna48;
|
|
67
|
+
clone(): Kupyna48;
|
|
68
|
+
}
|
|
69
|
+
/** Kupyna 304 bit version */
|
|
70
|
+
export declare class Kupyna304 extends KupynaDerived<Kupyna512> {
|
|
71
|
+
/** Kupyna 304 bit version */
|
|
72
|
+
constructor();
|
|
73
|
+
/** Create hash instance */
|
|
74
|
+
static create(): Kupyna304;
|
|
75
|
+
_cloneInto(to?: Kupyna304): Kupyna304;
|
|
76
|
+
clone(): Kupyna304;
|
|
77
|
+
}
|
|
78
|
+
/** Kupyna 384 bit version */
|
|
79
|
+
export declare class Kupyna384 extends KupynaDerived<Kupyna512> {
|
|
80
|
+
/** Kupyna 384 bit version */
|
|
81
|
+
constructor();
|
|
82
|
+
/** Create hash instance */
|
|
83
|
+
static create(): Kupyna384;
|
|
84
|
+
_cloneInto(to?: Kupyna384): Kupyna384;
|
|
85
|
+
clone(): Kupyna384;
|
|
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";
|
package/kupyna/index.js
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { concatBytes, createHasher } from "@noble/hashes/utils.js";
|
|
2
|
+
import { bytesToUint64sLE, numberToBytesLE, uint64sToBytesLE, byte } from "../utils.js";
|
|
3
|
+
import { T } from "../const.js";
|
|
4
|
+
const r = 0x00f0f0f0f0f0f0f3n;
|
|
5
|
+
class Kupyna {
|
|
6
|
+
blockLen;
|
|
7
|
+
outputLen;
|
|
8
|
+
canXOF = false;
|
|
9
|
+
rounds;
|
|
10
|
+
stSize;
|
|
11
|
+
threshold;
|
|
12
|
+
s;
|
|
13
|
+
x;
|
|
14
|
+
nx;
|
|
15
|
+
len;
|
|
16
|
+
constructor(blockLen) {
|
|
17
|
+
this.blockLen = blockLen;
|
|
18
|
+
this.stSize = (blockLen / 2) / 4;
|
|
19
|
+
this.threshold = blockLen - 12;
|
|
20
|
+
this.rounds = 4 * Math.log2(blockLen) - 14;
|
|
21
|
+
this.outputLen = blockLen / 2;
|
|
22
|
+
this.destroy();
|
|
23
|
+
}
|
|
24
|
+
destroy() {
|
|
25
|
+
this.s = new BigUint64Array(this.stSize);
|
|
26
|
+
this.x = new Uint8Array(this.blockLen);
|
|
27
|
+
this.nx = 0;
|
|
28
|
+
this.len = 0n;
|
|
29
|
+
this.s[0] = BigInt(this.blockLen);
|
|
30
|
+
}
|
|
31
|
+
update(data) {
|
|
32
|
+
this.len += BigInt(data.length);
|
|
33
|
+
if (this.nx > 0) {
|
|
34
|
+
const n = Math.min(this.blockLen - this.nx, data.length);
|
|
35
|
+
this.x.set(data.subarray(0, n), this.nx);
|
|
36
|
+
this.nx += n;
|
|
37
|
+
if (this.nx === this.blockLen) {
|
|
38
|
+
this.transform(bytesToUint64sLE(this.x));
|
|
39
|
+
this.nx = 0;
|
|
40
|
+
}
|
|
41
|
+
data = data.slice(n);
|
|
42
|
+
}
|
|
43
|
+
while (data.length >= this.blockLen) {
|
|
44
|
+
this.transform(bytesToUint64sLE(data.subarray(0, this.blockLen)));
|
|
45
|
+
this.nx = 0;
|
|
46
|
+
data = data.slice(this.blockLen);
|
|
47
|
+
}
|
|
48
|
+
if (data.length > 0) {
|
|
49
|
+
this.x.set(data, 0);
|
|
50
|
+
this.nx = data.length;
|
|
51
|
+
}
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
digest() {
|
|
55
|
+
const buffer = new Uint8Array(this.outputLen);
|
|
56
|
+
this.digestInto(buffer);
|
|
57
|
+
return buffer;
|
|
58
|
+
}
|
|
59
|
+
digestInto(buffer) {
|
|
60
|
+
this.x[this.nx] = 0x80;
|
|
61
|
+
this.nx++;
|
|
62
|
+
const fillBytes = (start) => {
|
|
63
|
+
const available = this.x.length - start;
|
|
64
|
+
if (available > 0)
|
|
65
|
+
this.x.fill(0, start, start + available);
|
|
66
|
+
};
|
|
67
|
+
if (this.nx > this.threshold) {
|
|
68
|
+
fillBytes(this.nx);
|
|
69
|
+
this.transform(bytesToUint64sLE(this.x));
|
|
70
|
+
this.nx = 0;
|
|
71
|
+
}
|
|
72
|
+
fillBytes(this.nx);
|
|
73
|
+
this.x.set(numberToBytesLE(this.len * 8n, 12), this.threshold);
|
|
74
|
+
this.transform(bytesToUint64sLE(this.x));
|
|
75
|
+
this.outputTransform();
|
|
76
|
+
buffer.set(uint64sToBytesLE(this.s).subarray(this.outputLen));
|
|
77
|
+
this.destroy();
|
|
78
|
+
}
|
|
79
|
+
column(x, i) {
|
|
80
|
+
const lastOffset = this.blockLen == 64 ? 7 : 11;
|
|
81
|
+
return T[0][byte(x[(i + this.stSize) % this.stSize])] ^
|
|
82
|
+
T[1][byte(x[(i - 1 + this.stSize) % this.stSize] >> 8n)] ^
|
|
83
|
+
T[2][byte(x[(i - 2 + this.stSize) % this.stSize] >> 16n)] ^
|
|
84
|
+
T[3][byte(x[(i - 3 + this.stSize) % this.stSize] >> 24n)] ^
|
|
85
|
+
T[4][byte(x[(i - 4 + this.stSize) % this.stSize] >> 32n)] ^
|
|
86
|
+
T[5][byte(x[(i - 5 + this.stSize) % this.stSize] >> 40n)] ^
|
|
87
|
+
T[6][byte(x[(i - 6 + this.stSize) % this.stSize] >> 48n)] ^
|
|
88
|
+
T[7][byte(x[(i - lastOffset + this.stSize) % this.stSize] >> 56n)];
|
|
89
|
+
}
|
|
90
|
+
G(x, y) {
|
|
91
|
+
for (let i = 0; i < this.stSize; i++)
|
|
92
|
+
y[i] = this.column(x, i);
|
|
93
|
+
}
|
|
94
|
+
P(x, y, round) {
|
|
95
|
+
for (let idx = 0n; idx < BigInt(this.stSize); idx++)
|
|
96
|
+
x[Number(idx)] ^= (idx << 4n) ^ round;
|
|
97
|
+
const r1 = round + 1n;
|
|
98
|
+
for (let i = 0; i < this.stSize; i++)
|
|
99
|
+
y[i] = this.column(x, i) ^ BigInt(i << 4) ^ r1;
|
|
100
|
+
this.G(y, x);
|
|
101
|
+
}
|
|
102
|
+
Q(x, y, round) {
|
|
103
|
+
for (let j = 0n; j < BigInt(this.stSize); j++)
|
|
104
|
+
x[Number(j)] += r ^ ((((BigInt(this.stSize - 1) - j) * 0x10n) ^ round) << 56n);
|
|
105
|
+
const r1 = round + 1n;
|
|
106
|
+
for (let i = 0; i < this.stSize; i++)
|
|
107
|
+
y[i] = this.column(x, i) + (r ^ ((BigInt((this.stSize - 1 - i) * 16) ^ r1) << 56n));
|
|
108
|
+
this.G(y, x);
|
|
109
|
+
}
|
|
110
|
+
outputTransform() {
|
|
111
|
+
const t1 = new BigUint64Array(this.s), t2 = new BigUint64Array(this.stSize);
|
|
112
|
+
for (let r = 0n; r < BigInt(this.rounds); r += 2n)
|
|
113
|
+
this.P(t1, t2, r);
|
|
114
|
+
for (let column = 0; column < this.stSize; column++)
|
|
115
|
+
this.s[column] ^= t1[column];
|
|
116
|
+
}
|
|
117
|
+
transform(b) {
|
|
118
|
+
const AQ1 = new BigUint64Array(this.stSize);
|
|
119
|
+
const AP1 = new BigUint64Array(this.stSize);
|
|
120
|
+
const tmp = new BigUint64Array(this.stSize);
|
|
121
|
+
for (let column = 0; column < this.stSize; column++) {
|
|
122
|
+
AP1[column] = this.s[column] ^ b[column];
|
|
123
|
+
AQ1[column] = b[column];
|
|
124
|
+
}
|
|
125
|
+
for (let r = 0n; r < BigInt(this.rounds); r += 2n) {
|
|
126
|
+
this.P(AP1, tmp, r);
|
|
127
|
+
this.Q(AQ1, tmp, r);
|
|
128
|
+
}
|
|
129
|
+
for (let column = 0; column < this.stSize; column++)
|
|
130
|
+
this.s[column] ^= AP1[column] ^ AQ1[column];
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
class KupynaDerived {
|
|
134
|
+
hash;
|
|
135
|
+
slice;
|
|
136
|
+
outputLen;
|
|
137
|
+
blockLen;
|
|
138
|
+
canXOF = false;
|
|
139
|
+
buffer = new Uint8Array();
|
|
140
|
+
constructor(hash, slice) {
|
|
141
|
+
this.hash = hash;
|
|
142
|
+
this.slice = slice;
|
|
143
|
+
this.outputLen = Math.abs(slice);
|
|
144
|
+
this.blockLen = hash().blockLen;
|
|
145
|
+
}
|
|
146
|
+
destroy() { }
|
|
147
|
+
update(data) {
|
|
148
|
+
this.buffer = concatBytes(this.buffer, data);
|
|
149
|
+
return this;
|
|
150
|
+
}
|
|
151
|
+
digest() {
|
|
152
|
+
const buffer = new Uint8Array(this.outputLen);
|
|
153
|
+
this.digestInto(buffer);
|
|
154
|
+
return buffer;
|
|
155
|
+
}
|
|
156
|
+
digestInto(buffer) {
|
|
157
|
+
buffer.set(this.hash().update(this.buffer).digest().subarray(this.slice));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/** Kupyna 256 bit version */
|
|
161
|
+
export class Kupyna256 extends Kupyna {
|
|
162
|
+
/** Kupyna 256 bit version */
|
|
163
|
+
constructor() { super(64); }
|
|
164
|
+
_cloneInto(to) {
|
|
165
|
+
to ||= new Kupyna256();
|
|
166
|
+
to.s = new BigUint64Array(this.s);
|
|
167
|
+
to.x = new Uint8Array(this.x);
|
|
168
|
+
to.nx = this.nx;
|
|
169
|
+
to.len = this.len;
|
|
170
|
+
return to;
|
|
171
|
+
}
|
|
172
|
+
clone() { return this._cloneInto(); }
|
|
173
|
+
/** Create hash instance */
|
|
174
|
+
static create() { return new Kupyna256(); }
|
|
175
|
+
}
|
|
176
|
+
/** Kupyna 512 bit version */
|
|
177
|
+
export class Kupyna512 extends Kupyna {
|
|
178
|
+
/** Kupyna 512 bit version */
|
|
179
|
+
constructor() { super(128); }
|
|
180
|
+
_cloneInto(to) {
|
|
181
|
+
to ||= new Kupyna512();
|
|
182
|
+
to.s = new BigUint64Array(this.s);
|
|
183
|
+
to.x = new Uint8Array(this.x);
|
|
184
|
+
to.nx = this.nx;
|
|
185
|
+
to.len = this.len;
|
|
186
|
+
return to;
|
|
187
|
+
}
|
|
188
|
+
clone() { return this._cloneInto(); }
|
|
189
|
+
/** Create hash instance */
|
|
190
|
+
static create() { return new Kupyna512(); }
|
|
191
|
+
}
|
|
192
|
+
/** Kupyna 48 bit version */
|
|
193
|
+
export class Kupyna48 extends KupynaDerived {
|
|
194
|
+
/** Kupyna 48 bit version */
|
|
195
|
+
constructor() { super(Kupyna256.create, -6); }
|
|
196
|
+
/** Create hash instance */
|
|
197
|
+
static create() { return new Kupyna48(); }
|
|
198
|
+
_cloneInto(to) {
|
|
199
|
+
to ||= new Kupyna48();
|
|
200
|
+
to.buffer = new Uint8Array(this.buffer);
|
|
201
|
+
return to;
|
|
202
|
+
}
|
|
203
|
+
clone() { return this._cloneInto(); }
|
|
204
|
+
}
|
|
205
|
+
/** Kupyna 304 bit version */
|
|
206
|
+
export class Kupyna304 extends KupynaDerived {
|
|
207
|
+
/** Kupyna 304 bit version */
|
|
208
|
+
constructor() { super(Kupyna512.create, -38); }
|
|
209
|
+
/** Create hash instance */
|
|
210
|
+
static create() { return new Kupyna304(); }
|
|
211
|
+
_cloneInto(to) {
|
|
212
|
+
to ||= new Kupyna304();
|
|
213
|
+
to.buffer = new Uint8Array(this.buffer);
|
|
214
|
+
return to;
|
|
215
|
+
}
|
|
216
|
+
clone() { return this._cloneInto(); }
|
|
217
|
+
}
|
|
218
|
+
/** Kupyna 384 bit version */
|
|
219
|
+
export class Kupyna384 extends KupynaDerived {
|
|
220
|
+
/** Kupyna 384 bit version */
|
|
221
|
+
constructor() { super(Kupyna512.create, -48); }
|
|
222
|
+
/** Create hash instance */
|
|
223
|
+
static create() { return new Kupyna384(); }
|
|
224
|
+
_cloneInto(to) {
|
|
225
|
+
to ||= new Kupyna384();
|
|
226
|
+
to.buffer = new Uint8Array(this.buffer);
|
|
227
|
+
return to;
|
|
228
|
+
}
|
|
229
|
+
clone() { return this._cloneInto(); }
|
|
230
|
+
}
|
|
231
|
+
/** Kupyna 48 bit version */
|
|
232
|
+
export const kupyna48 = createHasher(Kupyna48.create);
|
|
233
|
+
/** Kupyna 256 bit version */
|
|
234
|
+
export const kupyna256 = createHasher(Kupyna256.create);
|
|
235
|
+
/** Kupyna 304 bit version */
|
|
236
|
+
export const kupyna304 = createHasher(Kupyna304.create);
|
|
237
|
+
/** Kupyna 384 bit version */
|
|
238
|
+
export const kupyna384 = createHasher(Kupyna384.create);
|
|
239
|
+
/** Kupyna 512 bit version */
|
|
240
|
+
export const kupyna512 = createHasher(Kupyna512.create);
|
|
241
|
+
export * from "./kmac.js";
|
package/kupyna/kmac.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Hash, TArg, TRet } from "@noble/hashes/utils.js";
|
|
2
|
+
import { Kupyna256, Kupyna384, Kupyna512 } from "./index.js";
|
|
3
|
+
declare abstract class KupynaKMAC<T, H extends Hash<H>> implements Hash<KupynaKMAC<T, H>> {
|
|
4
|
+
readonly outputLen: number;
|
|
5
|
+
readonly blockLen: number;
|
|
6
|
+
readonly canXOF = false;
|
|
7
|
+
protected readonly threshold: bigint;
|
|
8
|
+
protected h: H;
|
|
9
|
+
protected ik: Uint8Array;
|
|
10
|
+
protected len: bigint;
|
|
11
|
+
constructor(hash: () => H, kpad: TArg<Uint8Array>, key: TArg<Uint8Array>);
|
|
12
|
+
abstract clone(): KupynaKMAC<T, H>;
|
|
13
|
+
abstract _cloneInto(to?: KupynaKMAC<T, H>): KupynaKMAC<T, H>;
|
|
14
|
+
update(data: TArg<Uint8Array>): this;
|
|
15
|
+
digest(): TRet<Uint8Array>;
|
|
16
|
+
digestInto(buffer: TArg<Uint8Array>): void;
|
|
17
|
+
destroy(): void;
|
|
18
|
+
}
|
|
19
|
+
/** Kupyna KMAC (256 version) */
|
|
20
|
+
export declare class KupynaKMAC256 extends KupynaKMAC<KupynaKMAC256, Kupyna256> {
|
|
21
|
+
private key;
|
|
22
|
+
constructor(key: TArg<Uint8Array>);
|
|
23
|
+
_cloneInto(to?: KupynaKMAC256): KupynaKMAC256;
|
|
24
|
+
clone(): KupynaKMAC256;
|
|
25
|
+
}
|
|
26
|
+
/** Kupyna KMAC (384 bit version) */
|
|
27
|
+
export declare class KupynaKMAC384 extends KupynaKMAC<KupynaKMAC512, Kupyna384> {
|
|
28
|
+
private key;
|
|
29
|
+
constructor(key: TArg<Uint8Array>);
|
|
30
|
+
_cloneInto(to?: KupynaKMAC384): KupynaKMAC384;
|
|
31
|
+
clone(): KupynaKMAC384;
|
|
32
|
+
}
|
|
33
|
+
/** Kupyna KMAC (512 bit version) */
|
|
34
|
+
export declare class KupynaKMAC512 extends KupynaKMAC<KupynaKMAC512, Kupyna512> {
|
|
35
|
+
private key;
|
|
36
|
+
constructor(key: TArg<Uint8Array>);
|
|
37
|
+
_cloneInto(to?: KupynaKMAC512): KupynaKMAC512;
|
|
38
|
+
clone(): KupynaKMAC512;
|
|
39
|
+
}
|
|
40
|
+
/** Kupyna KMAC (256 version) */
|
|
41
|
+
export declare const kmac256: (key: TArg<Uint8Array>, msg: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
42
|
+
/** Kupyna KMAC (384 version) */
|
|
43
|
+
export declare const kmac384: (key: TArg<Uint8Array>, msg: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
44
|
+
/** Kupyna KMAC (512 version) */
|
|
45
|
+
export declare const kmac512: (key: TArg<Uint8Array>, msg: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
46
|
+
export {};
|