@li0ard/dstu 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -26,6 +26,8 @@ npm i @li0ard/dstu
26
26
  - Supports KMAC
27
27
  - Strumok stream cipher (DSTU 8845:2019)
28
28
  - ECIES encryption schema over Twisted Edwards curves (DSTU 9041:2020)
29
+ - DSTU 9311:2024 cipher (republished DSTU GOST 28147:2009)
30
+ - GOST 34.311-95 hash function over DSTU 9311:2024
29
31
 
30
32
  ## Supported cipher modes
31
33
 
@@ -37,6 +39,7 @@ npm i @li0ard/dstu
37
39
  - Message Authentication Code mode (MAC and GMAC)
38
40
  - Output Feedback mode (OFB)
39
41
  - Key wrapping (KW)
42
+ - Supports [`Dstu7624Wrap`](https://zakon.rada.gov.ua/laws/show/z0108-13)
40
43
  - XEX Tweakable Block Ciphertext Stealing (XTS)
41
44
 
42
45
  ## Supported curves
@@ -107,6 +107,8 @@ export const createField = (m, ks) => {
107
107
  return bMod(le2BN(words));
108
108
  };
109
109
  const mul = (x, v) => {
110
+ if (x.eq(v))
111
+ return sqr(x);
110
112
  const table = new Array(MUL_TABLE_SIZE);
111
113
  table[0] = new BN(0);
112
114
  for (let i = 1; i < MUL_TABLE_SIZE; i++) {
@@ -3,7 +3,7 @@ import { type DSTUParameters } from "./const.js";
3
3
  /** Create DSTU 4145-2002 signer (Big-Endian) */
4
4
  export declare const dstu4145: (parameters: DSTUParameters) => Readonly<{
5
5
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
6
- getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
6
+ getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
7
7
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
8
8
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
9
9
  keygen: (isCompressed?: boolean) => {
@@ -22,7 +22,7 @@ export * from "./ec/expand.js";
22
22
  /** DSTU 4145-2002 163 bit curve */
23
23
  export declare const dstu163: Readonly<{
24
24
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
25
- getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
25
+ getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
26
26
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
27
27
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
28
28
  keygen: (isCompressed?: boolean) => {
@@ -39,7 +39,7 @@ export declare const dstu163: Readonly<{
39
39
  /** DSTU 4145-2002 167 bit curve */
40
40
  export declare const dstu167: Readonly<{
41
41
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
42
- getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
42
+ getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
43
43
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
44
44
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
45
45
  keygen: (isCompressed?: boolean) => {
@@ -56,7 +56,7 @@ export declare const dstu167: Readonly<{
56
56
  /** DSTU 4145-2002 173 bit curve */
57
57
  export declare const dstu173: Readonly<{
58
58
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
59
- getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
59
+ getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
60
60
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
61
61
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
62
62
  keygen: (isCompressed?: boolean) => {
@@ -73,7 +73,7 @@ export declare const dstu173: Readonly<{
73
73
  /** DSTU 4145-2002 179 bit curve */
74
74
  export declare const dstu179: Readonly<{
75
75
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
76
- getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
76
+ getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
77
77
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
78
78
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
79
79
  keygen: (isCompressed?: boolean) => {
@@ -90,7 +90,7 @@ export declare const dstu179: Readonly<{
90
90
  /** DSTU 4145-2002 191 bit curve */
91
91
  export declare const dstu191: Readonly<{
92
92
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
93
- getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
93
+ getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
94
94
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
95
95
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
96
96
  keygen: (isCompressed?: boolean) => {
@@ -107,7 +107,7 @@ export declare const dstu191: Readonly<{
107
107
  /** DSTU 4145-2002 233 bit curve */
108
108
  export declare const dstu233: Readonly<{
109
109
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
110
- getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
110
+ getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
111
111
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
112
112
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
113
113
  keygen: (isCompressed?: boolean) => {
@@ -124,7 +124,7 @@ export declare const dstu233: Readonly<{
124
124
  /** DSTU 4145-2002 257 bit curve */
125
125
  export declare const dstu257: Readonly<{
126
126
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
127
- getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
127
+ getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
128
128
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
129
129
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
130
130
  keygen: (isCompressed?: boolean) => {
@@ -141,7 +141,7 @@ export declare const dstu257: Readonly<{
141
141
  /** DSTU 4145-2002 307 bit curve */
142
142
  export declare const dstu307: Readonly<{
143
143
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
144
- getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
144
+ getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
145
145
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
146
146
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
147
147
  keygen: (isCompressed?: boolean) => {
@@ -158,7 +158,7 @@ export declare const dstu307: Readonly<{
158
158
  /** DSTU 4145-2002 367 bit curve */
159
159
  export declare const dstu367: Readonly<{
160
160
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
161
- getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
161
+ getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
162
162
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
163
163
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
164
164
  keygen: (isCompressed?: boolean) => {
@@ -175,7 +175,7 @@ export declare const dstu367: Readonly<{
175
175
  /** DSTU 4145-2002 431 bit curve */
176
176
  export declare const dstu431: Readonly<{
177
177
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
178
- getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
178
+ getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
179
179
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
180
180
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
181
181
  keygen: (isCompressed?: boolean) => {
package/dstu4145/index.js CHANGED
@@ -6,27 +6,37 @@ import BN from "bn.js";
6
6
  export const dstu4145 = (parameters) => {
7
7
  const curve = binaryWeierstrass(parameters);
8
8
  const { Field, Point, MASK, lengths } = curve;
9
+ /**
10
+ * Computes public key for a secret key
11
+ * @param isCompressed - whether to return compact (default), or full key
12
+ * @returns Public key, full when `isCompressed=false`; short when `isCompressed=true`
13
+ */
9
14
  const getPublicKey = (secretKey, isCompressed = false) => Point.BASE.mul(Field.fromHexStringOrBytes(secretKey)).negate().toBytes(isCompressed);
10
15
  const randomPrivateKey = () => Field.toBytes(new BN(randomBytes(lengths.scalarByteLength)).imaskn(MASK), lengths.scalarByteLength);
16
+ /**
17
+ * Computes presign (ephermeral keypair)
18
+ * @param rand Predefined secret ephermeral key
19
+ * @returns Ephemeral public key (`Fe`) and secret key (`e`)
20
+ */
11
21
  const computePresign = (rand) => {
12
- const e = (rand
13
- ? new BN(rand)
14
- : new BN(randomBytes(lengths.scalarByteLength))).imaskn(MASK);
22
+ const e = new BN(rand ?? randomBytes(lengths.scalarByteLength)).imaskn(MASK);
15
23
  if (rand && e.isZero())
16
24
  throw new Error("Invalid custom rand for presign (rand = 0)");
17
- if (e.isZero())
25
+ else if (e.isZero())
18
26
  return computePresign(rand);
19
27
  const Fe = Point.BASE.mul(e).x;
20
28
  if (Fe.isZero())
21
29
  return computePresign(rand);
22
30
  return { Fe, e };
23
31
  };
32
+ /** Converts hash into field element */
24
33
  const prepareHash = (digest) => {
25
34
  let h = curve.toInternalField(Field.hashToField(digest));
26
35
  if (h.isZero())
27
36
  h = new BN(1);
28
37
  return h;
29
38
  };
39
+ /** Signs a message hash with a secret key. */
30
40
  const sign = (secretKey, digest, rand) => {
31
41
  const d = new BN(secretKey), h = prepareHash(digest), { Fe, e } = computePresign(rand);
32
42
  if (d.isZero() || d.gte(curve.ORDER))
@@ -39,6 +49,7 @@ export const dstu4145 = (parameters) => {
39
49
  return sign(secretKey, digest, rand);
40
50
  return concatBytes(Field.toBytes(s, lengths.scalarByteLength), Field.toBytes(r, lengths.scalarByteLength));
41
51
  };
52
+ /** Verifies a signature against message hash and public key. */
42
53
  const verify = (publicKey, digest, signature) => {
43
54
  if (signature.length != lengths.signatureByteLength)
44
55
  throw new Error("Invalid signature length");
@@ -51,7 +62,11 @@ export const dstu4145 = (parameters) => {
51
62
  const y = curve.toExternalField(Field.mul(h, R.x)).imaskn(MASK);
52
63
  return y.eq(r);
53
64
  };
54
- const getSharedKey = (secretKeyA, publicKeyB, withCofactor = true) => {
65
+ /**
66
+ * Computes shared secret key
67
+ * @param withCofactor Multiply result by curve cofactor? (default - `true`)
68
+ */
69
+ const getSharedSecret = (secretKeyA, publicKeyB, withCofactor = true) => {
55
70
  const d = new BN(secretKeyA), Q = Point.fromBytes(publicKeyB);
56
71
  if (d.isZero() || d.gte(curve.ORDER))
57
72
  throw new Error("Invalid private key, must be in range 1 < key < order");
@@ -69,7 +84,7 @@ export const dstu4145 = (parameters) => {
69
84
  };
70
85
  return Object.freeze({
71
86
  getPublicKey,
72
- getSharedKey,
87
+ getSharedSecret,
73
88
  sign,
74
89
  verify,
75
90
  keygen,
@@ -6,7 +6,7 @@ export declare const dstu9041_256: Readonly<{
6
6
  secretKey: TRet<Uint8Array>;
7
7
  publicKey: TRet<Uint8Array>;
8
8
  };
9
- encrypt: (message: TArg<Uint8Array>, publicKey: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
9
+ encrypt: (plaintext: TArg<Uint8Array>, publicKey: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
10
10
  decrypt: (ciphertext: TArg<Uint8Array>, privateKey: TArg<Uint8Array>) => TRet<Uint8Array>;
11
11
  }>;
12
12
  /** DSTU 9041:2020 (512 bit) */
@@ -16,6 +16,6 @@ export declare const dstu9041_512: Readonly<{
16
16
  secretKey: TRet<Uint8Array>;
17
17
  publicKey: TRet<Uint8Array>;
18
18
  };
19
- encrypt: (message: TArg<Uint8Array>, publicKey: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
19
+ encrypt: (plaintext: TArg<Uint8Array>, publicKey: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
20
20
  decrypt: (ciphertext: TArg<Uint8Array>, privateKey: TArg<Uint8Array>) => TRet<Uint8Array>;
21
21
  }>;
package/dstu9041/index.js CHANGED
@@ -1,34 +1,45 @@
1
1
  import { bytesToNumberBE, concatBytes, equalBytes, numberToBytesBE } from "@noble/curves/utils.js";
2
2
  import { kupyna256 } from "../kupyna/index.js";
3
- import { curve256, curve512 } from "./curve.js";
3
+ import { curve256, curve512, dstu9041Curve } from "./curve.js";
4
4
  import { kw } from "../modes/index.js";
5
5
  import { Kalyna256, Kalyna512 } from "../kalyna/index.js";
6
6
  import { randomBytes } from "@noble/hashes/utils.js";
7
7
  const ID_KUPYNA256 = 1;
8
8
  const dstu9041 = (length) => {
9
- const MAX_LENGTH = length == 256 ? 200 : 424;
10
- const PADDED_MESSAGE_BYTES = MAX_LENGTH / 8;
11
- const HASH_LENGTH_BYTES = length == 256 ? 4 : 8;
12
- const STRUCT_BYTES = 1 + HASH_LENGTH_BYTES + 2 + PADDED_MESSAGE_BYTES;
13
- const curve = length == 256 ? curve256 : curve512;
14
- const cipher = length == 256 ? Kalyna256 : Kalyna512;
15
- const padMessage = (message, messageBits) => {
16
- if (messageBits == 0)
9
+ let MAX_LENGTH, HASH_LENGTH_BYTES, curve, Сipher;
10
+ switch (length) {
11
+ case 256:
12
+ MAX_LENGTH = 200;
13
+ HASH_LENGTH_BYTES = 4;
14
+ curve = curve256;
15
+ Сipher = Kalyna256;
16
+ break;
17
+ case 512:
18
+ MAX_LENGTH = 424;
19
+ HASH_LENGTH_BYTES = 8;
20
+ curve = curve512;
21
+ Сipher = Kalyna512;
22
+ break;
23
+ default:
24
+ throw new Error("Invalid length, supported only 256 and 512 bits");
25
+ }
26
+ const PADDED_MESSAGE_BYTES = MAX_LENGTH / 8, STRUCT_BYTES = 1 + HASH_LENGTH_BYTES + 2 + PADDED_MESSAGE_BYTES, { Point } = curve, { Fp, Fn } = Point;
27
+ const padMessage = (message) => {
28
+ if (message.length == 0)
17
29
  throw new Error("Invalid message length");
18
- if (messageBits > MAX_LENGTH)
30
+ if (message.length > PADDED_MESSAGE_BYTES)
19
31
  throw new Error("Message too long");
20
- const messageBytes = Math.ceil(messageBits / 8);
21
- if (message.length != messageBytes)
22
- throw new Error("Message length mismatch");
23
32
  const paddedMessage = new Uint8Array(PADDED_MESSAGE_BYTES);
24
- paddedMessage.set(message, PADDED_MESSAGE_BYTES - messageBytes);
33
+ paddedMessage.set(message, PADDED_MESSAGE_BYTES - message.length);
25
34
  return paddedMessage;
26
35
  };
27
- const buildStruct = (hashId, paddedMessage, messageLength) => {
28
- const digest = kupyna256(concatBytes(messageLength, paddedMessage));
36
+ const buildStruct = (message) => {
37
+ const paddedMessage = padMessage(message);
38
+ const messageLength = numberToBytesBE(message.length * 8, 2);
39
+ const digest = kupyna256(concatBytes(messageLength, paddedMessage)).subarray(-HASH_LENGTH_BYTES);
29
40
  const struct = new Uint8Array(STRUCT_BYTES);
30
- struct[0] = hashId;
31
- struct.set(digest.subarray(digest.length - HASH_LENGTH_BYTES), 1);
41
+ struct[0] = ID_KUPYNA256;
42
+ struct.set(digest, 1);
32
43
  struct.set(messageLength, 1 + HASH_LENGTH_BYTES);
33
44
  struct.set(paddedMessage, 3 + HASH_LENGTH_BYTES);
34
45
  return struct;
@@ -36,20 +47,19 @@ const dstu9041 = (length) => {
36
47
  const parseStruct = (struct) => {
37
48
  if (struct.length != STRUCT_BYTES)
38
49
  throw new Error("Invalid ciphertext");
39
- const hashId = struct[0];
40
- if (hashId != ID_KUPYNA256)
50
+ if (struct[0] != ID_KUPYNA256)
41
51
  throw new Error("Invalid hash ID");
42
52
  const embeddedHash = struct.subarray(1, HASH_LENGTH_BYTES + 1);
43
53
  const messageLength = struct.subarray(1 + HASH_LENGTH_BYTES, 3 + HASH_LENGTH_BYTES);
44
- const paddedMessage = new Uint8Array(PADDED_MESSAGE_BYTES);
45
- paddedMessage.set(struct.subarray(3 + HASH_LENGTH_BYTES));
54
+ const paddedMessage = struct.subarray(3 + HASH_LENGTH_BYTES);
46
55
  const messageBits = Number(bytesToNumberBE(messageLength));
47
56
  if (messageBits == 0)
48
57
  throw new Error("Invalid message length");
49
58
  if (messageBits > MAX_LENGTH)
50
59
  throw new Error("Message too long");
51
- const digest = kupyna256(concatBytes(messageLength, paddedMessage));
52
- if (!equalBytes(embeddedHash, digest.subarray(digest.length - HASH_LENGTH_BYTES)))
60
+ // message len + padded message
61
+ const digest = kupyna256(struct.subarray(1 + HASH_LENGTH_BYTES)).subarray(-HASH_LENGTH_BYTES);
62
+ if (!equalBytes(embeddedHash, digest))
53
63
  throw new Error("Integrity check failed, hash mismatch");
54
64
  const messageBytes = Math.ceil(messageBits / 8);
55
65
  const paddingLen = PADDED_MESSAGE_BYTES - messageBytes;
@@ -58,37 +68,31 @@ const dstu9041 = (length) => {
58
68
  invalidPadding |= (i < paddingLen ? 1 : 0) & (paddedMessage[i] != 0 ? 1 : 0);
59
69
  if (invalidPadding != 0)
60
70
  throw new Error("Invalid padding");
61
- return paddedMessage.subarray(paddingLen);
71
+ return paddedMessage.slice(paddingLen);
62
72
  };
63
73
  return Object.freeze({
64
74
  getPublicKey: curve.getPublicKey,
65
75
  keygen: curve.keygen,
66
- encrypt: (message, publicKey, rand) => {
67
- const message_bits = message.length * 8;
68
- const paddedMessage = padMessage(message, message_bits);
69
- const struct = buildStruct(ID_KUPYNA256, paddedMessage, numberToBytesBE(message_bits, 2));
70
- const e = curve.Point.Fn.create(bytesToNumberBE(rand ?? randomBytes(STRUCT_BYTES)));
71
- const R = curve.Point.BASE.multiply(e);
72
- const r = curve.Point.Fp.toBytes(R.x);
73
- const T = curve.Point.fromBytes(publicKey).multiply(e);
74
- const key = curve.Point.Fp.toBytes(T.x);
75
- const plaintext = new Uint8Array(2 * STRUCT_BYTES);
76
- plaintext.set(struct);
77
- const ciphertext = kw(new cipher(key)).wrap(plaintext);
76
+ encrypt: (plaintext, publicKey, rand) => {
77
+ const struct = buildStruct(plaintext);
78
+ const e = Fn.create(bytesToNumberBE(rand ?? randomBytes(STRUCT_BYTES)));
79
+ const r = Fp.toBytes(Point.BASE.multiply(e).x);
80
+ const key = Fp.toBytes(Point.fromBytes(publicKey).multiply(e).x);
81
+ const plaintext_kw = new Uint8Array(2 * STRUCT_BYTES);
82
+ plaintext_kw.set(struct);
83
+ const ciphertext = kw(new Сipher(key)).wrap(plaintext_kw);
78
84
  return concatBytes(r, ciphertext);
79
85
  },
80
86
  decrypt: (ciphertext, privateKey) => {
81
87
  if (ciphertext.length != length / 2)
82
88
  throw new Error("Invalid ciphertext length");
83
89
  const r = bytesToNumberBE(ciphertext.subarray(0, STRUCT_BYTES));
84
- if (!curve.Point.Fp.isValidNot0(r))
90
+ if (!Fp.isValidNot0(r))
85
91
  throw new Error("Invalid ciphertext");
86
- const R = curve.Point.fromX(r);
87
- const T = R.multiply(bytesToNumberBE(privateKey));
88
- const key = curve.Point.Fp.toBytes(T.x);
89
- // KW already does unpadding
90
- const plaintext = kw(new cipher(key)).unwrap(ciphertext.subarray(STRUCT_BYTES));
91
- return parseStruct(plaintext);
92
+ const T = Point.fromX(r).multiply(bytesToNumberBE(privateKey));
93
+ const key = Fp.toBytes(T.x);
94
+ const plaintext = kw(new Сipher(key)).unwrap(ciphertext.subarray(STRUCT_BYTES));
95
+ return parseStruct(plaintext.subarray(0, STRUCT_BYTES));
92
96
  }
93
97
  });
94
98
  };
@@ -0,0 +1,29 @@
1
+ /** S-Box #1 */
2
+ export declare const DKE_1: Uint8Array<ArrayBuffer>;
3
+ /** S-Box #2 */
4
+ export declare const DKE_2: Uint8Array<ArrayBuffer>;
5
+ /** S-Box #3 */
6
+ export declare const DKE_3: Uint8Array<ArrayBuffer>;
7
+ /** S-Box #4 */
8
+ export declare const DKE_4: Uint8Array<ArrayBuffer>;
9
+ /** S-Box #5 */
10
+ export declare const DKE_5: Uint8Array<ArrayBuffer>;
11
+ /** S-Box #6 */
12
+ export declare const DKE_6: Uint8Array<ArrayBuffer>;
13
+ /** S-Box #7 */
14
+ export declare const DKE_7: Uint8Array<ArrayBuffer>;
15
+ /** S-Box #8 */
16
+ export declare const DKE_8: Uint8Array<ArrayBuffer>;
17
+ /** S-Box #9 */
18
+ export declare const DKE_9: Uint8Array<ArrayBuffer>;
19
+ /** S-Box #10 */
20
+ export declare const DKE_10: Uint8Array<ArrayBuffer>;
21
+ /** Test S-Box for DSTU GOST 28147:2009 */
22
+ export declare const GOST_28147_89_TEST_PARAM_SET: Uint8Array<ArrayBuffer>;
23
+ /** Test S-Box #2 for DSTU GOST 28147:2009 */
24
+ export declare const GOSTR_3411_94_TEST_PARAM_SET: Uint8Array<ArrayBuffer>;
25
+ export declare const keySequences: {
26
+ ENCRYPT: number[];
27
+ DECRYPT: number[];
28
+ MAC: number[];
29
+ };
@@ -0,0 +1,150 @@
1
+ /** S-Box #1 */
2
+ export const DKE_1 = new Uint8Array([
3
+ 0xA9, 0xD6, 0xEB, 0x45, 0xF1, 0x3C, 0x70, 0x82,
4
+ 0x80, 0xC4, 0x96, 0x7B, 0x23, 0x1F, 0x5E, 0xAD,
5
+ 0xF6, 0x58, 0xEB, 0xA4, 0xC0, 0x37, 0x29, 0x1D,
6
+ 0x38, 0xD9, 0x6B, 0xF0, 0x25, 0xCA, 0x4E, 0x17,
7
+ 0xF8, 0xE9, 0x72, 0x0D, 0xC6, 0x15, 0xB4, 0x3A,
8
+ 0x28, 0x97, 0x5F, 0x0B, 0xC1, 0xDE, 0xA3, 0x64,
9
+ 0x38, 0xB5, 0x64, 0xEA, 0x2C, 0x17, 0x9F, 0xD0,
10
+ 0x12, 0x3E, 0x6D, 0xB8, 0xFA, 0xC5, 0x79, 0x04
11
+ ]);
12
+ /** S-Box #2 */
13
+ export const DKE_2 = new Uint8Array([
14
+ 0xE9, 0x37, 0xF4, 0xCB, 0x6A, 0xD1, 0x05, 0x82,
15
+ 0xAD, 0xC7, 0x6E, 0x81, 0xF3, 0xB4, 0x09, 0x52,
16
+ 0x4B, 0x1F, 0x92, 0xEC, 0x6A, 0x87, 0x35, 0x0D,
17
+ 0x45, 0x1C, 0x7E, 0x92, 0xAF, 0xBD, 0x08, 0x63,
18
+ 0xCB, 0x39, 0xF0, 0x45, 0x72, 0xED, 0x1A, 0x86,
19
+ 0x87, 0x3A, 0x96, 0xE5, 0xD0, 0x4C, 0x12, 0xFB,
20
+ 0xF0, 0xE6, 0x8D, 0x59, 0xA3, 0x1C, 0x4B, 0x72,
21
+ 0x43, 0xED, 0x50, 0x2B, 0x1A, 0x76, 0x9F, 0x8C
22
+ ]);
23
+ /** S-Box #3 */
24
+ export const DKE_3 = new Uint8Array([
25
+ 0xD9, 0x1E, 0x72, 0xC5, 0x4B, 0x6F, 0x38, 0xA0,
26
+ 0x78, 0x6B, 0x03, 0x4D, 0x95, 0xFE, 0xAC, 0x21,
27
+ 0xA5, 0x3C, 0x98, 0xD6, 0x4F, 0xE0, 0x2B, 0x17,
28
+ 0xBA, 0xC1, 0x56, 0x9E, 0x2D, 0xF7, 0x04, 0x38,
29
+ 0x5B, 0x30, 0xF9, 0xE4, 0x1C, 0x86, 0x2A, 0x7D,
30
+ 0x43, 0xBD, 0x1F, 0x82, 0x7E, 0xC9, 0xA0, 0x65,
31
+ 0x37, 0x8B, 0x1E, 0x50, 0xD4, 0xCA, 0x29, 0xF6,
32
+ 0x6D, 0xCA, 0xB7, 0x93, 0xFE, 0x12, 0x08, 0x45
33
+ ]);
34
+ /** S-Box #4 */
35
+ export const DKE_4 = new Uint8Array([
36
+ 0x9C, 0x3D, 0x76, 0xE1, 0xA2, 0x04, 0x8F, 0x5B,
37
+ 0xA5, 0xBE, 0x76, 0x0C, 0x28, 0xF4, 0xD3, 0x91,
38
+ 0x4C, 0x30, 0xD2, 0xEB, 0x7F, 0x59, 0x18, 0xA6,
39
+ 0x39, 0x45, 0xE7, 0x86, 0xD0, 0x2F, 0xBC, 0xA1,
40
+ 0x29, 0xCF, 0xDB, 0x41, 0x75, 0x3E, 0x68, 0xA0,
41
+ 0xE5, 0xDB, 0x19, 0x42, 0xF8, 0x70, 0x3C, 0xA6,
42
+ 0xE6, 0x5A, 0x9D, 0x48, 0xBC, 0x03, 0x71, 0xF2,
43
+ 0x19, 0xCB, 0x76, 0x83, 0x2F, 0xE0, 0x5A, 0x4D
44
+ ]);
45
+ /** S-Box #5 */
46
+ export const DKE_5 = new Uint8Array([
47
+ 0x34, 0xD8, 0xC7, 0xA2, 0x0E, 0x9F, 0xB1, 0x56,
48
+ 0xC7, 0x69, 0x38, 0xB5, 0xFA, 0x0D, 0x42, 0x1E,
49
+ 0xE4, 0x87, 0xB3, 0xAC, 0x12, 0x69, 0xDF, 0x05,
50
+ 0x39, 0x6D, 0x8F, 0xA2, 0x7E, 0xC0, 0xB4, 0x15,
51
+ 0x5C, 0xA7, 0x21, 0xFD, 0xE3, 0xB4, 0x08, 0x96,
52
+ 0x18, 0xBE, 0x74, 0xA0, 0xC3, 0x5D, 0x9F, 0x62,
53
+ 0x9B, 0xAD, 0x5E, 0x23, 0x06, 0x4C, 0xF1, 0x78,
54
+ 0xE9, 0x18, 0x5F, 0xB0, 0x62, 0xC7, 0xA4, 0xD3
55
+ ]);
56
+ /** S-Box #6 */
57
+ export const DKE_6 = new Uint8Array([
58
+ 0xFC, 0x96, 0xE2, 0x1B, 0x0D, 0x4A, 0x78, 0x35,
59
+ 0xEC, 0x50, 0x74, 0xA3, 0x26, 0x1D, 0x9B, 0xF8,
60
+ 0x56, 0xD9, 0xBE, 0xA3, 0xF2, 0x81, 0x40, 0x7C,
61
+ 0x1F, 0x74, 0x2E, 0xC3, 0x6B, 0x98, 0x05, 0xAD,
62
+ 0xF9, 0xE6, 0xD1, 0x58, 0x42, 0x3C, 0xAB, 0x07,
63
+ 0xB0, 0xD7, 0xCE, 0x14, 0x23, 0x68, 0xA5, 0xF9,
64
+ 0x7E, 0xF8, 0xD0, 0xB3, 0xA1, 0x42, 0x9C, 0x65,
65
+ 0x15, 0xEB, 0x2C, 0x38, 0xA0, 0x97, 0xF6, 0x4D
66
+ ]);
67
+ /** S-Box #7 */
68
+ export const DKE_7 = new Uint8Array([
69
+ 0xFD, 0xA5, 0xC0, 0x16, 0x92, 0xE7, 0x3B, 0x48,
70
+ 0x25, 0xA0, 0x69, 0x1F, 0xD4, 0x7E, 0xB3, 0x8C,
71
+ 0x3E, 0x4B, 0x59, 0x12, 0xF6, 0x8D, 0x70, 0xAC,
72
+ 0x4A, 0xB9, 0xF2, 0xE5, 0xD1, 0x36, 0x07, 0xC8,
73
+ 0xF6, 0x58, 0x97, 0xCB, 0x0A, 0x31, 0x24, 0xDE,
74
+ 0xCB, 0xF4, 0x51, 0xE9, 0x08, 0xD2, 0xA7, 0x36,
75
+ 0xD2, 0x48, 0xBC, 0x13, 0xA5, 0x9E, 0x7F, 0x06,
76
+ 0x15, 0x0F, 0x6A, 0x3E, 0x72, 0xCD, 0xB8, 0x94
77
+ ]);
78
+ /** S-Box #8 */
79
+ export const DKE_8 = new Uint8Array([
80
+ 0xE4, 0xB2, 0x87, 0x5C, 0x9D, 0x03, 0x1F, 0x6A,
81
+ 0x3E, 0xCA, 0x62, 0xD1, 0x98, 0x74, 0x0F, 0x5B,
82
+ 0x52, 0x87, 0x1F, 0xE6, 0x4D, 0xB0, 0xA3, 0xC9,
83
+ 0xCA, 0x7D, 0xE3, 0x02, 0x95, 0x16, 0xB4, 0xF8,
84
+ 0x63, 0xF7, 0x09, 0xA8, 0xBC, 0x41, 0x52, 0xDE,
85
+ 0x6D, 0xF1, 0x53, 0x80, 0xBA, 0xE4, 0x9C, 0x27,
86
+ 0x2F, 0xC5, 0xB1, 0x3E, 0x06, 0xDA, 0x79, 0x48,
87
+ 0x30, 0x5C, 0x8F, 0xDE, 0xB6, 0x29, 0x71, 0x4A
88
+ ]);
89
+ /** S-Box #9 */
90
+ export const DKE_9 = new Uint8Array([
91
+ 0x90, 0xBC, 0x24, 0x3F, 0xD6, 0xE1, 0xA7, 0x58,
92
+ 0x35, 0x0F, 0x87, 0xEC, 0xDA, 0x16, 0xB2, 0x49,
93
+ 0x84, 0x5A, 0xEB, 0xD6, 0xCF, 0x79, 0x31, 0x20,
94
+ 0x54, 0xF0, 0xCB, 0xA9, 0x1E, 0x86, 0x32, 0xD7,
95
+ 0x7C, 0x30, 0x68, 0xEB, 0x1F, 0xDA, 0x95, 0x24,
96
+ 0x74, 0x3B, 0x6A, 0x81, 0x9C, 0xED, 0x0F, 0x25,
97
+ 0x7E, 0x9F, 0x14, 0x83, 0xBD, 0x02, 0x6A, 0x5C,
98
+ 0xE2, 0x8F, 0x30, 0x7C, 0xBD, 0x15, 0x64, 0x9A
99
+ ]);
100
+ /** S-Box #10 */
101
+ export const DKE_10 = new Uint8Array([
102
+ 0x84, 0x69, 0xBC, 0x12, 0x37, 0xE0, 0xDA, 0xF5,
103
+ 0x7D, 0x18, 0xAE, 0x4F, 0x90, 0x63, 0x2C, 0xB5,
104
+ 0xC8, 0xD1, 0xA2, 0x96, 0x34, 0xE7, 0x5F, 0x0B,
105
+ 0x2B, 0x34, 0xC7, 0x9D, 0xF8, 0x50, 0x1E, 0xA6,
106
+ 0x83, 0xDA, 0xEF, 0x51, 0x47, 0xBC, 0x20, 0x69,
107
+ 0x4C, 0x9B, 0xEA, 0x76, 0x35, 0x0F, 0x12, 0x8D,
108
+ 0x58, 0xE7, 0x30, 0x1D, 0xA6, 0x92, 0xFB, 0xC4,
109
+ 0xA3, 0x59, 0x0D, 0x78, 0xC4, 0x16, 0xBF, 0x2E
110
+ ]);
111
+ /** Test S-Box for DSTU GOST 28147:2009 */
112
+ export const GOST_28147_89_TEST_PARAM_SET = new Uint8Array([
113
+ 0x42, 0xF5, 0x91, 0x08, 0xE3, 0xBC, 0xD7, 0xA6,
114
+ 0xC9, 0xFE, 0x81, 0x3A, 0x27, 0x4D, 0x60, 0xB5,
115
+ 0xD8, 0xEC, 0x73, 0x9A, 0x15, 0x24, 0x6F, 0x0B,
116
+ 0xE9, 0xB2, 0x5F, 0x71, 0x0D, 0xC6, 0xA4, 0x38,
117
+ 0x3E, 0x59, 0x68, 0x0D, 0xAB, 0x7C, 0x21, 0xF4,
118
+ 0x8F, 0x6B, 0x19, 0xC5, 0xD3, 0x7A, 0x0E, 0x24,
119
+ 0x9B, 0xC0, 0x36, 0x75, 0x48, 0xEF, 0x1A, 0x2D,
120
+ 0xC6, 0x52, 0xB0, 0x9D, 0x3E, 0x7A, 0xF4, 0x18
121
+ ]);
122
+ /** Test S-Box #2 for DSTU GOST 28147:2009 */
123
+ export const GOSTR_3411_94_TEST_PARAM_SET = new Uint8Array([
124
+ 0x4A, 0x92, 0xD8, 0x0E, 0x6B, 0x1C, 0x7F, 0x53,
125
+ 0xEB, 0x4C, 0x6D, 0xFA, 0x23, 0x81, 0x07, 0x59,
126
+ 0x58, 0x1D, 0xA3, 0x42, 0xEF, 0xC7, 0x60, 0x9B,
127
+ 0x7D, 0xA1, 0x08, 0x9F, 0xE4, 0x6C, 0xB2, 0x53,
128
+ 0x6C, 0x71, 0x5F, 0xD8, 0x4A, 0x9E, 0x03, 0xB2,
129
+ 0x4B, 0xA0, 0x72, 0x1D, 0x36, 0x85, 0x9C, 0xFE,
130
+ 0xDB, 0x41, 0x3F, 0x59, 0x0A, 0xE7, 0x68, 0x2C,
131
+ 0x1F, 0xD0, 0x57, 0xA4, 0x92, 0x3E, 0x6B, 0x8C
132
+ ]);
133
+ export const keySequences = {
134
+ ENCRYPT: [
135
+ 0, 1, 2, 3, 4, 5, 6, 7,
136
+ 0, 1, 2, 3, 4, 5, 6, 7,
137
+ 0, 1, 2, 3, 4, 5, 6, 7,
138
+ 7, 6, 5, 4, 3, 2, 1, 0
139
+ ],
140
+ DECRYPT: [
141
+ 0, 1, 2, 3, 4, 5, 6, 7,
142
+ 7, 6, 5, 4, 3, 2, 1, 0,
143
+ 7, 6, 5, 4, 3, 2, 1, 0,
144
+ 7, 6, 5, 4, 3, 2, 1, 0
145
+ ],
146
+ MAC: [
147
+ 0, 1, 2, 3, 4, 5, 6, 7,
148
+ 0, 1, 2, 3, 4, 5, 6, 7
149
+ ]
150
+ };
@@ -0,0 +1,34 @@
1
+ import { type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
2
+ /** GOST R 34.311-95 hash function */
3
+ export declare class Gost3431195 implements Hash<Gost3431195> {
4
+ private sbox;
5
+ readonly blockLen = 32;
6
+ readonly outputLen = 32;
7
+ readonly canXOF = false;
8
+ private buffer;
9
+ /** GOST R 34.311-95 hash function */
10
+ constructor(sbox?: TArg<Uint8Array>);
11
+ /** Create hash instance */
12
+ static create(): Gost3431195;
13
+ destroy(): void;
14
+ clone(): Gost3431195;
15
+ _cloneInto(to?: Gost3431195): Gost3431195;
16
+ update(data: TArg<Uint8Array>): this;
17
+ digestInto(buf: TArg<Uint8Array>): void;
18
+ digest(): TRet<Uint8Array>;
19
+ }
20
+ /** GOST R 34.311-95 hash function */
21
+ export declare const gost3431195: {
22
+ outputLen: number;
23
+ blockLen: number;
24
+ canXOF: boolean;
25
+ } & import("@noble/hashes/utils.js").HashInfo & {
26
+ (msg: TArg<Uint8Array>): TRet<Uint8Array>;
27
+ create(): Gost3431195;
28
+ } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
29
+ outputLen: number;
30
+ blockLen: number;
31
+ canXOF: boolean;
32
+ oid?: TRet<Uint8Array> | undefined;
33
+ create: () => Gost3431195;
34
+ };