@li0ard/dstu 0.0.1 → 0.1.0

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/LICENSE +21 -0
  2. package/README.md +37 -45
  3. package/const.d.ts +14 -0
  4. package/const.js +1084 -0
  5. package/dstu4145/const.d.ts +22 -0
  6. package/dstu4145/const.js +36 -0
  7. package/dstu4145/ec/curve.d.ts +17 -0
  8. package/dstu4145/ec/curve.js +82 -0
  9. package/dstu4145/ec/field.d.ts +30 -0
  10. package/dstu4145/ec/field.js +135 -0
  11. package/dstu4145/ec/index.d.ts +3 -0
  12. package/dstu4145/ec/index.js +3 -0
  13. package/dstu4145/ec/point.d.ts +12 -0
  14. package/dstu4145/ec/point.js +79 -0
  15. package/dstu4145/index.d.ts +28 -0
  16. package/dstu4145/index.js +70 -0
  17. package/index.d.ts +6 -0
  18. package/index.js +6 -0
  19. package/kalyna/index.d.ts +50 -0
  20. package/kalyna/index.js +211 -0
  21. package/kupyna/index.d.ts +162 -0
  22. package/kupyna/index.js +241 -0
  23. package/kupyna/kmac.d.ts +46 -0
  24. package/kupyna/kmac.js +124 -0
  25. package/modes/cbc.d.ts +5 -0
  26. package/modes/cbc.js +10 -0
  27. package/modes/cfb.d.ts +5 -0
  28. package/modes/cfb.js +72 -0
  29. package/modes/ctr.d.ts +5 -0
  30. package/modes/ctr.js +24 -0
  31. package/modes/ecb.d.ts +4 -0
  32. package/modes/ecb.js +17 -0
  33. package/modes/gcm.d.ts +5 -0
  34. package/modes/gcm.js +20 -0
  35. package/modes/index.d.ts +7 -0
  36. package/modes/index.js +7 -0
  37. package/modes/mac.d.ts +6 -0
  38. package/modes/mac.js +65 -0
  39. package/modes/ofb.d.ts +5 -0
  40. package/modes/ofb.js +11 -0
  41. package/package.json +47 -7
  42. package/padding.d.ts +3 -0
  43. package/padding.js +22 -0
  44. package/strumok/index.d.ts +24 -0
  45. package/strumok/index.js +360 -0
  46. package/types.d.ts +41 -0
  47. package/types.js +1 -0
  48. package/utils.d.ts +14 -0
  49. package/utils.js +81 -0
@@ -0,0 +1,360 @@
1
+ import { ALPHA_MUL, ALPHA_MUL_INV, T as T_ } from "../const.js";
2
+ import { bytesToUint64sBE, byte } from "../utils.js";
3
+ const not = (w) => {
4
+ const MAX_UINT64 = (1n << 64n) - 1n;
5
+ return MAX_UINT64 - (w & MAX_UINT64);
6
+ };
7
+ const a_mul = (w) => (w << 8n) ^ (ALPHA_MUL[byte(w >> 56n)]);
8
+ const ainv_mul = (w) => (w >> 8n) ^ (ALPHA_MUL_INV[byte(w)]);
9
+ const T = (w) => (T_[0][byte(w)] ^
10
+ T_[1][byte(w >> 8n)] ^
11
+ T_[2][byte(w >> 16n)] ^
12
+ T_[3][byte(w >> 24n)] ^
13
+ T_[4][byte(w >> 32n)] ^
14
+ T_[5][byte(w >> 40n)] ^
15
+ T_[6][byte(w >> 48n)] ^
16
+ T_[7][byte(w >> 56n)]);
17
+ /** Strumok stream cipher */
18
+ export class Strumok {
19
+ blockSize = 128;
20
+ keySize;
21
+ S;
22
+ r;
23
+ key;
24
+ iv;
25
+ /**
26
+ * Strumok stream cipher
27
+ * @param key Encryption key (256 or 512 bit)
28
+ */
29
+ constructor(key, iv) {
30
+ if (iv.length !== 32)
31
+ throw new Error("Unsupported IV length");
32
+ this.key = bytesToUint64sBE(key);
33
+ this.iv = bytesToUint64sBE(iv);
34
+ this.keySize = key.length;
35
+ this.S = new BigUint64Array(16);
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]);
54
+ }
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];
72
+ }
73
+ else
74
+ throw new Error("Unsupported key length");
75
+ this.r[0] = 0n;
76
+ this.r[1] = 0n;
77
+ for (let i = 0; i < 2; i++) {
78
+ let outfrom_fsm, fsmtmp;
79
+ outfrom_fsm = (this.r[0] + this.S[15]) ^ this.r[1];
80
+ this.S[0] = a_mul(this.S[0]) ^ this.S[13] ^ ainv_mul(this.S[11]) ^ outfrom_fsm;
81
+ fsmtmp = this.r[1] + this.S[13];
82
+ this.r[1] = T(this.r[0]);
83
+ this.r[0] = fsmtmp;
84
+ outfrom_fsm = (this.r[0] + this.S[0]) ^ this.r[1];
85
+ this.S[1] = a_mul(this.S[1]) ^ this.S[14] ^ ainv_mul(this.S[12]) ^ outfrom_fsm;
86
+ fsmtmp = this.r[1] + this.S[14];
87
+ this.r[1] = T(this.r[0]);
88
+ this.r[0] = fsmtmp;
89
+ outfrom_fsm = (this.r[0] + this.S[1]) ^ this.r[1];
90
+ this.S[2] = a_mul(this.S[2]) ^ this.S[15] ^ ainv_mul(this.S[13]) ^ outfrom_fsm;
91
+ fsmtmp = this.r[1] + this.S[15];
92
+ this.r[1] = T(this.r[0]);
93
+ this.r[0] = fsmtmp;
94
+ outfrom_fsm = (this.r[0] + this.S[2]) ^ this.r[1];
95
+ this.S[3] = a_mul(this.S[3]) ^ this.S[0] ^ ainv_mul(this.S[14]) ^ outfrom_fsm;
96
+ fsmtmp = this.r[1] + this.S[0];
97
+ this.r[1] = T(this.r[0]);
98
+ this.r[0] = fsmtmp;
99
+ outfrom_fsm = (this.r[0] + this.S[3]) ^ this.r[1];
100
+ this.S[4] = a_mul(this.S[4]) ^ this.S[1] ^ ainv_mul(this.S[15]) ^ outfrom_fsm;
101
+ fsmtmp = this.r[1] + this.S[1];
102
+ this.r[1] = T(this.r[0]);
103
+ this.r[0] = fsmtmp;
104
+ outfrom_fsm = (this.r[0] + this.S[4]) ^ this.r[1];
105
+ this.S[5] = a_mul(this.S[5]) ^ this.S[2] ^ ainv_mul(this.S[0]) ^ outfrom_fsm;
106
+ fsmtmp = this.r[1] + this.S[2];
107
+ this.r[1] = T(this.r[0]);
108
+ this.r[0] = fsmtmp;
109
+ outfrom_fsm = (this.r[0] + this.S[5]) ^ this.r[1];
110
+ this.S[6] = a_mul(this.S[6]) ^ this.S[3] ^ ainv_mul(this.S[1]) ^ outfrom_fsm;
111
+ fsmtmp = this.r[1] + this.S[3];
112
+ this.r[1] = T(this.r[0]);
113
+ this.r[0] = fsmtmp;
114
+ outfrom_fsm = (this.r[0] + this.S[6]) ^ this.r[1];
115
+ this.S[7] = a_mul(this.S[7]) ^ this.S[4] ^ ainv_mul(this.S[2]) ^ outfrom_fsm;
116
+ fsmtmp = this.r[1] + this.S[4];
117
+ this.r[1] = T(this.r[0]);
118
+ this.r[0] = fsmtmp;
119
+ outfrom_fsm = (this.r[0] + this.S[7]) ^ this.r[1];
120
+ this.S[8] = a_mul(this.S[8]) ^ this.S[5] ^ ainv_mul(this.S[3]) ^ outfrom_fsm;
121
+ fsmtmp = this.r[1] + this.S[5];
122
+ this.r[1] = T(this.r[0]);
123
+ this.r[0] = fsmtmp;
124
+ outfrom_fsm = (this.r[0] + this.S[8]) ^ this.r[1];
125
+ this.S[9] = a_mul(this.S[9]) ^ this.S[6] ^ ainv_mul(this.S[4]) ^ outfrom_fsm;
126
+ fsmtmp = this.r[1] + this.S[6];
127
+ this.r[1] = T(this.r[0]);
128
+ this.r[0] = fsmtmp;
129
+ outfrom_fsm = (this.r[0] + this.S[9]) ^ this.r[1];
130
+ this.S[10] = a_mul(this.S[10]) ^ this.S[7] ^ ainv_mul(this.S[5]) ^ outfrom_fsm;
131
+ fsmtmp = this.r[1] + this.S[7];
132
+ this.r[1] = T(this.r[0]);
133
+ this.r[0] = fsmtmp;
134
+ outfrom_fsm = (this.r[0] + this.S[10]) ^ this.r[1];
135
+ this.S[11] = a_mul(this.S[11]) ^ this.S[8] ^ ainv_mul(this.S[6]) ^ outfrom_fsm;
136
+ fsmtmp = this.r[1] + this.S[8];
137
+ this.r[1] = T(this.r[0]);
138
+ this.r[0] = fsmtmp;
139
+ outfrom_fsm = (this.r[0] + this.S[11]) ^ this.r[1];
140
+ this.S[12] = a_mul(this.S[12]) ^ this.S[9] ^ ainv_mul(this.S[7]) ^ outfrom_fsm;
141
+ fsmtmp = this.r[1] + this.S[9];
142
+ this.r[1] = T(this.r[0]);
143
+ this.r[0] = fsmtmp;
144
+ outfrom_fsm = (this.r[0] + this.S[12]) ^ this.r[1];
145
+ this.S[13] = a_mul(this.S[13]) ^ this.S[10] ^ ainv_mul(this.S[8]) ^ outfrom_fsm;
146
+ fsmtmp = this.r[1] + this.S[10];
147
+ this.r[1] = T(this.r[0]);
148
+ this.r[0] = fsmtmp;
149
+ outfrom_fsm = (this.r[0] + this.S[13]) ^ this.r[1];
150
+ this.S[14] = a_mul(this.S[14]) ^ this.S[11] ^ ainv_mul(this.S[9]) ^ outfrom_fsm;
151
+ fsmtmp = this.r[1] + this.S[11];
152
+ this.r[1] = T(this.r[0]);
153
+ this.r[0] = fsmtmp;
154
+ outfrom_fsm = (this.r[0] + this.S[14]) ^ this.r[1];
155
+ this.S[15] = a_mul(this.S[15]) ^ this.S[12] ^ ainv_mul(this.S[10]) ^ outfrom_fsm;
156
+ fsmtmp = this.r[1] + this.S[12];
157
+ this.r[1] = T(this.r[0]);
158
+ this.r[0] = fsmtmp;
159
+ }
160
+ }
161
+ /** Generate next keystream */
162
+ next_stream() {
163
+ let fsmtmp;
164
+ let out_stream = new BigUint64Array(16);
165
+ this.S[0] = a_mul(this.S[0]) ^ this.S[13] ^ ainv_mul(this.S[11]);
166
+ fsmtmp = this.r[1] + this.S[13];
167
+ this.r[1] = T(this.r[0]);
168
+ this.r[0] = fsmtmp;
169
+ out_stream[0] = (this.r[0] + this.S[0]) ^ this.r[1] ^ this.S[1];
170
+ this.S[1] = a_mul(this.S[1]) ^ this.S[14] ^ ainv_mul(this.S[12]);
171
+ fsmtmp = this.r[1] + this.S[14];
172
+ this.r[1] = T(this.r[0]);
173
+ this.r[0] = fsmtmp;
174
+ out_stream[1] = (this.r[0] + this.S[1]) ^ this.r[1] ^ this.S[2];
175
+ this.S[2] = a_mul(this.S[2]) ^ this.S[15] ^ ainv_mul(this.S[13]);
176
+ fsmtmp = this.r[1] + this.S[15];
177
+ this.r[1] = T(this.r[0]);
178
+ this.r[0] = fsmtmp;
179
+ out_stream[2] = (this.r[0] + this.S[2]) ^ this.r[1] ^ this.S[3];
180
+ this.S[3] = a_mul(this.S[3]) ^ this.S[0] ^ ainv_mul(this.S[14]);
181
+ fsmtmp = this.r[1] + this.S[0];
182
+ this.r[1] = T(this.r[0]);
183
+ this.r[0] = fsmtmp;
184
+ out_stream[3] = (this.r[0] + this.S[3]) ^ this.r[1] ^ this.S[4];
185
+ this.S[4] = a_mul(this.S[4]) ^ this.S[1] ^ ainv_mul(this.S[15]);
186
+ fsmtmp = this.r[1] + this.S[1];
187
+ this.r[1] = T(this.r[0]);
188
+ this.r[0] = fsmtmp;
189
+ out_stream[4] = (this.r[0] + this.S[4]) ^ this.r[1] ^ this.S[5];
190
+ this.S[5] = a_mul(this.S[5]) ^ this.S[2] ^ ainv_mul(this.S[0]);
191
+ fsmtmp = this.r[1] + this.S[2];
192
+ this.r[1] = T(this.r[0]);
193
+ this.r[0] = fsmtmp;
194
+ out_stream[5] = (this.r[0] + this.S[5]) ^ this.r[1] ^ this.S[6];
195
+ this.S[6] = a_mul(this.S[6]) ^ this.S[3] ^ ainv_mul(this.S[1]);
196
+ fsmtmp = this.r[1] + this.S[3];
197
+ this.r[1] = T(this.r[0]);
198
+ this.r[0] = fsmtmp;
199
+ out_stream[6] = (this.r[0] + this.S[6]) ^ this.r[1] ^ this.S[7];
200
+ this.S[7] = a_mul(this.S[7]) ^ this.S[4] ^ ainv_mul(this.S[2]);
201
+ fsmtmp = this.r[1] + this.S[4];
202
+ this.r[1] = T(this.r[0]);
203
+ this.r[0] = fsmtmp;
204
+ out_stream[7] = (this.r[0] + this.S[7]) ^ this.r[1] ^ this.S[8];
205
+ this.S[8] = a_mul(this.S[8]) ^ this.S[5] ^ ainv_mul(this.S[3]);
206
+ fsmtmp = this.r[1] + this.S[5];
207
+ this.r[1] = T(this.r[0]);
208
+ this.r[0] = fsmtmp;
209
+ out_stream[8] = (this.r[0] + this.S[8]) ^ this.r[1] ^ this.S[9];
210
+ this.S[9] = a_mul(this.S[9]) ^ this.S[6] ^ ainv_mul(this.S[4]);
211
+ fsmtmp = this.r[1] + this.S[6];
212
+ this.r[1] = T(this.r[0]);
213
+ this.r[0] = fsmtmp;
214
+ out_stream[9] = (this.r[0] + this.S[9]) ^ this.r[1] ^ this.S[10];
215
+ this.S[10] = a_mul(this.S[10]) ^ this.S[7] ^ ainv_mul(this.S[5]);
216
+ fsmtmp = this.r[1] + this.S[7];
217
+ this.r[1] = T(this.r[0]);
218
+ this.r[0] = fsmtmp;
219
+ out_stream[10] = (this.r[0] + this.S[10]) ^ this.r[1] ^ this.S[11];
220
+ this.S[11] = a_mul(this.S[11]) ^ this.S[8] ^ ainv_mul(this.S[6]);
221
+ fsmtmp = this.r[1] + this.S[8];
222
+ this.r[1] = T(this.r[0]);
223
+ this.r[0] = fsmtmp;
224
+ out_stream[11] = (this.r[0] + this.S[11]) ^ this.r[1] ^ this.S[12];
225
+ this.S[12] = a_mul(this.S[12]) ^ this.S[9] ^ ainv_mul(this.S[7]);
226
+ fsmtmp = this.r[1] + this.S[9];
227
+ this.r[1] = T(this.r[0]);
228
+ this.r[0] = fsmtmp;
229
+ out_stream[12] = (this.r[0] + this.S[12]) ^ this.r[1] ^ this.S[13];
230
+ this.S[13] = a_mul(this.S[13]) ^ this.S[10] ^ ainv_mul(this.S[8]);
231
+ fsmtmp = this.r[1] + this.S[10];
232
+ this.r[1] = T(this.r[0]);
233
+ this.r[0] = fsmtmp;
234
+ out_stream[13] = (this.r[0] + this.S[13]) ^ this.r[1] ^ this.S[14];
235
+ this.S[14] = a_mul(this.S[14]) ^ this.S[11] ^ ainv_mul(this.S[9]);
236
+ fsmtmp = this.r[1] + this.S[11];
237
+ this.r[1] = T(this.r[0]);
238
+ this.r[0] = fsmtmp;
239
+ out_stream[14] = (this.r[0] + this.S[14]) ^ this.r[1] ^ this.S[15];
240
+ this.S[15] = a_mul(this.S[15]) ^ this.S[12] ^ ainv_mul(this.S[10]);
241
+ fsmtmp = this.r[1] + this.S[12];
242
+ this.r[1] = T(this.r[0]);
243
+ this.r[0] = fsmtmp;
244
+ out_stream[15] = (this.r[0] + this.S[15]) ^ this.r[1] ^ this.S[0];
245
+ return out_stream;
246
+ }
247
+ /** Generate next keystream and perform encryption */
248
+ next_stream_full_crypt(in_) {
249
+ let fsmtmp;
250
+ let out = new BigUint64Array(16);
251
+ this.S[0] = a_mul(this.S[0]) ^ this.S[13] ^ ainv_mul(this.S[11]);
252
+ fsmtmp = this.r[1] + this.S[13];
253
+ this.r[1] = T(this.r[0]);
254
+ this.r[0] = fsmtmp;
255
+ out[0] = in_[0] ^ (this.r[0] + this.S[0]) ^ this.r[1] ^ this.S[1];
256
+ this.S[1] = a_mul(this.S[1]) ^ this.S[14] ^ ainv_mul(this.S[12]);
257
+ fsmtmp = this.r[1] + this.S[14];
258
+ this.r[1] = T(this.r[0]);
259
+ this.r[0] = fsmtmp;
260
+ out[1] = in_[1] ^ (this.r[0] + this.S[1]) ^ this.r[1] ^ this.S[2];
261
+ this.S[2] = a_mul(this.S[2]) ^ this.S[15] ^ ainv_mul(this.S[13]);
262
+ fsmtmp = this.r[1] + this.S[15];
263
+ this.r[1] = T(this.r[0]);
264
+ this.r[0] = fsmtmp;
265
+ out[2] = in_[2] ^ (this.r[0] + this.S[2]) ^ this.r[1] ^ this.S[3];
266
+ this.S[3] = a_mul(this.S[3]) ^ this.S[0] ^ ainv_mul(this.S[14]);
267
+ fsmtmp = this.r[1] + this.S[0];
268
+ this.r[1] = T(this.r[0]);
269
+ this.r[0] = fsmtmp;
270
+ out[3] = in_[3] ^ (this.r[0] + this.S[3]) ^ this.r[1] ^ this.S[4];
271
+ this.S[4] = a_mul(this.S[4]) ^ this.S[1] ^ ainv_mul(this.S[15]);
272
+ fsmtmp = this.r[1] + this.S[1];
273
+ this.r[1] = T(this.r[0]);
274
+ this.r[0] = fsmtmp;
275
+ out[4] = in_[4] ^ (this.r[0] + this.S[4]) ^ this.r[1] ^ this.S[5];
276
+ this.S[5] = a_mul(this.S[5]) ^ this.S[2] ^ ainv_mul(this.S[0]);
277
+ fsmtmp = this.r[1] + this.S[2];
278
+ this.r[1] = T(this.r[0]);
279
+ this.r[0] = fsmtmp;
280
+ out[5] = in_[5] ^ (this.r[0] + this.S[5]) ^ this.r[1] ^ this.S[6];
281
+ this.S[6] = a_mul(this.S[6]) ^ this.S[3] ^ ainv_mul(this.S[1]);
282
+ fsmtmp = this.r[1] + this.S[3];
283
+ this.r[1] = T(this.r[0]);
284
+ this.r[0] = fsmtmp;
285
+ out[6] = in_[6] ^ (this.r[0] + this.S[6]) ^ this.r[1] ^ this.S[7];
286
+ this.S[7] = a_mul(this.S[7]) ^ this.S[4] ^ ainv_mul(this.S[2]);
287
+ fsmtmp = this.r[1] + this.S[4];
288
+ this.r[1] = T(this.r[0]);
289
+ this.r[0] = fsmtmp;
290
+ out[7] = in_[7] ^ (this.r[0] + this.S[7]) ^ this.r[1] ^ this.S[8];
291
+ this.S[8] = a_mul(this.S[8]) ^ this.S[5] ^ ainv_mul(this.S[3]);
292
+ fsmtmp = this.r[1] + this.S[5];
293
+ this.r[1] = T(this.r[0]);
294
+ this.r[0] = fsmtmp;
295
+ out[8] = in_[8] ^ (this.r[0] + this.S[8]) ^ this.r[1] ^ this.S[9];
296
+ this.S[9] = a_mul(this.S[9]) ^ this.S[6] ^ ainv_mul(this.S[4]);
297
+ fsmtmp = this.r[1] + this.S[6];
298
+ this.r[1] = T(this.r[0]);
299
+ this.r[0] = fsmtmp;
300
+ out[9] = in_[9] ^ (this.r[0] + this.S[9]) ^ this.r[1] ^ this.S[10];
301
+ this.S[10] = a_mul(this.S[10]) ^ this.S[7] ^ ainv_mul(this.S[5]);
302
+ fsmtmp = this.r[1] + this.S[7];
303
+ this.r[1] = T(this.r[0]);
304
+ this.r[0] = fsmtmp;
305
+ out[10] = in_[10] ^ (this.r[0] + this.S[10]) ^ this.r[1] ^ this.S[11];
306
+ this.S[11] = a_mul(this.S[11]) ^ this.S[8] ^ ainv_mul(this.S[6]);
307
+ fsmtmp = this.r[1] + this.S[8];
308
+ this.r[1] = T(this.r[0]);
309
+ this.r[0] = fsmtmp;
310
+ out[11] = in_[11] ^ (this.r[0] + this.S[11]) ^ this.r[1] ^ this.S[12];
311
+ this.S[12] = a_mul(this.S[12]) ^ this.S[9] ^ ainv_mul(this.S[7]);
312
+ fsmtmp = this.r[1] + this.S[9];
313
+ this.r[1] = T(this.r[0]);
314
+ this.r[0] = fsmtmp;
315
+ out[12] = in_[12] ^ (this.r[0] + this.S[12]) ^ this.r[1] ^ this.S[13];
316
+ this.S[13] = a_mul(this.S[13]) ^ this.S[10] ^ ainv_mul(this.S[8]);
317
+ fsmtmp = this.r[1] + this.S[10];
318
+ this.r[1] = T(this.r[0]);
319
+ this.r[0] = fsmtmp;
320
+ out[13] = in_[13] ^ (this.r[0] + this.S[13]) ^ this.r[1] ^ this.S[14];
321
+ this.S[14] = a_mul(this.S[14]) ^ this.S[11] ^ ainv_mul(this.S[9]);
322
+ fsmtmp = this.r[1] + this.S[11];
323
+ this.r[1] = T(this.r[0]);
324
+ this.r[0] = fsmtmp;
325
+ out[14] = in_[14] ^ (this.r[0] + this.S[14]) ^ this.r[1] ^ this.S[15];
326
+ this.S[15] = a_mul(this.S[15]) ^ this.S[12] ^ ainv_mul(this.S[10]);
327
+ fsmtmp = this.r[1] + this.S[12];
328
+ this.r[1] = T(this.r[0]);
329
+ this.r[0] = fsmtmp;
330
+ out[15] = in_[15] ^ (this.r[0] + this.S[15]) ^ this.r[1] ^ this.S[0];
331
+ return out;
332
+ }
333
+ /**
334
+ * Perform encryption/decryption
335
+ * @param in_ Input data
336
+ */
337
+ crypt(in_) {
338
+ const inl = in_.length;
339
+ const out = new Uint8Array(inl);
340
+ let inOffset = 0;
341
+ let outOffset = 0;
342
+ const blockBuffer = new ArrayBuffer(this.blockSize);
343
+ const block64 = new BigUint64Array(blockBuffer);
344
+ const block8 = new Uint8Array(blockBuffer);
345
+ while (inl - inOffset >= this.blockSize) {
346
+ block8.set(in_.slice(inOffset, inOffset + this.blockSize));
347
+ const encrypted8 = new Uint8Array(this.next_stream_full_crypt(block64).buffer, 0, this.blockSize);
348
+ out.set(encrypted8, outOffset);
349
+ inOffset += this.blockSize;
350
+ outOffset += this.blockSize;
351
+ }
352
+ if (inOffset < inl) {
353
+ const remaining = inl - inOffset;
354
+ const keystream = new Uint8Array(this.next_stream().buffer, 0, this.blockSize);
355
+ for (let i = 0; i < remaining; i++)
356
+ out[outOffset + i] = in_[inOffset + i] ^ keystream[i];
357
+ }
358
+ return out;
359
+ }
360
+ }
package/types.d.ts ADDED
@@ -0,0 +1,41 @@
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
+ /** GMAC mode for {@link Cipher} */
31
+ export type GMACMode = {
32
+ /** Compute MAC */
33
+ compute: (aad: TArg<Uint8Array>, msg: TArg<Uint8Array>) => TRet<Uint8Array>;
34
+ };
35
+ /** AEAD mode for {@link Cipher} */
36
+ export type AEADMode = {
37
+ /** Seal plaintext and AAD */
38
+ seal: (plaintext: TArg<Uint8Array>, aad?: TArg<Uint8Array>) => TRet<Uint8Array>;
39
+ /** Open ciphertext and AAD */
40
+ open: (ciphertext: TArg<Uint8Array>, aad?: TArg<Uint8Array>) => TRet<Uint8Array>;
41
+ };
package/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/utils.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { type TArg, type TRet } from "@noble/hashes/utils.js";
2
+ export declare const xorBytes: (a: TArg<Uint8Array>, b: TArg<Uint8Array>) => TRet<Uint8Array>;
3
+ export declare const bytesToUint64sBE: (b: TArg<Uint8Array>) => TRet<BigUint64Array>;
4
+ export declare const uint64sToBytesBE: (w: TArg<BigUint64Array>) => TRet<Uint8Array>;
5
+ export declare const bytesToUint64sLE: (b: TArg<Uint8Array>) => TRet<BigUint64Array>;
6
+ export declare const uint64sToBytesLE: (w: TArg<BigUint64Array>) => TRet<Uint8Array>;
7
+ export declare const hexToNumber: (hex: string) => bigint;
8
+ export declare const bytesToNumberBE: (bytes: TArg<Uint8Array>) => bigint;
9
+ export declare const numberToBytesBE: (n: number | bigint, len: number) => TRet<Uint8Array>;
10
+ export declare const bytesToNumberLE: (bytes: TArg<Uint8Array>) => bigint;
11
+ export declare const numberToBytesLE: (n: number | bigint, len: number) => TRet<Uint8Array>;
12
+ export declare const byte: (a: bigint) => number;
13
+ export declare const gf2mMul: (blockSize: number, a: TArg<Uint8Array>, b: TArg<Uint8Array>) => TRet<Uint8Array>;
14
+ export declare const equalBytes: (a: TArg<Uint8Array>, b: TArg<Uint8Array>) => boolean;
package/utils.js ADDED
@@ -0,0 +1,81 @@
1
+ import { bytesToHex, copyBytes, hexToBytes } from "@noble/hashes/utils.js";
2
+ export const xorBytes = (a, b) => {
3
+ const mlen = Math.min(a.length, b.length);
4
+ const result = new Uint8Array(mlen);
5
+ for (let i = 0; i < mlen; i++)
6
+ result[i] = a[i] ^ b[i];
7
+ return result;
8
+ };
9
+ export const bytesToUint64sBE = (b) => {
10
+ const size = Math.floor(b.length / 8);
11
+ const result = new BigUint64Array(size);
12
+ for (let i = 0; i < size; i++)
13
+ result[i] = bytesToNumberBE(b.subarray(i * 8, i * 8 + 8));
14
+ return result;
15
+ };
16
+ export const uint64sToBytesBE = (w) => {
17
+ const result = new Uint8Array(w.length * 8);
18
+ for (let i = 0; i < w.length; i++)
19
+ result.set(numberToBytesBE(w[i], 8), i * 8);
20
+ return result;
21
+ };
22
+ export const bytesToUint64sLE = (b) => {
23
+ const size = Math.floor(b.length / 8);
24
+ const result = new BigUint64Array(size);
25
+ for (let i = 0; i < size; i++)
26
+ result[i] = bytesToNumberLE(b.subarray(i * 8, i * 8 + 8));
27
+ return result;
28
+ };
29
+ export const uint64sToBytesLE = (w) => {
30
+ const result = new Uint8Array(w.length * 8);
31
+ for (let i = 0; i < w.length; i++)
32
+ result.set(numberToBytesLE(w[i], 8), i * 8);
33
+ return result;
34
+ };
35
+ export const hexToNumber = (hex) => {
36
+ if (typeof hex !== 'string')
37
+ throw new Error('hex string expected, got ' + typeof hex);
38
+ return hex === '' ? 0n : BigInt('0x' + hex);
39
+ };
40
+ export const bytesToNumberBE = (bytes) => hexToNumber(bytesToHex(bytes));
41
+ export const numberToBytesBE = (n, len) => {
42
+ let num = n.toString(16).padStart(len * 2, '0');
43
+ while (num.length % 2 != 0)
44
+ num = "0" + num;
45
+ return hexToBytes(num);
46
+ };
47
+ export const bytesToNumberLE = (bytes) => hexToNumber(bytesToHex(copyBytes(bytes).reverse()));
48
+ export const numberToBytesLE = (n, len) => numberToBytesBE(n, len).reverse();
49
+ export const byte = (a) => Number(a & 0xffn);
50
+ export const gf2mMul = (blockSize, a, b) => {
51
+ const temp = new Uint8Array(a);
52
+ const result = new Uint8Array(blockSize);
53
+ const reductionBytes = [[0x87], [0x25, 0x04], [0x25, 0x01]][Math.log2(blockSize) - 4];
54
+ for (let i = 0; i < blockSize * 8; i++) {
55
+ const byteIndex = Math.floor(i / 8);
56
+ const bitIndex = i % 8;
57
+ if (byteIndex < b.length && (b[byteIndex] & (1 << bitIndex)))
58
+ for (let j = 0; j < blockSize; j++)
59
+ result[j] ^= temp[j];
60
+ let carry = 0;
61
+ for (let j = 0; j < blockSize; j++) {
62
+ const nextCarry = (temp[j] & 0x80) ? 1 : 0;
63
+ temp[j] = ((temp[j] << 1) & 0xFF) | carry;
64
+ carry = nextCarry;
65
+ }
66
+ if (carry) {
67
+ for (let j = 0; j < reductionBytes.length; j++)
68
+ if (j < blockSize)
69
+ temp[j] ^= reductionBytes[j];
70
+ }
71
+ }
72
+ return result;
73
+ };
74
+ export const equalBytes = (a, b) => {
75
+ if (a.length !== b.length)
76
+ return false;
77
+ let diff = 0;
78
+ for (let i = 0; i < a.length; i++)
79
+ diff |= a[i] ^ b[i];
80
+ return diff === 0;
81
+ };