@li0ard/dstu 0.1.9 → 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 (55) hide show
  1. package/README.md +1 -1
  2. package/const.d.ts +5 -14
  3. package/const.js +47 -1010
  4. package/dstu4145/const.js +15 -15
  5. package/dstu4145/ec/index.d.ts +4 -0
  6. package/dstu4145/ec/index.js +28 -27
  7. package/dstu4145/ecies.d.ts +2 -2
  8. package/dstu4145/ecies.js +13 -2
  9. package/dstu4145/gf2m/utils.js +1 -1
  10. package/dstu4145/index.js +26 -17
  11. package/dstu9041/curve.d.ts +1 -1
  12. package/dstu9041/curve.js +16 -29
  13. package/dstu9041/index.js +21 -25
  14. package/dstu9311/const.js +12 -12
  15. package/dstu9311/index.d.ts +1 -2
  16. package/dstu9311/index.js +37 -34
  17. package/gf/index.d.ts +5 -0
  18. package/gf/index.js +62 -0
  19. package/gost34311/index.d.ts +55 -0
  20. package/gost34311/index.js +167 -0
  21. package/index.d.ts +1 -0
  22. package/index.js +1 -0
  23. package/iso15946.d.ts +1 -1
  24. package/iso15946.js +11 -6
  25. package/kalyna/index.d.ts +3 -5
  26. package/kalyna/index.js +119 -98
  27. package/keywrap.js +11 -13
  28. package/kupyna/index.d.ts +100 -133
  29. package/kupyna/index.js +192 -180
  30. package/modes/_guard.d.ts +4 -0
  31. package/modes/_guard.js +6 -0
  32. package/modes/cbc.d.ts +1 -1
  33. package/modes/cbc.js +6 -7
  34. package/modes/ccm.js +13 -4
  35. package/modes/cfb.js +43 -51
  36. package/modes/cmac.js +6 -2
  37. package/modes/ctr.js +6 -4
  38. package/modes/ecb.js +2 -2
  39. package/modes/gcm.d.ts +4 -1
  40. package/modes/gcm.js +33 -5
  41. package/modes/gmac.js +8 -9
  42. package/modes/index.d.ts +1 -1
  43. package/modes/index.js +1 -1
  44. package/modes/kw.js +10 -17
  45. package/modes/ofb.js +5 -4
  46. package/modes/xts.js +15 -25
  47. package/package.json +12 -9
  48. package/strumok/index.d.ts +4 -5
  49. package/strumok/index.js +132 -87
  50. package/utils.d.ts +7 -13
  51. package/utils.js +31 -61
  52. package/dstu9311/hash.d.ts +0 -41
  53. package/dstu9311/hash.js +0 -98
  54. package/kupyna/kmac.d.ts +0 -46
  55. package/kupyna/kmac.js +0 -125
package/strumok/index.js CHANGED
@@ -1,116 +1,161 @@
1
- import { ALPHA_MUL, ALPHA_MUL_INV, T as T_ } from "../const.js";
2
- import { bytesToUint64sBE, byte, xorBytes, uint64sToBytesBE } from "../utils.js";
3
- const MAX_UINT64 = (1n << 64n) - 1n;
4
- const not = (w) => MAX_UINT64 - (w & MAX_UINT64);
5
- const a_mul = (w) => (w << 8n) ^ ALPHA_MUL[byte(w >> 56n)];
6
- const ainv_mul = (w) => (w >> 8n) ^ ALPHA_MUL_INV[byte(w)];
7
- const T = (w) => T_[0][byte(w)] ^
8
- T_[1][byte(w >> 8n)] ^
9
- T_[2][byte(w >> 16n)] ^
10
- T_[3][byte(w >> 24n)] ^
11
- T_[4][byte(w >> 32n)] ^
12
- T_[5][byte(w >> 40n)] ^
13
- T_[6][byte(w >> 48n)] ^
14
- T_[7][byte(w >> 56n)];
15
- const S_SIZE = 16;
1
+ import { abytes } from "@noble/hashes/utils.js";
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;
16
24
  /** Strumok stream cipher */
17
25
  export class Strumok {
18
26
  blockSize = 128;
19
27
  keySize;
20
28
  S;
21
29
  r;
22
- key;
23
- iv;
30
+ ks;
24
31
  /**
25
32
  * Strumok stream cipher
26
33
  * @param key Encryption key (256 or 512 bit)
27
34
  */
28
35
  constructor(key, iv) {
29
- if (iv.length !== 32)
30
- throw new Error("Unsupported IV length");
31
- this.key = bytesToUint64sBE(key);
32
- this.iv = bytesToUint64sBE(iv);
36
+ abytes(key, undefined, "key");
37
+ abytes(iv, 32, "iv");
33
38
  this.keySize = key.length;
34
- this.S = new BigUint64Array(S_SIZE);
35
- this.r = new BigUint64Array(2);
36
- if (this.keySize == 32) {
37
- this.S[0] = this.key[3] ^ this.iv[0];
38
- this.S[1] = this.key[2];
39
- this.S[2] = this.key[1] ^ this.iv[1];
40
- this.S[3] = this.key[0] ^ this.iv[2];
41
- this.S[4] = this.key[3];
42
- this.S[5] = this.key[2] ^ this.iv[3];
43
- this.S[6] = not(this.key[1]);
44
- this.S[7] = not(this.key[0]);
45
- this.S[8] = this.key[3];
46
- this.S[9] = this.key[2];
47
- this.S[10] = not(this.key[1]);
48
- this.S[11] = this.key[0];
49
- this.S[12] = this.key[3];
50
- this.S[13] = not(this.key[2]);
51
- this.S[14] = this.key[1];
52
- 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);
53
75
  }
54
- else if (this.keySize == 64) {
55
- this.S[0] = this.key[7] ^ this.iv[0];
56
- this.S[1] = this.key[6];
57
- this.S[2] = this.key[5];
58
- this.S[3] = this.key[4] ^ this.iv[1];
59
- this.S[4] = this.key[3];
60
- this.S[5] = this.key[2] ^ this.iv[2];
61
- this.S[6] = this.key[1];
62
- this.S[7] = not(this.key[0]);
63
- this.S[8] = this.key[4] ^ this.iv[3];
64
- this.S[9] = not(this.key[6]);
65
- this.S[10] = this.key[5];
66
- this.S[11] = not(this.key[7]);
67
- this.S[12] = this.key[3];
68
- this.S[13] = this.key[2];
69
- this.S[14] = not(this.key[1]);
70
- 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);
71
93
  }
72
- else
73
- throw new Error("Unsupported key length");
74
94
  for (let round = 0; round < 2; round++)
75
95
  for (let i = 0; i < S_SIZE; i++)
76
- this.init(i);
96
+ this.feed(i, true);
77
97
  }
78
- updateR(s) {
79
- const r1 = this.r[1];
80
- this.r[1] = T(this.r[0]);
81
- 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;
82
110
  }
83
- init(i) {
84
- const prev = (i + 15) % S_SIZE;
85
- const s13 = (i + 13) % S_SIZE;
86
- const s11 = (i + 11) % S_SIZE;
87
- const outfrom_fsm = (this.r[0] + this.S[prev]) ^ this.r[1];
88
- this.S[i] = a_mul(this.S[i]) ^ this.S[s13] ^ ainv_mul(this.S[s11]) ^ outfrom_fsm;
89
- 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);
90
127
  }
91
- round(i) {
92
- const s13 = (i + 13) % S_SIZE;
93
- const s11 = (i + 11) % S_SIZE;
94
- this.S[i] = a_mul(this.S[i]) ^ this.S[s13] ^ ainv_mul(this.S[s11]);
95
- this.updateR(s13);
96
- const next = (i + 1) % S_SIZE;
97
- 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
+ }
98
143
  }
99
144
  /** Generate next keystream */
100
145
  next_stream() {
101
- const out_stream = new BigUint64Array(S_SIZE);
102
- for (let i = 0; i < S_SIZE; i++)
103
- out_stream[i] = this.round(i);
104
- return uint64sToBytesBE(out_stream);
146
+ const out = new Uint8Array(S_SIZE * 8);
147
+ this.gen(out);
148
+ return out;
105
149
  }
106
150
  /** Perform encryption/decryption */
107
151
  crypt(msg) {
152
+ abytes(msg, undefined, "msg");
108
153
  const out = new Uint8Array(msg.length);
109
- let offset = 0;
110
- while (offset < msg.length) {
111
- const chunk = xorBytes(msg.subarray(offset), this.next_stream());
112
- out.set(chunk, offset);
113
- 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];
114
159
  }
115
160
  return out;
116
161
  }
package/utils.d.ts CHANGED
@@ -1,14 +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>;
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,71 +1,41 @@
1
- import { copyBytes, bytesToNumberBE, bytesToNumberLE, numberToBytesBE, numberToBytesLE } 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
- const mlen = Math.min(a.length, b.length);
5
- const result = new Uint8Array(mlen);
4
+ const mlen = Math.min(a.length, b.length), result = new Uint8Array(mlen);
6
5
  for (let i = 0; i < mlen; i++)
7
6
  result[i] = a[i] ^ b[i];
8
7
  return result;
9
8
  };
10
- export const bytesToUint64sBE = (b) => {
11
- const size = Math.floor(b.length / 8);
12
- const result = new BigUint64Array(size);
13
- for (let i = 0; i < size; i++)
14
- result[i] = bytesToNumberBE(b.subarray(i * 8, i * 8 + 8));
15
- 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];
16
14
  };
17
- export const uint64sToBytesBE = (w) => {
18
- const result = new Uint8Array(w.length * 8);
19
- for (let i = 0; i < w.length; i++)
20
- result.set(numberToBytesBE(w[i], 8), i * 8);
21
- return result;
15
+ export const reverseBytes = (bytes, from, to) => bytes.slice(from, to).reverse();
16
+ const atitle = (title) => title ? `"${title}" ` : '';
17
+ export const abytesAligned = (value, blockSize, title = "") => {
18
+ abytes(value);
19
+ anumber(blockSize);
20
+ if (value.length !== 0 && value.length % blockSize === 0)
21
+ return value;
22
+ const ofLen = ` of length aligned to ${blockSize}`, got = `length=${value.length}`, message = atitle(title) + 'expected Uint8Array' + ofLen + ', got ' + got;
23
+ throw new RangeError(message);
22
24
  };
23
- export const bytesToUint64sLE = (b) => {
24
- const size = Math.floor(b.length / 8);
25
- const result = new BigUint64Array(size);
26
- for (let i = 0; i < size; i++)
27
- result[i] = bytesToNumberLE(b.subarray(i * 8, i * 8 + 8));
28
- return result;
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;
29
30
  };
30
- export const uint64sToBytesLE = (w) => {
31
- const result = new Uint8Array(w.length * 8);
32
- for (let i = 0; i < w.length; i++)
33
- result.set(numberToBytesLE(w[i], 8), i * 8);
34
- return result;
35
- };
36
- export const byte = (a) => Number(a & 0xffn);
37
- export const getGf2mReductionBytes = (blockSize) => [
38
- [0x87],
39
- [0x25, 0x04],
40
- [0x25, 0x01]
41
- ][Math.log2(blockSize) - 4];
42
- export const gf2mMul = (blockSize, a, b) => {
43
- const temp = copyBytes(a);
44
- const result = new Uint8Array(blockSize);
45
- const reductionBytes = getGf2mReductionBytes(blockSize);
46
- for (let i = 0; i < blockSize * 8; i++) {
47
- const byteIndex = Math.floor(i / 8);
48
- const bitIndex = i % 8;
49
- if (byteIndex < b.length && (b[byteIndex] & (1 << bitIndex)))
50
- for (let j = 0; j < blockSize; j++)
51
- result[j] ^= temp[j];
52
- let carry = 0;
53
- for (let j = 0; j < blockSize; j++) {
54
- const nextCarry = (temp[j] & 0x80) ? 1 : 0;
55
- temp[j] = ((temp[j] << 1) & 0xFF) | carry;
56
- carry = nextCarry;
57
- }
58
- if (carry) {
59
- for (let j = 0; j < reductionBytes.length; j++)
60
- if (j < blockSize)
61
- temp[j] ^= reductionBytes[j];
62
- }
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;
63
39
  }
64
- return result;
65
- };
66
- export const isKalyna = (cipher) => cipher instanceof Kalyna;
67
- export const assertKalyna = (cipher) => {
68
- if (!isKalyna(cipher))
69
- throw new Error("Cipher mode only for Kalyna");
40
+ return out;
70
41
  };
71
- export const reverseBytes = (bytes) => copyBytes(bytes).reverse();
@@ -1,41 +0,0 @@
1
- import { type Hash, type TArg, type TRet } from "@noble/hashes/utils.js";
2
- import { _HMAC } from "@noble/hashes/hmac.js";
3
- /** GOST 34.311-95 hash function */
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
- /** GOST 34.311-95 hash function */
11
- constructor(sbox?: TArg<Uint8Array>);
12
- /** Create hash instance */
13
- static create(): Gost3431195;
14
- destroy(): void;
15
- clone(): Gost3431195;
16
- _cloneInto(to?: Gost3431195): Gost3431195;
17
- update(data: TArg<Uint8Array>): this;
18
- digestInto(buf: TArg<Uint8Array>): void;
19
- digest(): TRet<Uint8Array>;
20
- }
21
- /** GOST 34.311-95 hash function */
22
- export declare const gost3431195: {
23
- outputLen: number;
24
- blockLen: number;
25
- canXOF: boolean;
26
- } & import("@noble/hashes/utils.js").HashInfo & {
27
- (msg: TArg<Uint8Array>): TRet<Uint8Array>;
28
- create(): Gost3431195;
29
- } & ((msg: TArg<TArg<Uint8Array<ArrayBufferLike>>>) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>) & {
30
- outputLen: number;
31
- blockLen: number;
32
- canXOF: boolean;
33
- oid?: TRet<Uint8Array> | undefined;
34
- create: () => Gost3431195;
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 DELETED
@@ -1,98 +0,0 @@
1
- import { 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
- /** GOST 34.311-95 hash function */
43
- export class Gost3431195 {
44
- sbox;
45
- blockLen = 32;
46
- outputLen = 32;
47
- canXOF = false;
48
- buffer;
49
- /** GOST 34.311-95 hash function */
50
- constructor(sbox = DKE_1) {
51
- this.sbox = sbox;
52
- this.buffer = new Uint8Array();
53
- }
54
- /** Create hash instance */
55
- static create() { return new Gost3431195(); }
56
- destroy() { this.buffer = new Uint8Array(); }
57
- clone() { return this._cloneInto(); }
58
- _cloneInto(to) {
59
- to ||= new Gost3431195();
60
- to.buffer = new Uint8Array(this.buffer);
61
- to.sbox = this.sbox;
62
- return to;
63
- }
64
- update(data) {
65
- this.buffer = concatBytes(this.buffer, data);
66
- return this;
67
- }
68
- digestInto(buf) {
69
- if (buf.length != this.outputLen)
70
- throw new Error("digestInto: Invalid buffer length");
71
- let len = 0n, checksum = 0n;
72
- const h = new Uint8Array(this.blockLen), m = copyBytes(this.buffer);
73
- for (let i = 0; i < m.length; i += this.blockLen) {
74
- let part = m.slice(i, i + this.blockLen).reverse();
75
- len += BigInt(part.length) * 8n;
76
- checksum = (checksum + bytesToNumberBE(part)) & r;
77
- if (part.length < this.blockLen)
78
- part = numberToBytesBE(bytesToNumberBE(part), this.blockLen);
79
- h.set(_step(h, part, this.sbox));
80
- }
81
- h.set(_step(_step(h, numberToBytesBE(len, this.blockLen), this.sbox), numberToBytesBE(checksum, this.blockLen), this.sbox));
82
- buf.set(h.reverse());
83
- this.destroy();
84
- }
85
- digest() {
86
- const buffer = new Uint8Array(this.outputLen);
87
- this.digestInto(buffer);
88
- return buffer;
89
- }
90
- }
91
- /** GOST 34.311-95 hash function */
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/kupyna/kmac.d.ts DELETED
@@ -1,46 +0,0 @@
1
- import type { Hash, TArg, TRet } from "@noble/hashes/utils.js";
2
- import { Kupyna256, Kupyna384, 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: () => 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 {};