@li0ard/dstu 0.1.6 → 0.1.8
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 +5 -0
- package/dstu4145/const.d.ts +2 -0
- package/dstu4145/const.js +42 -15
- package/dstu4145/ec/expand.d.ts +4 -3
- package/dstu4145/ec/expand.js +1 -2
- package/dstu4145/ec/index.js +1 -2
- package/dstu4145/{ec/math.d.ts → gf2m/index.d.ts} +1 -0
- package/dstu4145/gf2m/index.js +87 -0
- package/dstu4145/{ec → gf2m}/onb.js +1 -1
- package/dstu4145/gf2m/utils.d.ts +10 -0
- package/dstu4145/gf2m/utils.js +258 -0
- package/dstu4145/index.d.ts +66 -11
- package/dstu4145/index.js +53 -6
- package/dstu9041/curve.js +1 -2
- package/dstu9041/index.d.ts +2 -2
- package/dstu9041/index.js +49 -46
- package/dstu9311/const.d.ts +29 -0
- package/dstu9311/const.js +150 -0
- package/dstu9311/hash.d.ts +41 -0
- package/dstu9311/hash.js +98 -0
- package/dstu9311/index.d.ts +16 -0
- package/dstu9311/index.js +59 -0
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/kalyna/index.d.ts +2 -1
- package/kalyna/index.js +23 -28
- package/keywrap.d.ts +6 -0
- package/keywrap.js +39 -0
- package/modes/cbc.d.ts +2 -3
- package/modes/cbc.js +4 -3
- package/modes/ccm.d.ts +4 -0
- package/modes/ccm.js +47 -0
- package/modes/cfb.d.ts +3 -4
- package/modes/cfb.js +88 -56
- package/modes/cmac.d.ts +2 -0
- package/modes/cmac.js +45 -0
- package/modes/ctr.d.ts +2 -3
- package/modes/ctr.js +25 -14
- package/modes/ecb.d.ts +2 -3
- package/modes/gcm.d.ts +3 -4
- package/modes/gcm.js +19 -16
- package/modes/gmac.d.ts +3 -0
- package/modes/gmac.js +39 -0
- package/modes/index.d.ts +3 -1
- package/modes/index.js +3 -1
- package/modes/kw.d.ts +2 -2
- package/modes/kw.js +2 -0
- package/modes/ofb.d.ts +2 -3
- package/modes/ofb.js +3 -2
- package/modes/xts.d.ts +2 -2
- package/modes/xts.js +32 -21
- package/package.json +4 -4
- package/types.d.ts +5 -3
- package/utils.d.ts +6 -0
- package/utils.js +15 -3
- package/dstu4145/ec/math.js +0 -204
- package/modes/mac.d.ts +0 -6
- package/modes/mac.js +0 -61
- /package/dstu4145/{ec → gf2m}/onb.d.ts +0 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
|
|
2
|
+
import { _HMAC } from "@noble/hashes/hmac.js";
|
|
3
|
+
/** GOST 34.311-95 hash function */
|
|
4
|
+
export declare class Gost3431195 implements Hash<Gost3431195> {
|
|
5
|
+
private sbox;
|
|
6
|
+
readonly blockLen = 32;
|
|
7
|
+
readonly outputLen = 32;
|
|
8
|
+
readonly canXOF = false;
|
|
9
|
+
private buffer;
|
|
10
|
+
/** GOST 34.311-95 hash function */
|
|
11
|
+
constructor(sbox?: TArg<Uint8Array>);
|
|
12
|
+
/** Create hash instance */
|
|
13
|
+
static create(): Gost3431195;
|
|
14
|
+
destroy(): void;
|
|
15
|
+
clone(): Gost3431195;
|
|
16
|
+
_cloneInto(to?: Gost3431195): Gost3431195;
|
|
17
|
+
update(data: TArg<Uint8Array>): this;
|
|
18
|
+
digestInto(buf: TArg<Uint8Array>): void;
|
|
19
|
+
digest(): TRet<Uint8Array>;
|
|
20
|
+
}
|
|
21
|
+
/** GOST 34.311-95 hash function */
|
|
22
|
+
export declare const gost3431195: {
|
|
23
|
+
outputLen: number;
|
|
24
|
+
blockLen: number;
|
|
25
|
+
canXOF: boolean;
|
|
26
|
+
} & import("@noble/hashes/utils.js").HashInfo & {
|
|
27
|
+
(msg: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
28
|
+
create(): Gost3431195;
|
|
29
|
+
} & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
|
|
30
|
+
outputLen: number;
|
|
31
|
+
blockLen: number;
|
|
32
|
+
canXOF: boolean;
|
|
33
|
+
oid?: TRet<Uint8Array> | undefined;
|
|
34
|
+
create: () => Gost3431195;
|
|
35
|
+
};
|
|
36
|
+
/** GOST 34.311-95 HMAC */
|
|
37
|
+
export declare class Gost3431195HMAC extends _HMAC<Gost3431195> {
|
|
38
|
+
constructor(key: TArg<Uint8Array>);
|
|
39
|
+
}
|
|
40
|
+
/** GOST 34.311-95 HMAC */
|
|
41
|
+
export declare const gost3431195Hmac: (key: TArg<Uint8Array>, msg: TArg<Uint8Array>) => TRet<Uint8Array>;
|
package/dstu9311/hash.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { concatBytes, copyBytes, createHasher } from "@noble/hashes/utils.js";
|
|
2
|
+
import { Dstu9311, DKE_1 } from "../dstu9311/index.js";
|
|
3
|
+
import { bytesToNumberBE, numberToBytesBE } from "@noble/curves/utils.js";
|
|
4
|
+
import { xorBytes } from "../utils.js";
|
|
5
|
+
import { _HMAC } from "@noble/hashes/hmac.js";
|
|
6
|
+
const r = (1n << 256n) - 1n;
|
|
7
|
+
const C3 = new Uint8Array([
|
|
8
|
+
0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0xff,
|
|
9
|
+
0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0xff, 0x00,
|
|
10
|
+
0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
|
|
11
|
+
0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00
|
|
12
|
+
]);
|
|
13
|
+
const A = (x) => concatBytes(xorBytes(x.subarray(24, 32), x.subarray(16, 24)), x.subarray(0, 24));
|
|
14
|
+
const P = (x) => new Uint8Array([
|
|
15
|
+
x[31], x[23], x[15], x[7], x[30], x[22], x[14], x[6],
|
|
16
|
+
x[29], x[21], x[13], x[5], x[28], x[20], x[12], x[4],
|
|
17
|
+
x[27], x[19], x[11], x[3], x[26], x[18], x[10], x[2],
|
|
18
|
+
x[25], x[17], x[9], x[1], x[24], x[16], x[8], x[0]
|
|
19
|
+
]);
|
|
20
|
+
const chi = (Y) => new Uint8Array([
|
|
21
|
+
Y[30] ^ Y[28] ^ Y[26] ^ Y[24] ^ Y[6] ^ Y[0],
|
|
22
|
+
Y[31] ^ Y[29] ^ Y[27] ^ Y[25] ^ Y[7] ^ Y[1],
|
|
23
|
+
...Y.subarray(0, 30)
|
|
24
|
+
]);
|
|
25
|
+
const _getCipher = (u, v, sbox) => new Dstu9311(P(xorBytes(u, v)), sbox);
|
|
26
|
+
const _step = (hin, m, sbox) => {
|
|
27
|
+
const k1 = _getCipher(hin, m, sbox);
|
|
28
|
+
let u = A(hin), v = A(A(m));
|
|
29
|
+
const k2 = _getCipher(u, v, sbox);
|
|
30
|
+
u = xorBytes(A(u), C3), v = A(A(v));
|
|
31
|
+
const k3 = _getCipher(u, v, sbox);
|
|
32
|
+
u = A(u), v = A(A(v));
|
|
33
|
+
const k4 = _getCipher(u, v, sbox);
|
|
34
|
+
const x = concatBytes(k4.encrypt(hin.slice(0, 8).reverse()).reverse(), k3.encrypt(hin.slice(8, 16).reverse()).reverse(), k2.encrypt(hin.slice(16, 24).reverse()).reverse(), k1.encrypt(hin.slice(24, 32).reverse()).reverse());
|
|
35
|
+
for (let i = 0; i < 12; i++)
|
|
36
|
+
x.set(chi(x));
|
|
37
|
+
x.set(xorBytes(hin, chi(xorBytes(x, m))));
|
|
38
|
+
for (let i = 0; i < 61; i++)
|
|
39
|
+
x.set(chi(x));
|
|
40
|
+
return x;
|
|
41
|
+
};
|
|
42
|
+
/** GOST 34.311-95 hash function */
|
|
43
|
+
export class Gost3431195 {
|
|
44
|
+
sbox;
|
|
45
|
+
blockLen = 32;
|
|
46
|
+
outputLen = 32;
|
|
47
|
+
canXOF = false;
|
|
48
|
+
buffer;
|
|
49
|
+
/** GOST 34.311-95 hash function */
|
|
50
|
+
constructor(sbox = DKE_1) {
|
|
51
|
+
this.sbox = sbox;
|
|
52
|
+
this.buffer = new Uint8Array();
|
|
53
|
+
}
|
|
54
|
+
/** Create hash instance */
|
|
55
|
+
static create() { return new Gost3431195(); }
|
|
56
|
+
destroy() { this.buffer = new Uint8Array(); }
|
|
57
|
+
clone() { return this._cloneInto(); }
|
|
58
|
+
_cloneInto(to) {
|
|
59
|
+
to ||= new Gost3431195();
|
|
60
|
+
to.buffer = new Uint8Array(this.buffer);
|
|
61
|
+
to.sbox = this.sbox;
|
|
62
|
+
return to;
|
|
63
|
+
}
|
|
64
|
+
update(data) {
|
|
65
|
+
this.buffer = concatBytes(this.buffer, data);
|
|
66
|
+
return this;
|
|
67
|
+
}
|
|
68
|
+
digestInto(buf) {
|
|
69
|
+
if (buf.length != this.outputLen)
|
|
70
|
+
throw new Error("digestInto: Invalid buffer length");
|
|
71
|
+
let len = 0n, checksum = 0n;
|
|
72
|
+
const h = new Uint8Array(this.blockLen), m = copyBytes(this.buffer);
|
|
73
|
+
for (let i = 0; i < m.length; i += this.blockLen) {
|
|
74
|
+
let part = m.slice(i, i + this.blockLen).reverse();
|
|
75
|
+
len += BigInt(part.length) * 8n;
|
|
76
|
+
checksum = (checksum + bytesToNumberBE(part)) & r;
|
|
77
|
+
if (part.length < this.blockLen)
|
|
78
|
+
part = numberToBytesBE(bytesToNumberBE(part), this.blockLen);
|
|
79
|
+
h.set(_step(h, part, this.sbox));
|
|
80
|
+
}
|
|
81
|
+
h.set(_step(_step(h, numberToBytesBE(len, this.blockLen), this.sbox), numberToBytesBE(checksum, this.blockLen), this.sbox));
|
|
82
|
+
buf.set(h.reverse());
|
|
83
|
+
this.destroy();
|
|
84
|
+
}
|
|
85
|
+
digest() {
|
|
86
|
+
const buffer = new Uint8Array(this.outputLen);
|
|
87
|
+
this.digestInto(buffer);
|
|
88
|
+
return buffer;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/** GOST 34.311-95 hash function */
|
|
92
|
+
export const gost3431195 = createHasher(Gost3431195.create);
|
|
93
|
+
/** GOST 34.311-95 HMAC */
|
|
94
|
+
export class Gost3431195HMAC extends _HMAC {
|
|
95
|
+
constructor(key) { super(gost3431195, key); }
|
|
96
|
+
}
|
|
97
|
+
/** GOST 34.311-95 HMAC */
|
|
98
|
+
export const gost3431195Hmac = (key, msg) => new Gost3431195HMAC(key).update(msg).digest();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type TArg, type TRet } from "@noble/hashes/utils.js";
|
|
2
|
+
import type { Cipher } from "../types.js";
|
|
3
|
+
/** DSTU 9311:2024 cipher (Revised DSTU GOST 28147:2009) */
|
|
4
|
+
export declare class Dstu9311 implements Cipher {
|
|
5
|
+
private key;
|
|
6
|
+
private sbox;
|
|
7
|
+
readonly keySize = 32;
|
|
8
|
+
readonly blockSize = 8;
|
|
9
|
+
/** DSTU 9311:2024 cipher (Revised DSTU GOST 28147:2009) */
|
|
10
|
+
constructor(key: TArg<Uint8Array>, sbox?: TArg<Uint8Array>);
|
|
11
|
+
proceedBlock(block: TArg<Uint8Array>, sequence: number[]): TRet<Uint8Array>;
|
|
12
|
+
encrypt(plaintext: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
13
|
+
decrypt(ciphertext: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
14
|
+
}
|
|
15
|
+
export * from "./const.js";
|
|
16
|
+
export * from "./hash.js";
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { concatBytes, createView } from "@noble/hashes/utils.js";
|
|
2
|
+
import { DKE_1, keySequences } from "./const.js";
|
|
3
|
+
import { bytesToNumberLE, numberToBytesLE } from "@noble/curves/utils.js";
|
|
4
|
+
const G = (v, sbox) => {
|
|
5
|
+
const nibble = (r, i) => {
|
|
6
|
+
const b = sbox[r + (i >> 1)];
|
|
7
|
+
return (i & 1) == 0 ? (b >>> 4) : (b & 0x0f);
|
|
8
|
+
};
|
|
9
|
+
const t = (nibble(0, v & 0x0f) << 0) |
|
|
10
|
+
(nibble(8, (v >> 4) & 0x0f) << 4) |
|
|
11
|
+
(nibble(16, (v >> 8) & 0x0f) << 8) |
|
|
12
|
+
(nibble(24, (v >> 12) & 0x0f) << 12) |
|
|
13
|
+
(nibble(32, (v >> 16) & 0x0f) << 16) |
|
|
14
|
+
(nibble(40, (v >> 20) & 0x0f) << 20) |
|
|
15
|
+
(nibble(48, (v >> 24) & 0x0f) << 24) |
|
|
16
|
+
(nibble(56, (v >> 28) & 0x0f) << 28);
|
|
17
|
+
return ((t << 11) | (t >>> 21)) >>> 0;
|
|
18
|
+
};
|
|
19
|
+
const extendKey = (key, sequence) => {
|
|
20
|
+
const view = createView(key);
|
|
21
|
+
const result = new Uint32Array(sequence.length);
|
|
22
|
+
for (let i = 0; i < sequence.length; i++)
|
|
23
|
+
result[i] = view.getUint32(sequence[i] * 4, true);
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
/** DSTU 9311:2024 cipher (Revised DSTU GOST 28147:2009) */
|
|
27
|
+
export class Dstu9311 {
|
|
28
|
+
key;
|
|
29
|
+
sbox;
|
|
30
|
+
keySize = 32;
|
|
31
|
+
blockSize = 8;
|
|
32
|
+
/** DSTU 9311:2024 cipher (Revised DSTU GOST 28147:2009) */
|
|
33
|
+
constructor(key, sbox = DKE_1) {
|
|
34
|
+
this.key = key;
|
|
35
|
+
this.sbox = sbox;
|
|
36
|
+
if (key.length !== this.keySize)
|
|
37
|
+
throw new Error("Invalid key length");
|
|
38
|
+
}
|
|
39
|
+
proceedBlock(block, sequence) {
|
|
40
|
+
if (block.length !== this.blockSize)
|
|
41
|
+
throw new Error("Invalid block size");
|
|
42
|
+
const roundKeys = extendKey(this.key, sequence);
|
|
43
|
+
let a0 = Number(bytesToNumberLE(block.subarray(4, 8))), a1 = Number(bytesToNumberLE(block.subarray(0, 4)));
|
|
44
|
+
for (let i = 0; i < roundKeys.length; i++) {
|
|
45
|
+
const temp = a1;
|
|
46
|
+
a1 = (a0 ^ G((a1 + roundKeys[i]) >>> 0, this.sbox)) >>> 0;
|
|
47
|
+
a0 = temp;
|
|
48
|
+
}
|
|
49
|
+
return concatBytes(numberToBytesLE(a0, 4), numberToBytesLE(a1, 4));
|
|
50
|
+
}
|
|
51
|
+
encrypt(plaintext) {
|
|
52
|
+
return this.proceedBlock(plaintext, keySequences.ENCRYPT);
|
|
53
|
+
}
|
|
54
|
+
decrypt(ciphertext) {
|
|
55
|
+
return this.proceedBlock(ciphertext, keySequences.DECRYPT);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
export * from "./const.js";
|
|
59
|
+
export * from "./hash.js";
|
package/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export * from "./dstu4145/index.js";
|
|
2
2
|
export * from "./dstu9041/index.js";
|
|
3
|
+
export * from "./dstu9311/index.js";
|
|
3
4
|
export { Kalyna128, Kalyna128_256, Kalyna256, Kalyna256_512, Kalyna512 } from "./kalyna/index.js";
|
|
4
5
|
export * from "./kupyna/index.js";
|
|
5
6
|
export * from "./modes/index.js";
|
|
6
7
|
export * from "./strumok/index.js";
|
|
8
|
+
export * from "./keywrap.js";
|
|
7
9
|
export * from "./types.js";
|
package/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
export * from "./dstu4145/index.js";
|
|
2
2
|
export * from "./dstu9041/index.js";
|
|
3
|
+
export * from "./dstu9311/index.js";
|
|
3
4
|
export { Kalyna128, Kalyna128_256, Kalyna256, Kalyna256_512, Kalyna512 } from "./kalyna/index.js";
|
|
4
5
|
export * from "./kupyna/index.js";
|
|
5
6
|
export * from "./modes/index.js";
|
|
6
7
|
export * from "./strumok/index.js";
|
|
8
|
+
export * from "./keywrap.js";
|
|
7
9
|
export * from "./types.js";
|
package/kalyna/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { TArg, TRet } from "@noble/hashes/utils.js";
|
|
2
2
|
import type { Cipher } from "../types.js";
|
|
3
|
-
export declare const column: (x: TArg<BigUint64Array>, i: number, N: number, c1: number, c2: number, c3: number, c4: number, c5: number, c6: number, c7: number) => bigint;
|
|
3
|
+
export declare const column: (x: TArg<BigUint64Array>, i: number, N: number, c1: number, c2: number, c3: number, c4: number, c5: number, c6: number, c7: number, table?: Readonly<TArg<BigUint64Array>[]>, sign?: 1 | -1) => bigint;
|
|
4
4
|
export declare abstract class Kalyna implements Cipher {
|
|
5
5
|
readonly N: number;
|
|
6
6
|
/** Block size */
|
|
@@ -19,6 +19,7 @@ export declare abstract class Kalyna implements Cipher {
|
|
|
19
19
|
private addkey;
|
|
20
20
|
private subkey;
|
|
21
21
|
private column;
|
|
22
|
+
private invColumn;
|
|
22
23
|
private G;
|
|
23
24
|
private GL;
|
|
24
25
|
private IMC;
|
package/kalyna/index.js
CHANGED
|
@@ -8,14 +8,14 @@ const swap_block = (k, N) => {
|
|
|
8
8
|
k[i] = k[i + 1];
|
|
9
9
|
k[N - 1] = t;
|
|
10
10
|
};
|
|
11
|
-
export const column = (x, i, N, c1, c2, c3, c4, c5, c6, c7) => (
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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)]);
|
|
19
19
|
export class Kalyna {
|
|
20
20
|
N;
|
|
21
21
|
/** Block size */
|
|
@@ -110,6 +110,9 @@ export class Kalyna {
|
|
|
110
110
|
column(x, i) {
|
|
111
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]);
|
|
112
112
|
}
|
|
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);
|
|
115
|
+
}
|
|
113
116
|
G(x, y, k) {
|
|
114
117
|
for (let i = 0; i < this.N; i++)
|
|
115
118
|
y[i] = k[i] ^ this.column(x, i);
|
|
@@ -131,27 +134,19 @@ export class Kalyna {
|
|
|
131
134
|
}
|
|
132
135
|
IG(x, y, k) {
|
|
133
136
|
for (let i = 0; i < this.N; i++)
|
|
134
|
-
y[i] = k[i] ^
|
|
135
|
-
IT[0][byte(x[(i + this.wordOffsets[0]) % this.N])] ^
|
|
136
|
-
IT[1][byte(x[(i + this.wordOffsets[1]) % this.N] >> 8n)] ^
|
|
137
|
-
IT[2][byte(x[(i + this.wordOffsets[2]) % this.N] >> 16n)] ^
|
|
138
|
-
IT[3][byte(x[(i + this.wordOffsets[3]) % this.N] >> 24n)] ^
|
|
139
|
-
IT[4][byte(x[(i + this.wordOffsets[4]) % this.N] >> 32n)] ^
|
|
140
|
-
IT[5][byte(x[(i + this.wordOffsets[5]) % this.N] >> 40n)] ^
|
|
141
|
-
IT[6][byte(x[(i + this.wordOffsets[6]) % this.N] >> 48n)] ^
|
|
142
|
-
IT[7][byte(x[(i + this.wordOffsets[7]) % this.N] >> 56n)];
|
|
137
|
+
y[i] = k[i] ^ this.invColumn(x, i);
|
|
143
138
|
}
|
|
144
|
-
IGL(x, y
|
|
139
|
+
IGL(x, y) {
|
|
145
140
|
for (let i = 0; i < this.N; i++) {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
y[i] = result -
|
|
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];
|
|
155
150
|
}
|
|
156
151
|
}
|
|
157
152
|
/** Encrypt block */
|
|
@@ -184,7 +179,7 @@ export class Kalyna {
|
|
|
184
179
|
else
|
|
185
180
|
this.IG(t2, t1, roundKey);
|
|
186
181
|
}
|
|
187
|
-
this.IGL(t2, t1
|
|
182
|
+
this.IGL(t2, t1);
|
|
188
183
|
return uint64sToBytesLE(t1);
|
|
189
184
|
}
|
|
190
185
|
}
|
package/keywrap.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type TArg, type TRet } from "@noble/curves/utils.js";
|
|
2
|
+
/** Key wrap (`Dstu7624Wrap`/`GOST28147Wrap`) */
|
|
3
|
+
export declare const keyWrap: (kek: TArg<Uint8Array>, useDstu9311?: boolean) => {
|
|
4
|
+
wrap: (key: TArg<Uint8Array>, iv?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
5
|
+
unwrap: (wrappedKey: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
6
|
+
};
|
package/keywrap.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { equalBytes, concatBytes, randomBytes } from "@noble/curves/utils.js";
|
|
2
|
+
import { Kalyna256 } from "./kalyna/index.js";
|
|
3
|
+
import { cfb, cmac } from "./modes/index.js";
|
|
4
|
+
import { Dstu9311 } from "./dstu9311/index.js";
|
|
5
|
+
const IV1_KALYNA = new Uint8Array([
|
|
6
|
+
0x69, 0x73, 0x27, 0x1D, 0x6E, 0x61, 0x1D, 0x06, 0x61, 0x67, 0x15, 0x04, 0x6C, 0x65, 0x50, 0x4C,
|
|
7
|
+
0x20, 0x20, 0x00, 0x4F, 0x6D, 0x68, 0x01, 0x1F, 0x65, 0x61, 0x0C, 0x0C, 0x73, 0x73, 0x47, 0x14
|
|
8
|
+
]);
|
|
9
|
+
const IV1_9311 = new Uint8Array([
|
|
10
|
+
0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05
|
|
11
|
+
]);
|
|
12
|
+
/** Key wrap (`Dstu7624Wrap`/`GOST28147Wrap`) */
|
|
13
|
+
export const keyWrap = (kek, useDstu9311 = false) => {
|
|
14
|
+
if (kek.length != 32)
|
|
15
|
+
throw new Error("Invalid key length");
|
|
16
|
+
const cipher = new (useDstu9311 ? Dstu9311 : Kalyna256)(kek);
|
|
17
|
+
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
|
+
return Object.freeze({
|
|
21
|
+
wrap: (key, iv) => {
|
|
22
|
+
iv ??= randomBytes(ivLength);
|
|
23
|
+
if (iv.length != ivLength)
|
|
24
|
+
throw new Error("Invalid IV length");
|
|
25
|
+
const mac = macMode.compute(key);
|
|
26
|
+
return finalMode.encrypt(concatBytes(iv, cfb(cipher, iv, 32).encrypt(concatBytes(key, mac))).reverse());
|
|
27
|
+
},
|
|
28
|
+
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))
|
|
35
|
+
throw new Error("Invalid MAC");
|
|
36
|
+
return key;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
};
|
package/modes/cbc.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { TArg } from "@noble/hashes/utils.js";
|
|
2
|
-
import type {
|
|
3
|
-
import type { BlockMode } from "../types.js";
|
|
2
|
+
import type { BlockMode, Cipher } from "../types.js";
|
|
4
3
|
/** Cipher Block Chaining (CBC) mode */
|
|
5
|
-
export declare const cbc: (cipher:
|
|
4
|
+
export declare const cbc: (cipher: Cipher, iv: TArg<Uint8Array>) => BlockMode;
|
package/modes/cbc.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { xorBytes } from "../utils.js";
|
|
1
|
+
import { assertKalyna, xorBytes } from "../utils.js";
|
|
2
2
|
/** Cipher Block Chaining (CBC) mode */
|
|
3
3
|
export const cbc = (cipher, iv) => {
|
|
4
|
+
assertKalyna(cipher);
|
|
4
5
|
if (iv.length !== cipher.blockSize)
|
|
5
6
|
throw new Error("Invalid IV size");
|
|
6
7
|
return Object.freeze({
|
|
7
8
|
encrypt: (plaintext) => {
|
|
8
9
|
if (plaintext.length % cipher.blockSize !== 0)
|
|
9
10
|
throw new Error("Plaintext not aligned");
|
|
10
|
-
let buf =
|
|
11
|
+
let buf = iv;
|
|
11
12
|
const output = new Uint8Array(plaintext.length);
|
|
12
13
|
for (let i = 0; i < plaintext.length; i += cipher.blockSize) {
|
|
13
14
|
const blk = cipher.encrypt(xorBytes(plaintext.subarray(i, i + cipher.blockSize), buf));
|
|
@@ -19,7 +20,7 @@ export const cbc = (cipher, iv) => {
|
|
|
19
20
|
decrypt: (ciphertext) => {
|
|
20
21
|
if (ciphertext.length % cipher.blockSize !== 0)
|
|
21
22
|
throw new Error("Ciphertext not aligned");
|
|
22
|
-
let buf =
|
|
23
|
+
let buf = iv;
|
|
23
24
|
const output = new Uint8Array(ciphertext.length);
|
|
24
25
|
for (let i = 0; i < ciphertext.length; i += cipher.blockSize) {
|
|
25
26
|
const blk = ciphertext.subarray(i, i + cipher.blockSize);
|
package/modes/ccm.d.ts
ADDED
package/modes/ccm.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { equalBytes, concatBytes } from "@noble/curves/utils.js";
|
|
2
|
+
import { assertKalyna } from "../utils.js";
|
|
3
|
+
import { pad } from "../padding.js";
|
|
4
|
+
import { ctr } from "./ctr.js";
|
|
5
|
+
/** Counter with CBC-MAC (CCM) mode (AEAD) */
|
|
6
|
+
export const ccm = (cipher, iv, q = 16, Nb = 4) => {
|
|
7
|
+
assertKalyna(cipher);
|
|
8
|
+
if (q !== 8 && q !== 16 && q !== 32 && q !== 48 && q !== 64)
|
|
9
|
+
throw new Error('q must be 8, 16, 32, 48, or 64');
|
|
10
|
+
if (cipher.blockSize < Nb + 1)
|
|
11
|
+
throw new Error('Cipher block size must be >= Nb + 1');
|
|
12
|
+
const tmp = cipher.blockSize - 1, tmp2 = tmp - Nb;
|
|
13
|
+
const calculateMac = (msg, aad) => {
|
|
14
|
+
aad ??= new Uint8Array();
|
|
15
|
+
const G1 = new Uint8Array(cipher.blockSize), G2 = new Uint8Array(cipher.blockSize), B = new Uint8Array(cipher.blockSize);
|
|
16
|
+
G1.set(iv.subarray(0, tmp2));
|
|
17
|
+
G1[tmp2] = msg.length & 0xFF;
|
|
18
|
+
G1[tmp] = (msg.length > 0 ? 128 : 0) | (q + 32 - (q % 16)) | (Nb - 1);
|
|
19
|
+
G2[0] = aad.length & 0xFF;
|
|
20
|
+
const updateMac = (data) => {
|
|
21
|
+
for (let i = 0; i < data.length; i += cipher.blockSize) {
|
|
22
|
+
const chunk = data.subarray(i, i + cipher.blockSize);
|
|
23
|
+
for (let j = 0; j < cipher.blockSize; j++)
|
|
24
|
+
B[j] ^= chunk[j];
|
|
25
|
+
B.set(cipher.encrypt(B));
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
updateMac(concatBytes(G1, G2.subarray(0, cipher.blockSize - (aad.length % cipher.blockSize)), aad));
|
|
29
|
+
updateMac(pad(msg, cipher.blockSize));
|
|
30
|
+
return B.slice(0, q);
|
|
31
|
+
};
|
|
32
|
+
const mode = ctr(cipher, iv);
|
|
33
|
+
return Object.freeze({
|
|
34
|
+
seal: (plaintext, aad) => {
|
|
35
|
+
const mac = calculateMac(plaintext, aad);
|
|
36
|
+
return mode.crypt(concatBytes(plaintext, mac));
|
|
37
|
+
},
|
|
38
|
+
open: (ciphertext, aad) => {
|
|
39
|
+
const decrypted = mode.crypt(ciphertext);
|
|
40
|
+
const plaintext = decrypted.slice(0, -q);
|
|
41
|
+
const hC = calculateMac(plaintext, aad);
|
|
42
|
+
if (!equalBytes(decrypted.subarray(-q), hC))
|
|
43
|
+
throw new Error("Invalid MAC");
|
|
44
|
+
return plaintext;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
};
|
package/modes/cfb.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type {
|
|
3
|
-
import type { BlockMode } from "../types.js";
|
|
1
|
+
import { type TArg } from "@noble/hashes/utils.js";
|
|
2
|
+
import type { BlockMode, Cipher } from "../types.js";
|
|
4
3
|
/** Cipher Feedback (CFB) mode */
|
|
5
|
-
export declare const cfb: (cipher:
|
|
4
|
+
export declare const cfb: (cipher: Cipher, iv: TArg<Uint8Array>, q?: number) => BlockMode;
|
package/modes/cfb.js
CHANGED
|
@@ -1,65 +1,97 @@
|
|
|
1
|
+
import { concatBytes, copyBytes } from "@noble/hashes/utils.js";
|
|
2
|
+
import { isKalyna, xorBytes } from "../utils.js";
|
|
1
3
|
/** Cipher Feedback (CFB) mode */
|
|
2
4
|
export const cfb = (cipher, iv, q = cipher.blockSize) => {
|
|
3
|
-
|
|
5
|
+
const _isKalyna = isKalyna(cipher);
|
|
6
|
+
if (_isKalyna && q !== 1 && q !== 8 && q !== 16 && q !== 32 && q !== 64)
|
|
4
7
|
throw new Error('q must be 1, 8, 16, 32, or 64');
|
|
5
|
-
if (q > cipher.blockSize)
|
|
8
|
+
if (_isKalyna && q > cipher.blockSize)
|
|
6
9
|
throw new Error('q cannot exceed block size');
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
offset
|
|
10
|
+
if (!_isKalyna && (iv.length === 0 || iv.length % cipher.blockSize !== 0))
|
|
11
|
+
throw new Error("Invalid IV size");
|
|
12
|
+
if (_isKalyna)
|
|
13
|
+
return Object.freeze({
|
|
14
|
+
encrypt: (plaintext) => {
|
|
15
|
+
const gamma = cipher.encrypt(iv);
|
|
16
|
+
const feed = copyBytes(iv);
|
|
17
|
+
const result = new Uint8Array(plaintext.length);
|
|
18
|
+
let offset = 0, dataOff = 0;
|
|
19
|
+
while (offset > 0 && dataOff < plaintext.length) {
|
|
20
|
+
result[dataOff] = plaintext[dataOff] ^ gamma[offset];
|
|
21
|
+
feed[offset++] = result[dataOff++];
|
|
22
|
+
if (offset >= cipher.blockSize) {
|
|
23
|
+
gamma.set(cipher.encrypt(feed));
|
|
24
|
+
offset = cipher.blockSize - q;
|
|
25
|
+
}
|
|
20
26
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
dataOff += q;
|
|
29
|
-
}
|
|
30
|
-
while (dataOff < plaintext.length) {
|
|
31
|
-
result[dataOff] = plaintext[dataOff] ^ gamma[cipher.blockSize - (plaintext.length - dataOff)];
|
|
32
|
-
dataOff++;
|
|
33
|
-
}
|
|
34
|
-
return result;
|
|
35
|
-
},
|
|
36
|
-
decrypt: (ciphertext) => {
|
|
37
|
-
let gamma = cipher.encrypt(iv);
|
|
38
|
-
const feed = new Uint8Array(iv);
|
|
39
|
-
let offset = 0;
|
|
40
|
-
const result = new Uint8Array(ciphertext.length);
|
|
41
|
-
let dataOff = 0;
|
|
42
|
-
while (offset > 0 && dataOff < ciphertext.length) {
|
|
43
|
-
result[dataOff] = ciphertext[dataOff] ^ gamma[offset];
|
|
44
|
-
feed[offset++] = ciphertext[dataOff++];
|
|
45
|
-
if (offset >= cipher.blockSize) {
|
|
46
|
-
gamma = cipher.encrypt(feed);
|
|
47
|
-
offset = cipher.blockSize - q;
|
|
27
|
+
while (dataOff + q <= plaintext.length) {
|
|
28
|
+
for (let i = 0; i < q; i++)
|
|
29
|
+
result[dataOff + i] = plaintext[dataOff + i] ^ gamma[cipher.blockSize - q + i];
|
|
30
|
+
feed.set(gamma.subarray(0, cipher.blockSize - q));
|
|
31
|
+
feed.set(result.subarray(dataOff, dataOff + q), cipher.blockSize - q);
|
|
32
|
+
gamma.set(cipher.encrypt(feed));
|
|
33
|
+
dataOff += q;
|
|
48
34
|
}
|
|
35
|
+
while (dataOff < plaintext.length) {
|
|
36
|
+
result[dataOff] = plaintext[dataOff] ^ gamma[cipher.blockSize - (plaintext.length - dataOff)];
|
|
37
|
+
dataOff++;
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
},
|
|
41
|
+
decrypt: (ciphertext) => {
|
|
42
|
+
const gamma = cipher.encrypt(iv);
|
|
43
|
+
const feed = copyBytes(iv);
|
|
44
|
+
const result = new Uint8Array(ciphertext.length);
|
|
45
|
+
let offset = 0, dataOff = 0;
|
|
46
|
+
while (offset > 0 && dataOff < ciphertext.length) {
|
|
47
|
+
result[dataOff] = ciphertext[dataOff] ^ gamma[offset];
|
|
48
|
+
feed[offset++] = ciphertext[dataOff++];
|
|
49
|
+
if (offset >= cipher.blockSize) {
|
|
50
|
+
gamma.set(cipher.encrypt(feed));
|
|
51
|
+
offset = cipher.blockSize - q;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
while (dataOff + q <= ciphertext.length) {
|
|
55
|
+
for (let i = 0; i < q; i++)
|
|
56
|
+
result[dataOff + i] = ciphertext[dataOff + i] ^ gamma[cipher.blockSize - q + i];
|
|
57
|
+
feed.set(gamma.subarray(0, cipher.blockSize - q));
|
|
58
|
+
feed.set(ciphertext.subarray(dataOff, dataOff + q), cipher.blockSize - q);
|
|
59
|
+
gamma.set(cipher.encrypt(feed));
|
|
60
|
+
dataOff += q;
|
|
61
|
+
}
|
|
62
|
+
while (dataOff < ciphertext.length) {
|
|
63
|
+
result[dataOff] = ciphertext[dataOff] ^ gamma[cipher.blockSize - (ciphertext.length - dataOff)];
|
|
64
|
+
dataOff++;
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
49
67
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
68
|
+
});
|
|
69
|
+
else
|
|
70
|
+
return Object.freeze({
|
|
71
|
+
encrypt: (plaintext) => {
|
|
72
|
+
let r = [];
|
|
73
|
+
for (let i = 0; i < iv.length; i += cipher.blockSize)
|
|
74
|
+
r.push(iv.subarray(i, i + cipher.blockSize));
|
|
75
|
+
const len = Math.max(8, (plaintext.length + 7) & ~7);
|
|
76
|
+
const result = [];
|
|
77
|
+
for (let i = 0; i < len; i += cipher.blockSize) {
|
|
78
|
+
result.push(xorBytes(cipher.encrypt(r[0]), plaintext.subarray(i, i + cipher.blockSize)));
|
|
79
|
+
r = r.slice(1).concat(result[result.length - 1]);
|
|
80
|
+
}
|
|
81
|
+
return concatBytes(...result);
|
|
82
|
+
},
|
|
83
|
+
decrypt: (ciphertext) => {
|
|
84
|
+
let r = [];
|
|
85
|
+
for (let i = 0; i < iv.length; i += cipher.blockSize)
|
|
86
|
+
r.push(iv.subarray(i, i + cipher.blockSize));
|
|
87
|
+
const len = Math.max(8, (ciphertext.length + 7) & ~7);
|
|
88
|
+
const result = [];
|
|
89
|
+
for (let i = 0; i < len; i += cipher.blockSize) {
|
|
90
|
+
const blk = ciphertext.slice(i, i + cipher.blockSize);
|
|
91
|
+
result.push(xorBytes(cipher.encrypt(r[0]), blk));
|
|
92
|
+
r = r.slice(1).concat(blk);
|
|
93
|
+
}
|
|
94
|
+
return concatBytes(...result);
|
|
61
95
|
}
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
});
|
|
96
|
+
});
|
|
65
97
|
};
|
package/modes/cmac.d.ts
ADDED