@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
package/modes/mac.js ADDED
@@ -0,0 +1,65 @@
1
+ import { concatBytes, copyBytes } from "@noble/hashes/utils.js";
2
+ import { pad } from "../padding.js";
3
+ import { gf2mMul, numberToBytesLE, xorBytes } from "../utils.js";
4
+ /** Message Authentication Code (MAC) mode */
5
+ export const cmac = (cipher, q = 16) => {
6
+ return {
7
+ compute: (msg) => {
8
+ let data = copyBytes(msg);
9
+ const zeroBlock = new Uint8Array(cipher.blockSize);
10
+ if (data.length % cipher.blockSize !== 0) {
11
+ data = pad(data, cipher.blockSize);
12
+ zeroBlock[0] = 1;
13
+ }
14
+ const Kd = cipher.encrypt(zeroBlock);
15
+ let c = new Uint8Array(cipher.blockSize);
16
+ const numBlocks = data.length / cipher.blockSize;
17
+ for (let i = 0; i < numBlocks - 1; i++) {
18
+ const blockStart = i * cipher.blockSize;
19
+ c = cipher.encrypt(xorBytes(c, data.subarray(blockStart, blockStart + cipher.blockSize)));
20
+ }
21
+ const lastBlockStart = (numBlocks - 1) * cipher.blockSize;
22
+ c = cipher.encrypt(xorBytes(xorBytes(c, data.subarray(lastBlockStart, lastBlockStart + cipher.blockSize)), Kd));
23
+ return c.slice(0, q);
24
+ }
25
+ };
26
+ };
27
+ /** Galois Message Authentication Code (GMAC) mode */
28
+ export const gmac = (cipher, q = 16) => {
29
+ return {
30
+ compute: (aad, msg) => {
31
+ const blockSize = cipher.blockSize;
32
+ const H = cipher.encrypt(new Uint8Array(blockSize));
33
+ let B = new Uint8Array(blockSize);
34
+ let i = 0;
35
+ while (i < aad.length) {
36
+ const blockSizeToProcess = Math.min(blockSize, aad.length - i);
37
+ const block = new Uint8Array(blockSize);
38
+ for (let j = 0; j < blockSizeToProcess; j++)
39
+ block[j] = aad[i + j];
40
+ if (blockSizeToProcess < blockSize)
41
+ block[blockSizeToProcess] = 0x80;
42
+ for (let j = 0; j < blockSize; j++)
43
+ B[j] ^= block[j];
44
+ B = gf2mMul(blockSize, B, H);
45
+ i += blockSize;
46
+ }
47
+ i = 0;
48
+ while (i < msg.length) {
49
+ const blockSizeToProcess = Math.min(blockSize, msg.length - i);
50
+ const block = new Uint8Array(blockSize);
51
+ for (let j = 0; j < blockSizeToProcess; j++)
52
+ block[j] = msg[i + j];
53
+ if (blockSizeToProcess < blockSize)
54
+ block[blockSizeToProcess] = 0x80;
55
+ for (let j = 0; j < blockSize; j++)
56
+ B[j] ^= block[j];
57
+ B = gf2mMul(blockSize, B, H);
58
+ i += blockSize;
59
+ }
60
+ B = xorBytes(B, concatBytes(numberToBytesLE(aad.length * 8, blockSize / 2), numberToBytesLE(msg.length * 8, blockSize / 2)));
61
+ // B = gf2mMul(blockSize, B, H);
62
+ return cipher.encrypt(B).slice(0, q);
63
+ }
64
+ };
65
+ };
package/modes/ofb.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
+ /** Output Feedback (OFB) mode */
5
+ export declare const ofb: (cipher: Kalyna, iv: TArg<Uint8Array>) => StreamMode;
package/modes/ofb.js ADDED
@@ -0,0 +1,20 @@
1
+ import {} from "@noble/hashes/utils.js";
2
+ import { xorBytes } from "../utils.js";
3
+ /** Output Feedback (OFB) mode */
4
+ export const ofb = (cipher, iv) => {
5
+ if (iv.length !== cipher.blockSize)
6
+ throw new Error("Invalid IV size");
7
+ const encrypter = cipher.encrypt.bind(cipher);
8
+ return {
9
+ crypt: (msg) => {
10
+ let buf = new Uint8Array(iv);
11
+ const output = new Uint8Array(msg.length);
12
+ for (let i = 0; i < msg.length; i += cipher.blockSize) {
13
+ const enc = encrypter(buf);
14
+ output.set(xorBytes(enc, msg.subarray(i, i + cipher.blockSize)), i);
15
+ buf = enc;
16
+ }
17
+ return output;
18
+ }
19
+ };
20
+ };
package/package.json CHANGED
@@ -1,10 +1,52 @@
1
1
  {
2
2
  "name": "@li0ard/dstu",
3
- "version": "0.0.1",
4
- "description": "OIDC trusted publishing setup package for @li0ard/dstu",
5
- "keywords": [
6
- "oidc",
7
- "trusted-publishing",
8
- "setup"
9
- ]
3
+ "version": "0.1.1",
4
+ "main": "index.js",
5
+ "types": "index.d.ts",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": "./index.js",
9
+ "./dstu4145.js": "./dstu4145/index.js",
10
+ "./kalyna.js": "./kalyna/index.js",
11
+ "./kupyna.js": "./kupyna/index.js",
12
+ "./modes.js": "./modes/index.js",
13
+ "./strumok.js": "./strumok/index.js",
14
+ "./types.js": "./types.js"
15
+ },
16
+ "author": "li0ard",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/li0ard/dstu.git"
20
+ },
21
+ "bugs": {
22
+ "url": "https://github.com/li0ard/dstu/issues"
23
+ },
24
+ "homepage": "https://github.com/li0ard/dstu#readme",
25
+ "funding": "https://li0ard.rest/donate",
26
+ "description": "DSTU cryptography algorithms in pure TypeScript",
27
+ "license": "MIT",
28
+ "scripts": {
29
+ "build": "rm -rf dist/ && tsc --project tsconfig.build.json",
30
+ "docs": "typedoc --options .config/typedoc/config.cjs"
31
+ },
32
+ "devDependencies": {
33
+ "@li0ard/gost": "^0.2.0",
34
+ "@peculiar/asn1-schema": "^2.8.0",
35
+ "@peculiar/asn1-x509": "^2.8.0",
36
+ "@types/bn.js": "^5.2.0",
37
+ "@types/bun": "latest",
38
+ "typedoc": "^0.28.19"
39
+ },
40
+ "peerDependencies": {
41
+ "typescript": "^5"
42
+ },
43
+ "dependencies": {
44
+ "@noble/hashes": "^2.2.0",
45
+ "bn.js": "^5.2.3"
46
+ },
47
+ "files": ["**/*.js", "**/*.d.ts"],
48
+ "publishConfig": {
49
+ "access": "public"
50
+ },
51
+ "keywords": ["dstu", "crypto", "kalyna", "kupyna", "strumok"]
10
52
  }
package/padding.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import type { TArg, TRet } from "@noble/hashes/utils.js";
2
+ export declare const pad: (data: TArg<Uint8Array>, blockSize: number) => TRet<Uint8Array>;
3
+ export declare const unpad: (paddedData: TArg<Uint8Array>, blockSize: number) => TRet<Uint8Array>;
package/padding.js ADDED
@@ -0,0 +1,22 @@
1
+ export const pad = (data, blockSize) => {
2
+ const remainder = data.length % blockSize;
3
+ if (remainder === 0)
4
+ return data;
5
+ const padLength = blockSize - remainder;
6
+ const padded = new Uint8Array(data.length + padLength);
7
+ padded.set(data);
8
+ padded[data.length] = 0x80;
9
+ return padded;
10
+ };
11
+ export const unpad = (paddedData, blockSize) => {
12
+ if (paddedData.length % blockSize !== 0)
13
+ throw new Error("Padded data length must be multiple of block size");
14
+ let i = paddedData.length - 1;
15
+ while (i >= 0 && paddedData[i] === 0)
16
+ i--;
17
+ if (i < 0)
18
+ throw new Error("Padding error: all bytes are zero");
19
+ if (i === paddedData.length - 1)
20
+ throw new Error("Padding error: no padding bytes found");
21
+ return paddedData.slice(0, i + 1);
22
+ };
@@ -0,0 +1,21 @@
1
+ import type { TArg, TRet } from "@noble/hashes/utils.js";
2
+ /** Strumok stream cipher */
3
+ export declare class Strumok {
4
+ readonly blockSize = 128;
5
+ readonly keySize: number;
6
+ private S;
7
+ private r;
8
+ private key;
9
+ private iv;
10
+ /**
11
+ * Strumok stream cipher
12
+ * @param key Encryption key (256 or 512 bit)
13
+ */
14
+ constructor(key: TArg<Uint8Array>, iv: TArg<Uint8Array>);
15
+ /** Generate next keystream */
16
+ next_stream(): TRet<BigUint64Array>;
17
+ /** Generate next keystream and perform encryption */
18
+ next_stream_full_crypt(in_: TArg<BigUint64Array>): TRet<BigUint64Array>;
19
+ /** Perform encryption/decryption */
20
+ crypt(msg: TArg<Uint8Array>): TRet<Uint8Array>;
21
+ }
@@ -0,0 +1,352 @@
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
+ /** Perform encryption/decryption */
334
+ crypt(msg) {
335
+ const out = new Uint8Array(msg.length);
336
+ let inOffset = 0, outOffset = 0;
337
+ const blockBuffer = new ArrayBuffer(this.blockSize), block = new BigUint64Array(blockBuffer), block8 = new Uint8Array(blockBuffer);
338
+ while (msg.length - inOffset >= this.blockSize) {
339
+ block8.set(msg.subarray(inOffset, inOffset + this.blockSize));
340
+ const encrypted = new Uint8Array(this.next_stream_full_crypt(block).buffer, 0, this.blockSize);
341
+ out.set(encrypted, outOffset);
342
+ inOffset += this.blockSize;
343
+ outOffset += this.blockSize;
344
+ }
345
+ if (inOffset < msg.length) {
346
+ const keystream = new Uint8Array(this.next_stream().buffer, 0, this.blockSize);
347
+ for (let i = 0; i < msg.length - inOffset; i++)
348
+ out[outOffset + i] = msg[inOffset + i] ^ keystream[i];
349
+ }
350
+ return out;
351
+ }
352
+ }
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;