@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/modes/index.d.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  export * from "./cbc.js";
2
2
  export * from "./cfb.js";
3
+ export * from "./cmac.js";
3
4
  export * from "./ctr.js";
4
5
  export * from "./ecb.js";
5
6
  export * from "./gcm.js";
7
+ export * from "./gmac.js";
6
8
  export * from "./kw.js";
7
- export * from "./mac.js";
8
9
  export * from "./ofb.js";
9
10
  export * from "./xts.js";
package/modes/index.js CHANGED
@@ -1,9 +1,10 @@
1
1
  export * from "./cbc.js";
2
2
  export * from "./cfb.js";
3
+ export * from "./cmac.js";
3
4
  export * from "./ctr.js";
4
5
  export * from "./ecb.js";
5
6
  export * from "./gcm.js";
7
+ export * from "./gmac.js";
6
8
  export * from "./kw.js";
7
- export * from "./mac.js";
8
9
  export * from "./ofb.js";
9
10
  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.7",
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",
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,13 @@
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;
package/utils.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import { bytesToNumberBE, bytesToNumberLE, numberToBytesBE, numberToBytesLE } from "@noble/curves/utils.js";
2
+ import { copyBytes } from "@noble/hashes/utils.js";
3
+ import { Kalyna } from "./kalyna/index.js";
2
4
  export const xorBytes = (a, b) => {
3
5
  const mlen = Math.min(a.length, b.length);
4
6
  const result = new Uint8Array(mlen);
@@ -33,10 +35,15 @@ export const uint64sToBytesLE = (w) => {
33
35
  return result;
34
36
  };
35
37
  export const byte = (a) => Number(a & 0xffn);
38
+ export const getGf2mReductionBytes = (blockSize) => [
39
+ [0x87],
40
+ [0x25, 0x04],
41
+ [0x25, 0x01]
42
+ ][Math.log2(blockSize) - 4];
36
43
  export const gf2mMul = (blockSize, a, b) => {
37
- const temp = new Uint8Array(a);
44
+ const temp = copyBytes(a);
38
45
  const result = new Uint8Array(blockSize);
39
- const reductionBytes = [[0x87], [0x25, 0x04], [0x25, 0x01]][Math.log2(blockSize) - 4];
46
+ const reductionBytes = getGf2mReductionBytes(blockSize);
40
47
  for (let i = 0; i < blockSize * 8; i++) {
41
48
  const byteIndex = Math.floor(i / 8);
42
49
  const bitIndex = i % 8;
@@ -57,3 +64,8 @@ export const gf2mMul = (blockSize, a, b) => {
57
64
  }
58
65
  return result;
59
66
  };
67
+ export const isKalyna = (cipher) => cipher instanceof Kalyna;
68
+ export const assertKalyna = (cipher) => {
69
+ if (!isKalyna(cipher))
70
+ throw new Error("Cipher mode only for Kalyna");
71
+ };
package/modes/mac.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import type { Kalyna } from "../kalyna/index.js";
2
- import type { GMACMode, MACMode } from "../types.js";
3
- /** Message Authentication Code (MAC) mode */
4
- export declare const cmac: (cipher: Kalyna, q?: number) => MACMode;
5
- /** Galois Message Authentication Code (GMAC) mode */
6
- export declare const gmac: (cipher: Kalyna, q?: number) => GMACMode;
package/modes/mac.js DELETED
@@ -1,61 +0,0 @@
1
- import { concatBytes, copyBytes } from "@noble/hashes/utils.js";
2
- import { pad } from "../padding.js";
3
- import { gf2mMul, xorBytes } from "../utils.js";
4
- import { numberToBytesLE } from "@noble/curves/utils.js";
5
- /** Message Authentication Code (MAC) mode */
6
- export const cmac = (cipher, q = 16) => Object.freeze({
7
- compute: (msg) => {
8
- let data = copyBytes(msg);
9
- const zeroBlock = new Uint8Array(cipher.blockSize);
10
- if (data.length % cipher.blockSize !== 0) {
11
- data = pad(data, cipher.blockSize);
12
- zeroBlock[0] = 1;
13
- }
14
- const Kd = cipher.encrypt(zeroBlock);
15
- let c = new Uint8Array(cipher.blockSize);
16
- const numBlocks = data.length / cipher.blockSize;
17
- for (let i = 0; i < numBlocks - 1; i++) {
18
- const blockStart = i * cipher.blockSize;
19
- c = cipher.encrypt(xorBytes(c, data.subarray(blockStart, blockStart + cipher.blockSize)));
20
- }
21
- const lastBlockStart = (numBlocks - 1) * cipher.blockSize;
22
- c = cipher.encrypt(xorBytes(xorBytes(c, data.subarray(lastBlockStart, lastBlockStart + cipher.blockSize)), Kd));
23
- return c.slice(0, q);
24
- }
25
- });
26
- /** Galois Message Authentication Code (GMAC) mode */
27
- export const gmac = (cipher, q = 16) => Object.freeze({
28
- compute: (aad, msg) => {
29
- const blockSize = cipher.blockSize;
30
- const H = cipher.encrypt(new Uint8Array(blockSize));
31
- let B = new Uint8Array(blockSize);
32
- let i = 0;
33
- while (i < aad.length) {
34
- const blockSizeToProcess = Math.min(blockSize, aad.length - i);
35
- const block = new Uint8Array(blockSize);
36
- for (let j = 0; j < blockSizeToProcess; j++)
37
- block[j] = aad[i + j];
38
- if (blockSizeToProcess < blockSize)
39
- block[blockSizeToProcess] = 0x80;
40
- for (let j = 0; j < blockSize; j++)
41
- B[j] ^= block[j];
42
- B = gf2mMul(blockSize, B, H);
43
- i += blockSize;
44
- }
45
- i = 0;
46
- while (i < msg.length) {
47
- const blockSizeToProcess = Math.min(blockSize, msg.length - i);
48
- const block = new Uint8Array(blockSize);
49
- for (let j = 0; j < blockSizeToProcess; j++)
50
- block[j] = msg[i + j];
51
- if (blockSizeToProcess < blockSize)
52
- block[blockSizeToProcess] = 0x80;
53
- for (let j = 0; j < blockSize; j++)
54
- B[j] ^= block[j];
55
- B = gf2mMul(blockSize, B, H);
56
- i += blockSize;
57
- }
58
- B = xorBytes(B, concatBytes(numberToBytesLE(aad.length * 8, blockSize / 2), numberToBytesLE(msg.length * 8, blockSize / 2)));
59
- return cipher.encrypt(B).slice(0, q);
60
- }
61
- });