@li0ard/dstu 0.1.5 → 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.
Files changed (50) hide show
  1. package/README.md +5 -0
  2. package/dstu4145/const.d.ts +2 -0
  3. package/dstu4145/const.js +17 -0
  4. package/dstu4145/ec/index.d.ts +8 -4
  5. package/dstu4145/ec/index.js +24 -6
  6. package/dstu4145/ec/math.js +2 -0
  7. package/dstu4145/index.d.ts +11 -0
  8. package/dstu4145/index.js +35 -6
  9. package/dstu9041/curve.js +13 -10
  10. package/dstu9041/index.d.ts +2 -2
  11. package/dstu9041/index.js +48 -44
  12. package/dstu9311/const.d.ts +29 -0
  13. package/dstu9311/const.js +150 -0
  14. package/dstu9311/hash.d.ts +34 -0
  15. package/dstu9311/hash.js +91 -0
  16. package/dstu9311/index.d.ts +16 -0
  17. package/dstu9311/index.js +59 -0
  18. package/index.d.ts +2 -0
  19. package/index.js +2 -0
  20. package/keywrap.d.ts +6 -0
  21. package/keywrap.js +34 -0
  22. package/modes/cbc.d.ts +2 -3
  23. package/modes/cbc.js +6 -5
  24. package/modes/cfb.d.ts +3 -4
  25. package/modes/cfb.js +88 -56
  26. package/modes/cmac.d.ts +2 -0
  27. package/modes/cmac.js +45 -0
  28. package/modes/ctr.d.ts +2 -3
  29. package/modes/ctr.js +25 -14
  30. package/modes/ecb.d.ts +2 -3
  31. package/modes/gcm.d.ts +2 -3
  32. package/modes/gcm.js +18 -14
  33. package/modes/gmac.d.ts +3 -0
  34. package/modes/gmac.js +40 -0
  35. package/modes/index.d.ts +3 -1
  36. package/modes/index.js +3 -1
  37. package/modes/kw.d.ts +5 -4
  38. package/modes/kw.js +63 -56
  39. package/modes/ofb.d.ts +2 -3
  40. package/modes/ofb.js +3 -2
  41. package/modes/xts.d.ts +7 -0
  42. package/modes/xts.js +93 -0
  43. package/package.json +3 -2
  44. package/strumok/index.d.ts +4 -3
  45. package/strumok/index.js +42 -277
  46. package/types.d.ts +5 -3
  47. package/utils.d.ts +5 -0
  48. package/utils.js +14 -2
  49. package/modes/mac.d.ts +0 -6
  50. package/modes/mac.js +0 -61
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
+ };
@@ -0,0 +1,91 @@
1
+ import { concatBytes, copyBytes, createHasher } from "@noble/hashes/utils.js";
2
+ import { Dstu9311, DKE_1 } from "../dstu9311/index.js";
3
+ import { bytesToNumberBE, numberToBytesBE } from "@noble/curves/utils.js";
4
+ import { xorBytes } from "../utils.js";
5
+ const r = (1n << 256n) - 1n;
6
+ const C3 = new Uint8Array([
7
+ 0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0xff,
8
+ 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0xff, 0x00,
9
+ 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
10
+ 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00
11
+ ]);
12
+ const A = (x) => concatBytes(xorBytes(x.subarray(24, 32), x.subarray(16, 24)), x.subarray(0, 24));
13
+ const P = (x) => new Uint8Array([
14
+ x[31], x[23], x[15], x[7], x[30], x[22], x[14], x[6],
15
+ x[29], x[21], x[13], x[5], x[28], x[20], x[12], x[4],
16
+ x[27], x[19], x[11], x[3], x[26], x[18], x[10], x[2],
17
+ x[25], x[17], x[9], x[1], x[24], x[16], x[8], x[0]
18
+ ]);
19
+ const chi = (Y) => new Uint8Array([
20
+ Y[30] ^ Y[28] ^ Y[26] ^ Y[24] ^ Y[6] ^ Y[0],
21
+ Y[31] ^ Y[29] ^ Y[27] ^ Y[25] ^ Y[7] ^ Y[1],
22
+ ...Y.subarray(0, 30)
23
+ ]);
24
+ const _getCipher = (u, v, sbox) => new Dstu9311(P(xorBytes(u, v)), sbox);
25
+ const _step = (hin, m, sbox) => {
26
+ const k1 = _getCipher(hin, m, sbox);
27
+ let u = A(hin), v = A(A(m));
28
+ const k2 = _getCipher(u, v, sbox);
29
+ u = xorBytes(A(u), C3), v = A(A(v));
30
+ const k3 = _getCipher(u, v, sbox);
31
+ u = A(u), v = A(A(v));
32
+ const k4 = _getCipher(u, v, sbox);
33
+ const x = concatBytes(k4.encrypt(hin.slice(0, 8).reverse()).reverse(), k3.encrypt(hin.slice(8, 16).reverse()).reverse(), k2.encrypt(hin.slice(16, 24).reverse()).reverse(), k1.encrypt(hin.slice(24, 32).reverse()).reverse());
34
+ for (let i = 0; i < 12; i++)
35
+ x.set(chi(x));
36
+ x.set(xorBytes(hin, chi(xorBytes(x, m))));
37
+ for (let i = 0; i < 61; i++)
38
+ x.set(chi(x));
39
+ return x;
40
+ };
41
+ /** GOST R 34.311-95 hash function */
42
+ export class Gost3431195 {
43
+ sbox;
44
+ blockLen = 32;
45
+ outputLen = 32;
46
+ canXOF = false;
47
+ buffer;
48
+ /** GOST R 34.311-95 hash function */
49
+ constructor(sbox = DKE_1) {
50
+ this.sbox = sbox;
51
+ this.buffer = new Uint8Array();
52
+ }
53
+ /** Create hash instance */
54
+ static create() { return new Gost3431195(); }
55
+ destroy() { this.buffer = new Uint8Array(); }
56
+ clone() { return this._cloneInto(); }
57
+ _cloneInto(to) {
58
+ to ||= new Gost3431195();
59
+ to.buffer = new Uint8Array(this.buffer);
60
+ to.sbox = this.sbox;
61
+ return to;
62
+ }
63
+ update(data) {
64
+ this.buffer = concatBytes(this.buffer, data);
65
+ return this;
66
+ }
67
+ digestInto(buf) {
68
+ if (buf.length != this.outputLen)
69
+ throw new Error("digestInto: Invalid buffer length");
70
+ let len = 0n, checksum = 0n;
71
+ const h = new Uint8Array(this.blockLen), m = copyBytes(this.buffer);
72
+ for (let i = 0; i < m.length; i += this.blockLen) {
73
+ let part = m.slice(i, i + this.blockLen).reverse();
74
+ len += BigInt(part.length) * 8n;
75
+ checksum = (checksum + bytesToNumberBE(part)) & r;
76
+ if (part.length < this.blockLen)
77
+ part = numberToBytesBE(bytesToNumberBE(part), this.blockLen);
78
+ h.set(_step(h, part, this.sbox));
79
+ }
80
+ h.set(_step(_step(h, numberToBytesBE(len, this.blockLen), this.sbox), numberToBytesBE(checksum, this.blockLen), this.sbox));
81
+ buf.set(h.reverse());
82
+ this.destroy();
83
+ }
84
+ digest() {
85
+ const buffer = new Uint8Array(this.outputLen);
86
+ this.digestInto(buffer);
87
+ return buffer;
88
+ }
89
+ }
90
+ /** GOST R 34.311-95 hash function */
91
+ export const gost3431195 = createHasher(Gost3431195.create);
@@ -0,0 +1,16 @@
1
+ import { type TArg, type TRet } from "@noble/hashes/utils.js";
2
+ import type { Cipher } from "../types.js";
3
+ /** DSTU 9311:2024 cipher (Revised DSTU GOST 28147:2009) */
4
+ export declare class Dstu9311 implements Cipher {
5
+ private key;
6
+ private sbox;
7
+ readonly keySize = 32;
8
+ readonly blockSize = 8;
9
+ /** DSTU 9311:2024 cipher (Revised DSTU GOST 28147:2009) */
10
+ constructor(key: TArg<Uint8Array>, sbox?: TArg<Uint8Array>);
11
+ proceedBlock(block: TArg<Uint8Array>, sequence: number[]): TRet<Uint8Array>;
12
+ encrypt(plaintext: TArg<Uint8Array>): TRet<Uint8Array>;
13
+ decrypt(ciphertext: TArg<Uint8Array>): TRet<Uint8Array>;
14
+ }
15
+ export * from "./const.js";
16
+ export * from "./hash.js";
@@ -0,0 +1,59 @@
1
+ import { concatBytes, createView } from "@noble/hashes/utils.js";
2
+ import { DKE_1, keySequences } from "./const.js";
3
+ import { bytesToNumberLE, numberToBytesLE } from "@noble/curves/utils.js";
4
+ const G = (v, sbox) => {
5
+ const nibble = (r, i) => {
6
+ const b = sbox[r + (i >> 1)];
7
+ return (i & 1) == 0 ? (b >>> 4) : (b & 0x0f);
8
+ };
9
+ const t = (nibble(0, v & 0x0f) << 0) |
10
+ (nibble(8, (v >> 4) & 0x0f) << 4) |
11
+ (nibble(16, (v >> 8) & 0x0f) << 8) |
12
+ (nibble(24, (v >> 12) & 0x0f) << 12) |
13
+ (nibble(32, (v >> 16) & 0x0f) << 16) |
14
+ (nibble(40, (v >> 20) & 0x0f) << 20) |
15
+ (nibble(48, (v >> 24) & 0x0f) << 24) |
16
+ (nibble(56, (v >> 28) & 0x0f) << 28);
17
+ return ((t << 11) | (t >>> 21)) >>> 0;
18
+ };
19
+ const extendKey = (key, sequence) => {
20
+ const view = createView(key);
21
+ const result = new Uint32Array(sequence.length);
22
+ for (let i = 0; i < sequence.length; i++)
23
+ result[i] = view.getUint32(sequence[i] * 4, true);
24
+ return result;
25
+ };
26
+ /** DSTU 9311:2024 cipher (Revised DSTU GOST 28147:2009) */
27
+ export class Dstu9311 {
28
+ key;
29
+ sbox;
30
+ keySize = 32;
31
+ blockSize = 8;
32
+ /** DSTU 9311:2024 cipher (Revised DSTU GOST 28147:2009) */
33
+ constructor(key, sbox = DKE_1) {
34
+ this.key = key;
35
+ this.sbox = sbox;
36
+ if (key.length !== this.keySize)
37
+ throw new Error("Invalid key length");
38
+ }
39
+ proceedBlock(block, sequence) {
40
+ if (block.length !== this.blockSize)
41
+ throw new Error("Invalid block size");
42
+ const roundKeys = extendKey(this.key, sequence);
43
+ let a0 = Number(bytesToNumberLE(block.subarray(4, 8))), a1 = Number(bytesToNumberLE(block.subarray(0, 4)));
44
+ for (let i = 0; i < roundKeys.length; i++) {
45
+ const temp = a1;
46
+ a1 = (a0 ^ G((a1 + roundKeys[i]) >>> 0, this.sbox)) >>> 0;
47
+ a0 = temp;
48
+ }
49
+ return concatBytes(numberToBytesLE(a0, 4), numberToBytesLE(a1, 4));
50
+ }
51
+ encrypt(plaintext) {
52
+ return this.proceedBlock(plaintext, keySequences.ENCRYPT);
53
+ }
54
+ decrypt(ciphertext) {
55
+ return this.proceedBlock(ciphertext, keySequences.DECRYPT);
56
+ }
57
+ }
58
+ export * from "./const.js";
59
+ export * from "./hash.js";
package/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  export * from "./dstu4145/index.js";
2
2
  export * from "./dstu9041/index.js";
3
+ export * from "./dstu9311/index.js";
3
4
  export { Kalyna128, Kalyna128_256, Kalyna256, Kalyna256_512, Kalyna512 } from "./kalyna/index.js";
4
5
  export * from "./kupyna/index.js";
5
6
  export * from "./modes/index.js";
6
7
  export * from "./strumok/index.js";
8
+ export * from "./keywrap.js";
7
9
  export * from "./types.js";
package/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  export * from "./dstu4145/index.js";
2
2
  export * from "./dstu9041/index.js";
3
+ export * from "./dstu9311/index.js";
3
4
  export { Kalyna128, Kalyna128_256, Kalyna256, Kalyna256_512, Kalyna512 } from "./kalyna/index.js";
4
5
  export * from "./kupyna/index.js";
5
6
  export * from "./modes/index.js";
6
7
  export * from "./strumok/index.js";
8
+ export * from "./keywrap.js";
7
9
  export * from "./types.js";
package/keywrap.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { type TArg, type TRet } from "@noble/hashes/utils.js";
2
+ /** Key wrap with Kalyna (`Dstu7624Wrap`) */
3
+ export declare const keyWrap: (kek: TArg<Uint8Array>) => {
4
+ wrap: (key: TArg<Uint8Array>, iv?: TArg<Uint8Array>) => TRet<Uint8Array>;
5
+ unwrap: (wrappedKey: TArg<Uint8Array>) => TRet<Uint8Array>;
6
+ };
package/keywrap.js ADDED
@@ -0,0 +1,34 @@
1
+ import { concatBytes, randomBytes } from "@noble/hashes/utils.js";
2
+ import { Kalyna256 } from "./kalyna/index.js";
3
+ import { cfb, cmac } from "./modes/index.js";
4
+ import { equalBytes } from "@noble/curves/utils.js";
5
+ const IV1 = new Uint8Array([
6
+ 0x69, 0x73, 0x27, 0x1D, 0x6E, 0x61, 0x1D, 0x06, 0x61, 0x67, 0x15, 0x04, 0x6C, 0x65, 0x50, 0x4C,
7
+ 0x20, 0x20, 0x00, 0x4F, 0x6D, 0x68, 0x01, 0x1F, 0x65, 0x61, 0x0C, 0x0C, 0x73, 0x73, 0x47, 0x14
8
+ ]);
9
+ /** Key wrap with Kalyna (`Dstu7624Wrap`) */
10
+ export const keyWrap = (kek) => {
11
+ if (kek.length != 32)
12
+ throw new Error("Invalid key length");
13
+ const cipher = new Kalyna256(kek);
14
+ return Object.freeze({
15
+ wrap: (key, iv) => {
16
+ iv ??= randomBytes(32);
17
+ if (iv.length != 32)
18
+ throw new Error("Invalid IV length");
19
+ const mac = cmac(cipher, 32).compute(key);
20
+ const mode = cfb(cipher, iv, 32);
21
+ return cfb(cipher, IV1, 32).encrypt(concatBytes(iv, mode.encrypt(concatBytes(key, mac))).reverse());
22
+ },
23
+ unwrap: (wrappedKey) => {
24
+ const dec_wrapped = cfb(cipher, IV1, 32).decrypt(wrappedKey).reverse();
25
+ const iv = dec_wrapped.subarray(0, 32);
26
+ const key_mac = cfb(cipher, iv, 32).decrypt(dec_wrapped.subarray(32));
27
+ const key = key_mac.slice(0, 32), mac = key_mac.subarray(32);
28
+ const actualMac = cmac(cipher, 32).compute(key);
29
+ if (!equalBytes(actualMac, mac))
30
+ throw new Error("Invalid MAC");
31
+ return key;
32
+ }
33
+ });
34
+ };
package/modes/cbc.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import type { TArg } from "@noble/hashes/utils.js";
2
- import type { Kalyna } from "../kalyna/index.js";
3
- import type { BlockMode } from "../types.js";
2
+ import type { BlockMode, Cipher } from "../types.js";
4
3
  /** Cipher Block Chaining (CBC) mode */
5
- export declare const cbc: (cipher: Kalyna, iv: TArg<Uint8Array>) => BlockMode;
4
+ export declare const cbc: (cipher: Cipher, iv: TArg<Uint8Array>) => BlockMode;
package/modes/cbc.js CHANGED
@@ -1,30 +1,31 @@
1
- import { xorBytes } from "../utils.js";
1
+ import { assertKalyna, xorBytes } from "../utils.js";
2
2
  /** Cipher Block Chaining (CBC) mode */
3
3
  export const cbc = (cipher, iv) => {
4
+ assertKalyna(cipher);
4
5
  if (iv.length !== cipher.blockSize)
5
6
  throw new Error("Invalid IV size");
6
7
  return Object.freeze({
7
8
  encrypt: (plaintext) => {
8
9
  if (plaintext.length % cipher.blockSize !== 0)
9
10
  throw new Error("Plaintext not aligned");
10
- let buf = new Uint8Array(iv);
11
+ let buf = iv;
11
12
  const output = new Uint8Array(plaintext.length);
12
13
  for (let i = 0; i < plaintext.length; i += cipher.blockSize) {
13
14
  const blk = cipher.encrypt(xorBytes(plaintext.subarray(i, i + cipher.blockSize), buf));
14
15
  output.set(blk, i);
15
- buf = blk.slice();
16
+ buf = blk;
16
17
  }
17
18
  return output;
18
19
  },
19
20
  decrypt: (ciphertext) => {
20
21
  if (ciphertext.length % cipher.blockSize !== 0)
21
22
  throw new Error("Ciphertext not aligned");
22
- let buf = new Uint8Array(iv);
23
+ let buf = iv;
23
24
  const output = new Uint8Array(ciphertext.length);
24
25
  for (let i = 0; i < ciphertext.length; i += cipher.blockSize) {
25
26
  const blk = ciphertext.subarray(i, i + cipher.blockSize);
26
27
  output.set(xorBytes(cipher.decrypt(blk), buf), i);
27
- buf = blk.slice();
28
+ buf = blk;
28
29
  }
29
30
  return output;
30
31
  }
package/modes/cfb.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import type { TArg } from "@noble/hashes/utils.js";
2
- import type { Kalyna } from "../kalyna/index.js";
3
- import type { BlockMode } from "../types.js";
1
+ import { type TArg } from "@noble/hashes/utils.js";
2
+ import type { BlockMode, Cipher } from "../types.js";
4
3
  /** Cipher Feedback (CFB) mode */
5
- export declare const cfb: (cipher: Kalyna, iv: TArg<Uint8Array>, q?: number) => BlockMode;
4
+ export declare const cfb: (cipher: Cipher, iv: TArg<Uint8Array>, q?: number) => BlockMode;