@li0ard/dstu 0.1.4 → 0.1.6

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.
package/modes/mac.js CHANGED
@@ -1,65 +1,61 @@
1
1
  import { concatBytes, copyBytes } from "@noble/hashes/utils.js";
2
2
  import { pad } from "../padding.js";
3
- import { gf2mMul, numberToBytesLE, xorBytes } from "../utils.js";
3
+ import { gf2mMul, xorBytes } from "../utils.js";
4
+ import { numberToBytesLE } from "@noble/curves/utils.js";
4
5
  /** 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);
6
+ export const cmac = (cipher, q = 16) => Object.freeze({
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;
24
13
  }
25
- };
26
- };
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
+ });
27
26
  /** 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);
27
+ export const gmac = (cipher, q = 16) => Object.freeze({
28
+ compute: (aad, msg) => {
29
+ const blockSize = cipher.blockSize;
30
+ const H = cipher.encrypt(new Uint8Array(blockSize));
31
+ let B = new Uint8Array(blockSize);
32
+ let i = 0;
33
+ while (i < aad.length) {
34
+ const blockSizeToProcess = Math.min(blockSize, aad.length - i);
35
+ const block = new Uint8Array(blockSize);
36
+ for (let j = 0; j < blockSizeToProcess; j++)
37
+ block[j] = aad[i + j];
38
+ if (blockSizeToProcess < blockSize)
39
+ block[blockSizeToProcess] = 0x80;
40
+ for (let j = 0; j < blockSize; j++)
41
+ B[j] ^= block[j];
42
+ B = gf2mMul(blockSize, B, H);
43
+ i += blockSize;
44
+ }
45
+ i = 0;
46
+ while (i < msg.length) {
47
+ const blockSizeToProcess = Math.min(blockSize, msg.length - i);
48
+ const block = new Uint8Array(blockSize);
49
+ for (let j = 0; j < blockSizeToProcess; j++)
50
+ block[j] = msg[i + j];
51
+ if (blockSizeToProcess < blockSize)
52
+ block[blockSizeToProcess] = 0x80;
53
+ for (let j = 0; j < blockSize; j++)
54
+ B[j] ^= block[j];
55
+ B = gf2mMul(blockSize, B, H);
56
+ i += blockSize;
63
57
  }
64
- };
65
- };
58
+ B = xorBytes(B, concatBytes(numberToBytesLE(aad.length * 8, blockSize / 2), numberToBytesLE(msg.length * 8, blockSize / 2)));
59
+ return cipher.encrypt(B).slice(0, q);
60
+ }
61
+ });
package/modes/ofb.js CHANGED
@@ -4,17 +4,16 @@ import { xorBytes } from "../utils.js";
4
4
  export const ofb = (cipher, iv) => {
5
5
  if (iv.length !== cipher.blockSize)
6
6
  throw new Error("Invalid IV size");
7
- const encrypter = cipher.encrypt.bind(cipher);
8
- return {
7
+ return Object.freeze({
9
8
  crypt: (msg) => {
10
9
  let buf = new Uint8Array(iv);
11
10
  const output = new Uint8Array(msg.length);
12
11
  for (let i = 0; i < msg.length; i += cipher.blockSize) {
13
- const enc = encrypter(buf);
12
+ const enc = cipher.encrypt(buf);
14
13
  output.set(xorBytes(enc, msg.subarray(i, i + cipher.blockSize)), i);
15
14
  buf = enc;
16
15
  }
17
16
  return output;
18
17
  }
19
- };
18
+ });
20
19
  };
package/modes/xts.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { type TArg, type TRet } from "@noble/hashes/utils.js";
2
+ import type { Kalyna } from "../kalyna/index.js";
3
+ /** XEX Tweakable Block Ciphertext Stealing (XTS) */
4
+ export declare const xts: (cipher: Kalyna) => {
5
+ encrypt: (plaintext: TArg<Uint8Array>, tweak: TArg<Uint8Array>) => TRet<Uint8Array>;
6
+ decrypt: (ciphertext: TArg<Uint8Array>, iv: TArg<Uint8Array>) => TRet<Uint8Array>;
7
+ };
package/modes/xts.js ADDED
@@ -0,0 +1,82 @@
1
+ import { concatBytes } from "@noble/hashes/utils.js";
2
+ import { gf2mMul, xorBytes } from "../utils.js";
3
+ import { numberToBytesLE } from "@noble/curves/utils.js";
4
+ /** XEX Tweakable Block Ciphertext Stealing (XTS) */
5
+ export const xts = (cipher) => {
6
+ const two = numberToBytesLE(2, cipher.blockSize);
7
+ const increaseGamma = (gamma) => gf2mMul(cipher.blockSize, gamma, two);
8
+ const transform = (transform, block, gamma) => xorBytes(transform(xorBytes(block, gamma)), gamma);
9
+ const init = (iv) => {
10
+ const gamma = cipher.encrypt(iv);
11
+ const expanded = new Uint8Array(cipher.blockSize);
12
+ expanded.set(gamma);
13
+ return expanded;
14
+ };
15
+ return Object.freeze({
16
+ encrypt: (plaintext, tweak) => {
17
+ if (plaintext.length < cipher.blockSize)
18
+ throw new Error(`Invalid length (need at least ${cipher.blockSize}, got ${plaintext.length})`);
19
+ let gamma = init(tweak);
20
+ const encrypt = cipher.encrypt.bind(cipher);
21
+ const buffer = new Uint8Array(plaintext);
22
+ const k = Math.floor(plaintext.length / cipher.blockSize);
23
+ const r = plaintext.length % cipher.blockSize;
24
+ if (r === 0) {
25
+ for (let off = 0; off < plaintext.length; off += cipher.blockSize) {
26
+ gamma = increaseGamma(gamma);
27
+ buffer.set(transform(encrypt, buffer.subarray(off, off + cipher.blockSize), gamma), off);
28
+ }
29
+ }
30
+ else {
31
+ for (let i = 0; i < k; i++) {
32
+ gamma = increaseGamma(gamma);
33
+ const off = i * cipher.blockSize;
34
+ buffer.set(transform(encrypt, buffer.subarray(off, off + cipher.blockSize), gamma), off);
35
+ }
36
+ const last_off = (k - 1) * cipher.blockSize;
37
+ const tail_off = k * cipher.blockSize;
38
+ const scratch = buffer.slice(last_off, last_off + cipher.blockSize);
39
+ const combined = concatBytes(buffer.subarray(tail_off, tail_off + r), scratch.subarray(r));
40
+ gamma = increaseGamma(gamma);
41
+ const encrypted = transform(encrypt, combined, gamma);
42
+ buffer.set(encrypted, last_off);
43
+ buffer.set(scratch.subarray(0, r), tail_off);
44
+ }
45
+ return buffer;
46
+ },
47
+ decrypt: (ciphertext, iv) => {
48
+ if (ciphertext.length < cipher.blockSize)
49
+ throw new Error(`Invalid length (need at least ${cipher.blockSize}, got ${ciphertext.length})`);
50
+ const n = ciphertext.length;
51
+ let gamma = init(iv);
52
+ const decrypt = cipher.decrypt.bind(cipher);
53
+ const buffer = new Uint8Array(ciphertext);
54
+ const k = Math.floor(n / cipher.blockSize);
55
+ const r = n % cipher.blockSize;
56
+ if (r === 0) {
57
+ for (let off = 0; off < n; off += cipher.blockSize) {
58
+ gamma = increaseGamma(gamma);
59
+ buffer.set(transform(decrypt, buffer.subarray(off, off + cipher.blockSize), gamma), off);
60
+ }
61
+ }
62
+ else {
63
+ for (let i = 0; i < k - 1; i++) {
64
+ gamma = increaseGamma(gamma);
65
+ const off = i * cipher.blockSize;
66
+ buffer.set(transform(decrypt, buffer.subarray(off, off + cipher.blockSize), gamma), off);
67
+ }
68
+ gamma = increaseGamma(gamma);
69
+ const gamma_k = gamma;
70
+ const gamma_k_plus_1 = increaseGamma(gamma);
71
+ const last_off = (k - 1) * cipher.blockSize;
72
+ const tail_off = k * cipher.blockSize;
73
+ const combined = transform(decrypt, buffer.subarray(last_off, last_off + cipher.blockSize), gamma_k_plus_1);
74
+ const rec = concatBytes(buffer.subarray(tail_off, tail_off + r), combined.subarray(r));
75
+ const decrypted = transform(decrypt, rec, gamma_k);
76
+ buffer.set(decrypted, last_off);
77
+ buffer.set(combined.subarray(0, r), tail_off);
78
+ }
79
+ return buffer;
80
+ }
81
+ });
82
+ };
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@li0ard/dstu",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "main": "index.js",
5
5
  "types": "index.d.ts",
6
6
  "type": "module",
7
7
  "exports": {
8
8
  ".": "./index.js",
9
9
  "./dstu4145.js": "./dstu4145/index.js",
10
+ "./dstu9041.js": "./dstu9041/index.js",
10
11
  "./kalyna.js": "./kalyna/index.js",
11
12
  "./kupyna.js": "./kupyna/index.js",
12
13
  "./modes.js": "./modes/index.js",
@@ -41,6 +42,7 @@
41
42
  "typescript": "^5"
42
43
  },
43
44
  "dependencies": {
45
+ "@noble/curves": "^2.3.0",
44
46
  "@noble/hashes": "^2.3.0",
45
47
  "bn.js": "^5.2.3"
46
48
  },
@@ -12,10 +12,11 @@ export declare class Strumok {
12
12
  * @param key Encryption key (256 or 512 bit)
13
13
  */
14
14
  constructor(key: TArg<Uint8Array>, iv: TArg<Uint8Array>);
15
+ private updateR;
16
+ private init;
17
+ private round;
15
18
  /** 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
+ next_stream(): TRet<Uint8Array>;
19
20
  /** Perform encryption/decryption */
20
21
  crypt(msg: TArg<Uint8Array>): TRet<Uint8Array>;
21
22
  }
package/strumok/index.js CHANGED
@@ -1,19 +1,18 @@
1
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)] ^
2
+ import { bytesToUint64sBE, byte, xorBytes, uint64sToBytesBE } from "../utils.js";
3
+ const MAX_UINT64 = (1n << 64n) - 1n;
4
+ const not = (w) => MAX_UINT64 - (w & MAX_UINT64);
5
+ const a_mul = (w) => (w << 8n) ^ ALPHA_MUL[byte(w >> 56n)];
6
+ const ainv_mul = (w) => (w >> 8n) ^ ALPHA_MUL_INV[byte(w)];
7
+ const T = (w) => T_[0][byte(w)] ^
10
8
  T_[1][byte(w >> 8n)] ^
11
9
  T_[2][byte(w >> 16n)] ^
12
10
  T_[3][byte(w >> 24n)] ^
13
11
  T_[4][byte(w >> 32n)] ^
14
12
  T_[5][byte(w >> 40n)] ^
15
13
  T_[6][byte(w >> 48n)] ^
16
- T_[7][byte(w >> 56n)]);
14
+ T_[7][byte(w >> 56n)];
15
+ const S_SIZE = 16;
17
16
  /** Strumok stream cipher */
18
17
  export class Strumok {
19
18
  blockSize = 128;
@@ -32,7 +31,7 @@ export class Strumok {
32
31
  this.key = bytesToUint64sBE(key);
33
32
  this.iv = bytesToUint64sBE(iv);
34
33
  this.keySize = key.length;
35
- this.S = new BigUint64Array(16);
34
+ this.S = new BigUint64Array(S_SIZE);
36
35
  this.r = new BigUint64Array(2);
37
36
  if (this.keySize == 32) {
38
37
  this.S[0] = this.key[3] ^ this.iv[0];
@@ -72,280 +71,46 @@ export class Strumok {
72
71
  }
73
72
  else
74
73
  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
- }
74
+ for (let round = 0; round < 2; round++)
75
+ for (let i = 0; i < S_SIZE; i++)
76
+ this.init(i);
160
77
  }
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];
78
+ updateR(s) {
79
+ const r1 = this.r[1];
202
80
  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;
81
+ this.r[0] = r1 + this.S[s];
246
82
  }
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;
83
+ init(i) {
84
+ const prev = (i + 15) % S_SIZE;
85
+ const s13 = (i + 13) % S_SIZE;
86
+ const s11 = (i + 11) % S_SIZE;
87
+ const outfrom_fsm = (this.r[0] + this.S[prev]) ^ this.r[1];
88
+ this.S[i] = a_mul(this.S[i]) ^ this.S[s13] ^ ainv_mul(this.S[s11]) ^ outfrom_fsm;
89
+ this.updateR(s13);
90
+ }
91
+ round(i) {
92
+ const s13 = (i + 13) % S_SIZE;
93
+ const s11 = (i + 11) % S_SIZE;
94
+ this.S[i] = a_mul(this.S[i]) ^ this.S[s13] ^ ainv_mul(this.S[s11]);
95
+ this.updateR(s13);
96
+ const next = (i + 1) % S_SIZE;
97
+ return (this.r[0] + this.S[i]) ^ this.r[1] ^ this.S[next];
98
+ }
99
+ /** Generate next keystream */
100
+ next_stream() {
101
+ const out_stream = new BigUint64Array(S_SIZE);
102
+ for (let i = 0; i < S_SIZE; i++)
103
+ out_stream[i] = this.round(i);
104
+ return uint64sToBytesBE(out_stream);
332
105
  }
333
106
  /** Perform encryption/decryption */
334
107
  crypt(msg) {
335
108
  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];
109
+ let offset = 0;
110
+ while (offset < msg.length) {
111
+ const chunk = xorBytes(msg.subarray(offset), this.next_stream());
112
+ out.set(chunk, offset);
113
+ offset += chunk.length;
349
114
  }
350
115
  return out;
351
116
  }
package/utils.d.ts CHANGED
@@ -1,14 +1,8 @@
1
- import { type TArg, type TRet } from "@noble/hashes/utils.js";
1
+ import { type TArg, type TRet } from "@noble/curves/utils.js";
2
2
  export declare const xorBytes: (a: TArg<Uint8Array>, b: TArg<Uint8Array>) => TRet<Uint8Array>;
3
3
  export declare const bytesToUint64sBE: (b: TArg<Uint8Array>) => TRet<BigUint64Array>;
4
4
  export declare const uint64sToBytesBE: (w: TArg<BigUint64Array>) => TRet<Uint8Array>;
5
5
  export declare const bytesToUint64sLE: (b: TArg<Uint8Array>) => TRet<BigUint64Array>;
6
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
7
  export declare const byte: (a: bigint) => number;
13
8
  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;