@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 CHANGED
@@ -28,6 +28,7 @@ npm i @li0ard/gost
28
28
  - Kuznyechik cipher (GOST R 34.12-2015)
29
29
  - Magma cipher (GOST R 34.12-2015)
30
30
  - Supports legacy version from GOST 28147-89
31
+ - Various S-Box'es included (CryptoPro, DSSZZI, tests etc.)
31
32
  - Streebog hash function (GOST R 34.11-2012)
32
33
  - Supports HMAC, PBKDF2 (512 bit), `kdf_gostr3411_2012_256`, `kdf_tree_gostr3411_2012_256` and CPKDF
33
34
 
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, 0x55, 0xFF, 0xC2, 0x85, 0x4C, 0xD4, 0xBF, 0x02, 0x06, 0x0A,
26
+ 0x1E, 0x22, 0x66, 0xAA, 0x3D, 0x47, 0xC9, 0x98, 0x6B, 0xBD, 0x04, 0x0C, 0x14, 0x3C, 0x44, 0xCC,
27
+ 0x97, 0x7A, 0x8E, 0x51, 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A, 0xA2, 0x21, 0x7D, 0xEA, 0x3C, 0x73, 0x4C,
51
+ 0x5B, 0x4D, 0x74, 0x50, 0xEB, 0x09, 0x69, 0x3D, 0x2B, 0xE0, 0x82, 0xA3, 0x5E, 0x7E, 0x22, 0x88,
52
+ 0xE7, 0xE5, 0x77, 0x99, 0x4B, 0xE9, 0xA1, 0x20, 0x17, 0xB7, 0x9B, 0x62, 0xD6, 0x53, 0xF9, 0x79,
53
+ 0x43, 0x40, 0x30, 0xFB, 0x96, 0xD3, 0x7B, 0x71, 0x8C, 0xD8, 0x13, 0xDC, 0x55, 0xAA, 0x6C, 0x46,
54
+ 0xEE, 0x33, 0x64, 0xC6, 0xCF, 0xCB, 0x39, 0x9D, 0xB9, 0x26, 0x90, 0xBE, 0xB1, 0x19, 0xFE, 0x0C,
55
+ 0x4F, 0x5A, 0x08, 0x68, 0x87, 0x5D, 0xDF, 0x81, 0x61, 0x16, 0x52, 0xF8, 0x1F, 0x4A, 0xE4, 0x76,
56
+ 0xC5, 0xED, 0xCA, 0x38, 0x0B, 0xB0, 0x25, 0x8F, 0xDB, 0x8B, 0xA9, 0x6B, 0x70, 0x95, 0x3F, 0x2F,
57
+ 0xF3, 0xF2, 0xA5, 0xF4, 0xAD, 0xA6, 0xF5, 0x84, 0xE2, 0xF6, 0x85, 0x58, 0xA7, 0x2D, 0xC3, 0xAE,
58
+ 0x37, 0xC4, 0xAF, 0x24, 0x2E, 0x6F, 0x8A, 0xA8, 0xF7, 0x60, 0x49, 0xE3, 0x80, 0x86, 0x59, 0x07
59
+ ]);
60
+ export const gf256Multiply = (a, b) => (a == 0 || b == 0)
61
+ ? 0
62
+ : gf_multtable_exp[(gf_multtable_log[a] + gf_multtable_log[b]) % 255];
package/gf/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { type TArg, type TRet } from "@noble/curves/utils.js";
2
+ export declare const gf64Multiply: (a: TArg<Uint8Array>, b: TArg<Uint8Array>) => TRet<Uint8Array>;
3
+ export declare const gf128Multiply: (a: TArg<Uint8Array>, b: TArg<Uint8Array>) => TRet<Uint8Array>;
4
+ export { gf256Multiply } from "./gf256.js";
package/gf/index.js ADDED
@@ -0,0 +1,18 @@
1
+ import { bytesToNumberBE, numberToBytesBE } from "@noble/curves/utils.js";
2
+ const gf2m_multiply = (degree, poly, a, b) => {
3
+ let x = bytesToNumberBE(a), y = bytesToNumberBE(b), z = 0n;
4
+ const max_bit = 1n << (degree - 1n);
5
+ while (y > 0n) {
6
+ if ((y & 1n) == 1n)
7
+ z ^= x;
8
+ if ((x & max_bit) > 0n)
9
+ x = ((x ^ max_bit) << 1n) ^ poly;
10
+ else
11
+ x <<= 1n;
12
+ y >>= 1n;
13
+ }
14
+ return numberToBytesBE(z, Number(degree / 8n));
15
+ };
16
+ export const gf64Multiply = (a, b) => gf2m_multiply(64n, 0x1bn, a, b);
17
+ export const gf128Multiply = (a, b) => gf2m_multiply(128n, 0x87n, a, b);
18
+ 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,
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
  /**
@@ -1,13 +1,13 @@
1
1
  import { type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
2
2
  /** GOST R 34.11-94 hash function */
3
3
  export declare class Gost341194 implements Hash<Gost341194> {
4
- private data;
4
+ private buffer;
5
5
  private sbox;
6
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(buffer?: 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>;
@@ -1,4 +1,4 @@
1
- import { concatBytes } from "@noble/hashes/utils.js";
1
+ import { concatBytes, copyBytes } from "@noble/hashes/utils.js";
2
2
  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";
@@ -10,7 +10,7 @@ const C3 = new Uint8Array([
10
10
  0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
11
11
  0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00
12
12
  ]);
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));
13
+ const A = (x) => concatBytes(xorBytes(x.subarray(24, 32), x.subarray(16, 24)), x.subarray(0, 24));
14
14
  const P = (x) => new Uint8Array([
15
15
  x[0], x[8], x[16], x[24], x[1], x[9], x[17], x[25],
16
16
  x[2], x[10], x[18], x[26], x[3], x[11], x[19], x[27],
@@ -44,35 +44,35 @@ const _step = (hin, m, sbox) => {
44
44
  };
45
45
  /** GOST R 34.11-94 hash function */
46
46
  export class Gost341194 {
47
- data;
47
+ buffer;
48
48
  sbox;
49
49
  blockLen = 32;
50
50
  outputLen = 32;
51
51
  canXOF = false;
52
52
  /** GOST R 34.11-94 hash function */
53
- constructor(data = new Uint8Array(), sbox = ID_GOSTR_3411_94_CRYPTOPRO_PARAM_SET) {
54
- this.data = data;
53
+ constructor(buffer = new Uint8Array(), sbox = ID_GOSTR_3411_94_CRYPTOPRO_PARAM_SET) {
54
+ this.buffer = buffer;
55
55
  this.sbox = sbox;
56
56
  }
57
57
  /** Create hash instance */
58
58
  static create() { return new Gost341194(); }
59
- destroy() { this.data = new Uint8Array(); }
59
+ destroy() { this.buffer = new Uint8Array(); }
60
60
  clone() { return this._cloneInto(); }
61
61
  _cloneInto(to) {
62
62
  to ||= new Gost341194();
63
- to.data = new Uint8Array(this.data);
63
+ to.buffer = new Uint8Array(this.buffer);
64
64
  to.sbox = this.sbox;
65
65
  return to;
66
66
  }
67
67
  update(data) {
68
- this.data = concatBytes(this.data, data);
68
+ this.buffer = concatBytes(this.buffer, data);
69
69
  return this;
70
70
  }
71
71
  digestInto(buf) {
72
72
  let len = 0n;
73
73
  let checksum = 0n;
74
74
  const h = new Uint8Array(this.blockLen);
75
- const m = new Uint8Array(this.data);
75
+ const m = copyBytes(this.buffer);
76
76
  for (let i = 0; i < m.length; i += this.blockLen) {
77
77
  let part = m.slice(i, i + this.blockLen).reverse();
78
78
  len += BigInt(part.length) * 8n;
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";
@@ -8,8 +8,6 @@ import { pad1, xorBytes } from "./utils.js";
8
8
  const _0 = new Uint8Array([0]);
9
9
  const _1 = new Uint8Array([1]);
10
10
  const _0100 = new Uint8Array([1, 0]);
11
- const CPKDF_CONST = new Uint8Array(64);
12
- CPKDF_CONST.set(new TextEncoder().encode("DENEFH028.760246785.IUEFHWUIO.EF"));
13
11
  const _36 = new Uint8Array(64).fill(0x36);
14
12
  const _5C = new Uint8Array(64).fill(0x5C);
15
13
  export const kdf_gostr3411_2012_256 = (key, label, seed) => streebog256hmac(key, concatBytes(_1, label, _0, seed, _0100));
@@ -35,9 +33,9 @@ export const cpkdf = (password, salt) => {
35
33
  if (password.length != 0)
36
34
  hasher.update(pin);
37
35
  const hash = hasher.digest();
38
- const c = new Uint8Array(CPKDF_CONST);
39
- const m0 = new Uint8Array(bs);
40
- const m1 = new Uint8Array(bs);
36
+ const c = new Uint8Array(64);
37
+ c.set(new TextEncoder().encode("DENEFH028.760246785.IUEFHWUIO.EF"));
38
+ const m0 = new Uint8Array(bs), m1 = new Uint8Array(bs);
41
39
  for (let j = 0; j < (password.length != 0 ? 2000 : 2); j++) {
42
40
  m0.set(xorBytes(c, _36));
43
41
  m1.set(xorBytes(c, _5C));
@@ -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));
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]),
@@ -1,11 +1,18 @@
1
1
  import { type TArg, type TRet } from "@noble/curves/utils.js";
2
2
  import type { Cipher } from "../types.js";
3
- /** Kuznyechik (GOST R 34.12-2015) cipher */
3
+ /**
4
+ * **Kuznyechik cipher**
5
+ *
6
+ * Described by GOST R 34.12-2015 ([RFC 7801](https://datatracker.ietf.org/doc/html/rfc7801.html))
7
+ */
4
8
  export declare class Kuznyechik implements Cipher {
5
9
  readonly keySize = 32;
6
10
  readonly blockSize = 16;
7
11
  private roundKeys;
8
- /** Kuznyechik (GOST R 34.12-2015) cipher */
12
+ /**
13
+ * Kuznyechik (GOST R 34.12-2015) cipher
14
+ * @param key Encryption key
15
+ */
9
16
  constructor(key: TArg<Uint8Array>);
10
17
  encrypt(plaintext: TArg<Uint8Array>): TRet<Uint8Array>;
11
18
  decrypt(ciphertext: TArg<Uint8Array>): TRet<Uint8Array>;
@@ -1,19 +1,7 @@
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 gfMultiply = (a, b) => {
5
- let result = 0, high_bit;
6
- for (let _ = 0; _ < 8; _++) {
7
- if ((b & 1) === 1)
8
- result ^= a;
9
- high_bit = a & 0x80;
10
- a <<= 1;
11
- if (high_bit == 0x80)
12
- a ^= 0xC3;
13
- b >>= 1;
14
- }
15
- return result & 0xFF;
16
- };
4
+ import { gf256Multiply } from "../gf/index.js";
17
5
  const S = (input, pi = PI) => new Uint8Array([
18
6
  pi[input[0]], pi[input[1]], pi[input[2]], pi[input[3]],
19
7
  pi[input[4]], pi[input[5]], pi[input[6]], pi[input[7]],
@@ -21,26 +9,26 @@ const S = (input, pi = PI) => new Uint8Array([
21
9
  pi[input[12]], pi[input[13]], pi[input[14]], pi[input[15]]
22
10
  ]);
23
11
  const R = (input) => new Uint8Array([
24
- gfMultiply(input[15], L[0]) ^ gfMultiply(input[0], L[1]) ^
25
- gfMultiply(input[1], L[2]) ^ gfMultiply(input[2], L[3]) ^
26
- gfMultiply(input[3], L[4]) ^ gfMultiply(input[4], L[5]) ^
27
- gfMultiply(input[5], L[6]) ^ gfMultiply(input[6], L[7]) ^
28
- gfMultiply(input[7], L[8]) ^ gfMultiply(input[8], L[9]) ^
29
- gfMultiply(input[9], L[10]) ^ gfMultiply(input[10], L[11]) ^
30
- gfMultiply(input[11], L[12]) ^ gfMultiply(input[12], L[13]) ^
31
- gfMultiply(input[13], L[14]) ^ gfMultiply(input[14], L[15]),
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]),
32
20
  ...input.subarray(0, 15)
33
21
  ]);
34
22
  const Rr = (input) => new Uint8Array([
35
23
  ...input.subarray(1, 16),
36
- gfMultiply(input[0], L[0]) ^ gfMultiply(input[1], L[1]) ^
37
- gfMultiply(input[2], L[2]) ^ gfMultiply(input[3], L[3]) ^
38
- gfMultiply(input[4], L[4]) ^ gfMultiply(input[5], L[5]) ^
39
- gfMultiply(input[6], L[6]) ^ gfMultiply(input[7], L[7]) ^
40
- gfMultiply(input[8], L[8]) ^ gfMultiply(input[9], L[9]) ^
41
- gfMultiply(input[10], L[10]) ^ gfMultiply(input[11], L[11]) ^
42
- gfMultiply(input[12], L[12]) ^ gfMultiply(input[13], L[13]) ^
43
- gfMultiply(input[14], L[14]) ^ gfMultiply(input[15], L[15])
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])
44
32
  ]);
45
33
  // Call `R` 16x times
46
34
  const LL = (input) => R(R(R(R(R(R(R(R(R(R(R(R(R(R(R(R(input))))))))))))))));
@@ -49,22 +37,26 @@ const LLr = (input) => Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(Rr(input))))
49
37
  const LLS = (block) => LL(S(block));
50
38
  const SLLr = (block) => S(LLr(block), PI_REV);
51
39
  const F = (inKey, inKey2, iter) => xorBytes(LLS(xorBytes(inKey, iter)), inKey2);
52
- /** Kuznyechik (GOST R 34.12-2015) cipher */
40
+ /**
41
+ * **Kuznyechik cipher**
42
+ *
43
+ * Described by GOST R 34.12-2015 ([RFC 7801](https://datatracker.ietf.org/doc/html/rfc7801.html))
44
+ */
53
45
  export class Kuznyechik {
54
46
  keySize = 32;
55
47
  blockSize = 16;
56
48
  roundKeys;
57
- /** Kuznyechik (GOST R 34.12-2015) cipher */
49
+ /**
50
+ * Kuznyechik (GOST R 34.12-2015) cipher
51
+ * @param key Encryption key
52
+ */
58
53
  constructor(key) {
59
54
  if (key.length !== this.keySize)
60
55
  throw new Error("Invalid key length");
61
56
  const roundKeys = Array(10);
62
57
  roundKeys[0] = key.slice(0, this.blockSize);
63
58
  roundKeys[1] = key.slice(this.blockSize);
64
- let temp1 = copyBytes(roundKeys[0]);
65
- let temp2 = copyBytes(roundKeys[1]);
66
- let temp3 = new Uint8Array(16);
67
- let temp4 = new Uint8Array(16);
59
+ let temp1 = copyBytes(roundKeys[0]), temp2 = copyBytes(roundKeys[1]), temp3 = new Uint8Array(16), temp4 = new Uint8Array(16);
68
60
  for (let i = 0; i < 4; i++) {
69
61
  const baseIndex = i * 8;
70
62
  temp3 = F(temp1, temp2, ITER[baseIndex]);
package/magma/const.d.ts CHANGED
@@ -1,58 +1,58 @@
1
- /** S-Box from RFC 7836 */
2
- export declare const ID_TC26_GOST_28147_PARAM_Z: Uint8Array<ArrayBuffer>[];
1
+ /** S-Box from RFC 8891 */
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: {