@li0ard/dstu 0.1.8 → 0.1.9
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 +3 -1
- package/dstu4145/ec/wnaf.d.ts +2 -1
- package/dstu4145/ecies.d.ts +82 -0
- package/dstu4145/ecies.js +62 -0
- package/dstu4145/gf2m/index.js +2 -11
- package/dstu4145/gf2m/onb.js +30 -16
- package/dstu4145/gf2m/utils.d.ts +1 -1
- package/dstu4145/gf2m/utils.js +12 -8
- package/dstu4145/index.d.ts +16 -225
- package/dstu4145/index.js +7 -22
- package/dstu9041/curve.d.ts +4 -12
- package/dstu9041/curve.js +3 -1
- package/dstu9041/index.d.ts +2 -8
- package/dstu9041/index.js +1 -1
- package/iso15946.d.ts +3 -0
- package/iso15946.js +49 -0
- package/modes/kw.d.ts +2 -6
- package/modes/xts.d.ts +2 -6
- package/modes/xts.js +2 -2
- package/package.json +4 -4
- package/strumok/index.d.ts +2 -1
- package/types.d.ts +77 -0
package/README.md
CHANGED
|
@@ -20,7 +20,9 @@ npm i @li0ard/dstu
|
|
|
20
20
|
|
|
21
21
|
## Supported algorithms
|
|
22
22
|
|
|
23
|
-
- Curves and ECDSA (DSTU 4145-2002
|
|
23
|
+
- Curves and ECDSA (DSTU 4145-2002)
|
|
24
|
+
- Supports polynomial and optimal normal basis
|
|
25
|
+
- Supports ECDH and encryption
|
|
24
26
|
- Kalyna cipher (DSTU 7624:2014)
|
|
25
27
|
- Kupyna hash function (DSTU 7564:2014)
|
|
26
28
|
- Supports KMAC
|
package/dstu4145/ec/wnaf.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
+
import type { TRet } from "@noble/hashes/utils.js";
|
|
1
2
|
import BN from "bn.js";
|
|
2
3
|
export declare const bitLength: (n: number) => number;
|
|
3
|
-
export declare const windowNaf: (width: number, k: BN) => Int32Array
|
|
4
|
+
export declare const windowNaf: (width: number, k: BN) => TRet<Int32Array>;
|
|
4
5
|
export declare const getWindowSize: (bits: number) => number;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { type CHash, type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
|
|
2
|
+
import type { ECDSA } from "../types.js";
|
|
3
|
+
/** Encrypted message */
|
|
4
|
+
export type EncryptedMessage = {
|
|
5
|
+
/** User keying material (UKM) for KDF */
|
|
6
|
+
ukm?: TArg<Uint8Array>;
|
|
7
|
+
/** Encryption IV */
|
|
8
|
+
iv: TArg<Uint8Array>;
|
|
9
|
+
/** Wrapped encryption key */
|
|
10
|
+
wcek: TArg<Uint8Array>;
|
|
11
|
+
/** Encrypted message */
|
|
12
|
+
data: TArg<Uint8Array>;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* ECIES schema over DSTU 4145-2002.
|
|
16
|
+
* @param curve - Curve to use.
|
|
17
|
+
* @param hash - Hash function to use. (Output length must be 32 bytes)
|
|
18
|
+
* @param withCofactor - Whether to multiply shared key by curve cofactor (See {@link types!ECDSA.getSharedSecret ECDSA.getSharedSecret})
|
|
19
|
+
* @param useDstu9311 - Whether to use DSTU 9311:2024 instead of Kalyna-256?
|
|
20
|
+
*/
|
|
21
|
+
export declare const dstu4145Encrypter: (curve: ECDSA, hash: CHash<Hash<any>>, withCofactor?: boolean, useDstu9311?: boolean) => Readonly<{
|
|
22
|
+
/**
|
|
23
|
+
* Encrypt message
|
|
24
|
+
* @param secretKeyA - Sender secret key.
|
|
25
|
+
* @param publicKeyB - Recipient public key.
|
|
26
|
+
* @param plaintext - Message to encrypt.
|
|
27
|
+
*/
|
|
28
|
+
encrypt: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, plaintext: TArg<Uint8Array>) => EncryptedMessage;
|
|
29
|
+
/**
|
|
30
|
+
* Decrypt message
|
|
31
|
+
* @param secretKeyB - Recipient secret key.
|
|
32
|
+
* @param publicKeyA - Sender public key.
|
|
33
|
+
* @param encryptedMessage - Encrypted message object.
|
|
34
|
+
*/
|
|
35
|
+
decrypt: (secretKeyB: TArg<Uint8Array>, publicKeyA: TArg<Uint8Array>, encryptedMessage: EncryptedMessage) => TRet<Uint8Array>;
|
|
36
|
+
}>;
|
|
37
|
+
/**
|
|
38
|
+
* ECIES schema over DSTU 4145-2002.
|
|
39
|
+
*
|
|
40
|
+
* Uses GOST 34.311-95 as hash function.
|
|
41
|
+
* @param curve - Curve to use.
|
|
42
|
+
* @param withCofactor - Whether to multiply shared key by curve cofactor (See {@link types!ECDSA.getSharedSecret ECDSA.getSharedSecret})
|
|
43
|
+
*/
|
|
44
|
+
export declare const dstu4145EncrypterWithGost: (curve: ECDSA, withCofactor?: boolean) => Readonly<{
|
|
45
|
+
/**
|
|
46
|
+
* Encrypt message
|
|
47
|
+
* @param secretKeyA - Sender secret key.
|
|
48
|
+
* @param publicKeyB - Recipient public key.
|
|
49
|
+
* @param plaintext - Message to encrypt.
|
|
50
|
+
*/
|
|
51
|
+
encrypt: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, plaintext: TArg<Uint8Array>) => EncryptedMessage;
|
|
52
|
+
/**
|
|
53
|
+
* Decrypt message
|
|
54
|
+
* @param secretKeyB - Recipient secret key.
|
|
55
|
+
* @param publicKeyA - Sender public key.
|
|
56
|
+
* @param encryptedMessage - Encrypted message object.
|
|
57
|
+
*/
|
|
58
|
+
decrypt: (secretKeyB: TArg<Uint8Array>, publicKeyA: TArg<Uint8Array>, encryptedMessage: EncryptedMessage) => TRet<Uint8Array>;
|
|
59
|
+
}>;
|
|
60
|
+
/**
|
|
61
|
+
* ECIES schema over DSTU 4145-2002.
|
|
62
|
+
*
|
|
63
|
+
* Uses Kupyna-256 as hash function.
|
|
64
|
+
* @param curve - Curve to use.
|
|
65
|
+
* @param withCofactor - Whether to multiply shared key by curve cofactor (See {@link types!ECDSA.getSharedSecret ECDSA.getSharedSecret})
|
|
66
|
+
*/
|
|
67
|
+
export declare const dstu4145EncrypterWithKupyna: (curve: ECDSA, withCofactor?: boolean) => Readonly<{
|
|
68
|
+
/**
|
|
69
|
+
* Encrypt message
|
|
70
|
+
* @param secretKeyA - Sender secret key.
|
|
71
|
+
* @param publicKeyB - Recipient public key.
|
|
72
|
+
* @param plaintext - Message to encrypt.
|
|
73
|
+
*/
|
|
74
|
+
encrypt: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, plaintext: TArg<Uint8Array>) => EncryptedMessage;
|
|
75
|
+
/**
|
|
76
|
+
* Decrypt message
|
|
77
|
+
* @param secretKeyB - Recipient secret key.
|
|
78
|
+
* @param publicKeyA - Sender public key.
|
|
79
|
+
* @param encryptedMessage - Encrypted message object.
|
|
80
|
+
*/
|
|
81
|
+
decrypt: (secretKeyB: TArg<Uint8Array>, publicKeyA: TArg<Uint8Array>, encryptedMessage: EncryptedMessage) => TRet<Uint8Array>;
|
|
82
|
+
}>;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { randomBytes } from "@noble/hashes/utils.js";
|
|
2
|
+
import { Dstu9311, gost3431195 } from "../dstu9311/index.js";
|
|
3
|
+
import { Kalyna256 } from "../kalyna/index.js";
|
|
4
|
+
import { keyWrap } from "../keywrap.js";
|
|
5
|
+
import { cfb } from "../modes/cfb.js";
|
|
6
|
+
import { kupyna256 } from "../kupyna/index.js";
|
|
7
|
+
import { iso15946_kdf } from "../iso15946.js";
|
|
8
|
+
/**
|
|
9
|
+
* ECIES schema over DSTU 4145-2002.
|
|
10
|
+
* @param curve - Curve to use.
|
|
11
|
+
* @param hash - Hash function to use. (Output length must be 32 bytes)
|
|
12
|
+
* @param withCofactor - Whether to multiply shared key by curve cofactor (See {@link types!ECDSA.getSharedSecret ECDSA.getSharedSecret})
|
|
13
|
+
* @param useDstu9311 - Whether to use DSTU 9311:2024 instead of Kalyna-256?
|
|
14
|
+
*/
|
|
15
|
+
export const dstu4145Encrypter = (curve, hash, withCofactor = true, useDstu9311 = false) => {
|
|
16
|
+
const oid = useDstu9311 ? "1.2.804.2.1.1.1.1.1.1.5" : "1.2.804.2.1.1.1.1.1.3.11";
|
|
17
|
+
const Cipher = useDstu9311 ? Dstu9311 : Kalyna256;
|
|
18
|
+
const macLength = useDstu9311 ? 8 : 32;
|
|
19
|
+
const getSharedSecret = (sharedKey, ukm) => iso15946_kdf(hash, oid, sharedKey, ukm);
|
|
20
|
+
return Object.freeze({
|
|
21
|
+
/**
|
|
22
|
+
* Encrypt message
|
|
23
|
+
* @param secretKeyA - Sender secret key.
|
|
24
|
+
* @param publicKeyB - Recipient public key.
|
|
25
|
+
* @param plaintext - Message to encrypt.
|
|
26
|
+
*/
|
|
27
|
+
encrypt: (secretKeyA, publicKeyB, plaintext) => {
|
|
28
|
+
const cek = randomBytes(32), ukm = randomBytes(64), iv = randomBytes(macLength);
|
|
29
|
+
const sharedKey = getSharedSecret(curve.getSharedSecret(secretKeyA, publicKeyB, withCofactor), ukm);
|
|
30
|
+
const wcek = keyWrap(sharedKey, useDstu9311).wrap(cek, iv);
|
|
31
|
+
const data = cfb(new Cipher(cek), iv).encrypt(plaintext);
|
|
32
|
+
return { ukm, iv, wcek, data };
|
|
33
|
+
},
|
|
34
|
+
/**
|
|
35
|
+
* Decrypt message
|
|
36
|
+
* @param secretKeyB - Recipient secret key.
|
|
37
|
+
* @param publicKeyA - Sender public key.
|
|
38
|
+
* @param encryptedMessage - Encrypted message object.
|
|
39
|
+
*/
|
|
40
|
+
decrypt: (secretKeyB, publicKeyA, encryptedMessage) => {
|
|
41
|
+
const sharedKey = getSharedSecret(curve.getSharedSecret(secretKeyB, publicKeyA, withCofactor), encryptedMessage.ukm);
|
|
42
|
+
const cek = keyWrap(sharedKey, useDstu9311).unwrap(encryptedMessage.wcek);
|
|
43
|
+
return cfb(new Cipher(cek), encryptedMessage.iv).decrypt(encryptedMessage.data);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* ECIES schema over DSTU 4145-2002.
|
|
49
|
+
*
|
|
50
|
+
* Uses GOST 34.311-95 as hash function.
|
|
51
|
+
* @param curve - Curve to use.
|
|
52
|
+
* @param withCofactor - Whether to multiply shared key by curve cofactor (See {@link types!ECDSA.getSharedSecret ECDSA.getSharedSecret})
|
|
53
|
+
*/
|
|
54
|
+
export const dstu4145EncrypterWithGost = (curve, withCofactor) => dstu4145Encrypter(curve, gost3431195, withCofactor, true);
|
|
55
|
+
/**
|
|
56
|
+
* ECIES schema over DSTU 4145-2002.
|
|
57
|
+
*
|
|
58
|
+
* Uses Kupyna-256 as hash function.
|
|
59
|
+
* @param curve - Curve to use.
|
|
60
|
+
* @param withCofactor - Whether to multiply shared key by curve cofactor (See {@link types!ECDSA.getSharedSecret ECDSA.getSharedSecret})
|
|
61
|
+
*/
|
|
62
|
+
export const dstu4145EncrypterWithKupyna = (curve, withCofactor) => dstu4145Encrypter(curve, kupyna256, withCofactor);
|
package/dstu4145/gf2m/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import BN from "bn.js";
|
|
2
|
-
import {
|
|
2
|
+
import { bn2LE, invWords, le2BN, modWords, mulWords, sqrWords, WORD_BITS } from "./utils.js";
|
|
3
3
|
/** Compute curve modulo */
|
|
4
4
|
export const computeMod = (m, ks) => {
|
|
5
5
|
const modulo = new BN(0);
|
|
@@ -24,16 +24,7 @@ export const createField = (m, ks) => {
|
|
|
24
24
|
? sqr(x)
|
|
25
25
|
: modw(mulWords(bn2LE(x), bn2LE(v)));
|
|
26
26
|
const div = (x, v) => mul(x, invert(v));
|
|
27
|
-
const sqr = (x) =>
|
|
28
|
-
const bytes = x.toArray();
|
|
29
|
-
const out = new Uint8Array(bytes.length * 2);
|
|
30
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
31
|
-
const v = SQR_PRECOMP[bytes[i]];
|
|
32
|
-
out[2 * i] = (v >> 8) & 0xff;
|
|
33
|
-
out[2 * i + 1] = v & 0xff;
|
|
34
|
-
}
|
|
35
|
-
return modw(be2LEw(out));
|
|
36
|
-
};
|
|
27
|
+
const sqr = (x) => modw(sqrWords(bn2LE(x)));
|
|
37
28
|
const testBit = (x, i) => x.testn(i) ? 1 : 0;
|
|
38
29
|
const invBit = (x, i) => x.setn(i, !x.testn(i));
|
|
39
30
|
const sqrt = (x) => {
|
package/dstu4145/gf2m/onb.js
CHANGED
|
@@ -12,38 +12,52 @@ const decompress_matrix = (compress_mulp, mulp) => {
|
|
|
12
12
|
temp.ishrn(9);
|
|
13
13
|
}
|
|
14
14
|
};
|
|
15
|
+
const bnToBits = (x, m) => {
|
|
16
|
+
const bits = new Uint8Array(m);
|
|
17
|
+
for (let i = 0; i < m; i++)
|
|
18
|
+
bits[i] = x.testn(i) ? 1 : 0;
|
|
19
|
+
return bits;
|
|
20
|
+
};
|
|
21
|
+
const bitsToBN = (bits) => {
|
|
22
|
+
const r = new BN(0);
|
|
23
|
+
for (let j = 0; j < bits.length; j++)
|
|
24
|
+
if (bits[j])
|
|
25
|
+
r.setn(j, 1);
|
|
26
|
+
return r;
|
|
27
|
+
};
|
|
15
28
|
export const init_onb_parameters = (parameters) => {
|
|
16
29
|
if (!parameters.onb)
|
|
17
30
|
throw new Error("Invalid curve: Curve doesn't support ONB");
|
|
18
31
|
const { m, onb, ks } = parameters;
|
|
32
|
+
const m_sub_1 = m - 1;
|
|
19
33
|
const mulp = new Uint16Array(2 * m - 1);
|
|
20
34
|
const compress_mulp = new BN(hexToBytes(onb.matrix), "le");
|
|
21
35
|
decompress_matrix(compress_mulp, mulp);
|
|
22
36
|
const root1 = new BN(hexToBytes(onb.root1), "le"), root2 = new BN(hexToBytes(onb.root2), "le");
|
|
37
|
+
const root2Bits = bnToBits(root2, m);
|
|
38
|
+
const bitAt = (bits, pos) => bits[pos % m];
|
|
23
39
|
const multiplyOnb = (x) => {
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
const yb = (pos) => (root2.testn(pos % m) ? 1 : 0);
|
|
40
|
+
const xBits = bnToBits(x, m);
|
|
41
|
+
const out = new Uint8Array(m);
|
|
27
42
|
for (let j = 0; j < m; j++) {
|
|
28
43
|
let bit = 0;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
const
|
|
44
|
+
const j1 = j + 1;
|
|
45
|
+
for (let i = 0; i < m_sub_1; i++) {
|
|
46
|
+
const i2 = i * 2;
|
|
47
|
+
const t1 = bitAt(root2Bits, mulp[i2] + j1), t2 = bitAt(root2Bits, mulp[i2 + 1] + j1), t3 = bitAt(xBits, i + j1);
|
|
33
48
|
bit ^= (t1 ^ t2) & t3;
|
|
34
49
|
}
|
|
35
|
-
bit ^=
|
|
36
|
-
|
|
37
|
-
r.setn(j, 1);
|
|
50
|
+
bit ^= bitAt(root2Bits, mulp[2 * m - 2] + j1) & bitAt(xBits, m_sub_1 + j1);
|
|
51
|
+
out[j] = bit;
|
|
38
52
|
}
|
|
39
|
-
return
|
|
53
|
+
return bitsToBN(out);
|
|
40
54
|
};
|
|
41
55
|
const reverseBits = (x) => {
|
|
42
|
-
const
|
|
56
|
+
const xBits = bnToBits(x, m);
|
|
57
|
+
const out = new Uint8Array(m);
|
|
43
58
|
for (let i = 0; i < m; i++)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return r;
|
|
59
|
+
out[m_sub_1 - i] = xBits[i];
|
|
60
|
+
return bitsToBN(out);
|
|
47
61
|
};
|
|
48
62
|
const toPb = new Array(m);
|
|
49
63
|
toPb[0] = root1.clone();
|
|
@@ -60,7 +74,7 @@ export const init_onb_parameters = (parameters) => {
|
|
|
60
74
|
const r = new BN(0);
|
|
61
75
|
for (let p = 0; p < m; p++)
|
|
62
76
|
if (x.testn(p))
|
|
63
|
-
r.ixor(toPb[
|
|
77
|
+
r.ixor(toPb[m_sub_1 - p]);
|
|
64
78
|
return r;
|
|
65
79
|
},
|
|
66
80
|
pbToOnb: (x) => {
|
package/dstu4145/gf2m/utils.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export declare const SQR_PRECOMP: Uint16Array<ArrayBuffer>;
|
|
|
4
4
|
export declare const WORD_BITS = 32;
|
|
5
5
|
export declare const bn2LE: (x: BN, numWords?: number) => TRet<Uint32Array>;
|
|
6
6
|
export declare const le2BN: (arr: TArg<Uint32Array>) => BN;
|
|
7
|
-
export declare const be2LEw: (bytes: TArg<Uint8Array>) => TRet<Uint32Array>;
|
|
8
7
|
export declare const mulWords: (a: TArg<Uint32Array>, b: TArg<Uint32Array>) => TRet<Uint32Array>;
|
|
8
|
+
export declare const sqrWords: (xw: TArg<Uint32Array>) => TRet<Uint32Array>;
|
|
9
9
|
export declare const modWords: (a: TArg<Uint32Array>, poly: TArg<Int32Array>) => TRet<Uint32Array>;
|
|
10
10
|
export declare const invWords: (a: TArg<Uint32Array>, poly: TArg<Uint32Array>) => TRet<Uint32Array>;
|
package/dstu4145/gf2m/utils.js
CHANGED
|
@@ -53,14 +53,6 @@ export const le2BN = (arr) => {
|
|
|
53
53
|
}
|
|
54
54
|
return new BN(bytes, "le");
|
|
55
55
|
};
|
|
56
|
-
export const be2LEw = (bytes) => {
|
|
57
|
-
const n = bytes.length, arr = new Uint32Array(Math.max(1, Math.ceil(n / 4)));
|
|
58
|
-
for (let i = 0; i < n; i++) {
|
|
59
|
-
const b = bytes[n - 1 - i];
|
|
60
|
-
arr[i >> 2] |= b << ((i & 3) * 8);
|
|
61
|
-
}
|
|
62
|
-
return arr;
|
|
63
|
-
};
|
|
64
56
|
const wBitLen = (words) => {
|
|
65
57
|
let nz = words.length - 1;
|
|
66
58
|
while (nz >= 0 && words[nz] === 0)
|
|
@@ -163,6 +155,18 @@ export const mulWords = (a, b) => {
|
|
|
163
155
|
}
|
|
164
156
|
return out;
|
|
165
157
|
};
|
|
158
|
+
export const sqrWords = (xw) => {
|
|
159
|
+
const out = new Uint32Array(xw.length * 2);
|
|
160
|
+
for (let j = 0; j < xw.length; j++) {
|
|
161
|
+
const w = xw[j];
|
|
162
|
+
if (w === 0)
|
|
163
|
+
continue;
|
|
164
|
+
const b0 = w & 0xff, b1 = (w >>> 8) & 0xff, b2 = (w >>> 16) & 0xff, b3 = (w >>> 24) & 0xff;
|
|
165
|
+
out[2 * j] = SQR_PRECOMP[b0] | (SQR_PRECOMP[b1] << 16);
|
|
166
|
+
out[2 * j + 1] = SQR_PRECOMP[b2] | (SQR_PRECOMP[b3] << 16);
|
|
167
|
+
}
|
|
168
|
+
return out;
|
|
169
|
+
};
|
|
166
170
|
export const modWords = (a, poly) => {
|
|
167
171
|
const dN = Math.floor(poly[0] / WORD_BITS), len = Math.max(a.length, dN + 1), res = new Uint32Array(len);
|
|
168
172
|
res.set(a);
|
package/dstu4145/index.d.ts
CHANGED
|
@@ -1,246 +1,37 @@
|
|
|
1
|
-
import { type TArg, type TRet } from "@noble/hashes/utils.js";
|
|
2
1
|
import { type DSTUParameters } from "./const.js";
|
|
2
|
+
import type { ECDSA } from "../types.js";
|
|
3
3
|
/** Create DSTU 4145-2002 signer (Big-Endian) */
|
|
4
|
-
export declare const dstu4145: (parameters: DSTUParameters) =>
|
|
5
|
-
getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
|
|
6
|
-
getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
|
|
7
|
-
sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
8
|
-
verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
|
|
9
|
-
keygen: (isCompressed?: boolean) => {
|
|
10
|
-
secretKey: TRet<Uint8Array>;
|
|
11
|
-
publicKey: TRet<Uint8Array>;
|
|
12
|
-
};
|
|
13
|
-
lengths: Readonly<{
|
|
14
|
-
fieldByteLength: number;
|
|
15
|
-
pointByteLength: number;
|
|
16
|
-
scalarByteLength: number;
|
|
17
|
-
signatureByteLength: number;
|
|
18
|
-
}>;
|
|
19
|
-
}>;
|
|
4
|
+
export declare const dstu4145: (parameters: DSTUParameters) => ECDSA;
|
|
20
5
|
/**
|
|
21
6
|
* Create DSTU 4145-2002 signer (Little-Endian)
|
|
22
7
|
*
|
|
23
8
|
* **NOTE:** Parameters MUST be in Big-Endian
|
|
24
9
|
*/
|
|
25
|
-
export declare const dstu4145_le: (parameters: DSTUParameters) =>
|
|
26
|
-
getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
|
|
27
|
-
getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
|
|
28
|
-
sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
29
|
-
verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
|
|
30
|
-
keygen: (isCompressed?: boolean) => {
|
|
31
|
-
secretKey: TRet<Uint8Array>;
|
|
32
|
-
publicKey: TRet<Uint8Array>;
|
|
33
|
-
};
|
|
34
|
-
lengths: Readonly<{
|
|
35
|
-
fieldByteLength: number;
|
|
36
|
-
pointByteLength: number;
|
|
37
|
-
scalarByteLength: number;
|
|
38
|
-
signatureByteLength: number;
|
|
39
|
-
}>;
|
|
40
|
-
}>;
|
|
10
|
+
export declare const dstu4145_le: (parameters: DSTUParameters) => ECDSA;
|
|
41
11
|
export * from "./const.js";
|
|
42
12
|
export * from "./ec/expand.js";
|
|
13
|
+
export * from "./ecies.js";
|
|
43
14
|
/** DSTU 4145-2002 163 bit curve */
|
|
44
|
-
export declare const dstu163:
|
|
45
|
-
getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
|
|
46
|
-
getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
|
|
47
|
-
sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
48
|
-
verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
|
|
49
|
-
keygen: (isCompressed?: boolean) => {
|
|
50
|
-
secretKey: TRet<Uint8Array>;
|
|
51
|
-
publicKey: TRet<Uint8Array>;
|
|
52
|
-
};
|
|
53
|
-
lengths: Readonly<{
|
|
54
|
-
fieldByteLength: number;
|
|
55
|
-
pointByteLength: number;
|
|
56
|
-
scalarByteLength: number;
|
|
57
|
-
signatureByteLength: number;
|
|
58
|
-
}>;
|
|
59
|
-
}>;
|
|
15
|
+
export declare const dstu163: ECDSA;
|
|
60
16
|
/** DSTU 4145-2002 167 bit curve */
|
|
61
|
-
export declare const dstu167:
|
|
62
|
-
getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
|
|
63
|
-
getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
|
|
64
|
-
sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
65
|
-
verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
|
|
66
|
-
keygen: (isCompressed?: boolean) => {
|
|
67
|
-
secretKey: TRet<Uint8Array>;
|
|
68
|
-
publicKey: TRet<Uint8Array>;
|
|
69
|
-
};
|
|
70
|
-
lengths: Readonly<{
|
|
71
|
-
fieldByteLength: number;
|
|
72
|
-
pointByteLength: number;
|
|
73
|
-
scalarByteLength: number;
|
|
74
|
-
signatureByteLength: number;
|
|
75
|
-
}>;
|
|
76
|
-
}>;
|
|
17
|
+
export declare const dstu167: ECDSA;
|
|
77
18
|
/** DSTU 4145-2002 173 bit curve */
|
|
78
|
-
export declare const dstu173:
|
|
79
|
-
getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
|
|
80
|
-
getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
|
|
81
|
-
sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
82
|
-
verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
|
|
83
|
-
keygen: (isCompressed?: boolean) => {
|
|
84
|
-
secretKey: TRet<Uint8Array>;
|
|
85
|
-
publicKey: TRet<Uint8Array>;
|
|
86
|
-
};
|
|
87
|
-
lengths: Readonly<{
|
|
88
|
-
fieldByteLength: number;
|
|
89
|
-
pointByteLength: number;
|
|
90
|
-
scalarByteLength: number;
|
|
91
|
-
signatureByteLength: number;
|
|
92
|
-
}>;
|
|
93
|
-
}>;
|
|
19
|
+
export declare const dstu173: ECDSA;
|
|
94
20
|
/** DSTU 4145-2002 179 bit curve */
|
|
95
|
-
export declare const dstu179:
|
|
96
|
-
getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
|
|
97
|
-
getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
|
|
98
|
-
sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
99
|
-
verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
|
|
100
|
-
keygen: (isCompressed?: boolean) => {
|
|
101
|
-
secretKey: TRet<Uint8Array>;
|
|
102
|
-
publicKey: TRet<Uint8Array>;
|
|
103
|
-
};
|
|
104
|
-
lengths: Readonly<{
|
|
105
|
-
fieldByteLength: number;
|
|
106
|
-
pointByteLength: number;
|
|
107
|
-
scalarByteLength: number;
|
|
108
|
-
signatureByteLength: number;
|
|
109
|
-
}>;
|
|
110
|
-
}>;
|
|
21
|
+
export declare const dstu179: ECDSA;
|
|
111
22
|
/** DSTU 4145-2002 191 bit curve */
|
|
112
|
-
export declare const dstu191:
|
|
113
|
-
getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
|
|
114
|
-
getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
|
|
115
|
-
sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
116
|
-
verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
|
|
117
|
-
keygen: (isCompressed?: boolean) => {
|
|
118
|
-
secretKey: TRet<Uint8Array>;
|
|
119
|
-
publicKey: TRet<Uint8Array>;
|
|
120
|
-
};
|
|
121
|
-
lengths: Readonly<{
|
|
122
|
-
fieldByteLength: number;
|
|
123
|
-
pointByteLength: number;
|
|
124
|
-
scalarByteLength: number;
|
|
125
|
-
signatureByteLength: number;
|
|
126
|
-
}>;
|
|
127
|
-
}>;
|
|
23
|
+
export declare const dstu191: ECDSA;
|
|
128
24
|
/** DSTU 4145-2002 233 bit curve */
|
|
129
|
-
export declare const dstu233:
|
|
130
|
-
getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
|
|
131
|
-
getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
|
|
132
|
-
sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
133
|
-
verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
|
|
134
|
-
keygen: (isCompressed?: boolean) => {
|
|
135
|
-
secretKey: TRet<Uint8Array>;
|
|
136
|
-
publicKey: TRet<Uint8Array>;
|
|
137
|
-
};
|
|
138
|
-
lengths: Readonly<{
|
|
139
|
-
fieldByteLength: number;
|
|
140
|
-
pointByteLength: number;
|
|
141
|
-
scalarByteLength: number;
|
|
142
|
-
signatureByteLength: number;
|
|
143
|
-
}>;
|
|
144
|
-
}>;
|
|
25
|
+
export declare const dstu233: ECDSA;
|
|
145
26
|
/** DSTU 4145-2002 257 bit curve */
|
|
146
|
-
export declare const dstu257:
|
|
147
|
-
getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
|
|
148
|
-
getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
|
|
149
|
-
sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
150
|
-
verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
|
|
151
|
-
keygen: (isCompressed?: boolean) => {
|
|
152
|
-
secretKey: TRet<Uint8Array>;
|
|
153
|
-
publicKey: TRet<Uint8Array>;
|
|
154
|
-
};
|
|
155
|
-
lengths: Readonly<{
|
|
156
|
-
fieldByteLength: number;
|
|
157
|
-
pointByteLength: number;
|
|
158
|
-
scalarByteLength: number;
|
|
159
|
-
signatureByteLength: number;
|
|
160
|
-
}>;
|
|
161
|
-
}>;
|
|
27
|
+
export declare const dstu257: ECDSA;
|
|
162
28
|
/** DSTU 4145-2002 257 bit curve (LE) */
|
|
163
|
-
export declare const dstu257_le:
|
|
164
|
-
getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
|
|
165
|
-
getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
|
|
166
|
-
sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
167
|
-
verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
|
|
168
|
-
keygen: (isCompressed?: boolean) => {
|
|
169
|
-
secretKey: TRet<Uint8Array>;
|
|
170
|
-
publicKey: TRet<Uint8Array>;
|
|
171
|
-
};
|
|
172
|
-
lengths: Readonly<{
|
|
173
|
-
fieldByteLength: number;
|
|
174
|
-
pointByteLength: number;
|
|
175
|
-
scalarByteLength: number;
|
|
176
|
-
signatureByteLength: number;
|
|
177
|
-
}>;
|
|
178
|
-
}>;
|
|
29
|
+
export declare const dstu257_le: ECDSA;
|
|
179
30
|
/** DSTU 4145-2002 307 bit curve */
|
|
180
|
-
export declare const dstu307:
|
|
181
|
-
getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
|
|
182
|
-
getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
|
|
183
|
-
sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
184
|
-
verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
|
|
185
|
-
keygen: (isCompressed?: boolean) => {
|
|
186
|
-
secretKey: TRet<Uint8Array>;
|
|
187
|
-
publicKey: TRet<Uint8Array>;
|
|
188
|
-
};
|
|
189
|
-
lengths: Readonly<{
|
|
190
|
-
fieldByteLength: number;
|
|
191
|
-
pointByteLength: number;
|
|
192
|
-
scalarByteLength: number;
|
|
193
|
-
signatureByteLength: number;
|
|
194
|
-
}>;
|
|
195
|
-
}>;
|
|
31
|
+
export declare const dstu307: ECDSA;
|
|
196
32
|
/** DSTU 4145-2002 367 bit curve */
|
|
197
|
-
export declare const dstu367:
|
|
198
|
-
getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
|
|
199
|
-
getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
|
|
200
|
-
sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
201
|
-
verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
|
|
202
|
-
keygen: (isCompressed?: boolean) => {
|
|
203
|
-
secretKey: TRet<Uint8Array>;
|
|
204
|
-
publicKey: TRet<Uint8Array>;
|
|
205
|
-
};
|
|
206
|
-
lengths: Readonly<{
|
|
207
|
-
fieldByteLength: number;
|
|
208
|
-
pointByteLength: number;
|
|
209
|
-
scalarByteLength: number;
|
|
210
|
-
signatureByteLength: number;
|
|
211
|
-
}>;
|
|
212
|
-
}>;
|
|
33
|
+
export declare const dstu367: ECDSA;
|
|
213
34
|
/** DSTU 4145-2002 431 bit curve */
|
|
214
|
-
export declare const dstu431:
|
|
215
|
-
getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
|
|
216
|
-
getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
|
|
217
|
-
sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
218
|
-
verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
|
|
219
|
-
keygen: (isCompressed?: boolean) => {
|
|
220
|
-
secretKey: TRet<Uint8Array>;
|
|
221
|
-
publicKey: TRet<Uint8Array>;
|
|
222
|
-
};
|
|
223
|
-
lengths: Readonly<{
|
|
224
|
-
fieldByteLength: number;
|
|
225
|
-
pointByteLength: number;
|
|
226
|
-
scalarByteLength: number;
|
|
227
|
-
signatureByteLength: number;
|
|
228
|
-
}>;
|
|
229
|
-
}>;
|
|
35
|
+
export declare const dstu431: ECDSA;
|
|
230
36
|
/** DSTU 4145-2002 431 bit curve (LE) */
|
|
231
|
-
export declare const dstu431_le:
|
|
232
|
-
getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
|
|
233
|
-
getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
|
|
234
|
-
sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
235
|
-
verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
|
|
236
|
-
keygen: (isCompressed?: boolean) => {
|
|
237
|
-
secretKey: TRet<Uint8Array>;
|
|
238
|
-
publicKey: TRet<Uint8Array>;
|
|
239
|
-
};
|
|
240
|
-
lengths: Readonly<{
|
|
241
|
-
fieldByteLength: number;
|
|
242
|
-
pointByteLength: number;
|
|
243
|
-
scalarByteLength: number;
|
|
244
|
-
signatureByteLength: number;
|
|
245
|
-
}>;
|
|
246
|
-
}>;
|
|
37
|
+
export declare const dstu431_le: ECDSA;
|
package/dstu4145/index.js
CHANGED
|
@@ -7,18 +7,8 @@ import { reverseBytes } from "../utils.js";
|
|
|
7
7
|
export const dstu4145 = (parameters) => {
|
|
8
8
|
const curve = binaryWeierstrass(parameters);
|
|
9
9
|
const { Field, Point, MASK, lengths } = curve;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* @param isCompressed - whether to return compact (default), or full key
|
|
13
|
-
* @returns Public key, full when `isCompressed=false`; short when `isCompressed=true`
|
|
14
|
-
*/
|
|
15
|
-
const getPublicKey = (secretKey, isCompressed = false) => Point.BASE.mul(Field.fromHexStringOrBytes(secretKey)).negate().toBytes(isCompressed);
|
|
16
|
-
const randomPrivateKey = () => Field.toBytes(new BN(randomBytes(lengths.scalarByteLength)).imaskn(MASK), lengths.scalarByteLength);
|
|
17
|
-
/**
|
|
18
|
-
* Computes presign (ephermeral keypair)
|
|
19
|
-
* @param rand Predefined secret ephermeral key
|
|
20
|
-
* @returns Ephemeral public key (`Fe`) and secret key (`e`)
|
|
21
|
-
*/
|
|
10
|
+
const getPublicKey = (secretKey, isCompressed = true) => Point.BASE.mul(Field.fromHexStringOrBytes(secretKey)).negate().toBytes(isCompressed);
|
|
11
|
+
/** Computes presign (ephermeral keypair) */
|
|
22
12
|
const computePresign = (rand) => {
|
|
23
13
|
const e = new BN(rand ?? randomBytes(lengths.scalarByteLength)).imaskn(MASK);
|
|
24
14
|
if (rand && e.isZero())
|
|
@@ -37,7 +27,6 @@ export const dstu4145 = (parameters) => {
|
|
|
37
27
|
h = new BN(1);
|
|
38
28
|
return h;
|
|
39
29
|
};
|
|
40
|
-
/** Signs a message hash with a secret key. */
|
|
41
30
|
const sign = (secretKey, digest, rand) => {
|
|
42
31
|
const d = new BN(secretKey), h = prepareHash(digest), { Fe, e } = computePresign(rand);
|
|
43
32
|
if (d.isZero() || d.gte(curve.ORDER))
|
|
@@ -50,7 +39,6 @@ export const dstu4145 = (parameters) => {
|
|
|
50
39
|
return sign(secretKey, digest, rand);
|
|
51
40
|
return concatBytes(Field.toBytes(s, lengths.scalarByteLength), Field.toBytes(r, lengths.scalarByteLength));
|
|
52
41
|
};
|
|
53
|
-
/** Verifies a signature against message hash and public key. */
|
|
54
42
|
const verify = (publicKey, digest, signature) => {
|
|
55
43
|
if (signature.length != lengths.signatureByteLength)
|
|
56
44
|
throw new Error("Invalid signature length");
|
|
@@ -63,10 +51,6 @@ export const dstu4145 = (parameters) => {
|
|
|
63
51
|
const y = curve.toExternalField(Field.mul(h, R.x)).imaskn(MASK);
|
|
64
52
|
return y.eq(r);
|
|
65
53
|
};
|
|
66
|
-
/**
|
|
67
|
-
* Computes shared secret key
|
|
68
|
-
* @param withCofactor Multiply result by curve cofactor? (default - `true`)
|
|
69
|
-
*/
|
|
70
54
|
const getSharedSecret = (secretKeyA, publicKeyB, withCofactor = true) => {
|
|
71
55
|
const d = new BN(secretKeyA), Q = Point.fromBytes(publicKeyB);
|
|
72
56
|
if (d.isZero() || d.gte(curve.ORDER))
|
|
@@ -78,8 +62,8 @@ export const dstu4145 = (parameters) => {
|
|
|
78
62
|
throw new Error(`Invalid parameters, shared secret is point at infinity`);
|
|
79
63
|
return Field.toBytes(curve.toExternalField(R.x), lengths.fieldByteLength);
|
|
80
64
|
};
|
|
81
|
-
const keygen = (isCompressed =
|
|
82
|
-
const secretKey =
|
|
65
|
+
const keygen = (isCompressed = true) => {
|
|
66
|
+
const secretKey = Field.toBytes(new BN(randomBytes(lengths.scalarByteLength)).imaskn(MASK), lengths.scalarByteLength);
|
|
83
67
|
const publicKey = getPublicKey(secretKey, isCompressed);
|
|
84
68
|
return { secretKey, publicKey };
|
|
85
69
|
};
|
|
@@ -99,11 +83,11 @@ export const dstu4145 = (parameters) => {
|
|
|
99
83
|
*/
|
|
100
84
|
export const dstu4145_le = (parameters) => {
|
|
101
85
|
const signer = dstu4145(parameters);
|
|
102
|
-
const getPublicKey = (secretKey, isCompressed =
|
|
86
|
+
const getPublicKey = (secretKey, isCompressed = true) => reverseBytes(signer.getPublicKey(reverseBytes(secretKey), isCompressed));
|
|
103
87
|
const sign = (secretKey, digest, rand) => reverseBytes(signer.sign(reverseBytes(secretKey), reverseBytes(digest), rand ? reverseBytes(rand) : undefined));
|
|
104
88
|
const verify = (publicKey, digest, signature) => signer.verify(reverseBytes(publicKey), reverseBytes(digest), reverseBytes(signature));
|
|
105
89
|
const getSharedSecret = (secretKeyA, publicKeyB, withCofactor = true) => reverseBytes(signer.getSharedSecret(reverseBytes(secretKeyA), reverseBytes(publicKeyB), withCofactor));
|
|
106
|
-
const keygen = (isCompressed =
|
|
90
|
+
const keygen = (isCompressed = true) => {
|
|
107
91
|
const { secretKey, publicKey } = signer.keygen(isCompressed);
|
|
108
92
|
return {
|
|
109
93
|
secretKey: reverseBytes(secretKey),
|
|
@@ -121,6 +105,7 @@ export const dstu4145_le = (parameters) => {
|
|
|
121
105
|
};
|
|
122
106
|
export * from "./const.js";
|
|
123
107
|
export * from "./ec/expand.js";
|
|
108
|
+
export * from "./ecies.js";
|
|
124
109
|
// ONB curves don't preinitialized because `multiplyOnb` is slow O(m^3) function
|
|
125
110
|
// You need to initialize them manually via `dstu4145`
|
|
126
111
|
/** DSTU 4145-2002 163 bit curve */
|
package/dstu9041/curve.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { EdwardsOpts } from "@noble/curves/abstract/edwards.js";
|
|
2
2
|
import { type TArg, type TRet } from "@noble/curves/utils.js";
|
|
3
|
+
import type { KeyPair } from "../types";
|
|
3
4
|
export declare const dstu9041Curve: (parameters: EdwardsOpts) => Readonly<{
|
|
4
5
|
Point: {
|
|
5
6
|
new (X: bigint, Y: bigint, Z: bigint): {
|
|
@@ -138,10 +139,7 @@ export declare const dstu9041Curve: (parameters: EdwardsOpts) => Readonly<{
|
|
|
138
139
|
};
|
|
139
140
|
};
|
|
140
141
|
getPublicKey: (secretKey: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
141
|
-
keygen: () =>
|
|
142
|
-
secretKey: TRet<Uint8Array>;
|
|
143
|
-
publicKey: TRet<Uint8Array>;
|
|
144
|
-
};
|
|
142
|
+
keygen: () => KeyPair;
|
|
145
143
|
}>;
|
|
146
144
|
export declare const curve256: Readonly<{
|
|
147
145
|
Point: {
|
|
@@ -281,10 +279,7 @@ export declare const curve256: Readonly<{
|
|
|
281
279
|
};
|
|
282
280
|
};
|
|
283
281
|
getPublicKey: (secretKey: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
284
|
-
keygen: () =>
|
|
285
|
-
secretKey: TRet<Uint8Array>;
|
|
286
|
-
publicKey: TRet<Uint8Array>;
|
|
287
|
-
};
|
|
282
|
+
keygen: () => KeyPair;
|
|
288
283
|
}>;
|
|
289
284
|
export declare const curve512: Readonly<{
|
|
290
285
|
Point: {
|
|
@@ -424,8 +419,5 @@ export declare const curve512: Readonly<{
|
|
|
424
419
|
};
|
|
425
420
|
};
|
|
426
421
|
getPublicKey: (secretKey: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
427
|
-
keygen: () =>
|
|
428
|
-
secretKey: TRet<Uint8Array>;
|
|
429
|
-
publicKey: TRet<Uint8Array>;
|
|
430
|
-
};
|
|
422
|
+
keygen: () => KeyPair;
|
|
431
423
|
}>;
|
package/dstu9041/curve.js
CHANGED
|
@@ -84,7 +84,9 @@ export const dstu9041Curve = (parameters) => {
|
|
|
84
84
|
const y = Fp.fromBytes(bytes.subarray(Fp.BYTES, 2 * Fp.BYTES));
|
|
85
85
|
return Point.fromAffine(x, y);
|
|
86
86
|
}
|
|
87
|
-
static fromAffine(x, y) {
|
|
87
|
+
static fromAffine(x, y) {
|
|
88
|
+
return new Point(x, y, Fp.ONE).assertValidity();
|
|
89
|
+
}
|
|
88
90
|
static fromX(x) {
|
|
89
91
|
const x2 = Fp.sqr(x);
|
|
90
92
|
if (Fp.eql(x, Fp.ZERO) ||
|
package/dstu9041/index.d.ts
CHANGED
|
@@ -2,20 +2,14 @@ import { type TArg, type TRet } from "@noble/curves/utils.js";
|
|
|
2
2
|
/** DSTU 9041:2020 (256 bit) */
|
|
3
3
|
export declare const dstu9041_256: Readonly<{
|
|
4
4
|
getPublicKey: (secretKey: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
5
|
-
keygen: () =>
|
|
6
|
-
secretKey: TRet<Uint8Array>;
|
|
7
|
-
publicKey: TRet<Uint8Array>;
|
|
8
|
-
};
|
|
5
|
+
keygen: () => import("../types.js").KeyPair;
|
|
9
6
|
encrypt: (plaintext: TArg<Uint8Array>, publicKey: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
10
7
|
decrypt: (ciphertext: TArg<Uint8Array>, privateKey: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
11
8
|
}>;
|
|
12
9
|
/** DSTU 9041:2020 (512 bit) */
|
|
13
10
|
export declare const dstu9041_512: Readonly<{
|
|
14
11
|
getPublicKey: (secretKey: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
15
|
-
keygen: () =>
|
|
16
|
-
secretKey: TRet<Uint8Array>;
|
|
17
|
-
publicKey: TRet<Uint8Array>;
|
|
18
|
-
};
|
|
12
|
+
keygen: () => import("../types.js").KeyPair;
|
|
19
13
|
encrypt: (plaintext: TArg<Uint8Array>, publicKey: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
20
14
|
decrypt: (ciphertext: TArg<Uint8Array>, privateKey: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
21
15
|
}>;
|
package/dstu9041/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { randomBytes, bytesToNumberBE, concatBytes, equalBytes, numberToBytesBE } from "@noble/curves/utils.js";
|
|
2
2
|
import { kupyna256 } from "../kupyna/index.js";
|
|
3
3
|
import { curve256, curve512, dstu9041Curve } from "./curve.js";
|
|
4
|
-
import { kw } from "../modes/
|
|
4
|
+
import { kw } from "../modes/kw.js";
|
|
5
5
|
import { Kalyna256, Kalyna512 } from "../kalyna/index.js";
|
|
6
6
|
const ID_KUPYNA256 = 1;
|
|
7
7
|
const dstu9041 = (length) => {
|
package/iso15946.d.ts
ADDED
package/iso15946.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { AsnConvert, AsnProp, AsnPropTypes } from "@peculiar/asn1-schema";
|
|
8
|
+
import { AlgorithmIdentifier } from "@peculiar/asn1-x509";
|
|
9
|
+
import { Gost3431195 } from "./dstu9311/hash.js";
|
|
10
|
+
const KEY_LENGTH = new Uint8Array([0, 0, 1, 0]);
|
|
11
|
+
const COUNTER = new Uint8Array([0, 0, 0, 1]);
|
|
12
|
+
class SharedInfo {
|
|
13
|
+
keyInfo = new AlgorithmIdentifier();
|
|
14
|
+
entityInfo;
|
|
15
|
+
suppPubInfo = new ArrayBuffer(0);
|
|
16
|
+
constructor(params = {}) {
|
|
17
|
+
Object.assign(this, params);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
__decorate([
|
|
21
|
+
AsnProp({ type: AlgorithmIdentifier })
|
|
22
|
+
], SharedInfo.prototype, "keyInfo", void 0);
|
|
23
|
+
__decorate([
|
|
24
|
+
AsnProp({ type: AsnPropTypes.OctetString, context: 0, optional: true })
|
|
25
|
+
], SharedInfo.prototype, "entityInfo", void 0);
|
|
26
|
+
__decorate([
|
|
27
|
+
AsnProp({ type: AsnPropTypes.OctetString, context: 2 })
|
|
28
|
+
], SharedInfo.prototype, "suppPubInfo", void 0);
|
|
29
|
+
const removeLeadZeros = (bytes) => {
|
|
30
|
+
const idx = bytes.findIndex(i => i !== 0);
|
|
31
|
+
return idx === -1 ? new Uint8Array() : bytes.slice(idx);
|
|
32
|
+
};
|
|
33
|
+
const encodeSharedInfo = (oid, ukm) => new Uint8Array(AsnConvert.serialize(new SharedInfo({
|
|
34
|
+
keyInfo: new AlgorithmIdentifier({
|
|
35
|
+
algorithm: oid,
|
|
36
|
+
parameters: null
|
|
37
|
+
}),
|
|
38
|
+
entityInfo: ukm,
|
|
39
|
+
suppPubInfo: KEY_LENGTH.buffer
|
|
40
|
+
})));
|
|
41
|
+
/** ISO/IEC 15946 KDF function */
|
|
42
|
+
export const iso15946_kdf = (hash, wrapAlgoOid, inputData, ukm) => {
|
|
43
|
+
if (hash.outputLen != 32)
|
|
44
|
+
throw new Error("Invalid hash function. Output length must be 32 bytes");
|
|
45
|
+
const hasher = hash.create();
|
|
46
|
+
return hasher.update(hasher instanceof Gost3431195
|
|
47
|
+
? removeLeadZeros(inputData)
|
|
48
|
+
: inputData).update(COUNTER).update(encodeSharedInfo(wrapAlgoOid, ukm)).digest();
|
|
49
|
+
};
|
package/modes/kw.d.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { Cipher } from "../types.js";
|
|
1
|
+
import type { Cipher, WrapMode } from "../types.js";
|
|
3
2
|
/** Key wrapping (KW) */
|
|
4
|
-
export declare const kw: (cipher: Cipher) =>
|
|
5
|
-
wrap: (key: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
6
|
-
unwrap: (wrappedKey: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
7
|
-
};
|
|
3
|
+
export declare const kw: (cipher: Cipher) => WrapMode;
|
package/modes/xts.d.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { Cipher } from "../types.js";
|
|
1
|
+
import type { Cipher, DiskMode } from "../types.js";
|
|
3
2
|
/** XEX Tweakable Block Ciphertext Stealing (XTS) */
|
|
4
|
-
export declare const xts: (cipher: Cipher) =>
|
|
5
|
-
encrypt: (plaintext: TArg<Uint8Array>, tweak: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
6
|
-
decrypt: (ciphertext: TArg<Uint8Array>, iv: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
7
|
-
};
|
|
3
|
+
export declare const xts: (cipher: Cipher) => DiskMode;
|
package/modes/xts.js
CHANGED
|
@@ -57,10 +57,10 @@ export const xts = (cipher) => {
|
|
|
57
57
|
}
|
|
58
58
|
return buffer;
|
|
59
59
|
},
|
|
60
|
-
decrypt: (ciphertext,
|
|
60
|
+
decrypt: (ciphertext, tweak) => {
|
|
61
61
|
if (ciphertext.length < cipher.blockSize)
|
|
62
62
|
throw new Error(`Invalid length (need at least ${cipher.blockSize}, got ${ciphertext.length})`);
|
|
63
|
-
const gamma = init(
|
|
63
|
+
const gamma = init(tweak);
|
|
64
64
|
const decrypt = cipher.decrypt.bind(cipher);
|
|
65
65
|
const buffer = new Uint8Array(ciphertext);
|
|
66
66
|
const k = Math.floor(ciphertext.length / cipher.blockSize);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@li0ard/dstu",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.9",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"types": "index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -32,8 +32,6 @@
|
|
|
32
32
|
"docs": "typedoc --options .config/typedoc/config.cjs"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@peculiar/asn1-schema": "^2.8.0",
|
|
36
|
-
"@peculiar/asn1-x509": "^2.8.0",
|
|
37
35
|
"@types/bn.js": "^5.2.0",
|
|
38
36
|
"@types/bun": "latest",
|
|
39
37
|
"typedoc": "^0.28.19"
|
|
@@ -44,7 +42,9 @@
|
|
|
44
42
|
"dependencies": {
|
|
45
43
|
"@noble/curves": "^2.4.0",
|
|
46
44
|
"@noble/hashes": "^2.4.0",
|
|
47
|
-
"bn.js": "^5.2.3"
|
|
45
|
+
"bn.js": "^5.2.3",
|
|
46
|
+
"@peculiar/asn1-schema": "^2.9.5",
|
|
47
|
+
"@peculiar/asn1-x509": "^2.9.5"
|
|
48
48
|
},
|
|
49
49
|
"files": ["**/*.js", "**/*.d.ts"],
|
|
50
50
|
"publishConfig": {
|
package/strumok/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { TArg, TRet } from "@noble/hashes/utils.js";
|
|
2
|
+
import type { StreamCipher } from "../types.js";
|
|
2
3
|
/** Strumok stream cipher */
|
|
3
|
-
export declare class Strumok {
|
|
4
|
+
export declare class Strumok implements StreamCipher {
|
|
4
5
|
readonly blockSize = 128;
|
|
5
6
|
readonly keySize: number;
|
|
6
7
|
private S;
|
package/types.d.ts
CHANGED
|
@@ -12,6 +12,16 @@ export type Cipher = {
|
|
|
12
12
|
/** Decrypt block */
|
|
13
13
|
decrypt(ciphertext: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
14
14
|
};
|
|
15
|
+
export type StreamCipher = {
|
|
16
|
+
/** Block size */
|
|
17
|
+
readonly blockSize: number;
|
|
18
|
+
/** Key size */
|
|
19
|
+
readonly keySize: number;
|
|
20
|
+
/** Generate keystream word */
|
|
21
|
+
next_stream(): TRet<Uint8Array>;
|
|
22
|
+
/** Perform encryption/decryption */
|
|
23
|
+
crypt(msg: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
24
|
+
};
|
|
15
25
|
/** Block mode for {@link Cipher} */
|
|
16
26
|
export type BlockMode = {
|
|
17
27
|
/** Encrypt plaintext */
|
|
@@ -41,3 +51,70 @@ export type AEADMode = {
|
|
|
41
51
|
/** Open ciphertext and AAD */
|
|
42
52
|
open: (ciphertext: TArg<Uint8Array>, aad?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
43
53
|
};
|
|
54
|
+
/** Key wrap mode for {@link Cipher} */
|
|
55
|
+
export type WrapMode = {
|
|
56
|
+
/** Wrap encryption key */
|
|
57
|
+
wrap: (key: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
58
|
+
/** Unwrap encryption key */
|
|
59
|
+
unwrap: (wrappedKey: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
60
|
+
};
|
|
61
|
+
/** Disk encryption mode for {@link Cipher} */
|
|
62
|
+
export type DiskMode = {
|
|
63
|
+
/** Encrypt disk sector */
|
|
64
|
+
encrypt: (plaintext: TArg<Uint8Array>, tweak: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
65
|
+
/** Decrypt disk sector */
|
|
66
|
+
decrypt: (ciphertext: TArg<Uint8Array>, tweak: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
67
|
+
};
|
|
68
|
+
/** ECDSA signer */
|
|
69
|
+
export type ECDSA = {
|
|
70
|
+
/**
|
|
71
|
+
* Computes public key for a secret key
|
|
72
|
+
* @param isCompressed - Whether to return compact (default), or full key
|
|
73
|
+
* @returns Public key, full when `isCompressed=false`; short when `isCompressed=true`
|
|
74
|
+
*/
|
|
75
|
+
getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
|
|
76
|
+
/**
|
|
77
|
+
* Signs a message hash with a secret key.
|
|
78
|
+
* @param secretKey - Secret key bytes.
|
|
79
|
+
* @param digest - Digest bytes.
|
|
80
|
+
* @param rand - Optional parameter for ephemeral key
|
|
81
|
+
* @returns Encoded signature bytes.
|
|
82
|
+
*/
|
|
83
|
+
sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
84
|
+
/**
|
|
85
|
+
* Verifies a signature against message hash and public key.
|
|
86
|
+
* @param publicKey - Public key
|
|
87
|
+
* @param digest - Digest bytes
|
|
88
|
+
* @param signature - Encoded signature bytes.
|
|
89
|
+
* @returns Whether the signature is valid.
|
|
90
|
+
*/
|
|
91
|
+
verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Compute the shared secret point from a secret key and peer public key.
|
|
94
|
+
* @param secretKeyA - Local secret key bytes.
|
|
95
|
+
* @param publicKeyB - Peer public key bytes.
|
|
96
|
+
* @param withCofactor - Whether to multiply result by curve cofactor? (default - `true`).
|
|
97
|
+
* @returns Encoded shared point.
|
|
98
|
+
*/
|
|
99
|
+
getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
|
|
100
|
+
/**
|
|
101
|
+
* Generate a secret/public key pair.
|
|
102
|
+
* @param isCompressed - Whether to return compact (default), or full key
|
|
103
|
+
* @returns Secret/public key pair.
|
|
104
|
+
*/
|
|
105
|
+
keygen: (isCompressed?: boolean) => KeyPair;
|
|
106
|
+
/** Byte lengths for keys and signatures exposed by this curve. */
|
|
107
|
+
lengths: Readonly<{
|
|
108
|
+
fieldByteLength: number;
|
|
109
|
+
pointByteLength: number;
|
|
110
|
+
scalarByteLength: number;
|
|
111
|
+
signatureByteLength: number;
|
|
112
|
+
}>;
|
|
113
|
+
};
|
|
114
|
+
/** Keypair */
|
|
115
|
+
export type KeyPair = {
|
|
116
|
+
/** Secret key */
|
|
117
|
+
secretKey: TArg<Uint8Array>;
|
|
118
|
+
/** Public key */
|
|
119
|
+
publicKey: TArg<Uint8Array>;
|
|
120
|
+
};
|