@li0ard/dstu 0.1.3 → 0.1.5

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,98 @@
1
+ import { bytesToNumberBE, concatBytes, equalBytes, numberToBytesBE } from "@noble/curves/utils.js";
2
+ import { kupyna256 } from "../kupyna/index.js";
3
+ import { curve256, curve512 } from "./curve.js";
4
+ import { kw } from "../modes/index.js";
5
+ import { Kalyna256, Kalyna512 } from "../kalyna/index.js";
6
+ import { randomBytes } from "@noble/hashes/utils.js";
7
+ const ID_KUPYNA256 = 1;
8
+ const dstu9041 = (length) => {
9
+ const MAX_LENGTH = length == 256 ? 200 : 424;
10
+ const PADDED_MESSAGE_BYTES = MAX_LENGTH / 8;
11
+ const HASH_LENGTH_BYTES = length == 256 ? 4 : 8;
12
+ const STRUCT_BYTES = 1 + HASH_LENGTH_BYTES + 2 + PADDED_MESSAGE_BYTES;
13
+ const curve = length == 256 ? curve256 : curve512;
14
+ const cipher = length == 256 ? Kalyna256 : Kalyna512;
15
+ const padMessage = (message, messageBits) => {
16
+ if (messageBits == 0)
17
+ throw new Error("Invalid message length");
18
+ if (messageBits > MAX_LENGTH)
19
+ throw new Error("Message too long");
20
+ const messageBytes = Math.ceil(messageBits / 8);
21
+ if (message.length != messageBytes)
22
+ throw new Error("Message length mismatch");
23
+ const paddedMessage = new Uint8Array(PADDED_MESSAGE_BYTES);
24
+ paddedMessage.set(message, PADDED_MESSAGE_BYTES - messageBytes);
25
+ return paddedMessage;
26
+ };
27
+ const buildStruct = (hashId, paddedMessage, messageLength) => {
28
+ const digest = kupyna256(concatBytes(messageLength, paddedMessage));
29
+ const struct = new Uint8Array(STRUCT_BYTES);
30
+ struct[0] = hashId;
31
+ struct.set(digest.subarray(digest.length - HASH_LENGTH_BYTES), 1);
32
+ struct.set(messageLength, 1 + HASH_LENGTH_BYTES);
33
+ struct.set(paddedMessage, 3 + HASH_LENGTH_BYTES);
34
+ return struct;
35
+ };
36
+ const parseStruct = (struct) => {
37
+ if (struct.length != STRUCT_BYTES)
38
+ throw new Error("Invalid ciphertext");
39
+ const hashId = struct[0];
40
+ if (hashId != ID_KUPYNA256)
41
+ throw new Error("Invalid hash ID");
42
+ const embeddedHash = struct.subarray(1, HASH_LENGTH_BYTES + 1);
43
+ const messageLength = struct.subarray(1 + HASH_LENGTH_BYTES, 3 + HASH_LENGTH_BYTES);
44
+ const paddedMessage = new Uint8Array(PADDED_MESSAGE_BYTES);
45
+ paddedMessage.set(struct.subarray(3 + HASH_LENGTH_BYTES));
46
+ const messageBits = Number(bytesToNumberBE(messageLength));
47
+ if (messageBits == 0)
48
+ throw new Error("Invalid message length");
49
+ if (messageBits > MAX_LENGTH)
50
+ throw new Error("Message too long");
51
+ const digest = kupyna256(concatBytes(messageLength, paddedMessage));
52
+ if (!equalBytes(embeddedHash, digest.subarray(digest.length - HASH_LENGTH_BYTES)))
53
+ throw new Error("Integrity check failed, hash mismatch");
54
+ const messageBytes = Math.ceil(messageBits / 8);
55
+ const paddingLen = PADDED_MESSAGE_BYTES - messageBytes;
56
+ let invalidPadding = 0;
57
+ for (let i = 0; i < paddedMessage.length; i++)
58
+ invalidPadding |= (i < paddingLen ? 1 : 0) & (paddedMessage[i] != 0 ? 1 : 0);
59
+ if (invalidPadding != 0)
60
+ throw new Error("Invalid padding");
61
+ return paddedMessage.subarray(paddingLen);
62
+ };
63
+ return Object.freeze({
64
+ getPublicKey: curve.getPublicKey,
65
+ keygen: curve.keygen,
66
+ encrypt: (message, publicKey, rand) => {
67
+ const message_bits = message.length * 8;
68
+ const paddedMessage = padMessage(message, message_bits);
69
+ const struct = buildStruct(ID_KUPYNA256, paddedMessage, numberToBytesBE(message_bits, 2));
70
+ const e = curve.Point.Fn.create(bytesToNumberBE(rand ?? randomBytes(STRUCT_BYTES)));
71
+ const R = curve.Point.BASE.multiply(e);
72
+ const r = curve.Point.Fp.toBytes(R.x);
73
+ const T = curve.Point.fromBytes(publicKey).multiply(e);
74
+ const key = curve.Point.Fp.toBytes(T.x);
75
+ const plaintext = new Uint8Array(2 * STRUCT_BYTES);
76
+ plaintext.set(struct);
77
+ const ciphertext = kw(new cipher(key)).wrap(plaintext);
78
+ return concatBytes(r, ciphertext);
79
+ },
80
+ decrypt: (ciphertext, privateKey) => {
81
+ if (ciphertext.length != length / 2)
82
+ throw new Error("Invalid ciphertext length");
83
+ const r = bytesToNumberBE(ciphertext.subarray(0, STRUCT_BYTES));
84
+ if (!curve.Point.Fp.isValidNot0(r))
85
+ throw new Error("Invalid ciphertext");
86
+ const R = curve.Point.fromX(r);
87
+ const T = R.multiply(bytesToNumberBE(privateKey));
88
+ const key = curve.Point.Fp.toBytes(T.x);
89
+ // KW already does unpadding
90
+ const plaintext = kw(new cipher(key)).unwrap(ciphertext.subarray(STRUCT_BYTES));
91
+ return parseStruct(plaintext);
92
+ }
93
+ });
94
+ };
95
+ /** DSTU 9041:2020 (256 bit) */
96
+ export const dstu9041_256 = dstu9041(256);
97
+ /** DSTU 9041:2020 (512 bit) */
98
+ export const dstu9041_512 = dstu9041(512);
package/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./dstu4145/index.js";
2
+ export * from "./dstu9041/index.js";
2
3
  export { Kalyna128, Kalyna128_256, Kalyna256, Kalyna256_512, Kalyna512 } from "./kalyna/index.js";
3
4
  export * from "./kupyna/index.js";
4
5
  export * from "./modes/index.js";
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./dstu4145/index.js";
2
+ export * from "./dstu9041/index.js";
2
3
  export { Kalyna128, Kalyna128_256, Kalyna256, Kalyna256_512, Kalyna512 } from "./kalyna/index.js";
3
4
  export * from "./kupyna/index.js";
4
5
  export * from "./modes/index.js";
package/kupyna/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { concatBytes, createHasher } from "@noble/hashes/utils.js";
2
- import { bytesToUint64sLE, numberToBytesLE, uint64sToBytesLE } from "../utils.js";
2
+ import { bytesToUint64sLE, uint64sToBytesLE } from "../utils.js";
3
3
  import { column } from "../kalyna/index.js";
4
+ import { numberToBytesLE } from "@noble/curves/utils.js";
4
5
  const r = 0x00f0f0f0f0f0f0f3n;
5
6
  class Kupyna {
6
7
  blockLen;
package/kupyna/kmac.js CHANGED
@@ -1,5 +1,6 @@
1
- import { numberToBytesLE, uint64sToBytesLE } from "../utils.js";
1
+ import { uint64sToBytesLE } from "../utils.js";
2
2
  import { Kupyna256, Kupyna384, Kupyna512 } from "./index.js";
3
+ import { numberToBytesLE } from "@noble/curves/utils.js";
3
4
  const dpad = numberToBytesLE(128, 128);
4
5
  const kpad32 = new Uint8Array([
5
6
  0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
package/modes/cbc.js CHANGED
@@ -3,16 +3,14 @@ import { xorBytes } from "../utils.js";
3
3
  export const cbc = (cipher, iv) => {
4
4
  if (iv.length !== cipher.blockSize)
5
5
  throw new Error("Invalid IV size");
6
- const encrypter = cipher.encrypt.bind(cipher);
7
- const decrypter = cipher.decrypt.bind(cipher);
8
- return {
6
+ return Object.freeze({
9
7
  encrypt: (plaintext) => {
10
8
  if (plaintext.length % cipher.blockSize !== 0)
11
9
  throw new Error("Plaintext not aligned");
12
10
  let buf = new Uint8Array(iv);
13
11
  const output = new Uint8Array(plaintext.length);
14
12
  for (let i = 0; i < plaintext.length; i += cipher.blockSize) {
15
- const blk = encrypter(xorBytes(plaintext.subarray(i, i + cipher.blockSize), buf));
13
+ const blk = cipher.encrypt(xorBytes(plaintext.subarray(i, i + cipher.blockSize), buf));
16
14
  output.set(blk, i);
17
15
  buf = blk.slice();
18
16
  }
@@ -25,10 +23,10 @@ export const cbc = (cipher, iv) => {
25
23
  const output = new Uint8Array(ciphertext.length);
26
24
  for (let i = 0; i < ciphertext.length; i += cipher.blockSize) {
27
25
  const blk = ciphertext.subarray(i, i + cipher.blockSize);
28
- output.set(xorBytes(decrypter(blk), buf), i);
26
+ output.set(xorBytes(cipher.decrypt(blk), buf), i);
29
27
  buf = blk.slice();
30
28
  }
31
29
  return output;
32
30
  }
33
- };
31
+ });
34
32
  };
package/modes/cfb.js CHANGED
@@ -1,14 +1,12 @@
1
1
  /** Cipher Feedback (CFB) mode */
2
2
  export const cfb = (cipher, iv, q = cipher.blockSize) => {
3
- const encrypter = cipher.encrypt.bind(cipher);
4
- return {
3
+ if (q !== 1 && q !== 8 && q !== 16 && q !== 32 && q !== 64)
4
+ throw new Error('q must be 1, 8, 16, 32, or 64');
5
+ if (q > cipher.blockSize)
6
+ throw new Error('q cannot exceed block size');
7
+ return Object.freeze({
5
8
  encrypt: (plaintext) => {
6
- const blockSize = cipher.blockSize;
7
- if (q !== 1 && q !== 8 && q !== 16 && q !== 32 && q !== 64)
8
- throw new Error('q must be 1, 8, 16, 32, or 64');
9
- if (q > blockSize)
10
- throw new Error('q cannot exceed block size');
11
- let gamma = encrypter(iv);
9
+ let gamma = cipher.encrypt(iv);
12
10
  const feed = new Uint8Array(iv);
13
11
  let offset = 0;
14
12
  const result = new Uint8Array(plaintext.length);
@@ -16,32 +14,27 @@ export const cfb = (cipher, iv, q = cipher.blockSize) => {
16
14
  while (offset > 0 && dataOff < plaintext.length) {
17
15
  result[dataOff] = plaintext[dataOff] ^ gamma[offset];
18
16
  feed[offset++] = result[dataOff++];
19
- if (offset >= blockSize) {
20
- gamma = encrypter(feed);
21
- offset = blockSize - q;
17
+ if (offset >= cipher.blockSize) {
18
+ gamma = cipher.encrypt(feed);
19
+ offset = cipher.blockSize - q;
22
20
  }
23
21
  }
24
22
  while (dataOff + q <= plaintext.length) {
25
23
  for (let i = 0; i < q; i++)
26
- result[dataOff + i] = plaintext[dataOff + i] ^ gamma[blockSize - q + i];
27
- feed.set(gamma.subarray(0, blockSize - q));
28
- feed.set(result.subarray(dataOff, dataOff + q), blockSize - q);
29
- gamma = encrypter(feed);
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);
30
28
  dataOff += q;
31
29
  }
32
30
  while (dataOff < plaintext.length) {
33
- result[dataOff] = plaintext[dataOff] ^ gamma[blockSize - (plaintext.length - dataOff)];
31
+ result[dataOff] = plaintext[dataOff] ^ gamma[cipher.blockSize - (plaintext.length - dataOff)];
34
32
  dataOff++;
35
33
  }
36
34
  return result;
37
35
  },
38
36
  decrypt: (ciphertext) => {
39
- const blockSize = cipher.blockSize;
40
- if (q !== 1 && q !== 8 && q !== 16 && q !== 32 && q !== 64)
41
- throw new Error('q must be 1, 8, 16, 32, or 64');
42
- if (q > blockSize)
43
- throw new Error('q cannot exceed block size');
44
- let gamma = encrypter(iv);
37
+ let gamma = cipher.encrypt(iv);
45
38
  const feed = new Uint8Array(iv);
46
39
  let offset = 0;
47
40
  const result = new Uint8Array(ciphertext.length);
@@ -49,24 +42,24 @@ export const cfb = (cipher, iv, q = cipher.blockSize) => {
49
42
  while (offset > 0 && dataOff < ciphertext.length) {
50
43
  result[dataOff] = ciphertext[dataOff] ^ gamma[offset];
51
44
  feed[offset++] = ciphertext[dataOff++];
52
- if (offset >= blockSize) {
53
- gamma = encrypter(feed);
54
- offset = blockSize - q;
45
+ if (offset >= cipher.blockSize) {
46
+ gamma = cipher.encrypt(feed);
47
+ offset = cipher.blockSize - q;
55
48
  }
56
49
  }
57
50
  while (dataOff + q <= ciphertext.length) {
58
51
  for (let i = 0; i < q; i++)
59
- result[dataOff + i] = ciphertext[dataOff + i] ^ gamma[blockSize - q + i];
60
- feed.set(gamma.subarray(0, blockSize - q));
61
- feed.set(ciphertext.subarray(dataOff, dataOff + q), blockSize - q);
62
- gamma = encrypter(feed);
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);
63
56
  dataOff += q;
64
57
  }
65
58
  while (dataOff < ciphertext.length) {
66
- result[dataOff] = ciphertext[dataOff] ^ gamma[blockSize - (ciphertext.length - dataOff)];
59
+ result[dataOff] = ciphertext[dataOff] ^ gamma[cipher.blockSize - (ciphertext.length - dataOff)];
67
60
  dataOff++;
68
61
  }
69
62
  return result;
70
63
  }
71
- };
64
+ });
72
65
  };
package/modes/ctr.js CHANGED
@@ -10,7 +10,7 @@ const incrementCounterAt = (ctr, pos) => {
10
10
  export const ctr = (cipher, iv) => {
11
11
  if (iv.length !== cipher.blockSize)
12
12
  throw new Error("Invalid IV size");
13
- return {
13
+ return Object.freeze({
14
14
  crypt: (msg) => {
15
15
  const keystreamBlocks = [];
16
16
  const ctr = cipher.encrypt(iv);
@@ -20,5 +20,5 @@ export const ctr = (cipher, iv) => {
20
20
  }
21
21
  return xorBytes(concatBytes(...keystreamBlocks), msg);
22
22
  }
23
- };
23
+ });
24
24
  };
package/modes/ecb.js CHANGED
@@ -10,8 +10,8 @@ export const ecb = (cipher) => {
10
10
  output.set(crypter(data.subarray(i, i + cipher.blockSize)), i);
11
11
  return output;
12
12
  };
13
- return {
13
+ return Object.freeze({
14
14
  encrypt: (plaintext) => core(encrypter, plaintext),
15
15
  decrypt: (ciphertext) => core(decrypter, ciphertext),
16
- };
16
+ });
17
17
  };
package/modes/gcm.js CHANGED
@@ -1,20 +1,18 @@
1
1
  import { concatBytes } from "@noble/hashes/utils.js";
2
2
  import { ctr } from "./ctr.js";
3
3
  import { gmac } from "./mac.js";
4
- import { equalBytes } from "../utils.js";
4
+ import { equalBytes } from "@noble/curves/utils.js";
5
5
  /** Galois counter (GCM) mode (AEAD) */
6
- export const gcm = (cipher, iv, q = 16) => {
7
- return {
8
- seal: (plaintext, aad) => {
9
- const enc = ctr(cipher, iv).crypt(plaintext);
10
- return concatBytes(enc, gmac(cipher, q).compute(aad || new Uint8Array(), enc));
11
- },
12
- open: (ciphertext, aad) => {
13
- const enc = ciphertext.subarray(0, -q);
14
- const hC = gmac(cipher, q).compute(aad || new Uint8Array(), enc);
15
- if (!equalBytes(ciphertext.subarray(-q), hC))
16
- throw new Error("Invalid MAC");
17
- return ctr(cipher, iv).crypt(enc);
18
- }
19
- };
20
- };
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
+ });
package/modes/index.d.ts CHANGED
@@ -3,5 +3,6 @@ export * from "./cfb.js";
3
3
  export * from "./ctr.js";
4
4
  export * from "./ecb.js";
5
5
  export * from "./gcm.js";
6
+ export * from "./kw.js";
6
7
  export * from "./mac.js";
7
8
  export * from "./ofb.js";
package/modes/index.js CHANGED
@@ -3,5 +3,6 @@ export * from "./cfb.js";
3
3
  export * from "./ctr.js";
4
4
  export * from "./ecb.js";
5
5
  export * from "./gcm.js";
6
+ export * from "./kw.js";
6
7
  export * from "./mac.js";
7
8
  export * from "./ofb.js";
package/modes/kw.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import type { TArg, TRet } from "@noble/hashes/utils.js";
2
+ import type { Kalyna } from "../kalyna/index.js";
3
+ export declare const kw: (cipher: Kalyna) => Readonly<{
4
+ wrap: (key: TArg<Uint8Array>) => TRet<Uint8Array>;
5
+ unwrap: (wrappedKey: TArg<Uint8Array>) => TRet<Uint8Array>;
6
+ }>;
package/modes/kw.js ADDED
@@ -0,0 +1,74 @@
1
+ import { pad, unpad } from "../padding.js";
2
+ export const kw = (cipher) => {
3
+ const block_size_kw_byte = cipher.blockSize >> 1;
4
+ return Object.freeze({
5
+ wrap: (key) => {
6
+ let plain_data_size_byte = key.length;
7
+ const cipher_data = new Uint8Array(plain_data_size_byte + (cipher.blockSize << 2));
8
+ cipher_data.set(key);
9
+ let i = 0;
10
+ if (plain_data_size_byte % cipher.blockSize !== 0) {
11
+ let bitLength = plain_data_size_byte << 3;
12
+ while (bitLength > 0) {
13
+ cipher_data[plain_data_size_byte + i] = bitLength & 0xFF;
14
+ i++;
15
+ bitLength >>>= 8;
16
+ }
17
+ plain_data_size_byte += block_size_kw_byte;
18
+ const paddedData = pad(cipher_data.subarray(0, plain_data_size_byte), cipher.blockSize);
19
+ cipher_data.set(paddedData, 0);
20
+ plain_data_size_byte = paddedData.length;
21
+ }
22
+ const r = Math.floor(plain_data_size_byte / cipher.blockSize), n = 2 * (r + 1), v = (n - 1) * 6;
23
+ plain_data_size_byte += cipher.blockSize;
24
+ const b_el_count = (n - 1) * block_size_kw_byte, b_last_el = (n - 2) * block_size_kw_byte;
25
+ const b = new Uint8Array(n * block_size_kw_byte), shift = new Uint8Array(n * block_size_kw_byte), B = new Uint8Array(block_size_kw_byte), swap = new Uint8Array(cipher.blockSize);
26
+ B.set(cipher_data.subarray(0, block_size_kw_byte));
27
+ b.set(cipher_data.subarray(block_size_kw_byte, block_size_kw_byte + b_el_count));
28
+ for (i = 1; i <= v; i++) {
29
+ swap.set(B, 0);
30
+ swap.set(b.subarray(0, block_size_kw_byte), block_size_kw_byte);
31
+ swap.set(cipher.encrypt(swap));
32
+ swap[block_size_kw_byte] ^= i;
33
+ B.set(swap.subarray(block_size_kw_byte, cipher.blockSize));
34
+ shift.set(b.subarray(block_size_kw_byte, b_el_count));
35
+ b.set(shift.subarray(0, b_el_count - block_size_kw_byte), 0);
36
+ b.set(swap.subarray(0, block_size_kw_byte), b_last_el);
37
+ }
38
+ cipher_data.set(B, 0);
39
+ cipher_data.set(b.subarray(0, b_el_count), block_size_kw_byte);
40
+ return cipher_data.subarray(0, b_el_count + block_size_kw_byte);
41
+ },
42
+ unwrap: (wrappedKey) => {
43
+ const cipher_data = new Uint8Array(wrappedKey);
44
+ if (cipher_data.length < 2 * cipher.blockSize)
45
+ throw new Error("Invalid input length: must be at least 2 blocks");
46
+ const r = Math.floor(cipher_data.length / cipher.blockSize) - 1, n = 2 * (r + 1), v = (n - 1) * 6;
47
+ if (r < 0 || n <= 0 || v < 0)
48
+ throw new Error("Invalid input length for decryption");
49
+ const B = new Uint8Array(block_size_kw_byte);
50
+ B.set(cipher_data.subarray(0, block_size_kw_byte));
51
+ const b_el_count = (n - 1) * block_size_kw_byte;
52
+ const b = new Uint8Array(cipher_data.length);
53
+ b.set(cipher_data.subarray(block_size_kw_byte, block_size_kw_byte + b_el_count));
54
+ const b_last_el = (n - 2) * block_size_kw_byte;
55
+ const shift = new Uint8Array(cipher_data.length), swap = new Uint8Array(cipher.blockSize);
56
+ for (let i = v; i >= 1; i--) {
57
+ swap.set(b.subarray(b_last_el, b_last_el + block_size_kw_byte), 0);
58
+ B[0] ^= i;
59
+ swap.set(B, block_size_kw_byte);
60
+ swap.set(cipher.decrypt(swap));
61
+ B.set(swap.subarray(0, block_size_kw_byte));
62
+ shift.set(b.subarray(0, b_el_count - block_size_kw_byte));
63
+ b.set(shift.subarray(0, b_el_count - block_size_kw_byte), block_size_kw_byte);
64
+ b.set(swap.subarray(block_size_kw_byte, cipher.blockSize), 0);
65
+ }
66
+ cipher_data.set(B, 0);
67
+ cipher_data.set(b.subarray(0, b_el_count), block_size_kw_byte);
68
+ let current_length = unpad(cipher_data, cipher.blockSize).length;
69
+ if (current_length % cipher.blockSize !== 0)
70
+ current_length -= block_size_kw_byte + 1;
71
+ return cipher_data.slice(0, current_length);
72
+ }
73
+ });
74
+ };
package/modes/mac.js CHANGED
@@ -1,65 +1,61 @@
1
1
  import { concatBytes, copyBytes } from "@noble/hashes/utils.js";
2
2
  import { pad } from "../padding.js";
3
- import { gf2mMul, numberToBytesLE, xorBytes } from "../utils.js";
3
+ import { gf2mMul, xorBytes } from "../utils.js";
4
+ import { numberToBytesLE } from "@noble/curves/utils.js";
4
5
  /** Message Authentication Code (MAC) mode */
5
- export const cmac = (cipher, q = 16) => {
6
- return {
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);
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;
24
13
  }
25
- };
26
- };
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
+ });
27
26
  /** Galois Message Authentication Code (GMAC) mode */
28
- export const gmac = (cipher, q = 16) => {
29
- return {
30
- compute: (aad, msg) => {
31
- const blockSize = cipher.blockSize;
32
- const H = cipher.encrypt(new Uint8Array(blockSize));
33
- let B = new Uint8Array(blockSize);
34
- let i = 0;
35
- while (i < aad.length) {
36
- const blockSizeToProcess = Math.min(blockSize, aad.length - i);
37
- const block = new Uint8Array(blockSize);
38
- for (let j = 0; j < blockSizeToProcess; j++)
39
- block[j] = aad[i + j];
40
- if (blockSizeToProcess < blockSize)
41
- block[blockSizeToProcess] = 0x80;
42
- for (let j = 0; j < blockSize; j++)
43
- B[j] ^= block[j];
44
- B = gf2mMul(blockSize, B, H);
45
- i += blockSize;
46
- }
47
- i = 0;
48
- while (i < msg.length) {
49
- const blockSizeToProcess = Math.min(blockSize, msg.length - i);
50
- const block = new Uint8Array(blockSize);
51
- for (let j = 0; j < blockSizeToProcess; j++)
52
- block[j] = msg[i + j];
53
- if (blockSizeToProcess < blockSize)
54
- block[blockSizeToProcess] = 0x80;
55
- for (let j = 0; j < blockSize; j++)
56
- B[j] ^= block[j];
57
- B = gf2mMul(blockSize, B, H);
58
- i += blockSize;
59
- }
60
- B = xorBytes(B, concatBytes(numberToBytesLE(aad.length * 8, blockSize / 2), numberToBytesLE(msg.length * 8, blockSize / 2)));
61
- // B = gf2mMul(blockSize, B, H);
62
- return cipher.encrypt(B).slice(0, q);
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;
63
57
  }
64
- };
65
- };
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
+ });
package/modes/ofb.js CHANGED
@@ -4,17 +4,16 @@ import { xorBytes } from "../utils.js";
4
4
  export const ofb = (cipher, iv) => {
5
5
  if (iv.length !== cipher.blockSize)
6
6
  throw new Error("Invalid IV size");
7
- const encrypter = cipher.encrypt.bind(cipher);
8
- return {
7
+ return Object.freeze({
9
8
  crypt: (msg) => {
10
9
  let buf = new Uint8Array(iv);
11
10
  const output = new Uint8Array(msg.length);
12
11
  for (let i = 0; i < msg.length; i += cipher.blockSize) {
13
- const enc = encrypter(buf);
12
+ const enc = cipher.encrypt(buf);
14
13
  output.set(xorBytes(enc, msg.subarray(i, i + cipher.blockSize)), i);
15
14
  buf = enc;
16
15
  }
17
16
  return output;
18
17
  }
19
- };
18
+ });
20
19
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@li0ard/dstu",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "type": "module",
@@ -41,7 +41,8 @@
41
41
  "typescript": "^5"
42
42
  },
43
43
  "dependencies": {
44
- "@noble/hashes": "^2.2.0",
44
+ "@noble/curves": "^2.3.0",
45
+ "@noble/hashes": "^2.3.0",
45
46
  "bn.js": "^5.2.3"
46
47
  },
47
48
  "files": ["**/*.js", "**/*.d.ts"],
package/utils.d.ts CHANGED
@@ -1,14 +1,8 @@
1
- import { type TArg, type TRet } from "@noble/hashes/utils.js";
1
+ import { type TArg, type TRet } from "@noble/curves/utils.js";
2
2
  export declare const xorBytes: (a: TArg<Uint8Array>, b: TArg<Uint8Array>) => TRet<Uint8Array>;
3
3
  export declare const bytesToUint64sBE: (b: TArg<Uint8Array>) => TRet<BigUint64Array>;
4
4
  export declare const uint64sToBytesBE: (w: TArg<BigUint64Array>) => TRet<Uint8Array>;
5
5
  export declare const bytesToUint64sLE: (b: TArg<Uint8Array>) => TRet<BigUint64Array>;
6
6
  export declare const uint64sToBytesLE: (w: TArg<BigUint64Array>) => TRet<Uint8Array>;
7
- export declare const hexToNumber: (hex: string) => bigint;
8
- export declare const bytesToNumberBE: (bytes: TArg<Uint8Array>) => bigint;
9
- export declare const numberToBytesBE: (n: number | bigint, len: number) => TRet<Uint8Array>;
10
- export declare const bytesToNumberLE: (bytes: TArg<Uint8Array>) => bigint;
11
- export declare const numberToBytesLE: (n: number | bigint, len: number) => TRet<Uint8Array>;
12
7
  export declare const byte: (a: bigint) => number;
13
8
  export declare const gf2mMul: (blockSize: number, a: TArg<Uint8Array>, b: TArg<Uint8Array>) => TRet<Uint8Array>;
14
- export declare const equalBytes: (a: TArg<Uint8Array>, b: TArg<Uint8Array>) => boolean;
package/utils.js CHANGED
@@ -1,4 +1,4 @@
1
- import { bytesToHex, copyBytes, hexToBytes } from "@noble/hashes/utils.js";
1
+ import { bytesToNumberBE, bytesToNumberLE, numberToBytesBE, numberToBytesLE } from "@noble/curves/utils.js";
2
2
  export const xorBytes = (a, b) => {
3
3
  const mlen = Math.min(a.length, b.length);
4
4
  const result = new Uint8Array(mlen);
@@ -32,20 +32,6 @@ export const uint64sToBytesLE = (w) => {
32
32
  result.set(numberToBytesLE(w[i], 8), i * 8);
33
33
  return result;
34
34
  };
35
- export const hexToNumber = (hex) => {
36
- if (typeof hex !== 'string')
37
- throw new Error('hex string expected, got ' + typeof hex);
38
- return hex === '' ? 0n : BigInt('0x' + hex);
39
- };
40
- export const bytesToNumberBE = (bytes) => hexToNumber(bytesToHex(bytes));
41
- export const numberToBytesBE = (n, len) => {
42
- let num = n.toString(16).padStart(len * 2, '0');
43
- while (num.length % 2 != 0)
44
- num = "0" + num;
45
- return hexToBytes(num);
46
- };
47
- export const bytesToNumberLE = (bytes) => hexToNumber(bytesToHex(copyBytes(bytes).reverse()));
48
- export const numberToBytesLE = (n, len) => numberToBytesBE(n, len).reverse();
49
35
  export const byte = (a) => Number(a & 0xffn);
50
36
  export const gf2mMul = (blockSize, a, b) => {
51
37
  const temp = new Uint8Array(a);
@@ -71,11 +57,3 @@ export const gf2mMul = (blockSize, a, b) => {
71
57
  }
72
58
  return result;
73
59
  };
74
- export const equalBytes = (a, b) => {
75
- if (a.length !== b.length)
76
- return false;
77
- let diff = 0;
78
- for (let i = 0; i < a.length; i++)
79
- diff |= a[i] ^ b[i];
80
- return diff === 0;
81
- };