@li0ard/gost 0.0.1 → 0.1.3

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/LICENSE +21 -0
  2. package/README.md +47 -45
  3. package/gost3410/const.d.ts +47 -0
  4. package/gost3410/const.js +126 -0
  5. package/gost3410/conversion.d.ts +19 -0
  6. package/gost3410/conversion.js +35 -0
  7. package/gost3410/index.d.ts +29 -0
  8. package/gost3410/index.js +84 -0
  9. package/gost3410/vko.d.ts +38 -0
  10. package/gost3410/vko.js +47 -0
  11. package/gost341194/index.d.ts +23 -0
  12. package/gost341194/index.js +193 -0
  13. package/hmac.d.ts +15 -0
  14. package/hmac.js +22 -0
  15. package/index.d.ts +9 -0
  16. package/index.js +9 -0
  17. package/kdf.d.ts +7 -0
  18. package/kdf.js +59 -0
  19. package/kuznyechik/const.d.ts +4 -0
  20. package/kuznyechik/const.js +78 -0
  21. package/kuznyechik/index.d.ts +12 -0
  22. package/kuznyechik/index.js +207 -0
  23. package/magma/const.d.ts +62 -0
  24. package/magma/const.js +244 -0
  25. package/magma/index.d.ts +24 -0
  26. package/magma/index.js +86 -0
  27. package/modes/_keytransform.d.ts +5 -0
  28. package/modes/_keytransform.js +35 -0
  29. package/modes/cbc.d.ts +8 -0
  30. package/modes/cbc.js +42 -0
  31. package/modes/cfb.d.ts +8 -0
  32. package/modes/cfb.js +37 -0
  33. package/modes/ctr.d.ts +15 -0
  34. package/modes/ctr.js +62 -0
  35. package/modes/ecb.d.ts +7 -0
  36. package/modes/ecb.js +21 -0
  37. package/modes/index.d.ts +8 -0
  38. package/modes/index.js +8 -0
  39. package/modes/mac.d.ts +21 -0
  40. package/modes/mac.js +119 -0
  41. package/modes/mgm.d.ts +8 -0
  42. package/modes/mgm.js +90 -0
  43. package/modes/ofb.d.ts +8 -0
  44. package/modes/ofb.js +25 -0
  45. package/modes/wrap.d.ts +14 -0
  46. package/modes/wrap.js +57 -0
  47. package/package.json +48 -7
  48. package/streebog/const.d.ts +4 -0
  49. package/streebog/const.js +102 -0
  50. package/streebog/index.d.ts +66 -0
  51. package/streebog/index.js +295 -0
  52. package/types.d.ts +50 -0
  53. package/types.js +1 -0
  54. package/utils.d.ts +7 -0
  55. package/utils.js +47 -0
@@ -0,0 +1,295 @@
1
+ import { concatBytes, createHasher } from "@noble/hashes/utils.js";
2
+ import { A, C, TAU } from "./const.js";
3
+ import { PI } from "../kuznyechik/const.js";
4
+ import { pad1, xorBytes } from "../utils.js";
5
+ import { numberToBytesBE } from "@noble/curves/utils.js";
6
+ const BLOCKSIZE = 64;
7
+ const _0020 = new Uint8Array([0, 0, 2, 0]);
8
+ const _0 = new Uint8Array(64);
9
+ const add512 = (a, b) => {
10
+ const c = new Uint8Array(64);
11
+ const tmpA = new Uint8Array(64);
12
+ const tmpB = new Uint8Array(64);
13
+ for (let i = 0; i < a.length; i++)
14
+ tmpA[63 - i] = a[a.length - i - 1];
15
+ for (let i = 0; i < b.length; i++)
16
+ tmpB[63 - i] = b[b.length - i - 1];
17
+ for (let i = 63, tmp = 0; i >= 0; i--) {
18
+ tmp = tmpA[i] + tmpB[i] + (tmp >> 8);
19
+ c[i] = tmp & 0xff;
20
+ }
21
+ return c;
22
+ };
23
+ const S = (input) => {
24
+ const result = new Uint8Array(BLOCKSIZE);
25
+ //for (let i = 0; i < BLOCKSIZE; i++) result[i] = PI[input[i]];
26
+ result[0] = PI[input[0]];
27
+ result[1] = PI[input[1]];
28
+ result[2] = PI[input[2]];
29
+ result[3] = PI[input[3]];
30
+ result[4] = PI[input[4]];
31
+ result[5] = PI[input[5]];
32
+ result[6] = PI[input[6]];
33
+ result[7] = PI[input[7]];
34
+ result[8] = PI[input[8]];
35
+ result[9] = PI[input[9]];
36
+ result[10] = PI[input[10]];
37
+ result[11] = PI[input[11]];
38
+ result[12] = PI[input[12]];
39
+ result[13] = PI[input[13]];
40
+ result[14] = PI[input[14]];
41
+ result[15] = PI[input[15]];
42
+ result[16] = PI[input[16]];
43
+ result[17] = PI[input[17]];
44
+ result[18] = PI[input[18]];
45
+ result[19] = PI[input[19]];
46
+ result[20] = PI[input[20]];
47
+ result[21] = PI[input[21]];
48
+ result[22] = PI[input[22]];
49
+ result[23] = PI[input[23]];
50
+ result[24] = PI[input[24]];
51
+ result[25] = PI[input[25]];
52
+ result[26] = PI[input[26]];
53
+ result[27] = PI[input[27]];
54
+ result[28] = PI[input[28]];
55
+ result[29] = PI[input[29]];
56
+ result[30] = PI[input[30]];
57
+ result[31] = PI[input[31]];
58
+ result[32] = PI[input[32]];
59
+ result[33] = PI[input[33]];
60
+ result[34] = PI[input[34]];
61
+ result[35] = PI[input[35]];
62
+ result[36] = PI[input[36]];
63
+ result[37] = PI[input[37]];
64
+ result[38] = PI[input[38]];
65
+ result[39] = PI[input[39]];
66
+ result[40] = PI[input[40]];
67
+ result[41] = PI[input[41]];
68
+ result[42] = PI[input[42]];
69
+ result[43] = PI[input[43]];
70
+ result[44] = PI[input[44]];
71
+ result[45] = PI[input[45]];
72
+ result[46] = PI[input[46]];
73
+ result[47] = PI[input[47]];
74
+ result[48] = PI[input[48]];
75
+ result[49] = PI[input[49]];
76
+ result[50] = PI[input[50]];
77
+ result[51] = PI[input[51]];
78
+ result[52] = PI[input[52]];
79
+ result[53] = PI[input[53]];
80
+ result[54] = PI[input[54]];
81
+ result[55] = PI[input[55]];
82
+ result[56] = PI[input[56]];
83
+ result[57] = PI[input[57]];
84
+ result[58] = PI[input[58]];
85
+ result[59] = PI[input[59]];
86
+ result[60] = PI[input[60]];
87
+ result[61] = PI[input[61]];
88
+ result[62] = PI[input[62]];
89
+ result[63] = PI[input[63]];
90
+ return result;
91
+ };
92
+ const P = (input) => {
93
+ const result = new Uint8Array(BLOCKSIZE);
94
+ //for (let i = 0; i < BLOCKSIZE; i++) result[i] = input[TAU[i]];
95
+ result[0] = input[TAU[0]];
96
+ result[1] = input[TAU[1]];
97
+ result[2] = input[TAU[2]];
98
+ result[3] = input[TAU[3]];
99
+ result[4] = input[TAU[4]];
100
+ result[5] = input[TAU[5]];
101
+ result[6] = input[TAU[6]];
102
+ result[7] = input[TAU[7]];
103
+ result[8] = input[TAU[8]];
104
+ result[9] = input[TAU[9]];
105
+ result[10] = input[TAU[10]];
106
+ result[11] = input[TAU[11]];
107
+ result[12] = input[TAU[12]];
108
+ result[13] = input[TAU[13]];
109
+ result[14] = input[TAU[14]];
110
+ result[15] = input[TAU[15]];
111
+ result[16] = input[TAU[16]];
112
+ result[17] = input[TAU[17]];
113
+ result[18] = input[TAU[18]];
114
+ result[19] = input[TAU[19]];
115
+ result[20] = input[TAU[20]];
116
+ result[21] = input[TAU[21]];
117
+ result[22] = input[TAU[22]];
118
+ result[23] = input[TAU[23]];
119
+ result[24] = input[TAU[24]];
120
+ result[25] = input[TAU[25]];
121
+ result[26] = input[TAU[26]];
122
+ result[27] = input[TAU[27]];
123
+ result[28] = input[TAU[28]];
124
+ result[29] = input[TAU[29]];
125
+ result[30] = input[TAU[30]];
126
+ result[31] = input[TAU[31]];
127
+ result[32] = input[TAU[32]];
128
+ result[33] = input[TAU[33]];
129
+ result[34] = input[TAU[34]];
130
+ result[35] = input[TAU[35]];
131
+ result[36] = input[TAU[36]];
132
+ result[37] = input[TAU[37]];
133
+ result[38] = input[TAU[38]];
134
+ result[39] = input[TAU[39]];
135
+ result[40] = input[TAU[40]];
136
+ result[41] = input[TAU[41]];
137
+ result[42] = input[TAU[42]];
138
+ result[43] = input[TAU[43]];
139
+ result[44] = input[TAU[44]];
140
+ result[45] = input[TAU[45]];
141
+ result[46] = input[TAU[46]];
142
+ result[47] = input[TAU[47]];
143
+ result[48] = input[TAU[48]];
144
+ result[49] = input[TAU[49]];
145
+ result[50] = input[TAU[50]];
146
+ result[51] = input[TAU[51]];
147
+ result[52] = input[TAU[52]];
148
+ result[53] = input[TAU[53]];
149
+ result[54] = input[TAU[54]];
150
+ result[55] = input[TAU[55]];
151
+ result[56] = input[TAU[56]];
152
+ result[57] = input[TAU[57]];
153
+ result[58] = input[TAU[58]];
154
+ result[59] = input[TAU[59]];
155
+ result[60] = input[TAU[60]];
156
+ result[61] = input[TAU[61]];
157
+ result[62] = input[TAU[62]];
158
+ result[63] = input[TAU[63]];
159
+ return result;
160
+ };
161
+ const L = (input) => {
162
+ const result = new Uint8Array(BLOCKSIZE);
163
+ for (let i = 0; i < 8; i++) {
164
+ const parts = new Uint32Array(2);
165
+ const tmp = input.slice(i * 8, i * 8 + 8).reverse();
166
+ for (let j = 0; j < 8; j++) {
167
+ for (let k = 0; k < 8; k++) {
168
+ if ((tmp[7 - j] >> 7 - k) & 1) {
169
+ parts[0] ^= A[j * 16 + k * 2];
170
+ parts[1] ^= A[j * 16 + k * 2 + 1];
171
+ }
172
+ }
173
+ }
174
+ result.set(numberToBytesBE(parts[0], 4), i * 8);
175
+ result.set(numberToBytesBE(parts[1], 4), i * 8 + 4);
176
+ }
177
+ return result;
178
+ };
179
+ const LPS = (input) => L(P(S(input)));
180
+ const E = (block, keys) => {
181
+ // block will be mutated
182
+ let c = xorBytes(block, keys);
183
+ /*for (let i = 0; i < 12; i++) {
184
+ block = LPS(xorBytes(block, C[i]));
185
+ c = xorBytes(LPS(c), block);
186
+ }*/
187
+ block = LPS(xorBytes(block, C[0]));
188
+ c = xorBytes(LPS(c), block);
189
+ block = LPS(xorBytes(block, C[1]));
190
+ c = xorBytes(LPS(c), block);
191
+ block = LPS(xorBytes(block, C[2]));
192
+ c = xorBytes(LPS(c), block);
193
+ block = LPS(xorBytes(block, C[3]));
194
+ c = xorBytes(LPS(c), block);
195
+ block = LPS(xorBytes(block, C[4]));
196
+ c = xorBytes(LPS(c), block);
197
+ block = LPS(xorBytes(block, C[5]));
198
+ c = xorBytes(LPS(c), block);
199
+ block = LPS(xorBytes(block, C[6]));
200
+ c = xorBytes(LPS(c), block);
201
+ block = LPS(xorBytes(block, C[7]));
202
+ c = xorBytes(LPS(c), block);
203
+ block = LPS(xorBytes(block, C[8]));
204
+ c = xorBytes(LPS(c), block);
205
+ block = LPS(xorBytes(block, C[9]));
206
+ c = xorBytes(LPS(c), block);
207
+ block = LPS(xorBytes(block, C[10]));
208
+ c = xorBytes(LPS(c), block);
209
+ block = LPS(xorBytes(block, C[11]));
210
+ c = xorBytes(LPS(c), block);
211
+ return c;
212
+ };
213
+ const G = (hash, n, message) => xorBytes(xorBytes(E(LPS(xorBytes(n, hash)), message), n), message);
214
+ /** Streebog (GOST R 34.11-2012) hash function */
215
+ class Streebog {
216
+ is512;
217
+ blockLen = BLOCKSIZE;
218
+ outputLen;
219
+ canXOF = false;
220
+ buffer;
221
+ /** Streebog (GOST R 34.11-2012) hash function */
222
+ constructor(is512) {
223
+ this.is512 = is512;
224
+ this.buffer = new Uint8Array();
225
+ this.outputLen = is512 ? 64 : 32;
226
+ }
227
+ destroy() { this.buffer = new Uint8Array(); }
228
+ update(data) {
229
+ this.buffer = concatBytes(this.buffer, data);
230
+ return this;
231
+ }
232
+ digest() {
233
+ const buffer = new Uint8Array(this.outputLen);
234
+ this.digestInto(buffer);
235
+ return buffer;
236
+ }
237
+ digestInto(buf) {
238
+ const message = this.buffer.slice().reverse();
239
+ let n = new Uint8Array(BLOCKSIZE);
240
+ let sigma = new Uint8Array(BLOCKSIZE);
241
+ let hash = new Uint8Array(BLOCKSIZE).fill(this.is512 ? 0 : 1);
242
+ let blocks = 1;
243
+ for (let i = message.length; i >= BLOCKSIZE; i -= BLOCKSIZE) {
244
+ const pos = message.length - blocks * BLOCKSIZE;
245
+ hash = G(n, hash, message.subarray(pos, pos + BLOCKSIZE));
246
+ n = add512(n, _0020);
247
+ sigma = add512(sigma, message.subarray(pos, pos + BLOCKSIZE));
248
+ blocks++;
249
+ }
250
+ let paddedMsg = new Uint8Array(BLOCKSIZE);
251
+ const msg = message.subarray(0, message.length - (blocks - 1) * 64);
252
+ if (msg.length < BLOCKSIZE) {
253
+ paddedMsg = pad1(paddedMsg, BLOCKSIZE);
254
+ paddedMsg[BLOCKSIZE - msg.length - 1] = 0x01;
255
+ for (let i = 0; i < msg.length; i++)
256
+ paddedMsg[BLOCKSIZE - msg.length + i] = msg[i];
257
+ }
258
+ hash = G(_0, G(_0, G(n, hash, paddedMsg), add512(n, numberToBytesBE(msg.length * 8, 4))), add512(sigma, paddedMsg));
259
+ if (this.is512)
260
+ buf.set(hash.slice().reverse());
261
+ else
262
+ buf.set(hash.slice(0, 32).reverse());
263
+ this.destroy();
264
+ }
265
+ }
266
+ /** Streebog-256 (GOST R 34.11-2012) hash function */
267
+ export class Streebog256 extends Streebog {
268
+ /** Streebog-256 (GOST R 34.11-2012) hash function */
269
+ constructor() { super(false); }
270
+ /** Create hash instance */
271
+ static create() { return new Streebog256(); }
272
+ clone() { return this._cloneInto(); }
273
+ _cloneInto(to) {
274
+ to ||= new Streebog256();
275
+ to.buffer = new Uint8Array(this.buffer);
276
+ return to;
277
+ }
278
+ }
279
+ /** Streebog-512 (GOST R 34.11-2012) hash function */
280
+ export class Streebog512 extends Streebog {
281
+ /** Streebog-512 (GOST R 34.11-2012) hash function */
282
+ constructor() { super(true); }
283
+ /** Create hash instance */
284
+ static create() { return new Streebog512(); }
285
+ clone() { return this._cloneInto(); }
286
+ _cloneInto(to) {
287
+ to ||= new Streebog512();
288
+ to.buffer = new Uint8Array(this.buffer);
289
+ return to;
290
+ }
291
+ }
292
+ /** Streebog-256 (GOST R 34.11-2012) hash function */
293
+ export const streebog256 = createHasher(Streebog256.create);
294
+ /** Streebog-512 (GOST R 34.11-2012) hash function */
295
+ export const streebog512 = createHasher(Streebog512.create);
package/types.d.ts ADDED
@@ -0,0 +1,50 @@
1
+ import type { TArg, TRet } from "@noble/hashes/utils.js";
2
+ /** Cipher core */
3
+ export type Cipher = {
4
+ /** Block size */
5
+ readonly blockSize: number;
6
+ /** Key size */
7
+ readonly keySize: number;
8
+ /** Encrypt block */
9
+ encrypt(plaintext: TArg<Uint8Array>): TRet<Uint8Array>;
10
+ /** Decrypt block */
11
+ decrypt(ciphertext: TArg<Uint8Array>): TRet<Uint8Array>;
12
+ };
13
+ /** Block mode for {@link Cipher} */
14
+ export type BlockMode = {
15
+ /** Encrypt plaintext */
16
+ encrypt: (plaintext: TArg<Uint8Array>) => TRet<Uint8Array>;
17
+ /** Decrypt ciphertext */
18
+ decrypt: (ciphertext: TArg<Uint8Array>) => TRet<Uint8Array>;
19
+ };
20
+ /** Stream-like mode for {@link Cipher} */
21
+ export type StreamMode = {
22
+ /** Proceed message */
23
+ crypt: (msg: TArg<Uint8Array>) => TRet<Uint8Array>;
24
+ };
25
+ /** MAC mode for {@link Cipher} */
26
+ export type MACMode = {
27
+ /** Compute MAC */
28
+ compute: (msg: TArg<Uint8Array>) => TRet<Uint8Array>;
29
+ };
30
+ /** AEAD mode for {@link Cipher} */
31
+ export type AEADMode = {
32
+ /** Seal plaintext and AAD */
33
+ seal: (plaintext: TArg<Uint8Array>, aad?: TArg<Uint8Array>) => TRet<Uint8Array>;
34
+ /** Open ciphertext and AAD */
35
+ open: (ciphertext: TArg<Uint8Array>, aad?: TArg<Uint8Array>) => TRet<Uint8Array>;
36
+ };
37
+ /** Key wrapping mode for {@link Cipher} */
38
+ export type WrapMode = {
39
+ /** Wrap encryption key */
40
+ wrap: (key: TArg<Uint8Array>) => TRet<Uint8Array>;
41
+ /** Unwrap encryption key */
42
+ unwrap: (wrapped: TArg<Uint8Array>) => TRet<Uint8Array>;
43
+ };
44
+ /** Key wrapping mode (KWP) for Magma */
45
+ export type WrapModeMagma = {
46
+ /** Wrap encryption key */
47
+ wrap: (ukm: TArg<Uint8Array>, cek: TArg<Uint8Array>) => TRet<Uint8Array>;
48
+ /** Unwrap encryption key */
49
+ unwrap: (wrapped: TArg<Uint8Array>) => TRet<Uint8Array>;
50
+ };
package/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/utils.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import type { TArg, TRet } from "@noble/hashes/utils.js";
2
+ export declare const xorBytes: (a: TArg<Uint8Array>, b: TArg<Uint8Array>) => TRet<Uint8Array>;
3
+ export declare const getPadLength: (dataLength: number, blockSize: number) => number;
4
+ export declare const pad1: (data: TArg<Uint8Array>, blockSize: number) => TRet<Uint8Array>;
5
+ export declare const pad2: (data: TArg<Uint8Array>, blockSize: number) => TRet<Uint8Array>;
6
+ export declare const unpad2: (data: TArg<Uint8Array>, blockSize: number) => TRet<Uint8Array>;
7
+ export declare const pad3: (data: TArg<Uint8Array>, blockSize: number) => TRet<Uint8Array>;
package/utils.js ADDED
@@ -0,0 +1,47 @@
1
+ export const xorBytes = (a, b) => {
2
+ const mlen = Math.min(a.length, b.length);
3
+ const result = new Uint8Array(mlen);
4
+ for (let i = 0; i < mlen; i++)
5
+ result[i] = a[i] ^ b[i];
6
+ return result;
7
+ };
8
+ export const getPadLength = (dataLength, blockSize) => {
9
+ if (dataLength < blockSize)
10
+ return blockSize - dataLength;
11
+ if (dataLength % blockSize == 0)
12
+ return 0;
13
+ return blockSize - dataLength % blockSize;
14
+ };
15
+ export const pad1 = (data, blockSize) => {
16
+ const padded = new Uint8Array(data.length + getPadLength(data.length, blockSize));
17
+ padded.set(data);
18
+ return padded;
19
+ };
20
+ export const pad2 = (data, blockSize) => {
21
+ const padded = new Uint8Array(data.length + 1 + getPadLength(data.length + 1, blockSize));
22
+ padded.set(data, 0);
23
+ padded[data.length] = 0x80;
24
+ return padded;
25
+ };
26
+ export const unpad2 = (data, blockSize) => {
27
+ const lastBlock = data.subarray(data.length - blockSize);
28
+ let padIndex = -1;
29
+ for (let i = lastBlock.length - 1; i >= 0; i--) {
30
+ if (lastBlock[i] == 0x80) {
31
+ padIndex = i;
32
+ break;
33
+ }
34
+ }
35
+ if (padIndex === -1)
36
+ throw new Error("Padding marker (0x80) not found");
37
+ for (let i = padIndex + 1; i < lastBlock.length; i++) {
38
+ if (lastBlock[i] !== 0)
39
+ throw new Error("Invalid padding: non-zero bytes after 0x80");
40
+ }
41
+ return data.slice(0, data.length - (blockSize - padIndex));
42
+ };
43
+ export const pad3 = (data, blockSize) => {
44
+ if (getPadLength(data.length, blockSize) == 0)
45
+ return data;
46
+ return pad2(data, blockSize);
47
+ };