@li0ard/dstu 0.1.5 → 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/README.md CHANGED
@@ -36,6 +36,8 @@ npm i @li0ard/dstu
36
36
  - Galois Counter mode (GCM)
37
37
  - Message Authentication Code mode (MAC and GMAC)
38
38
  - Output Feedback mode (OFB)
39
+ - Key wrapping (KW)
40
+ - XEX Tweakable Block Ciphertext Stealing (XTS)
39
41
 
40
42
  ## Supported curves
41
43
 
@@ -22,6 +22,8 @@ export interface DSTUShortParameters {
22
22
  export interface DSTUParameters extends DSTUShortParameters {
23
23
  /** Curve order */
24
24
  order: string;
25
+ /** Curve cofactor */
26
+ cofactor: 2 | 4;
25
27
  /** Base point `X` coordinate */
26
28
  Gx: string;
27
29
  /** Base point `Y` coordinate */
package/dstu4145/const.js CHANGED
@@ -3,6 +3,7 @@ export const DSTU_163 = {
3
3
  a: 1,
4
4
  b: "05FF6108462A2DC8210AB403925E638A19C1455D21",
5
5
  order: "0400000000000000000002BEC12BE2262D39BCF14D",
6
+ cofactor: 2,
6
7
  Gx: "02E2F85F5DD74CE983A5C4237229DAF8A3F35823BE",
7
8
  Gy: "03826F008A8C51D7B95284D9D03FF0E00CE2CD723A",
8
9
  m: 163,
@@ -13,6 +14,7 @@ export const DSTU_163_TEST = {
13
14
  a: 1,
14
15
  b: "05FF6108462A2DC8210AB403925E638A19C1455D21",
15
16
  order: "0400000000000000000002BEC12BE2262D39BCF14D",
17
+ cofactor: 2,
16
18
  Gx: "072D867F93A93AC27DF9FF01AFFE74885C8C540420",
17
19
  Gy: "00224A9C3947852B97C5599D5F4AB81122ADC3FD9B",
18
20
  m: 163,
@@ -23,6 +25,7 @@ export const DSTU_167 = {
23
25
  a: 1,
24
26
  b: "6EE3CEEB230811759F20518A0930F1A4315A827DAC",
25
27
  order: "3FFFFFFFFFFFFFFFFFFFFFB12EBCC7D7F29FF7701F",
28
+ cofactor: 2,
26
29
  Gx: "7A1F6653786A68192803910A3D30B2A2018B21CD54",
27
30
  Gy: "5F49EB26781C0EC6B8909156D98ED435E45FD59918",
28
31
  m: 167,
@@ -33,6 +36,7 @@ export const DSTU_173 = {
33
36
  a: 0,
34
37
  b: "108576C80499DB2FC16EDDF6853BBB278F6B6FB437D9",
35
38
  order: "0800000000000000000000189B4E67606E3825BB2831",
39
+ cofactor: 4,
36
40
  Gx: "04D41A619BCC6EADF0448FA22FAD567A9181D37389CA",
37
41
  Gy: "10B51CC12849B234C75E6DD2028BF7FF5C1CE0D991A1",
38
42
  m: 173,
@@ -48,6 +52,7 @@ export const DSTU_173_ONB = {
48
52
  a: 0,
49
53
  b: "043D7E139319F43BA00944915740E1E6651B06E278C7",
50
54
  order: "800000000000000000000189B4E67606E3825BB2831",
55
+ cofactor: 4,
51
56
  Gx: "0A3B8B8D704089B24318CAA51917CB6E424B4E6DEA56",
52
57
  Gy: "005A43D5EE1F400349CF35150C74F119A39145F0CB08",
53
58
  m: 173,
@@ -59,6 +64,7 @@ export const DSTU_173_ONB_TEST = {
59
64
  a: 0,
60
65
  b: "043D7E139319F43BA00944915740E1E6651B06E278C7",
61
66
  order: "800000000000000000000189B4E67606E3825BB2831",
67
+ cofactor: 4,
62
68
  Gx: "1D26E5282C2647E1B0327CB4F6087932A0A1F006A89F",
63
69
  Gy: "0322218E4551CF8F6E3345644A10B2408F5DF19EC2C7",
64
70
  m: 173,
@@ -70,6 +76,7 @@ export const DSTU_179 = {
70
76
  a: 1,
71
77
  b: "04A6E0856526436F2F88DD07A341E32D04184572BEB710",
72
78
  order: "03FFFFFFFFFFFFFFFFFFFFFFB981960435FE5AB64236EF",
79
+ cofactor: 2,
73
80
  Gx: "06BA06FE51464B2BD26DC57F48819BA9954667022C7D03",
74
81
  Gy: "025FBC363582DCEC065080CA8287AAFF09788A66DC3A9E",
75
82
  m: 179,
@@ -80,6 +87,7 @@ export const DSTU_179_ONB = {
80
87
  a: 1,
81
88
  b: "019C9EBC4FD8308193D3A61762C547C82F2E6B2182CBCB",
82
89
  order: "03FFFFFFFFFFFFFFFFFFFFFFB981960435FE5AB64236EF",
90
+ cofactor: 2,
83
91
  Gx: "011F4112265410477159E0D3E39BC4AC4ADE43B21B66B9",
84
92
  Gy: "02CA5F0B34C61A86216F13766591CB266E2819EE582B3C",
85
93
  m: 179,
@@ -95,6 +103,7 @@ export const DSTU_191 = {
95
103
  a: 1,
96
104
  b: "7BC86E2102902EC4D5890E8B6B4981FF27E0482750FEFC03",
97
105
  order: "40000000000000000000000069A779CAC1DABC6788F7474F",
106
+ cofactor: 2,
98
107
  Gx: "714114B762F2FF4A7912A6D2AC58B9B5C2FCFE76DAEB7129",
99
108
  Gy: "29C41E568B77C617EFE5902F11DB96FA9613CD8D03DB08DA",
100
109
  m: 191,
@@ -105,6 +114,7 @@ export const DSTU_191_ONB = {
105
114
  a: 1,
106
115
  b: "13871C9D29D6CEEA740FD57444F72FDBAE559C13A1E31EF8",
107
116
  order: "40000000000000000000000069A779CAC1DABC6788F7474F",
117
+ cofactor: 2,
108
118
  Gx: "7159E0D3E39BC4AC4ADE43B21B66B94F2AF25ABEEB1D85D3",
109
119
  Gy: "57E45D546BE6201E9705E029535D009D31F51D81E18226BE",
110
120
  m: 191,
@@ -120,6 +130,7 @@ export const DSTU_233 = {
120
130
  a: 1,
121
131
  b: "6973B15095675534C7CF7E64A21BD54EF5DD3B8A0326AA936ECE454D2C",
122
132
  order: "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
133
+ cofactor: 2,
123
134
  Gx: "3FCDA526B6CDF83BA1118DF35B3C31761D3545F32728D003EEB25EFE96",
124
135
  Gy: "9CA8B57A934C54DEEDA9E54A7BBAD95E3B2E91C54D32BE0B9DF96D8D35",
125
136
  m: 233,
@@ -130,6 +141,7 @@ export const DSTU_233_ONB = {
130
141
  a: 1,
131
142
  b: "80F920952A702C75B704A424C018EEA55AA44664F3A003E0962D4F9A8E",
132
143
  order: "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
144
+ cofactor: 2,
133
145
  Gx: "A0823B8ACF069F1CDE256256F21D90DB35CA795792D5F758EC2E9992FB",
134
146
  Gy: "019DAE222AF326D33AFB7761953F624A23D3EB1CA7A1C300F0AA9953CA85",
135
147
  m: 233,
@@ -145,6 +157,7 @@ export const DSTU_257 = {
145
157
  a: 0,
146
158
  b: "01CEF494720115657E18F938D7A7942394FF9425C1458C57861F9EEA6ADBE3BE10",
147
159
  order: "00800000000000000000000000000000006759213AF182E987D3E17714907D470D",
160
+ cofactor: 4,
148
161
  Gx: "002A29EF207D0E9B6C55CD260B306C7E007AC491CA1B10C62334A9E8DCD8D20FB7",
149
162
  Gy: "010686D41FF744D4449FCCF6D8EEA03102E6812C93A9D60B978B702CF156D814EF",
150
163
  m: 257,
@@ -155,6 +168,7 @@ export const DSTU_307 = {
155
168
  a: 1,
156
169
  b: "0393C7F7D53666B5054B5E6C6D3DE94F4296C0C599E2E2E241050DF18B6090BDC90186904968BB",
157
170
  order: "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC079C2F3825DA70D390FBBA588D4604022B7B7",
171
+ cofactor: 2,
158
172
  Gx: "0216EE8B189D291A0224984C1E92F1D16BF75CCD825A087A239B276D3167743C52C02D6E7232AA",
159
173
  Gy: "05D9306BACD22B7FAEB09D2E049C6E2866C5D1677762A8F2F2DC9A11C7F7BE8340AB2237C7F2A0",
160
174
  m: 307,
@@ -165,6 +179,7 @@ export const DSTU_367 = {
165
179
  a: 1,
166
180
  b: "43FC8AD242B0B7A6F3D1627AD5654447556B47BF6AA4A64B0C2AFE42CADAB8F93D92394C79A79755437B56995136",
167
181
  order: "40000000000000000000000000000000000000000000009C300B75A3FA824F22428FD28CE8812245EF44049B2D49",
182
+ cofactor: 2,
168
183
  Gx: "324A6EDDD512F08C49A99AE0D3F961197A76413E7BE81A400CA681E09639B5FE12E59A109F78BF4A373541B3B9A1",
169
184
  Gy: "01AB597A5B4477F59E39539007C7F977D1A567B92B043A49C6B61984C3FE3481AAF454CD41BA1F051626442B3C10",
170
185
  m: 367,
@@ -175,6 +190,7 @@ export const DSTU_431 = {
175
190
  a: 1,
176
191
  b: "03CE10490F6A708FC26DFE8C3D27C4F94E690134D5BFF988D8D28AAEAEDE975936C66BAC536B18AE2DC312CA493117DAA469C640CAF3",
177
192
  order: "3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBA3175458009A8C0A724F02F81AA8A1FCBAF80D90C7A95110504CF",
193
+ cofactor: 2,
178
194
  Gx: "1A62BA79D98133A16BBAE7ED9A8E03C32E0824D57AEF72F88986874E5AAE49C27BED49A2A95058068426C2171E99FD3B43C5947C857D",
179
195
  Gy: "70B5E1E14031C1F70BBEFE96BDDE66F451754B4CA5F48DA241F331AA396B8D1839A855C1769B1EA14BA53308B5E2723724E090E02DB9",
180
196
  m: 431,
@@ -185,6 +201,7 @@ export const DSTU_431_ONB = {
185
201
  a: 1,
186
202
  b: "53FB7AF7B4407000A6F226AD6BAD28378646BD83F1F940810A4C19536EE65E53F40F973F2F06C5E80EFE3B43651BD5FF8B06BA5F9299",
187
203
  order: "3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFBA3175458009A8C0A724F02F81AA8A1FCBAF80D90C7A95110504CF",
204
+ cofactor: 2,
188
205
  Gx: "2020182CFCD7C724157B363D4ABFD4C16251773C595D4E5C88572FA9D5EBB58142326448C08D3EE3E17E40B835D3090DCFBA7C5ADEA6",
189
206
  Gy: "18E1B6EFB28EEF8A1F9C1859ED57C74CA747E4E1A60294C005D1920E80903A432929855828DD7F21714F837E119A044987F00110ADAD",
190
207
  m: 431,
@@ -28,8 +28,9 @@ export declare const binaryWeierstrass: (parameters: DSTUParameters) => Readonly
28
28
  _double?: /*elided*/ any;
29
29
  x: BN;
30
30
  y: BN;
31
+ assertValidity(): /*elided*/ any;
31
32
  add(p: /*elided*/ any): /*elided*/ any;
32
- mul(f: BN): /*elided*/ any;
33
+ mul(f: BN | number): /*elided*/ any;
33
34
  negate(): /*elided*/ any;
34
35
  clone(): /*elided*/ any;
35
36
  isZero(): boolean;
@@ -51,8 +52,9 @@ export declare const binaryWeierstrass: (parameters: DSTUParameters) => Readonly
51
52
  _double?: /*elided*/ any;
52
53
  x: BN;
53
54
  y: BN;
55
+ assertValidity(): /*elided*/ any;
54
56
  add(p: /*elided*/ any): /*elided*/ any;
55
- mul(f: BN): /*elided*/ any;
57
+ mul(f: BN | number): /*elided*/ any;
56
58
  negate(): /*elided*/ any;
57
59
  clone(): /*elided*/ any;
58
60
  isZero(): boolean;
@@ -74,8 +76,9 @@ export declare const binaryWeierstrass: (parameters: DSTUParameters) => Readonly
74
76
  _double?: /*elided*/ any;
75
77
  x: BN;
76
78
  y: BN;
79
+ assertValidity(): /*elided*/ any;
77
80
  add(p: /*elided*/ any): /*elided*/ any;
78
- mul(f: BN): /*elided*/ any;
81
+ mul(f: BN | number): /*elided*/ any;
79
82
  negate(): /*elided*/ any;
80
83
  clone(): /*elided*/ any;
81
84
  isZero(): boolean;
@@ -98,8 +101,9 @@ export declare const binaryWeierstrass: (parameters: DSTUParameters) => Readonly
98
101
  _double?: /*elided*/ any;
99
102
  x: BN;
100
103
  y: BN;
104
+ assertValidity(): /*elided*/ any;
101
105
  add(p: /*elided*/ any): /*elided*/ any;
102
- mul(f: BN): /*elided*/ any;
106
+ mul(f: BN | number): /*elided*/ any;
103
107
  negate(): /*elided*/ any;
104
108
  clone(): /*elided*/ any;
105
109
  isZero(): boolean;
@@ -11,6 +11,15 @@ export const binaryWeierstrass = (parameters) => {
11
11
  const order = field.fromHexStringOrBytes(parameters.order);
12
12
  const b = toInternal(field.fromHexStringOrBytes(parameters.b));
13
13
  const c = field.sqrt(b);
14
+ const isValidXY = (x, y) => {
15
+ const x2 = field.sqr(x);
16
+ const lhs = field.sqr(y).ixor(field.mul(x, y));
17
+ const rhs = field.mul(x2, x).ixor(b);
18
+ // `a` must be 0 or 1, so if a = 1, just add x^2
19
+ if (parameters.a === 1)
20
+ rhs.ixor(x2);
21
+ return lhs.eq(rhs);
22
+ };
14
23
  const scalarByteLength = Math.ceil(order.bitLength() / 8), pointByteLength = field.LENGTH * 2;
15
24
  const lengths = Object.freeze({
16
25
  fieldByteLength: field.LENGTH,
@@ -45,6 +54,13 @@ export const binaryWeierstrass = (parameters) => {
45
54
  constructor(x, y) {
46
55
  this.x = x;
47
56
  this.y = y;
57
+ this.assertValidity();
58
+ }
59
+ assertValidity() {
60
+ // Skip if point is infinity
61
+ if (!isValidXY(this.x, this.y) && !this.x.isZero() && !this.y.isZero())
62
+ throw new Error("Assertation failed: point isn't on curve");
63
+ return this;
48
64
  }
49
65
  add(p) {
50
66
  const pz = Point.ZERO.clone();
@@ -79,6 +95,8 @@ export const binaryWeierstrass = (parameters) => {
79
95
  return pz;
80
96
  }
81
97
  mul(f) {
98
+ if (typeof f == "number")
99
+ f = new BN(f);
82
100
  if (f.isZero() || this.x.isZero())
83
101
  return Point.ZERO.clone();
84
102
  let X1 = this.x.clone(), Z1 = new BN(1);
@@ -101,12 +119,12 @@ export const binaryWeierstrass = (parameters) => {
101
119
  return this.negate();
102
120
  const A = field.mul(this.x, Z1).ixor(X1);
103
121
  const B = field.mul(this.x, Z2).ixor(X2);
104
- const Z1Z2 = field.mul(Z1, Z2);
105
- const Z1Z2x = field.mul(Z1Z2, this.x);
106
- const D = field.invert(field.mul(Z1Z2x, Z1));
107
- const N = field.mul(A, B).ixor(field.mul(Z1Z2, xSqr.ixor(this.y)));
108
- const Xk = field.mul(X1, field.mul(Z1Z2x, D));
109
- const Yk = field.mul(field.mul(A, N), D).ixor(this.y);
122
+ const C = field.mul(Z1, Z2);
123
+ const D = field.mul(C, this.x);
124
+ const E = field.invert(field.mul(D, Z1));
125
+ const F = field.mul(A, B).ixor(field.mul(C, xSqr.ixor(this.y)));
126
+ const Xk = field.mul(X1, field.mul(D, E));
127
+ const Yk = field.mul(field.mul(A, F), E).ixor(this.y);
110
128
  return new Point(Xk, Yk);
111
129
  }
112
130
  negate() { return new Point(this.x.clone(), this.x.xor(this.y)); }
@@ -3,6 +3,7 @@ import { type DSTUParameters } from "./const.js";
3
3
  /** Create DSTU 4145-2002 signer (Big-Endian) */
4
4
  export declare const dstu4145: (parameters: DSTUParameters) => Readonly<{
5
5
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
6
+ getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
6
7
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
7
8
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
8
9
  keygen: (isCompressed?: boolean) => {
@@ -21,6 +22,7 @@ export * from "./ec/expand.js";
21
22
  /** DSTU 4145-2002 163 bit curve */
22
23
  export declare const dstu163: Readonly<{
23
24
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
25
+ getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
24
26
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
25
27
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
26
28
  keygen: (isCompressed?: boolean) => {
@@ -37,6 +39,7 @@ export declare const dstu163: Readonly<{
37
39
  /** DSTU 4145-2002 167 bit curve */
38
40
  export declare const dstu167: Readonly<{
39
41
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
42
+ getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
40
43
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
41
44
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
42
45
  keygen: (isCompressed?: boolean) => {
@@ -53,6 +56,7 @@ export declare const dstu167: Readonly<{
53
56
  /** DSTU 4145-2002 173 bit curve */
54
57
  export declare const dstu173: Readonly<{
55
58
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
59
+ getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
56
60
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
57
61
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
58
62
  keygen: (isCompressed?: boolean) => {
@@ -69,6 +73,7 @@ export declare const dstu173: Readonly<{
69
73
  /** DSTU 4145-2002 179 bit curve */
70
74
  export declare const dstu179: Readonly<{
71
75
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
76
+ getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
72
77
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
73
78
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
74
79
  keygen: (isCompressed?: boolean) => {
@@ -85,6 +90,7 @@ export declare const dstu179: Readonly<{
85
90
  /** DSTU 4145-2002 191 bit curve */
86
91
  export declare const dstu191: Readonly<{
87
92
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
93
+ getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
88
94
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
89
95
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
90
96
  keygen: (isCompressed?: boolean) => {
@@ -101,6 +107,7 @@ export declare const dstu191: Readonly<{
101
107
  /** DSTU 4145-2002 233 bit curve */
102
108
  export declare const dstu233: Readonly<{
103
109
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
110
+ getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
104
111
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
105
112
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
106
113
  keygen: (isCompressed?: boolean) => {
@@ -117,6 +124,7 @@ export declare const dstu233: Readonly<{
117
124
  /** DSTU 4145-2002 257 bit curve */
118
125
  export declare const dstu257: Readonly<{
119
126
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
127
+ getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
120
128
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
121
129
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
122
130
  keygen: (isCompressed?: boolean) => {
@@ -133,6 +141,7 @@ export declare const dstu257: Readonly<{
133
141
  /** DSTU 4145-2002 307 bit curve */
134
142
  export declare const dstu307: Readonly<{
135
143
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
144
+ getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
136
145
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
137
146
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
138
147
  keygen: (isCompressed?: boolean) => {
@@ -149,6 +158,7 @@ export declare const dstu307: Readonly<{
149
158
  /** DSTU 4145-2002 367 bit curve */
150
159
  export declare const dstu367: Readonly<{
151
160
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
161
+ getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
152
162
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
153
163
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
154
164
  keygen: (isCompressed?: boolean) => {
@@ -165,6 +175,7 @@ export declare const dstu367: Readonly<{
165
175
  /** DSTU 4145-2002 431 bit curve */
166
176
  export declare const dstu431: Readonly<{
167
177
  getPublicKey: (secretKey: TArg<Uint8Array>, isCompressed?: boolean) => TRet<Uint8Array>;
178
+ getSharedKey: (secretKeyA: TArg<Uint8Array>, publicKeyB: TArg<Uint8Array>, withCofactor?: boolean) => TRet<Uint8Array>;
168
179
  sign: (secretKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, rand?: TArg<Uint8Array>) => TRet<Uint8Array>;
169
180
  verify: (publicKey: TArg<Uint8Array>, digest: TArg<Uint8Array>, signature: TArg<Uint8Array>) => boolean;
170
181
  keygen: (isCompressed?: boolean) => {
package/dstu4145/index.js CHANGED
@@ -6,7 +6,7 @@ import BN from "bn.js";
6
6
  export const dstu4145 = (parameters) => {
7
7
  const curve = binaryWeierstrass(parameters);
8
8
  const { Field, Point, MASK, lengths } = curve;
9
- const getPublicKey = (secretKey, isCompressed = false) => Point.BASE.mulWnaf(Field.fromHexStringOrBytes(secretKey)).negate().toBytes(isCompressed);
9
+ const getPublicKey = (secretKey, isCompressed = false) => Point.BASE.mul(Field.fromHexStringOrBytes(secretKey)).negate().toBytes(isCompressed);
10
10
  const randomPrivateKey = () => Field.toBytes(new BN(randomBytes(lengths.scalarByteLength)).imaskn(MASK), lengths.scalarByteLength);
11
11
  const computePresign = (rand) => {
12
12
  const e = (rand
@@ -16,7 +16,7 @@ export const dstu4145 = (parameters) => {
16
16
  throw new Error("Invalid custom rand for presign (rand = 0)");
17
17
  if (e.isZero())
18
18
  return computePresign(rand);
19
- const Fe = Point.BASE.mulWnaf(e).x;
19
+ const Fe = Point.BASE.mul(e).x;
20
20
  if (Fe.isZero())
21
21
  return computePresign(rand);
22
22
  return { Fe, e };
@@ -29,6 +29,8 @@ export const dstu4145 = (parameters) => {
29
29
  };
30
30
  const sign = (secretKey, digest, rand) => {
31
31
  const d = new BN(secretKey), h = prepareHash(digest), { Fe, e } = computePresign(rand);
32
+ if (d.isZero() || d.gte(curve.ORDER))
33
+ throw new Error("Invalid private key, must be in range 1 < key < order");
32
34
  const r = curve.toExternalField(Field.mul(h, Fe)).imaskn(MASK);
33
35
  if (r.isZero())
34
36
  return sign(secretKey, digest, rand);
@@ -49,6 +51,17 @@ export const dstu4145 = (parameters) => {
49
51
  const y = curve.toExternalField(Field.mul(h, R.x)).imaskn(MASK);
50
52
  return y.eq(r);
51
53
  };
54
+ const getSharedKey = (secretKeyA, publicKeyB, withCofactor = true) => {
55
+ const d = new BN(secretKeyA), Q = Point.fromBytes(publicKeyB);
56
+ if (d.isZero() || d.gte(curve.ORDER))
57
+ throw new Error("Invalid private key, must be in range 1 < key < order");
58
+ let R = Q.mul(d);
59
+ if (withCofactor)
60
+ R = R.mul(curve.parameters.cofactor);
61
+ if (R.isZero())
62
+ throw new Error(`Invalid parameters, shared secret is point at infinity`);
63
+ return Field.toBytes(curve.toExternalField(R.x), lengths.fieldByteLength);
64
+ };
52
65
  const keygen = (isCompressed = false) => {
53
66
  const secretKey = randomPrivateKey();
54
67
  const publicKey = getPublicKey(secretKey, isCompressed);
@@ -56,6 +69,7 @@ export const dstu4145 = (parameters) => {
56
69
  };
57
70
  return Object.freeze({
58
71
  getPublicKey,
72
+ getSharedKey,
59
73
  sign,
60
74
  verify,
61
75
  keygen,
package/dstu9041/curve.js CHANGED
@@ -2,27 +2,30 @@ import { Field, mod } from "@noble/curves/abstract/modular.js";
2
2
  import { bitGet, bitLen, bytesToNumberBE, randomBytes } from "@noble/curves/utils.js";
3
3
  import { concatBytes } from "@noble/hashes/utils.js";
4
4
  export const dstu9041Curve = (parameters) => {
5
+ if (parameters.a != 2n)
6
+ throw new Error("Unsuppored `a` parameter, must be 2");
5
7
  if (mod(parameters.p, 8n) != 5n)
6
8
  throw new Error("Unsupported `p` prime");
7
9
  const Fp = Field(parameters.p), Fn = Field(parameters.n);
10
+ const { a, d, Gx, Gy } = parameters;
8
11
  const isValidXY = (x, y) => {
9
12
  const x2 = Fp.sqr(x);
10
13
  const y2 = Fp.sqr(y);
11
- const left = Fp.add(x2, Fp.mul(parameters.a, y2));
12
- const right = Fp.add(Fp.ONE, Fp.mul(parameters.d, Fp.mul(x2, y2)));
14
+ const left = Fp.add(x2, Fp.mul(a, y2));
15
+ const right = Fp.add(Fp.ONE, Fp.mul(d, Fp.mul(x2, y2)));
13
16
  return Fp.eql(left, right);
14
17
  };
15
- if (!isValidXY(parameters.Gx, parameters.Gy))
18
+ if (!isValidXY(Gx, Gy))
16
19
  throw new Error("Invalid base point");
17
20
  const p_minus_1 = Fp.sub(Fp.ZERO, Fp.ONE);
18
21
  const p_minus_1_over_2 = Fp.div(Fp.sub(Fp.ORDER, Fp.ONE), 2n);
19
- const a_over_d = Fp.div(parameters.a, parameters.d);
22
+ const a_over_d = Fp.div(a, d);
20
23
  class Point {
21
24
  X;
22
25
  Y;
23
26
  Z;
24
- static BASE = Point.fromAffine(parameters.Gx, parameters.Gy);
25
- static ZERO = Point.fromAffine(1n, 0n);
27
+ static BASE = new Point(Gx, Gy, Fp.ONE);
28
+ static ZERO = new Point(Fp.ONE, Fp.ZERO, Fp.ONE);
26
29
  static Fp = Fp;
27
30
  static Fn = Fn;
28
31
  constructor(X, Y, Z) {
@@ -42,13 +45,13 @@ export const dstu9041Curve = (parameters) => {
42
45
  const B = Fp.sqr(A);
43
46
  const C = Fp.mul(this.X, other.X);
44
47
  const D = Fp.mul(this.Y, other.Y);
45
- const E = Fp.mul(parameters.d, Fp.mul(C, D));
48
+ const E = Fp.mul(d, Fp.mul(C, D));
46
49
  const F = Fp.sub(B, E);
47
50
  const G = Fp.add(B, E);
48
51
  const self_sum = Fp.add(this.X, this.Y);
49
52
  const other_sum = Fp.add(other.X, other.Y);
50
53
  const cross = Fp.mul(self_sum, other_sum);
51
- const xr = Fp.mul(Fp.mul(A, G), Fp.sub(C, Fp.mul(parameters.a, D)));
54
+ const xr = Fp.mul(Fp.mul(A, G), Fp.sub(C, Fp.mul(a, D)));
52
55
  const yr = Fp.mul(Fp.mul(A, F), Fp.sub(Fp.sub(cross, C), D));
53
56
  const zr = Fp.mul(F, G);
54
57
  return new Point(xr, yr, zr);
@@ -59,7 +62,7 @@ export const dstu9041Curve = (parameters) => {
59
62
  let S = new Point(this.X, this.Y, this.Z);
60
63
  for (let i = bitLen(scalar) - 2; i >= 0; i--) {
61
64
  S = S.add(S);
62
- if (bitGet(scalar, i) == 1n)
65
+ if (bitGet(scalar, i) == Fp.ONE)
63
66
  S = S.add(this);
64
67
  }
65
68
  return S;
@@ -91,7 +94,7 @@ export const dstu9041Curve = (parameters) => {
91
94
  Fp.eql(x2, a_over_d))
92
95
  throw new Error("Invalid x-coordinate, can't recover y");
93
96
  const num = Fp.sub(Fp.ONE, x2);
94
- const denom = Fp.sub(parameters.a, Fp.mul(parameters.d, x2));
97
+ const denom = Fp.sub(a, Fp.mul(d, x2));
95
98
  const v = Fp.div(num, denom);
96
99
  if (Fp.pow(v, p_minus_1_over_2) != Fp.ONE)
97
100
  throw new Error("Invalid x-coordinate, non-complience with euler criterion");
package/modes/cbc.js CHANGED
@@ -12,7 +12,7 @@ export const cbc = (cipher, iv) => {
12
12
  for (let i = 0; i < plaintext.length; i += cipher.blockSize) {
13
13
  const blk = cipher.encrypt(xorBytes(plaintext.subarray(i, i + cipher.blockSize), buf));
14
14
  output.set(blk, i);
15
- buf = blk.slice();
15
+ buf = blk;
16
16
  }
17
17
  return output;
18
18
  },
@@ -24,7 +24,7 @@ export const cbc = (cipher, iv) => {
24
24
  for (let i = 0; i < ciphertext.length; i += cipher.blockSize) {
25
25
  const blk = ciphertext.subarray(i, i + cipher.blockSize);
26
26
  output.set(xorBytes(cipher.decrypt(blk), buf), i);
27
- buf = blk.slice();
27
+ buf = blk;
28
28
  }
29
29
  return output;
30
30
  }
package/modes/index.d.ts CHANGED
@@ -6,3 +6,4 @@ export * from "./gcm.js";
6
6
  export * from "./kw.js";
7
7
  export * from "./mac.js";
8
8
  export * from "./ofb.js";
9
+ export * from "./xts.js";
package/modes/index.js CHANGED
@@ -6,3 +6,4 @@ export * from "./gcm.js";
6
6
  export * from "./kw.js";
7
7
  export * from "./mac.js";
8
8
  export * from "./ofb.js";
9
+ export * from "./xts.js";
package/modes/kw.d.ts CHANGED
@@ -1,6 +1,7 @@
1
- import type { TArg, TRet } from "@noble/hashes/utils.js";
1
+ import { type TArg, type TRet } from "@noble/hashes/utils.js";
2
2
  import type { Kalyna } from "../kalyna/index.js";
3
- export declare const kw: (cipher: Kalyna) => Readonly<{
3
+ /** Key wrapping (KW) */
4
+ export declare const kw: (cipher: Kalyna) => {
4
5
  wrap: (key: TArg<Uint8Array>) => TRet<Uint8Array>;
5
6
  unwrap: (wrappedKey: TArg<Uint8Array>) => TRet<Uint8Array>;
6
- }>;
7
+ };
package/modes/kw.js CHANGED
@@ -1,74 +1,79 @@
1
+ import { concatBytes } from "@noble/hashes/utils.js";
1
2
  import { pad, unpad } from "../padding.js";
3
+ /** Key wrapping (KW) */
2
4
  export const kw = (cipher) => {
3
- const block_size_kw_byte = cipher.blockSize >> 1;
5
+ const half = cipher.blockSize >> 1;
4
6
  return Object.freeze({
5
7
  wrap: (key) => {
6
- let plain_data_size_byte = key.length;
7
- const cipher_data = new Uint8Array(plain_data_size_byte + (cipher.blockSize << 2));
8
- cipher_data.set(key);
9
- let i = 0;
10
- if (plain_data_size_byte % cipher.blockSize !== 0) {
11
- let bitLength = plain_data_size_byte << 3;
8
+ let plaintext;
9
+ if (key.length % cipher.blockSize === 0)
10
+ plaintext = key;
11
+ else {
12
+ const withLength = new Uint8Array(key.length + half);
13
+ withLength.set(key);
14
+ let bitLength = key.length << 3;
15
+ let i = 0;
12
16
  while (bitLength > 0) {
13
- cipher_data[plain_data_size_byte + i] = bitLength & 0xFF;
17
+ withLength[key.length + i] = bitLength & 0xff;
14
18
  i++;
15
19
  bitLength >>>= 8;
16
20
  }
17
- plain_data_size_byte += block_size_kw_byte;
18
- const paddedData = pad(cipher_data.subarray(0, plain_data_size_byte), cipher.blockSize);
19
- cipher_data.set(paddedData, 0);
20
- plain_data_size_byte = paddedData.length;
21
+ plaintext = pad(withLength, cipher.blockSize);
21
22
  }
22
- const r = Math.floor(plain_data_size_byte / cipher.blockSize), n = 2 * (r + 1), v = (n - 1) * 6;
23
- plain_data_size_byte += cipher.blockSize;
24
- const b_el_count = (n - 1) * block_size_kw_byte, b_last_el = (n - 2) * block_size_kw_byte;
25
- const b = new Uint8Array(n * block_size_kw_byte), shift = new Uint8Array(n * block_size_kw_byte), B = new Uint8Array(block_size_kw_byte), swap = new Uint8Array(cipher.blockSize);
26
- B.set(cipher_data.subarray(0, block_size_kw_byte));
27
- b.set(cipher_data.subarray(block_size_kw_byte, block_size_kw_byte + b_el_count));
28
- for (i = 1; i <= v; i++) {
29
- swap.set(B, 0);
30
- swap.set(b.subarray(0, block_size_kw_byte), block_size_kw_byte);
31
- swap.set(cipher.encrypt(swap));
32
- swap[block_size_kw_byte] ^= i;
33
- B.set(swap.subarray(block_size_kw_byte, cipher.blockSize));
34
- shift.set(b.subarray(block_size_kw_byte, b_el_count));
35
- b.set(shift.subarray(0, b_el_count - block_size_kw_byte), 0);
36
- b.set(swap.subarray(0, block_size_kw_byte), b_last_el);
23
+ const r = plaintext.length / cipher.blockSize;
24
+ const n = 2 * (r + 1);
25
+ const v = (n - 1) * 6;
26
+ const qLen = (n - 1) * half;
27
+ const B = plaintext.slice(0, half);
28
+ const q = new Uint8Array(qLen);
29
+ q.set(plaintext.subarray(half));
30
+ const block = new Uint8Array(cipher.blockSize);
31
+ for (let i = 1; i <= v; i++) {
32
+ block.set(B);
33
+ block.set(q.subarray(0, half), half);
34
+ const encrypted = cipher.encrypt(block);
35
+ q.copyWithin(0, half);
36
+ q.set(encrypted.subarray(0, half), qLen - half);
37
+ B.set(encrypted.subarray(half));
38
+ B[0] ^= i & 0xff;
39
+ B[1] ^= (i >>> 8) & 0xff;
40
+ B[2] ^= (i >>> 16) & 0xff;
41
+ B[3] ^= (i >>> 24) & 0xff;
37
42
  }
38
- cipher_data.set(B, 0);
39
- cipher_data.set(b.subarray(0, b_el_count), block_size_kw_byte);
40
- return cipher_data.subarray(0, b_el_count + block_size_kw_byte);
43
+ return concatBytes(B, q);
41
44
  },
42
45
  unwrap: (wrappedKey) => {
43
- const cipher_data = new Uint8Array(wrappedKey);
44
- if (cipher_data.length < 2 * cipher.blockSize)
46
+ if (wrappedKey.length < 2 * cipher.blockSize)
45
47
  throw new Error("Invalid input length: must be at least 2 blocks");
46
- const r = Math.floor(cipher_data.length / cipher.blockSize) - 1, n = 2 * (r + 1), v = (n - 1) * 6;
47
- if (r < 0 || n <= 0 || v < 0)
48
- throw new Error("Invalid input length for decryption");
49
- const B = new Uint8Array(block_size_kw_byte);
50
- B.set(cipher_data.subarray(0, block_size_kw_byte));
51
- const b_el_count = (n - 1) * block_size_kw_byte;
52
- const b = new Uint8Array(cipher_data.length);
53
- b.set(cipher_data.subarray(block_size_kw_byte, block_size_kw_byte + b_el_count));
54
- const b_last_el = (n - 2) * block_size_kw_byte;
55
- const shift = new Uint8Array(cipher_data.length), swap = new Uint8Array(cipher.blockSize);
48
+ const r = wrappedKey.length / cipher.blockSize - 1;
49
+ const n = 2 * (r + 1);
50
+ const v = (n - 1) * 6;
51
+ const qLen = (n - 1) * half;
52
+ const B = wrappedKey.slice(0, half);
53
+ const q = wrappedKey.slice(half, half + qLen);
54
+ const block = new Uint8Array(cipher.blockSize);
56
55
  for (let i = v; i >= 1; i--) {
57
- swap.set(b.subarray(b_last_el, b_last_el + block_size_kw_byte), 0);
58
- B[0] ^= i;
59
- swap.set(B, block_size_kw_byte);
60
- swap.set(cipher.decrypt(swap));
61
- B.set(swap.subarray(0, block_size_kw_byte));
62
- shift.set(b.subarray(0, b_el_count - block_size_kw_byte));
63
- b.set(shift.subarray(0, b_el_count - block_size_kw_byte), block_size_kw_byte);
64
- b.set(swap.subarray(block_size_kw_byte, cipher.blockSize), 0);
56
+ block.set(q.subarray(qLen - half));
57
+ block.set(B, half);
58
+ block[half] ^= i & 0xff;
59
+ block[half + 1] ^= (i >>> 8) & 0xff;
60
+ block[half + 2] ^= (i >>> 16) & 0xff;
61
+ block[half + 3] ^= (i >>> 24) & 0xff;
62
+ const decrypted = cipher.decrypt(block);
63
+ q.copyWithin(half, 0, qLen - half);
64
+ q.set(decrypted.subarray(half));
65
+ B.set(decrypted.subarray(0, half));
65
66
  }
66
- cipher_data.set(B, 0);
67
- cipher_data.set(b.subarray(0, b_el_count), block_size_kw_byte);
68
- let current_length = unpad(cipher_data, cipher.blockSize).length;
67
+ let mismatch = 0;
68
+ for (let k = qLen - cipher.blockSize; k < qLen; k++)
69
+ mismatch |= q[k];
70
+ if (mismatch != 0)
71
+ throw new Error("KW checksum mismatch");
72
+ const recovered = concatBytes(B, q);
73
+ let current_length = unpad(recovered, cipher.blockSize).length;
69
74
  if (current_length % cipher.blockSize !== 0)
70
- current_length -= block_size_kw_byte + 1;
71
- return cipher_data.slice(0, current_length);
72
- }
75
+ current_length -= half + 1;
76
+ return recovered.slice(0, current_length);
77
+ },
73
78
  });
74
79
  };
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.5",
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",
@@ -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
  }