@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/dstu9311/const.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** S-Box #1 */
|
|
2
|
-
export const DKE_1 = new Uint8Array([
|
|
2
|
+
export const DKE_1 = /* @__PURE__ */ new Uint8Array([
|
|
3
3
|
0xA9, 0xD6, 0xEB, 0x45, 0xF1, 0x3C, 0x70, 0x82,
|
|
4
4
|
0x80, 0xC4, 0x96, 0x7B, 0x23, 0x1F, 0x5E, 0xAD,
|
|
5
5
|
0xF6, 0x58, 0xEB, 0xA4, 0xC0, 0x37, 0x29, 0x1D,
|
|
@@ -10,7 +10,7 @@ export const DKE_1 = new Uint8Array([
|
|
|
10
10
|
0x12, 0x3E, 0x6D, 0xB8, 0xFA, 0xC5, 0x79, 0x04
|
|
11
11
|
]);
|
|
12
12
|
/** S-Box #2 */
|
|
13
|
-
export const DKE_2 = new Uint8Array([
|
|
13
|
+
export const DKE_2 = /* @__PURE__ */ new Uint8Array([
|
|
14
14
|
0xE9, 0x37, 0xF4, 0xCB, 0x6A, 0xD1, 0x05, 0x82,
|
|
15
15
|
0xAD, 0xC7, 0x6E, 0x81, 0xF3, 0xB4, 0x09, 0x52,
|
|
16
16
|
0x4B, 0x1F, 0x92, 0xEC, 0x6A, 0x87, 0x35, 0x0D,
|
|
@@ -21,7 +21,7 @@ export const DKE_2 = new Uint8Array([
|
|
|
21
21
|
0x43, 0xED, 0x50, 0x2B, 0x1A, 0x76, 0x9F, 0x8C
|
|
22
22
|
]);
|
|
23
23
|
/** S-Box #3 */
|
|
24
|
-
export const DKE_3 = new Uint8Array([
|
|
24
|
+
export const DKE_3 = /* @__PURE__ */ new Uint8Array([
|
|
25
25
|
0xD9, 0x1E, 0x72, 0xC5, 0x4B, 0x6F, 0x38, 0xA0,
|
|
26
26
|
0x78, 0x6B, 0x03, 0x4D, 0x95, 0xFE, 0xAC, 0x21,
|
|
27
27
|
0xA5, 0x3C, 0x98, 0xD6, 0x4F, 0xE0, 0x2B, 0x17,
|
|
@@ -32,7 +32,7 @@ export const DKE_3 = new Uint8Array([
|
|
|
32
32
|
0x6D, 0xCA, 0xB7, 0x93, 0xFE, 0x12, 0x08, 0x45
|
|
33
33
|
]);
|
|
34
34
|
/** S-Box #4 */
|
|
35
|
-
export const DKE_4 = new Uint8Array([
|
|
35
|
+
export const DKE_4 = /* @__PURE__ */ new Uint8Array([
|
|
36
36
|
0x9C, 0x3D, 0x76, 0xE1, 0xA2, 0x04, 0x8F, 0x5B,
|
|
37
37
|
0xA5, 0xBE, 0x76, 0x0C, 0x28, 0xF4, 0xD3, 0x91,
|
|
38
38
|
0x4C, 0x30, 0xD2, 0xEB, 0x7F, 0x59, 0x18, 0xA6,
|
|
@@ -43,7 +43,7 @@ export const DKE_4 = new Uint8Array([
|
|
|
43
43
|
0x19, 0xCB, 0x76, 0x83, 0x2F, 0xE0, 0x5A, 0x4D
|
|
44
44
|
]);
|
|
45
45
|
/** S-Box #5 */
|
|
46
|
-
export const DKE_5 = new Uint8Array([
|
|
46
|
+
export const DKE_5 = /* @__PURE__ */ new Uint8Array([
|
|
47
47
|
0x34, 0xD8, 0xC7, 0xA2, 0x0E, 0x9F, 0xB1, 0x56,
|
|
48
48
|
0xC7, 0x69, 0x38, 0xB5, 0xFA, 0x0D, 0x42, 0x1E,
|
|
49
49
|
0xE4, 0x87, 0xB3, 0xAC, 0x12, 0x69, 0xDF, 0x05,
|
|
@@ -54,7 +54,7 @@ export const DKE_5 = new Uint8Array([
|
|
|
54
54
|
0xE9, 0x18, 0x5F, 0xB0, 0x62, 0xC7, 0xA4, 0xD3
|
|
55
55
|
]);
|
|
56
56
|
/** S-Box #6 */
|
|
57
|
-
export const DKE_6 = new Uint8Array([
|
|
57
|
+
export const DKE_6 = /* @__PURE__ */ new Uint8Array([
|
|
58
58
|
0xFC, 0x96, 0xE2, 0x1B, 0x0D, 0x4A, 0x78, 0x35,
|
|
59
59
|
0xEC, 0x50, 0x74, 0xA3, 0x26, 0x1D, 0x9B, 0xF8,
|
|
60
60
|
0x56, 0xD9, 0xBE, 0xA3, 0xF2, 0x81, 0x40, 0x7C,
|
|
@@ -65,7 +65,7 @@ export const DKE_6 = new Uint8Array([
|
|
|
65
65
|
0x15, 0xEB, 0x2C, 0x38, 0xA0, 0x97, 0xF6, 0x4D
|
|
66
66
|
]);
|
|
67
67
|
/** S-Box #7 */
|
|
68
|
-
export const DKE_7 = new Uint8Array([
|
|
68
|
+
export const DKE_7 = /* @__PURE__ */ new Uint8Array([
|
|
69
69
|
0xFD, 0xA5, 0xC0, 0x16, 0x92, 0xE7, 0x3B, 0x48,
|
|
70
70
|
0x25, 0xA0, 0x69, 0x1F, 0xD4, 0x7E, 0xB3, 0x8C,
|
|
71
71
|
0x3E, 0x4B, 0x59, 0x12, 0xF6, 0x8D, 0x70, 0xAC,
|
|
@@ -76,7 +76,7 @@ export const DKE_7 = new Uint8Array([
|
|
|
76
76
|
0x15, 0x0F, 0x6A, 0x3E, 0x72, 0xCD, 0xB8, 0x94
|
|
77
77
|
]);
|
|
78
78
|
/** S-Box #8 */
|
|
79
|
-
export const DKE_8 = new Uint8Array([
|
|
79
|
+
export const DKE_8 = /* @__PURE__ */ new Uint8Array([
|
|
80
80
|
0xE4, 0xB2, 0x87, 0x5C, 0x9D, 0x03, 0x1F, 0x6A,
|
|
81
81
|
0x3E, 0xCA, 0x62, 0xD1, 0x98, 0x74, 0x0F, 0x5B,
|
|
82
82
|
0x52, 0x87, 0x1F, 0xE6, 0x4D, 0xB0, 0xA3, 0xC9,
|
|
@@ -87,7 +87,7 @@ export const DKE_8 = new Uint8Array([
|
|
|
87
87
|
0x30, 0x5C, 0x8F, 0xDE, 0xB6, 0x29, 0x71, 0x4A
|
|
88
88
|
]);
|
|
89
89
|
/** S-Box #9 */
|
|
90
|
-
export const DKE_9 = new Uint8Array([
|
|
90
|
+
export const DKE_9 = /* @__PURE__ */ new Uint8Array([
|
|
91
91
|
0x90, 0xBC, 0x24, 0x3F, 0xD6, 0xE1, 0xA7, 0x58,
|
|
92
92
|
0x35, 0x0F, 0x87, 0xEC, 0xDA, 0x16, 0xB2, 0x49,
|
|
93
93
|
0x84, 0x5A, 0xEB, 0xD6, 0xCF, 0x79, 0x31, 0x20,
|
|
@@ -98,7 +98,7 @@ export const DKE_9 = new Uint8Array([
|
|
|
98
98
|
0xE2, 0x8F, 0x30, 0x7C, 0xBD, 0x15, 0x64, 0x9A
|
|
99
99
|
]);
|
|
100
100
|
/** S-Box #10 */
|
|
101
|
-
export const DKE_10 = new Uint8Array([
|
|
101
|
+
export const DKE_10 = /* @__PURE__ */ new Uint8Array([
|
|
102
102
|
0x84, 0x69, 0xBC, 0x12, 0x37, 0xE0, 0xDA, 0xF5,
|
|
103
103
|
0x7D, 0x18, 0xAE, 0x4F, 0x90, 0x63, 0x2C, 0xB5,
|
|
104
104
|
0xC8, 0xD1, 0xA2, 0x96, 0x34, 0xE7, 0x5F, 0x0B,
|
|
@@ -109,7 +109,7 @@ export const DKE_10 = new Uint8Array([
|
|
|
109
109
|
0xA3, 0x59, 0x0D, 0x78, 0xC4, 0x16, 0xBF, 0x2E
|
|
110
110
|
]);
|
|
111
111
|
/** Test S-Box for DSTU GOST 28147:2009 */
|
|
112
|
-
export const GOST_28147_89_TEST_PARAM_SET = new Uint8Array([
|
|
112
|
+
export const GOST_28147_89_TEST_PARAM_SET = /* @__PURE__ */ new Uint8Array([
|
|
113
113
|
0x42, 0xF5, 0x91, 0x08, 0xE3, 0xBC, 0xD7, 0xA6,
|
|
114
114
|
0xC9, 0xFE, 0x81, 0x3A, 0x27, 0x4D, 0x60, 0xB5,
|
|
115
115
|
0xD8, 0xEC, 0x73, 0x9A, 0x15, 0x24, 0x6F, 0x0B,
|
|
@@ -120,7 +120,7 @@ export const GOST_28147_89_TEST_PARAM_SET = new Uint8Array([
|
|
|
120
120
|
0xC6, 0x52, 0xB0, 0x9D, 0x3E, 0x7A, 0xF4, 0x18
|
|
121
121
|
]);
|
|
122
122
|
/** Test S-Box #2 for DSTU GOST 28147:2009 */
|
|
123
|
-
export const GOSTR_3411_94_TEST_PARAM_SET = new Uint8Array([
|
|
123
|
+
export const GOSTR_3411_94_TEST_PARAM_SET = /* @__PURE__ */ new Uint8Array([
|
|
124
124
|
0x4A, 0x92, 0xD8, 0x0E, 0x6B, 0x1C, 0x7F, 0x53,
|
|
125
125
|
0xEB, 0x4C, 0x6D, 0xFA, 0x23, 0x81, 0x07, 0x59,
|
|
126
126
|
0x58, 0x1D, 0xA3, 0x42, 0xEF, 0xC7, 0x60, 0x9B,
|
package/dstu9311/index.d.ts
CHANGED
|
@@ -2,10 +2,10 @@ import { type TArg, type TRet } from "@noble/hashes/utils.js";
|
|
|
2
2
|
import type { Cipher } from "../types.js";
|
|
3
3
|
/** DSTU 9311:2024 cipher (Revised DSTU GOST 28147:2009) */
|
|
4
4
|
export declare class Dstu9311 implements Cipher {
|
|
5
|
-
private key;
|
|
6
5
|
private sbox;
|
|
7
6
|
readonly keySize = 32;
|
|
8
7
|
readonly blockSize = 8;
|
|
8
|
+
private rk;
|
|
9
9
|
/** DSTU 9311:2024 cipher (Revised DSTU GOST 28147:2009) */
|
|
10
10
|
constructor(key: TArg<Uint8Array>, sbox?: TArg<Uint8Array>);
|
|
11
11
|
proceedBlock(block: TArg<Uint8Array>, sequence: number[]): TRet<Uint8Array>;
|
|
@@ -13,4 +13,3 @@ export declare class Dstu9311 implements Cipher {
|
|
|
13
13
|
decrypt(ciphertext: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
14
14
|
}
|
|
15
15
|
export * from "./const.js";
|
|
16
|
-
export * from "./hash.js";
|
package/dstu9311/index.js
CHANGED
|
@@ -1,52 +1,56 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { abytes, createView } from "@noble/hashes/utils.js";
|
|
2
2
|
import { DKE_1, keySequences } from "./const.js";
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
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;
|
|
3
|
+
const nibble = (sbox, r, i) => {
|
|
4
|
+
const b = sbox[(r << 3) + (i >> 1)];
|
|
5
|
+
return (i & 1) === 0 ? (b >>> 4) : (b & 0x0f);
|
|
18
6
|
};
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
7
|
+
const genTable = (sbox) => {
|
|
8
|
+
const table = new Uint32Array(4 * 256);
|
|
9
|
+
for (let k = 0; k < 4; k++) {
|
|
10
|
+
for (let v = 0; v < 256; v++) {
|
|
11
|
+
const x = ((nibble(sbox, 2 * k + 1, v >> 4) << 4) | nibble(sbox, 2 * k, v & 15)) << (8 * k);
|
|
12
|
+
table[(k << 8) | v] = (x << 11) | (x >>> 21);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return table;
|
|
16
|
+
};
|
|
17
|
+
const TABLES = /* @__PURE__ */ new WeakMap();
|
|
18
|
+
const getTable = (sbox) => {
|
|
19
|
+
let t = TABLES.get(sbox);
|
|
20
|
+
if (!t)
|
|
21
|
+
TABLES.set(sbox, t = genTable(sbox));
|
|
22
|
+
return t;
|
|
25
23
|
};
|
|
24
|
+
const G = (v, t) => t[v & 0xff] ^ t[256 + ((v >>> 8) & 0xff)] ^
|
|
25
|
+
t[512 + ((v >>> 16) & 0xff)] ^ t[768 + (v >>> 24)];
|
|
26
26
|
/** DSTU 9311:2024 cipher (Revised DSTU GOST 28147:2009) */
|
|
27
27
|
export class Dstu9311 {
|
|
28
|
-
key;
|
|
29
28
|
sbox;
|
|
30
29
|
keySize = 32;
|
|
31
30
|
blockSize = 8;
|
|
31
|
+
rk = new Uint32Array(8);
|
|
32
32
|
/** DSTU 9311:2024 cipher (Revised DSTU GOST 28147:2009) */
|
|
33
33
|
constructor(key, sbox = DKE_1) {
|
|
34
|
-
this.key = key;
|
|
35
34
|
this.sbox = sbox;
|
|
36
|
-
|
|
37
|
-
|
|
35
|
+
abytes(key, this.keySize, "key");
|
|
36
|
+
abytes(sbox, 64, "sbox");
|
|
37
|
+
const view = createView(key);
|
|
38
|
+
for (let i = 0; i < 8; i++)
|
|
39
|
+
this.rk[i] = view.getUint32(i * 4, true);
|
|
38
40
|
}
|
|
39
41
|
proceedBlock(block, sequence) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
let
|
|
44
|
-
for (let i = 0; i < roundKeys.length; i++) {
|
|
42
|
+
abytes(block, this.blockSize, "block");
|
|
43
|
+
const inView = createView(block), t = getTable(this.sbox);
|
|
44
|
+
let a0 = inView.getUint32(4, true), a1 = inView.getUint32(0, true);
|
|
45
|
+
for (let i = 0; i < sequence.length; i++) {
|
|
45
46
|
const temp = a1;
|
|
46
|
-
a1 = (a0 ^ G((a1 +
|
|
47
|
+
a1 = (a0 ^ G((a1 + this.rk[sequence[i]]) >>> 0, t)) >>> 0;
|
|
47
48
|
a0 = temp;
|
|
48
49
|
}
|
|
49
|
-
|
|
50
|
+
const out = new Uint8Array(this.blockSize), outView = createView(out);
|
|
51
|
+
outView.setUint32(0, a0, true);
|
|
52
|
+
outView.setUint32(4, a1, true);
|
|
53
|
+
return out;
|
|
50
54
|
}
|
|
51
55
|
encrypt(plaintext) {
|
|
52
56
|
return this.proceedBlock(plaintext, keySequences.ENCRYPT);
|
|
@@ -56,4 +60,3 @@ export class Dstu9311 {
|
|
|
56
60
|
}
|
|
57
61
|
}
|
|
58
62
|
export * from "./const.js";
|
|
59
|
-
export * from "./hash.js";
|
package/gf/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type TArg, type TRet } from "@noble/hashes/utils.js";
|
|
2
|
+
export declare const gf256Multiply: (a: number, b: number) => number;
|
|
3
|
+
export declare const getGf2mReductionBytes: (blockSize: number) => number[];
|
|
4
|
+
export declare const gf2mMultiply: (blockSize: number, a: TArg<Uint8Array>, b: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
5
|
+
export declare const gf2mDouble: (blockSize: number, a: TArg<Uint8Array>) => TRet<Uint8Array>;
|
package/gf/index.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { copyBytes } from "@noble/hashes/utils.js";
|
|
2
|
+
const gf256Tables = /* @__PURE__ */ (() => {
|
|
3
|
+
const exp = new Uint8Array(510), log = new Uint8Array(256);
|
|
4
|
+
let x = 1;
|
|
5
|
+
for (let i = 0; i < 255; i++) {
|
|
6
|
+
exp[i] = x;
|
|
7
|
+
log[x] = i;
|
|
8
|
+
x <<= 1;
|
|
9
|
+
if (x & 0x100)
|
|
10
|
+
x ^= 0x11D;
|
|
11
|
+
}
|
|
12
|
+
for (let i = 255; i < 510; i++)
|
|
13
|
+
exp[i] = exp[i - 255];
|
|
14
|
+
return { exp, log };
|
|
15
|
+
})();
|
|
16
|
+
export const gf256Multiply = (a, b) => a === 0 || b === 0
|
|
17
|
+
? 0
|
|
18
|
+
: gf256Tables.exp[gf256Tables.log[a] + gf256Tables.log[b]];
|
|
19
|
+
const POLY = [
|
|
20
|
+
[0x87],
|
|
21
|
+
[0x25, 0x04],
|
|
22
|
+
[0x25, 0x01]
|
|
23
|
+
];
|
|
24
|
+
export const getGf2mReductionBytes = (blockSize) => POLY[Math.log2(blockSize) - 4];
|
|
25
|
+
export const gf2mMultiply = (blockSize, a, b) => {
|
|
26
|
+
const temp = copyBytes(a), result = new Uint8Array(blockSize), reductionBytes = getGf2mReductionBytes(blockSize);
|
|
27
|
+
for (let i = 0; i < blockSize * 8; i++) {
|
|
28
|
+
const byteIndex = Math.floor(i / 8);
|
|
29
|
+
const bitIndex = i % 8;
|
|
30
|
+
if (byteIndex < b.length && (b[byteIndex] & (1 << bitIndex)))
|
|
31
|
+
for (let j = 0; j < blockSize; j++)
|
|
32
|
+
result[j] ^= temp[j];
|
|
33
|
+
let carry = 0;
|
|
34
|
+
for (let j = 0; j < blockSize; j++) {
|
|
35
|
+
const nextCarry = (temp[j] & 0x80) ? 1 : 0;
|
|
36
|
+
temp[j] = ((temp[j] << 1) & 0xFF) | carry;
|
|
37
|
+
carry = nextCarry;
|
|
38
|
+
}
|
|
39
|
+
if (carry) {
|
|
40
|
+
for (let j = 0; j < reductionBytes.length; j++)
|
|
41
|
+
if (j < blockSize)
|
|
42
|
+
temp[j] ^= reductionBytes[j];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
};
|
|
47
|
+
export const gf2mDouble = (blockSize, a) => {
|
|
48
|
+
const result = copyBytes(a);
|
|
49
|
+
const reductionBytes = getGf2mReductionBytes(blockSize);
|
|
50
|
+
let carry = 0;
|
|
51
|
+
for (let j = 0; j < blockSize; j++) {
|
|
52
|
+
const nextCarry = (result[j] & 0x80) ? 1 : 0;
|
|
53
|
+
result[j] = ((result[j] << 1) & 0xFF) | carry;
|
|
54
|
+
carry = nextCarry;
|
|
55
|
+
}
|
|
56
|
+
if (carry) {
|
|
57
|
+
for (let j = 0; j < reductionBytes.length; j++)
|
|
58
|
+
if (j < blockSize)
|
|
59
|
+
result[j] ^= reductionBytes[j];
|
|
60
|
+
}
|
|
61
|
+
return result;
|
|
62
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { type CHash, type TArg, type TRet } from "@noble/hashes/utils.js";
|
|
2
|
+
import { _HMAC } from "@noble/hashes/hmac.js";
|
|
3
|
+
import { HashMD } from "@noble/hashes/_md.js";
|
|
4
|
+
/** Internal GOST 34.311-95 hash class */
|
|
5
|
+
export declare class _Gost3431195 extends HashMD<_Gost3431195> {
|
|
6
|
+
private sbox;
|
|
7
|
+
private h;
|
|
8
|
+
private checksum;
|
|
9
|
+
/** Internal GOST 34.311-95 hash class */
|
|
10
|
+
constructor(sbox?: TArg<Uint8Array>);
|
|
11
|
+
protected process(view: DataView, offset?: number): void;
|
|
12
|
+
protected get(): number[];
|
|
13
|
+
protected set(...args: number[]): void;
|
|
14
|
+
protected roundClean(): void;
|
|
15
|
+
digestInto(out: TArg<Uint8Array>): void;
|
|
16
|
+
destroy(): void;
|
|
17
|
+
_cloneInto(to?: _Gost3431195): _Gost3431195;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* GOST 34.311-95 hash function
|
|
21
|
+
*
|
|
22
|
+
* @param msg - message bytes to hash.
|
|
23
|
+
* @returns Digest bytes.
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { gost3431195 } from "@li0ard/dstu/gost34311.js";
|
|
27
|
+
*
|
|
28
|
+
* gost3431195(new Uint8Array([97, 98, 99]));
|
|
29
|
+
* gost3431195.create().update(new Uint8Array([97, 98, 99])).digest();
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare const gost3431195: TRet<CHash<_Gost3431195>>;
|
|
33
|
+
/**
|
|
34
|
+
* HMAC over GOST 34.311-95 hash function
|
|
35
|
+
*
|
|
36
|
+
* @param key - authentication key bytes
|
|
37
|
+
*/
|
|
38
|
+
export declare class Gost3431195HMAC extends _HMAC<_Gost3431195> {
|
|
39
|
+
constructor(key: TArg<Uint8Array>);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* HMAC over GOST 34.311-95 hash function
|
|
43
|
+
*
|
|
44
|
+
* @param key - authentication key bytes
|
|
45
|
+
* @param message - message bytes to authenticate
|
|
46
|
+
* @returns Authentication tag bytes.
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* import { gost3431195Hmac } from "@li0ard/dstu/gost34311.js";
|
|
50
|
+
* const key = new Uint8Array([1, 2, 3]);
|
|
51
|
+
* const message = new Uint8Array([4, 5, 6]);
|
|
52
|
+
* const mac = gost3431195Hmac(key, message);
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare const gost3431195Hmac: (key: TArg<Uint8Array>, msg: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { abytes, aexists, aoutput, clean, createHasher } from "@noble/hashes/utils.js";
|
|
2
|
+
import { Dstu9311, DKE_1 } from "../dstu9311/index.js";
|
|
3
|
+
import { numberToBytesBE } from "@noble/curves/utils.js";
|
|
4
|
+
import { _HMAC } from "@noble/hashes/hmac.js";
|
|
5
|
+
import { HashMD } from "@noble/hashes/_md.js";
|
|
6
|
+
const C3 = /* @__PURE__ */ new Uint8Array([
|
|
7
|
+
0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0xff,
|
|
8
|
+
0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0xff, 0x00,
|
|
9
|
+
0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
|
|
10
|
+
0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00
|
|
11
|
+
]);
|
|
12
|
+
const U = /* @__PURE__ */ new Uint8Array(32), V = /* @__PURE__ */ new Uint8Array(32), W = /* @__PURE__ */ new Uint8Array(32), X = /* @__PURE__ */ new Uint8Array(32), KEY = /* @__PURE__ */ new Uint8Array(32), BLOCK = /* @__PURE__ */ new Uint8Array(32), TMP = /* @__PURE__ */ new Uint8Array(8), CT = /* @__PURE__ */ new Uint8Array(8);
|
|
13
|
+
const A = (x) => {
|
|
14
|
+
for (let i = 0; i < 8; i++)
|
|
15
|
+
TMP[i] = x[24 + i] ^ x[16 + i];
|
|
16
|
+
x.copyWithin(8, 0, 24);
|
|
17
|
+
x.set(TMP);
|
|
18
|
+
};
|
|
19
|
+
const chi = (y) => {
|
|
20
|
+
const a = y[30] ^ y[28] ^ y[26] ^ y[24] ^ y[6] ^ y[0], b = y[31] ^ y[29] ^ y[27] ^ y[25] ^ y[7] ^ y[1];
|
|
21
|
+
y.copyWithin(2, 0, 30);
|
|
22
|
+
y[0] = a;
|
|
23
|
+
y[1] = b;
|
|
24
|
+
};
|
|
25
|
+
const deriveKey = (u, v) => {
|
|
26
|
+
for (let n = 0; n < 32; n++) {
|
|
27
|
+
const i = 31 - (n >> 2) - ((n & 3) << 3);
|
|
28
|
+
KEY[n] = u[i] ^ v[i];
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const encrypt = (cipher, plaintext, offset) => {
|
|
32
|
+
for (let i = 0; i < 8; i++)
|
|
33
|
+
CT[i] = plaintext[offset + 7 - i];
|
|
34
|
+
const e = cipher.encrypt(CT);
|
|
35
|
+
for (let i = 0; i < 8; i++)
|
|
36
|
+
X[offset + i] = e[7 - i];
|
|
37
|
+
};
|
|
38
|
+
const _step = (hin, m, sbox, out) => {
|
|
39
|
+
deriveKey(hin, m);
|
|
40
|
+
const k1 = new Dstu9311(KEY, sbox);
|
|
41
|
+
U.set(hin);
|
|
42
|
+
A(U);
|
|
43
|
+
V.set(m);
|
|
44
|
+
A(V);
|
|
45
|
+
A(V);
|
|
46
|
+
deriveKey(U, V);
|
|
47
|
+
const k2 = new Dstu9311(KEY, sbox);
|
|
48
|
+
A(U);
|
|
49
|
+
for (let i = 0; i < 32; i++)
|
|
50
|
+
U[i] ^= C3[i];
|
|
51
|
+
A(V);
|
|
52
|
+
A(V);
|
|
53
|
+
deriveKey(U, V);
|
|
54
|
+
const k3 = new Dstu9311(KEY, sbox);
|
|
55
|
+
A(U);
|
|
56
|
+
A(V);
|
|
57
|
+
A(V);
|
|
58
|
+
deriveKey(U, V);
|
|
59
|
+
const k4 = new Dstu9311(KEY, sbox);
|
|
60
|
+
encrypt(k4, hin, 0);
|
|
61
|
+
encrypt(k3, hin, 8);
|
|
62
|
+
encrypt(k2, hin, 16);
|
|
63
|
+
encrypt(k1, hin, 24);
|
|
64
|
+
for (let i = 0; i < 12; i++)
|
|
65
|
+
chi(X);
|
|
66
|
+
for (let i = 0; i < 32; i++)
|
|
67
|
+
W[i] = X[i] ^ m[i];
|
|
68
|
+
chi(W);
|
|
69
|
+
for (let i = 0; i < 32; i++)
|
|
70
|
+
X[i] = hin[i] ^ W[i];
|
|
71
|
+
for (let i = 0; i < 61; i++)
|
|
72
|
+
chi(X);
|
|
73
|
+
out.set(X);
|
|
74
|
+
};
|
|
75
|
+
/** Internal GOST 34.311-95 hash class */
|
|
76
|
+
export class _Gost3431195 extends HashMD {
|
|
77
|
+
sbox;
|
|
78
|
+
h = new Uint8Array(32);
|
|
79
|
+
checksum = new Uint8Array(32);
|
|
80
|
+
/** Internal GOST 34.311-95 hash class */
|
|
81
|
+
constructor(sbox = DKE_1) {
|
|
82
|
+
super(32, 32, 0, true);
|
|
83
|
+
this.sbox = sbox;
|
|
84
|
+
abytes(sbox, 64, "sbox");
|
|
85
|
+
}
|
|
86
|
+
process(view, offset = 0) {
|
|
87
|
+
let c = 0;
|
|
88
|
+
for (let i = 0; i < 32; i++) {
|
|
89
|
+
const b = view.getUint8(offset + i), s = this.checksum[i] + b + c;
|
|
90
|
+
this.checksum[i] = s;
|
|
91
|
+
c = s >> 8;
|
|
92
|
+
BLOCK[31 - i] = b;
|
|
93
|
+
}
|
|
94
|
+
_step(this.h, BLOCK, this.sbox, this.h);
|
|
95
|
+
}
|
|
96
|
+
// Unused
|
|
97
|
+
get() { return []; }
|
|
98
|
+
set(...args) { }
|
|
99
|
+
roundClean() {
|
|
100
|
+
clean(U, V, W, X, KEY, BLOCK, TMP, CT);
|
|
101
|
+
}
|
|
102
|
+
digestInto(out) {
|
|
103
|
+
aexists(this);
|
|
104
|
+
aoutput(out, this);
|
|
105
|
+
this.finished = true;
|
|
106
|
+
if (this.pos > 0) {
|
|
107
|
+
this.buffer.fill(0, this.pos);
|
|
108
|
+
this.process(this.view);
|
|
109
|
+
}
|
|
110
|
+
_step(this.h, numberToBytesBE(BigInt(this.length) * 8n, 32), this.sbox, this.h);
|
|
111
|
+
for (let i = 0; i < 32; i++)
|
|
112
|
+
BLOCK[i] = this.checksum[31 - i];
|
|
113
|
+
_step(this.h, BLOCK, this.sbox, this.h);
|
|
114
|
+
for (let i = 0; i < 32; i++)
|
|
115
|
+
out[i] = this.h[31 - i];
|
|
116
|
+
this.roundClean();
|
|
117
|
+
}
|
|
118
|
+
destroy() {
|
|
119
|
+
this.destroyed = true;
|
|
120
|
+
clean(this.buffer, this.h, this.checksum);
|
|
121
|
+
}
|
|
122
|
+
_cloneInto(to) {
|
|
123
|
+
to ||= new _Gost3431195(this.sbox);
|
|
124
|
+
this._cloneIntoMeta(to);
|
|
125
|
+
to.sbox = this.sbox;
|
|
126
|
+
to.h.set(this.h);
|
|
127
|
+
to.checksum.set(this.checksum);
|
|
128
|
+
return to;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* GOST 34.311-95 hash function
|
|
133
|
+
*
|
|
134
|
+
* @param msg - message bytes to hash.
|
|
135
|
+
* @returns Digest bytes.
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* import { gost3431195 } from "@li0ard/dstu/gost34311.js";
|
|
139
|
+
*
|
|
140
|
+
* gost3431195(new Uint8Array([97, 98, 99]));
|
|
141
|
+
* gost3431195.create().update(new Uint8Array([97, 98, 99])).digest();
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export const gost3431195 = /* @__PURE__ */ createHasher(() => new _Gost3431195());
|
|
145
|
+
/**
|
|
146
|
+
* HMAC over GOST 34.311-95 hash function
|
|
147
|
+
*
|
|
148
|
+
* @param key - authentication key bytes
|
|
149
|
+
*/
|
|
150
|
+
export class Gost3431195HMAC extends _HMAC {
|
|
151
|
+
constructor(key) { super(gost3431195, key); }
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* HMAC over GOST 34.311-95 hash function
|
|
155
|
+
*
|
|
156
|
+
* @param key - authentication key bytes
|
|
157
|
+
* @param message - message bytes to authenticate
|
|
158
|
+
* @returns Authentication tag bytes.
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* import { gost3431195Hmac } from "@li0ard/dstu/gost34311.js";
|
|
162
|
+
* const key = new Uint8Array([1, 2, 3]);
|
|
163
|
+
* const message = new Uint8Array([4, 5, 6]);
|
|
164
|
+
* const mac = gost3431195Hmac(key, message);
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
export const gost3431195Hmac = (key, msg) => new Gost3431195HMAC(key).update(msg).digest();
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from "./dstu4145/index.js";
|
|
2
2
|
export * from "./dstu9041/index.js";
|
|
3
3
|
export * from "./dstu9311/index.js";
|
|
4
|
+
export * from "./gost34311/index.js";
|
|
4
5
|
export { Kalyna128, Kalyna128_256, Kalyna256, Kalyna256_512, Kalyna512 } from "./kalyna/index.js";
|
|
5
6
|
export * from "./kupyna/index.js";
|
|
6
7
|
export * from "./modes/index.js";
|
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from "./dstu4145/index.js";
|
|
2
2
|
export * from "./dstu9041/index.js";
|
|
3
3
|
export * from "./dstu9311/index.js";
|
|
4
|
+
export * from "./gost34311/index.js";
|
|
4
5
|
export { Kalyna128, Kalyna128_256, Kalyna256, Kalyna256_512, Kalyna512 } from "./kalyna/index.js";
|
|
5
6
|
export * from "./kupyna/index.js";
|
|
6
7
|
export * from "./modes/index.js";
|
package/iso15946.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type CHash, type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
|
|
2
2
|
/** ISO/IEC 15946 KDF function */
|
|
3
3
|
export declare const iso15946_kdf: (hash: CHash<Hash<any>>, wrapAlgoOid: string, inputData: TArg<Uint8Array>, ukm?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
package/iso15946.js
CHANGED
|
@@ -4,11 +4,12 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
4
4
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
5
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
6
|
};
|
|
7
|
+
import { abytes, ahash } from "@noble/hashes/utils.js";
|
|
7
8
|
import { AsnConvert, AsnProp, AsnPropTypes } from "@peculiar/asn1-schema";
|
|
8
9
|
import { AlgorithmIdentifier } from "@peculiar/asn1-x509";
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
const COUNTER = new Uint8Array([0, 0, 0, 1]);
|
|
10
|
+
import { _Gost3431195 } from "./gost34311/index.js";
|
|
11
|
+
import { _NULL } from "./utils.js";
|
|
12
|
+
const KEY_LENGTH = /* @__PURE__ */ new Uint8Array([0, 0, 1, 0]), COUNTER = /* @__PURE__ */ new Uint8Array([0, 0, 0, 1]);
|
|
12
13
|
class SharedInfo {
|
|
13
14
|
keyInfo = new AlgorithmIdentifier();
|
|
14
15
|
entityInfo;
|
|
@@ -28,22 +29,26 @@ __decorate([
|
|
|
28
29
|
], SharedInfo.prototype, "suppPubInfo", void 0);
|
|
29
30
|
const removeLeadZeros = (bytes) => {
|
|
30
31
|
const idx = bytes.findIndex(i => i !== 0);
|
|
31
|
-
return idx === -1 ?
|
|
32
|
+
return idx === -1 ? _NULL : bytes.slice(idx);
|
|
32
33
|
};
|
|
33
34
|
const encodeSharedInfo = (oid, ukm) => new Uint8Array(AsnConvert.serialize(new SharedInfo({
|
|
34
35
|
keyInfo: new AlgorithmIdentifier({
|
|
35
36
|
algorithm: oid,
|
|
36
37
|
parameters: null
|
|
37
38
|
}),
|
|
38
|
-
entityInfo: ukm,
|
|
39
|
+
entityInfo: ukm?.buffer,
|
|
39
40
|
suppPubInfo: KEY_LENGTH.buffer
|
|
40
41
|
})));
|
|
41
42
|
/** ISO/IEC 15946 KDF function */
|
|
42
43
|
export const iso15946_kdf = (hash, wrapAlgoOid, inputData, ukm) => {
|
|
44
|
+
ahash(hash);
|
|
45
|
+
abytes(inputData, undefined, "inputData");
|
|
46
|
+
if (ukm)
|
|
47
|
+
abytes(ukm, undefined, "ukm");
|
|
43
48
|
if (hash.outputLen != 32)
|
|
44
49
|
throw new Error("Invalid hash function. Output length must be 32 bytes");
|
|
45
50
|
const hasher = hash.create();
|
|
46
|
-
return hasher.update(hasher instanceof
|
|
51
|
+
return hasher.update(hasher instanceof _Gost3431195
|
|
47
52
|
? removeLeadZeros(inputData)
|
|
48
53
|
: inputData).update(COUNTER).update(encodeSharedInfo(wrapAlgoOid, ukm)).digest();
|
|
49
54
|
};
|
package/kalyna/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type TArg, type TRet } from "@noble/hashes/utils.js";
|
|
2
2
|
import type { Cipher } from "../types.js";
|
|
3
|
-
export declare const column: (x:
|
|
3
|
+
export declare const column: (x: Uint32Array, i: number, N: number, offs: ArrayLike<number>, out: Uint32Array, o: number, table?: Uint32Array[], sign?: 1 | -1) => void;
|
|
4
4
|
export declare abstract class Kalyna implements Cipher {
|
|
5
5
|
readonly N: number;
|
|
6
6
|
/** Block size */
|
|
@@ -18,12 +18,10 @@ export declare abstract class Kalyna implements Cipher {
|
|
|
18
18
|
private makeOddKey;
|
|
19
19
|
private addkey;
|
|
20
20
|
private subkey;
|
|
21
|
-
private column;
|
|
22
|
-
private invColumn;
|
|
23
21
|
private G;
|
|
24
22
|
private GL;
|
|
25
|
-
private IMC;
|
|
26
23
|
private IG;
|
|
24
|
+
private IMC;
|
|
27
25
|
private IGL;
|
|
28
26
|
/** Encrypt block */
|
|
29
27
|
encrypt(block: TArg<Uint8Array>): TRet<Uint8Array>;
|