@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 ADDED
@@ -0,0 +1,2 @@
1
+ import { type TArg, type TRet } from "@noble/curves/utils.js";
2
+ export declare const gf128Multiply: (a: TArg<Uint8Array>, b: TArg<Uint8Array>) => TRet<Uint8Array>;
package/gf/gf128.js ADDED
@@ -0,0 +1,15 @@
1
+ import { bytesToNumberBE, numberToBytesBE } from "@noble/curves/utils.js";
2
+ export const gf128Multiply = (a, b) => {
3
+ let x = bytesToNumberBE(a), y = bytesToNumberBE(b), z = 0n;
4
+ const max_bit = 1n << 127n;
5
+ while (y > 0n) {
6
+ if ((y & 1n) == 1n)
7
+ z ^= x;
8
+ if ((x & max_bit) > 0n)
9
+ x = ((x ^ max_bit) << 1n) ^ 0x87n;
10
+ else
11
+ x <<= 1n;
12
+ y >>= 1n;
13
+ }
14
+ return numberToBytesBE(z, 16);
15
+ };
package/gf/gf256.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const gf256Multiply: (a: number, b: number) => number;
package/gf/gf256.js ADDED
@@ -0,0 +1,62 @@
1
+ /*// Precompute GF(256) multiply table
2
+ const gf256Multiply_slow = (a: number, b: number): number => {
3
+ let result = 0, high_bit: number;
4
+ for(let _ = 0; _ < 8; _++) {
5
+ if((b & 1) === 1) result ^= a;
6
+ high_bit = a & 0x80;
7
+ a <<= 1;
8
+ if(high_bit == 0x80) a ^= 0xC3;
9
+ b >>= 1;
10
+ }
11
+
12
+ return result & 0xFF;
13
+ }
14
+
15
+ const gen_mul_tables = () => {
16
+ let c = 1;
17
+
18
+ for (let i = 0; i < 256; i++) {
19
+ gf_multtable_log[c] = i;
20
+ gf_multtable_exp[i] = c;
21
+ c = gfMultiply_slow(c, 3);
22
+ }
23
+ }*/
24
+ const gf_multtable_exp = new Uint8Array([
25
+ 0x01, 0x03, 0x05, 0x0F, 0x11, 0x33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 0
62
+ : gf_multtable_exp[(gf_multtable_log[a] + gf_multtable_log[b]) % 255];
package/gf/gf64.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { type TArg, type TRet } from "@noble/curves/utils.js";
2
+ export declare const gf64Multiply: (a: TArg<Uint8Array>, b: TArg<Uint8Array>) => TRet<Uint8Array>;
package/gf/gf64.js ADDED
@@ -0,0 +1,15 @@
1
+ import { bytesToNumberBE, numberToBytesBE } from "@noble/curves/utils.js";
2
+ export const gf64Multiply = (a, b) => {
3
+ let x = bytesToNumberBE(a), y = bytesToNumberBE(b), z = 0n;
4
+ const max_bit = 1n << 63n;
5
+ while (y > 0n) {
6
+ if ((y & 1n) == 1n)
7
+ z ^= x;
8
+ if ((x & max_bit) > 0n)
9
+ x = ((x ^ max_bit) << 1n) ^ 0x1bn;
10
+ else
11
+ x <<= 1n;
12
+ y >>= 1n;
13
+ }
14
+ return numberToBytesBE(z, 8);
15
+ };
package/gf/index.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { gf64Multiply } from "./gf64.js";
2
+ export { gf128Multiply } from "./gf128.js";
3
+ export { gf256Multiply } from "./gf256.js";
package/gf/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export { gf64Multiply } from "./gf64.js";
2
+ export { gf128Multiply } from "./gf128.js";
3
+ export { gf256Multiply } from "./gf256.js";
package/gost3410/const.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /** GOST R 34.10-2001 CryptoCom param set */
2
- export const ID_GOSTR3410_2001_PARAM_SET_CC = ({
2
+ export const ID_GOSTR3410_2001_PARAM_SET_CC = {
3
3
  p: 0xc0000000000000000000000000000000000000000000000000000000000003c7n,
4
4
  n: 0x5fffffffffffffffffffffffffffffff606117a2f4bde428b7458a54b6e87b85n,
5
5
  a: 0xc0000000000000000000000000000000000000000000000000000000000003c4n,
@@ -9,9 +9,9 @@ export const ID_GOSTR3410_2001_PARAM_SET_CC = ({
9
9
  h: 1n,
10
10
  length: 32,
11
11
  oids: ["1.2.643.2.9.1.8.1"]
12
- });
12
+ };
13
13
  /** GOST R 34.10-2001 test param set */
14
- export const ID_GOSTR3410_2001_TEST_PARAM_SET = ({
14
+ export const ID_GOSTR3410_2001_TEST_PARAM_SET = {
15
15
  p: 0x8000000000000000000000000000000000000000000000000000000000000431n,
16
16
  n: 0x8000000000000000000000000000000150fe8a1892976154c59cfc193accf5b3n,
17
17
  a: 7n,
@@ -21,9 +21,9 @@ export const ID_GOSTR3410_2001_TEST_PARAM_SET = ({
21
21
  h: 1n,
22
22
  length: 32,
23
23
  oids: ["1.2.643.2.2.35.0"]
24
- });
24
+ };
25
25
  /** GOST R 34.10-2012 256 bit `A` param set */
26
- export const ID_GOSTR3410_2012_256_PARAM_SET_A = ({
26
+ export const ID_GOSTR3410_2012_256_PARAM_SET_A = {
27
27
  p: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd97n,
28
28
  n: 0x400000000000000000000000000000000fd8cddfc87b6635c115af556c360c67n,
29
29
  a: 0xc2173f1513981673af4892c23035a27ce25e2013bf95aa33b22c656f277e7335n,
@@ -36,9 +36,9 @@ export const ID_GOSTR3410_2012_256_PARAM_SET_A = ({
36
36
  length: 32,
37
37
  st: [0x7e7e82520f9f015faa1d0f18c14ab9fb35188275da3fd94206b74f34a48e0ecdn, 0x0100fe73f595ff158e974b44d478d9588744fe5c192ac47ea63075dce7a14aaan],
38
38
  oids: ["1.2.643.7.1.2.1.1.1"]
39
- });
39
+ };
40
40
  /** GOST R 34.10-2012 256 bit `B` param set (aka CryptoPro `A` (`XchA`) param set) */
41
- export const ID_GOSTR3410_2012_256_PARAM_SET_B = ({
41
+ export const ID_GOSTR3410_2012_256_PARAM_SET_B = {
42
42
  p: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd97n,
43
43
  n: 0xffffffffffffffffffffffffffffffff6c611070995ad10045841b09b761b893n,
44
44
  a: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd94n,
@@ -48,9 +48,9 @@ export const ID_GOSTR3410_2012_256_PARAM_SET_B = ({
48
48
  h: 1n,
49
49
  length: 32,
50
50
  oids: ["1.2.643.7.1.2.1.1.2", "1.2.643.2.2.35.1", "1.2.643.2.2.36.0"]
51
- });
51
+ };
52
52
  /** GOST R 34.10-2012 256 bit `C` param set (aka CryptoPro `B` param set) */
53
- export const ID_GOSTR3410_2012_256_PARAM_SET_C = ({
53
+ export const ID_GOSTR3410_2012_256_PARAM_SET_C = {
54
54
  p: 0x8000000000000000000000000000000000000000000000000000000000000c99n,
55
55
  n: 0x800000000000000000000000000000015f700cfff1a624e5e497161bcc8a198fn,
56
56
  a: 0x8000000000000000000000000000000000000000000000000000000000000c96n,
@@ -60,9 +60,9 @@ export const ID_GOSTR3410_2012_256_PARAM_SET_C = ({
60
60
  h: 1n,
61
61
  length: 32,
62
62
  oids: ["1.2.643.7.1.2.1.1.3", "1.2.643.2.2.35.2"]
63
- });
63
+ };
64
64
  /** GOST R 34.10-2012 256 bit `D` param set (aka CryptoPro `C` (`XchB`) param set) */
65
- export const ID_GOSTR3410_2012_256_PARAM_SET_D = ({
65
+ export const ID_GOSTR3410_2012_256_PARAM_SET_D = {
66
66
  p: 0x9b9f605f5a858107ab1ec85e6b41c8aacf846e86789051d37998f7b9022d759bn,
67
67
  n: 0x9b9f605f5a858107ab1ec85e6b41c8aa582ca3511eddfb74f02f3a6598980bb9n,
68
68
  a: 0x9b9f605f5a858107ab1ec85e6b41c8aacf846e86789051d37998f7b9022d7598n,
@@ -72,9 +72,9 @@ export const ID_GOSTR3410_2012_256_PARAM_SET_D = ({
72
72
  h: 1n,
73
73
  length: 32,
74
74
  oids: ["1.2.643.7.1.2.1.1.4", "1.2.643.2.2.35.3", "1.2.643.2.2.36.1"]
75
- });
75
+ };
76
76
  /** GOST R 34.10-2012 512 bit test param set */
77
- export const ID_GOSTR3410_2012_512_TEST_PARAM_SET = ({
77
+ export const ID_GOSTR3410_2012_512_TEST_PARAM_SET = {
78
78
  p: 0x4531acd1fe0023c7550d267b6b2fee80922b14b2ffb90f04d4eb7c09b5d2d15df1d852741af4704a0458047e80e4546d35b8336fac224dd81664bbf528be6373n,
79
79
  n: 0x4531acd1fe0023c7550d267b6b2fee80922b14b2ffb90f04d4eb7c09b5d2d15da82f2d7ecb1dbac719905c5eecc423f1d86e25edbe23c595d644aaf187e6e6dfn,
80
80
  a: 7n,
@@ -84,9 +84,9 @@ export const ID_GOSTR3410_2012_512_TEST_PARAM_SET = ({
84
84
  h: 1n,
85
85
  length: 64,
86
86
  oids: ["1.2.643.7.1.2.1.2.0"]
87
- });
87
+ };
88
88
  /** GOST R 34.10-2012 512 bit `A` param set */
89
- export const ID_GOSTR3410_2012_512_PARAM_SET_A = ({
89
+ export const ID_GOSTR3410_2012_512_PARAM_SET_A = {
90
90
  p: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdc7n,
91
91
  n: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff27e69532f48d89116ff22b8d4e0560609b4b38abfad2b85dcacdb1411f10b275n,
92
92
  a: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdc4n,
@@ -96,9 +96,9 @@ export const ID_GOSTR3410_2012_512_PARAM_SET_A = ({
96
96
  h: 1n,
97
97
  length: 64,
98
98
  oids: ["1.2.643.7.1.2.1.2.1"]
99
- });
99
+ };
100
100
  /** GOST R 34.10-2012 512 bit `B` param set */
101
- export const ID_GOSTR3410_2012_512_PARAM_SET_B = ({
101
+ export const ID_GOSTR3410_2012_512_PARAM_SET_B = {
102
102
  p: 0x8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006fn,
103
103
  n: 0x800000000000000000000000000000000000000000000000000000000000000149a1ec142565a545acfdb77bd9d40cfa8b996712101bea0ec6346c54374f25bdn,
104
104
  a: 0x8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006cn,
@@ -108,9 +108,9 @@ export const ID_GOSTR3410_2012_512_PARAM_SET_B = ({
108
108
  h: 1n,
109
109
  length: 64,
110
110
  oids: ["1.2.643.7.1.2.1.2.2"]
111
- });
111
+ };
112
112
  /** GOST R 34.10-2012 512 bit `C` param set */
113
- export const ID_GOSTR3410_2012_512_PARAM_SET_C = ({
113
+ export const ID_GOSTR3410_2012_512_PARAM_SET_C = {
114
114
  p: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdc7n,
115
115
  n: 0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc98cdba46506ab004c33a9ff5147502cc8eda9e7a769a12694623cef47f023edn,
116
116
  a: 0xdc9203e514a721875485a529d2c722fb187bc8980eb866644de41c68e143064546e861c0e2c9edd92ade71f46fcf50ff2ad97f951fda9f2a2eb6546f39689bd3n,
@@ -123,7 +123,7 @@ export const ID_GOSTR3410_2012_512_PARAM_SET_C = ({
123
123
  length: 64,
124
124
  st: [0x186c289cffa09c983b168c30c829006c952ff4aaf99c73850875d7e77bebef18d653187d6ba8fe533ec74c6f061872585b97cc0f50f57752cd73f4913304621en, 0x9a628f975594ecefd89ba28a2539ffb79c8ab238aeed0851fa5c1abb02b80b44c6734501b83a011dd625cd0b5145091a6d9acd4b1f5c5b1e21b2b249ddfd1271n],
125
125
  oids: ["1.2.643.7.1.2.1.2.3"]
126
- });
126
+ };
127
127
  export const CURVES = {
128
128
  ID_GOSTR3410_2001_PARAM_SET_CC,
129
129
  ID_GOSTR3410_2001_TEST_PARAM_SET,
@@ -1,5 +1,5 @@
1
1
  import { type TArg, type TRet } from "@noble/curves/utils.js";
2
- import { type GostCurveParameters } from "./const.js";
2
+ import type { GostCurveParameters } from "./const.js";
3
3
  /**
4
4
  * Generate public key from private.
5
5
  * @param parameters Curve parameters
package/gost3410/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { bytesToNumberBE, concatBytes, numberToBytesBE, randomBytes } from "@noble/curves/utils.js";
2
- import { CURVES } from "./const.js";
3
2
  import { mod } from "@noble/curves/abstract/modular.js";
4
3
  import { weierstrass } from "@noble/curves/abstract/weierstrass.js";
5
4
  /**
package/gost3410/vko.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Field } from "@noble/curves/abstract/modular.js";
2
2
  import { weierstrass } from "@noble/curves/abstract/weierstrass.js";
3
- import { gost341194 } from "../gost341194";
3
+ import { gost341194 } from "../gost341194/index.js";
4
4
  import { streebog256, streebog512 } from "../streebog/index.js";
5
5
  import { bytesToNumberBE, concatBytes, numberToBytesLE } from "@noble/curves/utils.js";
6
6
  /**
@@ -3,11 +3,11 @@ import { type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
3
3
  export declare class Gost341194 implements Hash<Gost341194> {
4
4
  private data;
5
5
  private sbox;
6
- readonly blockLen: number;
6
+ readonly blockLen = 32;
7
7
  readonly outputLen = 32;
8
8
  readonly canXOF = false;
9
9
  /** GOST R 34.11-94 hash function */
10
- constructor(data?: TArg<Uint8Array>, sbox?: TArg<Uint8Array>[]);
10
+ constructor(data?: TArg<Uint8Array>, sbox?: TArg<Uint8Array>);
11
11
  /** Create hash instance */
12
12
  static create(): Gost341194;
13
13
  destroy(): void;
@@ -18,6 +18,6 @@ export declare class Gost341194 implements Hash<Gost341194> {
18
18
  digest(): TRet<Uint8Array>;
19
19
  }
20
20
  /** GOST R 34.11-94 hash function */
21
- export declare const gost341194: (msg: TArg<Uint8Array>, sbox?: TArg<Uint8Array>[]) => TRet<Uint8Array>;
21
+ export declare const gost341194: (msg: TArg<Uint8Array>, sbox?: TArg<Uint8Array>) => TRet<Uint8Array>;
22
22
  /** DSTU GOST 34.311-95 */
23
23
  export declare const gost3431195: (msg: TArg<Uint8Array>) => TRet<Uint8Array>;
@@ -3,146 +3,50 @@ import { Magma } from "../magma/index.js";
3
3
  import { DSSZZI_UA_DKE_1, ID_GOSTR_3411_94_CRYPTOPRO_PARAM_SET } from "../magma/const.js";
4
4
  import { bytesToNumberBE, numberToBytesBE } from "@noble/curves/utils.js";
5
5
  import { xorBytes } from "../utils.js";
6
- const BLOCKSIZE = 32;
7
6
  const r = (1n << 256n) - 1n;
8
- const C2 = new Uint8Array(32);
9
7
  const C3 = new Uint8Array([
10
8
  0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0xff,
11
9
  0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0xff, 0x00,
12
10
  0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
13
11
  0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00
14
12
  ]);
15
- const C4 = new Uint8Array(32);
16
- const A = (x) => {
17
- const x2 = x.subarray(16, 24);
18
- return concatBytes(xorBytes(x.subarray(24, 32), x2), x.subarray(0, 8), x.subarray(8, 16), x2);
19
- };
13
+ const A = (x) => concatBytes(xorBytes(x.subarray(24, 32), x.subarray(16, 24)), x.subarray(0, 8), x.subarray(8, 16), x.subarray(16, 24));
20
14
  const P = (x) => new Uint8Array([
21
15
  x[0], x[8], x[16], x[24], x[1], x[9], x[17], x[25],
22
16
  x[2], x[10], x[18], x[26], x[3], x[11], x[19], x[27],
23
17
  x[4], x[12], x[20], x[28], x[5], x[13], x[21], x[29],
24
18
  x[6], x[14], x[22], x[30], x[7], x[15], x[23], x[31]
25
19
  ]);
26
- /*const chi = (Y: TArg<Uint8Array>): TRet<Uint8Array> => {
27
- const byx = new Uint8Array(2);
28
- byx[0] = Y[30] ^ Y[28] ^ Y[26] ^ Y[24] ^ Y[6] ^ Y[0];
29
- byx[1] = Y[31] ^ Y[29] ^ Y[27] ^ Y[25] ^ Y[7] ^ Y[1];
30
-
31
- const result = new Uint8Array(BLOCKSIZE);
32
- result.set(byx, 0);
33
- result.set(Y.slice(0,30), 2);
34
-
35
- return result;
36
- }*/
37
20
  const chi = (Y) => new Uint8Array([
38
21
  Y[30] ^ Y[28] ^ Y[26] ^ Y[24] ^ Y[6] ^ Y[0],
39
22
  Y[31] ^ Y[29] ^ Y[27] ^ Y[25] ^ Y[7] ^ Y[1],
40
23
  ...Y.subarray(0, 30)
41
24
  ]);
25
+ const _getMagma = (u, v, sbox) => new Magma(P(xorBytes(u, v)).reverse(), sbox, true);
42
26
  const _step = (hin, m, sbox) => {
43
- let u = hin;
44
- let v = m;
45
- let w = xorBytes(hin, m);
46
- const k1 = new Magma(P(w).reverse(), sbox, true);
47
- u = xorBytes(A(u), C2);
48
- v = A(A(v));
49
- w = xorBytes(u, v);
50
- const k2 = new Magma(P(w).reverse(), sbox, true);
27
+ const k1 = _getMagma(hin, m, sbox);
28
+ let u = A(hin);
29
+ let v = A(A(m));
30
+ const k2 = _getMagma(u, v, sbox);
51
31
  u = xorBytes(A(u), C3);
52
32
  v = A(A(v));
53
- w = xorBytes(u, v);
54
- const k3 = new Magma(P(w).reverse(), sbox, true);
55
- u = xorBytes(A(u), C4);
33
+ const k3 = _getMagma(u, v, sbox);
34
+ u = A(u);
56
35
  v = A(A(v));
57
- w = xorBytes(u, v);
58
- const k4 = new Magma(P(w).reverse(), sbox, true);
59
- const s = concatBytes(k4.encrypt(hin.slice(0, 8).reverse()).reverse(), k3.encrypt(hin.slice(8, 16).reverse()).reverse(), k2.encrypt(hin.slice(16, 24).reverse()).reverse(), k1.encrypt(hin.slice(24, 32).reverse()).reverse());
60
- //let x = new Uint8Array(s);
61
- //for(let i = 0; i < 12; i++) x = chi(x);
62
- let x = chi(s);
63
- x = chi(x);
64
- x = chi(x);
65
- x = chi(x);
66
- x = chi(x);
67
- x = chi(x);
68
- x = chi(x);
69
- x = chi(x);
70
- x = chi(x);
71
- x = chi(x);
72
- x = chi(x);
73
- x = chi(x);
74
- x = xorBytes(x, m);
75
- x = chi(x);
76
- x = xorBytes(hin, x);
77
- //for(let i = 0; i < 61; i++) x = chi(x);
78
- x = chi(x);
79
- x = chi(x);
80
- x = chi(x);
81
- x = chi(x);
82
- x = chi(x);
83
- x = chi(x);
84
- x = chi(x);
85
- x = chi(x);
86
- x = chi(x);
87
- x = chi(x);
88
- x = chi(x);
89
- x = chi(x);
90
- x = chi(x);
91
- x = chi(x);
92
- x = chi(x);
93
- x = chi(x);
94
- x = chi(x);
95
- x = chi(x);
96
- x = chi(x);
97
- x = chi(x);
98
- x = chi(x);
99
- x = chi(x);
100
- x = chi(x);
101
- x = chi(x);
102
- x = chi(x);
103
- x = chi(x);
104
- x = chi(x);
105
- x = chi(x);
106
- x = chi(x);
107
- x = chi(x);
108
- x = chi(x);
109
- x = chi(x);
110
- x = chi(x);
111
- x = chi(x);
112
- x = chi(x);
113
- x = chi(x);
114
- x = chi(x);
115
- x = chi(x);
116
- x = chi(x);
117
- x = chi(x);
118
- x = chi(x);
119
- x = chi(x);
120
- x = chi(x);
121
- x = chi(x);
122
- x = chi(x);
123
- x = chi(x);
124
- x = chi(x);
125
- x = chi(x);
126
- x = chi(x);
127
- x = chi(x);
128
- x = chi(x);
129
- x = chi(x);
130
- x = chi(x);
131
- x = chi(x);
132
- x = chi(x);
133
- x = chi(x);
134
- x = chi(x);
135
- x = chi(x);
136
- x = chi(x);
137
- x = chi(x);
138
- x = chi(x);
36
+ const k4 = _getMagma(u, v, sbox);
37
+ const x = concatBytes(k4.encrypt(hin.slice(0, 8).reverse()).reverse(), k3.encrypt(hin.slice(8, 16).reverse()).reverse(), k2.encrypt(hin.slice(16, 24).reverse()).reverse(), k1.encrypt(hin.slice(24, 32).reverse()).reverse());
38
+ for (let i = 0; i < 12; i++)
39
+ x.set(chi(x));
40
+ x.set(xorBytes(hin, chi(xorBytes(x, m))));
41
+ for (let i = 0; i < 61; i++)
42
+ x.set(chi(x));
139
43
  return x;
140
44
  };
141
45
  /** GOST R 34.11-94 hash function */
142
46
  export class Gost341194 {
143
47
  data;
144
48
  sbox;
145
- blockLen = BLOCKSIZE;
49
+ blockLen = 32;
146
50
  outputLen = 32;
147
51
  canXOF = false;
148
52
  /** GOST R 34.11-94 hash function */
@@ -167,17 +71,17 @@ export class Gost341194 {
167
71
  digestInto(buf) {
168
72
  let len = 0n;
169
73
  let checksum = 0n;
170
- const h = new Uint8Array(BLOCKSIZE);
74
+ const h = new Uint8Array(this.blockLen);
171
75
  const m = new Uint8Array(this.data);
172
- for (let i = 0; i < m.length; i += BLOCKSIZE) {
173
- let part = m.slice(i, i + BLOCKSIZE).reverse();
76
+ for (let i = 0; i < m.length; i += this.blockLen) {
77
+ let part = m.slice(i, i + this.blockLen).reverse();
174
78
  len += BigInt(part.length) * 8n;
175
79
  checksum = (checksum + bytesToNumberBE(part)) & r;
176
- if (part.length < BLOCKSIZE)
177
- part = numberToBytesBE(bytesToNumberBE(part), BLOCKSIZE);
80
+ if (part.length < this.blockLen)
81
+ part = numberToBytesBE(bytesToNumberBE(part), this.blockLen);
178
82
  h.set(_step(h, part, this.sbox));
179
83
  }
180
- h.set(_step(_step(h, numberToBytesBE(len, BLOCKSIZE), this.sbox), numberToBytesBE(checksum, BLOCKSIZE), this.sbox));
84
+ h.set(_step(_step(h, numberToBytesBE(len, this.blockLen), this.sbox), numberToBytesBE(checksum, this.blockLen), this.sbox));
181
85
  buf.set(h.reverse());
182
86
  this.destroy();
183
87
  }
package/kdf.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 { streebog256hmac } from "./hmac.js";
3
3
  import { numberToBytesBE } from "@noble/curves/utils.js";
4
4
  import { pbkdf2 } from "@noble/hashes/pbkdf2.js";
@@ -35,7 +35,7 @@ export const cpkdf = (password, salt) => {
35
35
  if (password.length != 0)
36
36
  hasher.update(pin);
37
37
  const hash = hasher.digest();
38
- const c = new Uint8Array(CPKDF_CONST);
38
+ const c = copyBytes(CPKDF_CONST);
39
39
  const m0 = new Uint8Array(bs);
40
40
  const m1 = new Uint8Array(bs);
41
41
  for (let j = 0; j < (password.length != 0 ? 2000 : 2); j++) {
@@ -37,11 +37,6 @@ export const PI_REV = new Uint8Array([
37
37
  export const L = new Uint8Array([
38
38
  0x01, 0x94, 0x20, 0x85, 0x10, 0xc2, 0xc0, 0x01, 0xfb, 0x01, 0xc0, 0xc2, 0x10, 0x85, 0x20, 0x94,
39
39
  ]);
40
- /*const ITER: Uint8Array[] = Array(32).fill(null).map(() => new Uint8Array(16).fill(0));
41
- for(let i = 0; i < 32; i++) {
42
- ITER[i][15] = i + 1;
43
- ITER[i] = LL(ITER[i]);
44
- }*/
45
40
  export const ITER = [
46
41
  new Uint8Array([0x6E, 0xA2, 0x76, 0x72, 0x6C, 0x48, 0x7A, 0xB8, 0x5D, 0x27, 0xBD, 0x10, 0xDD, 0x84, 0x94, 0x01]),
47
42
  new Uint8Array([0xDC, 0x87, 0xEC, 0xE4, 0xD8, 0x90, 0xF4, 0xB3, 0xBA, 0x4E, 0xB9, 0x20, 0x79, 0xCB, 0xEB, 0x02]),