@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.
@@ -1,152 +1,55 @@
1
1
  import { copyBytes } from "@noble/curves/utils.js";
2
2
  import { ITER, L, PI, PI_REV } from "./const.js";
3
3
  import { xorBytes } from "../utils.js";
4
- const BLOCKSIZE = 16, KEYSIZE = 32;
5
- const S = (input, pi = PI) => {
6
- const result = new Uint8Array(BLOCKSIZE);
7
- //for(let i = 0; i < BLOCKSIZE; i++) result[i] = pi[input[i]];
8
- result[0] = pi[input[0]];
9
- result[1] = pi[input[1]];
10
- result[2] = pi[input[2]];
11
- result[3] = pi[input[3]];
12
- result[4] = pi[input[4]];
13
- result[5] = pi[input[5]];
14
- result[6] = pi[input[6]];
15
- result[7] = pi[input[7]];
16
- result[8] = pi[input[8]];
17
- result[9] = pi[input[9]];
18
- result[10] = pi[input[10]];
19
- result[11] = pi[input[11]];
20
- result[12] = pi[input[12]];
21
- result[13] = pi[input[13]];
22
- result[14] = pi[input[14]];
23
- result[15] = pi[input[15]];
24
- return result;
25
- };
26
- const gfMultiply = (a, b) => {
27
- let result = 0;
28
- let high_bit;
29
- for (let i = 0; i < 8; i++) {
30
- if ((b & 0b00000001) === 0b00000001)
31
- result ^= a;
32
- high_bit = a & 0b10000000;
33
- a <<= 1;
34
- if (high_bit == 0b10000000)
35
- a ^= 0b11000011;
36
- b >>= 1;
37
- }
38
- return result & 0xFF;
39
- };
40
- const R = (input) => {
41
- const result = new Uint8Array(BLOCKSIZE);
42
- result.set(input.slice(0, 15), 1);
43
- result[0] = input[15];
44
- //let temp = 0;
45
- //for (let i = 0; i < BLOCKSIZE; i++) temp ^= gfMultiply(result[i], L[i]);
46
- let temp = gfMultiply(result[0], L[0]);
47
- temp ^= gfMultiply(result[1], L[1]);
48
- temp ^= gfMultiply(result[2], L[2]);
49
- temp ^= gfMultiply(result[3], L[3]);
50
- temp ^= gfMultiply(result[4], L[4]);
51
- temp ^= gfMultiply(result[5], L[5]);
52
- temp ^= gfMultiply(result[6], L[6]);
53
- temp ^= gfMultiply(result[7], L[7]);
54
- temp ^= gfMultiply(result[8], L[8]);
55
- temp ^= gfMultiply(result[9], L[9]);
56
- temp ^= gfMultiply(result[10], L[10]);
57
- temp ^= gfMultiply(result[11], L[11]);
58
- temp ^= gfMultiply(result[12], L[12]);
59
- temp ^= gfMultiply(result[13], L[13]);
60
- temp ^= gfMultiply(result[14], L[14]);
61
- temp ^= gfMultiply(result[15], L[15]);
62
- result[0] = temp;
63
- return result;
64
- };
65
- const Rr = (input) => {
66
- const result = new Uint8Array(BLOCKSIZE);
67
- //let temp = 0;
68
- //for (let i = 0; i < BLOCKSIZE; i++) temp ^= gfMultiply(input[i], L[i]);
69
- let temp = gfMultiply(input[0], L[0]);
70
- temp ^= gfMultiply(input[1], L[1]);
71
- temp ^= gfMultiply(input[2], L[2]);
72
- temp ^= gfMultiply(input[3], L[3]);
73
- temp ^= gfMultiply(input[4], L[4]);
74
- temp ^= gfMultiply(input[5], L[5]);
75
- temp ^= gfMultiply(input[6], L[6]);
76
- temp ^= gfMultiply(input[7], L[7]);
77
- temp ^= gfMultiply(input[8], L[8]);
78
- temp ^= gfMultiply(input[9], L[9]);
79
- temp ^= gfMultiply(input[10], L[10]);
80
- temp ^= gfMultiply(input[11], L[11]);
81
- temp ^= gfMultiply(input[12], L[12]);
82
- temp ^= gfMultiply(input[13], L[13]);
83
- temp ^= gfMultiply(input[14], L[14]);
84
- temp ^= gfMultiply(input[15], L[15]);
85
- result.set(input.slice(1));
86
- result[15] = temp;
87
- return result;
88
- };
89
- const LL = (input) => {
90
- //let result = copyBytes(input);
91
- //for(let i = 0; i < BLOCKSIZE; i++) result = R(result);
92
- let result = R(copyBytes(input));
93
- result = R(result);
94
- result = R(result);
95
- result = R(result);
96
- result = R(result);
97
- result = R(result);
98
- result = R(result);
99
- result = R(result);
100
- result = R(result);
101
- result = R(result);
102
- result = R(result);
103
- result = R(result);
104
- result = R(result);
105
- result = R(result);
106
- result = R(result);
107
- result = R(result);
108
- return result;
109
- };
110
- const LLr = (input) => {
111
- //let result = copyBytes(input);
112
- //for(let i = 0; i < BLOCKSIZE; i++) result = Rr(result);
113
- let result = Rr(copyBytes(input));
114
- result = Rr(result);
115
- result = Rr(result);
116
- result = Rr(result);
117
- result = Rr(result);
118
- result = Rr(result);
119
- result = Rr(result);
120
- result = Rr(result);
121
- result = Rr(result);
122
- result = Rr(result);
123
- result = Rr(result);
124
- result = Rr(result);
125
- result = Rr(result);
126
- result = Rr(result);
127
- result = Rr(result);
128
- result = Rr(result);
129
- return result;
130
- };
4
+ import { gf256Multiply } from "../gf/index.js";
5
+ const S = (input, pi = PI) => new Uint8Array([
6
+ pi[input[0]], pi[input[1]], pi[input[2]], pi[input[3]],
7
+ pi[input[4]], pi[input[5]], pi[input[6]], pi[input[7]],
8
+ pi[input[8]], pi[input[9]], pi[input[10]], pi[input[11]],
9
+ pi[input[12]], pi[input[13]], pi[input[14]], pi[input[15]]
10
+ ]);
11
+ const R = (input) => new Uint8Array([
12
+ gf256Multiply(input[15], L[0]) ^ gf256Multiply(input[0], L[1]) ^
13
+ gf256Multiply(input[1], L[2]) ^ gf256Multiply(input[2], L[3]) ^
14
+ gf256Multiply(input[3], L[4]) ^ gf256Multiply(input[4], L[5]) ^
15
+ gf256Multiply(input[5], L[6]) ^ gf256Multiply(input[6], L[7]) ^
16
+ gf256Multiply(input[7], L[8]) ^ gf256Multiply(input[8], L[9]) ^
17
+ gf256Multiply(input[9], L[10]) ^ gf256Multiply(input[10], L[11]) ^
18
+ gf256Multiply(input[11], L[12]) ^ gf256Multiply(input[12], L[13]) ^
19
+ gf256Multiply(input[13], L[14]) ^ gf256Multiply(input[14], L[15]),
20
+ ...input.subarray(0, 15)
21
+ ]);
22
+ const Rr = (input) => new Uint8Array([
23
+ ...input.subarray(1, 16),
24
+ gf256Multiply(input[0], L[0]) ^ gf256Multiply(input[1], L[1]) ^
25
+ gf256Multiply(input[2], L[2]) ^ gf256Multiply(input[3], L[3]) ^
26
+ gf256Multiply(input[4], L[4]) ^ gf256Multiply(input[5], L[5]) ^
27
+ gf256Multiply(input[6], L[6]) ^ gf256Multiply(input[7], L[7]) ^
28
+ gf256Multiply(input[8], L[8]) ^ gf256Multiply(input[9], L[9]) ^
29
+ gf256Multiply(input[10], L[10]) ^ gf256Multiply(input[11], L[11]) ^
30
+ gf256Multiply(input[12], L[12]) ^ gf256Multiply(input[13], L[13]) ^
31
+ gf256Multiply(input[14], L[14]) ^ gf256Multiply(input[15], L[15])
32
+ ]);
33
+ // Call `R` 16x times
34
+ const LL = (input) => R(R(R(R(R(R(R(R(R(R(R(R(R(R(R(R(input))))))))))))))));
35
+ // Call `Rr` 16x times
36
+ const LLr = (input) => Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(input))))))))))))))));
131
37
  const LLS = (block) => LL(S(block));
132
38
  const SLLr = (block) => S(LLr(block), PI_REV);
133
- const F = (in_key1, in_key2, iter_constant) => xorBytes(LLS(xorBytes(in_key1, iter_constant)), in_key2);
39
+ const F = (inKey, inKey2, iter) => xorBytes(LLS(xorBytes(inKey, iter)), inKey2);
134
40
  /** Kuznyechik (GOST R 34.12-2015) cipher */
135
41
  export class Kuznyechik {
136
- keySize = KEYSIZE;
137
- blockSize = BLOCKSIZE;
42
+ keySize = 32;
43
+ blockSize = 16;
138
44
  roundKeys;
139
45
  /** Kuznyechik (GOST R 34.12-2015) cipher */
140
46
  constructor(key) {
141
47
  if (key.length !== this.keySize)
142
48
  throw new Error("Invalid key length");
143
- const roundKeys = Array(10).fill(null).map(() => new Uint8Array(this.blockSize));
49
+ const roundKeys = Array(10);
144
50
  roundKeys[0] = key.slice(0, this.blockSize);
145
51
  roundKeys[1] = key.slice(this.blockSize);
146
- let temp1 = copyBytes(roundKeys[0]);
147
- let temp2 = copyBytes(roundKeys[1]);
148
- let temp3 = new Uint8Array(16);
149
- let temp4 = new Uint8Array(16);
52
+ let temp1 = copyBytes(roundKeys[0]), temp2 = copyBytes(roundKeys[1]), temp3 = new Uint8Array(16), temp4 = new Uint8Array(16);
150
53
  for (let i = 0; i < 4; i++) {
151
54
  const baseIndex = i * 8;
152
55
  temp3 = F(temp1, temp2, ITER[baseIndex]);
@@ -184,8 +87,7 @@ export class Kuznyechik {
184
87
  currentBlock = LLS(xorBytes(this.roundKeys[6], currentBlock));
185
88
  currentBlock = LLS(xorBytes(this.roundKeys[7], currentBlock));
186
89
  currentBlock = LLS(xorBytes(this.roundKeys[8], currentBlock));
187
- currentBlock = xorBytes(this.roundKeys[9], currentBlock);
188
- return currentBlock;
90
+ return xorBytes(this.roundKeys[9], currentBlock);
189
91
  }
190
92
  decrypt(ciphertext) {
191
93
  if (ciphertext.length !== this.blockSize)
@@ -201,7 +103,6 @@ export class Kuznyechik {
201
103
  currentBlock = xorBytes(this.roundKeys[3], SLLr(currentBlock));
202
104
  currentBlock = xorBytes(this.roundKeys[2], SLLr(currentBlock));
203
105
  currentBlock = xorBytes(this.roundKeys[1], SLLr(currentBlock));
204
- currentBlock = xorBytes(this.roundKeys[0], SLLr(currentBlock));
205
- return currentBlock;
106
+ return xorBytes(this.roundKeys[0], SLLr(currentBlock));
206
107
  }
207
108
  }
package/magma/const.d.ts CHANGED
@@ -1,58 +1,58 @@
1
1
  /** S-Box from RFC 7836 */
2
- export declare const ID_TC26_GOST_28147_PARAM_Z: Uint8Array<ArrayBuffer>[];
2
+ export declare const ID_TC26_GOST_28147_PARAM_Z: Uint8Array<ArrayBuffer>;
3
3
  /** S-Box from RFC 4357 aka `CryptoPro Paramset A` */
4
- export declare const ID_GOST_28147_89_CRYPTO_PRO_A_PARAM_SET: Uint8Array<ArrayBuffer>[];
4
+ export declare const ID_GOST_28147_89_CRYPTO_PRO_A_PARAM_SET: Uint8Array<ArrayBuffer>;
5
5
  /** S-Box from RFC 4357 aka `CryptoPro Paramset B` */
6
- export declare const ID_GOST_28147_89_CRYPTO_PRO_B_PARAM_SET: Uint8Array<ArrayBuffer>[];
6
+ export declare const ID_GOST_28147_89_CRYPTO_PRO_B_PARAM_SET: Uint8Array<ArrayBuffer>;
7
7
  /** S-Box from RFC 4357 aka `CryptoPro Paramset C` */
8
- export declare const ID_GOST_28147_89_CRYPTO_PRO_C_PARAM_SET: Uint8Array<ArrayBuffer>[];
8
+ export declare const ID_GOST_28147_89_CRYPTO_PRO_C_PARAM_SET: Uint8Array<ArrayBuffer>;
9
9
  /** S-Box from RFC 4357 aka `CryptoPro Paramset D` */
10
- export declare const ID_GOST_28147_89_CRYPTO_PRO_D_PARAM_SET: Uint8Array<ArrayBuffer>[];
10
+ export declare const ID_GOST_28147_89_CRYPTO_PRO_D_PARAM_SET: Uint8Array<ArrayBuffer>;
11
11
  /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
12
- export declare const DSSZZI_UA_DKE_1: Uint8Array<ArrayBuffer>[];
12
+ export declare const DSSZZI_UA_DKE_1: Uint8Array<ArrayBuffer>;
13
13
  /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
14
- export declare const DSSZZI_UA_DKE_2: Uint8Array<ArrayBuffer>[];
14
+ export declare const DSSZZI_UA_DKE_2: Uint8Array<ArrayBuffer>;
15
15
  /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
16
- export declare const DSSZZI_UA_DKE_3: Uint8Array<ArrayBuffer>[];
16
+ export declare const DSSZZI_UA_DKE_3: Uint8Array<ArrayBuffer>;
17
17
  /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
18
- export declare const DSSZZI_UA_DKE_4: Uint8Array<ArrayBuffer>[];
18
+ export declare const DSSZZI_UA_DKE_4: Uint8Array<ArrayBuffer>;
19
19
  /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
20
- export declare const DSSZZI_UA_DKE_5: Uint8Array<ArrayBuffer>[];
20
+ export declare const DSSZZI_UA_DKE_5: Uint8Array<ArrayBuffer>;
21
21
  /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
22
- export declare const DSSZZI_UA_DKE_6: Uint8Array<ArrayBuffer>[];
22
+ export declare const DSSZZI_UA_DKE_6: Uint8Array<ArrayBuffer>;
23
23
  /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
24
- export declare const DSSZZI_UA_DKE_7: Uint8Array<ArrayBuffer>[];
24
+ export declare const DSSZZI_UA_DKE_7: Uint8Array<ArrayBuffer>;
25
25
  /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
26
- export declare const DSSZZI_UA_DKE_8: Uint8Array<ArrayBuffer>[];
26
+ export declare const DSSZZI_UA_DKE_8: Uint8Array<ArrayBuffer>;
27
27
  /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
28
- export declare const DSSZZI_UA_DKE_9: Uint8Array<ArrayBuffer>[];
28
+ export declare const DSSZZI_UA_DKE_9: Uint8Array<ArrayBuffer>;
29
29
  /** S-Box from Instruction no. 114 by State Special Communications Service of Ukraine */
30
- export declare const DSSZZI_UA_DKE_10: Uint8Array<ArrayBuffer>[];
31
- export declare const ID_GOST_28147_89_TEST_PARAM_SET: Uint8Array<ArrayBuffer>[];
32
- export declare const ID_GOSTR_3411_94_TEST_PARAM_SET: Uint8Array<ArrayBuffer>[];
33
- export declare const ID_GOSTR_3411_94_CRYPTOPRO_PARAM_SET: Uint8Array<ArrayBuffer>[];
34
- export declare const EAC_PARAM_SET: Uint8Array<ArrayBuffer>[];
30
+ export declare const DSSZZI_UA_DKE_10: Uint8Array<ArrayBuffer>;
31
+ export declare const ID_GOST_28147_89_TEST_PARAM_SET: Uint8Array<ArrayBuffer>;
32
+ export declare const ID_GOSTR_3411_94_TEST_PARAM_SET: Uint8Array<ArrayBuffer>;
33
+ export declare const ID_GOSTR_3411_94_CRYPTOPRO_PARAM_SET: Uint8Array<ArrayBuffer>;
34
+ export declare const EAC_PARAM_SET: Uint8Array<ArrayBuffer>;
35
35
  /** Implemented S-Boxes */
36
36
  export declare const magmaSboxes: {
37
- ID_TC26_GOST_28147_PARAM_Z: Uint8Array<ArrayBuffer>[];
38
- ID_GOST_28147_89_CRYPTO_PRO_A_PARAM_SET: Uint8Array<ArrayBuffer>[];
39
- ID_GOST_28147_89_CRYPTO_PRO_B_PARAM_SET: Uint8Array<ArrayBuffer>[];
40
- ID_GOST_28147_89_CRYPTO_PRO_C_PARAM_SET: Uint8Array<ArrayBuffer>[];
41
- ID_GOST_28147_89_CRYPTO_PRO_D_PARAM_SET: Uint8Array<ArrayBuffer>[];
42
- ID_GOST_28147_89_TEST_PARAM_SET: Uint8Array<ArrayBuffer>[];
43
- ID_GOSTR_3411_94_TEST_PARAM_SET: Uint8Array<ArrayBuffer>[];
44
- ID_GOSTR_3411_94_CRYPTOPRO_PARAM_SET: Uint8Array<ArrayBuffer>[];
45
- EAC_PARAM_SET: Uint8Array<ArrayBuffer>[];
46
- DSSZZI_UA_DKE_1: Uint8Array<ArrayBuffer>[];
47
- DSSZZI_UA_DKE_2: Uint8Array<ArrayBuffer>[];
48
- DSSZZI_UA_DKE_3: Uint8Array<ArrayBuffer>[];
49
- DSSZZI_UA_DKE_4: Uint8Array<ArrayBuffer>[];
50
- DSSZZI_UA_DKE_5: Uint8Array<ArrayBuffer>[];
51
- DSSZZI_UA_DKE_6: Uint8Array<ArrayBuffer>[];
52
- DSSZZI_UA_DKE_7: Uint8Array<ArrayBuffer>[];
53
- DSSZZI_UA_DKE_8: Uint8Array<ArrayBuffer>[];
54
- DSSZZI_UA_DKE_9: Uint8Array<ArrayBuffer>[];
55
- DSSZZI_UA_DKE_10: Uint8Array<ArrayBuffer>[];
37
+ ID_TC26_GOST_28147_PARAM_Z: Uint8Array<ArrayBuffer>;
38
+ ID_GOST_28147_89_CRYPTO_PRO_A_PARAM_SET: Uint8Array<ArrayBuffer>;
39
+ ID_GOST_28147_89_CRYPTO_PRO_B_PARAM_SET: Uint8Array<ArrayBuffer>;
40
+ ID_GOST_28147_89_CRYPTO_PRO_C_PARAM_SET: Uint8Array<ArrayBuffer>;
41
+ ID_GOST_28147_89_CRYPTO_PRO_D_PARAM_SET: Uint8Array<ArrayBuffer>;
42
+ ID_GOST_28147_89_TEST_PARAM_SET: Uint8Array<ArrayBuffer>;
43
+ ID_GOSTR_3411_94_TEST_PARAM_SET: Uint8Array<ArrayBuffer>;
44
+ ID_GOSTR_3411_94_CRYPTOPRO_PARAM_SET: Uint8Array<ArrayBuffer>;
45
+ EAC_PARAM_SET: Uint8Array<ArrayBuffer>;
46
+ DSSZZI_UA_DKE_1: Uint8Array<ArrayBuffer>;
47
+ DSSZZI_UA_DKE_2: Uint8Array<ArrayBuffer>;
48
+ DSSZZI_UA_DKE_3: Uint8Array<ArrayBuffer>;
49
+ DSSZZI_UA_DKE_4: Uint8Array<ArrayBuffer>;
50
+ DSSZZI_UA_DKE_5: Uint8Array<ArrayBuffer>;
51
+ DSSZZI_UA_DKE_6: Uint8Array<ArrayBuffer>;
52
+ DSSZZI_UA_DKE_7: Uint8Array<ArrayBuffer>;
53
+ DSSZZI_UA_DKE_8: Uint8Array<ArrayBuffer>;
54
+ DSSZZI_UA_DKE_9: Uint8Array<ArrayBuffer>;
55
+ DSSZZI_UA_DKE_10: Uint8Array<ArrayBuffer>;
56
56
  };
57
57
  /** Sequences of `K_i` S-Box applying */
58
58
  export declare const magmaKeySequences: {