@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/strumok/index.js CHANGED
@@ -1,27 +1,33 @@
1
1
  import { abytes } from "@noble/hashes/utils.js";
2
- import { ALPHA_MUL, ALPHA_MUL_INV, T as T_ } from "../const.js";
3
- import { bytesToUint64sBE, byte, xorBytes, uint64sToBytesBE } from "../utils.js";
4
- const MAX_UINT64 = (1n << 64n) - 1n;
5
- const not = (w) => MAX_UINT64 - (w & MAX_UINT64);
6
- const a_mul = (w) => (w << 8n) ^ ALPHA_MUL[byte(w >> 56n)];
7
- const ainv_mul = (w) => (w >> 8n) ^ ALPHA_MUL_INV[byte(w)];
8
- const T = (w) => T_[0][byte(w)] ^
9
- T_[1][byte(w >> 8n)] ^
10
- T_[2][byte(w >> 16n)] ^
11
- T_[3][byte(w >> 24n)] ^
12
- T_[4][byte(w >> 32n)] ^
13
- T_[5][byte(w >> 40n)] ^
14
- T_[6][byte(w >> 48n)] ^
15
- T_[7][byte(w >> 56n)];
16
- const S_SIZE = 16;
2
+ import { T as T_ } from "../const.js";
3
+ import { _U32 } from "../utils.js";
4
+ import { gf256Multiply } from "../gf/index.js";
5
+ const loadBE = (b) => {
6
+ const n = b.length >>> 3, out = new Uint32Array(n * 2);
7
+ for (let w = 0, p = 0; w < n; w++, p += 8) {
8
+ out[2 * w + 1] = (b[p] << 24) | (b[p + 1] << 16) | (b[p + 2] << 8) | b[p + 3];
9
+ out[2 * w] = (b[p + 4] << 24) | (b[p + 5] << 16) | (b[p + 6] << 8) | b[p + 7];
10
+ }
11
+ return out;
12
+ };
13
+ const genAlphaTable = (v) => {
14
+ const t = new Uint32Array(512);
15
+ for (let c = 0; c < 256; c++) {
16
+ const m0 = gf256Multiply(c, v[0]), m1 = gf256Multiply(c, v[1]), m2 = gf256Multiply(c, v[2]), m3 = gf256Multiply(c, v[3]), m4 = gf256Multiply(c, v[4]), m5 = gf256Multiply(c, v[5]), m6 = gf256Multiply(c, v[6]), m7 = gf256Multiply(c, v[7]);
17
+ t[2 * c + 1] = (m0 << 24) | (m1 << 16) | (m2 << 8) | m3;
18
+ t[2 * c] = (m4 << 24) | (m5 << 16) | (m6 << 8) | m7;
19
+ }
20
+ return t;
21
+ };
22
+ const AM = /* @__PURE__ */ genAlphaTable([215, 63, 4, 18, 94, 0, 0, 4]), AMI = /* @__PURE__ */ genAlphaTable([71, 252, 198, 1, 138, 153, 0, 0]);
23
+ const S_SIZE = 16, S_MASK = S_SIZE - 1;
17
24
  /** Strumok stream cipher */
18
25
  export class Strumok {
19
26
  blockSize = 128;
20
27
  keySize;
21
28
  S;
22
29
  r;
23
- key;
24
- iv;
30
+ ks;
25
31
  /**
26
32
  * Strumok stream cipher
27
33
  * @param key Encryption key (256 or 512 bit)
@@ -29,90 +35,127 @@ export class Strumok {
29
35
  constructor(key, iv) {
30
36
  abytes(key, undefined, "key");
31
37
  abytes(iv, 32, "iv");
32
- this.key = bytesToUint64sBE(key);
33
- this.iv = bytesToUint64sBE(iv);
34
38
  this.keySize = key.length;
35
- this.S = new BigUint64Array(S_SIZE);
36
- this.r = new BigUint64Array(2);
37
- if (this.keySize == 32) {
38
- this.S[0] = this.key[3] ^ this.iv[0];
39
- this.S[1] = this.key[2];
40
- this.S[2] = this.key[1] ^ this.iv[1];
41
- this.S[3] = this.key[0] ^ this.iv[2];
42
- this.S[4] = this.key[3];
43
- this.S[5] = this.key[2] ^ this.iv[3];
44
- this.S[6] = not(this.key[1]);
45
- this.S[7] = not(this.key[0]);
46
- this.S[8] = this.key[3];
47
- this.S[9] = this.key[2];
48
- this.S[10] = not(this.key[1]);
49
- this.S[11] = this.key[0];
50
- this.S[12] = this.key[3];
51
- this.S[13] = not(this.key[2]);
52
- this.S[14] = this.key[1];
53
- this.S[15] = not(this.key[0]);
39
+ if (this.keySize !== 32 && this.keySize !== 64)
40
+ throw new Error("Unsupported key length");
41
+ const K = loadBE(key), IV = loadBE(iv);
42
+ const S = this.S = new Uint32Array(S_SIZE * 2);
43
+ this.r = new Uint32Array(4);
44
+ this.ks = new Uint8Array(S_SIZE * 8);
45
+ const put = (i, k, v = -1, inv = false) => {
46
+ let lo = K[2 * k], hi = K[2 * k + 1];
47
+ if (inv) {
48
+ lo = ~lo;
49
+ hi = ~hi;
50
+ }
51
+ if (v >= 0) {
52
+ lo ^= IV[2 * v];
53
+ hi ^= IV[2 * v + 1];
54
+ }
55
+ S[2 * i] = lo;
56
+ S[2 * i + 1] = hi;
57
+ };
58
+ if (this.keySize === 32) {
59
+ put(0, 3, 0);
60
+ put(1, 2);
61
+ put(2, 1, 1);
62
+ put(3, 0, 2);
63
+ put(4, 3);
64
+ put(5, 2, 3);
65
+ put(6, 1, -1, true);
66
+ put(7, 0, -1, true);
67
+ put(8, 3);
68
+ put(9, 2);
69
+ put(10, 1, -1, true);
70
+ put(11, 0);
71
+ put(12, 3);
72
+ put(13, 2, -1, true);
73
+ put(14, 1);
74
+ put(15, 0, -1, true);
54
75
  }
55
- else if (this.keySize == 64) {
56
- this.S[0] = this.key[7] ^ this.iv[0];
57
- this.S[1] = this.key[6];
58
- this.S[2] = this.key[5];
59
- this.S[3] = this.key[4] ^ this.iv[1];
60
- this.S[4] = this.key[3];
61
- this.S[5] = this.key[2] ^ this.iv[2];
62
- this.S[6] = this.key[1];
63
- this.S[7] = not(this.key[0]);
64
- this.S[8] = this.key[4] ^ this.iv[3];
65
- this.S[9] = not(this.key[6]);
66
- this.S[10] = this.key[5];
67
- this.S[11] = not(this.key[7]);
68
- this.S[12] = this.key[3];
69
- this.S[13] = this.key[2];
70
- this.S[14] = not(this.key[1]);
71
- this.S[15] = this.key[0];
76
+ else {
77
+ put(0, 7, 0);
78
+ put(1, 6);
79
+ put(2, 5);
80
+ put(3, 4, 1);
81
+ put(4, 3);
82
+ put(5, 2, 2);
83
+ put(6, 1);
84
+ put(7, 0, -1, true);
85
+ put(8, 4, 3);
86
+ put(9, 6, -1, true);
87
+ put(10, 5);
88
+ put(11, 7, -1, true);
89
+ put(12, 3);
90
+ put(13, 2);
91
+ put(14, 1, -1, true);
92
+ put(15, 0);
72
93
  }
73
- else
74
- throw new Error("Unsupported key length");
75
94
  for (let round = 0; round < 2; round++)
76
95
  for (let i = 0; i < S_SIZE; i++)
77
- this.init(i);
96
+ this.feed(i, true);
78
97
  }
79
- updateR(s) {
80
- const r1 = this.r[1];
81
- this.r[1] = T(this.r[0]);
82
- this.r[0] = r1 + this.S[s];
98
+ updateR(j) {
99
+ const S = this.S, r = this.r;
100
+ const l = r[0], h = r[1];
101
+ const l2 = r[2], h2 = r[3];
102
+ const b0 = (l & 0xFF) << 1, b1 = ((l >>> 8) & 0xFF) << 1, b2 = ((l >>> 16) & 0xFF) << 1, b3 = (l >>> 24) << 1, b4 = (h & 0xFF) << 1, b5 = ((h >>> 8) & 0xFF) << 1, b6 = ((h >>> 16) & 0xFF) << 1, b7 = (h >>> 24) << 1;
103
+ const t0 = T_[0], t1 = T_[1], t2 = T_[2], t3 = T_[3], t4 = T_[4], t5 = T_[5], t6 = T_[6], t7 = T_[7];
104
+ r[2] = t0[b0] ^ t1[b1] ^ t2[b2] ^ t3[b3] ^ t4[b4] ^ t5[b5] ^ t6[b6] ^ t7[b7];
105
+ r[3] = t0[b0 | 1] ^ t1[b1 | 1] ^ t2[b2 | 1] ^ t3[b3 | 1] ^
106
+ t4[b4 | 1] ^ t5[b5 | 1] ^ t6[b6 | 1] ^ t7[b7 | 1];
107
+ const lo = l2 + S[j];
108
+ r[1] = h2 + S[j + 1] + (lo > _U32 ? 1 : 0);
109
+ r[0] = lo;
83
110
  }
84
- init(i) {
85
- const prev = (i + 15) % S_SIZE;
86
- const s13 = (i + 13) % S_SIZE;
87
- const s11 = (i + 11) % S_SIZE;
88
- const outfrom_fsm = (this.r[0] + this.S[prev]) ^ this.r[1];
89
- this.S[i] = a_mul(this.S[i]) ^ this.S[s13] ^ ainv_mul(this.S[s11]) ^ outfrom_fsm;
90
- this.updateR(s13);
111
+ feed(i, fsm = false) {
112
+ const S = this.S, r = this.r;
113
+ const j = i << 1, j13 = ((i + 13) & S_MASK) << 1, j11 = ((i + 11) & S_MASK) << 1;
114
+ const l = S[j], h = S[j + 1], ai = (h >>> 24) << 1;
115
+ let lo = (l << 8) ^ AM[ai], hi = ((h << 8) | (l >>> 24)) ^ AM[ai + 1];
116
+ const l11 = S[j11], h11 = S[j11 + 1], bi = (l11 & 0xFF) << 1;
117
+ lo ^= ((l11 >>> 8) | (h11 << 24)) ^ AMI[bi] ^ S[j13];
118
+ hi ^= (h11 >>> 8) ^ AMI[bi + 1] ^ S[j13 + 1];
119
+ if (fsm) {
120
+ const jp = ((i + 15) & S_MASK) << 1, fl = r[0] + S[jp], fh = r[1] + S[jp + 1] + (fl > _U32 ? 1 : 0);
121
+ lo ^= fl ^ r[2];
122
+ hi ^= fh ^ r[3];
123
+ }
124
+ S[j] = lo;
125
+ S[j + 1] = hi;
126
+ this.updateR(j13);
91
127
  }
92
- round(i) {
93
- const s13 = (i + 13) % S_SIZE;
94
- const s11 = (i + 11) % S_SIZE;
95
- this.S[i] = a_mul(this.S[i]) ^ this.S[s13] ^ ainv_mul(this.S[s11]);
96
- this.updateR(s13);
97
- const next = (i + 1) % S_SIZE;
98
- return (this.r[0] + this.S[i]) ^ this.r[1] ^ this.S[next];
128
+ gen(ks) {
129
+ const S = this.S, r = this.r;
130
+ for (let i = 0; i < S_SIZE; i++) {
131
+ this.feed(i);
132
+ const j = i << 1, jn = ((i + 1) & S_MASK) << 1, fl = r[0] + S[j], fh = r[1] + S[j + 1] + (fl > _U32 ? 1 : 0), ol = fl ^ r[2] ^ S[jn], oh = fh ^ r[3] ^ S[jn + 1];
133
+ const p = i << 3;
134
+ ks[p] = oh >>> 24;
135
+ ks[p + 1] = oh >>> 16;
136
+ ks[p + 2] = oh >>> 8;
137
+ ks[p + 3] = oh;
138
+ ks[p + 4] = ol >>> 24;
139
+ ks[p + 5] = ol >>> 16;
140
+ ks[p + 6] = ol >>> 8;
141
+ ks[p + 7] = ol;
142
+ }
99
143
  }
100
144
  /** Generate next keystream */
101
145
  next_stream() {
102
- const out_stream = new BigUint64Array(S_SIZE);
103
- for (let i = 0; i < S_SIZE; i++)
104
- out_stream[i] = this.round(i);
105
- return uint64sToBytesBE(out_stream);
146
+ const out = new Uint8Array(S_SIZE * 8);
147
+ this.gen(out);
148
+ return out;
106
149
  }
107
150
  /** Perform encryption/decryption */
108
151
  crypt(msg) {
109
152
  abytes(msg, undefined, "msg");
110
153
  const out = new Uint8Array(msg.length);
111
- let offset = 0;
112
- while (offset < msg.length) {
113
- const chunk = xorBytes(msg.subarray(offset), this.next_stream());
114
- out.set(chunk, offset);
115
- offset += chunk.length;
154
+ for (let offset = 0; offset < msg.length; offset += this.ks.length) {
155
+ this.gen(this.ks);
156
+ const n = Math.min(this.ks.length, msg.length - offset);
157
+ for (let k = 0; k < n; k++)
158
+ out[offset + k] = msg[offset + k] ^ this.ks[k];
116
159
  }
117
160
  return out;
118
161
  }
package/utils.d.ts CHANGED
@@ -1,15 +1,8 @@
1
- import { type TArg, type TRet } from "@noble/curves/utils.js";
2
- import type { Cipher } from "./types.js";
3
- import { Kalyna } from "./kalyna/index.js";
1
+ import { type TArg, type TRet } from "@noble/hashes/utils.js";
2
+ export declare const _U32 = 4294967295, _NULL: Uint8Array<ArrayBuffer>;
4
3
  export declare const xorBytes: (a: TArg<Uint8Array>, b: TArg<Uint8Array>) => TRet<Uint8Array>;
5
- export declare const bytesToUint64sBE: (b: TArg<Uint8Array>) => TRet<BigUint64Array>;
6
- export declare const uint64sToBytesBE: (w: TArg<BigUint64Array>) => TRet<Uint8Array>;
7
- export declare const bytesToUint64sLE: (b: TArg<Uint8Array>) => TRet<BigUint64Array>;
8
- export declare const uint64sToBytesLE: (w: TArg<BigUint64Array>) => TRet<Uint8Array>;
9
- export declare const byte: (a: bigint) => number;
10
- export declare const getGf2mReductionBytes: (blockSize: number) => number[];
11
- export declare const gf2mMul: (blockSize: number, a: TArg<Uint8Array>, b: TArg<Uint8Array>) => TRet<Uint8Array>;
12
- export declare const isKalyna: (cipher: Cipher) => cipher is Kalyna;
13
- export declare const assertKalyna: (cipher: Cipher) => void;
14
- export declare const reverseBytes: (bytes: TArg<Uint8Array>) => TRet<Uint8Array>;
4
+ export declare const xorBytesInPlace: (dst: TArg<Uint8Array>, src: TArg<Uint8Array>, off?: number) => void;
5
+ export declare const reverseBytes: (bytes: TArg<Uint8Array>, from?: number, to?: number) => TRet<Uint8Array>;
15
6
  export declare const abytesAligned: (value: TArg<Uint8Array>, blockSize: number, title?: string) => TRet<Uint8Array>;
7
+ export declare const bytesToU32LE: (b: Uint8Array) => Uint32Array;
8
+ export declare const u32ToBytesLE: (w: Uint32Array) => Uint8Array;
package/utils.js CHANGED
@@ -1,84 +1,41 @@
1
- import { copyBytes, bytesToNumberBE, bytesToNumberLE, numberToBytesBE, numberToBytesLE, abytes, anumber } from "@noble/curves/utils.js";
2
- import { Kalyna } from "./kalyna/index.js";
1
+ import { abytes, anumber } from "@noble/hashes/utils.js";
2
+ export const _U32 = 0xFFFFFFFF, _NULL = /* @__PURE__ */ Uint8Array.of();
3
3
  export const xorBytes = (a, b) => {
4
- abytes(a);
5
- abytes(b);
6
- const mlen = Math.min(a.length, b.length);
7
- const result = new Uint8Array(mlen);
4
+ const mlen = Math.min(a.length, b.length), result = new Uint8Array(mlen);
8
5
  for (let i = 0; i < mlen; i++)
9
6
  result[i] = a[i] ^ b[i];
10
7
  return result;
11
8
  };
12
- export const bytesToUint64sBE = (b) => {
13
- const size = Math.floor(b.length / 8);
14
- const result = new BigUint64Array(size);
15
- for (let i = 0; i < size; i++)
16
- result[i] = bytesToNumberBE(b.subarray(i * 8, i * 8 + 8));
17
- return result;
18
- };
19
- export const uint64sToBytesBE = (w) => {
20
- const result = new Uint8Array(w.length * 8);
21
- for (let i = 0; i < w.length; i++)
22
- result.set(numberToBytesBE(w[i], 8), i * 8);
23
- return result;
24
- };
25
- export const bytesToUint64sLE = (b) => {
26
- const size = Math.floor(b.length / 8);
27
- const result = new BigUint64Array(size);
28
- for (let i = 0; i < size; i++)
29
- result[i] = bytesToNumberLE(b.subarray(i * 8, i * 8 + 8));
30
- return result;
31
- };
32
- export const uint64sToBytesLE = (w) => {
33
- const result = new Uint8Array(w.length * 8);
34
- for (let i = 0; i < w.length; i++)
35
- result.set(numberToBytesLE(w[i], 8), i * 8);
36
- return result;
9
+ export const xorBytesInPlace = (dst, src, off = 0) => {
10
+ if (off + dst.length > src.length)
11
+ throw new Error("src is too short");
12
+ for (let i = 0; i < dst.length; i++)
13
+ dst[i] ^= src[off + i];
37
14
  };
38
- export const byte = (a) => Number(a & 0xffn);
39
- export const getGf2mReductionBytes = (blockSize) => [
40
- [0x87],
41
- [0x25, 0x04],
42
- [0x25, 0x01]
43
- ][Math.log2(blockSize) - 4];
44
- export const gf2mMul = (blockSize, a, b) => {
45
- const temp = copyBytes(a);
46
- const result = new Uint8Array(blockSize);
47
- const reductionBytes = getGf2mReductionBytes(blockSize);
48
- for (let i = 0; i < blockSize * 8; i++) {
49
- const byteIndex = Math.floor(i / 8);
50
- const bitIndex = i % 8;
51
- if (byteIndex < b.length && (b[byteIndex] & (1 << bitIndex)))
52
- for (let j = 0; j < blockSize; j++)
53
- result[j] ^= temp[j];
54
- let carry = 0;
55
- for (let j = 0; j < blockSize; j++) {
56
- const nextCarry = (temp[j] & 0x80) ? 1 : 0;
57
- temp[j] = ((temp[j] << 1) & 0xFF) | carry;
58
- carry = nextCarry;
59
- }
60
- if (carry) {
61
- for (let j = 0; j < reductionBytes.length; j++)
62
- if (j < blockSize)
63
- temp[j] ^= reductionBytes[j];
64
- }
65
- }
66
- return result;
67
- };
68
- export const isKalyna = (cipher) => cipher instanceof Kalyna;
69
- export const assertKalyna = (cipher) => {
70
- if (!isKalyna(cipher))
71
- throw new Error("Cipher mode only for Kalyna");
72
- };
73
- export const reverseBytes = (bytes) => copyBytes(bytes).reverse();
15
+ export const reverseBytes = (bytes, from, to) => bytes.slice(from, to).reverse();
74
16
  const atitle = (title) => title ? `"${title}" ` : '';
75
17
  export const abytesAligned = (value, blockSize, title = "") => {
76
18
  abytes(value);
77
19
  anumber(blockSize);
78
20
  if (value.length !== 0 && value.length % blockSize === 0)
79
21
  return value;
80
- const ofLen = ` of length aligned to ${blockSize}`;
81
- const got = `length=${value.length}`;
82
- const message = atitle(title) + 'expected Uint8Array' + ofLen + ', got ' + got;
22
+ const ofLen = ` of length aligned to ${blockSize}`, got = `length=${value.length}`, message = atitle(title) + 'expected Uint8Array' + ofLen + ', got ' + got;
83
23
  throw new RangeError(message);
84
24
  };
25
+ export const bytesToU32LE = (b) => {
26
+ const out = new Uint32Array(b.length >>> 2);
27
+ for (let i = 0, j = 0; i < out.length; i++, j += 4)
28
+ out[i] = b[j] | (b[j + 1] << 8) | (b[j + 2] << 16) | (b[j + 3] << 24);
29
+ return out;
30
+ };
31
+ export const u32ToBytesLE = (w) => {
32
+ const out = new Uint8Array(w.length << 2);
33
+ for (let i = 0, j = 0; i < w.length; i++, j += 4) {
34
+ const v = w[i];
35
+ out[j] = v;
36
+ out[j + 1] = v >>> 8;
37
+ out[j + 2] = v >>> 16;
38
+ out[j + 3] = v >>> 24;
39
+ }
40
+ return out;
41
+ };
@@ -1,75 +0,0 @@
1
- import { type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
2
- import { _HMAC } from "@noble/hashes/hmac.js";
3
- /** Internal GOST 34.311-95 hash class */
4
- export declare class _Gost3431195 implements Hash<_Gost3431195> {
5
- private sbox;
6
- readonly blockLen = 32;
7
- readonly outputLen = 32;
8
- readonly canXOF = false;
9
- private buffer;
10
- private pos;
11
- private h;
12
- private len;
13
- private checksum;
14
- /** Internal GOST 34.311-95 hash class */
15
- constructor(sbox?: TArg<Uint8Array>);
16
- /** Create hash instance */
17
- static create(): _Gost3431195;
18
- destroy(): void;
19
- clone(): _Gost3431195;
20
- _cloneInto(to?: _Gost3431195): _Gost3431195;
21
- private processBlock;
22
- update(data: TArg<Uint8Array>): this;
23
- digestInto(buf: TArg<Uint8Array>): void;
24
- digest(): TRet<Uint8Array>;
25
- }
26
- /**
27
- * GOST 34.311-95 hash function
28
- *
29
- * @param msg - message bytes to hash.
30
- * @returns Digest bytes.
31
- * @example
32
- * ```ts
33
- * import { gost3431195 } from "@li0ard/dstu/dstu9311.js";
34
- *
35
- * gost3431195(new Uint8Array([97, 98, 99]));
36
- * gost3431195.create(sbox?).update(new Uint8Array([97, 98, 99])).digest();
37
- * ```
38
- */
39
- export declare const gost3431195: {
40
- outputLen: number;
41
- blockLen: number;
42
- canXOF: boolean;
43
- } & import("@noble/hashes/utils.js").HashInfo & {
44
- (msg: TArg<Uint8Array>): TRet<Uint8Array>;
45
- create(): _Gost3431195;
46
- } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
47
- outputLen: number;
48
- blockLen: number;
49
- canXOF: boolean;
50
- oid?: TRet<Uint8Array> | undefined;
51
- create: () => _Gost3431195;
52
- };
53
- /**
54
- * HMAC over GOST 34.311-95 hash function
55
- *
56
- * @param key - authentication key bytes
57
- */
58
- export declare class Gost3431195HMAC extends _HMAC<_Gost3431195> {
59
- constructor(key: TArg<Uint8Array>);
60
- }
61
- /**
62
- * HMAC over GOST 34.311-95 hash function
63
- *
64
- * @param key - authentication key bytes
65
- * @param message - message bytes to authenticate
66
- * @returns Authentication tag bytes.
67
- * @example
68
- * ```ts
69
- * import { gost3431195Hmac } from "@li0ard/dstu/dstu9311.js";
70
- * const key = new Uint8Array([1, 2, 3]);
71
- * const message = new Uint8Array([4, 5, 6]);
72
- * const mac = gost3431195Hmac(key, message);
73
- * ```
74
- */
75
- export declare const gost3431195Hmac: (key: TArg<Uint8Array>, msg: TArg<Uint8Array>) => TRet<Uint8Array>;
package/dstu9311/hash.js DELETED
@@ -1,159 +0,0 @@
1
- import { abytes, aoutput, clean, concatBytes, copyBytes, createHasher } from "@noble/hashes/utils.js";
2
- import { Dstu9311, DKE_1 } from "../dstu9311/index.js";
3
- import { bytesToNumberBE, numberToBytesBE } from "@noble/curves/utils.js";
4
- import { xorBytes } from "../utils.js";
5
- import { _HMAC } from "@noble/hashes/hmac.js";
6
- const r = (1n << 256n) - 1n;
7
- const C3 = new Uint8Array([
8
- 0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0xff,
9
- 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0xff, 0x00,
10
- 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
11
- 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00
12
- ]);
13
- const A = (x) => concatBytes(xorBytes(x.subarray(24, 32), x.subarray(16, 24)), x.subarray(0, 24));
14
- const P = (x) => new Uint8Array([
15
- x[31], x[23], x[15], x[7], x[30], x[22], x[14], x[6],
16
- x[29], x[21], x[13], x[5], x[28], x[20], x[12], x[4],
17
- x[27], x[19], x[11], x[3], x[26], x[18], x[10], x[2],
18
- x[25], x[17], x[9], x[1], x[24], x[16], x[8], x[0]
19
- ]);
20
- const chi = (Y) => new Uint8Array([
21
- Y[30] ^ Y[28] ^ Y[26] ^ Y[24] ^ Y[6] ^ Y[0],
22
- Y[31] ^ Y[29] ^ Y[27] ^ Y[25] ^ Y[7] ^ Y[1],
23
- ...Y.subarray(0, 30)
24
- ]);
25
- const _getCipher = (u, v, sbox) => new Dstu9311(P(xorBytes(u, v)), sbox);
26
- const _step = (hin, m, sbox) => {
27
- const k1 = _getCipher(hin, m, sbox);
28
- let u = A(hin), v = A(A(m));
29
- const k2 = _getCipher(u, v, sbox);
30
- u = xorBytes(A(u), C3), v = A(A(v));
31
- const k3 = _getCipher(u, v, sbox);
32
- u = A(u), v = A(A(v));
33
- const k4 = _getCipher(u, v, sbox);
34
- const x = concatBytes(k4.encrypt(hin.slice(0, 8).reverse()).reverse(), k3.encrypt(hin.slice(8, 16).reverse()).reverse(), k2.encrypt(hin.slice(16, 24).reverse()).reverse(), k1.encrypt(hin.slice(24, 32).reverse()).reverse());
35
- for (let i = 0; i < 12; i++)
36
- x.set(chi(x));
37
- x.set(xorBytes(hin, chi(xorBytes(x, m))));
38
- for (let i = 0; i < 61; i++)
39
- x.set(chi(x));
40
- return x;
41
- };
42
- /** Internal GOST 34.311-95 hash class */
43
- export class _Gost3431195 {
44
- sbox;
45
- blockLen = 32;
46
- outputLen = 32;
47
- canXOF = false;
48
- buffer = new Uint8Array(32);
49
- pos = 0;
50
- h = new Uint8Array(32);
51
- len = 0n;
52
- checksum = 0n;
53
- /** Internal GOST 34.311-95 hash class */
54
- constructor(sbox = DKE_1) {
55
- this.sbox = sbox;
56
- abytes(sbox, 64, "sbox");
57
- }
58
- /** Create hash instance */
59
- static create() { return new _Gost3431195(); }
60
- destroy() {
61
- clean(this.buffer, this.h);
62
- this.pos = 0;
63
- this.len = 0n;
64
- this.checksum = 0n;
65
- }
66
- clone() { return this._cloneInto(); }
67
- _cloneInto(to) {
68
- to ||= new _Gost3431195();
69
- to.sbox = this.sbox;
70
- to.buffer.set(this.buffer);
71
- to.pos = this.pos;
72
- to.h.set(this.h);
73
- to.len = this.len;
74
- to.checksum = this.checksum;
75
- return to;
76
- }
77
- processBlock(block) {
78
- const rev = copyBytes(block).reverse();
79
- this.len += 256n;
80
- this.checksum = (this.checksum + bytesToNumberBE(rev)) & r;
81
- this.h.set(_step(this.h, rev, this.sbox));
82
- }
83
- update(data) {
84
- abytes(data);
85
- let offset = 0;
86
- if (this.pos > 0) {
87
- const take = Math.min(this.blockLen - this.pos, data.length);
88
- this.buffer.set(data.subarray(0, take), this.pos);
89
- this.pos += take;
90
- offset = take;
91
- if (this.pos === this.blockLen) {
92
- this.processBlock(this.buffer);
93
- this.pos = 0;
94
- }
95
- }
96
- for (; offset + this.blockLen <= data.length; offset += this.blockLen)
97
- this.processBlock(data.subarray(offset, offset + this.blockLen));
98
- if (offset < data.length) {
99
- this.buffer.set(data.subarray(offset), 0);
100
- this.pos = data.length - offset;
101
- }
102
- return this;
103
- }
104
- digestInto(buf) {
105
- aoutput(buf, this);
106
- if (this.pos > 0) {
107
- const part = new Uint8Array(this.blockLen);
108
- part.set(this.buffer.slice(0, this.pos).reverse(), this.blockLen - this.pos);
109
- this.len += BigInt(this.pos) * 8n;
110
- this.checksum = (this.checksum + bytesToNumberBE(part)) & r;
111
- this.h.set(_step(this.h, part, this.sbox));
112
- }
113
- const res = _step(_step(this.h, numberToBytesBE(this.len, this.blockLen), this.sbox), numberToBytesBE(this.checksum, this.blockLen), this.sbox);
114
- buf.set(res.reverse());
115
- this.destroy();
116
- }
117
- digest() {
118
- const buffer = new Uint8Array(this.outputLen);
119
- this.digestInto(buffer);
120
- return buffer;
121
- }
122
- }
123
- /**
124
- * GOST 34.311-95 hash function
125
- *
126
- * @param msg - message bytes to hash.
127
- * @returns Digest bytes.
128
- * @example
129
- * ```ts
130
- * import { gost3431195 } from "@li0ard/dstu/dstu9311.js";
131
- *
132
- * gost3431195(new Uint8Array([97, 98, 99]));
133
- * gost3431195.create(sbox?).update(new Uint8Array([97, 98, 99])).digest();
134
- * ```
135
- */
136
- export const gost3431195 = createHasher(_Gost3431195.create);
137
- /**
138
- * HMAC over GOST 34.311-95 hash function
139
- *
140
- * @param key - authentication key bytes
141
- */
142
- export class Gost3431195HMAC extends _HMAC {
143
- constructor(key) { super(gost3431195, key); }
144
- }
145
- /**
146
- * HMAC over GOST 34.311-95 hash function
147
- *
148
- * @param key - authentication key bytes
149
- * @param message - message bytes to authenticate
150
- * @returns Authentication tag bytes.
151
- * @example
152
- * ```ts
153
- * import { gost3431195Hmac } from "@li0ard/dstu/dstu9311.js";
154
- * const key = new Uint8Array([1, 2, 3]);
155
- * const message = new Uint8Array([4, 5, 6]);
156
- * const mac = gost3431195Hmac(key, message);
157
- * ```
158
- */
159
- export const gost3431195Hmac = (key, msg) => new Gost3431195HMAC(key).update(msg).digest();
package/kupyna/kmac.d.ts DELETED
@@ -1,46 +0,0 @@
1
- import { type CHash, type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
2
- import { type _Kupyna256, type _Kupyna384, type _Kupyna512 } from "./index.js";
3
- declare abstract class KupynaKMAC<T, H extends Hash<H>> implements Hash<KupynaKMAC<T, H>> {
4
- readonly outputLen: number;
5
- readonly blockLen: number;
6
- readonly canXOF = false;
7
- protected readonly threshold: bigint;
8
- protected h: H;
9
- protected ik: Uint8Array;
10
- protected len: bigint;
11
- constructor(hash: CHash<H>, kpad: TArg<Uint8Array>, key: TArg<Uint8Array>);
12
- abstract clone(): KupynaKMAC<T, H>;
13
- abstract _cloneInto(to?: KupynaKMAC<T, H>): KupynaKMAC<T, H>;
14
- update(data: TArg<Uint8Array>): this;
15
- digest(): TRet<Uint8Array>;
16
- digestInto(buffer: TArg<Uint8Array>): void;
17
- destroy(): void;
18
- }
19
- /** Kupyna KMAC (256 version) */
20
- export declare class KupynaKMAC256 extends KupynaKMAC<KupynaKMAC256, _Kupyna256> {
21
- private key;
22
- constructor(key: TArg<Uint8Array>);
23
- _cloneInto(to?: KupynaKMAC256): KupynaKMAC256;
24
- clone(): KupynaKMAC256;
25
- }
26
- /** Kupyna KMAC (384 bit version) */
27
- export declare class KupynaKMAC384 extends KupynaKMAC<KupynaKMAC512, _Kupyna384> {
28
- private key;
29
- constructor(key: TArg<Uint8Array>);
30
- _cloneInto(to?: KupynaKMAC384): KupynaKMAC384;
31
- clone(): KupynaKMAC384;
32
- }
33
- /** Kupyna KMAC (512 bit version) */
34
- export declare class KupynaKMAC512 extends KupynaKMAC<KupynaKMAC512, _Kupyna512> {
35
- private key;
36
- constructor(key: TArg<Uint8Array>);
37
- _cloneInto(to?: KupynaKMAC512): KupynaKMAC512;
38
- clone(): KupynaKMAC512;
39
- }
40
- /** Kupyna KMAC (256 version) */
41
- export declare const kmac256: (key: TArg<Uint8Array>, msg: TArg<Uint8Array>) => TRet<Uint8Array>;
42
- /** Kupyna KMAC (384 version) */
43
- export declare const kmac384: (key: TArg<Uint8Array>, msg: TArg<Uint8Array>) => TRet<Uint8Array>;
44
- /** Kupyna KMAC (512 version) */
45
- export declare const kmac512: (key: TArg<Uint8Array>, msg: TArg<Uint8Array>) => TRet<Uint8Array>;
46
- export {};