@li0ard/dstu 0.2.0 → 0.2.1

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 (49) hide show
  1. package/const.d.ts +5 -14
  2. package/const.js +47 -1010
  3. package/dstu4145/const.js +15 -15
  4. package/dstu4145/ec/index.js +9 -27
  5. package/dstu4145/ecies.d.ts +2 -2
  6. package/dstu4145/ecies.js +2 -1
  7. package/dstu4145/gf2m/utils.js +1 -1
  8. package/dstu4145/index.js +12 -12
  9. package/dstu9041/curve.d.ts +1 -1
  10. package/dstu9041/curve.js +14 -27
  11. package/dstu9041/index.js +12 -20
  12. package/dstu9311/const.js +12 -12
  13. package/dstu9311/index.d.ts +1 -2
  14. package/dstu9311/index.js +34 -30
  15. package/gf/index.d.ts +5 -0
  16. package/gf/index.js +62 -0
  17. package/gost34311/index.d.ts +55 -0
  18. package/gost34311/index.js +167 -0
  19. package/index.d.ts +1 -0
  20. package/index.js +1 -0
  21. package/iso15946.js +4 -6
  22. package/kalyna/index.d.ts +2 -4
  23. package/kalyna/index.js +113 -92
  24. package/keywrap.js +5 -10
  25. package/kupyna/index.d.ts +21 -103
  26. package/kupyna/index.js +104 -153
  27. package/modes/_guard.d.ts +4 -0
  28. package/modes/_guard.js +6 -0
  29. package/modes/cbc.js +2 -1
  30. package/modes/ccm.js +1 -1
  31. package/modes/cfb.js +2 -1
  32. package/modes/cmac.js +2 -1
  33. package/modes/ctr.js +2 -1
  34. package/modes/gcm.js +2 -1
  35. package/modes/gmac.js +4 -8
  36. package/modes/kw.js +1 -1
  37. package/modes/ofb.js +2 -1
  38. package/modes/xts.js +11 -25
  39. package/package.json +12 -9
  40. package/padding.d.ts +1 -1
  41. package/padding.js +0 -5
  42. package/strumok/index.d.ts +3 -4
  43. package/strumok/index.js +128 -85
  44. package/utils.d.ts +6 -13
  45. package/utils.js +27 -70
  46. package/dstu9311/hash.d.ts +0 -75
  47. package/dstu9311/hash.js +0 -159
  48. package/kupyna/kmac.d.ts +0 -46
  49. package/kupyna/kmac.js +0 -130
package/kupyna/index.js CHANGED
@@ -1,135 +1,123 @@
1
- import { abytes, ahash, anumber, aoutput, createHasher } from "@noble/hashes/utils.js";
2
- import { bytesToUint64sLE, uint64sToBytesLE } from "../utils.js";
1
+ import { abytes, aexists, ahash, aoutput, clean, createHasher, createView } from "@noble/hashes/utils.js";
2
+ import { HashMD } from "@noble/hashes/_md.js";
3
3
  import { column } from "../kalyna/index.js";
4
- import { numberToBytesLE } from "@noble/curves/utils.js";
5
- const r = 0x00f0f0f0f0f0f0f3n;
6
- class Kupyna {
7
- blockLen;
8
- outputLen;
9
- canXOF = false;
4
+ import { _U32 } from "../utils.js";
5
+ const R_LO = 0xF0F0F0F3, R_HI = 0x00F0F0F0;
6
+ class Kupyna extends HashMD {
10
7
  rounds;
11
8
  stSize;
12
- threshold;
9
+ offs;
13
10
  s;
14
- x;
15
- nx;
16
- len;
11
+ m;
12
+ ap;
13
+ aq;
14
+ tmp;
17
15
  constructor(blockLen) {
18
- this.blockLen = blockLen;
19
- anumber(blockLen, "blockLen");
20
- this.stSize = (blockLen / 2) / 4;
21
- this.threshold = blockLen - 12;
16
+ super(blockLen, blockLen / 2, 12, true);
17
+ this.stSize = blockLen >>> 3;
22
18
  this.rounds = 4 * Math.log2(blockLen) - 14;
23
- this.outputLen = blockLen / 2;
24
- this.s = new BigUint64Array(this.stSize);
25
- this.x = new Uint8Array(this.blockLen);
26
- this.nx = 0;
27
- this.len = 0n;
28
- this.s[0] = BigInt(this.blockLen);
29
- }
30
- destroy() {
31
- this.s[0] = BigInt(this.blockLen);
32
- this.s.fill(0n, 1);
33
- this.x.fill(0);
34
- this.nx = 0;
35
- this.len = 0n;
36
- }
37
- update(data) {
38
- abytes(data, undefined, "data");
39
- this.len += BigInt(data.length);
40
- if (this.nx > 0) {
41
- const n = Math.min(this.blockLen - this.nx, data.length);
42
- this.x.set(data.subarray(0, n), this.nx);
43
- this.nx += n;
44
- if (this.nx === this.blockLen) {
45
- this.F(bytesToUint64sLE(this.x));
46
- this.nx = 0;
47
- }
48
- data = data.slice(n);
49
- }
50
- while (data.length >= this.blockLen) {
51
- this.F(bytesToUint64sLE(data.subarray(0, this.blockLen)));
52
- this.nx = 0;
53
- data = data.slice(this.blockLen);
19
+ this.offs = [0, 1, 2, 3, 4, 5, 6, blockLen === 64 ? 7 : 11];
20
+ const w = this.stSize * 2;
21
+ this.s = new Uint32Array(w);
22
+ this.m = new Uint32Array(w);
23
+ this.ap = new Uint32Array(w);
24
+ this.aq = new Uint32Array(w);
25
+ this.tmp = new Uint32Array(w);
26
+ this.s[0] = blockLen;
27
+ }
28
+ // Unused
29
+ get() { return []; }
30
+ set(..._args) { }
31
+ process(buf, offset = 0) {
32
+ for (let i = 0; i < this.m.length; i++)
33
+ this.m[i] = buf.getUint32(offset + (i << 2), true);
34
+ for (let i = 0; i < this.s.length; i++) {
35
+ this.ap[i] = this.s[i] ^ this.m[i];
36
+ this.aq[i] = this.m[i];
54
37
  }
55
- if (data.length > 0) {
56
- this.x.set(data, 0);
57
- this.nx = data.length;
38
+ for (let r = 0; r < this.rounds; r += 2) {
39
+ this.P(this.ap, this.tmp, r);
40
+ this.Q(this.aq, this.tmp, r);
58
41
  }
59
- return this;
42
+ for (let i = 0; i < this.s.length; i++)
43
+ this.s[i] ^= this.ap[i] ^ this.aq[i];
60
44
  }
61
- digest() {
62
- const buffer = new Uint8Array(this.outputLen);
63
- this.digestInto(buffer);
64
- return buffer;
45
+ roundClean() {
46
+ clean(this.m, this.ap, this.aq, this.tmp);
65
47
  }
66
- digestInto(buffer) {
67
- aoutput(buffer, this);
68
- this.x[this.nx] = 0x80;
69
- this.nx++;
70
- const fillBytes = (start) => {
71
- const available = this.x.length - start;
72
- if (available > 0)
73
- this.x.fill(0, start, start + available);
74
- };
75
- if (this.nx > this.threshold) {
76
- fillBytes(this.nx);
77
- this.F(bytesToUint64sLE(this.x));
78
- this.nx = 0;
48
+ destroy() {
49
+ this.destroyed = true;
50
+ clean(this.s, this.buffer);
51
+ this.roundClean();
52
+ }
53
+ digestInto(out) {
54
+ aexists(this);
55
+ aoutput(out, this);
56
+ this.finished = true;
57
+ const threshold = this.blockLen - 12;
58
+ let pos = this.pos;
59
+ this.buffer[pos++] = 0x80;
60
+ if (pos > threshold) {
61
+ this.buffer.fill(0, pos);
62
+ this.process(this.view);
63
+ pos = 0;
79
64
  }
80
- fillBytes(this.nx);
81
- this.x.set(numberToBytesLE(this.len * 8n, 12), this.threshold);
82
- this.F(bytesToUint64sLE(this.x));
65
+ this.buffer.fill(0, pos);
66
+ const length = this.length * 8;
67
+ this.view.setUint32(threshold, length >>> 0, true);
68
+ this.view.setUint32(threshold + 4, (length / 2 ** 32) | 0, true);
69
+ this.process(this.view);
83
70
  this.outputTransform();
84
- buffer.set(uint64sToBytesLE(this.s).subarray(this.outputLen));
85
- this.destroy();
86
- }
87
- column(x, i) {
88
- return column(x, i, this.stSize, 1, 2, 3, 4, 5, 6, this.blockLen == 64 ? 7 : 11);
71
+ this.roundClean();
72
+ const oview = out === this.buffer ? this.view : createView(out);
73
+ for (let i = this.outputLen >>> 2, j = 0; i < this.s.length; i++, j += 4)
74
+ oview.setUint32(j, this.s[i], true);
89
75
  }
90
76
  G(x, y) {
91
77
  for (let i = 0; i < this.stSize; i++)
92
- y[i] = this.column(x, i);
78
+ column(x, i, this.stSize, this.offs, y, i << 1);
93
79
  }
94
80
  P(x, y, round) {
95
- for (let i = 0n; i < BigInt(this.stSize); i++)
96
- x[Number(i)] ^= (i << 4n) ^ round;
97
- const r1 = round + 1n;
98
81
  for (let i = 0; i < this.stSize; i++)
99
- y[i] = this.column(x, i) ^ BigInt(i << 4) ^ r1;
82
+ x[i << 1] ^= (i << 4) ^ round;
83
+ const r1 = round + 1;
84
+ for (let i = 0; i < this.stSize; i++) {
85
+ const j = i << 1;
86
+ column(x, i, this.stSize, this.offs, y, j);
87
+ y[j] ^= (i << 4) ^ r1;
88
+ }
100
89
  this.G(y, x);
101
90
  }
102
91
  Q(x, y, round) {
103
- for (let j = 0n; j < BigInt(this.stSize); j++)
104
- x[Number(j)] += r ^ ((((BigInt(this.stSize - 1) - j) * 0x10n) ^ round) << 56n);
105
- const r1 = round + 1n;
106
- for (let i = 0; i < this.stSize; i++)
107
- y[i] = this.column(x, i) + (r ^ ((BigInt((this.stSize - 1 - i) * 16) ^ r1) << 56n));
92
+ for (let i = 0; i < this.stSize; i++) {
93
+ const j = i << 1, tb = (((this.stSize - 1 - i) << 4) ^ round) & 0xFF, ch = (R_HI ^ (tb << 24)) >>> 0, lo = x[j] + R_LO;
94
+ x[j + 1] += ch + (lo > _U32 ? 1 : 0);
95
+ x[j] = lo;
96
+ }
97
+ const r1 = round + 1;
98
+ for (let i = 0; i < this.stSize; i++) {
99
+ const j = i << 1;
100
+ column(x, i, this.stSize, this.offs, y, j);
101
+ const tb = (((this.stSize - 1 - i) << 4) ^ r1) & 0xFF;
102
+ const ch = (R_HI ^ (tb << 24)) >>> 0;
103
+ const lo = y[j] + R_LO;
104
+ y[j + 1] += ch + (lo > _U32 ? 1 : 0);
105
+ y[j] = lo;
106
+ }
108
107
  this.G(y, x);
109
108
  }
110
109
  outputTransform() {
111
- const t1 = new BigUint64Array(this.s), t2 = new BigUint64Array(this.stSize);
112
- for (let r = 0n; r < BigInt(this.rounds); r += 2n)
113
- this.P(t1, t2, r);
114
- for (let column = 0; column < this.stSize; column++)
115
- this.s[column] ^= t1[column];
110
+ this.ap.set(this.s);
111
+ for (let r = 0; r < this.rounds; r += 2)
112
+ this.P(this.ap, this.tmp, r);
113
+ for (let i = 0; i < this.s.length; i++)
114
+ this.s[i] ^= this.ap[i];
116
115
  }
117
- F(b) {
118
- const AQ1 = new BigUint64Array(this.stSize);
119
- const AP1 = new BigUint64Array(this.stSize);
120
- const tmp = new BigUint64Array(this.stSize);
121
- for (let column = 0; column < this.stSize; column++) {
122
- AP1[column] = this.s[column] ^ b[column];
123
- AQ1[column] = b[column];
124
- }
125
- for (let r = 0n; r < BigInt(this.rounds); r += 2n) {
126
- this.P(AP1, tmp, r);
127
- this.Q(AQ1, tmp, r);
128
- }
129
- for (let column = 0; column < this.stSize; column++)
130
- this.s[column] ^= AP1[column] ^ AQ1[column];
116
+ _cloneInto(to) {
117
+ to ||= new this.constructor();
118
+ to.s.set(this.s);
119
+ return this._cloneIntoMeta(to);
131
120
  }
132
- clone() { return this._cloneInto(); }
133
121
  }
134
122
  class KupynaDerived {
135
123
  slice;
@@ -145,6 +133,11 @@ class KupynaDerived {
145
133
  this.h = hash.create();
146
134
  }
147
135
  destroy() { this.h.destroy(); }
136
+ _cloneInto(to) {
137
+ to ||= new this.constructor();
138
+ to.h = this.h.clone();
139
+ return to;
140
+ }
148
141
  clone() { return this._cloneInto(); }
149
142
  update(data) {
150
143
  abytes(data, undefined, "data");
@@ -164,30 +157,10 @@ class KupynaDerived {
164
157
  /** Internal Kupyna-256 hash class */
165
158
  export class _Kupyna256 extends Kupyna {
166
159
  constructor() { super(64); }
167
- _cloneInto(to) {
168
- to ||= new _Kupyna256();
169
- to.s.set(this.s);
170
- to.x.set(this.x);
171
- to.nx = this.nx;
172
- to.len = this.len;
173
- return to;
174
- }
175
- /** Create hash instance */
176
- static create() { return new _Kupyna256(); }
177
160
  }
178
161
  /** Internal Kupyna-512 hash class */
179
162
  export class _Kupyna512 extends Kupyna {
180
163
  constructor() { super(128); }
181
- _cloneInto(to) {
182
- to ||= new _Kupyna512();
183
- to.s.set(this.s);
184
- to.x.set(this.x);
185
- to.nx = this.nx;
186
- to.len = this.len;
187
- return to;
188
- }
189
- /** Create hash instance */
190
- static create() { return new _Kupyna512(); }
191
164
  }
192
165
  /**
193
166
  * Kupyna-256 hash function
@@ -202,7 +175,7 @@ export class _Kupyna512 extends Kupyna {
202
175
  * kupyna256.create().update(new Uint8Array([97, 98, 99])).digest();
203
176
  * ```
204
177
  */
205
- export const kupyna256 = createHasher(_Kupyna256.create);
178
+ export const kupyna256 = /* @__PURE__ */ createHasher(() => new _Kupyna256());
206
179
  /**
207
180
  * Kupyna-512 hash function
208
181
  *
@@ -216,39 +189,18 @@ export const kupyna256 = createHasher(_Kupyna256.create);
216
189
  * kupyna512.create().update(new Uint8Array([97, 98, 99])).digest();
217
190
  * ```
218
191
  */
219
- export const kupyna512 = createHasher(_Kupyna512.create);
192
+ export const kupyna512 = /* @__PURE__ */ createHasher(() => new _Kupyna512());
220
193
  /** Internal Kupyna-48 hash class */
221
194
  export class _Kupyna48 extends KupynaDerived {
222
195
  constructor() { super(kupyna256, -6); }
223
- _cloneInto(to) {
224
- to ||= new _Kupyna48();
225
- to.h = this.h.clone();
226
- return to;
227
- }
228
- /** Create hash instance */
229
- static create() { return new _Kupyna48(); }
230
196
  }
231
197
  /** Internal Kupyna-304 hash class */
232
198
  export class _Kupyna304 extends KupynaDerived {
233
199
  constructor() { super(kupyna512, -38); }
234
- _cloneInto(to) {
235
- to ||= new _Kupyna304();
236
- to.h = this.h.clone();
237
- return to;
238
- }
239
- /** Create hash instance */
240
- static create() { return new _Kupyna304(); }
241
200
  }
242
201
  /** Internal Kupyna-384 hash class */
243
202
  export class _Kupyna384 extends KupynaDerived {
244
203
  constructor() { super(kupyna512, -48); }
245
- _cloneInto(to) {
246
- to ||= new _Kupyna384();
247
- to.h = this.h.clone();
248
- return to;
249
- }
250
- /** Create hash instance */
251
- static create() { return new _Kupyna384(); }
252
204
  }
253
205
  /**
254
206
  * Kupyna-48 hash function
@@ -263,7 +215,7 @@ export class _Kupyna384 extends KupynaDerived {
263
215
  * kupyna48.create().update(new Uint8Array([97, 98, 99])).digest();
264
216
  * ```
265
217
  */
266
- export const kupyna48 = createHasher(_Kupyna48.create);
218
+ export const kupyna48 = /* @__PURE__ */ createHasher(() => new _Kupyna48());
267
219
  /**
268
220
  * Kupyna-304 hash function
269
221
  *
@@ -277,7 +229,7 @@ export const kupyna48 = createHasher(_Kupyna48.create);
277
229
  * kupyna304.create().update(new Uint8Array([97, 98, 99])).digest();
278
230
  * ```
279
231
  */
280
- export const kupyna304 = createHasher(_Kupyna304.create);
232
+ export const kupyna304 = /* @__PURE__ */ createHasher(() => new _Kupyna304());
281
233
  /**
282
234
  * Kupyna-384 hash function
283
235
  *
@@ -291,5 +243,4 @@ export const kupyna304 = createHasher(_Kupyna304.create);
291
243
  * kupyna384.create().update(new Uint8Array([97, 98, 99])).digest();
292
244
  * ```
293
245
  */
294
- export const kupyna384 = createHasher(_Kupyna384.create);
295
- export * from "./kmac.js";
246
+ export const kupyna384 = /* @__PURE__ */ createHasher(() => new _Kupyna384());
@@ -0,0 +1,4 @@
1
+ import { Kalyna } from "../kalyna/index.js";
2
+ import type { Cipher } from "../types.js";
3
+ export declare const isKalyna: (cipher: Cipher) => cipher is Kalyna;
4
+ export declare const assertKalyna: (cipher: Cipher) => void;
@@ -0,0 +1,6 @@
1
+ import { Kalyna } from "../kalyna/index.js";
2
+ export const isKalyna = (cipher) => cipher instanceof Kalyna;
3
+ export const assertKalyna = (cipher) => {
4
+ if (!isKalyna(cipher))
5
+ throw new Error("Cipher mode only for Kalyna");
6
+ };
package/modes/cbc.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { abytes } from "@noble/hashes/utils.js";
2
- import { abytesAligned, assertKalyna, xorBytes } from "../utils.js";
2
+ import { abytesAligned, xorBytes } from "../utils.js";
3
+ import { assertKalyna } from "./_guard.js";
3
4
  /** Cipher Block Chaining (CBC) mode */
4
5
  export const cbc = (cipher, iv) => {
5
6
  assertKalyna(cipher);
package/modes/ccm.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { abytes, anumber, equalBytes, concatBytes } from "@noble/curves/utils.js";
2
- import { assertKalyna } from "../utils.js";
3
2
  import { pad } from "../padding.js";
4
3
  import { half_ctr } from "./gcm.js";
4
+ import { assertKalyna } from "./_guard.js";
5
5
  /** Counter with CBC-MAC (CCM) mode (AEAD) */
6
6
  export const ccm = (cipher, iv, q = 16, Nb = 4) => {
7
7
  assertKalyna(cipher);
package/modes/cfb.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { abytes, anumber, copyBytes } from "@noble/hashes/utils.js";
2
- import { abytesAligned, isKalyna, xorBytes } from "../utils.js";
2
+ import { abytesAligned, xorBytes } from "../utils.js";
3
+ import { isKalyna } from "./_guard.js";
3
4
  /** Cipher Feedback (CFB) mode */
4
5
  export const cfb = (cipher, iv, q = cipher.blockSize) => {
5
6
  const _isKalyna = isKalyna(cipher);
package/modes/cmac.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { anumber, concatBytes, bytesToNumberLE, numberToBytesLE, abytes } from "@noble/curves/utils.js";
2
2
  import { pad } from "../padding.js";
3
- import { isKalyna, xorBytes } from "../utils.js";
3
+ import { xorBytes } from "../utils.js";
4
4
  import { keySequences } from "../dstu9311/const.js";
5
+ import { isKalyna } from "./_guard.js";
5
6
  export const cmac = (cipher, q = 16) => {
6
7
  anumber(q, "q");
7
8
  if (isKalyna(cipher))
package/modes/ctr.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { abytes, createView } from "@noble/hashes/utils.js";
2
- import { isKalyna, xorBytes } from "../utils.js";
2
+ import { xorBytes } from "../utils.js";
3
+ import { isKalyna } from "./_guard.js";
3
4
  const C1 = 0x01010104, C2 = 0x01010101;
4
5
  /** Counter (CTR) mode */
5
6
  export const ctr = (cipher, iv) => {
package/modes/gcm.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { abytes, anumber, equalBytes, concatBytes } from "@noble/curves/utils.js";
2
2
  import { gmac } from "./gmac.js";
3
- import { assertKalyna, xorBytes } from "../utils.js";
3
+ import { xorBytes } from "../utils.js";
4
+ import { assertKalyna } from "./_guard.js";
4
5
  // CTR for GCM and CCM
5
6
  export const half_ctr = (cipher, iv) => {
6
7
  abytes(iv, cipher.blockSize, "iv");
package/modes/gmac.js CHANGED
@@ -1,11 +1,7 @@
1
1
  import { abytes, anumber, numberToBytesLE, concatBytes } from "@noble/curves/utils.js";
2
- import { assertKalyna, gf2mMul } from "../utils.js";
3
- const xorBytesInPlace = (a, b) => {
4
- if (a.length != b.length)
5
- throw new Error("Byte arrays must have same length");
6
- for (let i = 0; i < a.length; i++)
7
- a[i] ^= b[i];
8
- };
2
+ import { xorBytesInPlace } from "../utils.js";
3
+ import { gf2mMultiply } from "../gf/index.js";
4
+ import { assertKalyna } from "./_guard.js";
9
5
  /** Galois Message Authentication Code (GMAC) mode */
10
6
  export const gmac = (cipher, q = 16) => {
11
7
  assertKalyna(cipher);
@@ -29,7 +25,7 @@ export const gmac = (cipher, q = 16) => {
29
25
  block[blockSizeToProcess] = 0x80;
30
26
  for (let j = 0; j < cipher.blockSize; j++)
31
27
  B[j] ^= block[j];
32
- B.set(gf2mMul(cipher.blockSize, B, H));
28
+ B.set(gf2mMultiply(cipher.blockSize, B, H));
33
29
  i += cipher.blockSize;
34
30
  }
35
31
  };
package/modes/kw.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { abytes, concatBytes } from "@noble/hashes/utils.js";
2
2
  import { pad, unpad } from "../padding.js";
3
- import { assertKalyna } from "../utils.js";
3
+ import { assertKalyna } from "./_guard.js";
4
4
  /** Key wrapping (KW) */
5
5
  export const kw = (cipher) => {
6
6
  assertKalyna(cipher);
package/modes/ofb.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { abytes } from "@noble/hashes/utils.js";
2
- import { assertKalyna, xorBytes } from "../utils.js";
2
+ import { xorBytes } from "../utils.js";
3
+ import { assertKalyna } from "./_guard.js";
3
4
  /** Output Feedback (OFB) mode */
4
5
  export const ofb = (cipher, iv) => {
5
6
  assertKalyna(cipher);
package/modes/xts.js CHANGED
@@ -1,24 +1,10 @@
1
- import { abytes, concatBytes, copyBytes } from "@noble/hashes/utils.js";
2
- import { assertKalyna, getGf2mReductionBytes, xorBytes } from "../utils.js";
1
+ import { abytes, concatBytes } from "@noble/hashes/utils.js";
2
+ import { xorBytes } from "../utils.js";
3
+ import { gf2mDouble } from "../gf/index.js";
4
+ import { assertKalyna } from "./_guard.js";
3
5
  /** XEX Tweakable Block Ciphertext Stealing (XTS) */
4
6
  export const xts = (cipher) => {
5
7
  assertKalyna(cipher);
6
- const gf2mDouble = (a) => {
7
- const result = copyBytes(a);
8
- const reductionBytes = getGf2mReductionBytes(cipher.blockSize);
9
- let carry = 0;
10
- for (let j = 0; j < cipher.blockSize; j++) {
11
- const nextCarry = (result[j] & 0x80) ? 1 : 0;
12
- result[j] = ((result[j] << 1) & 0xFF) | carry;
13
- carry = nextCarry;
14
- }
15
- if (carry) {
16
- for (let j = 0; j < reductionBytes.length; j++)
17
- if (j < cipher.blockSize)
18
- result[j] ^= reductionBytes[j];
19
- }
20
- return result;
21
- };
22
8
  const transform = (transform, block, gamma) => xorBytes(transform(xorBytes(block, gamma)), gamma);
23
9
  const init = (iv) => {
24
10
  const gamma = cipher.encrypt(iv);
@@ -39,13 +25,13 @@ export const xts = (cipher) => {
39
25
  const r = plaintext.length % cipher.blockSize;
40
26
  if (r === 0) {
41
27
  for (let off = 0; off < plaintext.length; off += cipher.blockSize) {
42
- gamma.set(gf2mDouble(gamma));
28
+ gamma.set(gf2mDouble(cipher.blockSize, gamma));
43
29
  buffer.set(transform(encrypt, buffer.subarray(off, off + cipher.blockSize), gamma), off);
44
30
  }
45
31
  }
46
32
  else {
47
33
  for (let i = 0; i < k; i++) {
48
- gamma.set(gf2mDouble(gamma));
34
+ gamma.set(gf2mDouble(cipher.blockSize, gamma));
49
35
  const off = i * cipher.blockSize;
50
36
  buffer.set(transform(encrypt, buffer.subarray(off, off + cipher.blockSize), gamma), off);
51
37
  }
@@ -53,7 +39,7 @@ export const xts = (cipher) => {
53
39
  const tail_off = k * cipher.blockSize;
54
40
  const scratch = buffer.slice(last_off, last_off + cipher.blockSize);
55
41
  const combined = concatBytes(buffer.subarray(tail_off, tail_off + r), scratch.subarray(r));
56
- const encrypted = transform(encrypt, combined, gf2mDouble(gamma));
42
+ const encrypted = transform(encrypt, combined, gf2mDouble(cipher.blockSize, gamma));
57
43
  buffer.set(encrypted, last_off);
58
44
  buffer.set(scratch.subarray(0, r), tail_off);
59
45
  }
@@ -71,18 +57,18 @@ export const xts = (cipher) => {
71
57
  const r = ciphertext.length % cipher.blockSize;
72
58
  if (r === 0) {
73
59
  for (let off = 0; off < ciphertext.length; off += cipher.blockSize) {
74
- gamma.set(gf2mDouble(gamma));
60
+ gamma.set(gf2mDouble(cipher.blockSize, gamma));
75
61
  buffer.set(transform(decrypt, buffer.subarray(off, off + cipher.blockSize), gamma), off);
76
62
  }
77
63
  }
78
64
  else {
79
65
  for (let i = 0; i < k - 1; i++) {
80
- gamma.set(gf2mDouble(gamma));
66
+ gamma.set(gf2mDouble(cipher.blockSize, gamma));
81
67
  const off = i * cipher.blockSize;
82
68
  buffer.set(transform(decrypt, buffer.subarray(off, off + cipher.blockSize), gamma), off);
83
69
  }
84
- gamma.set(gf2mDouble(gamma));
85
- const gamma_k_plus_1 = gf2mDouble(gamma);
70
+ gamma.set(gf2mDouble(cipher.blockSize, gamma));
71
+ const gamma_k_plus_1 = gf2mDouble(cipher.blockSize, gamma);
86
72
  const last_off = (k - 1) * cipher.blockSize;
87
73
  const tail_off = k * cipher.blockSize;
88
74
  const combined = transform(decrypt, buffer.subarray(last_off, last_off + cipher.blockSize), gamma_k_plus_1);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@li0ard/dstu",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "type": "module",
@@ -9,10 +9,14 @@
9
9
  "./dstu4145.js": "./dstu4145/index.js",
10
10
  "./dstu9041.js": "./dstu9041/index.js",
11
11
  "./dstu9311.js": "./dstu9311/index.js",
12
+ "./gost34311.js": "./gost34311/index.js",
12
13
  "./kalyna.js": "./kalyna/index.js",
13
14
  "./kupyna.js": "./kupyna/index.js",
14
15
  "./modes.js": "./modes/index.js",
15
16
  "./strumok.js": "./strumok/index.js",
17
+ "./iso15946.js": "./iso15946.js",
18
+ "./keywrap.js": "./keywrap.js",
19
+ "./kmac.js": "./kmac.js",
16
20
  "./types.js": "./types.js"
17
21
  },
18
22
  "author": "li0ard",
@@ -34,21 +38,20 @@
34
38
  "devDependencies": {
35
39
  "@types/bn.js": "^5.2.0",
36
40
  "@types/bun": "latest",
37
- "typedoc": "^0.28.19"
38
- },
39
- "peerDependencies": {
40
- "typescript": "^5"
41
+ "typedoc": "^0.28.20",
42
+ "typescript": "^5.9.3"
41
43
  },
42
44
  "dependencies": {
43
45
  "@noble/curves": "^2.4.0",
44
46
  "@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"
47
+ "bn.js": "^5.2.5",
48
+ "@peculiar/asn1-schema": "^2.11.1",
49
+ "@peculiar/asn1-x509": "^2.11.1"
48
50
  },
49
51
  "files": ["**/*.js", "**/*.d.ts"],
50
52
  "publishConfig": {
51
53
  "access": "public"
52
54
  },
53
- "keywords": ["dstu", "crypto", "kalyna", "kupyna", "strumok"]
55
+ "keywords": ["dstu", "crypto", "kalyna", "kupyna", "strumok"],
56
+ "sideEffects": false
54
57
  }
package/padding.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { type TArg, type TRet } from "@noble/hashes/utils.js";
1
+ import type { TArg, TRet } from "@noble/hashes/utils.js";
2
2
  export declare const pad: (data: TArg<Uint8Array>, blockSize: number) => TRet<Uint8Array>;
3
3
  export declare const unpad: (paddedData: TArg<Uint8Array>, blockSize: number) => TRet<Uint8Array>;
package/padding.js CHANGED
@@ -1,7 +1,4 @@
1
- import { abytes, anumber } from "@noble/hashes/utils.js";
2
1
  export const pad = (data, blockSize) => {
3
- abytes(data, undefined, "data");
4
- anumber(blockSize, "blockSize");
5
2
  const remainder = data.length % blockSize;
6
3
  if (remainder === 0)
7
4
  return data;
@@ -12,8 +9,6 @@ export const pad = (data, blockSize) => {
12
9
  return padded;
13
10
  };
14
11
  export const unpad = (paddedData, blockSize) => {
15
- abytes(paddedData, undefined, "paddedData");
16
- anumber(blockSize, "blockSize");
17
12
  if (paddedData.length % blockSize !== 0)
18
13
  throw new Error("Padded data length must be multiple of block size");
19
14
  let i = paddedData.length - 1;
@@ -6,16 +6,15 @@ export declare class Strumok implements StreamCipher {
6
6
  readonly keySize: number;
7
7
  private S;
8
8
  private r;
9
- private key;
10
- private iv;
9
+ private ks;
11
10
  /**
12
11
  * Strumok stream cipher
13
12
  * @param key Encryption key (256 or 512 bit)
14
13
  */
15
14
  constructor(key: TArg<Uint8Array>, iv: TArg<Uint8Array>);
16
15
  private updateR;
17
- private init;
18
- private round;
16
+ private feed;
17
+ private gen;
19
18
  /** Generate next keystream */
20
19
  next_stream(): TRet<Uint8Array>;
21
20
  /** Perform encryption/decryption */