@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/modes/wrap.d.ts CHANGED
@@ -11,4 +11,4 @@ export declare const kexp15: (cipherEnc: Cipher, cipherMac: Cipher, iv: TArg<Uin
11
11
  *
12
12
  * **RU:** Режим обёртки ключей шифрования согласно ГОСТ 28147-89
13
13
  */
14
- export declare const kwp: (kek: TArg<Uint8Array>, isCryptoPro?: boolean, sbox?: TArg<Uint8Array>[]) => WrapModeMagma;
14
+ export declare const kwp: (kek: TArg<Uint8Array>, isCryptoPro?: boolean, sbox?: TArg<Uint8Array>) => WrapModeMagma;
package/modes/wrap.js CHANGED
@@ -2,7 +2,7 @@ import { concatBytes } from "@noble/hashes/utils.js";
2
2
  import { mac as _mac, mac_legacy } from "./mac.js";
3
3
  import { ctr } from "./ctr.js";
4
4
  import { equalBytes } from "@noble/curves/utils.js";
5
- import { ID_GOST_28147_89_CRYPTO_PRO_A_PARAM_SET } from "../magma/const";
5
+ import { ID_GOST_28147_89_CRYPTO_PRO_A_PARAM_SET } from "../magma/const.js";
6
6
  import { Magma } from "../magma/index.js";
7
7
  import { ecb } from "./ecb.js";
8
8
  import { cp_kek_diversify } from "./_keytransform.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@li0ard/gost",
3
- "version": "0.1.5",
3
+ "version": "0.1.7",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "type": "module",
package/streebog/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { concatBytes, createHasher } from "@noble/hashes/utils.js";
1
+ import { concatBytes, copyBytes, createHasher } from "@noble/hashes/utils.js";
2
2
  import { A, C, TAU } from "./const.js";
3
3
  import { PI } from "../kuznyechik/const.js";
4
4
  import { pad1, xorBytes } from "../utils.js";
@@ -20,144 +20,32 @@ const add512 = (a, b) => {
20
20
  }
21
21
  return c;
22
22
  };
23
- const S = (input) => {
24
- const result = new Uint8Array(BLOCKSIZE);
25
- //for (let i = 0; i < BLOCKSIZE; i++) result[i] = PI[input[i]];
26
- result[0] = PI[input[0]];
27
- result[1] = PI[input[1]];
28
- result[2] = PI[input[2]];
29
- result[3] = PI[input[3]];
30
- result[4] = PI[input[4]];
31
- result[5] = PI[input[5]];
32
- result[6] = PI[input[6]];
33
- result[7] = PI[input[7]];
34
- result[8] = PI[input[8]];
35
- result[9] = PI[input[9]];
36
- result[10] = PI[input[10]];
37
- result[11] = PI[input[11]];
38
- result[12] = PI[input[12]];
39
- result[13] = PI[input[13]];
40
- result[14] = PI[input[14]];
41
- result[15] = PI[input[15]];
42
- result[16] = PI[input[16]];
43
- result[17] = PI[input[17]];
44
- result[18] = PI[input[18]];
45
- result[19] = PI[input[19]];
46
- result[20] = PI[input[20]];
47
- result[21] = PI[input[21]];
48
- result[22] = PI[input[22]];
49
- result[23] = PI[input[23]];
50
- result[24] = PI[input[24]];
51
- result[25] = PI[input[25]];
52
- result[26] = PI[input[26]];
53
- result[27] = PI[input[27]];
54
- result[28] = PI[input[28]];
55
- result[29] = PI[input[29]];
56
- result[30] = PI[input[30]];
57
- result[31] = PI[input[31]];
58
- result[32] = PI[input[32]];
59
- result[33] = PI[input[33]];
60
- result[34] = PI[input[34]];
61
- result[35] = PI[input[35]];
62
- result[36] = PI[input[36]];
63
- result[37] = PI[input[37]];
64
- result[38] = PI[input[38]];
65
- result[39] = PI[input[39]];
66
- result[40] = PI[input[40]];
67
- result[41] = PI[input[41]];
68
- result[42] = PI[input[42]];
69
- result[43] = PI[input[43]];
70
- result[44] = PI[input[44]];
71
- result[45] = PI[input[45]];
72
- result[46] = PI[input[46]];
73
- result[47] = PI[input[47]];
74
- result[48] = PI[input[48]];
75
- result[49] = PI[input[49]];
76
- result[50] = PI[input[50]];
77
- result[51] = PI[input[51]];
78
- result[52] = PI[input[52]];
79
- result[53] = PI[input[53]];
80
- result[54] = PI[input[54]];
81
- result[55] = PI[input[55]];
82
- result[56] = PI[input[56]];
83
- result[57] = PI[input[57]];
84
- result[58] = PI[input[58]];
85
- result[59] = PI[input[59]];
86
- result[60] = PI[input[60]];
87
- result[61] = PI[input[61]];
88
- result[62] = PI[input[62]];
89
- result[63] = PI[input[63]];
90
- return result;
91
- };
92
- const P = (input) => {
93
- const result = new Uint8Array(BLOCKSIZE);
94
- //for (let i = 0; i < BLOCKSIZE; i++) result[i] = input[TAU[i]];
95
- result[0] = input[TAU[0]];
96
- result[1] = input[TAU[1]];
97
- result[2] = input[TAU[2]];
98
- result[3] = input[TAU[3]];
99
- result[4] = input[TAU[4]];
100
- result[5] = input[TAU[5]];
101
- result[6] = input[TAU[6]];
102
- result[7] = input[TAU[7]];
103
- result[8] = input[TAU[8]];
104
- result[9] = input[TAU[9]];
105
- result[10] = input[TAU[10]];
106
- result[11] = input[TAU[11]];
107
- result[12] = input[TAU[12]];
108
- result[13] = input[TAU[13]];
109
- result[14] = input[TAU[14]];
110
- result[15] = input[TAU[15]];
111
- result[16] = input[TAU[16]];
112
- result[17] = input[TAU[17]];
113
- result[18] = input[TAU[18]];
114
- result[19] = input[TAU[19]];
115
- result[20] = input[TAU[20]];
116
- result[21] = input[TAU[21]];
117
- result[22] = input[TAU[22]];
118
- result[23] = input[TAU[23]];
119
- result[24] = input[TAU[24]];
120
- result[25] = input[TAU[25]];
121
- result[26] = input[TAU[26]];
122
- result[27] = input[TAU[27]];
123
- result[28] = input[TAU[28]];
124
- result[29] = input[TAU[29]];
125
- result[30] = input[TAU[30]];
126
- result[31] = input[TAU[31]];
127
- result[32] = input[TAU[32]];
128
- result[33] = input[TAU[33]];
129
- result[34] = input[TAU[34]];
130
- result[35] = input[TAU[35]];
131
- result[36] = input[TAU[36]];
132
- result[37] = input[TAU[37]];
133
- result[38] = input[TAU[38]];
134
- result[39] = input[TAU[39]];
135
- result[40] = input[TAU[40]];
136
- result[41] = input[TAU[41]];
137
- result[42] = input[TAU[42]];
138
- result[43] = input[TAU[43]];
139
- result[44] = input[TAU[44]];
140
- result[45] = input[TAU[45]];
141
- result[46] = input[TAU[46]];
142
- result[47] = input[TAU[47]];
143
- result[48] = input[TAU[48]];
144
- result[49] = input[TAU[49]];
145
- result[50] = input[TAU[50]];
146
- result[51] = input[TAU[51]];
147
- result[52] = input[TAU[52]];
148
- result[53] = input[TAU[53]];
149
- result[54] = input[TAU[54]];
150
- result[55] = input[TAU[55]];
151
- result[56] = input[TAU[56]];
152
- result[57] = input[TAU[57]];
153
- result[58] = input[TAU[58]];
154
- result[59] = input[TAU[59]];
155
- result[60] = input[TAU[60]];
156
- result[61] = input[TAU[61]];
157
- result[62] = input[TAU[62]];
158
- result[63] = input[TAU[63]];
159
- return result;
160
- };
23
+ const S = (input) => new Uint8Array([
24
+ PI[input[0]], PI[input[1]], PI[input[2]], PI[input[3]], PI[input[4]], PI[input[5]],
25
+ PI[input[6]], PI[input[7]], PI[input[8]], PI[input[9]], PI[input[10]], PI[input[11]],
26
+ PI[input[12]], PI[input[13]], PI[input[14]], PI[input[15]], PI[input[16]], PI[input[17]],
27
+ PI[input[18]], PI[input[19]], PI[input[20]], PI[input[21]], PI[input[22]], PI[input[23]],
28
+ PI[input[24]], PI[input[25]], PI[input[26]], PI[input[27]], PI[input[28]], PI[input[29]],
29
+ PI[input[30]], PI[input[31]], PI[input[32]], PI[input[33]], PI[input[34]], PI[input[35]],
30
+ PI[input[36]], PI[input[37]], PI[input[38]], PI[input[39]], PI[input[40]], PI[input[41]],
31
+ PI[input[42]], PI[input[43]], PI[input[44]], PI[input[45]], PI[input[46]], PI[input[47]],
32
+ PI[input[48]], PI[input[49]], PI[input[50]], PI[input[51]], PI[input[52]], PI[input[53]],
33
+ PI[input[54]], PI[input[55]], PI[input[56]], PI[input[57]], PI[input[58]], PI[input[59]],
34
+ PI[input[60]], PI[input[61]], PI[input[62]], PI[input[63]]
35
+ ]);
36
+ const P = (input) => new Uint8Array([
37
+ input[TAU[0]], input[TAU[1]], input[TAU[2]], input[TAU[3]], input[TAU[4]], input[TAU[5]],
38
+ input[TAU[6]], input[TAU[7]], input[TAU[8]], input[TAU[9]], input[TAU[10]], input[TAU[11]],
39
+ input[TAU[12]], input[TAU[13]], input[TAU[14]], input[TAU[15]], input[TAU[16]], input[TAU[17]],
40
+ input[TAU[18]], input[TAU[19]], input[TAU[20]], input[TAU[21]], input[TAU[22]], input[TAU[23]],
41
+ input[TAU[24]], input[TAU[25]], input[TAU[26]], input[TAU[27]], input[TAU[28]], input[TAU[29]],
42
+ input[TAU[30]], input[TAU[31]], input[TAU[32]], input[TAU[33]], input[TAU[34]], input[TAU[35]],
43
+ input[TAU[36]], input[TAU[37]], input[TAU[38]], input[TAU[39]], input[TAU[40]], input[TAU[41]],
44
+ input[TAU[42]], input[TAU[43]], input[TAU[44]], input[TAU[45]], input[TAU[46]], input[TAU[47]],
45
+ input[TAU[48]], input[TAU[49]], input[TAU[50]], input[TAU[51]], input[TAU[52]], input[TAU[53]],
46
+ input[TAU[54]], input[TAU[55]], input[TAU[56]], input[TAU[57]], input[TAU[58]], input[TAU[59]],
47
+ input[TAU[60]], input[TAU[61]], input[TAU[62]], input[TAU[63]]
48
+ ]);
161
49
  const L = (input) => {
162
50
  const result = new Uint8Array(BLOCKSIZE);
163
51
  for (let i = 0; i < 8; i++) {
@@ -181,8 +69,7 @@ const E = (block, keys) => {
181
69
  // block will be mutated
182
70
  let c = xorBytes(block, keys);
183
71
  /*for (let i = 0; i < 12; i++) {
184
- block = LPS(xorBytes(block, C[i]));
185
- c = xorBytes(LPS(c), block);
72
+ block = LPS(xorBytes(block, C[i])); c = xorBytes(LPS(c), block);
186
73
  }*/
187
74
  block = LPS(xorBytes(block, C[0]));
188
75
  c = xorBytes(LPS(c), block);
@@ -235,29 +122,29 @@ class Streebog {
235
122
  return buffer;
236
123
  }
237
124
  digestInto(buf) {
238
- const message = this.buffer.slice().reverse();
239
- let n = new Uint8Array(BLOCKSIZE);
240
- let sigma = new Uint8Array(BLOCKSIZE);
241
- let hash = new Uint8Array(BLOCKSIZE).fill(this.is512 ? 0 : 1);
125
+ const message = copyBytes(this.buffer).reverse();
126
+ let n = new Uint8Array(this.blockLen);
127
+ let sigma = new Uint8Array(this.blockLen);
128
+ let hash = new Uint8Array(this.blockLen).fill(this.is512 ? 0 : 1);
242
129
  let blocks = 1;
243
- for (let i = message.length; i >= BLOCKSIZE; i -= BLOCKSIZE) {
244
- const pos = message.length - blocks * BLOCKSIZE;
245
- hash = G(n, hash, message.subarray(pos, pos + BLOCKSIZE));
130
+ for (let i = message.length; i >= this.blockLen; i -= this.blockLen) {
131
+ const pos = message.length - blocks * this.blockLen;
132
+ hash = G(n, hash, message.subarray(pos, pos + this.blockLen));
246
133
  n = add512(n, _0020);
247
- sigma = add512(sigma, message.subarray(pos, pos + BLOCKSIZE));
134
+ sigma = add512(sigma, message.subarray(pos, pos + this.blockLen));
248
135
  blocks++;
249
136
  }
250
- let paddedMsg = new Uint8Array(BLOCKSIZE);
137
+ let paddedMsg = new Uint8Array(this.blockLen);
251
138
  const msg = message.subarray(0, message.length - (blocks - 1) * 64);
252
- if (msg.length < BLOCKSIZE) {
253
- paddedMsg = pad1(paddedMsg, BLOCKSIZE);
254
- paddedMsg[BLOCKSIZE - msg.length - 1] = 0x01;
139
+ if (msg.length < this.blockLen) {
140
+ paddedMsg = pad1(paddedMsg, this.blockLen);
141
+ paddedMsg[this.blockLen - msg.length - 1] = 0x01;
255
142
  for (let i = 0; i < msg.length; i++)
256
- paddedMsg[BLOCKSIZE - msg.length + i] = msg[i];
143
+ paddedMsg[this.blockLen - msg.length + i] = msg[i];
257
144
  }
258
145
  hash = G(_0, G(_0, G(n, hash, paddedMsg), add512(n, numberToBytesBE(msg.length * 8, 4))), add512(sigma, paddedMsg));
259
146
  if (this.is512)
260
- buf.set(hash.slice().reverse());
147
+ buf.set(copyBytes(hash).reverse());
261
148
  else
262
149
  buf.set(hash.slice(0, 32).reverse());
263
150
  this.destroy();