@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.
@@ -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,13 +1,14 @@
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));
@@ -19,7 +20,7 @@ export const cbc = (cipher, iv) => {
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);
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;
package/modes/cfb.js CHANGED
@@ -1,65 +1,97 @@
1
+ import { concatBytes, copyBytes } from "@noble/hashes/utils.js";
2
+ import { isKalyna, xorBytes } from "../utils.js";
1
3
  /** Cipher Feedback (CFB) mode */
2
4
  export const cfb = (cipher, iv, q = cipher.blockSize) => {
3
- if (q !== 1 && q !== 8 && q !== 16 && q !== 32 && q !== 64)
5
+ const _isKalyna = isKalyna(cipher);
6
+ if (_isKalyna && q !== 1 && q !== 8 && q !== 16 && q !== 32 && q !== 64)
4
7
  throw new Error('q must be 1, 8, 16, 32, or 64');
5
- if (q > cipher.blockSize)
8
+ if (_isKalyna && q > cipher.blockSize)
6
9
  throw new Error('q cannot exceed block size');
7
- return Object.freeze({
8
- encrypt: (plaintext) => {
9
- let gamma = cipher.encrypt(iv);
10
- const feed = new Uint8Array(iv);
11
- let offset = 0;
12
- const result = new Uint8Array(plaintext.length);
13
- let dataOff = 0;
14
- while (offset > 0 && dataOff < plaintext.length) {
15
- result[dataOff] = plaintext[dataOff] ^ gamma[offset];
16
- feed[offset++] = result[dataOff++];
17
- if (offset >= cipher.blockSize) {
18
- gamma = cipher.encrypt(feed);
19
- offset = cipher.blockSize - q;
10
+ if (!_isKalyna && (iv.length === 0 || iv.length % cipher.blockSize !== 0))
11
+ throw new Error("Invalid IV size");
12
+ if (_isKalyna)
13
+ return Object.freeze({
14
+ encrypt: (plaintext) => {
15
+ const gamma = cipher.encrypt(iv);
16
+ const feed = copyBytes(iv);
17
+ 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
+ }
20
26
  }
21
- }
22
- while (dataOff + q <= plaintext.length) {
23
- for (let i = 0; i < q; i++)
24
- result[dataOff + i] = plaintext[dataOff + i] ^ gamma[cipher.blockSize - q + i];
25
- feed.set(gamma.subarray(0, cipher.blockSize - q));
26
- feed.set(result.subarray(dataOff, dataOff + q), cipher.blockSize - q);
27
- gamma = cipher.encrypt(feed);
28
- dataOff += q;
29
- }
30
- while (dataOff < plaintext.length) {
31
- result[dataOff] = plaintext[dataOff] ^ gamma[cipher.blockSize - (plaintext.length - dataOff)];
32
- dataOff++;
33
- }
34
- return result;
35
- },
36
- decrypt: (ciphertext) => {
37
- let gamma = cipher.encrypt(iv);
38
- const feed = new Uint8Array(iv);
39
- let offset = 0;
40
- const result = new Uint8Array(ciphertext.length);
41
- let dataOff = 0;
42
- while (offset > 0 && dataOff < ciphertext.length) {
43
- result[dataOff] = ciphertext[dataOff] ^ gamma[offset];
44
- feed[offset++] = ciphertext[dataOff++];
45
- if (offset >= cipher.blockSize) {
46
- gamma = cipher.encrypt(feed);
47
- offset = cipher.blockSize - q;
27
+ while (dataOff + q <= plaintext.length) {
28
+ for (let i = 0; i < q; i++)
29
+ result[dataOff + i] = plaintext[dataOff + i] ^ gamma[cipher.blockSize - q + i];
30
+ feed.set(gamma.subarray(0, cipher.blockSize - q));
31
+ feed.set(result.subarray(dataOff, dataOff + q), cipher.blockSize - q);
32
+ gamma.set(cipher.encrypt(feed));
33
+ dataOff += q;
48
34
  }
35
+ while (dataOff < plaintext.length) {
36
+ result[dataOff] = plaintext[dataOff] ^ gamma[cipher.blockSize - (plaintext.length - dataOff)];
37
+ dataOff++;
38
+ }
39
+ return result;
40
+ },
41
+ decrypt: (ciphertext) => {
42
+ const gamma = cipher.encrypt(iv);
43
+ const feed = copyBytes(iv);
44
+ 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) {
55
+ for (let i = 0; i < q; i++)
56
+ result[dataOff + i] = ciphertext[dataOff + i] ^ gamma[cipher.blockSize - q + i];
57
+ feed.set(gamma.subarray(0, cipher.blockSize - q));
58
+ feed.set(ciphertext.subarray(dataOff, dataOff + q), cipher.blockSize - q);
59
+ gamma.set(cipher.encrypt(feed));
60
+ dataOff += q;
61
+ }
62
+ while (dataOff < ciphertext.length) {
63
+ result[dataOff] = ciphertext[dataOff] ^ gamma[cipher.blockSize - (ciphertext.length - dataOff)];
64
+ dataOff++;
65
+ }
66
+ return result;
49
67
  }
50
- while (dataOff + q <= ciphertext.length) {
51
- for (let i = 0; i < q; i++)
52
- result[dataOff + i] = ciphertext[dataOff + i] ^ gamma[cipher.blockSize - q + i];
53
- feed.set(gamma.subarray(0, cipher.blockSize - q));
54
- feed.set(ciphertext.subarray(dataOff, dataOff + q), cipher.blockSize - q);
55
- gamma = cipher.encrypt(feed);
56
- dataOff += q;
57
- }
58
- while (dataOff < ciphertext.length) {
59
- result[dataOff] = ciphertext[dataOff] ^ gamma[cipher.blockSize - (ciphertext.length - dataOff)];
60
- dataOff++;
68
+ });
69
+ else
70
+ return Object.freeze({
71
+ encrypt: (plaintext) => {
72
+ let r = [];
73
+ for (let i = 0; i < iv.length; i += cipher.blockSize)
74
+ 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]);
80
+ }
81
+ return concatBytes(...result);
82
+ },
83
+ decrypt: (ciphertext) => {
84
+ let r = [];
85
+ for (let i = 0; i < iv.length; i += cipher.blockSize)
86
+ 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);
93
+ }
94
+ return concatBytes(...result);
61
95
  }
62
- return result;
63
- }
64
- });
96
+ });
65
97
  };
@@ -0,0 +1,2 @@
1
+ import type { Cipher, MACMode } from "../types.js";
2
+ export declare const cmac: (cipher: Cipher, q?: number) => MACMode;
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
1
  import { type TArg } from "@noble/hashes/utils.js";
2
- import type { Kalyna } from "../kalyna/index.js";
3
- import type { AEADMode } from "../types.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,22 @@
1
1
  import { concatBytes } from "@noble/hashes/utils.js";
2
2
  import { ctr } from "./ctr.js";
3
- import { gmac } from "./mac.js";
3
+ import { gmac } from "./gmac.js";
4
4
  import { equalBytes } from "@noble/curves/utils.js";
5
+ import { assertKalyna } from "../utils.js";
5
6
  /** 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
- });
7
+ export const gcm = (cipher, iv, q = 16) => {
8
+ assertKalyna(cipher);
9
+ return Object.freeze({
10
+ seal: (plaintext, aad) => {
11
+ const enc = ctr(cipher, iv).crypt(plaintext);
12
+ return concatBytes(enc, gmac(cipher, q).compute(enc, aad));
13
+ },
14
+ open: (ciphertext, aad) => {
15
+ const enc = ciphertext.subarray(0, -q);
16
+ const hC = gmac(cipher, q).compute(enc, aad);
17
+ if (!equalBytes(ciphertext.subarray(-q), hC))
18
+ throw new Error("Invalid MAC");
19
+ return ctr(cipher, iv).crypt(enc);
20
+ }
21
+ });
22
+ };
@@ -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,40 @@
1
+ import { concatBytes } from "@noble/hashes/utils.js";
2
+ import { assertKalyna, gf2mMul } from "../utils.js";
3
+ import { numberToBytesLE } from "@noble/curves/utils.js";
4
+ const xorBytesInPlace = (a, b) => {
5
+ if (a.length != b.length)
6
+ throw new Error("Byte arrays must have same length");
7
+ for (let i = 0; i < a.length; i++)
8
+ a[i] ^= b[i];
9
+ };
10
+ /** Galois Message Authentication Code (GMAC) mode */
11
+ export const gmac = (cipher, q = 16) => {
12
+ assertKalyna(cipher);
13
+ const hblock = cipher.blockSize >> 1;
14
+ return Object.freeze({
15
+ compute: (msg, aad) => {
16
+ aad ??= new Uint8Array();
17
+ const H = cipher.encrypt(new Uint8Array(cipher.blockSize));
18
+ const B = new Uint8Array(cipher.blockSize);
19
+ const updateMac = (input) => {
20
+ let i = 0;
21
+ while (i < input.length) {
22
+ const blockSizeToProcess = Math.min(cipher.blockSize, input.length - i);
23
+ const block = new Uint8Array(cipher.blockSize);
24
+ for (let j = 0; j < blockSizeToProcess; j++)
25
+ block[j] = input[i + j];
26
+ if (blockSizeToProcess < cipher.blockSize)
27
+ block[blockSizeToProcess] = 0x80;
28
+ for (let j = 0; j < cipher.blockSize; j++)
29
+ B[j] ^= block[j];
30
+ B.set(gf2mMul(cipher.blockSize, B, H));
31
+ i += cipher.blockSize;
32
+ }
33
+ };
34
+ updateMac(aad);
35
+ updateMac(msg);
36
+ xorBytesInPlace(B, concatBytes(numberToBytesLE(aad.length * 8, hblock), numberToBytesLE(msg.length * 8, hblock)));
37
+ return cipher.encrypt(B).slice(0, q);
38
+ }
39
+ });
40
+ };