@li0ard/gost 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 +1 -0
- package/gf/gf256.d.ts +1 -0
- package/gf/gf256.js +62 -0
- package/gf/index.d.ts +4 -0
- package/gf/index.js +18 -0
- package/gost3410/const.js +20 -20
- package/gost3410/vko.js +1 -1
- package/gost341194/index.d.ts +3 -3
- package/gost341194/index.js +9 -9
- package/kdf.js +4 -6
- package/kuznyechik/const.js +0 -5
- package/kuznyechik/index.d.ts +9 -2
- package/kuznyechik/index.js +27 -35
- package/magma/const.d.ts +39 -39
- package/magma/const.js +191 -192
- package/magma/index.d.ts +9 -6
- package/magma/index.js +39 -47
- package/modes/_keytransform.d.ts +1 -1
- package/modes/ctr.js +5 -7
- package/modes/mac.js +43 -37
- package/modes/mgm.js +2 -17
- package/modes/wrap.d.ts +1 -1
- package/modes/wrap.js +1 -1
- package/package.json +1 -1
- package/streebog/index.d.ts +20 -6
- package/streebog/index.js +24 -11
package/magma/const.js
CHANGED
|
@@ -1,209 +1,208 @@
|
|
|
1
|
-
/** S-Box from RFC
|
|
2
|
-
export const ID_TC26_GOST_28147_PARAM_Z = [
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
];
|
|
1
|
+
/** S-Box from RFC 8891 */
|
|
2
|
+
export const ID_TC26_GOST_28147_PARAM_Z = new Uint8Array([
|
|
3
|
+
0x0c, 0x04, 0x06, 0x02, 0x0a, 0x05, 0x0b, 0x09, 0x0e, 0x08, 0x0d, 0x07, 0x00, 0x03, 0x0f, 0x01,
|
|
4
|
+
0x06, 0x08, 0x02, 0x03, 0x09, 0x0a, 0x05, 0x0c, 0x01, 0x0e, 0x04, 0x07, 0x0b, 0x0d, 0x00, 0x0f,
|
|
5
|
+
0x0b, 0x03, 0x05, 0x08, 0x02, 0x0f, 0x0a, 0x0d, 0x0e, 0x01, 0x07, 0x04, 0x0c, 0x09, 0x06, 0x00,
|
|
6
|
+
0x0c, 0x08, 0x02, 0x01, 0x0d, 0x04, 0x0f, 0x06, 0x07, 0x00, 0x0a, 0x05, 0x03, 0x0e, 0x09, 0x0b,
|
|
7
|
+
0x07, 0x0f, 0x05, 0x0a, 0x08, 0x01, 0x06, 0x0d, 0x00, 0x09, 0x03, 0x0e, 0x0b, 0x04, 0x02, 0x0c,
|
|
8
|
+
0x05, 0x0d, 0x0f, 0x06, 0x09, 0x02, 0x0c, 0x0a, 0x0b, 0x07, 0x08, 0x01, 0x04, 0x03, 0x0e, 0x00,
|
|
9
|
+
0x08, 0x0e, 0x02, 0x05, 0x06, 0x09, 0x01, 0x0c, 0x0f, 0x04, 0x0b, 0x00, 0x0d, 0x0a, 0x03, 0x07,
|
|
10
|
+
0x01, 0x07, 0x0e, 0x0d, 0x00, 0x05, 0x08, 0x03, 0x04, 0x0f, 0x0a, 0x06, 0x09, 0x0c, 0x0b, 0x02,
|
|
11
|
+
]);
|
|
12
12
|
/** S-Box from RFC 4357 aka `CryptoPro Paramset A` */
|
|
13
|
-
export const ID_GOST_28147_89_CRYPTO_PRO_A_PARAM_SET = [
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
];
|
|
13
|
+
export const ID_GOST_28147_89_CRYPTO_PRO_A_PARAM_SET = new Uint8Array([
|
|
14
|
+
0x09, 0x06, 0x03, 0x02, 0x08, 0x0b, 0x01, 0x07, 0x0a, 0x04, 0x0e, 0x0f, 0x0c, 0x00, 0x0d, 0x05,
|
|
15
|
+
0x03, 0x07, 0x0e, 0x09, 0x08, 0x0a, 0x0f, 0x00, 0x05, 0x02, 0x06, 0x0c, 0x0b, 0x04, 0x0d, 0x01,
|
|
16
|
+
0x0e, 0x04, 0x06, 0x02, 0x0b, 0x03, 0x0d, 0x08, 0x0c, 0x0f, 0x05, 0x0a, 0x00, 0x07, 0x01, 0x09,
|
|
17
|
+
0x0e, 0x07, 0x0a, 0x0c, 0x0d, 0x01, 0x03, 0x09, 0x00, 0x02, 0x0b, 0x04, 0x0f, 0x08, 0x05, 0x06,
|
|
18
|
+
0x0b, 0x05, 0x01, 0x09, 0x08, 0x0d, 0x0f, 0x00, 0x0e, 0x04, 0x02, 0x03, 0x0c, 0x07, 0x0a, 0x06,
|
|
19
|
+
0x03, 0x0a, 0x0d, 0x0c, 0x01, 0x02, 0x00, 0x0b, 0x07, 0x05, 0x09, 0x04, 0x08, 0x0f, 0x0e, 0x06,
|
|
20
|
+
0x01, 0x0d, 0x02, 0x09, 0x07, 0x0a, 0x06, 0x00, 0x08, 0x0c, 0x04, 0x05, 0x0f, 0x03, 0x0b, 0x0e,
|
|
21
|
+
0x0b, 0x0a, 0x0f, 0x05, 0x00, 0x0c, 0x0e, 0x08, 0x06, 0x02, 0x03, 0x09, 0x01, 0x07, 0x0d, 0x04,
|
|
22
|
+
]);
|
|
23
23
|
/** S-Box from RFC 4357 aka `CryptoPro Paramset B` */
|
|
24
|
-
export const ID_GOST_28147_89_CRYPTO_PRO_B_PARAM_SET = [
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
];
|
|
24
|
+
export const ID_GOST_28147_89_CRYPTO_PRO_B_PARAM_SET = new Uint8Array([
|
|
25
|
+
0x08, 0x04, 0x0b, 0x01, 0x03, 0x05, 0x00, 0x09, 0x02, 0x0e, 0x0a, 0x0c, 0x0d, 0x06, 0x07, 0x0f,
|
|
26
|
+
0x00, 0x01, 0x02, 0x0a, 0x04, 0x0d, 0x05, 0x0c, 0x09, 0x07, 0x03, 0x0f, 0x0b, 0x08, 0x06, 0x0e,
|
|
27
|
+
0x0e, 0x0c, 0x00, 0x0a, 0x09, 0x02, 0x0d, 0x0b, 0x07, 0x05, 0x08, 0x0f, 0x03, 0x06, 0x01, 0x04,
|
|
28
|
+
0x07, 0x05, 0x00, 0x0d, 0x0b, 0x06, 0x01, 0x02, 0x03, 0x0a, 0x0c, 0x0f, 0x04, 0x0e, 0x09, 0x08,
|
|
29
|
+
0x02, 0x07, 0x0c, 0x0f, 0x09, 0x05, 0x0a, 0x0b, 0x01, 0x04, 0x00, 0x0d, 0x06, 0x08, 0x0e, 0x03,
|
|
30
|
+
0x08, 0x03, 0x02, 0x06, 0x04, 0x0d, 0x0e, 0x0b, 0x0c, 0x01, 0x07, 0x0f, 0x0a, 0x00, 0x09, 0x05,
|
|
31
|
+
0x05, 0x02, 0x0a, 0x0b, 0x09, 0x01, 0x0c, 0x03, 0x07, 0x04, 0x0d, 0x00, 0x06, 0x0f, 0x08, 0x0e,
|
|
32
|
+
0x00, 0x04, 0x0b, 0x0e, 0x08, 0x03, 0x07, 0x01, 0x0a, 0x02, 0x09, 0x06, 0x0f, 0x0d, 0x05, 0x0c,
|
|
33
|
+
]);
|
|
34
34
|
/** S-Box from RFC 4357 aka `CryptoPro Paramset C` */
|
|
35
|
-
export const ID_GOST_28147_89_CRYPTO_PRO_C_PARAM_SET = [
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
];
|
|
35
|
+
export const ID_GOST_28147_89_CRYPTO_PRO_C_PARAM_SET = new Uint8Array([
|
|
36
|
+
0x01, 0x0b, 0x0c, 0x02, 0x09, 0x0d, 0x00, 0x0f, 0x04, 0x05, 0x08, 0x0e, 0x0a, 0x07, 0x06, 0x03,
|
|
37
|
+
0x00, 0x01, 0x07, 0x0d, 0x0b, 0x04, 0x05, 0x02, 0x08, 0x0e, 0x0f, 0x0c, 0x09, 0x0a, 0x06, 0x03,
|
|
38
|
+
0x08, 0x02, 0x05, 0x00, 0x04, 0x09, 0x0f, 0x0a, 0x03, 0x07, 0x0c, 0x0d, 0x06, 0x0e, 0x01, 0x0b,
|
|
39
|
+
0x03, 0x06, 0x00, 0x01, 0x05, 0x0d, 0x0a, 0x08, 0x0b, 0x02, 0x09, 0x07, 0x0e, 0x0f, 0x0c, 0x04,
|
|
40
|
+
0x08, 0x0d, 0x0b, 0x00, 0x04, 0x05, 0x01, 0x02, 0x09, 0x03, 0x0c, 0x0e, 0x06, 0x0f, 0x0a, 0x07,
|
|
41
|
+
0x0c, 0x09, 0x0b, 0x01, 0x08, 0x0e, 0x02, 0x04, 0x07, 0x03, 0x06, 0x05, 0x0a, 0x00, 0x0f, 0x0d,
|
|
42
|
+
0x0a, 0x09, 0x06, 0x08, 0x0d, 0x0e, 0x02, 0x00, 0x0f, 0x03, 0x05, 0x0b, 0x04, 0x01, 0x0c, 0x07,
|
|
43
|
+
0x07, 0x04, 0x00, 0x05, 0x0a, 0x02, 0x0f, 0x0e, 0x0c, 0x06, 0x01, 0x0b, 0x0d, 0x09, 0x03, 0x08,
|
|
44
|
+
]);
|
|
45
45
|
/** S-Box from RFC 4357 aka `CryptoPro Paramset D` */
|
|
46
|
-
export const ID_GOST_28147_89_CRYPTO_PRO_D_PARAM_SET = [
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
];
|
|
46
|
+
export const ID_GOST_28147_89_CRYPTO_PRO_D_PARAM_SET = new Uint8Array([
|
|
47
|
+
0x0f, 0x0c, 0x02, 0x0a, 0x06, 0x04, 0x05, 0x00, 0x07, 0x09, 0x0e, 0x0d, 0x01, 0x0b, 0x08, 0x03,
|
|
48
|
+
0x0b, 0x06, 0x03, 0x04, 0x0c, 0x0f, 0x0e, 0x02, 0x07, 0x0d, 0x08, 0x00, 0x05, 0x0a, 0x09, 0x01,
|
|
49
|
+
0x01, 0x0c, 0x0b, 0x00, 0x0f, 0x0e, 0x06, 0x05, 0x0a, 0x0d, 0x04, 0x08, 0x09, 0x03, 0x07, 0x02,
|
|
50
|
+
0x01, 0x05, 0x0e, 0x0c, 0x0a, 0x07, 0x00, 0x0d, 0x06, 0x02, 0x0b, 0x04, 0x09, 0x03, 0x0f, 0x08,
|
|
51
|
+
0x00, 0x0c, 0x08, 0x09, 0x0d, 0x02, 0x0a, 0x0b, 0x07, 0x03, 0x06, 0x05, 0x04, 0x0e, 0x0f, 0x01,
|
|
52
|
+
0x08, 0x00, 0x0f, 0x03, 0x02, 0x05, 0x0e, 0x0b, 0x01, 0x0a, 0x04, 0x07, 0x0c, 0x09, 0x0d, 0x06,
|
|
53
|
+
0x03, 0x00, 0x06, 0x0f, 0x01, 0x0e, 0x09, 0x02, 0x0d, 0x08, 0x0c, 0x04, 0x0b, 0x0a, 0x05, 0x07,
|
|
54
|
+
0x01, 0x0a, 0x06, 0x08, 0x0f, 0x0b, 0x00, 0x04, 0x0c, 0x03, 0x05, 0x09, 0x07, 0x0d, 0x02, 0x0e,
|
|
55
|
+
]);
|
|
56
56
|
/** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
|
|
57
|
-
export const DSSZZI_UA_DKE_1 = [
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
];
|
|
67
|
-
// Converted from tables by script, accuracy not guaranteed
|
|
57
|
+
export const DSSZZI_UA_DKE_1 = new Uint8Array([
|
|
58
|
+
0x0a, 0x09, 0x0d, 0x06, 0x0e, 0x0b, 0x04, 0x05, 0x0f, 0x01, 0x03, 0x0c, 0x07, 0x00, 0x08, 0x02,
|
|
59
|
+
0x08, 0x00, 0x0c, 0x04, 0x09, 0x06, 0x07, 0x0b, 0x02, 0x03, 0x01, 0x0f, 0x05, 0x0e, 0x0a, 0x0d,
|
|
60
|
+
0x0f, 0x06, 0x05, 0x08, 0x0e, 0x0b, 0x0a, 0x04, 0x0c, 0x00, 0x03, 0x07, 0x02, 0x09, 0x01, 0x0d,
|
|
61
|
+
0x03, 0x08, 0x0d, 0x09, 0x06, 0x0b, 0x0f, 0x00, 0x02, 0x05, 0x0c, 0x0a, 0x04, 0x0e, 0x01, 0x07,
|
|
62
|
+
0x0f, 0x08, 0x0e, 0x09, 0x07, 0x02, 0x00, 0x0d, 0x0c, 0x06, 0x01, 0x05, 0x0b, 0x04, 0x03, 0x0a,
|
|
63
|
+
0x02, 0x08, 0x09, 0x07, 0x05, 0x0f, 0x00, 0x0b, 0x0c, 0x01, 0x0d, 0x0e, 0x0a, 0x03, 0x06, 0x04,
|
|
64
|
+
0x03, 0x08, 0x0b, 0x05, 0x06, 0x04, 0x0e, 0x0a, 0x02, 0x0c, 0x01, 0x07, 0x09, 0x0f, 0x0d, 0x00,
|
|
65
|
+
0x01, 0x02, 0x03, 0x0e, 0x06, 0x0d, 0x0b, 0x08, 0x0f, 0x0a, 0x0c, 0x05, 0x07, 0x09, 0x00, 0x04,
|
|
66
|
+
]);
|
|
68
67
|
/** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
|
|
69
|
-
export const DSSZZI_UA_DKE_2 = [
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
];
|
|
68
|
+
export const DSSZZI_UA_DKE_2 = new Uint8Array([
|
|
69
|
+
0x0e, 0x09, 0x03, 0x07, 0x0f, 0x04, 0x0c, 0x0b, 0x06, 0x0a, 0x0d, 0x01, 0x00, 0x05, 0x08, 0x02,
|
|
70
|
+
0x0a, 0x0d, 0x0c, 0x07, 0x06, 0x0e, 0x08, 0x01, 0x0f, 0x03, 0x0b, 0x04, 0x00, 0x09, 0x05, 0x02,
|
|
71
|
+
0x04, 0x0b, 0x01, 0x0f, 0x09, 0x02, 0x0e, 0x0c, 0x06, 0x0a, 0x08, 0x07, 0x03, 0x05, 0x00, 0x0d,
|
|
72
|
+
0x04, 0x05, 0x01, 0x0c, 0x07, 0x0e, 0x09, 0x02, 0x0a, 0x0f, 0x0b, 0x0d, 0x00, 0x08, 0x06, 0x03,
|
|
73
|
+
0x0c, 0x0b, 0x03, 0x09, 0x0f, 0x00, 0x04, 0x05, 0x07, 0x02, 0x0e, 0x0d, 0x01, 0x0a, 0x08, 0x06,
|
|
74
|
+
0x08, 0x07, 0x03, 0x0a, 0x09, 0x06, 0x0e, 0x05, 0x0d, 0x00, 0x04, 0x0c, 0x01, 0x02, 0x0f, 0x0b,
|
|
75
|
+
0x0f, 0x00, 0x0e, 0x06, 0x08, 0x0d, 0x05, 0x09, 0x0a, 0x03, 0x01, 0x0c, 0x04, 0x0b, 0x07, 0x02,
|
|
76
|
+
0x04, 0x03, 0x0e, 0x0d, 0x05, 0x00, 0x02, 0x0b, 0x01, 0x0a, 0x07, 0x06, 0x09, 0x0f, 0x08, 0x0c,
|
|
77
|
+
]);
|
|
79
78
|
/** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
|
|
80
|
-
export const DSSZZI_UA_DKE_3 = [
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
];
|
|
79
|
+
export const DSSZZI_UA_DKE_3 = new Uint8Array([
|
|
80
|
+
0x0d, 0x09, 0x01, 0x0e, 0x07, 0x02, 0x0c, 0x05, 0x04, 0x0b, 0x06, 0x0f, 0x03, 0x08, 0x0a, 0x00,
|
|
81
|
+
0x07, 0x08, 0x06, 0x0b, 0x00, 0x03, 0x04, 0x0d, 0x09, 0x05, 0x0f, 0x0e, 0x0a, 0x0c, 0x02, 0x01,
|
|
82
|
+
0x0a, 0x05, 0x03, 0x0c, 0x09, 0x08, 0x0d, 0x06, 0x04, 0x0f, 0x0e, 0x00, 0x02, 0x0b, 0x01, 0x07,
|
|
83
|
+
0x0b, 0x0a, 0x0c, 0x01, 0x05, 0x06, 0x09, 0x0e, 0x02, 0x0d, 0x0f, 0x07, 0x00, 0x04, 0x03, 0x08,
|
|
84
|
+
0x05, 0x0b, 0x03, 0x00, 0x0f, 0x09, 0x0e, 0x04, 0x01, 0x0c, 0x08, 0x06, 0x02, 0x0a, 0x07, 0x0d,
|
|
85
|
+
0x04, 0x03, 0x0b, 0x0d, 0x01, 0x0f, 0x08, 0x02, 0x07, 0x0e, 0x0c, 0x09, 0x0a, 0x00, 0x06, 0x05,
|
|
86
|
+
0x03, 0x07, 0x08, 0x0b, 0x01, 0x0e, 0x05, 0x00, 0x0d, 0x04, 0x0c, 0x0a, 0x02, 0x09, 0x0f, 0x06,
|
|
87
|
+
0x06, 0x0d, 0x0c, 0x0a, 0x0b, 0x07, 0x09, 0x03, 0x0f, 0x0e, 0x01, 0x02, 0x00, 0x08, 0x04, 0x05,
|
|
88
|
+
]);
|
|
90
89
|
/** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
|
|
91
|
-
export const DSSZZI_UA_DKE_4 = [
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
];
|
|
90
|
+
export const DSSZZI_UA_DKE_4 = new Uint8Array([
|
|
91
|
+
0x09, 0x0c, 0x03, 0x0d, 0x07, 0x06, 0x0e, 0x01, 0x0a, 0x02, 0x00, 0x04, 0x08, 0x0f, 0x05, 0x0b,
|
|
92
|
+
0x0a, 0x05, 0x0b, 0x0e, 0x07, 0x06, 0x00, 0x0c, 0x02, 0x08, 0x0f, 0x04, 0x0d, 0x03, 0x09, 0x01,
|
|
93
|
+
0x04, 0x0c, 0x03, 0x00, 0x0d, 0x02, 0x0e, 0x0b, 0x07, 0x0f, 0x05, 0x09, 0x01, 0x08, 0x0a, 0x06,
|
|
94
|
+
0x03, 0x09, 0x04, 0x05, 0x0e, 0x07, 0x08, 0x06, 0x0d, 0x00, 0x02, 0x0f, 0x0b, 0x0c, 0x0a, 0x01,
|
|
95
|
+
0x02, 0x09, 0x0c, 0x0f, 0x0d, 0x0b, 0x04, 0x01, 0x07, 0x05, 0x03, 0x0e, 0x06, 0x08, 0x0a, 0x00,
|
|
96
|
+
0x0e, 0x05, 0x0d, 0x0b, 0x01, 0x09, 0x04, 0x02, 0x0f, 0x08, 0x07, 0x00, 0x03, 0x0c, 0x0a, 0x06,
|
|
97
|
+
0x0e, 0x06, 0x05, 0x0a, 0x09, 0x0d, 0x04, 0x08, 0x0b, 0x0c, 0x00, 0x03, 0x07, 0x01, 0x0f, 0x02,
|
|
98
|
+
0x01, 0x09, 0x0c, 0x0b, 0x07, 0x06, 0x08, 0x03, 0x02, 0x0f, 0x0e, 0x00, 0x05, 0x0a, 0x04, 0x0d,
|
|
99
|
+
]);
|
|
101
100
|
/** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
|
|
102
|
-
export const DSSZZI_UA_DKE_5 = [
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
];
|
|
101
|
+
export const DSSZZI_UA_DKE_5 = new Uint8Array([
|
|
102
|
+
0x03, 0x04, 0x0d, 0x08, 0x0c, 0x07, 0x0a, 0x02, 0x00, 0x0e, 0x09, 0x0f, 0x0b, 0x01, 0x05, 0x06,
|
|
103
|
+
0x0c, 0x07, 0x06, 0x09, 0x03, 0x08, 0x0b, 0x05, 0x0f, 0x0a, 0x00, 0x0d, 0x04, 0x02, 0x01, 0x0e,
|
|
104
|
+
0x0e, 0x04, 0x08, 0x07, 0x0b, 0x03, 0x0a, 0x0c, 0x01, 0x02, 0x06, 0x09, 0x0d, 0x0f, 0x00, 0x05,
|
|
105
|
+
0x03, 0x09, 0x06, 0x0d, 0x08, 0x0f, 0x0a, 0x02, 0x07, 0x0e, 0x0c, 0x00, 0x0b, 0x04, 0x01, 0x05,
|
|
106
|
+
0x05, 0x0c, 0x0a, 0x07, 0x02, 0x01, 0x0f, 0x0d, 0x0e, 0x03, 0x0b, 0x04, 0x00, 0x08, 0x09, 0x06,
|
|
107
|
+
0x01, 0x08, 0x0b, 0x0e, 0x07, 0x04, 0x0a, 0x00, 0x0c, 0x03, 0x05, 0x0d, 0x09, 0x0f, 0x06, 0x02,
|
|
108
|
+
0x09, 0x0b, 0x0a, 0x0d, 0x05, 0x0e, 0x02, 0x03, 0x00, 0x06, 0x04, 0x0c, 0x0f, 0x01, 0x07, 0x08,
|
|
109
|
+
0x0e, 0x09, 0x01, 0x08, 0x05, 0x0f, 0x0b, 0x00, 0x06, 0x02, 0x0c, 0x07, 0x0a, 0x04, 0x0d, 0x03,
|
|
110
|
+
]);
|
|
112
111
|
/** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
|
|
113
|
-
export const DSSZZI_UA_DKE_6 = [
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
];
|
|
112
|
+
export const DSSZZI_UA_DKE_6 = new Uint8Array([
|
|
113
|
+
0x0f, 0x0c, 0x09, 0x06, 0x0e, 0x02, 0x01, 0x0b, 0x00, 0x0d, 0x04, 0x0a, 0x07, 0x08, 0x03, 0x05,
|
|
114
|
+
0x0e, 0x0c, 0x05, 0x00, 0x07, 0x04, 0x0a, 0x03, 0x02, 0x06, 0x01, 0x0d, 0x09, 0x0b, 0x0f, 0x08,
|
|
115
|
+
0x05, 0x06, 0x0d, 0x09, 0x0b, 0x0e, 0x0a, 0x03, 0x0f, 0x02, 0x08, 0x01, 0x04, 0x00, 0x07, 0x0c,
|
|
116
|
+
0x01, 0x0f, 0x07, 0x04, 0x02, 0x0e, 0x0c, 0x03, 0x06, 0x0b, 0x09, 0x08, 0x00, 0x05, 0x0a, 0x0d,
|
|
117
|
+
0x0f, 0x09, 0x0e, 0x06, 0x0d, 0x01, 0x05, 0x08, 0x04, 0x02, 0x03, 0x0c, 0x0a, 0x0b, 0x00, 0x07,
|
|
118
|
+
0x0b, 0x00, 0x0d, 0x07, 0x0c, 0x0e, 0x01, 0x04, 0x02, 0x03, 0x06, 0x08, 0x0a, 0x05, 0x0f, 0x09,
|
|
119
|
+
0x07, 0x0e, 0x0f, 0x08, 0x0d, 0x00, 0x0b, 0x03, 0x0a, 0x01, 0x04, 0x02, 0x09, 0x0c, 0x06, 0x05,
|
|
120
|
+
0x01, 0x05, 0x0e, 0x0b, 0x02, 0x0c, 0x03, 0x08, 0x0a, 0x00, 0x09, 0x07, 0x0f, 0x06, 0x04, 0x0d,
|
|
121
|
+
]);
|
|
123
122
|
/** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
|
|
124
|
-
export const DSSZZI_UA_DKE_7 = [
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
];
|
|
123
|
+
export const DSSZZI_UA_DKE_7 = new Uint8Array([
|
|
124
|
+
0x0f, 0x0d, 0x0a, 0x05, 0x0c, 0x00, 0x01, 0x06, 0x09, 0x02, 0x0e, 0x07, 0x03, 0x0b, 0x04, 0x08,
|
|
125
|
+
0x02, 0x05, 0x0a, 0x00, 0x06, 0x09, 0x01, 0x0f, 0x0d, 0x04, 0x07, 0x0e, 0x0b, 0x03, 0x08, 0x0c,
|
|
126
|
+
0x03, 0x0e, 0x04, 0x0b, 0x05, 0x09, 0x01, 0x02, 0x0f, 0x06, 0x08, 0x0d, 0x07, 0x00, 0x0a, 0x0c,
|
|
127
|
+
0x04, 0x0a, 0x0b, 0x09, 0x0f, 0x02, 0x0e, 0x05, 0x0d, 0x01, 0x03, 0x06, 0x00, 0x07, 0x0c, 0x08,
|
|
128
|
+
0x0f, 0x06, 0x05, 0x08, 0x09, 0x07, 0x0c, 0x0b, 0x00, 0x0a, 0x03, 0x01, 0x02, 0x04, 0x0d, 0x0e,
|
|
129
|
+
0x0c, 0x0b, 0x0f, 0x04, 0x05, 0x01, 0x0e, 0x09, 0x00, 0x08, 0x0d, 0x02, 0x0a, 0x07, 0x03, 0x06,
|
|
130
|
+
0x0d, 0x02, 0x04, 0x08, 0x0b, 0x0c, 0x01, 0x03, 0x0a, 0x05, 0x09, 0x0e, 0x07, 0x0f, 0x00, 0x06,
|
|
131
|
+
0x01, 0x05, 0x00, 0x0f, 0x06, 0x0a, 0x03, 0x0e, 0x07, 0x02, 0x0c, 0x0d, 0x0b, 0x08, 0x09, 0x04,
|
|
132
|
+
]);
|
|
134
133
|
/** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
|
|
135
|
-
export const DSSZZI_UA_DKE_8 = [
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
];
|
|
134
|
+
export const DSSZZI_UA_DKE_8 = new Uint8Array([
|
|
135
|
+
0x0e, 0x04, 0x0b, 0x02, 0x08, 0x07, 0x05, 0x0c, 0x09, 0x0d, 0x00, 0x03, 0x01, 0x0f, 0x06, 0x0a,
|
|
136
|
+
0x03, 0x0e, 0x0c, 0x0a, 0x06, 0x02, 0x0d, 0x01, 0x09, 0x08, 0x07, 0x04, 0x00, 0x0f, 0x05, 0x0b,
|
|
137
|
+
0x05, 0x02, 0x08, 0x07, 0x01, 0x0f, 0x0e, 0x06, 0x04, 0x0d, 0x0b, 0x00, 0x0a, 0x03, 0x0c, 0x09,
|
|
138
|
+
0x0c, 0x0a, 0x07, 0x0d, 0x0e, 0x03, 0x00, 0x02, 0x09, 0x05, 0x01, 0x06, 0x0b, 0x04, 0x0f, 0x08,
|
|
139
|
+
0x06, 0x03, 0x0f, 0x07, 0x00, 0x09, 0x0a, 0x08, 0x0b, 0x0c, 0x04, 0x01, 0x05, 0x02, 0x0d, 0x0e,
|
|
140
|
+
0x06, 0x0d, 0x0f, 0x01, 0x05, 0x03, 0x08, 0x00, 0x0b, 0x0a, 0x0e, 0x04, 0x09, 0x0c, 0x02, 0x07,
|
|
141
|
+
0x02, 0x0f, 0x0c, 0x05, 0x0b, 0x01, 0x03, 0x0e, 0x00, 0x06, 0x0d, 0x0a, 0x07, 0x09, 0x04, 0x08,
|
|
142
|
+
0x03, 0x00, 0x05, 0x0c, 0x08, 0x0f, 0x0d, 0x0e, 0x0b, 0x06, 0x02, 0x09, 0x07, 0x01, 0x04, 0x0a,
|
|
143
|
+
]);
|
|
145
144
|
/** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
|
|
146
|
-
export const DSSZZI_UA_DKE_9 = [
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
];
|
|
145
|
+
export const DSSZZI_UA_DKE_9 = new Uint8Array([
|
|
146
|
+
0x09, 0x00, 0x0b, 0x0c, 0x02, 0x04, 0x03, 0x0f, 0x0d, 0x06, 0x0e, 0x01, 0x0a, 0x07, 0x05, 0x08,
|
|
147
|
+
0x03, 0x05, 0x00, 0x0f, 0x08, 0x07, 0x0e, 0x0c, 0x0d, 0x0a, 0x01, 0x06, 0x0b, 0x02, 0x04, 0x09,
|
|
148
|
+
0x08, 0x04, 0x05, 0x0a, 0x0e, 0x0b, 0x0d, 0x06, 0x0c, 0x0f, 0x07, 0x09, 0x03, 0x01, 0x02, 0x00,
|
|
149
|
+
0x05, 0x04, 0x0f, 0x00, 0x0c, 0x0b, 0x0a, 0x09, 0x01, 0x0e, 0x08, 0x06, 0x03, 0x02, 0x0d, 0x07,
|
|
150
|
+
0x07, 0x0c, 0x03, 0x00, 0x06, 0x08, 0x0e, 0x0b, 0x01, 0x0f, 0x0d, 0x0a, 0x09, 0x05, 0x02, 0x04,
|
|
151
|
+
0x07, 0x04, 0x03, 0x0b, 0x06, 0x0a, 0x08, 0x01, 0x09, 0x0c, 0x0e, 0x0d, 0x00, 0x0f, 0x02, 0x05,
|
|
152
|
+
0x07, 0x0e, 0x09, 0x0f, 0x01, 0x04, 0x08, 0x03, 0x0b, 0x0d, 0x00, 0x02, 0x06, 0x0a, 0x05, 0x0c,
|
|
153
|
+
0x0e, 0x02, 0x08, 0x0f, 0x03, 0x00, 0x07, 0x0c, 0x0b, 0x0d, 0x01, 0x05, 0x06, 0x04, 0x09, 0x0a,
|
|
154
|
+
]);
|
|
156
155
|
/** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
|
|
157
|
-
export const DSSZZI_UA_DKE_10 = [
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
];
|
|
167
|
-
export const ID_GOST_28147_89_TEST_PARAM_SET = [
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
];
|
|
177
|
-
export const ID_GOSTR_3411_94_TEST_PARAM_SET = [
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
];
|
|
187
|
-
export const ID_GOSTR_3411_94_CRYPTOPRO_PARAM_SET = [
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
];
|
|
197
|
-
export const EAC_PARAM_SET = [
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
];
|
|
156
|
+
export const DSSZZI_UA_DKE_10 = new Uint8Array([
|
|
157
|
+
0x08, 0x04, 0x06, 0x09, 0x0b, 0x0c, 0x01, 0x02, 0x03, 0x07, 0x0e, 0x00, 0x0d, 0x0a, 0x0f, 0x05,
|
|
158
|
+
0x07, 0x0d, 0x01, 0x08, 0x0a, 0x0e, 0x04, 0x0f, 0x09, 0x00, 0x06, 0x03, 0x02, 0x0c, 0x0b, 0x05,
|
|
159
|
+
0x0c, 0x08, 0x0d, 0x01, 0x0a, 0x02, 0x09, 0x06, 0x03, 0x04, 0x0e, 0x07, 0x05, 0x0f, 0x00, 0x0b,
|
|
160
|
+
0x02, 0x0b, 0x03, 0x04, 0x0c, 0x07, 0x09, 0x0d, 0x0f, 0x08, 0x05, 0x00, 0x01, 0x0e, 0x0a, 0x06,
|
|
161
|
+
0x08, 0x03, 0x0d, 0x0a, 0x0e, 0x0f, 0x05, 0x01, 0x04, 0x07, 0x0b, 0x0c, 0x02, 0x00, 0x06, 0x09,
|
|
162
|
+
0x04, 0x0c, 0x09, 0x0b, 0x0e, 0x0a, 0x07, 0x06, 0x03, 0x05, 0x00, 0x0f, 0x01, 0x02, 0x08, 0x0d,
|
|
163
|
+
0x05, 0x08, 0x0e, 0x07, 0x03, 0x00, 0x01, 0x0d, 0x0a, 0x06, 0x09, 0x02, 0x0f, 0x0b, 0x0c, 0x04,
|
|
164
|
+
0x0a, 0x03, 0x05, 0x09, 0x00, 0x0d, 0x07, 0x08, 0x0c, 0x04, 0x01, 0x06, 0x0b, 0x0f, 0x02, 0x0e,
|
|
165
|
+
]);
|
|
166
|
+
export const ID_GOST_28147_89_TEST_PARAM_SET = new Uint8Array([
|
|
167
|
+
4, 2, 15, 5, 9, 1, 0, 8, 14, 3, 11, 12, 13, 7, 10, 6,
|
|
168
|
+
12, 9, 15, 14, 8, 1, 3, 10, 2, 7, 4, 13, 6, 0, 11, 5,
|
|
169
|
+
13, 8, 14, 12, 7, 3, 9, 10, 1, 5, 2, 4, 6, 15, 0, 11,
|
|
170
|
+
14, 9, 11, 2, 5, 15, 7, 1, 0, 13, 12, 6, 10, 4, 3, 8,
|
|
171
|
+
3, 14, 5, 9, 6, 8, 0, 13, 10, 11, 7, 12, 2, 1, 15, 4,
|
|
172
|
+
8, 15, 6, 11, 1, 9, 12, 5, 13, 3, 7, 10, 0, 14, 2, 4,
|
|
173
|
+
9, 11, 12, 0, 3, 6, 7, 5, 4, 8, 14, 15, 1, 10, 2, 13,
|
|
174
|
+
12, 6, 5, 2, 11, 0, 9, 13, 3, 14, 7, 10, 15, 4, 1, 8,
|
|
175
|
+
]);
|
|
176
|
+
export const ID_GOSTR_3411_94_TEST_PARAM_SET = new Uint8Array([
|
|
177
|
+
4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3,
|
|
178
|
+
14, 11, 4, 12, 6, 13, 15, 10, 2, 3, 8, 1, 0, 7, 5, 9,
|
|
179
|
+
5, 8, 1, 13, 10, 3, 4, 2, 14, 15, 12, 7, 6, 0, 9, 11,
|
|
180
|
+
7, 13, 10, 1, 0, 8, 9, 15, 14, 4, 6, 12, 11, 2, 5, 3,
|
|
181
|
+
6, 12, 7, 1, 5, 15, 13, 8, 4, 10, 9, 14, 0, 3, 11, 2,
|
|
182
|
+
4, 11, 10, 0, 7, 2, 1, 13, 3, 6, 8, 5, 9, 12, 15, 14,
|
|
183
|
+
13, 11, 4, 1, 3, 15, 5, 9, 0, 10, 14, 7, 6, 8, 2, 12,
|
|
184
|
+
1, 15, 13, 0, 5, 7, 10, 4, 9, 2, 3, 14, 6, 11, 8, 12,
|
|
185
|
+
]);
|
|
186
|
+
export const ID_GOSTR_3411_94_CRYPTOPRO_PARAM_SET = new Uint8Array([
|
|
187
|
+
10, 4, 5, 6, 8, 1, 3, 7, 13, 12, 14, 0, 9, 2, 11, 15,
|
|
188
|
+
5, 15, 4, 0, 2, 13, 11, 9, 1, 7, 6, 3, 12, 14, 10, 8,
|
|
189
|
+
7, 15, 12, 14, 9, 4, 1, 0, 3, 11, 5, 2, 6, 10, 8, 13,
|
|
190
|
+
4, 10, 7, 12, 0, 15, 2, 8, 14, 1, 6, 5, 13, 11, 9, 3,
|
|
191
|
+
7, 6, 4, 11, 9, 12, 2, 10, 1, 8, 0, 14, 15, 13, 3, 5,
|
|
192
|
+
7, 6, 2, 4, 13, 9, 15, 0, 10, 1, 5, 11, 8, 14, 12, 3,
|
|
193
|
+
13, 14, 4, 1, 7, 0, 5, 10, 3, 12, 8, 15, 6, 2, 9, 11,
|
|
194
|
+
1, 3, 10, 9, 5, 11, 4, 15, 8, 6, 7, 14, 13, 0, 2, 12,
|
|
195
|
+
]);
|
|
196
|
+
export const EAC_PARAM_SET = new Uint8Array([
|
|
197
|
+
11, 4, 8, 10, 9, 7, 0, 3, 1, 6, 2, 15, 14, 5, 12, 13,
|
|
198
|
+
1, 7, 14, 9, 11, 3, 15, 12, 0, 5, 4, 6, 13, 10, 8, 2,
|
|
199
|
+
7, 3, 1, 9, 2, 4, 13, 15, 8, 10, 12, 6, 5, 0, 11, 14,
|
|
200
|
+
10, 5, 15, 7, 14, 11, 3, 9, 2, 8, 1, 12, 0, 4, 6, 13,
|
|
201
|
+
0, 14, 6, 11, 9, 3, 8, 4, 12, 15, 10, 5, 13, 7, 1, 2,
|
|
202
|
+
9, 2, 11, 12, 0, 4, 5, 6, 3, 15, 13, 8, 1, 7, 14, 10,
|
|
203
|
+
4, 0, 14, 1, 5, 11, 8, 3, 12, 2, 9, 7, 6, 10, 13, 15,
|
|
204
|
+
7, 14, 12, 13, 9, 4, 8, 15, 10, 2, 6, 0, 3, 11, 5, 1,
|
|
205
|
+
]);
|
|
207
206
|
/** Implemented S-Boxes */
|
|
208
207
|
export const magmaSboxes = {
|
|
209
208
|
ID_TC26_GOST_28147_PARAM_Z,
|
package/magma/index.d.ts
CHANGED
|
@@ -1,24 +1,27 @@
|
|
|
1
1
|
import { type TArg, type TRet } from "@noble/curves/utils.js";
|
|
2
2
|
import type { Cipher } from "../types.js";
|
|
3
|
-
/**
|
|
3
|
+
/**
|
|
4
|
+
* **Magma cipher**
|
|
5
|
+
*
|
|
6
|
+
* Described by GOST 28147-89 ([RFC 5830](https://datatracker.ietf.org/doc/html/rfc5830.html)) in 1989 (Uses LE byte order, no S-Box'es defined)
|
|
7
|
+
*
|
|
8
|
+
* Revised by GOST R 34.12-2015 ([RFC 8891](https://datatracker.ietf.org/doc/html/rfc8891.html)) in 2015 (Uses BE byte order and fixed {@link ID_TC26_GOST_28147_PARAM_Z S-Box})
|
|
9
|
+
*/
|
|
4
10
|
export declare class Magma implements Cipher {
|
|
11
|
+
private key;
|
|
5
12
|
private sbox;
|
|
6
13
|
isLegacy: boolean;
|
|
7
14
|
readonly keySize = 32;
|
|
8
15
|
readonly blockSize = 8;
|
|
9
|
-
private key;
|
|
10
16
|
/**
|
|
11
17
|
* Magma (GOST R 34.12-2015 and GOST 28147-89) cipher
|
|
12
18
|
* @param key Encryption key
|
|
13
19
|
* @param sbox S-Box
|
|
14
20
|
* @param isLegacy Use GOST 28147-89 instead of GOST R 34.12-2015?
|
|
15
21
|
*/
|
|
16
|
-
constructor(key: TArg<Uint8Array>, sbox?: TArg<Uint8Array
|
|
17
|
-
private regenerateRoundKeys;
|
|
22
|
+
constructor(key: TArg<Uint8Array>, sbox?: TArg<Uint8Array>, isLegacy?: boolean);
|
|
18
23
|
proceedBlock(block: TArg<Uint8Array>, sequence: number[]): TRet<Uint8Array>;
|
|
19
24
|
encrypt(plaintext: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
20
25
|
decrypt(ciphertext: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
21
|
-
static reverseKey(key: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
22
|
-
static reverseChunks(data: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
23
26
|
}
|
|
24
27
|
export { magmaSboxes, magmaKeySequences } from "./const.js";
|
package/magma/index.js
CHANGED
|
@@ -1,25 +1,38 @@
|
|
|
1
|
-
import { bytesToNumberBE,
|
|
1
|
+
import { bytesToNumberBE, bytesToNumberLE, concatBytes, numberToBytesBE, numberToBytesLE } from "@noble/curves/utils.js";
|
|
2
2
|
import { ID_TC26_GOST_28147_PARAM_Z, magmaKeySequences } from "./const.js";
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
(sbox[
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return ((
|
|
3
|
+
import { createView } from "@noble/hashes/utils.js";
|
|
4
|
+
const BLOCKSIZE = 8;
|
|
5
|
+
const G = (v, sbox) => {
|
|
6
|
+
const t = (sbox[(v & 0x0f)] << 0) |
|
|
7
|
+
(sbox[16 + ((v >> 4) & 0x0f)] << 4) |
|
|
8
|
+
(sbox[32 + ((v >> 8) & 0x0f)] << 8) |
|
|
9
|
+
(sbox[48 + ((v >> 12) & 0x0f)] << 12) |
|
|
10
|
+
(sbox[64 + ((v >> 16) & 0x0f)] << 16) |
|
|
11
|
+
(sbox[80 + ((v >> 20) & 0x0f)] << 20) |
|
|
12
|
+
(sbox[96 + ((v >> 24) & 0x0f)] << 24) |
|
|
13
|
+
(sbox[112 + ((v >> 28) & 0x0f)] << 28);
|
|
14
|
+
return ((t << 11) | (t >>> 21)) >>> 0;
|
|
15
15
|
};
|
|
16
|
-
|
|
16
|
+
const extendKey = (key, sequence, isLegacy) => {
|
|
17
|
+
const view = createView(key);
|
|
18
|
+
const chunks = new Uint32Array(BLOCKSIZE);
|
|
19
|
+
for (let i = 0; i < BLOCKSIZE; i++)
|
|
20
|
+
chunks[i] = view.getUint32(i * 4, isLegacy);
|
|
21
|
+
return new Uint32Array(sequence.map(i => chunks[i]));
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* **Magma cipher**
|
|
25
|
+
*
|
|
26
|
+
* Described by GOST 28147-89 ([RFC 5830](https://datatracker.ietf.org/doc/html/rfc5830.html)) in 1989 (Uses LE byte order, no S-Box'es defined)
|
|
27
|
+
*
|
|
28
|
+
* Revised by GOST R 34.12-2015 ([RFC 8891](https://datatracker.ietf.org/doc/html/rfc8891.html)) in 2015 (Uses BE byte order and fixed {@link ID_TC26_GOST_28147_PARAM_Z S-Box})
|
|
29
|
+
*/
|
|
17
30
|
export class Magma {
|
|
31
|
+
key;
|
|
18
32
|
sbox;
|
|
19
33
|
isLegacy;
|
|
20
|
-
keySize =
|
|
34
|
+
keySize = 32;
|
|
21
35
|
blockSize = BLOCKSIZE;
|
|
22
|
-
key;
|
|
23
36
|
/**
|
|
24
37
|
* Magma (GOST R 34.12-2015 and GOST 28147-89) cipher
|
|
25
38
|
* @param key Encryption key
|
|
@@ -27,55 +40,34 @@ export class Magma {
|
|
|
27
40
|
* @param isLegacy Use GOST 28147-89 instead of GOST R 34.12-2015?
|
|
28
41
|
*/
|
|
29
42
|
constructor(key, sbox = ID_TC26_GOST_28147_PARAM_Z, isLegacy = false) {
|
|
43
|
+
this.key = key;
|
|
30
44
|
this.sbox = sbox;
|
|
31
45
|
this.isLegacy = isLegacy;
|
|
32
46
|
if (key.length !== this.keySize)
|
|
33
47
|
throw new Error("Invalid key length");
|
|
34
|
-
this.key = isLegacy ? Magma.reverseKey(key) : key;
|
|
35
|
-
}
|
|
36
|
-
regenerateRoundKeys(sequence) {
|
|
37
|
-
const keyChunks = [];
|
|
38
|
-
for (let j = 0; j < 8; j++)
|
|
39
|
-
keyChunks.push(Number(bytesToNumberBE(this.key.subarray(j * 4, j * 4 + 4))));
|
|
40
|
-
const roundKeys = new Array(sequence.length);
|
|
41
|
-
for (let i = 0; i < sequence.length; i++)
|
|
42
|
-
roundKeys[i] = keyChunks[sequence[i]];
|
|
43
|
-
return roundKeys;
|
|
44
48
|
}
|
|
45
49
|
proceedBlock(block, sequence) {
|
|
46
50
|
if (block.length !== this.blockSize)
|
|
47
51
|
throw new Error("Invalid block size");
|
|
48
|
-
const roundKeys = this.
|
|
49
|
-
|
|
50
|
-
|
|
52
|
+
const roundKeys = extendKey(this.key, sequence, this.isLegacy);
|
|
53
|
+
const F = block.subarray(0, 4), S = block.subarray(4, 8);
|
|
54
|
+
const bytesToNumber = this.isLegacy ? bytesToNumberLE : bytesToNumberBE;
|
|
55
|
+
let a0 = Number(bytesToNumber(this.isLegacy ? S : F)), a1 = Number(bytesToNumber(this.isLegacy ? F : S));
|
|
51
56
|
for (let i = 0; i < roundKeys.length; i++) {
|
|
52
57
|
const temp = a1;
|
|
53
|
-
a1 = (a0 ^ G(a1
|
|
58
|
+
a1 = (a0 ^ G((a1 + roundKeys[i]) >>> 0, this.sbox)) >>> 0;
|
|
54
59
|
a0 = temp;
|
|
55
60
|
}
|
|
56
|
-
|
|
61
|
+
if (this.isLegacy)
|
|
62
|
+
return concatBytes(numberToBytesLE(a0, 4), numberToBytesLE(a1, 4));
|
|
63
|
+
else
|
|
64
|
+
return concatBytes(numberToBytesBE(a1, 4), numberToBytesBE(a0, 4));
|
|
57
65
|
}
|
|
58
66
|
encrypt(plaintext) {
|
|
59
|
-
if (this.isLegacy)
|
|
60
|
-
return Magma.reverseChunks(this.proceedBlock(Magma.reverseChunks(plaintext), magmaKeySequences.ENCRYPT));
|
|
61
67
|
return this.proceedBlock(plaintext, magmaKeySequences.ENCRYPT);
|
|
62
68
|
}
|
|
63
69
|
decrypt(ciphertext) {
|
|
64
|
-
if (this.isLegacy)
|
|
65
|
-
return Magma.reverseChunks(this.proceedBlock(Magma.reverseChunks(ciphertext), magmaKeySequences.DECRYPT));
|
|
66
70
|
return this.proceedBlock(ciphertext, magmaKeySequences.DECRYPT);
|
|
67
71
|
}
|
|
68
|
-
static reverseKey(key) {
|
|
69
|
-
const result = new Uint8Array(KEYSIZE);
|
|
70
|
-
for (let i = 0; i < BLOCKSIZE; i++)
|
|
71
|
-
result.set(copyBytes(key.subarray(i * 4, i * 4 + 4)).reverse(), i * 4);
|
|
72
|
-
return result;
|
|
73
|
-
}
|
|
74
|
-
static reverseChunks(data) {
|
|
75
|
-
const chunks = [];
|
|
76
|
-
for (let i = 0; i < data.length; i += BLOCKSIZE)
|
|
77
|
-
chunks.push(copyBytes(data.subarray(i, i + BLOCKSIZE)).reverse());
|
|
78
|
-
return concatBytes(...chunks);
|
|
79
|
-
}
|
|
80
72
|
}
|
|
81
73
|
export { magmaSboxes, magmaKeySequences } from "./const.js";
|
package/modes/_keytransform.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type TArg, type TRet } from "@noble/curves/utils.js";
|
|
2
2
|
import type { Cipher } from "../types.js";
|
|
3
|
-
export declare const cp_kek_diversify: (kek: TArg<Uint8Array>, ukm: TArg<Uint8Array>, sbox?: TArg<Uint8Array>
|
|
3
|
+
export declare const cp_kek_diversify: (kek: TArg<Uint8Array>, ukm: TArg<Uint8Array>, sbox?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
4
4
|
export declare const acpkm: (encrypter: (msg: TArg<Uint8Array>) => TRet<Uint8Array>, bs: number) => TRet<Uint8Array>;
|
|
5
5
|
export declare const acpkm_master: (cipher: Cipher, length: number) => TRet<Uint8Array>;
|
package/modes/ctr.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { concatBytes } from "@noble/hashes/utils.js";
|
|
2
2
|
import { getPadLength, xorBytes } from "../utils.js";
|
|
3
|
-
import {
|
|
3
|
+
import { bytesToNumberLE, numberToBytesBE, numberToBytesLE } from "@noble/curves/utils.js";
|
|
4
4
|
import { acpkm } from "./_keytransform.js";
|
|
5
5
|
/**
|
|
6
6
|
* **EN:** Counter (CTR) mode
|
|
@@ -42,19 +42,17 @@ export const ctr = (cipher, iv, isAcpkm, _isAcpkmOmac) => {
|
|
|
42
42
|
export const cnt = (cipher, iv) => {
|
|
43
43
|
if (iv.length !== cipher.blockSize)
|
|
44
44
|
throw new Error("Invalid IV size");
|
|
45
|
-
const C1 = 0x01010104n;
|
|
46
|
-
const C2 = 0x01010101n;
|
|
45
|
+
const C1 = 0x01010104n, C2 = 0x01010101n;
|
|
47
46
|
const encrypter = cipher.encrypt.bind(cipher);
|
|
48
47
|
return {
|
|
49
48
|
crypt: (msg) => {
|
|
50
|
-
const encryptedIv = encrypter(iv)
|
|
51
|
-
let
|
|
52
|
-
let n1 = (bytesToNumberBE(encryptedIv.subarray(4)));
|
|
49
|
+
const encryptedIv = encrypter(iv);
|
|
50
|
+
let n1 = bytesToNumberLE(encryptedIv.subarray(0, 4)), n2 = bytesToNumberLE(encryptedIv.subarray(4));
|
|
53
51
|
const gamma = [];
|
|
54
52
|
for (let i = 0; i < (msg.length + getPadLength(msg.length, cipher.blockSize)); i += cipher.blockSize) {
|
|
55
53
|
n1 = (n1 + C2) % 0x100000000n;
|
|
56
54
|
n2 = (n2 + C1) % 0xffffffffn;
|
|
57
|
-
gamma.push(encrypter(concatBytes(
|
|
55
|
+
gamma.push(encrypter(concatBytes(numberToBytesLE(n1, 4), numberToBytesLE(n2, 4))));
|
|
58
56
|
}
|
|
59
57
|
return xorBytes(concatBytes(...gamma), msg);
|
|
60
58
|
}
|