@li0ard/gost 0.1.5 → 0.1.7
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/gf/gf128.d.ts +2 -0
- package/gf/gf128.js +15 -0
- package/gf/gf256.d.ts +1 -0
- package/gf/gf256.js +62 -0
- package/gf/gf64.d.ts +2 -0
- package/gf/gf64.js +15 -0
- package/gf/index.d.ts +3 -0
- package/gf/index.js +3 -0
- package/gost3410/const.js +20 -20
- package/gost3410/index.d.ts +1 -1
- package/gost3410/index.js +0 -1
- package/gost3410/vko.js +1 -1
- package/gost341194/index.d.ts +3 -3
- package/gost341194/index.js +22 -118
- package/kdf.js +2 -2
- package/kuznyechik/const.js +0 -5
- package/kuznyechik/index.js +40 -139
- package/magma/const.d.ts +38 -38
- package/magma/const.js +190 -190
- package/magma/index.d.ts +1 -2
- package/magma/index.js +16 -23
- package/modes/_keytransform.d.ts +1 -1
- package/modes/mac.js +37 -28
- 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.js +42 -155
package/magma/const.js
CHANGED
|
@@ -1,209 +1,209 @@
|
|
|
1
1
|
/** S-Box from RFC 7836 */
|
|
2
|
-
export const ID_TC26_GOST_28147_PARAM_Z = [
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
];
|
|
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
|
-
];
|
|
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
|
+
]);
|
|
67
67
|
// Converted from tables by script, accuracy not guaranteed
|
|
68
68
|
/** 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
|
-
];
|
|
69
|
+
export const DSSZZI_UA_DKE_2 = new Uint8Array([
|
|
70
|
+
0x0e, 0x09, 0x03, 0x07, 0x0f, 0x04, 0x0c, 0x0b, 0x06, 0x0a, 0x0d, 0x01, 0x00, 0x05, 0x08, 0x02,
|
|
71
|
+
0x0a, 0x0d, 0x0c, 0x07, 0x06, 0x0e, 0x08, 0x01, 0x0f, 0x03, 0x0b, 0x04, 0x00, 0x09, 0x05, 0x02,
|
|
72
|
+
0x04, 0x0b, 0x01, 0x0f, 0x09, 0x02, 0x0e, 0x0c, 0x06, 0x0a, 0x08, 0x07, 0x03, 0x05, 0x00, 0x0d,
|
|
73
|
+
0x04, 0x05, 0x01, 0x0c, 0x07, 0x0e, 0x09, 0x02, 0x0a, 0x0f, 0x0b, 0x0d, 0x00, 0x08, 0x06, 0x03,
|
|
74
|
+
0x0c, 0x0b, 0x03, 0x09, 0x0f, 0x00, 0x04, 0x05, 0x07, 0x02, 0x0e, 0x0d, 0x01, 0x0a, 0x08, 0x06,
|
|
75
|
+
0x08, 0x07, 0x03, 0x0a, 0x09, 0x06, 0x0e, 0x05, 0x0d, 0x00, 0x04, 0x0c, 0x01, 0x02, 0x0f, 0x0b,
|
|
76
|
+
0x0f, 0x00, 0x0e, 0x06, 0x08, 0x0d, 0x05, 0x09, 0x0a, 0x03, 0x01, 0x0c, 0x04, 0x0b, 0x07, 0x02,
|
|
77
|
+
0x04, 0x03, 0x0e, 0x0d, 0x05, 0x00, 0x02, 0x0b, 0x01, 0x0a, 0x07, 0x06, 0x09, 0x0f, 0x08, 0x0c,
|
|
78
|
+
]);
|
|
79
79
|
/** 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
|
-
];
|
|
80
|
+
export const DSSZZI_UA_DKE_3 = new Uint8Array([
|
|
81
|
+
0x0d, 0x09, 0x01, 0x0e, 0x07, 0x02, 0x0c, 0x05, 0x04, 0x0b, 0x06, 0x0f, 0x03, 0x08, 0x0a, 0x00,
|
|
82
|
+
0x07, 0x08, 0x06, 0x0b, 0x00, 0x03, 0x04, 0x0d, 0x09, 0x05, 0x0f, 0x0e, 0x0a, 0x0c, 0x02, 0x01,
|
|
83
|
+
0x0a, 0x05, 0x03, 0x0c, 0x09, 0x08, 0x0d, 0x06, 0x04, 0x0f, 0x0e, 0x00, 0x02, 0x0b, 0x01, 0x07,
|
|
84
|
+
0x0b, 0x0a, 0x0c, 0x01, 0x05, 0x06, 0x09, 0x0e, 0x02, 0x0d, 0x0f, 0x07, 0x00, 0x04, 0x03, 0x08,
|
|
85
|
+
0x05, 0x0b, 0x03, 0x00, 0x0f, 0x09, 0x0e, 0x04, 0x01, 0x0c, 0x08, 0x06, 0x02, 0x0a, 0x07, 0x0d,
|
|
86
|
+
0x04, 0x03, 0x0b, 0x0d, 0x01, 0x0f, 0x08, 0x02, 0x07, 0x0e, 0x0c, 0x09, 0x0a, 0x00, 0x06, 0x05,
|
|
87
|
+
0x03, 0x07, 0x08, 0x0b, 0x01, 0x0e, 0x05, 0x00, 0x0d, 0x04, 0x0c, 0x0a, 0x02, 0x09, 0x0f, 0x06,
|
|
88
|
+
0x06, 0x0d, 0x0c, 0x0a, 0x0b, 0x07, 0x09, 0x03, 0x0f, 0x0e, 0x01, 0x02, 0x00, 0x08, 0x04, 0x05,
|
|
89
|
+
]);
|
|
90
90
|
/** 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
|
-
];
|
|
91
|
+
export const DSSZZI_UA_DKE_4 = new Uint8Array([
|
|
92
|
+
0x09, 0x0c, 0x03, 0x0d, 0x07, 0x06, 0x0e, 0x01, 0x0a, 0x02, 0x00, 0x04, 0x08, 0x0f, 0x05, 0x0b,
|
|
93
|
+
0x0a, 0x05, 0x0b, 0x0e, 0x07, 0x06, 0x00, 0x0c, 0x02, 0x08, 0x0f, 0x04, 0x0d, 0x03, 0x09, 0x01,
|
|
94
|
+
0x04, 0x0c, 0x03, 0x00, 0x0d, 0x02, 0x0e, 0x0b, 0x07, 0x0f, 0x05, 0x09, 0x01, 0x08, 0x0a, 0x06,
|
|
95
|
+
0x03, 0x09, 0x04, 0x05, 0x0e, 0x07, 0x08, 0x06, 0x0d, 0x00, 0x02, 0x0f, 0x0b, 0x0c, 0x0a, 0x01,
|
|
96
|
+
0x02, 0x09, 0x0c, 0x0f, 0x0d, 0x0b, 0x04, 0x01, 0x07, 0x05, 0x03, 0x0e, 0x06, 0x08, 0x0a, 0x00,
|
|
97
|
+
0x0e, 0x05, 0x0d, 0x0b, 0x01, 0x09, 0x04, 0x02, 0x0f, 0x08, 0x07, 0x00, 0x03, 0x0c, 0x0a, 0x06,
|
|
98
|
+
0x0e, 0x06, 0x05, 0x0a, 0x09, 0x0d, 0x04, 0x08, 0x0b, 0x0c, 0x00, 0x03, 0x07, 0x01, 0x0f, 0x02,
|
|
99
|
+
0x01, 0x09, 0x0c, 0x0b, 0x07, 0x06, 0x08, 0x03, 0x02, 0x0f, 0x0e, 0x00, 0x05, 0x0a, 0x04, 0x0d,
|
|
100
|
+
]);
|
|
101
101
|
/** 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
|
-
];
|
|
102
|
+
export const DSSZZI_UA_DKE_5 = new Uint8Array([
|
|
103
|
+
0x03, 0x04, 0x0d, 0x08, 0x0c, 0x07, 0x0a, 0x02, 0x00, 0x0e, 0x09, 0x0f, 0x0b, 0x01, 0x05, 0x06,
|
|
104
|
+
0x0c, 0x07, 0x06, 0x09, 0x03, 0x08, 0x0b, 0x05, 0x0f, 0x0a, 0x00, 0x0d, 0x04, 0x02, 0x01, 0x0e,
|
|
105
|
+
0x0e, 0x04, 0x08, 0x07, 0x0b, 0x03, 0x0a, 0x0c, 0x01, 0x02, 0x06, 0x09, 0x0d, 0x0f, 0x00, 0x05,
|
|
106
|
+
0x03, 0x09, 0x06, 0x0d, 0x08, 0x0f, 0x0a, 0x02, 0x07, 0x0e, 0x0c, 0x00, 0x0b, 0x04, 0x01, 0x05,
|
|
107
|
+
0x05, 0x0c, 0x0a, 0x07, 0x02, 0x01, 0x0f, 0x0d, 0x0e, 0x03, 0x0b, 0x04, 0x00, 0x08, 0x09, 0x06,
|
|
108
|
+
0x01, 0x08, 0x0b, 0x0e, 0x07, 0x04, 0x0a, 0x00, 0x0c, 0x03, 0x05, 0x0d, 0x09, 0x0f, 0x06, 0x02,
|
|
109
|
+
0x09, 0x0b, 0x0a, 0x0d, 0x05, 0x0e, 0x02, 0x03, 0x00, 0x06, 0x04, 0x0c, 0x0f, 0x01, 0x07, 0x08,
|
|
110
|
+
0x0e, 0x09, 0x01, 0x08, 0x05, 0x0f, 0x0b, 0x00, 0x06, 0x02, 0x0c, 0x07, 0x0a, 0x04, 0x0d, 0x03,
|
|
111
|
+
]);
|
|
112
112
|
/** 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
|
-
];
|
|
113
|
+
export const DSSZZI_UA_DKE_6 = new Uint8Array([
|
|
114
|
+
0x0f, 0x0c, 0x09, 0x06, 0x0e, 0x02, 0x01, 0x0b, 0x00, 0x0d, 0x04, 0x0a, 0x07, 0x08, 0x03, 0x05,
|
|
115
|
+
0x0e, 0x0c, 0x05, 0x00, 0x07, 0x04, 0x0a, 0x03, 0x02, 0x06, 0x01, 0x0d, 0x09, 0x0b, 0x0f, 0x08,
|
|
116
|
+
0x05, 0x06, 0x0d, 0x09, 0x0b, 0x0e, 0x0a, 0x03, 0x0f, 0x02, 0x08, 0x01, 0x04, 0x00, 0x07, 0x0c,
|
|
117
|
+
0x01, 0x0f, 0x07, 0x04, 0x02, 0x0e, 0x0c, 0x03, 0x06, 0x0b, 0x09, 0x08, 0x00, 0x05, 0x0a, 0x0d,
|
|
118
|
+
0x0f, 0x09, 0x0e, 0x06, 0x0d, 0x01, 0x05, 0x08, 0x04, 0x02, 0x03, 0x0c, 0x0a, 0x0b, 0x00, 0x07,
|
|
119
|
+
0x0b, 0x00, 0x0d, 0x07, 0x0c, 0x0e, 0x01, 0x04, 0x02, 0x03, 0x06, 0x08, 0x0a, 0x05, 0x0f, 0x09,
|
|
120
|
+
0x07, 0x0e, 0x0f, 0x08, 0x0d, 0x00, 0x0b, 0x03, 0x0a, 0x01, 0x04, 0x02, 0x09, 0x0c, 0x06, 0x05,
|
|
121
|
+
0x01, 0x05, 0x0e, 0x0b, 0x02, 0x0c, 0x03, 0x08, 0x0a, 0x00, 0x09, 0x07, 0x0f, 0x06, 0x04, 0x0d,
|
|
122
|
+
]);
|
|
123
123
|
/** 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
|
-
];
|
|
124
|
+
export const DSSZZI_UA_DKE_7 = new Uint8Array([
|
|
125
|
+
0x0f, 0x0d, 0x0a, 0x05, 0x0c, 0x00, 0x01, 0x06, 0x09, 0x02, 0x0e, 0x07, 0x03, 0x0b, 0x04, 0x08,
|
|
126
|
+
0x02, 0x05, 0x0a, 0x00, 0x06, 0x09, 0x01, 0x0f, 0x0d, 0x04, 0x07, 0x0e, 0x0b, 0x03, 0x08, 0x0c,
|
|
127
|
+
0x03, 0x0e, 0x04, 0x0b, 0x05, 0x09, 0x01, 0x02, 0x0f, 0x06, 0x08, 0x0d, 0x07, 0x00, 0x0a, 0x0c,
|
|
128
|
+
0x04, 0x0a, 0x0b, 0x09, 0x0f, 0x02, 0x0e, 0x05, 0x0d, 0x01, 0x03, 0x06, 0x00, 0x07, 0x0c, 0x08,
|
|
129
|
+
0x0f, 0x06, 0x05, 0x08, 0x09, 0x07, 0x0c, 0x0b, 0x00, 0x0a, 0x03, 0x01, 0x02, 0x04, 0x0d, 0x0e,
|
|
130
|
+
0x0c, 0x0b, 0x0f, 0x04, 0x05, 0x01, 0x0e, 0x09, 0x00, 0x08, 0x0d, 0x02, 0x0a, 0x07, 0x03, 0x06,
|
|
131
|
+
0x0d, 0x02, 0x04, 0x08, 0x0b, 0x0c, 0x01, 0x03, 0x0a, 0x05, 0x09, 0x0e, 0x07, 0x0f, 0x00, 0x06,
|
|
132
|
+
0x01, 0x05, 0x00, 0x0f, 0x06, 0x0a, 0x03, 0x0e, 0x07, 0x02, 0x0c, 0x0d, 0x0b, 0x08, 0x09, 0x04,
|
|
133
|
+
]);
|
|
134
134
|
/** 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
|
-
];
|
|
135
|
+
export const DSSZZI_UA_DKE_8 = new Uint8Array([
|
|
136
|
+
0x0e, 0x04, 0x0b, 0x02, 0x08, 0x07, 0x05, 0x0c, 0x09, 0x0d, 0x00, 0x03, 0x01, 0x0f, 0x06, 0x0a,
|
|
137
|
+
0x03, 0x0e, 0x0c, 0x0a, 0x06, 0x02, 0x0d, 0x01, 0x09, 0x08, 0x07, 0x04, 0x00, 0x0f, 0x05, 0x0b,
|
|
138
|
+
0x05, 0x02, 0x08, 0x07, 0x01, 0x0f, 0x0e, 0x06, 0x04, 0x0d, 0x0b, 0x00, 0x0a, 0x03, 0x0c, 0x09,
|
|
139
|
+
0x0c, 0x0a, 0x07, 0x0d, 0x0e, 0x03, 0x00, 0x02, 0x09, 0x05, 0x01, 0x06, 0x0b, 0x04, 0x0f, 0x08,
|
|
140
|
+
0x06, 0x03, 0x0f, 0x07, 0x00, 0x09, 0x0a, 0x08, 0x0b, 0x0c, 0x04, 0x01, 0x05, 0x02, 0x0d, 0x0e,
|
|
141
|
+
0x06, 0x0d, 0x0f, 0x01, 0x05, 0x03, 0x08, 0x00, 0x0b, 0x0a, 0x0e, 0x04, 0x09, 0x0c, 0x02, 0x07,
|
|
142
|
+
0x02, 0x0f, 0x0c, 0x05, 0x0b, 0x01, 0x03, 0x0e, 0x00, 0x06, 0x0d, 0x0a, 0x07, 0x09, 0x04, 0x08,
|
|
143
|
+
0x03, 0x00, 0x05, 0x0c, 0x08, 0x0f, 0x0d, 0x0e, 0x0b, 0x06, 0x02, 0x09, 0x07, 0x01, 0x04, 0x0a,
|
|
144
|
+
]);
|
|
145
145
|
/** 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
|
-
];
|
|
146
|
+
export const DSSZZI_UA_DKE_9 = new Uint8Array([
|
|
147
|
+
0x09, 0x00, 0x0b, 0x0c, 0x02, 0x04, 0x03, 0x0f, 0x0d, 0x06, 0x0e, 0x01, 0x0a, 0x07, 0x05, 0x08,
|
|
148
|
+
0x03, 0x05, 0x00, 0x0f, 0x08, 0x07, 0x0e, 0x0c, 0x0d, 0x0a, 0x01, 0x06, 0x0b, 0x02, 0x04, 0x09,
|
|
149
|
+
0x08, 0x04, 0x05, 0x0a, 0x0e, 0x0b, 0x0d, 0x06, 0x0c, 0x0f, 0x07, 0x09, 0x03, 0x01, 0x02, 0x00,
|
|
150
|
+
0x05, 0x04, 0x0f, 0x00, 0x0c, 0x0b, 0x0a, 0x09, 0x01, 0x0e, 0x08, 0x06, 0x03, 0x02, 0x0d, 0x07,
|
|
151
|
+
0x07, 0x0c, 0x03, 0x00, 0x06, 0x08, 0x0e, 0x0b, 0x01, 0x0f, 0x0d, 0x0a, 0x09, 0x05, 0x02, 0x04,
|
|
152
|
+
0x07, 0x04, 0x03, 0x0b, 0x06, 0x0a, 0x08, 0x01, 0x09, 0x0c, 0x0e, 0x0d, 0x00, 0x0f, 0x02, 0x05,
|
|
153
|
+
0x07, 0x0e, 0x09, 0x0f, 0x01, 0x04, 0x08, 0x03, 0x0b, 0x0d, 0x00, 0x02, 0x06, 0x0a, 0x05, 0x0c,
|
|
154
|
+
0x0e, 0x02, 0x08, 0x0f, 0x03, 0x00, 0x07, 0x0c, 0x0b, 0x0d, 0x01, 0x05, 0x06, 0x04, 0x09, 0x0a,
|
|
155
|
+
]);
|
|
156
156
|
/** 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
|
-
];
|
|
157
|
+
export const DSSZZI_UA_DKE_10 = new Uint8Array([
|
|
158
|
+
0x08, 0x04, 0x06, 0x09, 0x0b, 0x0c, 0x01, 0x02, 0x03, 0x07, 0x0e, 0x00, 0x0d, 0x0a, 0x0f, 0x05,
|
|
159
|
+
0x07, 0x0d, 0x01, 0x08, 0x0a, 0x0e, 0x04, 0x0f, 0x09, 0x00, 0x06, 0x03, 0x02, 0x0c, 0x0b, 0x05,
|
|
160
|
+
0x0c, 0x08, 0x0d, 0x01, 0x0a, 0x02, 0x09, 0x06, 0x03, 0x04, 0x0e, 0x07, 0x05, 0x0f, 0x00, 0x0b,
|
|
161
|
+
0x02, 0x0b, 0x03, 0x04, 0x0c, 0x07, 0x09, 0x0d, 0x0f, 0x08, 0x05, 0x00, 0x01, 0x0e, 0x0a, 0x06,
|
|
162
|
+
0x08, 0x03, 0x0d, 0x0a, 0x0e, 0x0f, 0x05, 0x01, 0x04, 0x07, 0x0b, 0x0c, 0x02, 0x00, 0x06, 0x09,
|
|
163
|
+
0x04, 0x0c, 0x09, 0x0b, 0x0e, 0x0a, 0x07, 0x06, 0x03, 0x05, 0x00, 0x0f, 0x01, 0x02, 0x08, 0x0d,
|
|
164
|
+
0x05, 0x08, 0x0e, 0x07, 0x03, 0x00, 0x01, 0x0d, 0x0a, 0x06, 0x09, 0x02, 0x0f, 0x0b, 0x0c, 0x04,
|
|
165
|
+
0x0a, 0x03, 0x05, 0x09, 0x00, 0x0d, 0x07, 0x08, 0x0c, 0x04, 0x01, 0x06, 0x0b, 0x0f, 0x02, 0x0e,
|
|
166
|
+
]);
|
|
167
|
+
export const ID_GOST_28147_89_TEST_PARAM_SET = new Uint8Array([
|
|
168
|
+
4, 2, 15, 5, 9, 1, 0, 8, 14, 3, 11, 12, 13, 7, 10, 6,
|
|
169
|
+
12, 9, 15, 14, 8, 1, 3, 10, 2, 7, 4, 13, 6, 0, 11, 5,
|
|
170
|
+
13, 8, 14, 12, 7, 3, 9, 10, 1, 5, 2, 4, 6, 15, 0, 11,
|
|
171
|
+
14, 9, 11, 2, 5, 15, 7, 1, 0, 13, 12, 6, 10, 4, 3, 8,
|
|
172
|
+
3, 14, 5, 9, 6, 8, 0, 13, 10, 11, 7, 12, 2, 1, 15, 4,
|
|
173
|
+
8, 15, 6, 11, 1, 9, 12, 5, 13, 3, 7, 10, 0, 14, 2, 4,
|
|
174
|
+
9, 11, 12, 0, 3, 6, 7, 5, 4, 8, 14, 15, 1, 10, 2, 13,
|
|
175
|
+
12, 6, 5, 2, 11, 0, 9, 13, 3, 14, 7, 10, 15, 4, 1, 8,
|
|
176
|
+
]);
|
|
177
|
+
export const ID_GOSTR_3411_94_TEST_PARAM_SET = new Uint8Array([
|
|
178
|
+
4, 10, 9, 2, 13, 8, 0, 14, 6, 11, 1, 12, 7, 15, 5, 3,
|
|
179
|
+
14, 11, 4, 12, 6, 13, 15, 10, 2, 3, 8, 1, 0, 7, 5, 9,
|
|
180
|
+
5, 8, 1, 13, 10, 3, 4, 2, 14, 15, 12, 7, 6, 0, 9, 11,
|
|
181
|
+
7, 13, 10, 1, 0, 8, 9, 15, 14, 4, 6, 12, 11, 2, 5, 3,
|
|
182
|
+
6, 12, 7, 1, 5, 15, 13, 8, 4, 10, 9, 14, 0, 3, 11, 2,
|
|
183
|
+
4, 11, 10, 0, 7, 2, 1, 13, 3, 6, 8, 5, 9, 12, 15, 14,
|
|
184
|
+
13, 11, 4, 1, 3, 15, 5, 9, 0, 10, 14, 7, 6, 8, 2, 12,
|
|
185
|
+
1, 15, 13, 0, 5, 7, 10, 4, 9, 2, 3, 14, 6, 11, 8, 12,
|
|
186
|
+
]);
|
|
187
|
+
export const ID_GOSTR_3411_94_CRYPTOPRO_PARAM_SET = new Uint8Array([
|
|
188
|
+
10, 4, 5, 6, 8, 1, 3, 7, 13, 12, 14, 0, 9, 2, 11, 15,
|
|
189
|
+
5, 15, 4, 0, 2, 13, 11, 9, 1, 7, 6, 3, 12, 14, 10, 8,
|
|
190
|
+
7, 15, 12, 14, 9, 4, 1, 0, 3, 11, 5, 2, 6, 10, 8, 13,
|
|
191
|
+
4, 10, 7, 12, 0, 15, 2, 8, 14, 1, 6, 5, 13, 11, 9, 3,
|
|
192
|
+
7, 6, 4, 11, 9, 12, 2, 10, 1, 8, 0, 14, 15, 13, 3, 5,
|
|
193
|
+
7, 6, 2, 4, 13, 9, 15, 0, 10, 1, 5, 11, 8, 14, 12, 3,
|
|
194
|
+
13, 14, 4, 1, 7, 0, 5, 10, 3, 12, 8, 15, 6, 2, 9, 11,
|
|
195
|
+
1, 3, 10, 9, 5, 11, 4, 15, 8, 6, 7, 14, 13, 0, 2, 12,
|
|
196
|
+
]);
|
|
197
|
+
export const EAC_PARAM_SET = new Uint8Array([
|
|
198
|
+
11, 4, 8, 10, 9, 7, 0, 3, 1, 6, 2, 15, 14, 5, 12, 13,
|
|
199
|
+
1, 7, 14, 9, 11, 3, 15, 12, 0, 5, 4, 6, 13, 10, 8, 2,
|
|
200
|
+
7, 3, 1, 9, 2, 4, 13, 15, 8, 10, 12, 6, 5, 0, 11, 14,
|
|
201
|
+
10, 5, 15, 7, 14, 11, 3, 9, 2, 8, 1, 12, 0, 4, 6, 13,
|
|
202
|
+
0, 14, 6, 11, 9, 3, 8, 4, 12, 15, 10, 5, 13, 7, 1, 2,
|
|
203
|
+
9, 2, 11, 12, 0, 4, 5, 6, 3, 15, 13, 8, 1, 7, 14, 10,
|
|
204
|
+
4, 0, 14, 1, 5, 11, 8, 3, 12, 2, 9, 7, 6, 10, 13, 15,
|
|
205
|
+
7, 14, 12, 13, 9, 4, 8, 15, 10, 2, 6, 0, 3, 11, 5, 1,
|
|
206
|
+
]);
|
|
207
207
|
/** Implemented S-Boxes */
|
|
208
208
|
export const magmaSboxes = {
|
|
209
209
|
ID_TC26_GOST_28147_PARAM_Z,
|
package/magma/index.d.ts
CHANGED
|
@@ -13,8 +13,7 @@ export declare class Magma implements Cipher {
|
|
|
13
13
|
* @param sbox S-Box
|
|
14
14
|
* @param isLegacy Use GOST 28147-89 instead of GOST R 34.12-2015?
|
|
15
15
|
*/
|
|
16
|
-
constructor(key: TArg<Uint8Array>, sbox?: TArg<Uint8Array
|
|
17
|
-
private regenerateRoundKeys;
|
|
16
|
+
constructor(key: TArg<Uint8Array>, sbox?: TArg<Uint8Array>, isLegacy?: boolean);
|
|
18
17
|
proceedBlock(block: TArg<Uint8Array>, sequence: number[]): TRet<Uint8Array>;
|
|
19
18
|
encrypt(plaintext: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
20
19
|
decrypt(ciphertext: TArg<Uint8Array>): TRet<Uint8Array>;
|
package/magma/index.js
CHANGED
|
@@ -1,23 +1,25 @@
|
|
|
1
1
|
import { bytesToNumberBE, concatBytes, copyBytes, numberToBytesBE } from "@noble/curves/utils.js";
|
|
2
2
|
import { ID_TC26_GOST_28147_PARAM_Z, magmaKeySequences } from "./const.js";
|
|
3
3
|
const BLOCKSIZE = 8, KEYSIZE = 32;
|
|
4
|
-
const T = (value, sbox) =>
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
result |= sbox[5][(value >> 20) & 0x0f] << 20;
|
|
13
|
-
result |= sbox[6][(value >> 24) & 0x0f] << 24;
|
|
14
|
-
result |= sbox[7][(value >> 28) & 0x0f] << 28;
|
|
15
|
-
return result >>> 0;
|
|
16
|
-
};
|
|
4
|
+
const T = (value, sbox) => ((sbox[((value >> 0) & 0x0f)] << 0) |
|
|
5
|
+
(sbox[16 + ((value >> 4) & 0x0f)] << 4) |
|
|
6
|
+
(sbox[32 + ((value >> 8) & 0x0f)] << 8) |
|
|
7
|
+
(sbox[48 + ((value >> 12) & 0x0f)] << 12) |
|
|
8
|
+
(sbox[64 + ((value >> 16) & 0x0f)] << 16) |
|
|
9
|
+
(sbox[80 + ((value >> 20) & 0x0f)] << 20) |
|
|
10
|
+
(sbox[96 + ((value >> 24) & 0x0f)] << 24) |
|
|
11
|
+
(sbox[112 + ((value >> 28) & 0x0f)] << 28)) >>> 0;
|
|
17
12
|
const G = (a, k, sbox) => {
|
|
18
13
|
const substituted = T((a + k) >>> 0, sbox);
|
|
19
14
|
return ((substituted << 11) | (substituted >>> 21)) >>> 0;
|
|
20
15
|
};
|
|
16
|
+
const extendKey = (key, sequence) => {
|
|
17
|
+
const view = new DataView(key.buffer, key.byteOffset, key.byteLength);
|
|
18
|
+
const chunks = new Uint32Array(BLOCKSIZE);
|
|
19
|
+
for (let i = 0; i < BLOCKSIZE; i++)
|
|
20
|
+
chunks[i] = view.getUint32(i * 4);
|
|
21
|
+
return new Uint32Array(sequence.map(i => chunks[i]));
|
|
22
|
+
};
|
|
21
23
|
/** Magma (GOST R 34.12-2015 and GOST 28147-89) cipher */
|
|
22
24
|
export class Magma {
|
|
23
25
|
sbox;
|
|
@@ -38,19 +40,10 @@ export class Magma {
|
|
|
38
40
|
throw new Error("Invalid key length");
|
|
39
41
|
this.key = isLegacy ? Magma.reverseKey(key) : key;
|
|
40
42
|
}
|
|
41
|
-
regenerateRoundKeys(sequence) {
|
|
42
|
-
const keyChunks = [];
|
|
43
|
-
for (let j = 0; j < 8; j++)
|
|
44
|
-
keyChunks.push(Number(bytesToNumberBE(this.key.subarray(j * 4, j * 4 + 4))));
|
|
45
|
-
const roundKeys = new Array(sequence.length);
|
|
46
|
-
for (let i = 0; i < sequence.length; i++)
|
|
47
|
-
roundKeys[i] = keyChunks[sequence[i]];
|
|
48
|
-
return roundKeys;
|
|
49
|
-
}
|
|
50
43
|
proceedBlock(block, sequence) {
|
|
51
44
|
if (block.length !== this.blockSize)
|
|
52
45
|
throw new Error("Invalid block size");
|
|
53
|
-
const roundKeys = this.
|
|
46
|
+
const roundKeys = extendKey(this.key, sequence);
|
|
54
47
|
let a0 = Number(bytesToNumberBE(block.subarray(0, 4)));
|
|
55
48
|
let a1 = Number(bytesToNumberBE(block.subarray(4, 8)));
|
|
56
49
|
for (let i = 0; i < roundKeys.length; i++) {
|
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/mac.js
CHANGED
|
@@ -5,6 +5,15 @@ import { magmaKeySequences, Magma } from "../magma/index.js";
|
|
|
5
5
|
import { acpkm_master } from "./_keytransform.js";
|
|
6
6
|
const Rb64 = 0b11011;
|
|
7
7
|
const Rb128 = 0b10000111;
|
|
8
|
+
const shift1 = (src, dst) => {
|
|
9
|
+
let b = 0;
|
|
10
|
+
for (let i = src.length - 1; i >= 0; i--) {
|
|
11
|
+
const bb = src[i] >> 7;
|
|
12
|
+
dst[i] = src[i] << 1 | b;
|
|
13
|
+
b = bb;
|
|
14
|
+
}
|
|
15
|
+
return b;
|
|
16
|
+
};
|
|
8
17
|
/**
|
|
9
18
|
* **EN:** Message Authentication Code (MAC) mode
|
|
10
19
|
*
|
|
@@ -12,36 +21,36 @@ const Rb128 = 0b10000111;
|
|
|
12
21
|
*/
|
|
13
22
|
export const mac = (cipher) => {
|
|
14
23
|
const encrypter = cipher.encrypt.bind(cipher);
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
const Rb = cipher.blockSize === 16 ? Rb128 : Rb64;
|
|
18
|
-
const l = encrypter(new Uint8Array(cipher.blockSize));
|
|
19
|
-
let k1;
|
|
20
|
-
if ((l[0] & 0x80) !== 0)
|
|
21
|
-
k1 = macShift(l, Rb);
|
|
22
|
-
else
|
|
23
|
-
k1 = macShift(l);
|
|
24
|
-
let k2;
|
|
25
|
-
if ((k1[0] & 0x80) !== 0)
|
|
26
|
-
k2 = macShift(k1, Rb);
|
|
27
|
-
else
|
|
28
|
-
k2 = macShift(k1);
|
|
29
|
-
return [k1, k2];
|
|
30
|
-
};
|
|
24
|
+
const Rb = cipher.blockSize === 16 ? Rb128 : Rb64;
|
|
25
|
+
const L = encrypter(new Uint8Array(cipher.blockSize));
|
|
31
26
|
return {
|
|
32
27
|
compute: (msg) => {
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
if (
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
28
|
+
const k1 = new Uint8Array(cipher.blockSize);
|
|
29
|
+
const msb = shift1(L, k1);
|
|
30
|
+
if (msb)
|
|
31
|
+
k1[cipher.blockSize - 1] ^= Rb;
|
|
32
|
+
const k2 = new Uint8Array(cipher.blockSize);
|
|
33
|
+
const msb2 = shift1(k1, k2);
|
|
34
|
+
if (msb2)
|
|
35
|
+
k2[cipher.blockSize - 1] ^= Rb;
|
|
36
|
+
const n = Math.ceil(msg.length / cipher.blockSize) || 1;
|
|
37
|
+
const lastBlockComplete = msg.length > 0 && msg.length % cipher.blockSize === 0;
|
|
38
|
+
let buf = new Uint8Array(cipher.blockSize);
|
|
39
|
+
for (let i = 0; i < n - 1; i++) {
|
|
40
|
+
const m = msg.subarray(i * cipher.blockSize, (i + 1) * cipher.blockSize);
|
|
41
|
+
buf = encrypter(xorBytes(buf, m));
|
|
42
|
+
}
|
|
43
|
+
let lastBlock;
|
|
44
|
+
if (lastBlockComplete && msg.length > 0)
|
|
45
|
+
lastBlock = xorBytes(msg.subarray((n - 1) * cipher.blockSize, n * cipher.blockSize), k1);
|
|
46
|
+
else {
|
|
47
|
+
const padded = new Uint8Array(cipher.blockSize);
|
|
48
|
+
const remaining = msg.length - (n - 1) * cipher.blockSize;
|
|
49
|
+
padded.set(msg.subarray((n - 1) * cipher.blockSize));
|
|
50
|
+
padded[remaining] = 0x80;
|
|
51
|
+
lastBlock = xorBytes(padded, k2);
|
|
52
|
+
}
|
|
53
|
+
return encrypter(xorBytes(buf, lastBlock));
|
|
45
54
|
}
|
|
46
55
|
};
|
|
47
56
|
};
|
package/modes/mgm.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { concatBytes } from "@noble/hashes/utils.js";
|
|
2
2
|
import { bytesToNumberBE, equalBytes, numberToBytesBE } from "@noble/curves/utils.js";
|
|
3
3
|
import { pad1, xorBytes } from "../utils.js";
|
|
4
|
+
import { gf64Multiply, gf128Multiply } from "../gf/index.js";
|
|
4
5
|
/**
|
|
5
6
|
* **EN:** Multilinear Galois (MGM) mode (AEAD)
|
|
6
7
|
*
|
|
@@ -15,29 +16,13 @@ export const mgm = (cipher, nonce, tagSize = cipher.blockSize) => {
|
|
|
15
16
|
throw new Error("Invalid tagSize");
|
|
16
17
|
const encrypter = cipher.encrypt.bind(cipher);
|
|
17
18
|
const maxSize = (1n << BigInt(cipher.blockSize * 4)) - 1n;
|
|
18
|
-
const r = (cipher.blockSize == 8 ? 0x1B : 0x87);
|
|
19
19
|
const validateSizes = (plaintext, additional) => {
|
|
20
20
|
if (plaintext.length == 0 && additional.length == 0)
|
|
21
21
|
throw new Error("At least one of plaintext or additional_data required");
|
|
22
22
|
if ((plaintext.length + additional.length) > maxSize)
|
|
23
23
|
throw new Error("plaintext+additional_data are too big");
|
|
24
24
|
};
|
|
25
|
-
const mul = (
|
|
26
|
-
let x = bytesToNumberBE(a);
|
|
27
|
-
let y = bytesToNumberBE(b);
|
|
28
|
-
let z = 0n;
|
|
29
|
-
const max_bit = 1n << (BigInt(cipher.blockSize) * 8n - 1n);
|
|
30
|
-
while (y > 0n) {
|
|
31
|
-
if ((y & 1n) == 1n)
|
|
32
|
-
z ^= x;
|
|
33
|
-
if ((x & max_bit) > 0n)
|
|
34
|
-
x = ((x ^ max_bit) << 1n) ^ BigInt(r);
|
|
35
|
-
else
|
|
36
|
-
x <<= 1n;
|
|
37
|
-
y >>= 1n;
|
|
38
|
-
}
|
|
39
|
-
return numberToBytesBE(z, cipher.blockSize);
|
|
40
|
-
};
|
|
25
|
+
const mul = (cipher.blockSize == 8 ? gf64Multiply : gf128Multiply);
|
|
41
26
|
const crypt = (icn, data) => {
|
|
42
27
|
icn[0] &= 0x7F;
|
|
43
28
|
let enc = encrypter(icn);
|