@li0ard/dstu 0.1.9 → 0.2.1

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 (55) hide show
  1. package/README.md +1 -1
  2. package/const.d.ts +5 -14
  3. package/const.js +47 -1010
  4. package/dstu4145/const.js +15 -15
  5. package/dstu4145/ec/index.d.ts +4 -0
  6. package/dstu4145/ec/index.js +28 -27
  7. package/dstu4145/ecies.d.ts +2 -2
  8. package/dstu4145/ecies.js +13 -2
  9. package/dstu4145/gf2m/utils.js +1 -1
  10. package/dstu4145/index.js +26 -17
  11. package/dstu9041/curve.d.ts +1 -1
  12. package/dstu9041/curve.js +16 -29
  13. package/dstu9041/index.js +21 -25
  14. package/dstu9311/const.js +12 -12
  15. package/dstu9311/index.d.ts +1 -2
  16. package/dstu9311/index.js +37 -34
  17. package/gf/index.d.ts +5 -0
  18. package/gf/index.js +62 -0
  19. package/gost34311/index.d.ts +55 -0
  20. package/gost34311/index.js +167 -0
  21. package/index.d.ts +1 -0
  22. package/index.js +1 -0
  23. package/iso15946.d.ts +1 -1
  24. package/iso15946.js +11 -6
  25. package/kalyna/index.d.ts +3 -5
  26. package/kalyna/index.js +119 -98
  27. package/keywrap.js +11 -13
  28. package/kupyna/index.d.ts +100 -133
  29. package/kupyna/index.js +192 -180
  30. package/modes/_guard.d.ts +4 -0
  31. package/modes/_guard.js +6 -0
  32. package/modes/cbc.d.ts +1 -1
  33. package/modes/cbc.js +6 -7
  34. package/modes/ccm.js +13 -4
  35. package/modes/cfb.js +43 -51
  36. package/modes/cmac.js +6 -2
  37. package/modes/ctr.js +6 -4
  38. package/modes/ecb.js +2 -2
  39. package/modes/gcm.d.ts +4 -1
  40. package/modes/gcm.js +33 -5
  41. package/modes/gmac.js +8 -9
  42. package/modes/index.d.ts +1 -1
  43. package/modes/index.js +1 -1
  44. package/modes/kw.js +10 -17
  45. package/modes/ofb.js +5 -4
  46. package/modes/xts.js +15 -25
  47. package/package.json +12 -9
  48. package/strumok/index.d.ts +4 -5
  49. package/strumok/index.js +132 -87
  50. package/utils.d.ts +7 -13
  51. package/utils.js +31 -61
  52. package/dstu9311/hash.d.ts +0 -41
  53. package/dstu9311/hash.js +0 -98
  54. package/kupyna/kmac.d.ts +0 -46
  55. package/kupyna/kmac.js +0 -125
package/modes/cfb.js CHANGED
@@ -1,5 +1,6 @@
1
- import { concatBytes, copyBytes } from "@noble/hashes/utils.js";
2
- import { isKalyna, xorBytes } from "../utils.js";
1
+ import { abytes, anumber, copyBytes } from "@noble/hashes/utils.js";
2
+ import { abytesAligned, xorBytes } from "../utils.js";
3
+ import { isKalyna } from "./_guard.js";
3
4
  /** Cipher Feedback (CFB) mode */
4
5
  export const cfb = (cipher, iv, q = cipher.blockSize) => {
5
6
  const _isKalyna = isKalyna(cipher);
@@ -7,61 +8,47 @@ export const cfb = (cipher, iv, q = cipher.blockSize) => {
7
8
  throw new Error('q must be 1, 8, 16, 32, or 64');
8
9
  if (_isKalyna && q > cipher.blockSize)
9
10
  throw new Error('q cannot exceed block size');
10
- if (!_isKalyna && (iv.length === 0 || iv.length % cipher.blockSize !== 0))
11
- throw new Error("Invalid IV size");
11
+ abytesAligned(iv, cipher.blockSize, "iv");
12
+ anumber(q, "q");
12
13
  if (_isKalyna)
13
14
  return Object.freeze({
14
15
  encrypt: (plaintext) => {
16
+ abytes(plaintext, undefined, "plaintext");
15
17
  const gamma = cipher.encrypt(iv);
16
18
  const feed = copyBytes(iv);
17
19
  const result = new Uint8Array(plaintext.length);
18
- let offset = 0, dataOff = 0;
19
- while (offset > 0 && dataOff < plaintext.length) {
20
- result[dataOff] = plaintext[dataOff] ^ gamma[offset];
21
- feed[offset++] = result[dataOff++];
22
- if (offset >= cipher.blockSize) {
23
- gamma.set(cipher.encrypt(feed));
24
- offset = cipher.blockSize - q;
25
- }
26
- }
27
- while (dataOff + q <= plaintext.length) {
20
+ let offset = 0;
21
+ while (offset + q <= plaintext.length) {
28
22
  for (let i = 0; i < q; i++)
29
- result[dataOff + i] = plaintext[dataOff + i] ^ gamma[cipher.blockSize - q + i];
23
+ result[offset + i] = plaintext[offset + i] ^ gamma[cipher.blockSize - q + i];
30
24
  feed.set(gamma.subarray(0, cipher.blockSize - q));
31
- feed.set(result.subarray(dataOff, dataOff + q), cipher.blockSize - q);
25
+ feed.set(result.subarray(offset, offset + q), cipher.blockSize - q);
32
26
  gamma.set(cipher.encrypt(feed));
33
- dataOff += q;
27
+ offset += q;
34
28
  }
35
- while (dataOff < plaintext.length) {
36
- result[dataOff] = plaintext[dataOff] ^ gamma[cipher.blockSize - (plaintext.length - dataOff)];
37
- dataOff++;
29
+ while (offset < plaintext.length) {
30
+ result[offset] = plaintext[offset] ^ gamma[cipher.blockSize - (plaintext.length - offset)];
31
+ offset++;
38
32
  }
39
33
  return result;
40
34
  },
41
35
  decrypt: (ciphertext) => {
36
+ abytes(ciphertext, undefined, "ciphertext");
42
37
  const gamma = cipher.encrypt(iv);
43
38
  const feed = copyBytes(iv);
44
39
  const result = new Uint8Array(ciphertext.length);
45
- let offset = 0, dataOff = 0;
46
- while (offset > 0 && dataOff < ciphertext.length) {
47
- result[dataOff] = ciphertext[dataOff] ^ gamma[offset];
48
- feed[offset++] = ciphertext[dataOff++];
49
- if (offset >= cipher.blockSize) {
50
- gamma.set(cipher.encrypt(feed));
51
- offset = cipher.blockSize - q;
52
- }
53
- }
54
- while (dataOff + q <= ciphertext.length) {
40
+ let offset = 0;
41
+ while (offset + q <= ciphertext.length) {
55
42
  for (let i = 0; i < q; i++)
56
- result[dataOff + i] = ciphertext[dataOff + i] ^ gamma[cipher.blockSize - q + i];
43
+ result[offset + i] = ciphertext[offset + i] ^ gamma[cipher.blockSize - q + i];
57
44
  feed.set(gamma.subarray(0, cipher.blockSize - q));
58
- feed.set(ciphertext.subarray(dataOff, dataOff + q), cipher.blockSize - q);
45
+ feed.set(ciphertext.subarray(offset, offset + q), cipher.blockSize - q);
59
46
  gamma.set(cipher.encrypt(feed));
60
- dataOff += q;
47
+ offset += q;
61
48
  }
62
- while (dataOff < ciphertext.length) {
63
- result[dataOff] = ciphertext[dataOff] ^ gamma[cipher.blockSize - (ciphertext.length - dataOff)];
64
- dataOff++;
49
+ while (offset < ciphertext.length) {
50
+ result[offset] = ciphertext[offset] ^ gamma[cipher.blockSize - (ciphertext.length - offset)];
51
+ offset++;
65
52
  }
66
53
  return result;
67
54
  }
@@ -69,29 +56,34 @@ export const cfb = (cipher, iv, q = cipher.blockSize) => {
69
56
  else
70
57
  return Object.freeze({
71
58
  encrypt: (plaintext) => {
72
- let r = [];
59
+ abytes(plaintext, undefined, "plaintext");
60
+ const r = [];
73
61
  for (let i = 0; i < iv.length; i += cipher.blockSize)
74
62
  r.push(iv.subarray(i, i + cipher.blockSize));
75
- const len = Math.max(8, (plaintext.length + 7) & ~7);
76
- const result = [];
77
- for (let i = 0; i < len; i += cipher.blockSize) {
78
- result.push(xorBytes(cipher.encrypt(r[0]), plaintext.subarray(i, i + cipher.blockSize)));
79
- r = r.slice(1).concat(result[result.length - 1]);
63
+ const out = new Uint8Array(plaintext.length);
64
+ for (let n = 0; n < Math.ceil(plaintext.length / cipher.blockSize); n++) {
65
+ const i = n * cipher.blockSize;
66
+ const ct = xorBytes(plaintext.subarray(i, i + cipher.blockSize), cipher.encrypt(r[0]));
67
+ out.set(ct, i);
68
+ r.shift();
69
+ r.push(ct);
80
70
  }
81
- return concatBytes(...result);
71
+ return out;
82
72
  },
83
73
  decrypt: (ciphertext) => {
84
- let r = [];
74
+ abytes(ciphertext, undefined, "ciphertext");
75
+ const r = [];
85
76
  for (let i = 0; i < iv.length; i += cipher.blockSize)
86
77
  r.push(iv.subarray(i, i + cipher.blockSize));
87
- const len = Math.max(8, (ciphertext.length + 7) & ~7);
88
- const result = [];
89
- for (let i = 0; i < len; i += cipher.blockSize) {
90
- const blk = ciphertext.slice(i, i + cipher.blockSize);
91
- result.push(xorBytes(cipher.encrypt(r[0]), blk));
92
- r = r.slice(1).concat(blk);
78
+ const out = new Uint8Array(ciphertext.length);
79
+ for (let n = 0; n < Math.ceil(ciphertext.length / cipher.blockSize); n++) {
80
+ const i = n * cipher.blockSize;
81
+ const blk = ciphertext.subarray(i, i + cipher.blockSize);
82
+ out.set(xorBytes(blk, cipher.encrypt(r[0])), i);
83
+ r.shift();
84
+ r.push(blk);
93
85
  }
94
- return concatBytes(...result);
86
+ return out;
95
87
  }
96
88
  });
97
89
  };
package/modes/cmac.js CHANGED
@@ -1,11 +1,14 @@
1
- import { concatBytes, bytesToNumberLE, numberToBytesLE } from "@noble/curves/utils.js";
1
+ import { anumber, concatBytes, bytesToNumberLE, numberToBytesLE, abytes } from "@noble/curves/utils.js";
2
2
  import { pad } from "../padding.js";
3
- import { isKalyna, xorBytes } from "../utils.js";
3
+ import { xorBytes } from "../utils.js";
4
4
  import { keySequences } from "../dstu9311/const.js";
5
+ import { isKalyna } from "./_guard.js";
5
6
  export const cmac = (cipher, q = 16) => {
7
+ anumber(q, "q");
6
8
  if (isKalyna(cipher))
7
9
  return Object.freeze({
8
10
  compute: (msg) => {
11
+ abytes(msg, undefined, "msg");
9
12
  let data = msg;
10
13
  const zeroBlock = new Uint8Array(cipher.blockSize);
11
14
  if (data.length % cipher.blockSize !== 0) {
@@ -27,6 +30,7 @@ export const cmac = (cipher, q = 16) => {
27
30
  else
28
31
  return Object.freeze({
29
32
  compute: (msg) => {
33
+ abytes(msg, undefined, "msg");
30
34
  const paddedLength = Math.max(8, (msg.length + 7) & ~7);
31
35
  const paddedData = new Uint8Array(paddedLength);
32
36
  paddedData.set(msg);
package/modes/ctr.js CHANGED
@@ -1,11 +1,12 @@
1
- import { createView } from "@noble/hashes/utils.js";
2
- import { isKalyna, xorBytes } from "../utils.js";
1
+ import { abytes, createView } from "@noble/hashes/utils.js";
2
+ import { xorBytes } from "../utils.js";
3
+ import { isKalyna } from "./_guard.js";
3
4
  const C1 = 0x01010104, C2 = 0x01010101;
4
5
  /** Counter (CTR) mode */
5
6
  export const ctr = (cipher, iv) => {
6
- if (iv.length !== cipher.blockSize)
7
- throw new Error("Invalid IV size");
7
+ abytes(iv, cipher.blockSize, "iv");
8
8
  const incrementCounter = (ctr) => {
9
+ abytes(ctr);
9
10
  if (isKalyna(cipher)) {
10
11
  let j = 0;
11
12
  while (j < ctr.length)
@@ -23,6 +24,7 @@ export const ctr = (cipher, iv) => {
23
24
  };
24
25
  return Object.freeze({
25
26
  crypt: (msg) => {
27
+ abytes(msg, undefined, "msg");
26
28
  const buf = cipher.encrypt(iv), output = new Uint8Array(msg.length);
27
29
  for (let i = 0; i < msg.length; i += cipher.blockSize) {
28
30
  incrementCounter(buf);
package/modes/ecb.js CHANGED
@@ -1,10 +1,10 @@
1
+ import { abytesAligned } from "../utils.js";
1
2
  /** Electronic Codebook (ECB) mode */
2
3
  export const ecb = (cipher) => {
3
4
  const encrypter = cipher.encrypt.bind(cipher);
4
5
  const decrypter = cipher.decrypt.bind(cipher);
5
6
  const core = (crypter, data) => {
6
- if (data.length == 0 || data.length % cipher.blockSize !== 0)
7
- throw new Error("Data not aligned");
7
+ abytesAligned(data, cipher.blockSize, "data");
8
8
  const output = new Uint8Array(data.length);
9
9
  for (let i = 0; i < data.length; i += cipher.blockSize)
10
10
  output.set(crypter(data.subarray(i, i + cipher.blockSize)), i);
package/modes/gcm.d.ts CHANGED
@@ -1,4 +1,7 @@
1
- import { type TArg } from "@noble/curves/utils.js";
1
+ import { type TArg, type TRet } from "@noble/curves/utils.js";
2
2
  import type { AEADMode, Cipher } from "../types.js";
3
+ export declare const half_ctr: (cipher: Cipher, iv: TArg<Uint8Array>) => Readonly<{
4
+ crypt: (msg: TArg<Uint8Array>) => TRet<Uint8Array>;
5
+ }>;
3
6
  /** Galois counter (GCM) mode (AEAD) */
4
7
  export declare const gcm: (cipher: Cipher, iv: TArg<Uint8Array>, q?: number) => AEADMode;
package/modes/gcm.js CHANGED
@@ -1,21 +1,49 @@
1
- import { equalBytes, concatBytes } from "@noble/curves/utils.js";
2
- import { ctr } from "./ctr.js";
1
+ import { abytes, anumber, equalBytes, concatBytes } from "@noble/curves/utils.js";
3
2
  import { gmac } from "./gmac.js";
4
- import { assertKalyna } from "../utils.js";
3
+ import { xorBytes } from "../utils.js";
4
+ import { assertKalyna } from "./_guard.js";
5
+ // CTR for GCM and CCM
6
+ export const half_ctr = (cipher, iv) => {
7
+ abytes(iv, cipher.blockSize, "iv");
8
+ return Object.freeze({
9
+ crypt: (msg) => {
10
+ abytes(msg, undefined, "msg");
11
+ const buf = cipher.encrypt(iv), output = new Uint8Array(msg.length), half_len_word = cipher.blockSize >> 1;
12
+ for (let i = 0; i < msg.length; i += cipher.blockSize) {
13
+ for (let j = 0; j < half_len_word; j++) {
14
+ if (++buf[j] != 0)
15
+ break;
16
+ }
17
+ const ct = xorBytes(cipher.encrypt(buf), msg.subarray(i, i + cipher.blockSize));
18
+ output.set(ct, i);
19
+ }
20
+ return output;
21
+ }
22
+ });
23
+ };
5
24
  /** Galois counter (GCM) mode (AEAD) */
6
25
  export const gcm = (cipher, iv, q = 16) => {
7
26
  assertKalyna(cipher);
27
+ abytes(iv, cipher.blockSize, "iv");
28
+ anumber(q, "q");
29
+ const mode = half_ctr(cipher, iv);
8
30
  return Object.freeze({
9
31
  seal: (plaintext, aad) => {
10
- const enc = ctr(cipher, iv).crypt(plaintext);
32
+ abytes(plaintext, undefined, "plaintext");
33
+ if (aad)
34
+ abytes(aad, undefined, "aad");
35
+ const enc = mode.crypt(plaintext);
11
36
  return concatBytes(enc, gmac(cipher, q).compute(enc, aad));
12
37
  },
13
38
  open: (ciphertext, aad) => {
39
+ abytes(ciphertext, undefined, "ciphertext");
40
+ if (aad)
41
+ abytes(aad, undefined, "aad");
14
42
  const enc = ciphertext.subarray(0, -q);
15
43
  const hC = gmac(cipher, q).compute(enc, aad);
16
44
  if (!equalBytes(ciphertext.subarray(-q), hC))
17
45
  throw new Error("Invalid MAC");
18
- return ctr(cipher, iv).crypt(enc);
46
+ return mode.crypt(enc);
19
47
  }
20
48
  });
21
49
  };
package/modes/gmac.js CHANGED
@@ -1,18 +1,17 @@
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
- };
1
+ import { abytes, anumber, numberToBytesLE, concatBytes } from "@noble/curves/utils.js";
2
+ import { xorBytesInPlace } from "../utils.js";
3
+ import { gf2mMultiply } from "../gf/index.js";
4
+ import { assertKalyna } from "./_guard.js";
9
5
  /** Galois Message Authentication Code (GMAC) mode */
10
6
  export const gmac = (cipher, q = 16) => {
11
7
  assertKalyna(cipher);
8
+ anumber(q, "q");
12
9
  const hblock = cipher.blockSize >> 1;
13
10
  return Object.freeze({
14
11
  compute: (msg, aad) => {
15
12
  aad ??= new Uint8Array();
13
+ abytes(msg, undefined, "msg");
14
+ abytes(aad, undefined, "aad");
16
15
  const H = cipher.encrypt(new Uint8Array(cipher.blockSize));
17
16
  const B = new Uint8Array(cipher.blockSize);
18
17
  const updateMac = (input) => {
@@ -26,7 +25,7 @@ export const gmac = (cipher, q = 16) => {
26
25
  block[blockSizeToProcess] = 0x80;
27
26
  for (let j = 0; j < cipher.blockSize; j++)
28
27
  B[j] ^= block[j];
29
- B.set(gf2mMul(cipher.blockSize, B, H));
28
+ B.set(gf2mMultiply(cipher.blockSize, B, H));
30
29
  i += cipher.blockSize;
31
30
  }
32
31
  };
package/modes/index.d.ts CHANGED
@@ -4,7 +4,7 @@ export * from "./cfb.js";
4
4
  export * from "./cmac.js";
5
5
  export * from "./ctr.js";
6
6
  export * from "./ecb.js";
7
- export * from "./gcm.js";
7
+ export { gcm } from "./gcm.js";
8
8
  export * from "./gmac.js";
9
9
  export * from "./kw.js";
10
10
  export * from "./ofb.js";
package/modes/index.js CHANGED
@@ -4,7 +4,7 @@ export * from "./cfb.js";
4
4
  export * from "./cmac.js";
5
5
  export * from "./ctr.js";
6
6
  export * from "./ecb.js";
7
- export * from "./gcm.js";
7
+ export { gcm } from "./gcm.js";
8
8
  export * from "./gmac.js";
9
9
  export * from "./kw.js";
10
10
  export * from "./ofb.js";
package/modes/kw.js CHANGED
@@ -1,20 +1,20 @@
1
- import { concatBytes } from "@noble/hashes/utils.js";
1
+ import { abytes, concatBytes } from "@noble/hashes/utils.js";
2
2
  import { pad, unpad } from "../padding.js";
3
- import { assertKalyna } from "../utils.js";
3
+ import { assertKalyna } from "./_guard.js";
4
4
  /** Key wrapping (KW) */
5
5
  export const kw = (cipher) => {
6
6
  assertKalyna(cipher);
7
7
  const half = cipher.blockSize >> 1;
8
8
  return Object.freeze({
9
9
  wrap: (key) => {
10
+ abytes(key, undefined, "key");
10
11
  let plaintext;
11
12
  if (key.length % cipher.blockSize === 0)
12
13
  plaintext = key;
13
14
  else {
14
15
  const withLength = new Uint8Array(key.length + half);
15
16
  withLength.set(key);
16
- let bitLength = key.length << 3;
17
- let i = 0;
17
+ let bitLength = key.length << 3, i = 0;
18
18
  while (bitLength > 0) {
19
19
  withLength[key.length + i] = bitLength & 0xff;
20
20
  i++;
@@ -22,12 +22,8 @@ export const kw = (cipher) => {
22
22
  }
23
23
  plaintext = pad(withLength, cipher.blockSize);
24
24
  }
25
- const r = plaintext.length / cipher.blockSize;
26
- const n = 2 * (r + 1);
27
- const v = (n - 1) * 6;
28
- const qLen = (n - 1) * half;
29
- const B = plaintext.slice(0, half);
30
- const q = new Uint8Array(qLen);
25
+ const r = plaintext.length / cipher.blockSize, n = 2 * (r + 1), v = (n - 1) * 6, qLen = (n - 1) * half;
26
+ const B = plaintext.slice(0, half), q = new Uint8Array(qLen);
31
27
  q.set(plaintext.subarray(half));
32
28
  const block = new Uint8Array(cipher.blockSize);
33
29
  for (let i = 1; i <= v; i++) {
@@ -45,14 +41,11 @@ export const kw = (cipher) => {
45
41
  return concatBytes(B, q);
46
42
  },
47
43
  unwrap: (wrappedKey) => {
44
+ abytes(wrappedKey, undefined, "wrappedKey");
48
45
  if (wrappedKey.length < 2 * cipher.blockSize)
49
46
  throw new Error("Invalid input length: must be at least 2 blocks");
50
- const r = wrappedKey.length / cipher.blockSize - 1;
51
- const n = 2 * (r + 1);
52
- const v = (n - 1) * 6;
53
- const qLen = (n - 1) * half;
54
- const B = wrappedKey.slice(0, half);
55
- const q = wrappedKey.slice(half, half + qLen);
47
+ const r = wrappedKey.length / cipher.blockSize - 1, n = 2 * (r + 1), v = (n - 1) * 6, qLen = (n - 1) * half;
48
+ const B = wrappedKey.slice(0, half), q = wrappedKey.slice(half, half + qLen);
56
49
  const block = new Uint8Array(cipher.blockSize);
57
50
  for (let i = v; i >= 1; i--) {
58
51
  block.set(q.subarray(qLen - half));
@@ -76,6 +69,6 @@ export const kw = (cipher) => {
76
69
  if (current_length % cipher.blockSize !== 0)
77
70
  current_length -= half + 1;
78
71
  return recovered.slice(0, current_length);
79
- },
72
+ }
80
73
  });
81
74
  };
package/modes/ofb.js CHANGED
@@ -1,12 +1,13 @@
1
- import {} from "@noble/hashes/utils.js";
2
- import { assertKalyna, xorBytes } from "../utils.js";
1
+ import { abytes } from "@noble/hashes/utils.js";
2
+ import { xorBytes } from "../utils.js";
3
+ import { assertKalyna } from "./_guard.js";
3
4
  /** Output Feedback (OFB) mode */
4
5
  export const ofb = (cipher, iv) => {
5
6
  assertKalyna(cipher);
6
- if (iv.length !== cipher.blockSize)
7
- throw new Error("Invalid IV size");
7
+ abytes(iv, cipher.blockSize, "iv");
8
8
  return Object.freeze({
9
9
  crypt: (msg) => {
10
+ abytes(msg, undefined, "msg");
10
11
  let buf = iv;
11
12
  const output = new Uint8Array(msg.length);
12
13
  for (let i = 0; i < msg.length; i += cipher.blockSize) {
package/modes/xts.js CHANGED
@@ -1,24 +1,10 @@
1
- import { concatBytes, copyBytes } from "@noble/hashes/utils.js";
2
- import { assertKalyna, getGf2mReductionBytes, xorBytes } from "../utils.js";
1
+ import { abytes, concatBytes } from "@noble/hashes/utils.js";
2
+ import { xorBytes } from "../utils.js";
3
+ import { gf2mDouble } from "../gf/index.js";
4
+ import { assertKalyna } from "./_guard.js";
3
5
  /** XEX Tweakable Block Ciphertext Stealing (XTS) */
4
6
  export const xts = (cipher) => {
5
7
  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
- };
22
8
  const transform = (transform, block, gamma) => xorBytes(transform(xorBytes(block, gamma)), gamma);
23
9
  const init = (iv) => {
24
10
  const gamma = cipher.encrypt(iv);
@@ -28,6 +14,8 @@ export const xts = (cipher) => {
28
14
  };
29
15
  return Object.freeze({
30
16
  encrypt: (plaintext, tweak) => {
17
+ abytes(plaintext, undefined, "plaintext");
18
+ abytes(tweak, undefined, "tweak");
31
19
  if (plaintext.length < cipher.blockSize)
32
20
  throw new Error(`Invalid length (need at least ${cipher.blockSize}, got ${plaintext.length})`);
33
21
  const gamma = init(tweak);
@@ -37,13 +25,13 @@ export const xts = (cipher) => {
37
25
  const r = plaintext.length % cipher.blockSize;
38
26
  if (r === 0) {
39
27
  for (let off = 0; off < plaintext.length; off += cipher.blockSize) {
40
- gamma.set(gf2mDouble(gamma));
28
+ gamma.set(gf2mDouble(cipher.blockSize, gamma));
41
29
  buffer.set(transform(encrypt, buffer.subarray(off, off + cipher.blockSize), gamma), off);
42
30
  }
43
31
  }
44
32
  else {
45
33
  for (let i = 0; i < k; i++) {
46
- gamma.set(gf2mDouble(gamma));
34
+ gamma.set(gf2mDouble(cipher.blockSize, gamma));
47
35
  const off = i * cipher.blockSize;
48
36
  buffer.set(transform(encrypt, buffer.subarray(off, off + cipher.blockSize), gamma), off);
49
37
  }
@@ -51,13 +39,15 @@ export const xts = (cipher) => {
51
39
  const tail_off = k * cipher.blockSize;
52
40
  const scratch = buffer.slice(last_off, last_off + cipher.blockSize);
53
41
  const combined = concatBytes(buffer.subarray(tail_off, tail_off + r), scratch.subarray(r));
54
- const encrypted = transform(encrypt, combined, gf2mDouble(gamma));
42
+ const encrypted = transform(encrypt, combined, gf2mDouble(cipher.blockSize, gamma));
55
43
  buffer.set(encrypted, last_off);
56
44
  buffer.set(scratch.subarray(0, r), tail_off);
57
45
  }
58
46
  return buffer;
59
47
  },
60
48
  decrypt: (ciphertext, tweak) => {
49
+ abytes(ciphertext, undefined, "ciphertext");
50
+ abytes(tweak, undefined, "tweak");
61
51
  if (ciphertext.length < cipher.blockSize)
62
52
  throw new Error(`Invalid length (need at least ${cipher.blockSize}, got ${ciphertext.length})`);
63
53
  const gamma = init(tweak);
@@ -67,18 +57,18 @@ export const xts = (cipher) => {
67
57
  const r = ciphertext.length % cipher.blockSize;
68
58
  if (r === 0) {
69
59
  for (let off = 0; off < ciphertext.length; off += cipher.blockSize) {
70
- gamma.set(gf2mDouble(gamma));
60
+ gamma.set(gf2mDouble(cipher.blockSize, gamma));
71
61
  buffer.set(transform(decrypt, buffer.subarray(off, off + cipher.blockSize), gamma), off);
72
62
  }
73
63
  }
74
64
  else {
75
65
  for (let i = 0; i < k - 1; i++) {
76
- gamma.set(gf2mDouble(gamma));
66
+ gamma.set(gf2mDouble(cipher.blockSize, gamma));
77
67
  const off = i * cipher.blockSize;
78
68
  buffer.set(transform(decrypt, buffer.subarray(off, off + cipher.blockSize), gamma), off);
79
69
  }
80
- gamma.set(gf2mDouble(gamma));
81
- const gamma_k_plus_1 = gf2mDouble(gamma);
70
+ gamma.set(gf2mDouble(cipher.blockSize, gamma));
71
+ const gamma_k_plus_1 = gf2mDouble(cipher.blockSize, gamma);
82
72
  const last_off = (k - 1) * cipher.blockSize;
83
73
  const tail_off = k * cipher.blockSize;
84
74
  const combined = transform(decrypt, buffer.subarray(last_off, last_off + cipher.blockSize), gamma_k_plus_1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@li0ard/dstu",
3
- "version": "0.1.9",
3
+ "version": "0.2.1",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "type": "module",
@@ -9,10 +9,14 @@
9
9
  "./dstu4145.js": "./dstu4145/index.js",
10
10
  "./dstu9041.js": "./dstu9041/index.js",
11
11
  "./dstu9311.js": "./dstu9311/index.js",
12
+ "./gost34311.js": "./gost34311/index.js",
12
13
  "./kalyna.js": "./kalyna/index.js",
13
14
  "./kupyna.js": "./kupyna/index.js",
14
15
  "./modes.js": "./modes/index.js",
15
16
  "./strumok.js": "./strumok/index.js",
17
+ "./iso15946.js": "./iso15946.js",
18
+ "./keywrap.js": "./keywrap.js",
19
+ "./kmac.js": "./kmac.js",
16
20
  "./types.js": "./types.js"
17
21
  },
18
22
  "author": "li0ard",
@@ -34,21 +38,20 @@
34
38
  "devDependencies": {
35
39
  "@types/bn.js": "^5.2.0",
36
40
  "@types/bun": "latest",
37
- "typedoc": "^0.28.19"
38
- },
39
- "peerDependencies": {
40
- "typescript": "^5"
41
+ "typedoc": "^0.28.20",
42
+ "typescript": "^5.9.3"
41
43
  },
42
44
  "dependencies": {
43
45
  "@noble/curves": "^2.4.0",
44
46
  "@noble/hashes": "^2.4.0",
45
- "bn.js": "^5.2.3",
46
- "@peculiar/asn1-schema": "^2.9.5",
47
- "@peculiar/asn1-x509": "^2.9.5"
47
+ "bn.js": "^5.2.5",
48
+ "@peculiar/asn1-schema": "^2.11.1",
49
+ "@peculiar/asn1-x509": "^2.11.1"
48
50
  },
49
51
  "files": ["**/*.js", "**/*.d.ts"],
50
52
  "publishConfig": {
51
53
  "access": "public"
52
54
  },
53
- "keywords": ["dstu", "crypto", "kalyna", "kupyna", "strumok"]
55
+ "keywords": ["dstu", "crypto", "kalyna", "kupyna", "strumok"],
56
+ "sideEffects": false
54
57
  }
@@ -1,4 +1,4 @@
1
- import type { TArg, TRet } from "@noble/hashes/utils.js";
1
+ import { type TArg, type TRet } from "@noble/hashes/utils.js";
2
2
  import type { StreamCipher } from "../types.js";
3
3
  /** Strumok stream cipher */
4
4
  export declare class Strumok implements StreamCipher {
@@ -6,16 +6,15 @@ export declare class Strumok implements StreamCipher {
6
6
  readonly keySize: number;
7
7
  private S;
8
8
  private r;
9
- private key;
10
- private iv;
9
+ private ks;
11
10
  /**
12
11
  * Strumok stream cipher
13
12
  * @param key Encryption key (256 or 512 bit)
14
13
  */
15
14
  constructor(key: TArg<Uint8Array>, iv: TArg<Uint8Array>);
16
15
  private updateR;
17
- private init;
18
- private round;
16
+ private feed;
17
+ private gen;
19
18
  /** Generate next keystream */
20
19
  next_stream(): TRet<Uint8Array>;
21
20
  /** Perform encryption/decryption */