@li0ard/dstu 0.1.7 → 0.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/README.md +6 -2
  2. package/dstu4145/const.d.ts +2 -0
  3. package/dstu4145/const.js +42 -15
  4. package/dstu4145/ec/expand.d.ts +4 -3
  5. package/dstu4145/ec/expand.js +1 -2
  6. package/dstu4145/ec/index.js +1 -2
  7. package/dstu4145/ec/wnaf.d.ts +2 -1
  8. package/dstu4145/ecies.d.ts +82 -0
  9. package/dstu4145/ecies.js +62 -0
  10. package/dstu4145/{ec/math.d.ts → gf2m/index.d.ts} +1 -0
  11. package/dstu4145/gf2m/index.js +78 -0
  12. package/dstu4145/{ec → gf2m}/onb.js +31 -17
  13. package/dstu4145/gf2m/utils.d.ts +10 -0
  14. package/dstu4145/gf2m/utils.js +262 -0
  15. package/dstu4145/index.d.ts +23 -177
  16. package/dstu4145/index.js +37 -20
  17. package/dstu9041/curve.d.ts +4 -12
  18. package/dstu9041/curve.js +4 -3
  19. package/dstu9041/index.d.ts +2 -8
  20. package/dstu9041/index.js +2 -3
  21. package/dstu9311/hash.d.ts +10 -3
  22. package/dstu9311/hash.js +10 -3
  23. package/iso15946.d.ts +3 -0
  24. package/iso15946.js +49 -0
  25. package/kalyna/index.d.ts +2 -1
  26. package/kalyna/index.js +23 -28
  27. package/keywrap.d.ts +3 -3
  28. package/keywrap.js +21 -16
  29. package/modes/ccm.d.ts +4 -0
  30. package/modes/ccm.js +47 -0
  31. package/modes/gcm.d.ts +1 -1
  32. package/modes/gcm.js +1 -2
  33. package/modes/gmac.js +1 -2
  34. package/modes/index.d.ts +1 -0
  35. package/modes/index.js +1 -0
  36. package/modes/kw.d.ts +2 -6
  37. package/modes/xts.d.ts +2 -6
  38. package/modes/xts.js +2 -2
  39. package/package.json +6 -6
  40. package/strumok/index.d.ts +2 -1
  41. package/types.d.ts +77 -0
  42. package/utils.d.ts +1 -0
  43. package/utils.js +2 -2
  44. package/dstu4145/ec/math.js +0 -206
  45. /package/dstu4145/{ec → gf2m}/onb.d.ts +0 -0
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
- import { kw } from "../modes/index.js";
4
+ import { kw } from "../modes/kw.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/iso15946.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import type { CHash, Hash, TArg, TRet } from "@noble/hashes/utils.js";
2
+ /** ISO/IEC 15946 KDF function */
3
+ export declare const iso15946_kdf: (hash: CHash<Hash<any>>, wrapAlgoOid: string, inputData: TArg<Uint8Array>, ukm?: TArg<Uint8Array>) => TRet<Uint8Array>;
package/iso15946.js ADDED
@@ -0,0 +1,49 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ import { AsnConvert, AsnProp, AsnPropTypes } from "@peculiar/asn1-schema";
8
+ import { AlgorithmIdentifier } from "@peculiar/asn1-x509";
9
+ import { Gost3431195 } from "./dstu9311/hash.js";
10
+ const KEY_LENGTH = new Uint8Array([0, 0, 1, 0]);
11
+ const COUNTER = new Uint8Array([0, 0, 0, 1]);
12
+ class SharedInfo {
13
+ keyInfo = new AlgorithmIdentifier();
14
+ entityInfo;
15
+ suppPubInfo = new ArrayBuffer(0);
16
+ constructor(params = {}) {
17
+ Object.assign(this, params);
18
+ }
19
+ }
20
+ __decorate([
21
+ AsnProp({ type: AlgorithmIdentifier })
22
+ ], SharedInfo.prototype, "keyInfo", void 0);
23
+ __decorate([
24
+ AsnProp({ type: AsnPropTypes.OctetString, context: 0, optional: true })
25
+ ], SharedInfo.prototype, "entityInfo", void 0);
26
+ __decorate([
27
+ AsnProp({ type: AsnPropTypes.OctetString, context: 2 })
28
+ ], SharedInfo.prototype, "suppPubInfo", void 0);
29
+ const removeLeadZeros = (bytes) => {
30
+ const idx = bytes.findIndex(i => i !== 0);
31
+ return idx === -1 ? new Uint8Array() : bytes.slice(idx);
32
+ };
33
+ const encodeSharedInfo = (oid, ukm) => new Uint8Array(AsnConvert.serialize(new SharedInfo({
34
+ keyInfo: new AlgorithmIdentifier({
35
+ algorithm: oid,
36
+ parameters: null
37
+ }),
38
+ entityInfo: ukm,
39
+ suppPubInfo: KEY_LENGTH.buffer
40
+ })));
41
+ /** ISO/IEC 15946 KDF function */
42
+ export const iso15946_kdf = (hash, wrapAlgoOid, inputData, ukm) => {
43
+ if (hash.outputLen != 32)
44
+ throw new Error("Invalid hash function. Output length must be 32 bytes");
45
+ const hasher = hash.create();
46
+ return hasher.update(hasher instanceof Gost3431195
47
+ ? removeLeadZeros(inputData)
48
+ : inputData).update(COUNTER).update(encodeSharedInfo(wrapAlgoOid, ukm)).digest();
49
+ };
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/modes/kw.d.ts CHANGED
@@ -1,7 +1,3 @@
1
- import { type TArg, type TRet } from "@noble/hashes/utils.js";
2
- import type { Cipher } from "../types.js";
1
+ import type { Cipher, WrapMode } from "../types.js";
3
2
  /** Key wrapping (KW) */
4
- export declare const kw: (cipher: Cipher) => {
5
- wrap: (key: TArg<Uint8Array>) => TRet<Uint8Array>;
6
- unwrap: (wrappedKey: TArg<Uint8Array>) => TRet<Uint8Array>;
7
- };
3
+ export declare const kw: (cipher: Cipher) => WrapMode;
package/modes/xts.d.ts CHANGED
@@ -1,7 +1,3 @@
1
- import { type TArg, type TRet } from "@noble/hashes/utils.js";
2
- import type { Cipher } from "../types.js";
1
+ import type { Cipher, DiskMode } from "../types.js";
3
2
  /** XEX Tweakable Block Ciphertext Stealing (XTS) */
4
- export declare const xts: (cipher: Cipher) => {
5
- encrypt: (plaintext: TArg<Uint8Array>, tweak: TArg<Uint8Array>) => TRet<Uint8Array>;
6
- decrypt: (ciphertext: TArg<Uint8Array>, iv: TArg<Uint8Array>) => TRet<Uint8Array>;
7
- };
3
+ export declare const xts: (cipher: Cipher) => DiskMode;
package/modes/xts.js CHANGED
@@ -57,10 +57,10 @@ export const xts = (cipher) => {
57
57
  }
58
58
  return buffer;
59
59
  },
60
- decrypt: (ciphertext, iv) => {
60
+ decrypt: (ciphertext, tweak) => {
61
61
  if (ciphertext.length < cipher.blockSize)
62
62
  throw new Error(`Invalid length (need at least ${cipher.blockSize}, got ${ciphertext.length})`);
63
- const gamma = init(iv);
63
+ const gamma = init(tweak);
64
64
  const decrypt = cipher.decrypt.bind(cipher);
65
65
  const buffer = new Uint8Array(ciphertext);
66
66
  const k = Math.floor(ciphertext.length / cipher.blockSize);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@li0ard/dstu",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "type": "module",
@@ -32,8 +32,6 @@
32
32
  "docs": "typedoc --options .config/typedoc/config.cjs"
33
33
  },
34
34
  "devDependencies": {
35
- "@peculiar/asn1-schema": "^2.8.0",
36
- "@peculiar/asn1-x509": "^2.8.0",
37
35
  "@types/bn.js": "^5.2.0",
38
36
  "@types/bun": "latest",
39
37
  "typedoc": "^0.28.19"
@@ -42,9 +40,11 @@
42
40
  "typescript": "^5"
43
41
  },
44
42
  "dependencies": {
45
- "@noble/curves": "^2.3.0",
46
- "@noble/hashes": "^2.3.0",
47
- "bn.js": "^5.2.3"
43
+ "@noble/curves": "^2.4.0",
44
+ "@noble/hashes": "^2.4.0",
45
+ "bn.js": "^5.2.3",
46
+ "@peculiar/asn1-schema": "^2.9.5",
47
+ "@peculiar/asn1-x509": "^2.9.5"
48
48
  },
49
49
  "files": ["**/*.js", "**/*.d.ts"],
50
50
  "publishConfig": {
@@ -1,6 +1,7 @@
1
1
  import type { TArg, TRet } from "@noble/hashes/utils.js";
2
+ import type { StreamCipher } from "../types.js";
2
3
  /** Strumok stream cipher */
3
- export declare class Strumok {
4
+ export declare class Strumok implements StreamCipher {
4
5
  readonly blockSize = 128;
5
6
  readonly keySize: number;
6
7
  private S;
package/types.d.ts CHANGED
@@ -12,6 +12,16 @@ export type Cipher = {
12
12
  /** Decrypt block */
13
13
  decrypt(ciphertext: TArg<Uint8Array>): TRet<Uint8Array>;
14
14
  };
15
+ export type StreamCipher = {
16
+ /** Block size */
17
+ readonly blockSize: number;
18
+ /** Key size */
19
+ readonly keySize: number;
20
+ /** Generate keystream word */
21
+ next_stream(): TRet<Uint8Array>;
22
+ /** Perform encryption/decryption */
23
+ crypt(msg: TArg<Uint8Array>): TRet<Uint8Array>;
24
+ };
15
25
  /** Block mode for {@link Cipher} */
16
26
  export type BlockMode = {
17
27
  /** Encrypt plaintext */
@@ -41,3 +51,70 @@ export type AEADMode = {
41
51
  /** Open ciphertext and AAD */
42
52
  open: (ciphertext: TArg<Uint8Array>, aad?: TArg<Uint8Array>) => TRet<Uint8Array>;
43
53
  };
54
+ /** Key wrap mode for {@link Cipher} */
55
+ export type WrapMode = {
56
+ /** Wrap encryption key */
57
+ wrap: (key: TArg<Uint8Array>) => TRet<Uint8Array>;
58
+ /** Unwrap encryption key */
59
+ unwrap: (wrappedKey: TArg<Uint8Array>) => TRet<Uint8Array>;
60
+ };
61
+ /** Disk encryption mode for {@link Cipher} */
62
+ export type DiskMode = {
63
+ /** Encrypt disk sector */
64
+ encrypt: (plaintext: TArg<Uint8Array>, tweak: TArg<Uint8Array>) => TRet<Uint8Array>;
65
+ /** Decrypt disk sector */
66
+ decrypt: (ciphertext: TArg<Uint8Array>, tweak: TArg<Uint8Array>) => TRet<Uint8Array>;
67
+ };
68
+ /** ECDSA signer */
69
+ export type ECDSA = {
70
+ /**
71
+ * Computes public key for a secret key
72
+ * @param isCompressed - Whether to return compact (default), or full key
73
+ * @returns Public key, full when `isCompressed=false`; short when `isCompressed=true`
74
+ */
75
+ getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
76
+ /**
77
+ * Signs a message hash with a secret key.
78
+ * @param secretKey - Secret key bytes.
79
+ * @param digest - Digest bytes.
80
+ * @param rand - Optional parameter for ephemeral key
81
+ * @returns Encoded signature bytes.
82
+ */
83
+ sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
84
+ /**
85
+ * Verifies a signature against message hash and public key.
86
+ * @param publicKey - Public key
87
+ * @param digest - Digest bytes
88
+ * @param signature - Encoded signature bytes.
89
+ * @returns Whether the signature is valid.
90
+ */
91
+ verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
92
+ /**
93
+ * Compute the shared secret point from a secret key and peer public key.
94
+ * @param secretKeyA - Local secret key bytes.
95
+ * @param publicKeyB - Peer public key bytes.
96
+ * @param withCofactor - Whether to multiply result by curve cofactor? (default - `true`).
97
+ * @returns Encoded shared point.
98
+ */
99
+ getSharedSecret: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
100
+ /**
101
+ * Generate a secret/public key pair.
102
+ * @param isCompressed - Whether to return compact (default), or full key
103
+ * @returns Secret/public key pair.
104
+ */
105
+ keygen: (isCompressed?: boolean) => KeyPair;
106
+ /** Byte lengths for keys and signatures exposed by this curve. */
107
+ lengths: Readonly<{
108
+ fieldByteLength: number;
109
+ pointByteLength: number;
110
+ scalarByteLength: number;
111
+ signatureByteLength: number;
112
+ }>;
113
+ };
114
+ /** Keypair */
115
+ export type KeyPair = {
116
+ /** Secret key */
117
+ secretKey: TArg<Uint8Array>;
118
+ /** Public key */
119
+ publicKey: TArg<Uint8Array>;
120
+ };
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();