@li0ard/dstu 0.0.1 → 0.1.0
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/LICENSE +21 -0
- package/README.md +37 -45
- package/const.d.ts +14 -0
- package/const.js +1084 -0
- package/dstu4145/const.d.ts +22 -0
- package/dstu4145/const.js +36 -0
- package/dstu4145/ec/curve.d.ts +17 -0
- package/dstu4145/ec/curve.js +82 -0
- package/dstu4145/ec/field.d.ts +30 -0
- package/dstu4145/ec/field.js +135 -0
- package/dstu4145/ec/index.d.ts +3 -0
- package/dstu4145/ec/index.js +3 -0
- package/dstu4145/ec/point.d.ts +12 -0
- package/dstu4145/ec/point.js +79 -0
- package/dstu4145/index.d.ts +28 -0
- package/dstu4145/index.js +70 -0
- package/index.d.ts +6 -0
- package/index.js +6 -0
- package/kalyna/index.d.ts +50 -0
- package/kalyna/index.js +211 -0
- package/kupyna/index.d.ts +162 -0
- package/kupyna/index.js +241 -0
- package/kupyna/kmac.d.ts +46 -0
- package/kupyna/kmac.js +124 -0
- package/modes/cbc.d.ts +5 -0
- package/modes/cbc.js +10 -0
- package/modes/cfb.d.ts +5 -0
- package/modes/cfb.js +72 -0
- package/modes/ctr.d.ts +5 -0
- package/modes/ctr.js +24 -0
- package/modes/ecb.d.ts +4 -0
- package/modes/ecb.js +17 -0
- package/modes/gcm.d.ts +5 -0
- package/modes/gcm.js +20 -0
- package/modes/index.d.ts +7 -0
- package/modes/index.js +7 -0
- package/modes/mac.d.ts +6 -0
- package/modes/mac.js +65 -0
- package/modes/ofb.d.ts +5 -0
- package/modes/ofb.js +11 -0
- package/package.json +47 -7
- package/padding.d.ts +3 -0
- package/padding.js +22 -0
- package/strumok/index.d.ts +24 -0
- package/strumok/index.js +360 -0
- package/types.d.ts +41 -0
- package/types.js +1 -0
- package/utils.d.ts +14 -0
- package/utils.js +81 -0
package/kupyna/kmac.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { numberToBytesLE, uint64sToBytesLE } from "../utils.js";
|
|
2
|
+
import { Kupyna256, Kupyna384, Kupyna512 } from "./index.js";
|
|
3
|
+
const dpad = numberToBytesLE(128, 128);
|
|
4
|
+
const kpad32 = new Uint8Array([
|
|
5
|
+
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
6
|
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
7
|
+
]);
|
|
8
|
+
const kpad48 = new Uint8Array([
|
|
9
|
+
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
10
|
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
11
|
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
12
|
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
13
|
+
0x00, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
14
|
+
]);
|
|
15
|
+
const kpad64 = new Uint8Array([
|
|
16
|
+
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
17
|
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
18
|
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
19
|
+
0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
20
|
+
]);
|
|
21
|
+
class KupynaKMAC {
|
|
22
|
+
outputLen;
|
|
23
|
+
blockLen;
|
|
24
|
+
canXOF = false;
|
|
25
|
+
threshold;
|
|
26
|
+
h;
|
|
27
|
+
ik;
|
|
28
|
+
len;
|
|
29
|
+
constructor(hash, kpad, key) {
|
|
30
|
+
this.len = 0n;
|
|
31
|
+
this.h = hash();
|
|
32
|
+
this.outputLen = this.h.outputLen;
|
|
33
|
+
this.blockLen = this.h.blockLen;
|
|
34
|
+
if (key.length != this.h.outputLen)
|
|
35
|
+
throw new Error("Invalid key length");
|
|
36
|
+
this.h.update(key);
|
|
37
|
+
this.h.update(kpad);
|
|
38
|
+
this.ik = new Uint8Array(key.length);
|
|
39
|
+
for (let i = 0; i < key.length; i++)
|
|
40
|
+
this.ik[i] = ~key[i] & 0xFF;
|
|
41
|
+
this.threshold = BigInt(this.blockLen - 12);
|
|
42
|
+
}
|
|
43
|
+
update(data) {
|
|
44
|
+
this.len += BigInt(data.length);
|
|
45
|
+
this.h.update(data);
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
48
|
+
digest() {
|
|
49
|
+
const buffer = new Uint8Array(this.outputLen);
|
|
50
|
+
this.digestInto(buffer);
|
|
51
|
+
return buffer;
|
|
52
|
+
}
|
|
53
|
+
digestInto(buffer) {
|
|
54
|
+
let n = this.len;
|
|
55
|
+
let pad_size;
|
|
56
|
+
if (n < this.threshold)
|
|
57
|
+
pad_size = this.threshold - 1n - n;
|
|
58
|
+
else
|
|
59
|
+
pad_size = (BigInt(this.blockLen) - 1n) - ((n - this.threshold) % BigInt(this.blockLen));
|
|
60
|
+
n *= 8n;
|
|
61
|
+
this.h.update(dpad.slice(0, Number(pad_size + 1n)));
|
|
62
|
+
this.h.update(uint64sToBytesLE(new BigUint64Array([n])));
|
|
63
|
+
this.h.update(dpad.slice(16, 20));
|
|
64
|
+
this.h.update(this.ik);
|
|
65
|
+
this.h.digestInto(buffer);
|
|
66
|
+
}
|
|
67
|
+
destroy() {
|
|
68
|
+
throw new Error("Not implemented");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/** Kupyna KMAC (256 version) */
|
|
72
|
+
export class KupynaKMAC256 extends KupynaKMAC {
|
|
73
|
+
key;
|
|
74
|
+
constructor(key) {
|
|
75
|
+
super(Kupyna256.create, kpad32, key);
|
|
76
|
+
this.key = key;
|
|
77
|
+
}
|
|
78
|
+
_cloneInto(to) {
|
|
79
|
+
to ||= new KupynaKMAC256(this.key);
|
|
80
|
+
to.h = this.h.clone();
|
|
81
|
+
to.len = this.len;
|
|
82
|
+
to.ik = new Uint8Array(this.ik);
|
|
83
|
+
return to;
|
|
84
|
+
}
|
|
85
|
+
clone() { return this._cloneInto(); }
|
|
86
|
+
}
|
|
87
|
+
/** Kupyna KMAC (384 bit version) */
|
|
88
|
+
export class KupynaKMAC384 extends KupynaKMAC {
|
|
89
|
+
key;
|
|
90
|
+
constructor(key) {
|
|
91
|
+
super(Kupyna384.create, kpad48, key);
|
|
92
|
+
this.key = key;
|
|
93
|
+
}
|
|
94
|
+
_cloneInto(to) {
|
|
95
|
+
to ||= new KupynaKMAC384(this.key);
|
|
96
|
+
to.h = this.h.clone();
|
|
97
|
+
to.len = this.len;
|
|
98
|
+
to.ik = new Uint8Array(this.ik);
|
|
99
|
+
return to;
|
|
100
|
+
}
|
|
101
|
+
clone() { return this._cloneInto(); }
|
|
102
|
+
}
|
|
103
|
+
/** Kupyna KMAC (512 bit version) */
|
|
104
|
+
export class KupynaKMAC512 extends KupynaKMAC {
|
|
105
|
+
key;
|
|
106
|
+
constructor(key) {
|
|
107
|
+
super(Kupyna512.create, kpad64, key);
|
|
108
|
+
this.key = key;
|
|
109
|
+
}
|
|
110
|
+
_cloneInto(to) {
|
|
111
|
+
to ||= new KupynaKMAC512(this.key);
|
|
112
|
+
to.h = this.h.clone();
|
|
113
|
+
to.len = this.len;
|
|
114
|
+
to.ik = new Uint8Array(this.ik);
|
|
115
|
+
return to;
|
|
116
|
+
}
|
|
117
|
+
clone() { return this._cloneInto(); }
|
|
118
|
+
}
|
|
119
|
+
/** Kupyna KMAC (256 version) */
|
|
120
|
+
export const kmac256 = (key, msg) => new KupynaKMAC256(key).update(msg).digest();
|
|
121
|
+
/** Kupyna KMAC (384 version) */
|
|
122
|
+
export const kmac384 = (key, msg) => new KupynaKMAC384(key).update(msg).digest();
|
|
123
|
+
/** Kupyna KMAC (512 version) */
|
|
124
|
+
export const kmac512 = (key, msg) => new KupynaKMAC512(key).update(msg).digest();
|
package/modes/cbc.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { TArg } from "@noble/hashes/utils.js";
|
|
2
|
+
import type { Kalyna } from "../kalyna/index.js";
|
|
3
|
+
import type { BlockMode } from "../types.js";
|
|
4
|
+
/** Cipher Block Chaining (CBC) mode */
|
|
5
|
+
export declare const cbc: (cipher: Kalyna, iv: TArg<Uint8Array>) => BlockMode;
|
package/modes/cbc.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { cbc_decrypt, cbc_encrypt } from "@li0ard/sp80038";
|
|
2
|
+
/** Cipher Block Chaining (CBC) mode */
|
|
3
|
+
export const cbc = (cipher, iv) => {
|
|
4
|
+
const encrypter = cipher.encrypt.bind(cipher);
|
|
5
|
+
const decrypter = cipher.decrypt.bind(cipher);
|
|
6
|
+
return {
|
|
7
|
+
encrypt: (plaintext) => cbc_encrypt(encrypter, cipher.blockSize, plaintext, iv),
|
|
8
|
+
decrypt: (ciphertext) => cbc_decrypt(decrypter, cipher.blockSize, ciphertext, iv)
|
|
9
|
+
};
|
|
10
|
+
};
|
package/modes/cfb.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { TArg } from "@noble/hashes/utils.js";
|
|
2
|
+
import type { Kalyna } from "../kalyna/index.js";
|
|
3
|
+
import type { BlockMode } from "../types.js";
|
|
4
|
+
/** Cipher Feedback (CFB) mode */
|
|
5
|
+
export declare const cfb: (cipher: Kalyna, iv: TArg<Uint8Array>, q?: number) => BlockMode;
|
package/modes/cfb.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/** Cipher Feedback (CFB) mode */
|
|
2
|
+
export const cfb = (cipher, iv, q = cipher.blockSize) => {
|
|
3
|
+
const encrypter = cipher.encrypt.bind(cipher);
|
|
4
|
+
return {
|
|
5
|
+
encrypt: (plaintext) => {
|
|
6
|
+
const blockSize = cipher.blockSize;
|
|
7
|
+
if (q !== 1 && q !== 8 && q !== 16 && q !== 32 && q !== 64)
|
|
8
|
+
throw new Error('q must be 1, 8, 16, 32, or 64');
|
|
9
|
+
if (q > blockSize)
|
|
10
|
+
throw new Error('q cannot exceed block size');
|
|
11
|
+
let gamma = encrypter(iv);
|
|
12
|
+
const feed = new Uint8Array(iv);
|
|
13
|
+
let offset = 0;
|
|
14
|
+
const result = new Uint8Array(plaintext.length);
|
|
15
|
+
let dataOff = 0;
|
|
16
|
+
while (offset > 0 && dataOff < plaintext.length) {
|
|
17
|
+
result[dataOff] = plaintext[dataOff] ^ gamma[offset];
|
|
18
|
+
feed[offset++] = result[dataOff++];
|
|
19
|
+
if (offset >= blockSize) {
|
|
20
|
+
gamma = encrypter(feed);
|
|
21
|
+
offset = blockSize - q;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
while (dataOff + q <= plaintext.length) {
|
|
25
|
+
for (let i = 0; i < q; i++)
|
|
26
|
+
result[dataOff + i] = plaintext[dataOff + i] ^ gamma[blockSize - q + i];
|
|
27
|
+
feed.set(gamma.subarray(0, blockSize - q));
|
|
28
|
+
feed.set(result.subarray(dataOff, dataOff + q), blockSize - q);
|
|
29
|
+
gamma = encrypter(feed);
|
|
30
|
+
dataOff += q;
|
|
31
|
+
}
|
|
32
|
+
while (dataOff < plaintext.length) {
|
|
33
|
+
result[dataOff] = plaintext[dataOff] ^ gamma[blockSize - (plaintext.length - dataOff)];
|
|
34
|
+
dataOff++;
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
},
|
|
38
|
+
decrypt: (ciphertext) => {
|
|
39
|
+
const blockSize = cipher.blockSize;
|
|
40
|
+
if (q !== 1 && q !== 8 && q !== 16 && q !== 32 && q !== 64)
|
|
41
|
+
throw new Error('q must be 1, 8, 16, 32, or 64');
|
|
42
|
+
if (q > blockSize)
|
|
43
|
+
throw new Error('q cannot exceed block size');
|
|
44
|
+
let gamma = encrypter(iv);
|
|
45
|
+
const feed = new Uint8Array(iv);
|
|
46
|
+
let offset = 0;
|
|
47
|
+
const result = new Uint8Array(ciphertext.length);
|
|
48
|
+
let dataOff = 0;
|
|
49
|
+
while (offset > 0 && dataOff < ciphertext.length) {
|
|
50
|
+
result[dataOff] = ciphertext[dataOff] ^ gamma[offset];
|
|
51
|
+
feed[offset++] = ciphertext[dataOff++];
|
|
52
|
+
if (offset >= blockSize) {
|
|
53
|
+
gamma = encrypter(feed);
|
|
54
|
+
offset = blockSize - q;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
while (dataOff + q <= ciphertext.length) {
|
|
58
|
+
for (let i = 0; i < q; i++)
|
|
59
|
+
result[dataOff + i] = ciphertext[dataOff + i] ^ gamma[blockSize - q + i];
|
|
60
|
+
feed.set(gamma.subarray(0, blockSize - q));
|
|
61
|
+
feed.set(ciphertext.subarray(dataOff, dataOff + q), blockSize - q);
|
|
62
|
+
gamma = encrypter(feed);
|
|
63
|
+
dataOff += q;
|
|
64
|
+
}
|
|
65
|
+
while (dataOff < ciphertext.length) {
|
|
66
|
+
result[dataOff] = ciphertext[dataOff] ^ gamma[blockSize - (ciphertext.length - dataOff)];
|
|
67
|
+
dataOff++;
|
|
68
|
+
}
|
|
69
|
+
return result;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
};
|
package/modes/ctr.d.ts
ADDED
package/modes/ctr.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { concatBytes } from "@noble/hashes/utils.js";
|
|
2
|
+
import { xorBytes } from "../utils.js";
|
|
3
|
+
const incrementCounterAt = (ctr, pos) => {
|
|
4
|
+
let j = pos;
|
|
5
|
+
while (j < ctr.length)
|
|
6
|
+
if (++ctr[j++] != 0)
|
|
7
|
+
break;
|
|
8
|
+
};
|
|
9
|
+
/** Counter (CTR) mode */
|
|
10
|
+
export const ctr = (cipher, iv) => {
|
|
11
|
+
if (iv.length !== cipher.blockSize)
|
|
12
|
+
throw new Error("Invalid IV size");
|
|
13
|
+
return {
|
|
14
|
+
crypt: (msg) => {
|
|
15
|
+
const keystreamBlocks = [];
|
|
16
|
+
const ctr = cipher.encrypt(iv);
|
|
17
|
+
for (let i = 0; i < Math.ceil(msg.length / cipher.blockSize); i++) {
|
|
18
|
+
incrementCounterAt(ctr, 0);
|
|
19
|
+
keystreamBlocks.push(cipher.encrypt(ctr));
|
|
20
|
+
}
|
|
21
|
+
return xorBytes(concatBytes(...keystreamBlocks), msg);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
};
|
package/modes/ecb.d.ts
ADDED
package/modes/ecb.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Electronic Codebook (ECB) mode */
|
|
2
|
+
export const ecb = (cipher) => {
|
|
3
|
+
const encrypter = cipher.encrypt.bind(cipher);
|
|
4
|
+
const decrypter = cipher.decrypt.bind(cipher);
|
|
5
|
+
const core = (crypter, data) => {
|
|
6
|
+
if (data.length == 0 || data.length % cipher.blockSize !== 0)
|
|
7
|
+
throw new Error("Data not aligned");
|
|
8
|
+
const output = new Uint8Array(data.length);
|
|
9
|
+
for (let i = 0; i < data.length; i += cipher.blockSize)
|
|
10
|
+
output.set(crypter(data.subarray(i, i + cipher.blockSize)), i);
|
|
11
|
+
return output;
|
|
12
|
+
};
|
|
13
|
+
return {
|
|
14
|
+
encrypt: (plaintext) => core(encrypter, plaintext),
|
|
15
|
+
decrypt: (ciphertext) => core(decrypter, ciphertext),
|
|
16
|
+
};
|
|
17
|
+
};
|
package/modes/gcm.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type TArg } from "@noble/hashes/utils.js";
|
|
2
|
+
import type { Kalyna } from "../kalyna/index.js";
|
|
3
|
+
import type { AEADMode } from "../types.js";
|
|
4
|
+
/** Galois counter (GCM) mode (AEAD) */
|
|
5
|
+
export declare const gcm: (cipher: Kalyna, iv: TArg<Uint8Array>, q?: number) => AEADMode;
|
package/modes/gcm.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { concatBytes } from "@noble/hashes/utils.js";
|
|
2
|
+
import { ctr } from "./ctr.js";
|
|
3
|
+
import { gmac } from "./mac.js";
|
|
4
|
+
import { equalBytes } from "../utils.js";
|
|
5
|
+
/** Galois counter (GCM) mode (AEAD) */
|
|
6
|
+
export const gcm = (cipher, iv, q = 16) => {
|
|
7
|
+
return {
|
|
8
|
+
seal: (plaintext, aad) => {
|
|
9
|
+
const enc = ctr(cipher, iv).crypt(plaintext);
|
|
10
|
+
return concatBytes(enc, gmac(cipher, q).compute(aad || new Uint8Array(), enc));
|
|
11
|
+
},
|
|
12
|
+
open: (ciphertext, aad) => {
|
|
13
|
+
const enc = ciphertext.slice(0, -q);
|
|
14
|
+
const hC = gmac(cipher, q).compute(aad || new Uint8Array(), enc);
|
|
15
|
+
if (!equalBytes(ciphertext.subarray(-q), hC))
|
|
16
|
+
throw new Error("Invalid MAC");
|
|
17
|
+
return ctr(cipher, iv).crypt(enc);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
};
|
package/modes/index.d.ts
ADDED
package/modes/index.js
ADDED
package/modes/mac.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Kalyna } from "../kalyna/index.js";
|
|
2
|
+
import type { GMACMode, MACMode } from "../types.js";
|
|
3
|
+
/** Message Authentication Code (MAC) mode */
|
|
4
|
+
export declare const cmac: (cipher: Kalyna, q?: number) => MACMode;
|
|
5
|
+
/** Galois Message Authentication Code (GMAC) mode */
|
|
6
|
+
export declare const gmac: (cipher: Kalyna, q?: number) => GMACMode;
|
package/modes/mac.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { concatBytes, copyBytes } from "@noble/hashes/utils.js";
|
|
2
|
+
import { pad } from "../padding.js";
|
|
3
|
+
import { gf2mMul, numberToBytesLE, xorBytes } from "../utils.js";
|
|
4
|
+
/** Message Authentication Code (MAC) mode */
|
|
5
|
+
export const cmac = (cipher, q = 16) => {
|
|
6
|
+
return {
|
|
7
|
+
compute: (msg) => {
|
|
8
|
+
let data = copyBytes(msg);
|
|
9
|
+
const zeroBlock = new Uint8Array(cipher.blockSize);
|
|
10
|
+
if (data.length % cipher.blockSize !== 0) {
|
|
11
|
+
data = pad(data, cipher.blockSize);
|
|
12
|
+
zeroBlock[0] = 1;
|
|
13
|
+
}
|
|
14
|
+
const Kd = cipher.encrypt(zeroBlock);
|
|
15
|
+
let c = new Uint8Array(cipher.blockSize);
|
|
16
|
+
const numBlocks = data.length / cipher.blockSize;
|
|
17
|
+
for (let i = 0; i < numBlocks - 1; i++) {
|
|
18
|
+
const blockStart = i * cipher.blockSize;
|
|
19
|
+
c = cipher.encrypt(xorBytes(c, data.subarray(blockStart, blockStart + cipher.blockSize)));
|
|
20
|
+
}
|
|
21
|
+
const lastBlockStart = (numBlocks - 1) * cipher.blockSize;
|
|
22
|
+
c = cipher.encrypt(xorBytes(xorBytes(c, data.subarray(lastBlockStart, lastBlockStart + cipher.blockSize)), Kd));
|
|
23
|
+
return c.slice(0, q);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
/** Galois Message Authentication Code (GMAC) mode */
|
|
28
|
+
export const gmac = (cipher, q = 16) => {
|
|
29
|
+
return {
|
|
30
|
+
compute: (aad, msg) => {
|
|
31
|
+
const blockSize = cipher.blockSize;
|
|
32
|
+
const H = cipher.encrypt(new Uint8Array(blockSize));
|
|
33
|
+
let B = new Uint8Array(blockSize);
|
|
34
|
+
let i = 0;
|
|
35
|
+
while (i < aad.length) {
|
|
36
|
+
const blockSizeToProcess = Math.min(blockSize, aad.length - i);
|
|
37
|
+
const block = new Uint8Array(blockSize);
|
|
38
|
+
for (let j = 0; j < blockSizeToProcess; j++)
|
|
39
|
+
block[j] = aad[i + j];
|
|
40
|
+
if (blockSizeToProcess < blockSize)
|
|
41
|
+
block[blockSizeToProcess] = 0x80;
|
|
42
|
+
for (let j = 0; j < blockSize; j++)
|
|
43
|
+
B[j] ^= block[j];
|
|
44
|
+
B = gf2mMul(blockSize, B, H);
|
|
45
|
+
i += blockSize;
|
|
46
|
+
}
|
|
47
|
+
i = 0;
|
|
48
|
+
while (i < msg.length) {
|
|
49
|
+
const blockSizeToProcess = Math.min(blockSize, msg.length - i);
|
|
50
|
+
const block = new Uint8Array(blockSize);
|
|
51
|
+
for (let j = 0; j < blockSizeToProcess; j++)
|
|
52
|
+
block[j] = msg[i + j];
|
|
53
|
+
if (blockSizeToProcess < blockSize)
|
|
54
|
+
block[blockSizeToProcess] = 0x80;
|
|
55
|
+
for (let j = 0; j < blockSize; j++)
|
|
56
|
+
B[j] ^= block[j];
|
|
57
|
+
B = gf2mMul(blockSize, B, H);
|
|
58
|
+
i += blockSize;
|
|
59
|
+
}
|
|
60
|
+
B = xorBytes(B, concatBytes(numberToBytesLE(aad.length * 8, blockSize / 2), numberToBytesLE(msg.length * 8, blockSize / 2)));
|
|
61
|
+
// B = gf2mMul(blockSize, B, H);
|
|
62
|
+
return cipher.encrypt(B).slice(0, q);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
};
|
package/modes/ofb.d.ts
ADDED
package/modes/ofb.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import {} from "@noble/hashes/utils.js";
|
|
2
|
+
import { ofb as ofb_ } from "@li0ard/sp80038";
|
|
3
|
+
/** Output Feedback (OFB) mode */
|
|
4
|
+
export const ofb = (cipher, iv) => {
|
|
5
|
+
if (iv.length !== cipher.blockSize)
|
|
6
|
+
throw new Error("Invalid IV size");
|
|
7
|
+
const encrypter = cipher.encrypt.bind(cipher);
|
|
8
|
+
return {
|
|
9
|
+
crypt: (msg) => ofb_(encrypter, cipher.blockSize, msg, iv)
|
|
10
|
+
};
|
|
11
|
+
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@li0ard/dstu",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"types": "index.d.ts",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./index.js",
|
|
9
|
+
"./dstu4145.js": "./dstu4145/index.js",
|
|
10
|
+
"./kalyna.js": "./kalyna/index.js",
|
|
11
|
+
"./kupyna.js": "./kupyna/index.js",
|
|
12
|
+
"./modes.js": "./modes/index.js",
|
|
13
|
+
"./strumok.js": "./strumok/index.js",
|
|
14
|
+
"./types.js": "./types.js"
|
|
15
|
+
},
|
|
16
|
+
"author": "li0ard",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/li0ard/dstu.git"
|
|
20
|
+
},
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/li0ard/dstu/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/li0ard/dstu#readme",
|
|
25
|
+
"funding": "https://li0ard.rest/donate",
|
|
26
|
+
"description": "DSTU cryptography algorithms in pure TypeScript",
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "rm -rf dist/ && tsc --project tsconfig.build.json",
|
|
30
|
+
"docs": "typedoc --options .config/typedoc/config.cjs"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/bn.js": "^5.2.0",
|
|
34
|
+
"@types/bun": "latest",
|
|
35
|
+
"typedoc": "^0.28.19"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"typescript": "^5"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@li0ard/sp80038": "^0.1.2",
|
|
42
|
+
"@noble/hashes": "^2.2.0",
|
|
43
|
+
"bn.js": "^5.2.3"
|
|
44
|
+
},
|
|
45
|
+
"files": ["**/*.js", "**/*.d.ts"],
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public"
|
|
48
|
+
},
|
|
49
|
+
"keywords": ["dstu", "crypto", "kalyna", "kupyna", "strumok"]
|
|
10
50
|
}
|
package/padding.d.ts
ADDED
package/padding.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export const pad = (data, blockSize) => {
|
|
2
|
+
const remainder = data.length % blockSize;
|
|
3
|
+
if (remainder === 0)
|
|
4
|
+
return data;
|
|
5
|
+
const padLength = blockSize - remainder;
|
|
6
|
+
const padded = new Uint8Array(data.length + padLength);
|
|
7
|
+
padded.set(data);
|
|
8
|
+
padded[data.length] = 0x80;
|
|
9
|
+
return padded;
|
|
10
|
+
};
|
|
11
|
+
export const unpad = (paddedData, blockSize) => {
|
|
12
|
+
if (paddedData.length % blockSize !== 0)
|
|
13
|
+
throw new Error("Padded data length must be multiple of block size");
|
|
14
|
+
let i = paddedData.length - 1;
|
|
15
|
+
while (i >= 0 && paddedData[i] === 0)
|
|
16
|
+
i--;
|
|
17
|
+
if (i < 0)
|
|
18
|
+
throw new Error("Padding error: all bytes are zero");
|
|
19
|
+
if (i === paddedData.length - 1)
|
|
20
|
+
throw new Error("Padding error: no padding bytes found");
|
|
21
|
+
return paddedData.slice(0, i + 1);
|
|
22
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { TArg, TRet } from "@noble/hashes/utils.js";
|
|
2
|
+
/** Strumok stream cipher */
|
|
3
|
+
export declare class Strumok {
|
|
4
|
+
readonly blockSize = 128;
|
|
5
|
+
readonly keySize: number;
|
|
6
|
+
private S;
|
|
7
|
+
private r;
|
|
8
|
+
private key;
|
|
9
|
+
private iv;
|
|
10
|
+
/**
|
|
11
|
+
* Strumok stream cipher
|
|
12
|
+
* @param key Encryption key (256 or 512 bit)
|
|
13
|
+
*/
|
|
14
|
+
constructor(key: TArg<Uint8Array>, iv: TArg<Uint8Array>);
|
|
15
|
+
/** Generate next keystream */
|
|
16
|
+
next_stream(): TRet<BigUint64Array>;
|
|
17
|
+
/** Generate next keystream and perform encryption */
|
|
18
|
+
next_stream_full_crypt(in_: TArg<BigUint64Array>): TRet<BigUint64Array>;
|
|
19
|
+
/**
|
|
20
|
+
* Perform encryption/decryption
|
|
21
|
+
* @param in_ Input data
|
|
22
|
+
*/
|
|
23
|
+
crypt(in_: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
24
|
+
}
|