@li0ard/dstu 0.0.1 → 0.1.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/LICENSE +21 -0
  2. package/README.md +52 -45
  3. package/const.d.ts +14 -0
  4. package/const.js +1084 -0
  5. package/dstu4145/const.d.ts +59 -0
  6. package/dstu4145/const.js +197 -0
  7. package/dstu4145/ec/expand.d.ts +6 -0
  8. package/dstu4145/ec/expand.js +129 -0
  9. package/dstu4145/ec/index.d.ts +823 -0
  10. package/dstu4145/ec/index.js +301 -0
  11. package/dstu4145/ec/onb.d.ts +6 -0
  12. package/dstu4145/ec/onb.js +100 -0
  13. package/dstu4145/ec/wnaf.d.ts +4 -0
  14. package/dstu4145/ec/wnaf.js +63 -0
  15. package/dstu4145/index.d.ts +180 -0
  16. package/dstu4145/index.js +93 -0
  17. package/index.d.ts +6 -0
  18. package/index.js +6 -0
  19. package/kalyna/index.d.ts +51 -0
  20. package/kalyna/index.js +210 -0
  21. package/kupyna/index.d.ts +162 -0
  22. package/kupyna/index.js +233 -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 +34 -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 +20 -0
  41. package/package.json +49 -7
  42. package/padding.d.ts +3 -0
  43. package/padding.js +22 -0
  44. package/strumok/index.d.ts +21 -0
  45. package/strumok/index.js +352 -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,233 @@
1
+ import { concatBytes, createHasher } from "@noble/hashes/utils.js";
2
+ import { bytesToUint64sLE, numberToBytesLE, uint64sToBytesLE } from "../utils.js";
3
+ import { column } from "../kalyna/index.js";
4
+ const r = 0x00f0f0f0f0f0f0f3n;
5
+ class Kupyna {
6
+ blockLen;
7
+ outputLen;
8
+ canXOF = false;
9
+ rounds;
10
+ stSize;
11
+ threshold;
12
+ s;
13
+ x;
14
+ nx;
15
+ len;
16
+ constructor(blockLen) {
17
+ this.blockLen = blockLen;
18
+ this.stSize = (blockLen / 2) / 4;
19
+ this.threshold = blockLen - 12;
20
+ this.rounds = 4 * Math.log2(blockLen) - 14;
21
+ this.outputLen = blockLen / 2;
22
+ this.destroy();
23
+ }
24
+ destroy() {
25
+ this.s = new BigUint64Array(this.stSize);
26
+ this.x = new Uint8Array(this.blockLen);
27
+ this.nx = 0;
28
+ this.len = 0n;
29
+ this.s[0] = BigInt(this.blockLen);
30
+ }
31
+ update(data) {
32
+ this.len += BigInt(data.length);
33
+ if (this.nx > 0) {
34
+ const n = Math.min(this.blockLen - this.nx, data.length);
35
+ this.x.set(data.subarray(0, n), this.nx);
36
+ this.nx += n;
37
+ if (this.nx === this.blockLen) {
38
+ this.F(bytesToUint64sLE(this.x));
39
+ this.nx = 0;
40
+ }
41
+ data = data.slice(n);
42
+ }
43
+ while (data.length >= this.blockLen) {
44
+ this.F(bytesToUint64sLE(data.subarray(0, this.blockLen)));
45
+ this.nx = 0;
46
+ data = data.slice(this.blockLen);
47
+ }
48
+ if (data.length > 0) {
49
+ this.x.set(data, 0);
50
+ this.nx = data.length;
51
+ }
52
+ return this;
53
+ }
54
+ digest() {
55
+ const buffer = new Uint8Array(this.outputLen);
56
+ this.digestInto(buffer);
57
+ return buffer;
58
+ }
59
+ digestInto(buffer) {
60
+ this.x[this.nx] = 0x80;
61
+ this.nx++;
62
+ const fillBytes = (start) => {
63
+ const available = this.x.length - start;
64
+ if (available > 0)
65
+ this.x.fill(0, start, start + available);
66
+ };
67
+ if (this.nx > this.threshold) {
68
+ fillBytes(this.nx);
69
+ this.F(bytesToUint64sLE(this.x));
70
+ this.nx = 0;
71
+ }
72
+ fillBytes(this.nx);
73
+ this.x.set(numberToBytesLE(this.len * 8n, 12), this.threshold);
74
+ this.F(bytesToUint64sLE(this.x));
75
+ this.outputTransform();
76
+ buffer.set(uint64sToBytesLE(this.s).subarray(this.outputLen));
77
+ this.destroy();
78
+ }
79
+ column(x, i) {
80
+ return column(x, i, this.stSize, 1, 2, 3, 4, 5, 6, this.blockLen == 64 ? 7 : 11);
81
+ }
82
+ G(x, y) {
83
+ for (let i = 0; i < this.stSize; i++)
84
+ y[i] = this.column(x, i);
85
+ }
86
+ P(x, y, round) {
87
+ for (let i = 0n; i < BigInt(this.stSize); i++)
88
+ x[Number(i)] ^= (i << 4n) ^ round;
89
+ const r1 = round + 1n;
90
+ for (let i = 0; i < this.stSize; i++)
91
+ y[i] = this.column(x, i) ^ BigInt(i << 4) ^ r1;
92
+ this.G(y, x);
93
+ }
94
+ Q(x, y, round) {
95
+ for (let j = 0n; j < BigInt(this.stSize); j++)
96
+ x[Number(j)] += r ^ ((((BigInt(this.stSize - 1) - j) * 0x10n) ^ round) << 56n);
97
+ const r1 = round + 1n;
98
+ for (let i = 0; i < this.stSize; i++)
99
+ y[i] = this.column(x, i) + (r ^ ((BigInt((this.stSize - 1 - i) * 16) ^ r1) << 56n));
100
+ this.G(y, x);
101
+ }
102
+ outputTransform() {
103
+ const t1 = new BigUint64Array(this.s), t2 = new BigUint64Array(this.stSize);
104
+ for (let r = 0n; r < BigInt(this.rounds); r += 2n)
105
+ this.P(t1, t2, r);
106
+ for (let column = 0; column < this.stSize; column++)
107
+ this.s[column] ^= t1[column];
108
+ }
109
+ F(b) {
110
+ const AQ1 = new BigUint64Array(this.stSize);
111
+ const AP1 = new BigUint64Array(this.stSize);
112
+ const tmp = new BigUint64Array(this.stSize);
113
+ for (let column = 0; column < this.stSize; column++) {
114
+ AP1[column] = this.s[column] ^ b[column];
115
+ AQ1[column] = b[column];
116
+ }
117
+ for (let r = 0n; r < BigInt(this.rounds); r += 2n) {
118
+ this.P(AP1, tmp, r);
119
+ this.Q(AQ1, tmp, r);
120
+ }
121
+ for (let column = 0; column < this.stSize; column++)
122
+ this.s[column] ^= AP1[column] ^ AQ1[column];
123
+ }
124
+ }
125
+ class KupynaDerived {
126
+ hash;
127
+ slice;
128
+ outputLen;
129
+ blockLen;
130
+ canXOF = false;
131
+ buffer = new Uint8Array();
132
+ constructor(hash, slice) {
133
+ this.hash = hash;
134
+ this.slice = slice;
135
+ this.outputLen = Math.abs(slice);
136
+ this.blockLen = hash().blockLen;
137
+ }
138
+ destroy() { }
139
+ update(data) {
140
+ this.buffer = concatBytes(this.buffer, data);
141
+ return this;
142
+ }
143
+ digest() {
144
+ const buffer = new Uint8Array(this.outputLen);
145
+ this.digestInto(buffer);
146
+ return buffer;
147
+ }
148
+ digestInto(buffer) {
149
+ buffer.set(this.hash().update(this.buffer).digest().subarray(this.slice));
150
+ }
151
+ }
152
+ /** Kupyna 256 bit version */
153
+ export class Kupyna256 extends Kupyna {
154
+ /** Kupyna 256 bit version */
155
+ constructor() { super(64); }
156
+ _cloneInto(to) {
157
+ to ||= new Kupyna256();
158
+ to.s = new BigUint64Array(this.s);
159
+ to.x = new Uint8Array(this.x);
160
+ to.nx = this.nx;
161
+ to.len = this.len;
162
+ return to;
163
+ }
164
+ clone() { return this._cloneInto(); }
165
+ /** Create hash instance */
166
+ static create() { return new Kupyna256(); }
167
+ }
168
+ /** Kupyna 512 bit version */
169
+ export class Kupyna512 extends Kupyna {
170
+ /** Kupyna 512 bit version */
171
+ constructor() { super(128); }
172
+ _cloneInto(to) {
173
+ to ||= new Kupyna512();
174
+ to.s = new BigUint64Array(this.s);
175
+ to.x = new Uint8Array(this.x);
176
+ to.nx = this.nx;
177
+ to.len = this.len;
178
+ return to;
179
+ }
180
+ clone() { return this._cloneInto(); }
181
+ /** Create hash instance */
182
+ static create() { return new Kupyna512(); }
183
+ }
184
+ /** Kupyna 48 bit version */
185
+ export class Kupyna48 extends KupynaDerived {
186
+ /** Kupyna 48 bit version */
187
+ constructor() { super(Kupyna256.create, -6); }
188
+ /** Create hash instance */
189
+ static create() { return new Kupyna48(); }
190
+ _cloneInto(to) {
191
+ to ||= new Kupyna48();
192
+ to.buffer = new Uint8Array(this.buffer);
193
+ return to;
194
+ }
195
+ clone() { return this._cloneInto(); }
196
+ }
197
+ /** Kupyna 304 bit version */
198
+ export class Kupyna304 extends KupynaDerived {
199
+ /** Kupyna 304 bit version */
200
+ constructor() { super(Kupyna512.create, -38); }
201
+ /** Create hash instance */
202
+ static create() { return new Kupyna304(); }
203
+ _cloneInto(to) {
204
+ to ||= new Kupyna304();
205
+ to.buffer = new Uint8Array(this.buffer);
206
+ return to;
207
+ }
208
+ clone() { return this._cloneInto(); }
209
+ }
210
+ /** Kupyna 384 bit version */
211
+ export class Kupyna384 extends KupynaDerived {
212
+ /** Kupyna 384 bit version */
213
+ constructor() { super(Kupyna512.create, -48); }
214
+ /** Create hash instance */
215
+ static create() { return new Kupyna384(); }
216
+ _cloneInto(to) {
217
+ to ||= new Kupyna384();
218
+ to.buffer = new Uint8Array(this.buffer);
219
+ return to;
220
+ }
221
+ clone() { return this._cloneInto(); }
222
+ }
223
+ /** Kupyna 48 bit version */
224
+ export const kupyna48 = createHasher(Kupyna48.create);
225
+ /** Kupyna 256 bit version */
226
+ export const kupyna256 = createHasher(Kupyna256.create);
227
+ /** Kupyna 304 bit version */
228
+ export const kupyna304 = createHasher(Kupyna304.create);
229
+ /** Kupyna 384 bit version */
230
+ export const kupyna384 = createHasher(Kupyna384.create);
231
+ /** Kupyna 512 bit version */
232
+ export const kupyna512 = createHasher(Kupyna512.create);
233
+ export * from "./kmac.js";
@@ -0,0 +1,46 @@
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 {};
package/kupyna/kmac.js ADDED
@@ -0,0 +1,124 @@
1
+ import { numberToBytesLE, uint64sToBytesLE } from "../utils.js";
2
+ import { Kupyna256, Kupyna384, Kupyna512 } from "./index.js";
3
+ const dpad = numberToBytesLE(128, 128);
4
+ const kpad32 = new Uint8Array([
5
+ 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
6
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7
+ ]);
8
+ const kpad48 = new Uint8Array([
9
+ 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
10
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
11
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
13
+ 0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
14
+ ]);
15
+ const kpad64 = new Uint8Array([
16
+ 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
17
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
18
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
19
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
20
+ ]);
21
+ class KupynaKMAC {
22
+ outputLen;
23
+ blockLen;
24
+ canXOF = false;
25
+ threshold;
26
+ h;
27
+ ik;
28
+ len;
29
+ constructor(hash, kpad, key) {
30
+ this.len = 0n;
31
+ this.h = hash();
32
+ this.outputLen = this.h.outputLen;
33
+ this.blockLen = this.h.blockLen;
34
+ if (key.length != this.h.outputLen)
35
+ throw new Error("Invalid key length");
36
+ this.h.update(key);
37
+ this.h.update(kpad);
38
+ this.ik = new Uint8Array(key.length);
39
+ for (let i = 0; i < key.length; i++)
40
+ this.ik[i] = ~key[i] & 0xFF;
41
+ this.threshold = BigInt(this.blockLen - 12);
42
+ }
43
+ update(data) {
44
+ this.len += BigInt(data.length);
45
+ this.h.update(data);
46
+ return this;
47
+ }
48
+ digest() {
49
+ const buffer = new Uint8Array(this.outputLen);
50
+ this.digestInto(buffer);
51
+ return buffer;
52
+ }
53
+ digestInto(buffer) {
54
+ let n = this.len;
55
+ let pad_size;
56
+ if (n < this.threshold)
57
+ pad_size = this.threshold - 1n - n;
58
+ else
59
+ pad_size = (BigInt(this.blockLen) - 1n) - ((n - this.threshold) % BigInt(this.blockLen));
60
+ n *= 8n;
61
+ this.h.update(dpad.slice(0, Number(pad_size + 1n)));
62
+ this.h.update(uint64sToBytesLE(new BigUint64Array([n])));
63
+ this.h.update(dpad.slice(16, 20));
64
+ this.h.update(this.ik);
65
+ this.h.digestInto(buffer);
66
+ }
67
+ destroy() {
68
+ throw new Error("Not implemented");
69
+ }
70
+ }
71
+ /** Kupyna KMAC (256 version) */
72
+ export class KupynaKMAC256 extends KupynaKMAC {
73
+ key;
74
+ constructor(key) {
75
+ super(Kupyna256.create, kpad32, key);
76
+ this.key = key;
77
+ }
78
+ _cloneInto(to) {
79
+ to ||= new KupynaKMAC256(this.key);
80
+ to.h = this.h.clone();
81
+ to.len = this.len;
82
+ to.ik = new Uint8Array(this.ik);
83
+ return to;
84
+ }
85
+ clone() { return this._cloneInto(); }
86
+ }
87
+ /** Kupyna KMAC (384 bit version) */
88
+ export class KupynaKMAC384 extends KupynaKMAC {
89
+ key;
90
+ constructor(key) {
91
+ super(Kupyna384.create, kpad48, key);
92
+ this.key = key;
93
+ }
94
+ _cloneInto(to) {
95
+ to ||= new KupynaKMAC384(this.key);
96
+ to.h = this.h.clone();
97
+ to.len = this.len;
98
+ to.ik = new Uint8Array(this.ik);
99
+ return to;
100
+ }
101
+ clone() { return this._cloneInto(); }
102
+ }
103
+ /** Kupyna KMAC (512 bit version) */
104
+ export class KupynaKMAC512 extends KupynaKMAC {
105
+ key;
106
+ constructor(key) {
107
+ super(Kupyna512.create, kpad64, key);
108
+ this.key = key;
109
+ }
110
+ _cloneInto(to) {
111
+ to ||= new KupynaKMAC512(this.key);
112
+ to.h = this.h.clone();
113
+ to.len = this.len;
114
+ to.ik = new Uint8Array(this.ik);
115
+ return to;
116
+ }
117
+ clone() { return this._cloneInto(); }
118
+ }
119
+ /** Kupyna KMAC (256 version) */
120
+ export const kmac256 = (key, msg) => new KupynaKMAC256(key).update(msg).digest();
121
+ /** Kupyna KMAC (384 version) */
122
+ export const kmac384 = (key, msg) => new KupynaKMAC384(key).update(msg).digest();
123
+ /** Kupyna KMAC (512 version) */
124
+ export const kmac512 = (key, msg) => new KupynaKMAC512(key).update(msg).digest();
package/modes/cbc.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import type { TArg } from "@noble/hashes/utils.js";
2
+ import type { Kalyna } from "../kalyna/index.js";
3
+ import type { BlockMode } from "../types.js";
4
+ /** Cipher Block Chaining (CBC) mode */
5
+ export declare const cbc: (cipher: Kalyna, iv: TArg<Uint8Array>) => BlockMode;
package/modes/cbc.js ADDED
@@ -0,0 +1,34 @@
1
+ import { xorBytes } from "../utils.js";
2
+ /** Cipher Block Chaining (CBC) mode */
3
+ export const cbc = (cipher, iv) => {
4
+ if (iv.length !== cipher.blockSize)
5
+ throw new Error("Invalid IV size");
6
+ const encrypter = cipher.encrypt.bind(cipher);
7
+ const decrypter = cipher.decrypt.bind(cipher);
8
+ return {
9
+ encrypt: (plaintext) => {
10
+ if (plaintext.length % cipher.blockSize !== 0)
11
+ throw new Error("Plaintext not aligned");
12
+ let buf = new Uint8Array(iv);
13
+ const output = new Uint8Array(plaintext.length);
14
+ for (let i = 0; i < plaintext.length; i += cipher.blockSize) {
15
+ const blk = encrypter(xorBytes(plaintext.subarray(i, i + cipher.blockSize), buf));
16
+ output.set(blk, i);
17
+ buf = blk.slice();
18
+ }
19
+ return output;
20
+ },
21
+ decrypt: (ciphertext) => {
22
+ if (ciphertext.length % cipher.blockSize !== 0)
23
+ throw new Error("Ciphertext not aligned");
24
+ let buf = new Uint8Array(iv);
25
+ const output = new Uint8Array(ciphertext.length);
26
+ for (let i = 0; i < ciphertext.length; i += cipher.blockSize) {
27
+ const blk = ciphertext.subarray(i, i + cipher.blockSize);
28
+ output.set(xorBytes(decrypter(blk), buf), i);
29
+ buf = blk.slice();
30
+ }
31
+ return output;
32
+ }
33
+ };
34
+ };
package/modes/cfb.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import type { TArg } from "@noble/hashes/utils.js";
2
+ import type { Kalyna } from "../kalyna/index.js";
3
+ import type { BlockMode } from "../types.js";
4
+ /** Cipher Feedback (CFB) mode */
5
+ export declare const cfb: (cipher: Kalyna, iv: TArg<Uint8Array>, q?: number) => BlockMode;
package/modes/cfb.js ADDED
@@ -0,0 +1,72 @@
1
+ /** Cipher Feedback (CFB) mode */
2
+ export const cfb = (cipher, iv, q = cipher.blockSize) => {
3
+ const encrypter = cipher.encrypt.bind(cipher);
4
+ return {
5
+ encrypt: (plaintext) => {
6
+ const blockSize = cipher.blockSize;
7
+ if (q !== 1 && q !== 8 && q !== 16 && q !== 32 && q !== 64)
8
+ throw new Error('q must be 1, 8, 16, 32, or 64');
9
+ if (q > blockSize)
10
+ throw new Error('q cannot exceed block size');
11
+ let gamma = encrypter(iv);
12
+ const feed = new Uint8Array(iv);
13
+ let offset = 0;
14
+ const result = new Uint8Array(plaintext.length);
15
+ let dataOff = 0;
16
+ while (offset > 0 && dataOff < plaintext.length) {
17
+ result[dataOff] = plaintext[dataOff] ^ gamma[offset];
18
+ feed[offset++] = result[dataOff++];
19
+ if (offset >= blockSize) {
20
+ gamma = encrypter(feed);
21
+ offset = blockSize - q;
22
+ }
23
+ }
24
+ while (dataOff + q <= plaintext.length) {
25
+ for (let i = 0; i < q; i++)
26
+ result[dataOff + i] = plaintext[dataOff + i] ^ gamma[blockSize - q + i];
27
+ feed.set(gamma.subarray(0, blockSize - q));
28
+ feed.set(result.subarray(dataOff, dataOff + q), blockSize - q);
29
+ gamma = encrypter(feed);
30
+ dataOff += q;
31
+ }
32
+ while (dataOff < plaintext.length) {
33
+ result[dataOff] = plaintext[dataOff] ^ gamma[blockSize - (plaintext.length - dataOff)];
34
+ dataOff++;
35
+ }
36
+ return result;
37
+ },
38
+ decrypt: (ciphertext) => {
39
+ const blockSize = cipher.blockSize;
40
+ if (q !== 1 && q !== 8 && q !== 16 && q !== 32 && q !== 64)
41
+ throw new Error('q must be 1, 8, 16, 32, or 64');
42
+ if (q > blockSize)
43
+ throw new Error('q cannot exceed block size');
44
+ let gamma = encrypter(iv);
45
+ const feed = new Uint8Array(iv);
46
+ let offset = 0;
47
+ const result = new Uint8Array(ciphertext.length);
48
+ let dataOff = 0;
49
+ while (offset > 0 && dataOff < ciphertext.length) {
50
+ result[dataOff] = ciphertext[dataOff] ^ gamma[offset];
51
+ feed[offset++] = ciphertext[dataOff++];
52
+ if (offset >= blockSize) {
53
+ gamma = encrypter(feed);
54
+ offset = blockSize - q;
55
+ }
56
+ }
57
+ while (dataOff + q <= ciphertext.length) {
58
+ for (let i = 0; i < q; i++)
59
+ result[dataOff + i] = ciphertext[dataOff + i] ^ gamma[blockSize - q + i];
60
+ feed.set(gamma.subarray(0, blockSize - q));
61
+ feed.set(ciphertext.subarray(dataOff, dataOff + q), blockSize - q);
62
+ gamma = encrypter(feed);
63
+ dataOff += q;
64
+ }
65
+ while (dataOff < ciphertext.length) {
66
+ result[dataOff] = ciphertext[dataOff] ^ gamma[blockSize - (ciphertext.length - dataOff)];
67
+ dataOff++;
68
+ }
69
+ return result;
70
+ }
71
+ };
72
+ };
package/modes/ctr.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { type TArg } from "@noble/hashes/utils.js";
2
+ import type { Kalyna } from "../kalyna/index.js";
3
+ import type { StreamMode } from "../types.js";
4
+ /** Counter (CTR) mode */
5
+ export declare const ctr: (cipher: Kalyna, iv: TArg<Uint8Array>) => StreamMode;
package/modes/ctr.js ADDED
@@ -0,0 +1,24 @@
1
+ import { concatBytes } from "@noble/hashes/utils.js";
2
+ import { xorBytes } from "../utils.js";
3
+ const incrementCounterAt = (ctr, pos) => {
4
+ let j = pos;
5
+ while (j < ctr.length)
6
+ if (++ctr[j++] != 0)
7
+ break;
8
+ };
9
+ /** Counter (CTR) mode */
10
+ export const ctr = (cipher, iv) => {
11
+ if (iv.length !== cipher.blockSize)
12
+ throw new Error("Invalid IV size");
13
+ return {
14
+ crypt: (msg) => {
15
+ const keystreamBlocks = [];
16
+ const ctr = cipher.encrypt(iv);
17
+ for (let i = 0; i < Math.ceil(msg.length / cipher.blockSize); i++) {
18
+ incrementCounterAt(ctr, 0);
19
+ keystreamBlocks.push(cipher.encrypt(ctr));
20
+ }
21
+ return xorBytes(concatBytes(...keystreamBlocks), msg);
22
+ }
23
+ };
24
+ };
package/modes/ecb.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import type { Kalyna } from "../kalyna/index.js";
2
+ import type { BlockMode } from "../types.js";
3
+ /** Electronic Codebook (ECB) mode */
4
+ export declare const ecb: (cipher: Kalyna) => BlockMode;
package/modes/ecb.js ADDED
@@ -0,0 +1,17 @@
1
+ /** Electronic Codebook (ECB) mode */
2
+ export const ecb = (cipher) => {
3
+ const encrypter = cipher.encrypt.bind(cipher);
4
+ const decrypter = cipher.decrypt.bind(cipher);
5
+ const core = (crypter, data) => {
6
+ if (data.length == 0 || data.length % cipher.blockSize !== 0)
7
+ throw new Error("Data not aligned");
8
+ const output = new Uint8Array(data.length);
9
+ for (let i = 0; i < data.length; i += cipher.blockSize)
10
+ output.set(crypter(data.subarray(i, i + cipher.blockSize)), i);
11
+ return output;
12
+ };
13
+ return {
14
+ encrypt: (plaintext) => core(encrypter, plaintext),
15
+ decrypt: (ciphertext) => core(decrypter, ciphertext),
16
+ };
17
+ };
package/modes/gcm.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { type TArg } from "@noble/hashes/utils.js";
2
+ import type { Kalyna } from "../kalyna/index.js";
3
+ import type { AEADMode } from "../types.js";
4
+ /** Galois counter (GCM) mode (AEAD) */
5
+ export declare const gcm: (cipher: Kalyna, iv: TArg<Uint8Array>, q?: number) => AEADMode;
package/modes/gcm.js ADDED
@@ -0,0 +1,20 @@
1
+ import { concatBytes } from "@noble/hashes/utils.js";
2
+ import { ctr } from "./ctr.js";
3
+ import { gmac } from "./mac.js";
4
+ import { equalBytes } from "../utils.js";
5
+ /** Galois counter (GCM) mode (AEAD) */
6
+ export const gcm = (cipher, iv, q = 16) => {
7
+ return {
8
+ seal: (plaintext, aad) => {
9
+ const enc = ctr(cipher, iv).crypt(plaintext);
10
+ return concatBytes(enc, gmac(cipher, q).compute(aad || new Uint8Array(), enc));
11
+ },
12
+ open: (ciphertext, aad) => {
13
+ const enc = ciphertext.subarray(0, -q);
14
+ const hC = gmac(cipher, q).compute(aad || new Uint8Array(), enc);
15
+ if (!equalBytes(ciphertext.subarray(-q), hC))
16
+ throw new Error("Invalid MAC");
17
+ return ctr(cipher, iv).crypt(enc);
18
+ }
19
+ };
20
+ };
@@ -0,0 +1,7 @@
1
+ export * from "./cbc.js";
2
+ export * from "./cfb.js";
3
+ export * from "./ctr.js";
4
+ export * from "./ecb.js";
5
+ export * from "./gcm.js";
6
+ export * from "./mac.js";
7
+ export * from "./ofb.js";
package/modes/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export * from "./cbc.js";
2
+ export * from "./cfb.js";
3
+ export * from "./ctr.js";
4
+ export * from "./ecb.js";
5
+ export * from "./gcm.js";
6
+ export * from "./mac.js";
7
+ export * from "./ofb.js";
package/modes/mac.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import type { Kalyna } from "../kalyna/index.js";
2
+ import type { GMACMode, MACMode } from "../types.js";
3
+ /** Message Authentication Code (MAC) mode */
4
+ export declare const cmac: (cipher: Kalyna, q?: number) => MACMode;
5
+ /** Galois Message Authentication Code (GMAC) mode */
6
+ export declare const gmac: (cipher: Kalyna, q?: number) => GMACMode;