@li0ard/dstu 0.1.7 → 0.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dstu4145/index.js CHANGED
@@ -2,6 +2,7 @@ import { concatBytes, randomBytes } from "@noble/hashes/utils.js";
2
2
  import { DSTU_163, DSTU_167, DSTU_173, DSTU_179, DSTU_191, DSTU_233, DSTU_257, DSTU_307, DSTU_367, DSTU_431 } from "./const.js";
3
3
  import { binaryWeierstrass } from "./ec/index.js";
4
4
  import BN from "bn.js";
5
+ import { reverseBytes } from "../utils.js";
5
6
  /** Create DSTU 4145-2002 signer (Big-Endian) */
6
7
  export const dstu4145 = (parameters) => {
7
8
  const curve = binaryWeierstrass(parameters);
@@ -91,6 +92,33 @@ export const dstu4145 = (parameters) => {
91
92
  lengths
92
93
  });
93
94
  };
95
+ /**
96
+ * Create DSTU 4145-2002 signer (Little-Endian)
97
+ *
98
+ * **NOTE:** Parameters MUST be in Big-Endian
99
+ */
100
+ export const dstu4145_le = (parameters) => {
101
+ const signer = dstu4145(parameters);
102
+ const getPublicKey = (secretKey, isCompressed = false) => reverseBytes(signer.getPublicKey(reverseBytes(secretKey), isCompressed));
103
+ const sign = (secretKey, digest, rand) => reverseBytes(signer.sign(reverseBytes(secretKey), reverseBytes(digest), rand ? reverseBytes(rand) : undefined));
104
+ const verify = (publicKey, digest, signature) => signer.verify(reverseBytes(publicKey), reverseBytes(digest), reverseBytes(signature));
105
+ const getSharedSecret = (secretKeyA, publicKeyB, withCofactor = true) => reverseBytes(signer.getSharedSecret(reverseBytes(secretKeyA), reverseBytes(publicKeyB), withCofactor));
106
+ const keygen = (isCompressed = false) => {
107
+ const { secretKey, publicKey } = signer.keygen(isCompressed);
108
+ return {
109
+ secretKey: reverseBytes(secretKey),
110
+ publicKey: reverseBytes(publicKey)
111
+ };
112
+ };
113
+ return Object.freeze({
114
+ getPublicKey,
115
+ getSharedSecret,
116
+ sign,
117
+ verify,
118
+ keygen,
119
+ lengths: signer.lengths
120
+ });
121
+ };
94
122
  export * from "./const.js";
95
123
  export * from "./ec/expand.js";
96
124
  // ONB curves don't preinitialized because `multiplyOnb` is slow O(m^3) function
@@ -109,9 +137,13 @@ export const dstu191 = dstu4145(DSTU_191);
109
137
  export const dstu233 = dstu4145(DSTU_233);
110
138
  /** DSTU 4145-2002 257 bit curve */
111
139
  export const dstu257 = dstu4145(DSTU_257);
140
+ /** DSTU 4145-2002 257 bit curve (LE) */
141
+ export const dstu257_le = dstu4145_le(DSTU_257);
112
142
  /** DSTU 4145-2002 307 bit curve */
113
143
  export const dstu307 = dstu4145(DSTU_307);
114
144
  /** DSTU 4145-2002 367 bit curve */
115
145
  export const dstu367 = dstu4145(DSTU_367);
116
146
  /** DSTU 4145-2002 431 bit curve */
117
147
  export const dstu431 = dstu4145(DSTU_431);
148
+ /** DSTU 4145-2002 431 bit curve (LE) */
149
+ export const dstu431_le = dstu4145_le(DSTU_431);
package/dstu9041/curve.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import { Field, mod } from "@noble/curves/abstract/modular.js";
2
- import { bitGet, bitLen, bytesToNumberBE, randomBytes } from "@noble/curves/utils.js";
3
- import { concatBytes } from "@noble/hashes/utils.js";
2
+ import { concatBytes, bitGet, bitLen, bytesToNumberBE, randomBytes } from "@noble/curves/utils.js";
4
3
  export const dstu9041Curve = (parameters) => {
5
4
  if (parameters.a != 2n)
6
5
  throw new Error("Unsuppored `a` parameter, must be 2");
package/dstu9041/index.js CHANGED
@@ -1,9 +1,8 @@
1
- import { bytesToNumberBE, concatBytes, equalBytes, numberToBytesBE } from "@noble/curves/utils.js";
1
+ import { randomBytes, bytesToNumberBE, concatBytes, equalBytes, numberToBytesBE } from "@noble/curves/utils.js";
2
2
  import { kupyna256 } from "../kupyna/index.js";
3
3
  import { curve256, curve512, dstu9041Curve } from "./curve.js";
4
4
  import { kw } from "../modes/index.js";
5
5
  import { Kalyna256, Kalyna512 } from "../kalyna/index.js";
6
- import { randomBytes } from "@noble/hashes/utils.js";
7
6
  const ID_KUPYNA256 = 1;
8
7
  const dstu9041 = (length) => {
9
8
  let MAX_LENGTH, HASH_LENGTH_BYTES, curve, Сipher;
@@ -1,12 +1,13 @@
1
1
  import { type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
2
- /** GOST R 34.311-95 hash function */
2
+ import { _HMAC } from "@noble/hashes/hmac.js";
3
+ /** GOST 34.311-95 hash function */
3
4
  export declare class Gost3431195 implements Hash<Gost3431195> {
4
5
  private sbox;
5
6
  readonly blockLen = 32;
6
7
  readonly outputLen = 32;
7
8
  readonly canXOF = false;
8
9
  private buffer;
9
- /** GOST R 34.311-95 hash function */
10
+ /** GOST 34.311-95 hash function */
10
11
  constructor(sbox?: TArg<Uint8Array>);
11
12
  /** Create hash instance */
12
13
  static create(): Gost3431195;
@@ -17,7 +18,7 @@ export declare class Gost3431195 implements Hash<Gost3431195> {
17
18
  digestInto(buf: TArg<Uint8Array>): void;
18
19
  digest(): TRet<Uint8Array>;
19
20
  }
20
- /** GOST R 34.311-95 hash function */
21
+ /** GOST 34.311-95 hash function */
21
22
  export declare const gost3431195: {
22
23
  outputLen: number;
23
24
  blockLen: number;
@@ -32,3 +33,9 @@ export declare const gost3431195: {
32
33
  oid?: TRet<Uint8Array> | undefined;
33
34
  create: () => Gost3431195;
34
35
  };
36
+ /** GOST 34.311-95 HMAC */
37
+ export declare class Gost3431195HMAC extends _HMAC<Gost3431195> {
38
+ constructor(key: TArg<Uint8Array>);
39
+ }
40
+ /** GOST 34.311-95 HMAC */
41
+ export declare const gost3431195Hmac: (key: TArg<Uint8Array>, msg: TArg<Uint8Array>) => TRet<Uint8Array>;
package/dstu9311/hash.js CHANGED
@@ -2,6 +2,7 @@ import { concatBytes, copyBytes, createHasher } from "@noble/hashes/utils.js";
2
2
  import { Dstu9311, DKE_1 } from "../dstu9311/index.js";
3
3
  import { bytesToNumberBE, numberToBytesBE } from "@noble/curves/utils.js";
4
4
  import { xorBytes } from "../utils.js";
5
+ import { _HMAC } from "@noble/hashes/hmac.js";
5
6
  const r = (1n << 256n) - 1n;
6
7
  const C3 = new Uint8Array([
7
8
  0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0xff,
@@ -38,14 +39,14 @@ const _step = (hin, m, sbox) => {
38
39
  x.set(chi(x));
39
40
  return x;
40
41
  };
41
- /** GOST R 34.311-95 hash function */
42
+ /** GOST 34.311-95 hash function */
42
43
  export class Gost3431195 {
43
44
  sbox;
44
45
  blockLen = 32;
45
46
  outputLen = 32;
46
47
  canXOF = false;
47
48
  buffer;
48
- /** GOST R 34.311-95 hash function */
49
+ /** GOST 34.311-95 hash function */
49
50
  constructor(sbox = DKE_1) {
50
51
  this.sbox = sbox;
51
52
  this.buffer = new Uint8Array();
@@ -87,5 +88,11 @@ export class Gost3431195 {
87
88
  return buffer;
88
89
  }
89
90
  }
90
- /** GOST R 34.311-95 hash function */
91
+ /** GOST 34.311-95 hash function */
91
92
  export const gost3431195 = createHasher(Gost3431195.create);
93
+ /** GOST 34.311-95 HMAC */
94
+ export class Gost3431195HMAC extends _HMAC {
95
+ constructor(key) { super(gost3431195, key); }
96
+ }
97
+ /** GOST 34.311-95 HMAC */
98
+ export const gost3431195Hmac = (key, msg) => new Gost3431195HMAC(key).update(msg).digest();
package/kalyna/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { TArg, TRet } from "@noble/hashes/utils.js";
2
2
  import type { Cipher } from "../types.js";
3
- export declare const column: (x: TArg<BigUint64Array>, i: number, N: number, c1: number, c2: number, c3: number, c4: number, c5: number, c6: number, c7: number) => bigint;
3
+ export declare const column: (x: TArg<BigUint64Array>, i: number, N: number, c1: number, c2: number, c3: number, c4: number, c5: number, c6: number, c7: number, table?: Readonly<TArg<BigUint64Array>[]>, sign?: 1 | -1) => bigint;
4
4
  export declare abstract class Kalyna implements Cipher {
5
5
  readonly N: number;
6
6
  /** Block size */
@@ -19,6 +19,7 @@ export declare abstract class Kalyna implements Cipher {
19
19
  private addkey;
20
20
  private subkey;
21
21
  private column;
22
+ private invColumn;
22
23
  private G;
23
24
  private GL;
24
25
  private IMC;
package/kalyna/index.js CHANGED
@@ -8,14 +8,14 @@ const swap_block = (k, N) => {
8
8
  k[i] = k[i + 1];
9
9
  k[N - 1] = t;
10
10
  };
11
- export const column = (x, i, N, c1, c2, c3, c4, c5, c6, c7) => (T[0][byte(x[(i + N) % N])] ^ // c0 - always 0 for both Kalyna and Kupyna
12
- T[1][byte(x[(i - c1 + N) % N] >> 8n)] ^
13
- T[2][byte(x[(i - c2 + N) % N] >> 16n)] ^
14
- T[3][byte(x[(i - c3 + N) % N] >> 24n)] ^
15
- T[4][byte(x[(i - c4 + N) % N] >> 32n)] ^
16
- T[5][byte(x[(i - c5 + N) % N] >> 40n)] ^
17
- T[6][byte(x[(i - c6 + N) % N] >> 48n)] ^
18
- T[7][byte(x[(i - c7 + N) % N] >> 56n)]);
11
+ export const column = (x, i, N, c1, c2, c3, c4, c5, c6, c7, table = T, sign = -1) => (table[0][byte(x[(i + N) % N])] ^
12
+ table[1][byte(x[(i + sign * c1 + N) % N] >> 8n)] ^
13
+ table[2][byte(x[(i + sign * c2 + N) % N] >> 16n)] ^
14
+ table[3][byte(x[(i + sign * c3 + N) % N] >> 24n)] ^
15
+ table[4][byte(x[(i + sign * c4 + N) % N] >> 32n)] ^
16
+ table[5][byte(x[(i + sign * c5 + N) % N] >> 40n)] ^
17
+ table[6][byte(x[(i + sign * c6 + N) % N] >> 48n)] ^
18
+ table[7][byte(x[(i + sign * c7 + N) % N] >> 56n)]);
19
19
  export class Kalyna {
20
20
  N;
21
21
  /** Block size */
@@ -110,6 +110,9 @@ export class Kalyna {
110
110
  column(x, i) {
111
111
  return column(x, i, this.N, this.wordOffsets[1], this.wordOffsets[2], this.wordOffsets[3], this.wordOffsets[4], this.wordOffsets[5], this.wordOffsets[6], this.wordOffsets[7]);
112
112
  }
113
+ invColumn(x, i) {
114
+ return column(x, i, this.N, this.wordOffsets[1], this.wordOffsets[2], this.wordOffsets[3], this.wordOffsets[4], this.wordOffsets[5], this.wordOffsets[6], this.wordOffsets[7], IT, 1);
115
+ }
113
116
  G(x, y, k) {
114
117
  for (let i = 0; i < this.N; i++)
115
118
  y[i] = k[i] ^ this.column(x, i);
@@ -131,27 +134,19 @@ export class Kalyna {
131
134
  }
132
135
  IG(x, y, k) {
133
136
  for (let i = 0; i < this.N; i++)
134
- y[i] = k[i] ^
135
- IT[0][byte(x[(i + this.wordOffsets[0]) % this.N])] ^
136
- IT[1][byte(x[(i + this.wordOffsets[1]) % this.N] >> 8n)] ^
137
- IT[2][byte(x[(i + this.wordOffsets[2]) % this.N] >> 16n)] ^
138
- IT[3][byte(x[(i + this.wordOffsets[3]) % this.N] >> 24n)] ^
139
- IT[4][byte(x[(i + this.wordOffsets[4]) % this.N] >> 32n)] ^
140
- IT[5][byte(x[(i + this.wordOffsets[5]) % this.N] >> 40n)] ^
141
- IT[6][byte(x[(i + this.wordOffsets[6]) % this.N] >> 48n)] ^
142
- IT[7][byte(x[(i + this.wordOffsets[7]) % this.N] >> 56n)];
137
+ y[i] = k[i] ^ this.invColumn(x, i);
143
138
  }
144
- IGL(x, y, k) {
139
+ IGL(x, y) {
145
140
  for (let i = 0; i < this.N; i++) {
146
- let result = BigInt(IS[0][byte(x[(i + this.wordOffsets[0]) % this.N])]);
147
- result ^= BigInt(IS[1][byte(x[(i + this.wordOffsets[1]) % this.N] >> 8n)]) << 8n;
148
- result ^= BigInt(IS[2][byte(x[(i + this.wordOffsets[2]) % this.N] >> 16n)]) << 16n;
149
- result ^= BigInt(IS[3][byte(x[(i + this.wordOffsets[3]) % this.N] >> 24n)]) << 24n;
150
- result ^= BigInt(IS[0][byte(x[(i + this.wordOffsets[4]) % this.N] >> 32n)]) << 32n;
151
- result ^= BigInt(IS[1][byte(x[(i + this.wordOffsets[5]) % this.N] >> 40n)]) << 40n;
152
- result ^= BigInt(IS[2][byte(x[(i + this.wordOffsets[6]) % this.N] >> 48n)]) << 48n;
153
- result ^= BigInt(IS[3][byte(x[(i + this.wordOffsets[7]) % this.N] >> 56n)]) << 56n;
154
- y[i] = result - k[i];
141
+ const result = BigInt(IS[0][byte(x[i % this.N])]) ^
142
+ (BigInt(IS[1][byte(x[(i + this.wordOffsets[1]) % this.N] >> 8n)]) << 8n) ^
143
+ (BigInt(IS[2][byte(x[(i + this.wordOffsets[2]) % this.N] >> 16n)]) << 16n) ^
144
+ (BigInt(IS[3][byte(x[(i + this.wordOffsets[3]) % this.N] >> 24n)]) << 24n) ^
145
+ (BigInt(IS[0][byte(x[(i + this.wordOffsets[4]) % this.N] >> 32n)]) << 32n) ^
146
+ (BigInt(IS[1][byte(x[(i + this.wordOffsets[5]) % this.N] >> 40n)]) << 40n) ^
147
+ (BigInt(IS[2][byte(x[(i + this.wordOffsets[6]) % this.N] >> 48n)]) << 48n) ^
148
+ (BigInt(IS[3][byte(x[(i + this.wordOffsets[7]) % this.N] >> 56n)]) << 56n);
149
+ y[i] = result - this.drk[i];
155
150
  }
156
151
  }
157
152
  /** Encrypt block */
@@ -184,7 +179,7 @@ export class Kalyna {
184
179
  else
185
180
  this.IG(t2, t1, roundKey);
186
181
  }
187
- this.IGL(t2, t1, this.drk);
182
+ this.IGL(t2, t1);
188
183
  return uint64sToBytesLE(t1);
189
184
  }
190
185
  }
package/keywrap.d.ts CHANGED
@@ -1,6 +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>) => {
1
+ import { type TArg, type TRet } from "@noble/curves/utils.js";
2
+ /** Key wrap (`Dstu7624Wrap`/`GOST28147Wrap`) */
3
+ export declare const keyWrap: (kek: TArg<Uint8Array>, useDstu9311?: boolean) => {
4
4
  wrap: (key: TArg<Uint8Array>, iv?: TArg<Uint8Array>) => TRet<Uint8Array>;
5
5
  unwrap: (wrappedKey: TArg<Uint8Array>) => TRet<Uint8Array>;
6
6
  };
package/keywrap.js CHANGED
@@ -1,31 +1,36 @@
1
- import { concatBytes, randomBytes } from "@noble/hashes/utils.js";
1
+ import { equalBytes, concatBytes, randomBytes } from "@noble/curves/utils.js";
2
2
  import { Kalyna256 } from "./kalyna/index.js";
3
3
  import { cfb, cmac } from "./modes/index.js";
4
- import { equalBytes } from "@noble/curves/utils.js";
5
- const IV1 = new Uint8Array([
4
+ import { Dstu9311 } from "./dstu9311/index.js";
5
+ const IV1_KALYNA = new Uint8Array([
6
6
  0x69, 0x73, 0x27, 0x1D, 0x6E, 0x61, 0x1D, 0x06, 0x61, 0x67, 0x15, 0x04, 0x6C, 0x65, 0x50, 0x4C,
7
7
  0x20, 0x20, 0x00, 0x4F, 0x6D, 0x68, 0x01, 0x1F, 0x65, 0x61, 0x0C, 0x0C, 0x73, 0x73, 0x47, 0x14
8
8
  ]);
9
- /** Key wrap with Kalyna (`Dstu7624Wrap`) */
10
- export const keyWrap = (kek) => {
9
+ const IV1_9311 = new Uint8Array([
10
+ 0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05
11
+ ]);
12
+ /** Key wrap (`Dstu7624Wrap`/`GOST28147Wrap`) */
13
+ export const keyWrap = (kek, useDstu9311 = false) => {
11
14
  if (kek.length != 32)
12
15
  throw new Error("Invalid key length");
13
- const cipher = new Kalyna256(kek);
16
+ const cipher = new (useDstu9311 ? Dstu9311 : Kalyna256)(kek);
17
+ const ivLength = useDstu9311 ? 8 : 32, macLength = useDstu9311 ? 4 : 32;
18
+ const macMode = cmac(cipher, macLength);
19
+ const finalMode = cfb(cipher, useDstu9311 ? IV1_9311 : IV1_KALYNA, 32);
14
20
  return Object.freeze({
15
21
  wrap: (key, iv) => {
16
- iv ??= randomBytes(32);
17
- if (iv.length != 32)
22
+ iv ??= randomBytes(ivLength);
23
+ if (iv.length != ivLength)
18
24
  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());
25
+ const mac = macMode.compute(key);
26
+ return finalMode.encrypt(concatBytes(iv, cfb(cipher, iv, 32).encrypt(concatBytes(key, mac))).reverse());
22
27
  },
23
28
  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
+ const dec_wrapped = finalMode.decrypt(wrappedKey).reverse();
30
+ const iv = dec_wrapped.subarray(0, ivLength);
31
+ const key_mac = cfb(cipher, iv, 32).decrypt(dec_wrapped.subarray(ivLength));
32
+ const key = key_mac.slice(0, -macLength), mac = key_mac.subarray(-macLength);
33
+ const actualMac = macMode.compute(key);
29
34
  if (!equalBytes(actualMac, mac))
30
35
  throw new Error("Invalid MAC");
31
36
  return key;
package/modes/ccm.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { type TArg } from "@noble/curves/utils.js";
2
+ import type { AEADMode, Cipher } from "../types.js";
3
+ /** Counter with CBC-MAC (CCM) mode (AEAD) */
4
+ export declare const ccm: (cipher: Cipher, iv: TArg<Uint8Array>, q?: number, Nb?: number) => AEADMode;
package/modes/ccm.js ADDED
@@ -0,0 +1,47 @@
1
+ import { equalBytes, concatBytes } from "@noble/curves/utils.js";
2
+ import { assertKalyna } from "../utils.js";
3
+ import { pad } from "../padding.js";
4
+ import { ctr } from "./ctr.js";
5
+ /** Counter with CBC-MAC (CCM) mode (AEAD) */
6
+ export const ccm = (cipher, iv, q = 16, Nb = 4) => {
7
+ assertKalyna(cipher);
8
+ if (q !== 8 && q !== 16 && q !== 32 && q !== 48 && q !== 64)
9
+ throw new Error('q must be 8, 16, 32, 48, or 64');
10
+ if (cipher.blockSize < Nb + 1)
11
+ throw new Error('Cipher block size must be >= Nb + 1');
12
+ const tmp = cipher.blockSize - 1, tmp2 = tmp - Nb;
13
+ const calculateMac = (msg, aad) => {
14
+ aad ??= new Uint8Array();
15
+ const G1 = new Uint8Array(cipher.blockSize), G2 = new Uint8Array(cipher.blockSize), B = new Uint8Array(cipher.blockSize);
16
+ G1.set(iv.subarray(0, tmp2));
17
+ G1[tmp2] = msg.length & 0xFF;
18
+ G1[tmp] = (msg.length > 0 ? 128 : 0) | (q + 32 - (q % 16)) | (Nb - 1);
19
+ G2[0] = aad.length & 0xFF;
20
+ const updateMac = (data) => {
21
+ for (let i = 0; i < data.length; i += cipher.blockSize) {
22
+ const chunk = data.subarray(i, i + cipher.blockSize);
23
+ for (let j = 0; j < cipher.blockSize; j++)
24
+ B[j] ^= chunk[j];
25
+ B.set(cipher.encrypt(B));
26
+ }
27
+ };
28
+ updateMac(concatBytes(G1, G2.subarray(0, cipher.blockSize - (aad.length % cipher.blockSize)), aad));
29
+ updateMac(pad(msg, cipher.blockSize));
30
+ return B.slice(0, q);
31
+ };
32
+ const mode = ctr(cipher, iv);
33
+ return Object.freeze({
34
+ seal: (plaintext, aad) => {
35
+ const mac = calculateMac(plaintext, aad);
36
+ return mode.crypt(concatBytes(plaintext, mac));
37
+ },
38
+ open: (ciphertext, aad) => {
39
+ const decrypted = mode.crypt(ciphertext);
40
+ const plaintext = decrypted.slice(0, -q);
41
+ const hC = calculateMac(plaintext, aad);
42
+ if (!equalBytes(decrypted.subarray(-q), hC))
43
+ throw new Error("Invalid MAC");
44
+ return plaintext;
45
+ }
46
+ });
47
+ };
package/modes/gcm.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type TArg } from "@noble/hashes/utils.js";
1
+ import { type TArg } from "@noble/curves/utils.js";
2
2
  import type { AEADMode, Cipher } from "../types.js";
3
3
  /** Galois counter (GCM) mode (AEAD) */
4
4
  export declare const gcm: (cipher: Cipher, iv: TArg<Uint8Array>, q?: number) => AEADMode;
package/modes/gcm.js CHANGED
@@ -1,7 +1,6 @@
1
- import { concatBytes } from "@noble/hashes/utils.js";
1
+ import { equalBytes, concatBytes } from "@noble/curves/utils.js";
2
2
  import { ctr } from "./ctr.js";
3
3
  import { gmac } from "./gmac.js";
4
- import { equalBytes } from "@noble/curves/utils.js";
5
4
  import { assertKalyna } from "../utils.js";
6
5
  /** Galois counter (GCM) mode (AEAD) */
7
6
  export const gcm = (cipher, iv, q = 16) => {
package/modes/gmac.js CHANGED
@@ -1,6 +1,5 @@
1
- import { concatBytes } from "@noble/hashes/utils.js";
1
+ import { numberToBytesLE, concatBytes } from "@noble/curves/utils.js";
2
2
  import { assertKalyna, gf2mMul } from "../utils.js";
3
- import { numberToBytesLE } from "@noble/curves/utils.js";
4
3
  const xorBytesInPlace = (a, b) => {
5
4
  if (a.length != b.length)
6
5
  throw new Error("Byte arrays must have same length");
package/modes/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./cbc.js";
2
+ export * from "./ccm.js";
2
3
  export * from "./cfb.js";
3
4
  export * from "./cmac.js";
4
5
  export * from "./ctr.js";
package/modes/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from "./cbc.js";
2
+ export * from "./ccm.js";
2
3
  export * from "./cfb.js";
3
4
  export * from "./cmac.js";
4
5
  export * from "./ctr.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@li0ard/dstu",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "type": "module",
@@ -42,8 +42,8 @@
42
42
  "typescript": "^5"
43
43
  },
44
44
  "dependencies": {
45
- "@noble/curves": "^2.3.0",
46
- "@noble/hashes": "^2.3.0",
45
+ "@noble/curves": "^2.4.0",
46
+ "@noble/hashes": "^2.4.0",
47
47
  "bn.js": "^5.2.3"
48
48
  },
49
49
  "files": ["**/*.js", "**/*.d.ts"],
package/utils.d.ts CHANGED
@@ -11,3 +11,4 @@ export declare const getGf2mReductionBytes: (blockSize: number) => number[];
11
11
  export declare const gf2mMul: (blockSize: number, a: TArg<Uint8Array>, b: TArg<Uint8Array>) => TRet<Uint8Array>;
12
12
  export declare const isKalyna: (cipher: Cipher) => cipher is Kalyna;
13
13
  export declare const assertKalyna: (cipher: Cipher) => void;
14
+ export declare const reverseBytes: (bytes: TArg<Uint8Array>) => TRet<Uint8Array>;
package/utils.js CHANGED
@@ -1,5 +1,4 @@
1
- import { bytesToNumberBE, bytesToNumberLE, numberToBytesBE, numberToBytesLE } from "@noble/curves/utils.js";
2
- import { copyBytes } from "@noble/hashes/utils.js";
1
+ import { copyBytes, bytesToNumberBE, bytesToNumberLE, numberToBytesBE, numberToBytesLE } from "@noble/curves/utils.js";
3
2
  import { Kalyna } from "./kalyna/index.js";
4
3
  export const xorBytes = (a, b) => {
5
4
  const mlen = Math.min(a.length, b.length);
@@ -69,3 +68,4 @@ export const assertKalyna = (cipher) => {
69
68
  if (!isKalyna(cipher))
70
69
  throw new Error("Cipher mode only for Kalyna");
71
70
  };
71
+ export const reverseBytes = (bytes) => copyBytes(bytes).reverse();
@@ -1,206 +0,0 @@
1
- import BN from "bn.js";
2
- const SQR_PRECOMP = new Uint16Array([
3
- 0x0000, 0x0001, 0x0004, 0x0005, 0x0010, 0x0011, 0x0014, 0x0015,
4
- 0x0040, 0x0041, 0x0044, 0x0045, 0x0050, 0x0051, 0x0054, 0x0055,
5
- 0x0100, 0x0101, 0x0104, 0x0105, 0x0110, 0x0111, 0x0114, 0x0115,
6
- 0x0140, 0x0141, 0x0144, 0x0145, 0x0150, 0x0151, 0x0154, 0x0155,
7
- 0x0400, 0x0401, 0x0404, 0x0405, 0x0410, 0x0411, 0x0414, 0x0415,
8
- 0x0440, 0x0441, 0x0444, 0x0445, 0x0450, 0x0451, 0x0454, 0x0455,
9
- 0x0500, 0x0501, 0x0504, 0x0505, 0x0510, 0x0511, 0x0514, 0x0515,
10
- 0x0540, 0x0541, 0x0544, 0x0545, 0x0550, 0x0551, 0x0554, 0x0555,
11
- 0x1000, 0x1001, 0x1004, 0x1005, 0x1010, 0x1011, 0x1014, 0x1015,
12
- 0x1040, 0x1041, 0x1044, 0x1045, 0x1050, 0x1051, 0x1054, 0x1055,
13
- 0x1100, 0x1101, 0x1104, 0x1105, 0x1110, 0x1111, 0x1114, 0x1115,
14
- 0x1140, 0x1141, 0x1144, 0x1145, 0x1150, 0x1151, 0x1154, 0x1155,
15
- 0x1400, 0x1401, 0x1404, 0x1405, 0x1410, 0x1411, 0x1414, 0x1415,
16
- 0x1440, 0x1441, 0x1444, 0x1445, 0x1450, 0x1451, 0x1454, 0x1455,
17
- 0x1500, 0x1501, 0x1504, 0x1505, 0x1510, 0x1511, 0x1514, 0x1515,
18
- 0x1540, 0x1541, 0x1544, 0x1545, 0x1550, 0x1551, 0x1554, 0x1555,
19
- 0x4000, 0x4001, 0x4004, 0x4005, 0x4010, 0x4011, 0x4014, 0x4015,
20
- 0x4040, 0x4041, 0x4044, 0x4045, 0x4050, 0x4051, 0x4054, 0x4055,
21
- 0x4100, 0x4101, 0x4104, 0x4105, 0x4110, 0x4111, 0x4114, 0x4115,
22
- 0x4140, 0x4141, 0x4144, 0x4145, 0x4150, 0x4151, 0x4154, 0x4155,
23
- 0x4400, 0x4401, 0x4404, 0x4405, 0x4410, 0x4411, 0x4414, 0x4415,
24
- 0x4440, 0x4441, 0x4444, 0x4445, 0x4450, 0x4451, 0x4454, 0x4455,
25
- 0x4500, 0x4501, 0x4504, 0x4505, 0x4510, 0x4511, 0x4514, 0x4515,
26
- 0x4540, 0x4541, 0x4544, 0x4545, 0x4550, 0x4551, 0x4554, 0x4555,
27
- 0x5000, 0x5001, 0x5004, 0x5005, 0x5010, 0x5011, 0x5014, 0x5015,
28
- 0x5040, 0x5041, 0x5044, 0x5045, 0x5050, 0x5051, 0x5054, 0x5055,
29
- 0x5100, 0x5101, 0x5104, 0x5105, 0x5110, 0x5111, 0x5114, 0x5115,
30
- 0x5140, 0x5141, 0x5144, 0x5145, 0x5150, 0x5151, 0x5154, 0x5155,
31
- 0x5400, 0x5401, 0x5404, 0x5405, 0x5410, 0x5411, 0x5414, 0x5415,
32
- 0x5440, 0x5441, 0x5444, 0x5445, 0x5450, 0x5451, 0x5454, 0x5455,
33
- 0x5500, 0x5501, 0x5504, 0x5505, 0x5510, 0x5511, 0x5514, 0x5515,
34
- 0x5540, 0x5541, 0x5544, 0x5545, 0x5550, 0x5551, 0x5554, 0x5555
35
- ]);
36
- const WORD_BITS = 32;
37
- const MUL_WINDOW = 4, MUL_TABLE_SIZE = 1 << MUL_WINDOW;
38
- const bn2LE = (x, numWords) => {
39
- const arr = new Uint32Array(numWords);
40
- const bytes = x.toArray("le");
41
- for (let i = 0; i < bytes.length; i++)
42
- arr[i >> 2] |= bytes[i] << ((i & 3) * 8);
43
- return arr;
44
- };
45
- const le2BN = (arr) => {
46
- const bytes = new Uint8Array(arr.length * 4);
47
- for (let i = 0; i < arr.length; i++) {
48
- const w = arr[i];
49
- bytes[i * 4] = w & 0xff;
50
- bytes[i * 4 + 1] = (w >>> 8) & 0xff;
51
- bytes[i * 4 + 2] = (w >>> 16) & 0xff;
52
- bytes[i * 4 + 3] = (w >>> 24) & 0xff;
53
- }
54
- return new BN(bytes, "le");
55
- };
56
- const xorShiftedWordInto = (dst, val, shift) => {
57
- if (val === 0 || shift < 0)
58
- return;
59
- const wordShift = shift >>> 5, bitShift = shift & 31;
60
- if (bitShift === 0) {
61
- if (wordShift < dst.length)
62
- dst[wordShift] ^= (val >>> 0);
63
- }
64
- else {
65
- const lo = (val << bitShift) >>> 0;
66
- const hi = (val >>> (WORD_BITS - bitShift)) >>> 0;
67
- if (wordShift < dst.length)
68
- dst[wordShift] ^= lo;
69
- if (wordShift + 1 < dst.length)
70
- dst[wordShift + 1] ^= hi;
71
- }
72
- };
73
- /** Compute curve modulo */
74
- export const computeMod = (m, ks) => {
75
- const modulo = new BN(0);
76
- modulo.setn(m, 1);
77
- modulo.setn(0, 1);
78
- for (const i of ks)
79
- modulo.setn(i, 1);
80
- return modulo;
81
- };
82
- /** Create `GF(2^m)` field */
83
- export const createField = (m, ks) => {
84
- const modulo = computeMod(m, ks), wordsPerElement = Math.ceil(m / WORD_BITS), wordsForProduct = 2 * wordsPerElement, vl = modulo.bitLength();
85
- const bMod = (f) => {
86
- // `f` will be mutated
87
- if (f.bitLength() <= m)
88
- return f;
89
- while (f.bitLength() > m)
90
- f.ixor(modulo.ushln(f.bitLength() - vl));
91
- return f;
92
- };
93
- const mod = (f) => {
94
- if (f.bitLength() <= m)
95
- return f.clone();
96
- const words = bn2LE(f, Math.max(wordsForProduct, Math.ceil(f.bitLength() / WORD_BITS) + 1));
97
- for (let i = words.length - 1; i >= wordsPerElement; i--) {
98
- const T = words[i];
99
- if (T === 0)
100
- continue;
101
- words[i] = 0;
102
- const base = i * WORD_BITS - m;
103
- xorShiftedWordInto(words, T, base);
104
- for (const k of ks)
105
- xorShiftedWordInto(words, T, base + k);
106
- }
107
- return bMod(le2BN(words));
108
- };
109
- const mul = (x, v) => {
110
- if (x.eq(v))
111
- return sqr(x);
112
- const table = new Array(MUL_TABLE_SIZE);
113
- table[0] = new BN(0);
114
- for (let i = 1; i < MUL_TABLE_SIZE; i++) {
115
- const lowBit = i & (-i);
116
- const lowBitIdx = Math.log2(lowBit) | 0;
117
- table[i] = table[i ^ lowBit].xor(x.ushln(lowBitIdx));
118
- }
119
- const vb = v.bitLength();
120
- const acc = new BN(0);
121
- for (let win = Math.ceil(vb / MUL_WINDOW) - 1; win >= 0; win--) {
122
- let windowVal = 0;
123
- for (let b = 0; b < MUL_WINDOW; b++) {
124
- const bitPos = win * MUL_WINDOW + b;
125
- if (bitPos < vb && v.testn(bitPos))
126
- windowVal |= (1 << b);
127
- }
128
- acc.iushln(MUL_WINDOW).ixor(table[windowVal]);
129
- }
130
- return mod(acc);
131
- };
132
- const div = (x, v) => mul(x, invert(v));
133
- const sqr = (x) => {
134
- const bytes = x.toArray();
135
- const out = new Uint8Array(bytes.length * 2);
136
- for (let i = 0; i < bytes.length; i++) {
137
- const v = SQR_PRECOMP[bytes[i]];
138
- out[2 * i] = (v >> 8) & 0xff;
139
- out[2 * i + 1] = v & 0xff;
140
- }
141
- return mod(new BN(out));
142
- };
143
- const testBit = (x, i) => x.testn(i) ? 1 : 0;
144
- const invBit = (x, i) => x.setn(i, !x.testn(i));
145
- const sqrt = (x) => {
146
- let r = mod(x);
147
- for (let i = 0; i < m - 1; i++)
148
- r = sqr(r);
149
- return r;
150
- };
151
- const trace = (x) => {
152
- let t = x;
153
- for (let i = 1; i < m; i++)
154
- t = sqr(t).ixor(x);
155
- return testBit(t, 0);
156
- };
157
- const traceOnb = (x) => {
158
- let t = 0;
159
- for (let i = 0; i < m; i++)
160
- t ^= testBit(x, i);
161
- return t;
162
- };
163
- const htrace = (x) => {
164
- let ht = x;
165
- for (let i = 1; i <= Math.floor((m - 1) / 2); i++)
166
- ht = sqr(sqr(ht)).ixor(x);
167
- return ht;
168
- };
169
- const invert = (f) => {
170
- let r = mod(f), s = modulo.clone();
171
- let u = new BN(1), v = new BN(0);
172
- while (r.bitLength() > 1) {
173
- let j = s.bitLength() - r.bitLength();
174
- if (j < 0) {
175
- let buf = r;
176
- r = s;
177
- s = buf;
178
- buf = u;
179
- u = v;
180
- v = buf;
181
- j = -j;
182
- }
183
- s.ixor(r.ushln(j));
184
- v.ixor(u.ushln(j));
185
- }
186
- return u;
187
- };
188
- const solve_quad = (v) => {
189
- const a = mod(v), z = htrace(a);
190
- if (sqr(z).ixor(z).cmp(a) == 0)
191
- return mod(z);
192
- throw new Error("squad eq fail: no square root exists");
193
- };
194
- const hashToField = (hash) => {
195
- const h = new BN(hash);
196
- return h.imaskn(Math.min(m, h.bitLength()));
197
- };
198
- const fromHexStringOrBytes = (v) => new BN(v, 16);
199
- const toBytes = (x, length) => new Uint8Array(x.toArray("be", length));
200
- return Object.freeze({
201
- MODULO: modulo, LENGTH: Math.ceil(m / 8),
202
- mod, mul, div, sqr, sqrt, invert,
203
- solve_quad, trace, traceOnb, hashToField, invBit,
204
- fromHexStringOrBytes, toBytes
205
- });
206
- };
File without changes