@li0ard/dstu 0.1.6 → 0.1.8

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 (59) hide show
  1. package/README.md +5 -0
  2. package/dstu4145/const.d.ts +2 -0
  3. package/dstu4145/const.js +42 -15
  4. package/dstu4145/ec/expand.d.ts +4 -3
  5. package/dstu4145/ec/expand.js +1 -2
  6. package/dstu4145/ec/index.js +1 -2
  7. package/dstu4145/{ec/math.d.ts → gf2m/index.d.ts} +1 -0
  8. package/dstu4145/gf2m/index.js +87 -0
  9. package/dstu4145/{ec → gf2m}/onb.js +1 -1
  10. package/dstu4145/gf2m/utils.d.ts +10 -0
  11. package/dstu4145/gf2m/utils.js +258 -0
  12. package/dstu4145/index.d.ts +66 -11
  13. package/dstu4145/index.js +53 -6
  14. package/dstu9041/curve.js +1 -2
  15. package/dstu9041/index.d.ts +2 -2
  16. package/dstu9041/index.js +49 -46
  17. package/dstu9311/const.d.ts +29 -0
  18. package/dstu9311/const.js +150 -0
  19. package/dstu9311/hash.d.ts +41 -0
  20. package/dstu9311/hash.js +98 -0
  21. package/dstu9311/index.d.ts +16 -0
  22. package/dstu9311/index.js +59 -0
  23. package/index.d.ts +2 -0
  24. package/index.js +2 -0
  25. package/kalyna/index.d.ts +2 -1
  26. package/kalyna/index.js +23 -28
  27. package/keywrap.d.ts +6 -0
  28. package/keywrap.js +39 -0
  29. package/modes/cbc.d.ts +2 -3
  30. package/modes/cbc.js +4 -3
  31. package/modes/ccm.d.ts +4 -0
  32. package/modes/ccm.js +47 -0
  33. package/modes/cfb.d.ts +3 -4
  34. package/modes/cfb.js +88 -56
  35. package/modes/cmac.d.ts +2 -0
  36. package/modes/cmac.js +45 -0
  37. package/modes/ctr.d.ts +2 -3
  38. package/modes/ctr.js +25 -14
  39. package/modes/ecb.d.ts +2 -3
  40. package/modes/gcm.d.ts +3 -4
  41. package/modes/gcm.js +19 -16
  42. package/modes/gmac.d.ts +3 -0
  43. package/modes/gmac.js +39 -0
  44. package/modes/index.d.ts +3 -1
  45. package/modes/index.js +3 -1
  46. package/modes/kw.d.ts +2 -2
  47. package/modes/kw.js +2 -0
  48. package/modes/ofb.d.ts +2 -3
  49. package/modes/ofb.js +3 -2
  50. package/modes/xts.d.ts +2 -2
  51. package/modes/xts.js +32 -21
  52. package/package.json +4 -4
  53. package/types.d.ts +5 -3
  54. package/utils.d.ts +6 -0
  55. package/utils.js +15 -3
  56. package/dstu4145/ec/math.js +0 -204
  57. package/modes/mac.d.ts +0 -6
  58. package/modes/mac.js +0 -61
  59. /package/dstu4145/{ec → gf2m}/onb.d.ts +0 -0
package/modes/cmac.js ADDED
@@ -0,0 +1,45 @@
1
+ import { concatBytes, bytesToNumberLE, numberToBytesLE } from "@noble/curves/utils.js";
2
+ import { pad } from "../padding.js";
3
+ import { isKalyna, xorBytes } from "../utils.js";
4
+ import { keySequences } from "../dstu9311/const.js";
5
+ export const cmac = (cipher, q = 16) => {
6
+ if (isKalyna(cipher))
7
+ return Object.freeze({
8
+ compute: (msg) => {
9
+ let data = msg;
10
+ const zeroBlock = new Uint8Array(cipher.blockSize);
11
+ if (data.length % cipher.blockSize !== 0) {
12
+ data = pad(data, cipher.blockSize);
13
+ zeroBlock[0] = 1;
14
+ }
15
+ const Kd = cipher.encrypt(zeroBlock);
16
+ const c = new Uint8Array(cipher.blockSize);
17
+ const numBlocks = data.length / cipher.blockSize;
18
+ for (let i = 0; i < numBlocks - 1; i++) {
19
+ const blockStart = i * cipher.blockSize;
20
+ c.set(cipher.encrypt(xorBytes(c, data.subarray(blockStart, blockStart + cipher.blockSize))));
21
+ }
22
+ const lastBlockStart = (numBlocks - 1) * cipher.blockSize;
23
+ c.set(cipher.encrypt(xorBytes(xorBytes(c, data.subarray(lastBlockStart, lastBlockStart + cipher.blockSize)), Kd)));
24
+ return c.slice(0, q);
25
+ }
26
+ });
27
+ else
28
+ return Object.freeze({
29
+ compute: (msg) => {
30
+ const paddedLength = Math.max(8, (msg.length + 7) & ~7);
31
+ const paddedData = new Uint8Array(paddedLength);
32
+ paddedData.set(msg);
33
+ let prev0 = 0n, prev1 = 0n;
34
+ const feedback = new Uint8Array(cipher.blockSize);
35
+ for (let i = 0; i < paddedData.length; i += cipher.blockSize) {
36
+ feedback.set(numberToBytesLE(prev1, 4), 0);
37
+ feedback.set(numberToBytesLE(prev0, 4), 4);
38
+ const out = cipher.proceedBlock(xorBytes(paddedData.subarray(i, i + cipher.blockSize), feedback), keySequences.MAC);
39
+ prev0 = bytesToNumberLE(out.subarray(0, 4));
40
+ prev1 = bytesToNumberLE(out.subarray(4, 8));
41
+ }
42
+ return concatBytes(numberToBytesLE(prev1, 4), numberToBytesLE(prev0, 4)).slice(0, q);
43
+ }
44
+ });
45
+ };
package/modes/ctr.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 { StreamMode } from "../types.js";
2
+ import type { Cipher, StreamMode } from "../types.js";
4
3
  /** Counter (CTR) mode */
5
- export declare const ctr: (cipher: Kalyna, iv: TArg<Uint8Array>) => StreamMode;
4
+ export declare const ctr: (cipher: Cipher, iv: TArg<Uint8Array>) => StreamMode;
package/modes/ctr.js CHANGED
@@ -1,24 +1,35 @@
1
- import { concatBytes } from "@noble/hashes/utils.js";
2
- import { xorBytes } from "../utils.js";
3
- const incrementCounterAt = (ctr, pos) => {
4
- let j = pos;
5
- while (j < ctr.length)
6
- if (++ctr[j++] != 0)
7
- break;
8
- };
1
+ import { createView } from "@noble/hashes/utils.js";
2
+ import { isKalyna, xorBytes } from "../utils.js";
3
+ const C1 = 0x01010104, C2 = 0x01010101;
9
4
  /** Counter (CTR) mode */
10
5
  export const ctr = (cipher, iv) => {
11
6
  if (iv.length !== cipher.blockSize)
12
7
  throw new Error("Invalid IV size");
8
+ const incrementCounter = (ctr) => {
9
+ if (isKalyna(cipher)) {
10
+ let j = 0;
11
+ while (j < ctr.length)
12
+ if (++ctr[j++] != 0)
13
+ break;
14
+ }
15
+ else {
16
+ const view = createView(ctr);
17
+ view.setUint32(0, (view.getUint32(0, true) + C2) >>> 0, true);
18
+ let s2 = view.getUint32(4, true) + C1;
19
+ if (s2 >= 0xFFFFFFFF)
20
+ s2 -= 0xFFFFFFFF;
21
+ view.setUint32(4, s2 >>> 0, true);
22
+ }
23
+ };
13
24
  return Object.freeze({
14
25
  crypt: (msg) => {
15
- const keystreamBlocks = [];
16
- const ctr = cipher.encrypt(iv);
17
- for (let i = 0; i < Math.ceil(msg.length / cipher.blockSize); i++) {
18
- incrementCounterAt(ctr, 0);
19
- keystreamBlocks.push(cipher.encrypt(ctr));
26
+ const buf = cipher.encrypt(iv), output = new Uint8Array(msg.length);
27
+ for (let i = 0; i < msg.length; i += cipher.blockSize) {
28
+ incrementCounter(buf);
29
+ const ct = xorBytes(cipher.encrypt(buf), msg.subarray(i, i + cipher.blockSize));
30
+ output.set(ct, i);
20
31
  }
21
- return xorBytes(concatBytes(...keystreamBlocks), msg);
32
+ return output;
22
33
  }
23
34
  });
24
35
  };
package/modes/ecb.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import type { Kalyna } from "../kalyna/index.js";
2
- import type { BlockMode } from "../types.js";
1
+ import type { BlockMode, Cipher } from "../types.js";
3
2
  /** Electronic Codebook (ECB) mode */
4
- export declare const ecb: (cipher: Kalyna) => BlockMode;
3
+ export declare const ecb: (cipher: Cipher) => BlockMode;
package/modes/gcm.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 { AEADMode } from "../types.js";
1
+ import { type TArg } from "@noble/curves/utils.js";
2
+ import type { AEADMode, Cipher } from "../types.js";
4
3
  /** Galois counter (GCM) mode (AEAD) */
5
- export declare const gcm: (cipher: Kalyna, iv: TArg<Uint8Array>, q?: number) => AEADMode;
4
+ export declare const gcm: (cipher: Cipher, iv: TArg<Uint8Array>, q?: number) => AEADMode;
package/modes/gcm.js CHANGED
@@ -1,18 +1,21 @@
1
- import { concatBytes } from "@noble/hashes/utils.js";
1
+ import { equalBytes, concatBytes } from "@noble/curves/utils.js";
2
2
  import { ctr } from "./ctr.js";
3
- import { gmac } from "./mac.js";
4
- import { equalBytes } from "@noble/curves/utils.js";
3
+ import { gmac } from "./gmac.js";
4
+ import { assertKalyna } from "../utils.js";
5
5
  /** Galois counter (GCM) mode (AEAD) */
6
- export const gcm = (cipher, iv, q = 16) => Object.freeze({
7
- seal: (plaintext, aad) => {
8
- const enc = ctr(cipher, iv).crypt(plaintext);
9
- return concatBytes(enc, gmac(cipher, q).compute(aad || new Uint8Array(), enc));
10
- },
11
- open: (ciphertext, aad) => {
12
- const enc = ciphertext.subarray(0, -q);
13
- const hC = gmac(cipher, q).compute(aad || new Uint8Array(), enc);
14
- if (!equalBytes(ciphertext.subarray(-q), hC))
15
- throw new Error("Invalid MAC");
16
- return ctr(cipher, iv).crypt(enc);
17
- }
18
- });
6
+ export const gcm = (cipher, iv, q = 16) => {
7
+ assertKalyna(cipher);
8
+ return Object.freeze({
9
+ seal: (plaintext, aad) => {
10
+ const enc = ctr(cipher, iv).crypt(plaintext);
11
+ return concatBytes(enc, gmac(cipher, q).compute(enc, aad));
12
+ },
13
+ open: (ciphertext, aad) => {
14
+ const enc = ciphertext.subarray(0, -q);
15
+ const hC = gmac(cipher, q).compute(enc, aad);
16
+ if (!equalBytes(ciphertext.subarray(-q), hC))
17
+ throw new Error("Invalid MAC");
18
+ return ctr(cipher, iv).crypt(enc);
19
+ }
20
+ });
21
+ };
@@ -0,0 +1,3 @@
1
+ import type { Cipher, GMACMode } from "../types.js";
2
+ /** Galois Message Authentication Code (GMAC) mode */
3
+ export declare const gmac: (cipher: Cipher, q?: number) => GMACMode;
package/modes/gmac.js ADDED
@@ -0,0 +1,39 @@
1
+ import { numberToBytesLE, concatBytes } from "@noble/curves/utils.js";
2
+ import { assertKalyna, gf2mMul } from "../utils.js";
3
+ const xorBytesInPlace = (a, b) => {
4
+ if (a.length != b.length)
5
+ throw new Error("Byte arrays must have same length");
6
+ for (let i = 0; i < a.length; i++)
7
+ a[i] ^= b[i];
8
+ };
9
+ /** Galois Message Authentication Code (GMAC) mode */
10
+ export const gmac = (cipher, q = 16) => {
11
+ assertKalyna(cipher);
12
+ const hblock = cipher.blockSize >> 1;
13
+ return Object.freeze({
14
+ compute: (msg, aad) => {
15
+ aad ??= new Uint8Array();
16
+ const H = cipher.encrypt(new Uint8Array(cipher.blockSize));
17
+ const B = new Uint8Array(cipher.blockSize);
18
+ const updateMac = (input) => {
19
+ let i = 0;
20
+ while (i < input.length) {
21
+ const blockSizeToProcess = Math.min(cipher.blockSize, input.length - i);
22
+ const block = new Uint8Array(cipher.blockSize);
23
+ for (let j = 0; j < blockSizeToProcess; j++)
24
+ block[j] = input[i + j];
25
+ if (blockSizeToProcess < cipher.blockSize)
26
+ block[blockSizeToProcess] = 0x80;
27
+ for (let j = 0; j < cipher.blockSize; j++)
28
+ B[j] ^= block[j];
29
+ B.set(gf2mMul(cipher.blockSize, B, H));
30
+ i += cipher.blockSize;
31
+ }
32
+ };
33
+ updateMac(aad);
34
+ updateMac(msg);
35
+ xorBytesInPlace(B, concatBytes(numberToBytesLE(aad.length * 8, hblock), numberToBytesLE(msg.length * 8, hblock)));
36
+ return cipher.encrypt(B).slice(0, q);
37
+ }
38
+ });
39
+ };
package/modes/index.d.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  export * from "./cbc.js";
2
+ export * from "./ccm.js";
2
3
  export * from "./cfb.js";
4
+ export * from "./cmac.js";
3
5
  export * from "./ctr.js";
4
6
  export * from "./ecb.js";
5
7
  export * from "./gcm.js";
8
+ export * from "./gmac.js";
6
9
  export * from "./kw.js";
7
- export * from "./mac.js";
8
10
  export * from "./ofb.js";
9
11
  export * from "./xts.js";
package/modes/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  export * from "./cbc.js";
2
+ export * from "./ccm.js";
2
3
  export * from "./cfb.js";
4
+ export * from "./cmac.js";
3
5
  export * from "./ctr.js";
4
6
  export * from "./ecb.js";
5
7
  export * from "./gcm.js";
8
+ export * from "./gmac.js";
6
9
  export * from "./kw.js";
7
- export * from "./mac.js";
8
10
  export * from "./ofb.js";
9
11
  export * from "./xts.js";
package/modes/kw.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { type TArg, type TRet } from "@noble/hashes/utils.js";
2
- import type { Kalyna } from "../kalyna/index.js";
2
+ import type { Cipher } from "../types.js";
3
3
  /** Key wrapping (KW) */
4
- export declare const kw: (cipher: Kalyna) => {
4
+ export declare const kw: (cipher: Cipher) => {
5
5
  wrap: (key: TArg<Uint8Array>) => TRet<Uint8Array>;
6
6
  unwrap: (wrappedKey: TArg<Uint8Array>) => TRet<Uint8Array>;
7
7
  };
package/modes/kw.js CHANGED
@@ -1,7 +1,9 @@
1
1
  import { concatBytes } from "@noble/hashes/utils.js";
2
2
  import { pad, unpad } from "../padding.js";
3
+ import { assertKalyna } from "../utils.js";
3
4
  /** Key wrapping (KW) */
4
5
  export const kw = (cipher) => {
6
+ assertKalyna(cipher);
5
7
  const half = cipher.blockSize >> 1;
6
8
  return Object.freeze({
7
9
  wrap: (key) => {
package/modes/ofb.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 { StreamMode } from "../types.js";
2
+ import type { Cipher, StreamMode } from "../types.js";
4
3
  /** Output Feedback (OFB) mode */
5
- export declare const ofb: (cipher: Kalyna, iv: TArg<Uint8Array>) => StreamMode;
4
+ export declare const ofb: (cipher: Cipher, iv: TArg<Uint8Array>) => StreamMode;
package/modes/ofb.js CHANGED
@@ -1,12 +1,13 @@
1
1
  import {} from "@noble/hashes/utils.js";
2
- import { xorBytes } from "../utils.js";
2
+ import { assertKalyna, xorBytes } from "../utils.js";
3
3
  /** Output Feedback (OFB) mode */
4
4
  export const ofb = (cipher, iv) => {
5
+ assertKalyna(cipher);
5
6
  if (iv.length !== cipher.blockSize)
6
7
  throw new Error("Invalid IV size");
7
8
  return Object.freeze({
8
9
  crypt: (msg) => {
9
- let buf = new Uint8Array(iv);
10
+ let buf = iv;
10
11
  const output = new Uint8Array(msg.length);
11
12
  for (let i = 0; i < msg.length; i += cipher.blockSize) {
12
13
  const enc = cipher.encrypt(buf);
package/modes/xts.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { type TArg, type TRet } from "@noble/hashes/utils.js";
2
- import type { Kalyna } from "../kalyna/index.js";
2
+ import type { Cipher } from "../types.js";
3
3
  /** XEX Tweakable Block Ciphertext Stealing (XTS) */
4
- export declare const xts: (cipher: Kalyna) => {
4
+ export declare const xts: (cipher: Cipher) => {
5
5
  encrypt: (plaintext: TArg<Uint8Array>, tweak: TArg<Uint8Array>) => TRet<Uint8Array>;
6
6
  decrypt: (ciphertext: TArg<Uint8Array>, iv: TArg<Uint8Array>) => TRet<Uint8Array>;
7
7
  };
package/modes/xts.js CHANGED
@@ -1,10 +1,24 @@
1
- import { concatBytes } from "@noble/hashes/utils.js";
2
- import { gf2mMul, xorBytes } from "../utils.js";
3
- import { numberToBytesLE } from "@noble/curves/utils.js";
1
+ import { concatBytes, copyBytes } from "@noble/hashes/utils.js";
2
+ import { assertKalyna, getGf2mReductionBytes, xorBytes } from "../utils.js";
4
3
  /** XEX Tweakable Block Ciphertext Stealing (XTS) */
5
4
  export const xts = (cipher) => {
6
- const two = numberToBytesLE(2, cipher.blockSize);
7
- const increaseGamma = (gamma) => gf2mMul(cipher.blockSize, gamma, two);
5
+ assertKalyna(cipher);
6
+ const gf2mDouble = (a) => {
7
+ const result = copyBytes(a);
8
+ const reductionBytes = getGf2mReductionBytes(cipher.blockSize);
9
+ let carry = 0;
10
+ for (let j = 0; j < cipher.blockSize; j++) {
11
+ const nextCarry = (result[j] & 0x80) ? 1 : 0;
12
+ result[j] = ((result[j] << 1) & 0xFF) | carry;
13
+ carry = nextCarry;
14
+ }
15
+ if (carry) {
16
+ for (let j = 0; j < reductionBytes.length; j++)
17
+ if (j < cipher.blockSize)
18
+ result[j] ^= reductionBytes[j];
19
+ }
20
+ return result;
21
+ };
8
22
  const transform = (transform, block, gamma) => xorBytes(transform(xorBytes(block, gamma)), gamma);
9
23
  const init = (iv) => {
10
24
  const gamma = cipher.encrypt(iv);
@@ -16,20 +30,20 @@ export const xts = (cipher) => {
16
30
  encrypt: (plaintext, tweak) => {
17
31
  if (plaintext.length < cipher.blockSize)
18
32
  throw new Error(`Invalid length (need at least ${cipher.blockSize}, got ${plaintext.length})`);
19
- let gamma = init(tweak);
33
+ const gamma = init(tweak);
20
34
  const encrypt = cipher.encrypt.bind(cipher);
21
35
  const buffer = new Uint8Array(plaintext);
22
36
  const k = Math.floor(plaintext.length / cipher.blockSize);
23
37
  const r = plaintext.length % cipher.blockSize;
24
38
  if (r === 0) {
25
39
  for (let off = 0; off < plaintext.length; off += cipher.blockSize) {
26
- gamma = increaseGamma(gamma);
40
+ gamma.set(gf2mDouble(gamma));
27
41
  buffer.set(transform(encrypt, buffer.subarray(off, off + cipher.blockSize), gamma), off);
28
42
  }
29
43
  }
30
44
  else {
31
45
  for (let i = 0; i < k; i++) {
32
- gamma = increaseGamma(gamma);
46
+ gamma.set(gf2mDouble(gamma));
33
47
  const off = i * cipher.blockSize;
34
48
  buffer.set(transform(encrypt, buffer.subarray(off, off + cipher.blockSize), gamma), off);
35
49
  }
@@ -37,8 +51,7 @@ export const xts = (cipher) => {
37
51
  const tail_off = k * cipher.blockSize;
38
52
  const scratch = buffer.slice(last_off, last_off + cipher.blockSize);
39
53
  const combined = concatBytes(buffer.subarray(tail_off, tail_off + r), scratch.subarray(r));
40
- gamma = increaseGamma(gamma);
41
- const encrypted = transform(encrypt, combined, gamma);
54
+ const encrypted = transform(encrypt, combined, gf2mDouble(gamma));
42
55
  buffer.set(encrypted, last_off);
43
56
  buffer.set(scratch.subarray(0, r), tail_off);
44
57
  }
@@ -47,32 +60,30 @@ export const xts = (cipher) => {
47
60
  decrypt: (ciphertext, iv) => {
48
61
  if (ciphertext.length < cipher.blockSize)
49
62
  throw new Error(`Invalid length (need at least ${cipher.blockSize}, got ${ciphertext.length})`);
50
- const n = ciphertext.length;
51
- let gamma = init(iv);
63
+ const gamma = init(iv);
52
64
  const decrypt = cipher.decrypt.bind(cipher);
53
65
  const buffer = new Uint8Array(ciphertext);
54
- const k = Math.floor(n / cipher.blockSize);
55
- const r = n % cipher.blockSize;
66
+ const k = Math.floor(ciphertext.length / cipher.blockSize);
67
+ const r = ciphertext.length % cipher.blockSize;
56
68
  if (r === 0) {
57
- for (let off = 0; off < n; off += cipher.blockSize) {
58
- gamma = increaseGamma(gamma);
69
+ for (let off = 0; off < ciphertext.length; off += cipher.blockSize) {
70
+ gamma.set(gf2mDouble(gamma));
59
71
  buffer.set(transform(decrypt, buffer.subarray(off, off + cipher.blockSize), gamma), off);
60
72
  }
61
73
  }
62
74
  else {
63
75
  for (let i = 0; i < k - 1; i++) {
64
- gamma = increaseGamma(gamma);
76
+ gamma.set(gf2mDouble(gamma));
65
77
  const off = i * cipher.blockSize;
66
78
  buffer.set(transform(decrypt, buffer.subarray(off, off + cipher.blockSize), gamma), off);
67
79
  }
68
- gamma = increaseGamma(gamma);
69
- const gamma_k = gamma;
70
- const gamma_k_plus_1 = increaseGamma(gamma);
80
+ gamma.set(gf2mDouble(gamma));
81
+ const gamma_k_plus_1 = gf2mDouble(gamma);
71
82
  const last_off = (k - 1) * cipher.blockSize;
72
83
  const tail_off = k * cipher.blockSize;
73
84
  const combined = transform(decrypt, buffer.subarray(last_off, last_off + cipher.blockSize), gamma_k_plus_1);
74
85
  const rec = concatBytes(buffer.subarray(tail_off, tail_off + r), combined.subarray(r));
75
- const decrypted = transform(decrypt, rec, gamma_k);
86
+ const decrypted = transform(decrypt, rec, gamma);
76
87
  buffer.set(decrypted, last_off);
77
88
  buffer.set(combined.subarray(0, r), tail_off);
78
89
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@li0ard/dstu",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "type": "module",
@@ -8,6 +8,7 @@
8
8
  ".": "./index.js",
9
9
  "./dstu4145.js": "./dstu4145/index.js",
10
10
  "./dstu9041.js": "./dstu9041/index.js",
11
+ "./dstu9311.js": "./dstu9311/index.js",
11
12
  "./kalyna.js": "./kalyna/index.js",
12
13
  "./kupyna.js": "./kupyna/index.js",
13
14
  "./modes.js": "./modes/index.js",
@@ -31,7 +32,6 @@
31
32
  "docs": "typedoc --options .config/typedoc/config.cjs"
32
33
  },
33
34
  "devDependencies": {
34
- "@li0ard/gost": "^0.2.2",
35
35
  "@peculiar/asn1-schema": "^2.8.0",
36
36
  "@peculiar/asn1-x509": "^2.8.0",
37
37
  "@types/bn.js": "^5.2.0",
@@ -42,8 +42,8 @@
42
42
  "typescript": "^5"
43
43
  },
44
44
  "dependencies": {
45
- "@noble/curves": "^2.3.0",
46
- "@noble/hashes": "^2.3.0",
45
+ "@noble/curves": "^2.4.0",
46
+ "@noble/hashes": "^2.4.0",
47
47
  "bn.js": "^5.2.3"
48
48
  },
49
49
  "files": ["**/*.js", "**/*.d.ts"],
package/types.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  import type { TArg, TRet } from "@noble/hashes/utils.js";
2
+ /** Cipher or hash function wrapper */
3
+ export type CipherOrHashFunctionWrapper = (msg: TArg<Uint8Array>) => TRet<Uint8Array>;
2
4
  /** Cipher core */
3
5
  export type Cipher = {
4
6
  /** Block size */
@@ -20,17 +22,17 @@ export type BlockMode = {
20
22
  /** Stream-like mode for {@link Cipher} */
21
23
  export type StreamMode = {
22
24
  /** Proceed message */
23
- crypt: (msg: TArg<Uint8Array>) => TRet<Uint8Array>;
25
+ crypt: CipherOrHashFunctionWrapper;
24
26
  };
25
27
  /** MAC mode for {@link Cipher} */
26
28
  export type MACMode = {
27
29
  /** Compute MAC */
28
- compute: (msg: TArg<Uint8Array>) => TRet<Uint8Array>;
30
+ compute: CipherOrHashFunctionWrapper;
29
31
  };
30
32
  /** GMAC mode for {@link Cipher} */
31
33
  export type GMACMode = {
32
34
  /** Compute MAC */
33
- compute: (aad: TArg<Uint8Array>, msg: TArg<Uint8Array>) => TRet<Uint8Array>;
35
+ compute: (msg: TArg<Uint8Array>, aad?: TArg<Uint8Array>) => TRet<Uint8Array>;
34
36
  };
35
37
  /** AEAD mode for {@link Cipher} */
36
38
  export type AEADMode = {
package/utils.d.ts CHANGED
@@ -1,8 +1,14 @@
1
1
  import { type TArg, type TRet } from "@noble/curves/utils.js";
2
+ import type { Cipher } from "./types.js";
3
+ import { Kalyna } from "./kalyna/index.js";
2
4
  export declare const xorBytes: (a: TArg<Uint8Array>, b: TArg<Uint8Array>) => TRet<Uint8Array>;
3
5
  export declare const bytesToUint64sBE: (b: TArg<Uint8Array>) => TRet<BigUint64Array>;
4
6
  export declare const uint64sToBytesBE: (w: TArg<BigUint64Array>) => TRet<Uint8Array>;
5
7
  export declare const bytesToUint64sLE: (b: TArg<Uint8Array>) => TRet<BigUint64Array>;
6
8
  export declare const uint64sToBytesLE: (w: TArg<BigUint64Array>) => TRet<Uint8Array>;
7
9
  export declare const byte: (a: bigint) => number;
10
+ export declare const getGf2mReductionBytes: (blockSize: number) => number[];
8
11
  export declare const gf2mMul: (blockSize: number, a: TArg<Uint8Array>, b: TArg<Uint8Array>) => TRet<Uint8Array>;
12
+ export declare const isKalyna: (cipher: Cipher) => cipher is Kalyna;
13
+ export declare const assertKalyna: (cipher: Cipher) => void;
14
+ export declare const reverseBytes: (bytes: TArg<Uint8Array>) => TRet<Uint8Array>;
package/utils.js CHANGED
@@ -1,4 +1,5 @@
1
- import { bytesToNumberBE, bytesToNumberLE, numberToBytesBE, numberToBytesLE } from "@noble/curves/utils.js";
1
+ import { copyBytes, bytesToNumberBE, bytesToNumberLE, numberToBytesBE, numberToBytesLE } from "@noble/curves/utils.js";
2
+ import { Kalyna } from "./kalyna/index.js";
2
3
  export const xorBytes = (a, b) => {
3
4
  const mlen = Math.min(a.length, b.length);
4
5
  const result = new Uint8Array(mlen);
@@ -33,10 +34,15 @@ export const uint64sToBytesLE = (w) => {
33
34
  return result;
34
35
  };
35
36
  export const byte = (a) => Number(a & 0xffn);
37
+ export const getGf2mReductionBytes = (blockSize) => [
38
+ [0x87],
39
+ [0x25, 0x04],
40
+ [0x25, 0x01]
41
+ ][Math.log2(blockSize) - 4];
36
42
  export const gf2mMul = (blockSize, a, b) => {
37
- const temp = new Uint8Array(a);
43
+ const temp = copyBytes(a);
38
44
  const result = new Uint8Array(blockSize);
39
- const reductionBytes = [[0x87], [0x25, 0x04], [0x25, 0x01]][Math.log2(blockSize) - 4];
45
+ const reductionBytes = getGf2mReductionBytes(blockSize);
40
46
  for (let i = 0; i < blockSize * 8; i++) {
41
47
  const byteIndex = Math.floor(i / 8);
42
48
  const bitIndex = i % 8;
@@ -57,3 +63,9 @@ export const gf2mMul = (blockSize, a, b) => {
57
63
  }
58
64
  return result;
59
65
  };
66
+ export const isKalyna = (cipher) => cipher instanceof Kalyna;
67
+ export const assertKalyna = (cipher) => {
68
+ if (!isKalyna(cipher))
69
+ throw new Error("Cipher mode only for Kalyna");
70
+ };
71
+ export const reverseBytes = (bytes) => copyBytes(bytes).reverse();