@li0ard/gost 0.0.1 → 0.1.3
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/LICENSE +21 -0
- package/README.md +47 -45
- package/gost3410/const.d.ts +47 -0
- package/gost3410/const.js +126 -0
- package/gost3410/conversion.d.ts +19 -0
- package/gost3410/conversion.js +35 -0
- package/gost3410/index.d.ts +29 -0
- package/gost3410/index.js +84 -0
- package/gost3410/vko.d.ts +38 -0
- package/gost3410/vko.js +47 -0
- package/gost341194/index.d.ts +23 -0
- package/gost341194/index.js +193 -0
- package/hmac.d.ts +15 -0
- package/hmac.js +22 -0
- package/index.d.ts +9 -0
- package/index.js +9 -0
- package/kdf.d.ts +7 -0
- package/kdf.js +59 -0
- package/kuznyechik/const.d.ts +4 -0
- package/kuznyechik/const.js +78 -0
- package/kuznyechik/index.d.ts +12 -0
- package/kuznyechik/index.js +207 -0
- package/magma/const.d.ts +62 -0
- package/magma/const.js +244 -0
- package/magma/index.d.ts +24 -0
- package/magma/index.js +86 -0
- package/modes/_keytransform.d.ts +5 -0
- package/modes/_keytransform.js +35 -0
- package/modes/cbc.d.ts +8 -0
- package/modes/cbc.js +42 -0
- package/modes/cfb.d.ts +8 -0
- package/modes/cfb.js +37 -0
- package/modes/ctr.d.ts +15 -0
- package/modes/ctr.js +62 -0
- package/modes/ecb.d.ts +7 -0
- package/modes/ecb.js +21 -0
- package/modes/index.d.ts +8 -0
- package/modes/index.js +8 -0
- package/modes/mac.d.ts +21 -0
- package/modes/mac.js +119 -0
- package/modes/mgm.d.ts +8 -0
- package/modes/mgm.js +90 -0
- package/modes/ofb.d.ts +8 -0
- package/modes/ofb.js +25 -0
- package/modes/wrap.d.ts +14 -0
- package/modes/wrap.js +57 -0
- package/package.json +48 -7
- package/streebog/const.d.ts +4 -0
- package/streebog/const.js +102 -0
- package/streebog/index.d.ts +66 -0
- package/streebog/index.js +295 -0
- package/types.d.ts +50 -0
- package/types.js +1 -0
- package/utils.d.ts +7 -0
- package/utils.js +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nikolai Konovalov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,45 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
-
|
|
41
|
-
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<b>@li0ard/gost</b><br>
|
|
3
|
+
<b>GOST cryptography algorithms in pure TypeScript</b>
|
|
4
|
+
<br>
|
|
5
|
+
<a href="https://li0ard.is-cool.dev/gost">docs</a>
|
|
6
|
+
<br><br>
|
|
7
|
+
<a href="https://github.com/li0ard/gost/actions/workflows/test.yml"><img src="https://github.com/li0ard/gost/actions/workflows/test.yml/badge.svg" /></a>
|
|
8
|
+
<a href="https://github.com/li0ard/gost/blob/main/LICENSE"><img src="https://img.shields.io/github/license/li0ard/gost" /></a>
|
|
9
|
+
<br>
|
|
10
|
+
<a href="https://npmjs.com/package/@li0ard/gost"><img src="https://img.shields.io/npm/v/@li0ard/gost" /></a>
|
|
11
|
+
<br>
|
|
12
|
+
<hr>
|
|
13
|
+
</p>
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm i @li0ard/gost
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Supported algorithms
|
|
22
|
+
|
|
23
|
+
- Curves and DSA (GOST R 34.10-2001 and GOST R 34.10-2012)
|
|
24
|
+
- VKO key agreement function
|
|
25
|
+
- Points conversion from Weierstrass to Twisted Edwards form and vice versa
|
|
26
|
+
- GOST R 34.11-94 hash function
|
|
27
|
+
- Supports HMAC and PBKDF2
|
|
28
|
+
- Kuznyechik cipher (GOST R 34.12-2015)
|
|
29
|
+
- Magma cipher (GOST R 34.12-2015)
|
|
30
|
+
- Supports legacy version from GOST 28147-89
|
|
31
|
+
- Streebog hash function (GOST R 34.11-2012)
|
|
32
|
+
- Supports HMAC, PBKDF2 (512 bit), `kdf_gostr3411_2012_256`, `kdf_tree_gostr3411_2012_256` and CPKDF
|
|
33
|
+
|
|
34
|
+
## Supported cipher modes
|
|
35
|
+
|
|
36
|
+
- Cipher Block Chaining mode (CBC)
|
|
37
|
+
- Cipher Feedback mode (CFB)
|
|
38
|
+
- Counter mode (CTR)
|
|
39
|
+
- Supports legacy version for GOST 28147-89 (CNT) and Counter with Advance Cryptographic Prolongation of Key Material (CTR-ACPKM)
|
|
40
|
+
- Electronic Codebook mode (ECB)
|
|
41
|
+
- Message Authentication Code mode (MAC)
|
|
42
|
+
- Supports legacy version for GOST 28147-89 and MAC with Advance Cryptographic Prolongation of Key Material (OMAC-ACPKM)
|
|
43
|
+
- Multilinear Galois mode (MGM)
|
|
44
|
+
- Output Feedback mode (OFB)
|
|
45
|
+
- Key wrapping:
|
|
46
|
+
- Modern key wrapping with KExp15/KImp15
|
|
47
|
+
- Legacy key wrapping for GOST 28147-89 (KWP) with CryptoPro key derivation
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** Parameters for GOST curves */
|
|
2
|
+
export interface GostCurveParameters {
|
|
3
|
+
/** Prime field (`Fp`) */
|
|
4
|
+
p: bigint;
|
|
5
|
+
/** Curve order (`Fn`) */
|
|
6
|
+
n: bigint;
|
|
7
|
+
/** Param `a` */
|
|
8
|
+
a: bigint;
|
|
9
|
+
/** Param `b` */
|
|
10
|
+
b: bigint;
|
|
11
|
+
/** Base point `X` coordinate */
|
|
12
|
+
Gx: bigint;
|
|
13
|
+
/** Base point `Y` coordinate */
|
|
14
|
+
Gy: bigint;
|
|
15
|
+
/** Cofactor */
|
|
16
|
+
h: bigint;
|
|
17
|
+
/** Curve point length */
|
|
18
|
+
length: number;
|
|
19
|
+
/** Param `e` (`a`) for representation as Twisted Edwards */
|
|
20
|
+
e?: bigint;
|
|
21
|
+
/** Param `d` for representation as Twisted Edwards */
|
|
22
|
+
d?: bigint;
|
|
23
|
+
/** Precomputed parameters for point conversion */
|
|
24
|
+
st?: bigint[];
|
|
25
|
+
/** Curve OIDs */
|
|
26
|
+
oids?: string[];
|
|
27
|
+
}
|
|
28
|
+
/** GOST R 34.10-2001 CryptoCom param set */
|
|
29
|
+
export declare const ID_GOSTR3410_2001_PARAM_SET_CC: Readonly<GostCurveParameters>;
|
|
30
|
+
/** GOST R 34.10-2001 test param set */
|
|
31
|
+
export declare const ID_GOSTR3410_2001_TEST_PARAM_SET: Readonly<GostCurveParameters>;
|
|
32
|
+
/** GOST R 34.10-2012 256 bit `A` param set */
|
|
33
|
+
export declare const ID_GOSTR3410_2012_256_PARAM_SET_A: Readonly<GostCurveParameters>;
|
|
34
|
+
/** GOST R 34.10-2012 256 bit `B` param set (aka CryptoPro `A` (`XchA`) param set) */
|
|
35
|
+
export declare const ID_GOSTR3410_2012_256_PARAM_SET_B: Readonly<GostCurveParameters>;
|
|
36
|
+
/** GOST R 34.10-2012 256 bit `C` param set (aka CryptoPro `B` param set) */
|
|
37
|
+
export declare const ID_GOSTR3410_2012_256_PARAM_SET_C: Readonly<GostCurveParameters>;
|
|
38
|
+
/** GOST R 34.10-2012 256 bit `D` param set (aka CryptoPro `C` (`XchB`) param set) */
|
|
39
|
+
export declare const ID_GOSTR3410_2012_256_PARAM_SET_D: Readonly<GostCurveParameters>;
|
|
40
|
+
/** GOST R 34.10-2012 512 bit test param set */
|
|
41
|
+
export declare const ID_GOSTR3410_2012_512_TEST_PARAM_SET: Readonly<GostCurveParameters>;
|
|
42
|
+
/** GOST R 34.10-2012 512 bit `A` param set */
|
|
43
|
+
export declare const ID_GOSTR3410_2012_512_PARAM_SET_A: Readonly<GostCurveParameters>;
|
|
44
|
+
/** GOST R 34.10-2012 512 bit `B` param set */
|
|
45
|
+
export declare const ID_GOSTR3410_2012_512_PARAM_SET_B: Readonly<GostCurveParameters>;
|
|
46
|
+
/** GOST R 34.10-2012 512 bit `C` param set */
|
|
47
|
+
export declare const ID_GOSTR3410_2012_512_PARAM_SET_C: Readonly<GostCurveParameters>;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/** GOST R 34.10-2001 CryptoCom param set */
|
|
2
|
+
export const ID_GOSTR3410_2001_PARAM_SET_CC = ({
|
|
3
|
+
p: 0xc0000000000000000000000000000000000000000000000000000000000003c7n,
|
|
4
|
+
n: 0x5fffffffffffffffffffffffffffffff606117a2f4bde428b7458a54b6e87b85n,
|
|
5
|
+
a: 0xc0000000000000000000000000000000000000000000000000000000000003c4n,
|
|
6
|
+
b: 0x2d06b4265ebc749ff7d0f1f1f88232e81632e9088fd44b7787d5e407e955080cn,
|
|
7
|
+
Gx: 2n,
|
|
8
|
+
Gy: 0xa20e034bf8813ef5c18d01105e726a17eb248b264ae9706f440bedc8ccb6b22cn,
|
|
9
|
+
h: 1n,
|
|
10
|
+
length: 32,
|
|
11
|
+
oids: ["1.2.643.2.9.1.8.1"]
|
|
12
|
+
});
|
|
13
|
+
/** GOST R 34.10-2001 test param set */
|
|
14
|
+
export const ID_GOSTR3410_2001_TEST_PARAM_SET = ({
|
|
15
|
+
p: 0x8000000000000000000000000000000000000000000000000000000000000431n,
|
|
16
|
+
n: 0x8000000000000000000000000000000150fe8a1892976154c59cfc193accf5b3n,
|
|
17
|
+
a: 7n,
|
|
18
|
+
b: 0x5fbff498aa938ce739b8e022fbafef40563f6e6a3472fc2a514c0ce9dae23b7en,
|
|
19
|
+
Gx: 2n,
|
|
20
|
+
Gy: 0x08e2a8a0e65147d4bd6316030e16d19c85c97f0a9ca267122b96abbcea7e8fc8n,
|
|
21
|
+
h: 1n,
|
|
22
|
+
length: 32,
|
|
23
|
+
oids: ["1.2.643.2.2.35.0"]
|
|
24
|
+
});
|
|
25
|
+
/** GOST R 34.10-2012 256 bit `A` param set */
|
|
26
|
+
export const ID_GOSTR3410_2012_256_PARAM_SET_A = ({
|
|
27
|
+
p: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd97n,
|
|
28
|
+
n: 0x400000000000000000000000000000000fd8cddfc87b6635c115af556c360c67n,
|
|
29
|
+
a: 0xc2173f1513981673af4892c23035a27ce25e2013bf95aa33b22c656f277e7335n,
|
|
30
|
+
b: 0x295f9bae7428ed9ccc20e7c359a9d41a22fccd9108e17bf7ba9337a6f8ae9513n,
|
|
31
|
+
Gx: 0x91e38443a5e82c0d880923425712b2bb658b9196932e02c78b2582fe742daa28n,
|
|
32
|
+
Gy: 0x32879423ab1a0375895786c4bb46e9565fde0b5344766740af268adb32322e5cn,
|
|
33
|
+
h: 4n,
|
|
34
|
+
e: 1n,
|
|
35
|
+
d: 0x0605f6b7c183fa81578bc39cfad518132b9df62897009af7e522c32d6dc7bffbn,
|
|
36
|
+
length: 32,
|
|
37
|
+
st: [0x7e7e82520f9f015faa1d0f18c14ab9fb35188275da3fd94206b74f34a48e0ecdn, 0x0100fe73f595ff158e974b44d478d9588744fe5c192ac47ea63075dce7a14aaan],
|
|
38
|
+
oids: ["1.2.643.7.1.2.1.1.1"]
|
|
39
|
+
});
|
|
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 = ({
|
|
42
|
+
p: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd97n,
|
|
43
|
+
n: 0xffffffffffffffffffffffffffffffff6c611070995ad10045841b09b761b893n,
|
|
44
|
+
a: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd94n,
|
|
45
|
+
b: 0xa6n,
|
|
46
|
+
Gx: 1n,
|
|
47
|
+
Gy: 0x8d91e471e0989cda27df505a453f2b7635294f2ddf23e3b122acc99c9e9f1e14n,
|
|
48
|
+
h: 1n,
|
|
49
|
+
length: 32,
|
|
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
|
+
});
|
|
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 = ({
|
|
54
|
+
p: 0x8000000000000000000000000000000000000000000000000000000000000c99n,
|
|
55
|
+
n: 0x800000000000000000000000000000015f700cfff1a624e5e497161bcc8a198fn,
|
|
56
|
+
a: 0x8000000000000000000000000000000000000000000000000000000000000c96n,
|
|
57
|
+
b: 0x3e1af419a269a5f866a7d3c25c3df80ae979259373ff2b182f49d4ce7e1bbc8bn,
|
|
58
|
+
Gx: 1n,
|
|
59
|
+
Gy: 0x3fa8124359f96680b83d1c3eb2c070e5c545c9858d03ecfb744bf8d717717efcn,
|
|
60
|
+
h: 1n,
|
|
61
|
+
length: 32,
|
|
62
|
+
oids: ["1.2.643.7.1.2.1.1.3", "1.2.643.2.2.35.2"]
|
|
63
|
+
});
|
|
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 = ({
|
|
66
|
+
p: 0x9b9f605f5a858107ab1ec85e6b41c8aacf846e86789051d37998f7b9022d759bn,
|
|
67
|
+
n: 0x9b9f605f5a858107ab1ec85e6b41c8aa582ca3511eddfb74f02f3a6598980bb9n,
|
|
68
|
+
a: 0x9b9f605f5a858107ab1ec85e6b41c8aacf846e86789051d37998f7b9022d7598n,
|
|
69
|
+
b: 0x0805an,
|
|
70
|
+
Gx: 0n,
|
|
71
|
+
Gy: 0x41ece55743711a8c3cbf3783cd08c0ee4d4dc440d4641a8f366e550dfdb3bb67n,
|
|
72
|
+
h: 1n,
|
|
73
|
+
length: 32,
|
|
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
|
+
});
|
|
76
|
+
/** GOST R 34.10-2012 512 bit test param set */
|
|
77
|
+
export const ID_GOSTR3410_2012_512_TEST_PARAM_SET = ({
|
|
78
|
+
p: 0x4531acd1fe0023c7550d267b6b2fee80922b14b2ffb90f04d4eb7c09b5d2d15df1d852741af4704a0458047e80e4546d35b8336fac224dd81664bbf528be6373n,
|
|
79
|
+
n: 0x4531acd1fe0023c7550d267b6b2fee80922b14b2ffb90f04d4eb7c09b5d2d15da82f2d7ecb1dbac719905c5eecc423f1d86e25edbe23c595d644aaf187e6e6dfn,
|
|
80
|
+
a: 7n,
|
|
81
|
+
b: 0x1cff0806a31116da29d8cfa54e57eb748bc5f377e49400fdd788b649eca1ac4361834013b2ad7322480a89ca58e0cf74bc9e540c2add6897fad0a3084f302adcn,
|
|
82
|
+
Gx: 0x24d19cc64572ee30f396bf6ebbfd7a6c5213b3b3d7057cc825f91093a68cd762fd60611262cd838dc6b60aa7eee804e28bc849977fac33b4b530f1b120248a9an,
|
|
83
|
+
Gy: 0x2bb312a43bd2ce6e0d020613c857acddcfbf061e91e5f2c3f32447c259f39b2c83ab156d77f1496bf7eb3351e1ee4e43dc1a18b91b24640b6dbb92cb1add371en,
|
|
84
|
+
h: 1n,
|
|
85
|
+
length: 64,
|
|
86
|
+
oids: ["1.2.643.7.1.2.1.2.0"]
|
|
87
|
+
});
|
|
88
|
+
/** GOST R 34.10-2012 512 bit `A` param set */
|
|
89
|
+
export const ID_GOSTR3410_2012_512_PARAM_SET_A = ({
|
|
90
|
+
p: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdc7n,
|
|
91
|
+
n: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff27e69532f48d89116ff22b8d4e0560609b4b38abfad2b85dcacdb1411f10b275n,
|
|
92
|
+
a: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdc4n,
|
|
93
|
+
b: 0xe8c2505dedfc86ddc1bd0b2b6667f1da34b82574761cb0e879bd081cfd0b6265ee3cb090f30d27614cb4574010da90dd862ef9d4ebee4761503190785a71c760n,
|
|
94
|
+
Gx: 3n,
|
|
95
|
+
Gy: 0x7503cfe87a836ae3a61b8816e25450e6ce5e1c93acf1abc1778064fdcbefa921df1626be4fd036e93d75e6a50e3a41e98028fe5fc235f5b889a589cb5215f2a4n,
|
|
96
|
+
h: 1n,
|
|
97
|
+
length: 64,
|
|
98
|
+
oids: ["1.2.643.7.1.2.1.2.1"]
|
|
99
|
+
});
|
|
100
|
+
/** GOST R 34.10-2012 512 bit `B` param set */
|
|
101
|
+
export const ID_GOSTR3410_2012_512_PARAM_SET_B = ({
|
|
102
|
+
p: 0x8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006fn,
|
|
103
|
+
n: 0x800000000000000000000000000000000000000000000000000000000000000149a1ec142565a545acfdb77bd9d40cfa8b996712101bea0ec6346c54374f25bdn,
|
|
104
|
+
a: 0x8000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006cn,
|
|
105
|
+
b: 0x687d1b459dc841457e3e06cf6f5e2517b97c7d614af138bcbf85dc806c4b289f3e965d2db1416d217f8b276fad1ab69c50f78bee1fa3106efb8ccbc7c5140116n,
|
|
106
|
+
Gx: 2n,
|
|
107
|
+
Gy: 0x1a8f7eda389b094c2c071e3647a8940f3c123b697578c213be6dd9e6c8ec7335dcb228fd1edf4a39152cbcaaf8c0398828041055f94ceeec7e21340780fe41bdn,
|
|
108
|
+
h: 1n,
|
|
109
|
+
length: 64,
|
|
110
|
+
oids: ["1.2.643.7.1.2.1.2.2"]
|
|
111
|
+
});
|
|
112
|
+
/** GOST R 34.10-2012 512 bit `C` param set */
|
|
113
|
+
export const ID_GOSTR3410_2012_512_PARAM_SET_C = ({
|
|
114
|
+
p: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdc7n,
|
|
115
|
+
n: 0x3fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc98cdba46506ab004c33a9ff5147502cc8eda9e7a769a12694623cef47f023edn,
|
|
116
|
+
a: 0xdc9203e514a721875485a529d2c722fb187bc8980eb866644de41c68e143064546e861c0e2c9edd92ade71f46fcf50ff2ad97f951fda9f2a2eb6546f39689bd3n,
|
|
117
|
+
b: 0xb4c4ee28cebc6c2c8ac12952cf37f16ac7efb6a9f69f4b57ffda2e4f0de5ade038cbc2fff719d2c18de0284b8bfef3b52b8cc7a5f5bf0a3c8d2319a5312557e1n,
|
|
118
|
+
Gx: 0xe2e31edfc23de7bdebe241ce593ef5de2295b7a9cbaef021d385f7074cea043aa27272a7ae602bf2a7b9033db9ed3610c6fb85487eae97aac5bc7928c1950148n,
|
|
119
|
+
Gy: 0xf5ce40d95b5eb899abbccff5911cb8577939804d6527378b8c108c3d2090ff9be18e2d33e3021ed2ef32d85822423b6304f726aa854bae07d0396e9a9addc40fn,
|
|
120
|
+
h: 4n,
|
|
121
|
+
e: 1n,
|
|
122
|
+
d: 0x9e4f5d8c017d8d9f13a5cf3cdf5bfe4dab402d54198e31ebde28a0621050439ca6b39e0a515c06b304e2ce43e79e369e91a0cfc2bc2a22b4ca302dbb33ee7550n,
|
|
123
|
+
length: 64,
|
|
124
|
+
st: [0x186c289cffa09c983b168c30c829006c952ff4aaf99c73850875d7e77bebef18d653187d6ba8fe533ec74c6f061872585b97cc0f50f57752cd73f4913304621en, 0x9a628f975594ecefd89ba28a2539ffb79c8ab238aeed0851fa5c1abb02b80b44c6734501b83a011dd625cd0b5145091a6d9acd4b1f5c5b1e21b2b249ddfd1271n],
|
|
125
|
+
oids: ["1.2.643.7.1.2.1.2.3"]
|
|
126
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AffinePoint } from "@noble/curves/abstract/curve.js";
|
|
2
|
+
import type { GostCurveParameters } from "./const.js";
|
|
3
|
+
/**
|
|
4
|
+
* Compute parameters (`s`, `t`) for conversion
|
|
5
|
+
* @param curve Curve to use
|
|
6
|
+
*/
|
|
7
|
+
export declare const computeST: (curve: GostCurveParameters) => bigint[];
|
|
8
|
+
/**
|
|
9
|
+
* Convert Twisted Edwards point (`u`, `v`) to Weierstrass (`x`, `y`)
|
|
10
|
+
* @param curve Curve to use
|
|
11
|
+
* @param point Twisted Edwards point
|
|
12
|
+
*/
|
|
13
|
+
export declare const uv2xy: (curve: GostCurveParameters, point: AffinePoint<bigint>) => AffinePoint<bigint>;
|
|
14
|
+
/**
|
|
15
|
+
* Convert Weierstrass point (`x`, `y`) to Twisted Edwards (`u`, `v`)
|
|
16
|
+
* @param curve Curve to use
|
|
17
|
+
* @param point Weierstrass point
|
|
18
|
+
*/
|
|
19
|
+
export declare const xy2uv: (curve: GostCurveParameters, point: AffinePoint<bigint>) => AffinePoint<bigint>;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Field } from "@noble/curves/abstract/modular.js";
|
|
2
|
+
/**
|
|
3
|
+
* Compute parameters (`s`, `t`) for conversion
|
|
4
|
+
* @param curve Curve to use
|
|
5
|
+
*/
|
|
6
|
+
export const computeST = (curve) => {
|
|
7
|
+
if (!curve.e || !curve.d)
|
|
8
|
+
throw new Error("No Twisted Edwards parameters");
|
|
9
|
+
if (curve.st && curve.st.length != 0)
|
|
10
|
+
return curve.st;
|
|
11
|
+
const Fp = Field(curve.p);
|
|
12
|
+
return [Fp.div(Fp.sub(curve.e, curve.d), 4n), Fp.div(Fp.add(curve.e, curve.d), 6n)];
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Convert Twisted Edwards point (`u`, `v`) to Weierstrass (`x`, `y`)
|
|
16
|
+
* @param curve Curve to use
|
|
17
|
+
* @param point Twisted Edwards point
|
|
18
|
+
*/
|
|
19
|
+
export const uv2xy = (curve, point) => {
|
|
20
|
+
const Fp = Field(curve.p);
|
|
21
|
+
const [s, t] = computeST(curve);
|
|
22
|
+
const s1v = Fp.mul(s, Fp.add(1n, point.y)), onev = Fp.sub(1n, point.y);
|
|
23
|
+
return { x: Fp.add(t, Fp.div(s1v, onev)), y: Fp.div(s1v, Fp.mul(point.x, onev)) };
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Convert Weierstrass point (`x`, `y`) to Twisted Edwards (`u`, `v`)
|
|
27
|
+
* @param curve Curve to use
|
|
28
|
+
* @param point Weierstrass point
|
|
29
|
+
*/
|
|
30
|
+
export const xy2uv = (curve, point) => {
|
|
31
|
+
const Fp = Field(curve.p);
|
|
32
|
+
const [s, t] = computeST(curve);
|
|
33
|
+
const xt = Fp.sub(point.x, t);
|
|
34
|
+
return { x: Fp.div(xt, point.y), y: Fp.div(Fp.sub(xt, s), Fp.add(xt, s)) };
|
|
35
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type TArg, type TRet } from "@noble/curves/utils.js";
|
|
2
|
+
import { type GostCurveParameters } from "./const.js";
|
|
3
|
+
/**
|
|
4
|
+
* Generate public key from private.
|
|
5
|
+
* @param parameters Curve parameters
|
|
6
|
+
* @param prv Private key
|
|
7
|
+
* @returns {TRet<Uint8Array>} Uncompressed public key in ANSI X9.62 format
|
|
8
|
+
*/
|
|
9
|
+
export declare const getPublicKey: (parameters: GostCurveParameters, prv: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
10
|
+
/**
|
|
11
|
+
* Generate signature of provided digest
|
|
12
|
+
* @param parameters Curve parameters
|
|
13
|
+
* @param prv Private key
|
|
14
|
+
* @param digest Digest to sign
|
|
15
|
+
* @param rand Optional. Predefined random data for `r` and `k` generation
|
|
16
|
+
* @returns {TRet<Uint8Array>} Concated `r` and `s`
|
|
17
|
+
*/
|
|
18
|
+
export declare const sign: (parameters: GostCurveParameters, prv: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
19
|
+
/**
|
|
20
|
+
* Verify signature of provided digest
|
|
21
|
+
* @param parameters Curve parameters
|
|
22
|
+
* @param pub Public key
|
|
23
|
+
* @param digest Digest to verify
|
|
24
|
+
* @param signature Signature (Concated `r` and `s`)
|
|
25
|
+
*/
|
|
26
|
+
export declare const verify: (parameters: GostCurveParameters, pub: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
|
|
27
|
+
export * from "./const.js";
|
|
28
|
+
export * from "./vko.js";
|
|
29
|
+
export * from "./conversion.js";
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { bytesToNumberBE, concatBytes, numberToBytesBE, randomBytes } from "@noble/curves/utils.js";
|
|
2
|
+
import {} from "./const.js";
|
|
3
|
+
import { mod } from "@noble/curves/abstract/modular.js";
|
|
4
|
+
import { weierstrass } from "@noble/curves/abstract/weierstrass.js";
|
|
5
|
+
/**
|
|
6
|
+
* Generate public key from private.
|
|
7
|
+
* @param parameters Curve parameters
|
|
8
|
+
* @param prv Private key
|
|
9
|
+
* @returns {TRet<Uint8Array>} Uncompressed public key in ANSI X9.62 format
|
|
10
|
+
*/
|
|
11
|
+
export const getPublicKey = (parameters, prv) => weierstrass(parameters).BASE.multiply(bytesToNumberBE(prv)).toBytes(false);
|
|
12
|
+
/**
|
|
13
|
+
* Generate signature of provided digest
|
|
14
|
+
* @param parameters Curve parameters
|
|
15
|
+
* @param prv Private key
|
|
16
|
+
* @param digest Digest to sign
|
|
17
|
+
* @param rand Optional. Predefined random data for `r` and `k` generation
|
|
18
|
+
* @returns {TRet<Uint8Array>} Concated `r` and `s`
|
|
19
|
+
*/
|
|
20
|
+
export const sign = (parameters, prv, digest, rand) => {
|
|
21
|
+
const size = parameters.length;
|
|
22
|
+
const curve = weierstrass(parameters);
|
|
23
|
+
const Fn = curve.Fn;
|
|
24
|
+
let e = Fn.fromBytes(digest);
|
|
25
|
+
if (e === 0n)
|
|
26
|
+
e = 1n;
|
|
27
|
+
const prvNum = Fn.fromBytes(prv);
|
|
28
|
+
while (true) {
|
|
29
|
+
rand ||= randomBytes(size);
|
|
30
|
+
const k = mod(bytesToNumberBE(rand), parameters.n);
|
|
31
|
+
if (k === 0n)
|
|
32
|
+
continue;
|
|
33
|
+
try {
|
|
34
|
+
let { x: r } = curve.BASE.multiply(k);
|
|
35
|
+
r = Fn.create(r);
|
|
36
|
+
if (r === 0n)
|
|
37
|
+
continue;
|
|
38
|
+
const s = Fn.add(Fn.mul(r, prvNum), Fn.mul(k, e));
|
|
39
|
+
if (s === 0n)
|
|
40
|
+
continue;
|
|
41
|
+
return concatBytes(numberToBytesBE(r, parameters.length), numberToBytesBE(s, parameters.length));
|
|
42
|
+
}
|
|
43
|
+
catch (e) {
|
|
44
|
+
if (e instanceof Error && e.message === "invalid scalar: out of range")
|
|
45
|
+
continue;
|
|
46
|
+
throw e;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Verify signature of provided digest
|
|
52
|
+
* @param parameters Curve parameters
|
|
53
|
+
* @param pub Public key
|
|
54
|
+
* @param digest Digest to verify
|
|
55
|
+
* @param signature Signature (Concated `r` and `s`)
|
|
56
|
+
*/
|
|
57
|
+
export const verify = (parameters, pub, digest, signature) => {
|
|
58
|
+
const size = parameters.length;
|
|
59
|
+
const curve = weierstrass(parameters);
|
|
60
|
+
const Fn = curve.Fn;
|
|
61
|
+
if (signature.length != size * 2)
|
|
62
|
+
throw new Error("Invalid signature");
|
|
63
|
+
const r = bytesToNumberBE(signature.slice(0, size));
|
|
64
|
+
const s = bytesToNumberBE(signature.slice(size));
|
|
65
|
+
if (r <= 0 || r >= parameters.n || s <= 0 || s >= parameters.n)
|
|
66
|
+
return false;
|
|
67
|
+
let e = Fn.fromBytes(digest);
|
|
68
|
+
if (e === 0n)
|
|
69
|
+
e = 1n;
|
|
70
|
+
const v = Fn.inv(e);
|
|
71
|
+
const z1 = Fn.mul(s, v), z2 = Fn.mul(r, v);
|
|
72
|
+
let P, Q;
|
|
73
|
+
try {
|
|
74
|
+
P = curve.BASE.multiply(z1);
|
|
75
|
+
Q = curve.fromBytes(pub).multiply(z2).negate();
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
return Fn.create(P.add(Q).x) === r;
|
|
81
|
+
};
|
|
82
|
+
export * from "./const.js";
|
|
83
|
+
export * from "./vko.js";
|
|
84
|
+
export * from "./conversion.js";
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { GostCurveParameters } from "./const.js";
|
|
2
|
+
import { type TArg, type TRet } from "@noble/curves/utils.js";
|
|
3
|
+
/**
|
|
4
|
+
* Key agreement function
|
|
5
|
+
* @param parameters Curve parameters
|
|
6
|
+
* @param prv Private key
|
|
7
|
+
* @param pub Public key
|
|
8
|
+
* @param ukm User keying material (aka salt)
|
|
9
|
+
* @returns {TRet<Uint8Array>} Shared key
|
|
10
|
+
*/
|
|
11
|
+
export declare const kek: (parameters: GostCurveParameters, prv: TArg<Uint8Array>, pub: TArg<Uint8Array>, ukm: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
12
|
+
/**
|
|
13
|
+
* Key agreement function over GOST R 34.11-94 hash
|
|
14
|
+
* @param parameters Curve parameters
|
|
15
|
+
* @param prv Private key
|
|
16
|
+
* @param pub Public key
|
|
17
|
+
* @param ukm User keying material (aka salt)
|
|
18
|
+
* @returns {TRet<Uint8Array>} Shared key
|
|
19
|
+
*/
|
|
20
|
+
export declare const kek_34102001: (parameters: GostCurveParameters, prv: TArg<Uint8Array>, pub: TArg<Uint8Array>, ukm: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
21
|
+
/**
|
|
22
|
+
* Key agreement function over Streebog (GOST R 34.11-2012) 256 bit hash
|
|
23
|
+
* @param parameters Curve parameters
|
|
24
|
+
* @param prv Private key
|
|
25
|
+
* @param pub Public key
|
|
26
|
+
* @param ukm User keying material (aka salt)
|
|
27
|
+
* @returns {TRet<Uint8Array>} Shared key
|
|
28
|
+
*/
|
|
29
|
+
export declare const kek_34102012256: (parameters: GostCurveParameters, prv: TArg<Uint8Array>, pub: TArg<Uint8Array>, ukm: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
30
|
+
/**
|
|
31
|
+
* Key agreement function over Streebog (GOST R 34.11-2012) 512 bit hash
|
|
32
|
+
* @param parameters Curve parameters
|
|
33
|
+
* @param prv Private key
|
|
34
|
+
* @param pub Public key
|
|
35
|
+
* @param ukm User keying material (aka salt)
|
|
36
|
+
* @returns {TRet<Uint8Array>} Shared key
|
|
37
|
+
*/
|
|
38
|
+
export declare const kek_34102012512: (parameters: GostCurveParameters, prv: TArg<Uint8Array>, pub: TArg<Uint8Array>, ukm: TArg<Uint8Array>) => TRet<Uint8Array>;
|
package/gost3410/vko.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Field } from "@noble/curves/abstract/modular.js";
|
|
2
|
+
import { weierstrass } from "@noble/curves/abstract/weierstrass.js";
|
|
3
|
+
import { gost341194 } from "../gost341194";
|
|
4
|
+
import { streebog256, streebog512 } from "../streebog/index.js";
|
|
5
|
+
import { bytesToNumberBE, concatBytes, numberToBytesLE } from "@noble/curves/utils.js";
|
|
6
|
+
/**
|
|
7
|
+
* Key agreement function
|
|
8
|
+
* @param parameters Curve parameters
|
|
9
|
+
* @param prv Private key
|
|
10
|
+
* @param pub Public key
|
|
11
|
+
* @param ukm User keying material (aka salt)
|
|
12
|
+
* @returns {TRet<Uint8Array>} Shared key
|
|
13
|
+
*/
|
|
14
|
+
export const kek = (parameters, prv, pub, ukm) => {
|
|
15
|
+
const Fn = Field(parameters.n);
|
|
16
|
+
const key = weierstrass(parameters).fromBytes(pub)
|
|
17
|
+
.multiply(bytesToNumberBE(prv))
|
|
18
|
+
.multiply(Fn.mulN(parameters.h, bytesToNumberBE(ukm)));
|
|
19
|
+
return concatBytes(numberToBytesLE(key.x, parameters.length), numberToBytesLE(key.y, parameters.length));
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Key agreement function over GOST R 34.11-94 hash
|
|
23
|
+
* @param parameters Curve parameters
|
|
24
|
+
* @param prv Private key
|
|
25
|
+
* @param pub Public key
|
|
26
|
+
* @param ukm User keying material (aka salt)
|
|
27
|
+
* @returns {TRet<Uint8Array>} Shared key
|
|
28
|
+
*/
|
|
29
|
+
export const kek_34102001 = (parameters, prv, pub, ukm) => gost341194(kek(parameters, prv, pub, ukm));
|
|
30
|
+
/**
|
|
31
|
+
* Key agreement function over Streebog (GOST R 34.11-2012) 256 bit hash
|
|
32
|
+
* @param parameters Curve parameters
|
|
33
|
+
* @param prv Private key
|
|
34
|
+
* @param pub Public key
|
|
35
|
+
* @param ukm User keying material (aka salt)
|
|
36
|
+
* @returns {TRet<Uint8Array>} Shared key
|
|
37
|
+
*/
|
|
38
|
+
export const kek_34102012256 = (parameters, prv, pub, ukm) => streebog256(kek(parameters, prv, pub, ukm));
|
|
39
|
+
/**
|
|
40
|
+
* Key agreement function over Streebog (GOST R 34.11-2012) 512 bit hash
|
|
41
|
+
* @param parameters Curve parameters
|
|
42
|
+
* @param prv Private key
|
|
43
|
+
* @param pub Public key
|
|
44
|
+
* @param ukm User keying material (aka salt)
|
|
45
|
+
* @returns {TRet<Uint8Array>} Shared key
|
|
46
|
+
*/
|
|
47
|
+
export const kek_34102012512 = (parameters, prv, pub, ukm) => streebog512(kek(parameters, prv, pub, ukm));
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
|
|
2
|
+
/** GOST R 34.11-94 hash function */
|
|
3
|
+
export declare class Gost341194 implements Hash<Gost341194> {
|
|
4
|
+
private data;
|
|
5
|
+
private sbox;
|
|
6
|
+
readonly blockLen: number;
|
|
7
|
+
readonly outputLen = 32;
|
|
8
|
+
readonly canXOF = false;
|
|
9
|
+
/** GOST R 34.11-94 hash function */
|
|
10
|
+
constructor(data?: TArg<Uint8Array>, sbox?: TArg<Uint8Array>[]);
|
|
11
|
+
/** Create hash instance */
|
|
12
|
+
static create(): Gost341194;
|
|
13
|
+
destroy(): void;
|
|
14
|
+
clone(): Gost341194;
|
|
15
|
+
_cloneInto(to?: Gost341194): Gost341194;
|
|
16
|
+
update(data: TArg<Uint8Array>): this;
|
|
17
|
+
digestInto(buf: TArg<Uint8Array>): void;
|
|
18
|
+
digest(): TRet<Uint8Array>;
|
|
19
|
+
}
|
|
20
|
+
/** GOST R 34.11-94 hash function */
|
|
21
|
+
export declare const gost341194: (msg: TArg<Uint8Array>, sbox?: TArg<Uint8Array>[]) => TRet<Uint8Array>;
|
|
22
|
+
/** DSTU GOST 34.311-95 */
|
|
23
|
+
export declare const gost3431195: (msg: TArg<Uint8Array>) => TRet<Uint8Array>;
|