@li0ard/dstu 0.1.8 → 0.2.0
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 +4 -2
- package/dstu4145/ec/index.d.ts +4 -0
- package/dstu4145/ec/index.js +19 -0
- package/dstu4145/ec/wnaf.d.ts +2 -1
- package/dstu4145/ecies.d.ts +82 -0
- package/dstu4145/ecies.js +72 -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 +20 -26
- package/dstu9041/curve.d.ts +4 -12
- package/dstu9041/curve.js +5 -3
- package/dstu9041/index.d.ts +2 -8
- package/dstu9041/index.js +10 -6
- package/dstu9311/hash.d.ts +46 -12
- package/dstu9311/hash.js +89 -28
- package/dstu9311/index.js +4 -5
- package/iso15946.d.ts +3 -0
- package/iso15946.js +56 -0
- package/kalyna/index.d.ts +1 -1
- package/kalyna/index.js +6 -6
- package/keywrap.js +6 -3
- package/kupyna/index.d.ts +116 -67
- package/kupyna/index.js +122 -61
- package/kupyna/kmac.d.ts +6 -6
- package/kupyna/kmac.js +20 -15
- package/modes/cbc.d.ts +1 -1
- package/modes/cbc.js +5 -7
- package/modes/ccm.js +12 -3
- package/modes/cfb.js +42 -51
- package/modes/cmac.js +4 -1
- package/modes/ctr.js +4 -3
- package/modes/ecb.js +2 -2
- package/modes/gcm.d.ts +4 -1
- package/modes/gcm.js +32 -5
- package/modes/gmac.js +4 -1
- package/modes/index.d.ts +1 -1
- package/modes/index.js +1 -1
- package/modes/kw.d.ts +2 -6
- package/modes/kw.js +9 -16
- package/modes/ofb.js +3 -3
- package/modes/xts.d.ts +2 -6
- package/modes/xts.js +7 -3
- package/package.json +4 -4
- package/padding.d.ts +1 -1
- package/padding.js +5 -0
- package/strumok/index.d.ts +3 -2
- package/strumok/index.js +4 -2
- package/types.d.ts +77 -0
- package/utils.d.ts +1 -0
- package/utils.js +14 -1
package/dstu4145/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { concatBytes, randomBytes } from "@noble/hashes/utils.js";
|
|
1
|
+
import { abool, abytes, concatBytes, randomBytes } from "@noble/hashes/utils.js";
|
|
2
2
|
import { DSTU_163, DSTU_167, DSTU_173, DSTU_179, DSTU_191, DSTU_233, DSTU_257, DSTU_307, DSTU_367, DSTU_431 } from "./const.js";
|
|
3
3
|
import { binaryWeierstrass } from "./ec/index.js";
|
|
4
4
|
import BN from "bn.js";
|
|
@@ -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(abytes(secretKey, undefined, "secretKey"))).negate().toBytes(abool(isCompressed, "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,8 +27,11 @@ 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) => {
|
|
31
|
+
abytes(secretKey, undefined, "secretKey");
|
|
32
|
+
abytes(digest, undefined, "digest");
|
|
33
|
+
if (rand)
|
|
34
|
+
abytes(rand, undefined, "rand");
|
|
42
35
|
const d = new BN(secretKey), h = prepareHash(digest), { Fe, e } = computePresign(rand);
|
|
43
36
|
if (d.isZero() || d.gte(curve.ORDER))
|
|
44
37
|
throw new Error("Invalid private key, must be in range 1 < key < order");
|
|
@@ -50,24 +43,23 @@ export const dstu4145 = (parameters) => {
|
|
|
50
43
|
return sign(secretKey, digest, rand);
|
|
51
44
|
return concatBytes(Field.toBytes(s, lengths.scalarByteLength), Field.toBytes(r, lengths.scalarByteLength));
|
|
52
45
|
};
|
|
53
|
-
/** Verifies a signature against message hash and public key. */
|
|
54
46
|
const verify = (publicKey, digest, signature) => {
|
|
55
|
-
|
|
56
|
-
|
|
47
|
+
abytes(publicKey, undefined, "publicKey");
|
|
48
|
+
abytes(digest, undefined, "digest");
|
|
49
|
+
abytes(signature, lengths.signatureByteLength, "signature");
|
|
57
50
|
const Q = Point.fromBytes(publicKey);
|
|
58
51
|
const s = Field.fromHexStringOrBytes(signature.subarray(0, lengths.scalarByteLength)), r = Field.fromHexStringOrBytes(signature.subarray(lengths.scalarByteLength));
|
|
59
52
|
if (s.isZero() || s.gte(curve.ORDER) || r.isZero() || r.gte(curve.ORDER))
|
|
60
53
|
return false;
|
|
61
54
|
const h = prepareHash(digest);
|
|
62
|
-
const R = Point.BASE.
|
|
55
|
+
const R = Point.BASE.mulAdd(s, Q, r);
|
|
63
56
|
const y = curve.toExternalField(Field.mul(h, R.x)).imaskn(MASK);
|
|
64
57
|
return y.eq(r);
|
|
65
58
|
};
|
|
66
|
-
/**
|
|
67
|
-
* Computes shared secret key
|
|
68
|
-
* @param withCofactor Multiply result by curve cofactor? (default - `true`)
|
|
69
|
-
*/
|
|
70
59
|
const getSharedSecret = (secretKeyA, publicKeyB, withCofactor = true) => {
|
|
60
|
+
abytes(secretKeyA, undefined, "secretKeyA");
|
|
61
|
+
abytes(publicKeyB, undefined, "publicKeyB");
|
|
62
|
+
abool(withCofactor, "withCofactor");
|
|
71
63
|
const d = new BN(secretKeyA), Q = Point.fromBytes(publicKeyB);
|
|
72
64
|
if (d.isZero() || d.gte(curve.ORDER))
|
|
73
65
|
throw new Error("Invalid private key, must be in range 1 < key < order");
|
|
@@ -78,8 +70,9 @@ export const dstu4145 = (parameters) => {
|
|
|
78
70
|
throw new Error(`Invalid parameters, shared secret is point at infinity`);
|
|
79
71
|
return Field.toBytes(curve.toExternalField(R.x), lengths.fieldByteLength);
|
|
80
72
|
};
|
|
81
|
-
const keygen = (isCompressed =
|
|
82
|
-
|
|
73
|
+
const keygen = (isCompressed = true) => {
|
|
74
|
+
abool(isCompressed, "isCompressed");
|
|
75
|
+
const secretKey = Field.toBytes(new BN(randomBytes(lengths.scalarByteLength)).imaskn(MASK), lengths.scalarByteLength);
|
|
83
76
|
const publicKey = getPublicKey(secretKey, isCompressed);
|
|
84
77
|
return { secretKey, publicKey };
|
|
85
78
|
};
|
|
@@ -99,11 +92,11 @@ export const dstu4145 = (parameters) => {
|
|
|
99
92
|
*/
|
|
100
93
|
export const dstu4145_le = (parameters) => {
|
|
101
94
|
const signer = dstu4145(parameters);
|
|
102
|
-
const getPublicKey = (secretKey, isCompressed =
|
|
95
|
+
const getPublicKey = (secretKey, isCompressed = true) => reverseBytes(signer.getPublicKey(reverseBytes(secretKey), isCompressed));
|
|
103
96
|
const sign = (secretKey, digest, rand) => reverseBytes(signer.sign(reverseBytes(secretKey), reverseBytes(digest), rand ? reverseBytes(rand) : undefined));
|
|
104
97
|
const verify = (publicKey, digest, signature) => signer.verify(reverseBytes(publicKey), reverseBytes(digest), reverseBytes(signature));
|
|
105
98
|
const getSharedSecret = (secretKeyA, publicKeyB, withCofactor = true) => reverseBytes(signer.getSharedSecret(reverseBytes(secretKeyA), reverseBytes(publicKeyB), withCofactor));
|
|
106
|
-
const keygen = (isCompressed =
|
|
99
|
+
const keygen = (isCompressed = true) => {
|
|
107
100
|
const { secretKey, publicKey } = signer.keygen(isCompressed);
|
|
108
101
|
return {
|
|
109
102
|
secretKey: reverseBytes(secretKey),
|
|
@@ -121,6 +114,7 @@ export const dstu4145_le = (parameters) => {
|
|
|
121
114
|
};
|
|
122
115
|
export * from "./const.js";
|
|
123
116
|
export * from "./ec/expand.js";
|
|
117
|
+
export * from "./ecies.js";
|
|
124
118
|
// ONB curves don't preinitialized because `multiplyOnb` is slow O(m^3) function
|
|
125
119
|
// You need to initialize them manually via `dstu4145`
|
|
126
120
|
/** 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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Field, mod } from "@noble/curves/abstract/modular.js";
|
|
2
|
-
import { concatBytes, bitGet, bitLen, bytesToNumberBE, randomBytes } from "@noble/curves/utils.js";
|
|
2
|
+
import { concatBytes, bitGet, bitLen, bytesToNumberBE, randomBytes, abytes } from "@noble/curves/utils.js";
|
|
3
3
|
export const dstu9041Curve = (parameters) => {
|
|
4
4
|
if (parameters.a != 2n)
|
|
5
5
|
throw new Error("Unsuppored `a` parameter, must be 2");
|
|
@@ -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) ||
|
|
@@ -103,7 +105,7 @@ export const dstu9041Curve = (parameters) => {
|
|
|
103
105
|
return candidate;
|
|
104
106
|
}
|
|
105
107
|
}
|
|
106
|
-
const getPublicKey = (secretKey) => Point.BASE.multiply(bytesToNumberBE(secretKey)).toBytes();
|
|
108
|
+
const getPublicKey = (secretKey) => Point.BASE.multiply(bytesToNumberBE(abytes(secretKey, undefined, "secretKey"))).toBytes();
|
|
107
109
|
const keygen = () => {
|
|
108
110
|
const secretKey = randomBytes(Fp.BYTES);
|
|
109
111
|
const publicKey = getPublicKey(secretKey);
|
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
|
-
import { randomBytes, bytesToNumberBE, concatBytes, equalBytes, numberToBytesBE } from "@noble/curves/utils.js";
|
|
1
|
+
import { randomBytes, bytesToNumberBE, concatBytes, equalBytes, numberToBytesBE, abytes } 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) => {
|
|
@@ -33,6 +33,7 @@ const dstu9041 = (length) => {
|
|
|
33
33
|
return paddedMessage;
|
|
34
34
|
};
|
|
35
35
|
const buildStruct = (message) => {
|
|
36
|
+
abytes(message);
|
|
36
37
|
const paddedMessage = padMessage(message);
|
|
37
38
|
const messageLength = numberToBytesBE(message.length * 8, 2);
|
|
38
39
|
const digest = kupyna256(concatBytes(messageLength, paddedMessage)).subarray(-HASH_LENGTH_BYTES);
|
|
@@ -44,8 +45,7 @@ const dstu9041 = (length) => {
|
|
|
44
45
|
return struct;
|
|
45
46
|
};
|
|
46
47
|
const parseStruct = (struct) => {
|
|
47
|
-
|
|
48
|
-
throw new Error("Invalid ciphertext");
|
|
48
|
+
abytes(struct, STRUCT_BYTES, "struct");
|
|
49
49
|
if (struct[0] != ID_KUPYNA256)
|
|
50
50
|
throw new Error("Invalid hash ID");
|
|
51
51
|
const embeddedHash = struct.subarray(1, HASH_LENGTH_BYTES + 1);
|
|
@@ -73,6 +73,10 @@ const dstu9041 = (length) => {
|
|
|
73
73
|
getPublicKey: curve.getPublicKey,
|
|
74
74
|
keygen: curve.keygen,
|
|
75
75
|
encrypt: (plaintext, publicKey, rand) => {
|
|
76
|
+
abytes(plaintext, undefined, "plaintext");
|
|
77
|
+
abytes(publicKey, undefined, "publicKey");
|
|
78
|
+
if (rand)
|
|
79
|
+
abytes(rand);
|
|
76
80
|
const struct = buildStruct(plaintext);
|
|
77
81
|
const e = Fn.create(bytesToNumberBE(rand ?? randomBytes(STRUCT_BYTES)));
|
|
78
82
|
const r = Fp.toBytes(Point.BASE.multiply(e).x);
|
|
@@ -83,8 +87,8 @@ const dstu9041 = (length) => {
|
|
|
83
87
|
return concatBytes(r, ciphertext);
|
|
84
88
|
},
|
|
85
89
|
decrypt: (ciphertext, privateKey) => {
|
|
86
|
-
|
|
87
|
-
|
|
90
|
+
abytes(ciphertext, length / 2, "ciphertext");
|
|
91
|
+
abytes(privateKey, undefined, "privateKey");
|
|
88
92
|
const r = bytesToNumberBE(ciphertext.subarray(0, STRUCT_BYTES));
|
|
89
93
|
if (!Fp.isValidNot0(r))
|
|
90
94
|
throw new Error("Invalid ciphertext");
|
package/dstu9311/hash.d.ts
CHANGED
|
@@ -1,41 +1,75 @@
|
|
|
1
1
|
import { type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
|
|
2
2
|
import { _HMAC } from "@noble/hashes/hmac.js";
|
|
3
|
-
/** GOST 34.311-95 hash
|
|
4
|
-
export declare class
|
|
3
|
+
/** Internal GOST 34.311-95 hash class */
|
|
4
|
+
export declare class _Gost3431195 implements Hash<_Gost3431195> {
|
|
5
5
|
private sbox;
|
|
6
6
|
readonly blockLen = 32;
|
|
7
7
|
readonly outputLen = 32;
|
|
8
8
|
readonly canXOF = false;
|
|
9
9
|
private buffer;
|
|
10
|
-
|
|
10
|
+
private pos;
|
|
11
|
+
private h;
|
|
12
|
+
private len;
|
|
13
|
+
private checksum;
|
|
14
|
+
/** Internal GOST 34.311-95 hash class */
|
|
11
15
|
constructor(sbox?: TArg<Uint8Array>);
|
|
12
16
|
/** Create hash instance */
|
|
13
|
-
static create():
|
|
17
|
+
static create(): _Gost3431195;
|
|
14
18
|
destroy(): void;
|
|
15
|
-
clone():
|
|
16
|
-
_cloneInto(to?:
|
|
19
|
+
clone(): _Gost3431195;
|
|
20
|
+
_cloneInto(to?: _Gost3431195): _Gost3431195;
|
|
21
|
+
private processBlock;
|
|
17
22
|
update(data: TArg<Uint8Array>): this;
|
|
18
23
|
digestInto(buf: TArg<Uint8Array>): void;
|
|
19
24
|
digest(): TRet<Uint8Array>;
|
|
20
25
|
}
|
|
21
|
-
/**
|
|
26
|
+
/**
|
|
27
|
+
* GOST 34.311-95 hash function
|
|
28
|
+
*
|
|
29
|
+
* @param msg - message bytes to hash.
|
|
30
|
+
* @returns Digest bytes.
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { gost3431195 } from "@li0ard/dstu/dstu9311.js";
|
|
34
|
+
*
|
|
35
|
+
* gost3431195(new Uint8Array([97, 98, 99]));
|
|
36
|
+
* gost3431195.create(sbox?).update(new Uint8Array([97, 98, 99])).digest();
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
22
39
|
export declare const gost3431195: {
|
|
23
40
|
outputLen: number;
|
|
24
41
|
blockLen: number;
|
|
25
42
|
canXOF: boolean;
|
|
26
43
|
} & import("@noble/hashes/utils.js").HashInfo & {
|
|
27
44
|
(msg: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
28
|
-
create():
|
|
45
|
+
create(): _Gost3431195;
|
|
29
46
|
} & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
|
|
30
47
|
outputLen: number;
|
|
31
48
|
blockLen: number;
|
|
32
49
|
canXOF: boolean;
|
|
33
50
|
oid?: TRet<Uint8Array> | undefined;
|
|
34
|
-
create: () =>
|
|
51
|
+
create: () => _Gost3431195;
|
|
35
52
|
};
|
|
36
|
-
/**
|
|
37
|
-
|
|
53
|
+
/**
|
|
54
|
+
* HMAC over GOST 34.311-95 hash function
|
|
55
|
+
*
|
|
56
|
+
* @param key - authentication key bytes
|
|
57
|
+
*/
|
|
58
|
+
export declare class Gost3431195HMAC extends _HMAC<_Gost3431195> {
|
|
38
59
|
constructor(key: TArg<Uint8Array>);
|
|
39
60
|
}
|
|
40
|
-
/**
|
|
61
|
+
/**
|
|
62
|
+
* HMAC over GOST 34.311-95 hash function
|
|
63
|
+
*
|
|
64
|
+
* @param key - authentication key bytes
|
|
65
|
+
* @param message - message bytes to authenticate
|
|
66
|
+
* @returns Authentication tag bytes.
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* import { gost3431195Hmac } from "@li0ard/dstu/dstu9311.js";
|
|
70
|
+
* const key = new Uint8Array([1, 2, 3]);
|
|
71
|
+
* const message = new Uint8Array([4, 5, 6]);
|
|
72
|
+
* const mac = gost3431195Hmac(key, message);
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
41
75
|
export declare const gost3431195Hmac: (key: TArg<Uint8Array>, msg: TArg<Uint8Array>) => TRet<Uint8Array>;
|
package/dstu9311/hash.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { concatBytes, copyBytes, createHasher } from "@noble/hashes/utils.js";
|
|
1
|
+
import { abytes, aoutput, clean, concatBytes, copyBytes, createHasher } from "@noble/hashes/utils.js";
|
|
2
2
|
import { Dstu9311, DKE_1 } from "../dstu9311/index.js";
|
|
3
3
|
import { bytesToNumberBE, numberToBytesBE } from "@noble/curves/utils.js";
|
|
4
4
|
import { xorBytes } from "../utils.js";
|
|
@@ -39,47 +39,79 @@ const _step = (hin, m, sbox) => {
|
|
|
39
39
|
x.set(chi(x));
|
|
40
40
|
return x;
|
|
41
41
|
};
|
|
42
|
-
/** GOST 34.311-95 hash
|
|
43
|
-
export class
|
|
42
|
+
/** Internal GOST 34.311-95 hash class */
|
|
43
|
+
export class _Gost3431195 {
|
|
44
44
|
sbox;
|
|
45
45
|
blockLen = 32;
|
|
46
46
|
outputLen = 32;
|
|
47
47
|
canXOF = false;
|
|
48
|
-
buffer;
|
|
49
|
-
|
|
48
|
+
buffer = new Uint8Array(32);
|
|
49
|
+
pos = 0;
|
|
50
|
+
h = new Uint8Array(32);
|
|
51
|
+
len = 0n;
|
|
52
|
+
checksum = 0n;
|
|
53
|
+
/** Internal GOST 34.311-95 hash class */
|
|
50
54
|
constructor(sbox = DKE_1) {
|
|
51
55
|
this.sbox = sbox;
|
|
52
|
-
|
|
56
|
+
abytes(sbox, 64, "sbox");
|
|
53
57
|
}
|
|
54
58
|
/** Create hash instance */
|
|
55
|
-
static create() { return new
|
|
56
|
-
destroy() {
|
|
59
|
+
static create() { return new _Gost3431195(); }
|
|
60
|
+
destroy() {
|
|
61
|
+
clean(this.buffer, this.h);
|
|
62
|
+
this.pos = 0;
|
|
63
|
+
this.len = 0n;
|
|
64
|
+
this.checksum = 0n;
|
|
65
|
+
}
|
|
57
66
|
clone() { return this._cloneInto(); }
|
|
58
67
|
_cloneInto(to) {
|
|
59
|
-
to ||= new
|
|
60
|
-
to.buffer = new Uint8Array(this.buffer);
|
|
68
|
+
to ||= new _Gost3431195();
|
|
61
69
|
to.sbox = this.sbox;
|
|
70
|
+
to.buffer.set(this.buffer);
|
|
71
|
+
to.pos = this.pos;
|
|
72
|
+
to.h.set(this.h);
|
|
73
|
+
to.len = this.len;
|
|
74
|
+
to.checksum = this.checksum;
|
|
62
75
|
return to;
|
|
63
76
|
}
|
|
77
|
+
processBlock(block) {
|
|
78
|
+
const rev = copyBytes(block).reverse();
|
|
79
|
+
this.len += 256n;
|
|
80
|
+
this.checksum = (this.checksum + bytesToNumberBE(rev)) & r;
|
|
81
|
+
this.h.set(_step(this.h, rev, this.sbox));
|
|
82
|
+
}
|
|
64
83
|
update(data) {
|
|
65
|
-
|
|
84
|
+
abytes(data);
|
|
85
|
+
let offset = 0;
|
|
86
|
+
if (this.pos > 0) {
|
|
87
|
+
const take = Math.min(this.blockLen - this.pos, data.length);
|
|
88
|
+
this.buffer.set(data.subarray(0, take), this.pos);
|
|
89
|
+
this.pos += take;
|
|
90
|
+
offset = take;
|
|
91
|
+
if (this.pos === this.blockLen) {
|
|
92
|
+
this.processBlock(this.buffer);
|
|
93
|
+
this.pos = 0;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
for (; offset + this.blockLen <= data.length; offset += this.blockLen)
|
|
97
|
+
this.processBlock(data.subarray(offset, offset + this.blockLen));
|
|
98
|
+
if (offset < data.length) {
|
|
99
|
+
this.buffer.set(data.subarray(offset), 0);
|
|
100
|
+
this.pos = data.length - offset;
|
|
101
|
+
}
|
|
66
102
|
return this;
|
|
67
103
|
}
|
|
68
104
|
digestInto(buf) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
checksum = (checksum + bytesToNumberBE(part)) & r;
|
|
77
|
-
if (part.length < this.blockLen)
|
|
78
|
-
part = numberToBytesBE(bytesToNumberBE(part), this.blockLen);
|
|
79
|
-
h.set(_step(h, part, this.sbox));
|
|
105
|
+
aoutput(buf, this);
|
|
106
|
+
if (this.pos > 0) {
|
|
107
|
+
const part = new Uint8Array(this.blockLen);
|
|
108
|
+
part.set(this.buffer.slice(0, this.pos).reverse(), this.blockLen - this.pos);
|
|
109
|
+
this.len += BigInt(this.pos) * 8n;
|
|
110
|
+
this.checksum = (this.checksum + bytesToNumberBE(part)) & r;
|
|
111
|
+
this.h.set(_step(this.h, part, this.sbox));
|
|
80
112
|
}
|
|
81
|
-
|
|
82
|
-
buf.set(
|
|
113
|
+
const res = _step(_step(this.h, numberToBytesBE(this.len, this.blockLen), this.sbox), numberToBytesBE(this.checksum, this.blockLen), this.sbox);
|
|
114
|
+
buf.set(res.reverse());
|
|
83
115
|
this.destroy();
|
|
84
116
|
}
|
|
85
117
|
digest() {
|
|
@@ -88,11 +120,40 @@ export class Gost3431195 {
|
|
|
88
120
|
return buffer;
|
|
89
121
|
}
|
|
90
122
|
}
|
|
91
|
-
/**
|
|
92
|
-
|
|
93
|
-
|
|
123
|
+
/**
|
|
124
|
+
* GOST 34.311-95 hash function
|
|
125
|
+
*
|
|
126
|
+
* @param msg - message bytes to hash.
|
|
127
|
+
* @returns Digest bytes.
|
|
128
|
+
* @example
|
|
129
|
+
* ```ts
|
|
130
|
+
* import { gost3431195 } from "@li0ard/dstu/dstu9311.js";
|
|
131
|
+
*
|
|
132
|
+
* gost3431195(new Uint8Array([97, 98, 99]));
|
|
133
|
+
* gost3431195.create(sbox?).update(new Uint8Array([97, 98, 99])).digest();
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
export const gost3431195 = createHasher(_Gost3431195.create);
|
|
137
|
+
/**
|
|
138
|
+
* HMAC over GOST 34.311-95 hash function
|
|
139
|
+
*
|
|
140
|
+
* @param key - authentication key bytes
|
|
141
|
+
*/
|
|
94
142
|
export class Gost3431195HMAC extends _HMAC {
|
|
95
143
|
constructor(key) { super(gost3431195, key); }
|
|
96
144
|
}
|
|
97
|
-
/**
|
|
145
|
+
/**
|
|
146
|
+
* HMAC over GOST 34.311-95 hash function
|
|
147
|
+
*
|
|
148
|
+
* @param key - authentication key bytes
|
|
149
|
+
* @param message - message bytes to authenticate
|
|
150
|
+
* @returns Authentication tag bytes.
|
|
151
|
+
* @example
|
|
152
|
+
* ```ts
|
|
153
|
+
* import { gost3431195Hmac } from "@li0ard/dstu/dstu9311.js";
|
|
154
|
+
* const key = new Uint8Array([1, 2, 3]);
|
|
155
|
+
* const message = new Uint8Array([4, 5, 6]);
|
|
156
|
+
* const mac = gost3431195Hmac(key, message);
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
98
159
|
export const gost3431195Hmac = (key, msg) => new Gost3431195HMAC(key).update(msg).digest();
|
package/dstu9311/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { concatBytes, createView } from "@noble/hashes/utils.js";
|
|
1
|
+
import { abytes, concatBytes, createView } from "@noble/hashes/utils.js";
|
|
2
2
|
import { DKE_1, keySequences } from "./const.js";
|
|
3
3
|
import { bytesToNumberLE, numberToBytesLE } from "@noble/curves/utils.js";
|
|
4
4
|
const G = (v, sbox) => {
|
|
@@ -33,12 +33,11 @@ export class Dstu9311 {
|
|
|
33
33
|
constructor(key, sbox = DKE_1) {
|
|
34
34
|
this.key = key;
|
|
35
35
|
this.sbox = sbox;
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
abytes(key, this.keySize, "key");
|
|
37
|
+
abytes(sbox, 64, "sbox");
|
|
38
38
|
}
|
|
39
39
|
proceedBlock(block, sequence) {
|
|
40
|
-
|
|
41
|
-
throw new Error("Invalid block size");
|
|
40
|
+
abytes(block, this.blockSize, "block");
|
|
42
41
|
const roundKeys = extendKey(this.key, sequence);
|
|
43
42
|
let a0 = Number(bytesToNumberLE(block.subarray(4, 8))), a1 = Number(bytesToNumberLE(block.subarray(0, 4)));
|
|
44
43
|
for (let i = 0; i < roundKeys.length; i++) {
|
package/iso15946.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { type CHash, type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
|
|
2
|
+
/** ISO/IEC 15946 KDF function */
|
|
3
|
+
export declare const iso15946_kdf: (hash: CHash<Hash<any>>, wrapAlgoOid: string, inputData: TArg<Uint8Array>, ukm?: TArg<Uint8Array>) => TRet<Uint8Array>;
|
package/iso15946.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
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 { abytes, ahash } from "@noble/hashes/utils.js";
|
|
8
|
+
import { AsnConvert, AsnProp, AsnPropTypes } from "@peculiar/asn1-schema";
|
|
9
|
+
import { AlgorithmIdentifier } from "@peculiar/asn1-x509";
|
|
10
|
+
import { _Gost3431195 } from "./dstu9311/hash.js";
|
|
11
|
+
import { astring } from "@noble/curves/utils.js";
|
|
12
|
+
const KEY_LENGTH = new Uint8Array([0, 0, 1, 0]);
|
|
13
|
+
const COUNTER = new Uint8Array([0, 0, 0, 1]);
|
|
14
|
+
class SharedInfo {
|
|
15
|
+
keyInfo = new AlgorithmIdentifier();
|
|
16
|
+
entityInfo;
|
|
17
|
+
suppPubInfo = new ArrayBuffer(0);
|
|
18
|
+
constructor(params = {}) {
|
|
19
|
+
Object.assign(this, params);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
__decorate([
|
|
23
|
+
AsnProp({ type: AlgorithmIdentifier })
|
|
24
|
+
], SharedInfo.prototype, "keyInfo", void 0);
|
|
25
|
+
__decorate([
|
|
26
|
+
AsnProp({ type: AsnPropTypes.OctetString, context: 0, optional: true })
|
|
27
|
+
], SharedInfo.prototype, "entityInfo", void 0);
|
|
28
|
+
__decorate([
|
|
29
|
+
AsnProp({ type: AsnPropTypes.OctetString, context: 2 })
|
|
30
|
+
], SharedInfo.prototype, "suppPubInfo", void 0);
|
|
31
|
+
const removeLeadZeros = (bytes) => {
|
|
32
|
+
const idx = bytes.findIndex(i => i !== 0);
|
|
33
|
+
return idx === -1 ? new Uint8Array() : bytes.slice(idx);
|
|
34
|
+
};
|
|
35
|
+
const encodeSharedInfo = (oid, ukm) => new Uint8Array(AsnConvert.serialize(new SharedInfo({
|
|
36
|
+
keyInfo: new AlgorithmIdentifier({
|
|
37
|
+
algorithm: oid,
|
|
38
|
+
parameters: null
|
|
39
|
+
}),
|
|
40
|
+
entityInfo: ukm?.buffer,
|
|
41
|
+
suppPubInfo: KEY_LENGTH.buffer
|
|
42
|
+
})));
|
|
43
|
+
/** ISO/IEC 15946 KDF function */
|
|
44
|
+
export const iso15946_kdf = (hash, wrapAlgoOid, inputData, ukm) => {
|
|
45
|
+
ahash(hash);
|
|
46
|
+
astring(wrapAlgoOid, "wrapAlgoOid");
|
|
47
|
+
abytes(inputData, undefined, "inputData");
|
|
48
|
+
if (ukm)
|
|
49
|
+
abytes(ukm, undefined, "ukm");
|
|
50
|
+
if (hash.outputLen != 32)
|
|
51
|
+
throw new Error("Invalid hash function. Output length must be 32 bytes");
|
|
52
|
+
const hasher = hash.create();
|
|
53
|
+
return hasher.update(hasher instanceof _Gost3431195
|
|
54
|
+
? removeLeadZeros(inputData)
|
|
55
|
+
: inputData).update(COUNTER).update(encodeSharedInfo(wrapAlgoOid, ukm)).digest();
|
|
56
|
+
};
|
package/kalyna/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type TArg, type TRet } from "@noble/hashes/utils.js";
|
|
2
2
|
import type { Cipher } from "../types.js";
|
|
3
3
|
export declare const column: (x: TArg<BigUint64Array>, i: number, N: number, c1: number, c2: number, c3: number, c4: number, c5: number, c6: number, c7: number, table?: Readonly<TArg<BigUint64Array>[]>, sign?: 1 | -1) => bigint;
|
|
4
4
|
export declare abstract class Kalyna implements Cipher {
|
package/kalyna/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { abool, abytes, anumber } from "@noble/hashes/utils.js";
|
|
1
2
|
import { IS, IT, T, S } from "../const.js";
|
|
2
3
|
import { bytesToUint64sLE, uint64sToBytesLE, byte } from "../utils.js";
|
|
3
4
|
const swap_block = (k, N) => {
|
|
@@ -30,14 +31,15 @@ export class Kalyna {
|
|
|
30
31
|
/** Kalyna abstract class */
|
|
31
32
|
constructor(key, N, isDouble = false) {
|
|
32
33
|
this.N = N;
|
|
34
|
+
anumber(N, "N");
|
|
35
|
+
abool(isDouble, "isDouble");
|
|
33
36
|
if (N < 2 || (N & (N - 1)) !== 0)
|
|
34
37
|
throw new Error("N must be power of 2 and >= 2");
|
|
35
38
|
this.blockSize = N << 3;
|
|
36
39
|
this.keySize = this.blockSize;
|
|
37
40
|
if (isDouble)
|
|
38
41
|
this.keySize *= 2;
|
|
39
|
-
|
|
40
|
-
throw new Error("Invalid key length");
|
|
42
|
+
abytes(key, this.keySize, "key");
|
|
41
43
|
this.wordOffsets = Array.from({ length: 8 }, (_, j) => Math.floor(j * this.N / 8));
|
|
42
44
|
const X = 6 + 4 * Math.log2(N) + (isDouble ? 4 : 0);
|
|
43
45
|
this.numRounds = X - (N > 2 || isDouble ? 1 : 0);
|
|
@@ -151,8 +153,7 @@ export class Kalyna {
|
|
|
151
153
|
}
|
|
152
154
|
/** Encrypt block */
|
|
153
155
|
encrypt(block) {
|
|
154
|
-
|
|
155
|
-
throw new Error(`Incorrect length (need - ${this.blockSize}, got - ${block.length})`);
|
|
156
|
+
abytes(block, this.blockSize, "block");
|
|
156
157
|
const t1 = new BigUint64Array(this.N), t2 = new BigUint64Array(this.N);
|
|
157
158
|
this.addkey(bytesToUint64sLE(block), t1, this.erk);
|
|
158
159
|
for (let i = 0; i < this.numRounds; i++) {
|
|
@@ -167,8 +168,7 @@ export class Kalyna {
|
|
|
167
168
|
}
|
|
168
169
|
/** Decrypt block */
|
|
169
170
|
decrypt(block) {
|
|
170
|
-
|
|
171
|
-
throw new Error(`Incorrect length (need - ${this.blockSize}, got - ${block.length})`);
|
|
171
|
+
abytes(block, this.blockSize, "block");
|
|
172
172
|
const t1 = new BigUint64Array(this.N), t2 = new BigUint64Array(this.N);
|
|
173
173
|
this.subkey(bytesToUint64sLE(block), t1, this.drk.subarray(this.glOffset));
|
|
174
174
|
this.IMC(t1);
|
package/keywrap.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { equalBytes, concatBytes, randomBytes } from "@noble/curves/utils.js";
|
|
1
|
+
import { equalBytes, concatBytes, randomBytes, abytes, abool } from "@noble/curves/utils.js";
|
|
2
2
|
import { Kalyna256 } from "./kalyna/index.js";
|
|
3
3
|
import { cfb, cmac } from "./modes/index.js";
|
|
4
4
|
import { Dstu9311 } from "./dstu9311/index.js";
|
|
@@ -13,19 +13,22 @@ const IV1_9311 = new Uint8Array([
|
|
|
13
13
|
export const keyWrap = (kek, useDstu9311 = false) => {
|
|
14
14
|
if (kek.length != 32)
|
|
15
15
|
throw new Error("Invalid key length");
|
|
16
|
+
abytes(kek, 32, "kek");
|
|
17
|
+
abool(useDstu9311, "useDstu9311");
|
|
16
18
|
const cipher = new (useDstu9311 ? Dstu9311 : Kalyna256)(kek);
|
|
17
19
|
const ivLength = useDstu9311 ? 8 : 32, macLength = useDstu9311 ? 4 : 32;
|
|
18
20
|
const macMode = cmac(cipher, macLength);
|
|
19
21
|
const finalMode = cfb(cipher, useDstu9311 ? IV1_9311 : IV1_KALYNA, 32);
|
|
20
22
|
return Object.freeze({
|
|
21
23
|
wrap: (key, iv) => {
|
|
24
|
+
abytes(key, undefined, "key");
|
|
22
25
|
iv ??= randomBytes(ivLength);
|
|
23
|
-
|
|
24
|
-
throw new Error("Invalid IV length");
|
|
26
|
+
abytes(iv, ivLength, "iv");
|
|
25
27
|
const mac = macMode.compute(key);
|
|
26
28
|
return finalMode.encrypt(concatBytes(iv, cfb(cipher, iv, 32).encrypt(concatBytes(key, mac))).reverse());
|
|
27
29
|
},
|
|
28
30
|
unwrap: (wrappedKey) => {
|
|
31
|
+
abytes(wrappedKey, undefined, "wrappedKey");
|
|
29
32
|
const dec_wrapped = finalMode.decrypt(wrappedKey).reverse();
|
|
30
33
|
const iv = dec_wrapped.subarray(0, ivLength);
|
|
31
34
|
const key_mac = cfb(cipher, iv, 32).decrypt(dec_wrapped.subarray(ivLength));
|