@li0ard/dstu 0.1.0 → 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.
- package/README.md +17 -2
- package/dstu4145/const.d.ts +50 -13
- package/dstu4145/const.js +165 -4
- package/dstu4145/ec/expand.d.ts +6 -0
- package/dstu4145/ec/expand.js +129 -0
- package/dstu4145/ec/index.d.ts +823 -3
- package/dstu4145/ec/index.js +301 -3
- package/dstu4145/ec/onb.d.ts +6 -0
- package/dstu4145/ec/onb.js +100 -0
- package/dstu4145/ec/wnaf.d.ts +4 -0
- package/dstu4145/ec/wnaf.js +63 -0
- package/dstu4145/index.d.ts +179 -27
- package/dstu4145/index.js +91 -68
- package/kalyna/index.d.ts +1 -0
- package/kalyna/index.js +15 -16
- package/kupyna/index.d.ts +1 -1
- package/kupyna/index.js +10 -18
- package/modes/cbc.js +27 -3
- package/modes/gcm.js +1 -1
- package/modes/ofb.js +11 -2
- package/package.json +4 -2
- package/strumok/index.d.ts +2 -5
- package/strumok/index.js +12 -20
- package/dstu4145/ec/curve.d.ts +0 -17
- package/dstu4145/ec/curve.js +0 -82
- package/dstu4145/ec/field.d.ts +0 -30
- package/dstu4145/ec/field.js +0 -135
- package/dstu4145/ec/point.d.ts +0 -12
- package/dstu4145/ec/point.js +0 -79
package/dstu4145/index.js
CHANGED
|
@@ -1,70 +1,93 @@
|
|
|
1
|
-
import { randomBytes } from "@noble/hashes/utils.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
1
|
+
import { concatBytes, randomBytes } from "@noble/hashes/utils.js";
|
|
2
|
+
import { DSTU_163, DSTU_163_TEST, DSTU_167, DSTU_173, DSTU_173_ONB, DSTU_173_ONB_TEST, DSTU_179, DSTU_179_ONB, DSTU_191, DSTU_191_ONB, DSTU_233, DSTU_233_ONB, DSTU_257, DSTU_307, DSTU_367, DSTU_431, DSTU_431_ONB } from "./const.js";
|
|
3
|
+
import { binaryWeierstrass } from "./ec/index.js";
|
|
4
|
+
import BN from "bn.js";
|
|
5
|
+
/** Create DSTU 4145-2002 signer (Big-Endian) */
|
|
6
|
+
export const dstu4145 = (parameters) => {
|
|
7
|
+
const curve = binaryWeierstrass(parameters);
|
|
8
|
+
const k = curve.ORDER.getLength() - 1;
|
|
9
|
+
const getPublicKey = (secretKey, isCompressed = false) => curve.Point.BASE.mul(curve.Field.fromBytes(secretKey)).negate().toBytes(isCompressed);
|
|
10
|
+
const randomPrivateKey = () => new Uint8Array(new BN(randomBytes(curve.lengths.scalarByteLength))
|
|
11
|
+
.maskn(k)
|
|
12
|
+
.toArray("be", curve.lengths.scalarByteLength));
|
|
13
|
+
const computePresign = (rand) => {
|
|
14
|
+
const e = (rand
|
|
15
|
+
? new BN(rand)
|
|
16
|
+
: new BN(randomBytes(curve.lengths.scalarByteLength))).maskn(k);
|
|
17
|
+
if (rand && e.isZero())
|
|
18
|
+
throw new Error("Invalid custom rand for presign (rand = 0)");
|
|
19
|
+
if (e.isZero())
|
|
20
|
+
return computePresign(rand);
|
|
21
|
+
const xR = curve.Point.BASE.mul(new curve.Field(e)).x;
|
|
22
|
+
if (xR.is0())
|
|
23
|
+
return computePresign(rand);
|
|
24
|
+
return { Fe: xR, e };
|
|
25
|
+
};
|
|
26
|
+
const prepareHash = (digest) => {
|
|
27
|
+
let h = curve.toInternalField(curve.hashToField(digest));
|
|
28
|
+
if (h.is0())
|
|
29
|
+
h = curve.Field.get1();
|
|
30
|
+
return h;
|
|
31
|
+
};
|
|
32
|
+
const sign = (secretKey, digest, rand) => {
|
|
33
|
+
const d = new BN(secretKey).mod(curve.ORDER.value);
|
|
34
|
+
const h = prepareHash(digest);
|
|
35
|
+
const { Fe, e } = computePresign(rand);
|
|
36
|
+
const r = curve.toExternalField(h.mul(Fe)).value.maskn(k);
|
|
37
|
+
if (r.isZero())
|
|
38
|
+
return sign(secretKey, digest, rand);
|
|
39
|
+
const s = e.add(d.mul(r)).mod(curve.ORDER.value);
|
|
40
|
+
if (s.isZero())
|
|
41
|
+
return sign(secretKey, digest, rand);
|
|
42
|
+
return concatBytes(new Uint8Array(s.toArray("be", curve.lengths.scalarByteLength)), new Uint8Array(r.toArray("be", curve.lengths.scalarByteLength)));
|
|
43
|
+
};
|
|
44
|
+
const verify = (publicKey, digest, signature) => {
|
|
45
|
+
if (signature.length != curve.lengths.signatureByteLength)
|
|
46
|
+
throw new Error("Invalid signature length");
|
|
47
|
+
const Q = curve.Point.fromBytes(publicKey);
|
|
48
|
+
const _s = curve.Field.fromBytes(signature.subarray(0, curve.lengths.scalarByteLength)), _r = curve.Field.fromBytes(signature.subarray(curve.lengths.scalarByteLength));
|
|
49
|
+
if (_s.value.isZero() || _s.value.gte(curve.ORDER.value)
|
|
50
|
+
|| _r.value.isZero() || _r.value.gte(curve.ORDER.value))
|
|
51
|
+
return false;
|
|
52
|
+
const h = prepareHash(digest);
|
|
53
|
+
const R = curve.Point.BASE.mulWnaf(_s).add(Q.mulWnaf(_r));
|
|
54
|
+
const y = curve.toExternalField(h.mul(R.x)).value.maskn(k);
|
|
55
|
+
return y.eq(_r.value);
|
|
56
|
+
};
|
|
57
|
+
const keygen = (isCompressed = false) => {
|
|
58
|
+
const secretKey = randomPrivateKey();
|
|
59
|
+
const publicKey = getPublicKey(secretKey, isCompressed);
|
|
60
|
+
return { secretKey, publicKey };
|
|
61
|
+
};
|
|
62
|
+
return Object.freeze({
|
|
63
|
+
getPublicKey,
|
|
64
|
+
sign,
|
|
65
|
+
verify,
|
|
66
|
+
keygen,
|
|
67
|
+
lengths: curve.lengths
|
|
68
|
+
});
|
|
69
69
|
};
|
|
70
70
|
export * from "./const.js";
|
|
71
|
+
export * from "./ec/expand.js";
|
|
72
|
+
// ONB curves don't preinitialized because `multiplyOnb` is slow O(m^3) function
|
|
73
|
+
// You need to initialize it manually via `dstu4145`
|
|
74
|
+
/** DSTU 4145-2002 163 bit curve */
|
|
75
|
+
export const dstu163 = dstu4145(DSTU_163);
|
|
76
|
+
/** DSTU 4145-2002 167 bit curve */
|
|
77
|
+
export const dstu167 = dstu4145(DSTU_167);
|
|
78
|
+
/** DSTU 4145-2002 173 bit curve */
|
|
79
|
+
export const dstu173 = dstu4145(DSTU_173);
|
|
80
|
+
/** DSTU 4145-2002 179 bit curve */
|
|
81
|
+
export const dstu179 = dstu4145(DSTU_179);
|
|
82
|
+
/** DSTU 4145-2002 191 bit curve */
|
|
83
|
+
export const dstu191 = dstu4145(DSTU_191);
|
|
84
|
+
/** DSTU 4145-2002 233 bit curve */
|
|
85
|
+
export const dstu233 = dstu4145(DSTU_233);
|
|
86
|
+
/** DSTU 4145-2002 257 bit curve */
|
|
87
|
+
export const dstu257 = dstu4145(DSTU_257);
|
|
88
|
+
/** DSTU 4145-2002 307 bit curve */
|
|
89
|
+
export const dstu307 = dstu4145(DSTU_307);
|
|
90
|
+
/** DSTU 4145-2002 367 bit curve */
|
|
91
|
+
export const dstu367 = dstu4145(DSTU_367);
|
|
92
|
+
/** DSTU 4145-2002 431 bit curve */
|
|
93
|
+
export const dstu431 = dstu4145(DSTU_431);
|
package/kalyna/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { TArg, TRet } from "@noble/hashes/utils.js";
|
|
2
2
|
import type { Cipher } from "../types.js";
|
|
3
|
+
export declare const column: (x: TArg<BigUint64Array>, i: number, N: number, c1: number, c2: number, c3: number, c4: number, c5: number, c6: number, c7: number) => bigint;
|
|
3
4
|
export declare abstract class Kalyna implements Cipher {
|
|
4
5
|
readonly N: number;
|
|
5
6
|
/** Block size */
|
package/kalyna/index.js
CHANGED
|
@@ -8,6 +8,14 @@ const swap_block = (k, N) => {
|
|
|
8
8
|
k[i] = k[i + 1];
|
|
9
9
|
k[N - 1] = t;
|
|
10
10
|
};
|
|
11
|
+
export const column = (x, i, N, c1, c2, c3, c4, c5, c6, c7) => (T[0][byte(x[(i + N) % N])] ^ // c0 - always 0 for both Kalyna and Kupyna
|
|
12
|
+
T[1][byte(x[(i - c1 + N) % N] >> 8n)] ^
|
|
13
|
+
T[2][byte(x[(i - c2 + N) % N] >> 16n)] ^
|
|
14
|
+
T[3][byte(x[(i - c3 + N) % N] >> 24n)] ^
|
|
15
|
+
T[4][byte(x[(i - c4 + N) % N] >> 32n)] ^
|
|
16
|
+
T[5][byte(x[(i - c5 + N) % N] >> 40n)] ^
|
|
17
|
+
T[6][byte(x[(i - c6 + N) % N] >> 48n)] ^
|
|
18
|
+
T[7][byte(x[(i - c7 + N) % N] >> 56n)]);
|
|
11
19
|
export class Kalyna {
|
|
12
20
|
N;
|
|
13
21
|
/** Block size */
|
|
@@ -100,14 +108,7 @@ export class Kalyna {
|
|
|
100
108
|
y[i] = x[i] - k[i];
|
|
101
109
|
}
|
|
102
110
|
column(x, i) {
|
|
103
|
-
return
|
|
104
|
-
T[1][byte(x[(i - this.wordOffsets[1] + this.N) % this.N] >> 8n)] ^
|
|
105
|
-
T[2][byte(x[(i - this.wordOffsets[2] + this.N) % this.N] >> 16n)] ^
|
|
106
|
-
T[3][byte(x[(i - this.wordOffsets[3] + this.N) % this.N] >> 24n)] ^
|
|
107
|
-
T[4][byte(x[(i - this.wordOffsets[4] + this.N) % this.N] >> 32n)] ^
|
|
108
|
-
T[5][byte(x[(i - this.wordOffsets[5] + this.N) % this.N] >> 40n)] ^
|
|
109
|
-
T[6][byte(x[(i - this.wordOffsets[6] + this.N) % this.N] >> 48n)] ^
|
|
110
|
-
T[7][byte(x[(i - this.wordOffsets[7] + this.N) % this.N] >> 56n)];
|
|
111
|
+
return column(x, i, this.N, this.wordOffsets[1], this.wordOffsets[2], this.wordOffsets[3], this.wordOffsets[4], this.wordOffsets[5], this.wordOffsets[6], this.wordOffsets[7]);
|
|
111
112
|
}
|
|
112
113
|
G(x, y, k) {
|
|
113
114
|
for (let i = 0; i < this.N; i++)
|
|
@@ -158,16 +159,15 @@ export class Kalyna {
|
|
|
158
159
|
if (block.length != this.blockSize)
|
|
159
160
|
throw new Error(`Incorrect length (need - ${this.blockSize}, got - ${block.length})`);
|
|
160
161
|
const t1 = new BigUint64Array(this.N), t2 = new BigUint64Array(this.N);
|
|
161
|
-
|
|
162
|
-
this.addkey(bytesToUint64sLE(block), t1, rk);
|
|
162
|
+
this.addkey(bytesToUint64sLE(block), t1, this.erk);
|
|
163
163
|
for (let i = 0; i < this.numRounds; i++) {
|
|
164
|
-
const roundKey =
|
|
164
|
+
const roundKey = this.erk.subarray(this.N + i * this.N);
|
|
165
165
|
if (i % 2 === 0)
|
|
166
166
|
this.G(t1, t2, roundKey);
|
|
167
167
|
else
|
|
168
168
|
this.G(t2, t1, roundKey);
|
|
169
169
|
}
|
|
170
|
-
this.GL(t2, t1,
|
|
170
|
+
this.GL(t2, t1, this.erk.subarray(this.glOffset));
|
|
171
171
|
return uint64sToBytesLE(t1);
|
|
172
172
|
}
|
|
173
173
|
/** Decrypt block */
|
|
@@ -175,17 +175,16 @@ export class Kalyna {
|
|
|
175
175
|
if (block.length != this.blockSize)
|
|
176
176
|
throw new Error(`Incorrect length (need - ${this.blockSize}, got - ${block.length})`);
|
|
177
177
|
const t1 = new BigUint64Array(this.N), t2 = new BigUint64Array(this.N);
|
|
178
|
-
|
|
179
|
-
this.subkey(bytesToUint64sLE(block), t1, rk.subarray(this.glOffset));
|
|
178
|
+
this.subkey(bytesToUint64sLE(block), t1, this.drk.subarray(this.glOffset));
|
|
180
179
|
this.IMC(t1);
|
|
181
180
|
for (let i = 0; i < this.numRounds; i++) {
|
|
182
|
-
const roundKey =
|
|
181
|
+
const roundKey = this.drk.subarray(this.glOffset - this.N - i * this.N);
|
|
183
182
|
if (i % 2 === 0)
|
|
184
183
|
this.IG(t1, t2, roundKey);
|
|
185
184
|
else
|
|
186
185
|
this.IG(t2, t1, roundKey);
|
|
187
186
|
}
|
|
188
|
-
this.IGL(t2, t1,
|
|
187
|
+
this.IGL(t2, t1, this.drk);
|
|
189
188
|
return uint64sToBytesLE(t1);
|
|
190
189
|
}
|
|
191
190
|
}
|
package/kupyna/index.d.ts
CHANGED
package/kupyna/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { concatBytes, createHasher } from "@noble/hashes/utils.js";
|
|
2
|
-
import { bytesToUint64sLE, numberToBytesLE, uint64sToBytesLE
|
|
3
|
-
import {
|
|
2
|
+
import { bytesToUint64sLE, numberToBytesLE, uint64sToBytesLE } from "../utils.js";
|
|
3
|
+
import { column } from "../kalyna/index.js";
|
|
4
4
|
const r = 0x00f0f0f0f0f0f0f3n;
|
|
5
5
|
class Kupyna {
|
|
6
6
|
blockLen;
|
|
@@ -35,13 +35,13 @@ class Kupyna {
|
|
|
35
35
|
this.x.set(data.subarray(0, n), this.nx);
|
|
36
36
|
this.nx += n;
|
|
37
37
|
if (this.nx === this.blockLen) {
|
|
38
|
-
this.
|
|
38
|
+
this.F(bytesToUint64sLE(this.x));
|
|
39
39
|
this.nx = 0;
|
|
40
40
|
}
|
|
41
41
|
data = data.slice(n);
|
|
42
42
|
}
|
|
43
43
|
while (data.length >= this.blockLen) {
|
|
44
|
-
this.
|
|
44
|
+
this.F(bytesToUint64sLE(data.subarray(0, this.blockLen)));
|
|
45
45
|
this.nx = 0;
|
|
46
46
|
data = data.slice(this.blockLen);
|
|
47
47
|
}
|
|
@@ -66,34 +66,26 @@ class Kupyna {
|
|
|
66
66
|
};
|
|
67
67
|
if (this.nx > this.threshold) {
|
|
68
68
|
fillBytes(this.nx);
|
|
69
|
-
this.
|
|
69
|
+
this.F(bytesToUint64sLE(this.x));
|
|
70
70
|
this.nx = 0;
|
|
71
71
|
}
|
|
72
72
|
fillBytes(this.nx);
|
|
73
73
|
this.x.set(numberToBytesLE(this.len * 8n, 12), this.threshold);
|
|
74
|
-
this.
|
|
74
|
+
this.F(bytesToUint64sLE(this.x));
|
|
75
75
|
this.outputTransform();
|
|
76
76
|
buffer.set(uint64sToBytesLE(this.s).subarray(this.outputLen));
|
|
77
77
|
this.destroy();
|
|
78
78
|
}
|
|
79
79
|
column(x, i) {
|
|
80
|
-
|
|
81
|
-
return T[0][byte(x[(i + this.stSize) % this.stSize])] ^
|
|
82
|
-
T[1][byte(x[(i - 1 + this.stSize) % this.stSize] >> 8n)] ^
|
|
83
|
-
T[2][byte(x[(i - 2 + this.stSize) % this.stSize] >> 16n)] ^
|
|
84
|
-
T[3][byte(x[(i - 3 + this.stSize) % this.stSize] >> 24n)] ^
|
|
85
|
-
T[4][byte(x[(i - 4 + this.stSize) % this.stSize] >> 32n)] ^
|
|
86
|
-
T[5][byte(x[(i - 5 + this.stSize) % this.stSize] >> 40n)] ^
|
|
87
|
-
T[6][byte(x[(i - 6 + this.stSize) % this.stSize] >> 48n)] ^
|
|
88
|
-
T[7][byte(x[(i - lastOffset + this.stSize) % this.stSize] >> 56n)];
|
|
80
|
+
return column(x, i, this.stSize, 1, 2, 3, 4, 5, 6, this.blockLen == 64 ? 7 : 11);
|
|
89
81
|
}
|
|
90
82
|
G(x, y) {
|
|
91
83
|
for (let i = 0; i < this.stSize; i++)
|
|
92
84
|
y[i] = this.column(x, i);
|
|
93
85
|
}
|
|
94
86
|
P(x, y, round) {
|
|
95
|
-
for (let
|
|
96
|
-
x[Number(
|
|
87
|
+
for (let i = 0n; i < BigInt(this.stSize); i++)
|
|
88
|
+
x[Number(i)] ^= (i << 4n) ^ round;
|
|
97
89
|
const r1 = round + 1n;
|
|
98
90
|
for (let i = 0; i < this.stSize; i++)
|
|
99
91
|
y[i] = this.column(x, i) ^ BigInt(i << 4) ^ r1;
|
|
@@ -114,7 +106,7 @@ class Kupyna {
|
|
|
114
106
|
for (let column = 0; column < this.stSize; column++)
|
|
115
107
|
this.s[column] ^= t1[column];
|
|
116
108
|
}
|
|
117
|
-
|
|
109
|
+
F(b) {
|
|
118
110
|
const AQ1 = new BigUint64Array(this.stSize);
|
|
119
111
|
const AP1 = new BigUint64Array(this.stSize);
|
|
120
112
|
const tmp = new BigUint64Array(this.stSize);
|
package/modes/cbc.js
CHANGED
|
@@ -1,10 +1,34 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { xorBytes } from "../utils.js";
|
|
2
2
|
/** Cipher Block Chaining (CBC) mode */
|
|
3
3
|
export const cbc = (cipher, iv) => {
|
|
4
|
+
if (iv.length !== cipher.blockSize)
|
|
5
|
+
throw new Error("Invalid IV size");
|
|
4
6
|
const encrypter = cipher.encrypt.bind(cipher);
|
|
5
7
|
const decrypter = cipher.decrypt.bind(cipher);
|
|
6
8
|
return {
|
|
7
|
-
encrypt: (plaintext) =>
|
|
8
|
-
|
|
9
|
+
encrypt: (plaintext) => {
|
|
10
|
+
if (plaintext.length % cipher.blockSize !== 0)
|
|
11
|
+
throw new Error("Plaintext not aligned");
|
|
12
|
+
let buf = new Uint8Array(iv);
|
|
13
|
+
const output = new Uint8Array(plaintext.length);
|
|
14
|
+
for (let i = 0; i < plaintext.length; i += cipher.blockSize) {
|
|
15
|
+
const blk = encrypter(xorBytes(plaintext.subarray(i, i + cipher.blockSize), buf));
|
|
16
|
+
output.set(blk, i);
|
|
17
|
+
buf = blk.slice();
|
|
18
|
+
}
|
|
19
|
+
return output;
|
|
20
|
+
},
|
|
21
|
+
decrypt: (ciphertext) => {
|
|
22
|
+
if (ciphertext.length % cipher.blockSize !== 0)
|
|
23
|
+
throw new Error("Ciphertext not aligned");
|
|
24
|
+
let buf = new Uint8Array(iv);
|
|
25
|
+
const output = new Uint8Array(ciphertext.length);
|
|
26
|
+
for (let i = 0; i < ciphertext.length; i += cipher.blockSize) {
|
|
27
|
+
const blk = ciphertext.subarray(i, i + cipher.blockSize);
|
|
28
|
+
output.set(xorBytes(decrypter(blk), buf), i);
|
|
29
|
+
buf = blk.slice();
|
|
30
|
+
}
|
|
31
|
+
return output;
|
|
32
|
+
}
|
|
9
33
|
};
|
|
10
34
|
};
|
package/modes/gcm.js
CHANGED
|
@@ -10,7 +10,7 @@ export const gcm = (cipher, iv, q = 16) => {
|
|
|
10
10
|
return concatBytes(enc, gmac(cipher, q).compute(aad || new Uint8Array(), enc));
|
|
11
11
|
},
|
|
12
12
|
open: (ciphertext, aad) => {
|
|
13
|
-
const enc = ciphertext.
|
|
13
|
+
const enc = ciphertext.subarray(0, -q);
|
|
14
14
|
const hC = gmac(cipher, q).compute(aad || new Uint8Array(), enc);
|
|
15
15
|
if (!equalBytes(ciphertext.subarray(-q), hC))
|
|
16
16
|
throw new Error("Invalid MAC");
|
package/modes/ofb.js
CHANGED
|
@@ -1,11 +1,20 @@
|
|
|
1
1
|
import {} from "@noble/hashes/utils.js";
|
|
2
|
-
import {
|
|
2
|
+
import { xorBytes } from "../utils.js";
|
|
3
3
|
/** Output Feedback (OFB) mode */
|
|
4
4
|
export const ofb = (cipher, iv) => {
|
|
5
5
|
if (iv.length !== cipher.blockSize)
|
|
6
6
|
throw new Error("Invalid IV size");
|
|
7
7
|
const encrypter = cipher.encrypt.bind(cipher);
|
|
8
8
|
return {
|
|
9
|
-
crypt: (msg) =>
|
|
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
|
+
}
|
|
10
19
|
};
|
|
11
20
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@li0ard/dstu",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"types": "index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -30,6 +30,9 @@
|
|
|
30
30
|
"docs": "typedoc --options .config/typedoc/config.cjs"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
+
"@li0ard/gost": "^0.2.0",
|
|
34
|
+
"@peculiar/asn1-schema": "^2.8.0",
|
|
35
|
+
"@peculiar/asn1-x509": "^2.8.0",
|
|
33
36
|
"@types/bn.js": "^5.2.0",
|
|
34
37
|
"@types/bun": "latest",
|
|
35
38
|
"typedoc": "^0.28.19"
|
|
@@ -38,7 +41,6 @@
|
|
|
38
41
|
"typescript": "^5"
|
|
39
42
|
},
|
|
40
43
|
"dependencies": {
|
|
41
|
-
"@li0ard/sp80038": "^0.1.2",
|
|
42
44
|
"@noble/hashes": "^2.2.0",
|
|
43
45
|
"bn.js": "^5.2.3"
|
|
44
46
|
},
|
package/strumok/index.d.ts
CHANGED
|
@@ -16,9 +16,6 @@ export declare class Strumok {
|
|
|
16
16
|
next_stream(): TRet<BigUint64Array>;
|
|
17
17
|
/** Generate next keystream and perform encryption */
|
|
18
18
|
next_stream_full_crypt(in_: TArg<BigUint64Array>): TRet<BigUint64Array>;
|
|
19
|
-
/**
|
|
20
|
-
|
|
21
|
-
* @param in_ Input data
|
|
22
|
-
*/
|
|
23
|
-
crypt(in_: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
19
|
+
/** Perform encryption/decryption */
|
|
20
|
+
crypt(msg: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
24
21
|
}
|
package/strumok/index.js
CHANGED
|
@@ -330,30 +330,22 @@ export class Strumok {
|
|
|
330
330
|
out[15] = in_[15] ^ (this.r[0] + this.S[15]) ^ this.r[1] ^ this.S[0];
|
|
331
331
|
return out;
|
|
332
332
|
}
|
|
333
|
-
/**
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
const blockBuffer = new ArrayBuffer(this.blockSize);
|
|
343
|
-
const block64 = new BigUint64Array(blockBuffer);
|
|
344
|
-
const block8 = new Uint8Array(blockBuffer);
|
|
345
|
-
while (inl - inOffset >= this.blockSize) {
|
|
346
|
-
block8.set(in_.slice(inOffset, inOffset + this.blockSize));
|
|
347
|
-
const encrypted8 = new Uint8Array(this.next_stream_full_crypt(block64).buffer, 0, this.blockSize);
|
|
348
|
-
out.set(encrypted8, outOffset);
|
|
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);
|
|
349
342
|
inOffset += this.blockSize;
|
|
350
343
|
outOffset += this.blockSize;
|
|
351
344
|
}
|
|
352
|
-
if (inOffset <
|
|
353
|
-
const remaining = inl - inOffset;
|
|
345
|
+
if (inOffset < msg.length) {
|
|
354
346
|
const keystream = new Uint8Array(this.next_stream().buffer, 0, this.blockSize);
|
|
355
|
-
for (let i = 0; i <
|
|
356
|
-
out[outOffset + i] =
|
|
347
|
+
for (let i = 0; i < msg.length - inOffset; i++)
|
|
348
|
+
out[outOffset + i] = msg[inOffset + i] ^ keystream[i];
|
|
357
349
|
}
|
|
358
350
|
return out;
|
|
359
351
|
}
|
package/dstu4145/ec/curve.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { Field } from "./field.js";
|
|
2
|
-
import { Point } from "../ec/point.js";
|
|
3
|
-
import type { DSTUParameters } from "../const.js";
|
|
4
|
-
export declare class Curve {
|
|
5
|
-
a: number;
|
|
6
|
-
b: Field;
|
|
7
|
-
order: Field;
|
|
8
|
-
base: Point;
|
|
9
|
-
m: number;
|
|
10
|
-
ks: number[];
|
|
11
|
-
constructor(params: DSTUParameters);
|
|
12
|
-
get modulo(): Field;
|
|
13
|
-
random(): Field;
|
|
14
|
-
truncate(value: Field): Field;
|
|
15
|
-
private fsquad;
|
|
16
|
-
expand(x: Field): Point;
|
|
17
|
-
}
|
package/dstu4145/ec/curve.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { Field } from "./field.js";
|
|
2
|
-
import { Point } from "../ec/point.js";
|
|
3
|
-
import { randomBytes } from "@noble/hashes/utils.js";
|
|
4
|
-
export class Curve {
|
|
5
|
-
a;
|
|
6
|
-
b;
|
|
7
|
-
order;
|
|
8
|
-
base;
|
|
9
|
-
m;
|
|
10
|
-
ks;
|
|
11
|
-
constructor(params) {
|
|
12
|
-
this.a = params.a;
|
|
13
|
-
this.b = Field.fromString(params.b, 16);
|
|
14
|
-
this.order = Field.fromString(params.order, 16);
|
|
15
|
-
this.base = new Point(Field.fromString(params.Gx, 16, this), Field.fromString(params.Gy, 16, this));
|
|
16
|
-
this.m = params.m;
|
|
17
|
-
this.ks = params.ks;
|
|
18
|
-
}
|
|
19
|
-
get modulo() {
|
|
20
|
-
const m = Field.get0();
|
|
21
|
-
m.setBit(this.m, 1);
|
|
22
|
-
m.setBit(0, 1);
|
|
23
|
-
for (const i of this.ks)
|
|
24
|
-
m.setBit(i, 1);
|
|
25
|
-
return m;
|
|
26
|
-
}
|
|
27
|
-
random() {
|
|
28
|
-
return this.truncate(Field.fromU8(randomBytes(Math.ceil(this.m / 8))));
|
|
29
|
-
}
|
|
30
|
-
truncate(value) {
|
|
31
|
-
const bitl_o = this.order.getLength();
|
|
32
|
-
let xbit = value.getLength();
|
|
33
|
-
const ret = value.clone();
|
|
34
|
-
while (bitl_o <= xbit) {
|
|
35
|
-
ret.setBit(xbit - 1, 0);
|
|
36
|
-
xbit = ret.getLength();
|
|
37
|
-
}
|
|
38
|
-
return ret.clone();
|
|
39
|
-
}
|
|
40
|
-
fsquad(v) {
|
|
41
|
-
const bitl_m = this.m;
|
|
42
|
-
const range_to = Math.floor((bitl_m - 1) / 2);
|
|
43
|
-
const val_a = v.mod();
|
|
44
|
-
let val_z = val_a.clone();
|
|
45
|
-
for (let idx = 1; idx <= range_to; idx++) {
|
|
46
|
-
val_z = val_z.mulmod(val_z);
|
|
47
|
-
val_z = val_z.mulmod(val_z);
|
|
48
|
-
val_z = val_z.add(val_a);
|
|
49
|
-
}
|
|
50
|
-
const val_w = val_z.mulmod(val_z).add(val_z);
|
|
51
|
-
if (val_w.compare(val_a) == 0)
|
|
52
|
-
return val_z.mod();
|
|
53
|
-
throw new Error("squad eq fail: no square root exists");
|
|
54
|
-
}
|
|
55
|
-
expand(x) {
|
|
56
|
-
const bit = x.testBit(0);
|
|
57
|
-
const xClean = x.clone();
|
|
58
|
-
xClean.setBit(0, 0);
|
|
59
|
-
const traceX = xClean.trace();
|
|
60
|
-
const a = this.a ?? 0;
|
|
61
|
-
if ((traceX === 1 && a === 0) || (traceX === 0 && a === 1))
|
|
62
|
-
xClean.setBit(0, 1);
|
|
63
|
-
const x2 = xClean.mulmod(xClean);
|
|
64
|
-
let rhs = x2.mulmod(xClean);
|
|
65
|
-
if (a === 1)
|
|
66
|
-
rhs = rhs.add(x2);
|
|
67
|
-
if (this.b)
|
|
68
|
-
rhs = rhs.add(this.b);
|
|
69
|
-
const x2inv = x2.invert();
|
|
70
|
-
const c = rhs.mulmod(x2inv);
|
|
71
|
-
const z = this.fsquad(c);
|
|
72
|
-
const y = z.mulmod(xClean);
|
|
73
|
-
const traceY = y.trace();
|
|
74
|
-
if ((traceY === 0 && bit === 1) || (traceY === 1 && bit === 0)) {
|
|
75
|
-
const currentBit = y.testBit(0);
|
|
76
|
-
y.setBit(0, 1 ^ currentBit);
|
|
77
|
-
}
|
|
78
|
-
xClean.curve = this;
|
|
79
|
-
y.curve = this;
|
|
80
|
-
return new Point(xClean, y);
|
|
81
|
-
}
|
|
82
|
-
}
|
package/dstu4145/ec/field.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import BN from "bn.js";
|
|
2
|
-
import type { Curve } from "./curve.js";
|
|
3
|
-
import type { TArg, TRet } from "@noble/hashes/utils.js";
|
|
4
|
-
export declare class Field {
|
|
5
|
-
curve: Curve | null;
|
|
6
|
-
value: BN;
|
|
7
|
-
constructor(value?: BN, curve?: Curve | null);
|
|
8
|
-
static fromString(str: string, base: number, curve?: Curve | null): Field;
|
|
9
|
-
static fromInt(v: number, curve?: Curve | null): Field;
|
|
10
|
-
static fromU8(v: Uint8Array, curve?: Curve | null): Field;
|
|
11
|
-
toString(base?: number): string;
|
|
12
|
-
compare(v: Field): number;
|
|
13
|
-
clone(): Field;
|
|
14
|
-
getLength(): number;
|
|
15
|
-
testBit(i: number): 0 | 1;
|
|
16
|
-
setBit(i: number, v: number): void;
|
|
17
|
-
shiftLeft(n: number): Field;
|
|
18
|
-
add(v: Field): Field;
|
|
19
|
-
static hashToField(curve: Curve, hash: TArg<Uint8Array>): Field;
|
|
20
|
-
static get0(curve?: Curve | null): Field;
|
|
21
|
-
static get1(curve?: Curve | null): Field;
|
|
22
|
-
is0(): boolean;
|
|
23
|
-
mod(): Field;
|
|
24
|
-
mulmod(v: Field): Field;
|
|
25
|
-
mul(v: Field): Field;
|
|
26
|
-
trace(): number;
|
|
27
|
-
div(v: Field): [Field, Field];
|
|
28
|
-
invert(): Field;
|
|
29
|
-
buf8(): TRet<Uint8Array>;
|
|
30
|
-
}
|