@li0ard/dstu 0.1.5 → 0.1.7
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 +5 -0
- package/dstu4145/const.d.ts +2 -0
- package/dstu4145/const.js +17 -0
- package/dstu4145/ec/index.d.ts +8 -4
- package/dstu4145/ec/index.js +24 -6
- package/dstu4145/ec/math.js +2 -0
- package/dstu4145/index.d.ts +11 -0
- package/dstu4145/index.js +35 -6
- package/dstu9041/curve.js +13 -10
- package/dstu9041/index.d.ts +2 -2
- package/dstu9041/index.js +48 -44
- package/dstu9311/const.d.ts +29 -0
- package/dstu9311/const.js +150 -0
- package/dstu9311/hash.d.ts +34 -0
- package/dstu9311/hash.js +91 -0
- package/dstu9311/index.d.ts +16 -0
- package/dstu9311/index.js +59 -0
- package/index.d.ts +2 -0
- package/index.js +2 -0
- package/keywrap.d.ts +6 -0
- package/keywrap.js +34 -0
- package/modes/cbc.d.ts +2 -3
- package/modes/cbc.js +6 -5
- package/modes/cfb.d.ts +3 -4
- package/modes/cfb.js +88 -56
- package/modes/cmac.d.ts +2 -0
- package/modes/cmac.js +45 -0
- package/modes/ctr.d.ts +2 -3
- package/modes/ctr.js +25 -14
- package/modes/ecb.d.ts +2 -3
- package/modes/gcm.d.ts +2 -3
- package/modes/gcm.js +18 -14
- package/modes/gmac.d.ts +3 -0
- package/modes/gmac.js +40 -0
- package/modes/index.d.ts +3 -1
- package/modes/index.js +3 -1
- package/modes/kw.d.ts +5 -4
- package/modes/kw.js +63 -56
- package/modes/ofb.d.ts +2 -3
- package/modes/ofb.js +3 -2
- package/modes/xts.d.ts +7 -0
- package/modes/xts.js +93 -0
- package/package.json +3 -2
- package/strumok/index.d.ts +4 -3
- package/strumok/index.js +42 -277
- package/types.d.ts +5 -3
- package/utils.d.ts +5 -0
- package/utils.js +14 -2
- package/modes/mac.d.ts +0 -6
- package/modes/mac.js +0 -61
package/modes/cfb.js
CHANGED
|
@@ -1,65 +1,97 @@
|
|
|
1
|
+
import { concatBytes, copyBytes } from "@noble/hashes/utils.js";
|
|
2
|
+
import { isKalyna, xorBytes } from "../utils.js";
|
|
1
3
|
/** Cipher Feedback (CFB) mode */
|
|
2
4
|
export const cfb = (cipher, iv, q = cipher.blockSize) => {
|
|
3
|
-
|
|
5
|
+
const _isKalyna = isKalyna(cipher);
|
|
6
|
+
if (_isKalyna && q !== 1 && q !== 8 && q !== 16 && q !== 32 && q !== 64)
|
|
4
7
|
throw new Error('q must be 1, 8, 16, 32, or 64');
|
|
5
|
-
if (q > cipher.blockSize)
|
|
8
|
+
if (_isKalyna && q > cipher.blockSize)
|
|
6
9
|
throw new Error('q cannot exceed block size');
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
offset
|
|
10
|
+
if (!_isKalyna && (iv.length === 0 || iv.length % cipher.blockSize !== 0))
|
|
11
|
+
throw new Error("Invalid IV size");
|
|
12
|
+
if (_isKalyna)
|
|
13
|
+
return Object.freeze({
|
|
14
|
+
encrypt: (plaintext) => {
|
|
15
|
+
const gamma = cipher.encrypt(iv);
|
|
16
|
+
const feed = copyBytes(iv);
|
|
17
|
+
const result = new Uint8Array(plaintext.length);
|
|
18
|
+
let offset = 0, dataOff = 0;
|
|
19
|
+
while (offset > 0 && dataOff < plaintext.length) {
|
|
20
|
+
result[dataOff] = plaintext[dataOff] ^ gamma[offset];
|
|
21
|
+
feed[offset++] = result[dataOff++];
|
|
22
|
+
if (offset >= cipher.blockSize) {
|
|
23
|
+
gamma.set(cipher.encrypt(feed));
|
|
24
|
+
offset = cipher.blockSize - q;
|
|
25
|
+
}
|
|
20
26
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
dataOff += q;
|
|
29
|
-
}
|
|
30
|
-
while (dataOff < plaintext.length) {
|
|
31
|
-
result[dataOff] = plaintext[dataOff] ^ gamma[cipher.blockSize - (plaintext.length - dataOff)];
|
|
32
|
-
dataOff++;
|
|
33
|
-
}
|
|
34
|
-
return result;
|
|
35
|
-
},
|
|
36
|
-
decrypt: (ciphertext) => {
|
|
37
|
-
let gamma = cipher.encrypt(iv);
|
|
38
|
-
const feed = new Uint8Array(iv);
|
|
39
|
-
let offset = 0;
|
|
40
|
-
const result = new Uint8Array(ciphertext.length);
|
|
41
|
-
let dataOff = 0;
|
|
42
|
-
while (offset > 0 && dataOff < ciphertext.length) {
|
|
43
|
-
result[dataOff] = ciphertext[dataOff] ^ gamma[offset];
|
|
44
|
-
feed[offset++] = ciphertext[dataOff++];
|
|
45
|
-
if (offset >= cipher.blockSize) {
|
|
46
|
-
gamma = cipher.encrypt(feed);
|
|
47
|
-
offset = cipher.blockSize - q;
|
|
27
|
+
while (dataOff + q <= plaintext.length) {
|
|
28
|
+
for (let i = 0; i < q; i++)
|
|
29
|
+
result[dataOff + i] = plaintext[dataOff + i] ^ gamma[cipher.blockSize - q + i];
|
|
30
|
+
feed.set(gamma.subarray(0, cipher.blockSize - q));
|
|
31
|
+
feed.set(result.subarray(dataOff, dataOff + q), cipher.blockSize - q);
|
|
32
|
+
gamma.set(cipher.encrypt(feed));
|
|
33
|
+
dataOff += q;
|
|
48
34
|
}
|
|
35
|
+
while (dataOff < plaintext.length) {
|
|
36
|
+
result[dataOff] = plaintext[dataOff] ^ gamma[cipher.blockSize - (plaintext.length - dataOff)];
|
|
37
|
+
dataOff++;
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
40
|
+
},
|
|
41
|
+
decrypt: (ciphertext) => {
|
|
42
|
+
const gamma = cipher.encrypt(iv);
|
|
43
|
+
const feed = copyBytes(iv);
|
|
44
|
+
const result = new Uint8Array(ciphertext.length);
|
|
45
|
+
let offset = 0, dataOff = 0;
|
|
46
|
+
while (offset > 0 && dataOff < ciphertext.length) {
|
|
47
|
+
result[dataOff] = ciphertext[dataOff] ^ gamma[offset];
|
|
48
|
+
feed[offset++] = ciphertext[dataOff++];
|
|
49
|
+
if (offset >= cipher.blockSize) {
|
|
50
|
+
gamma.set(cipher.encrypt(feed));
|
|
51
|
+
offset = cipher.blockSize - q;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
while (dataOff + q <= ciphertext.length) {
|
|
55
|
+
for (let i = 0; i < q; i++)
|
|
56
|
+
result[dataOff + i] = ciphertext[dataOff + i] ^ gamma[cipher.blockSize - q + i];
|
|
57
|
+
feed.set(gamma.subarray(0, cipher.blockSize - q));
|
|
58
|
+
feed.set(ciphertext.subarray(dataOff, dataOff + q), cipher.blockSize - q);
|
|
59
|
+
gamma.set(cipher.encrypt(feed));
|
|
60
|
+
dataOff += q;
|
|
61
|
+
}
|
|
62
|
+
while (dataOff < ciphertext.length) {
|
|
63
|
+
result[dataOff] = ciphertext[dataOff] ^ gamma[cipher.blockSize - (ciphertext.length - dataOff)];
|
|
64
|
+
dataOff++;
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
49
67
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
68
|
+
});
|
|
69
|
+
else
|
|
70
|
+
return Object.freeze({
|
|
71
|
+
encrypt: (plaintext) => {
|
|
72
|
+
let r = [];
|
|
73
|
+
for (let i = 0; i < iv.length; i += cipher.blockSize)
|
|
74
|
+
r.push(iv.subarray(i, i + cipher.blockSize));
|
|
75
|
+
const len = Math.max(8, (plaintext.length + 7) & ~7);
|
|
76
|
+
const result = [];
|
|
77
|
+
for (let i = 0; i < len; i += cipher.blockSize) {
|
|
78
|
+
result.push(xorBytes(cipher.encrypt(r[0]), plaintext.subarray(i, i + cipher.blockSize)));
|
|
79
|
+
r = r.slice(1).concat(result[result.length - 1]);
|
|
80
|
+
}
|
|
81
|
+
return concatBytes(...result);
|
|
82
|
+
},
|
|
83
|
+
decrypt: (ciphertext) => {
|
|
84
|
+
let r = [];
|
|
85
|
+
for (let i = 0; i < iv.length; i += cipher.blockSize)
|
|
86
|
+
r.push(iv.subarray(i, i + cipher.blockSize));
|
|
87
|
+
const len = Math.max(8, (ciphertext.length + 7) & ~7);
|
|
88
|
+
const result = [];
|
|
89
|
+
for (let i = 0; i < len; i += cipher.blockSize) {
|
|
90
|
+
const blk = ciphertext.slice(i, i + cipher.blockSize);
|
|
91
|
+
result.push(xorBytes(cipher.encrypt(r[0]), blk));
|
|
92
|
+
r = r.slice(1).concat(blk);
|
|
93
|
+
}
|
|
94
|
+
return concatBytes(...result);
|
|
61
95
|
}
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
});
|
|
96
|
+
});
|
|
65
97
|
};
|
package/modes/cmac.d.ts
ADDED
package/modes/cmac.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { concatBytes, bytesToNumberLE, numberToBytesLE } from "@noble/curves/utils.js";
|
|
2
|
+
import { pad } from "../padding.js";
|
|
3
|
+
import { isKalyna, xorBytes } from "../utils.js";
|
|
4
|
+
import { keySequences } from "../dstu9311/const.js";
|
|
5
|
+
export const cmac = (cipher, q = 16) => {
|
|
6
|
+
if (isKalyna(cipher))
|
|
7
|
+
return Object.freeze({
|
|
8
|
+
compute: (msg) => {
|
|
9
|
+
let data = msg;
|
|
10
|
+
const zeroBlock = new Uint8Array(cipher.blockSize);
|
|
11
|
+
if (data.length % cipher.blockSize !== 0) {
|
|
12
|
+
data = pad(data, cipher.blockSize);
|
|
13
|
+
zeroBlock[0] = 1;
|
|
14
|
+
}
|
|
15
|
+
const Kd = cipher.encrypt(zeroBlock);
|
|
16
|
+
const c = new Uint8Array(cipher.blockSize);
|
|
17
|
+
const numBlocks = data.length / cipher.blockSize;
|
|
18
|
+
for (let i = 0; i < numBlocks - 1; i++) {
|
|
19
|
+
const blockStart = i * cipher.blockSize;
|
|
20
|
+
c.set(cipher.encrypt(xorBytes(c, data.subarray(blockStart, blockStart + cipher.blockSize))));
|
|
21
|
+
}
|
|
22
|
+
const lastBlockStart = (numBlocks - 1) * cipher.blockSize;
|
|
23
|
+
c.set(cipher.encrypt(xorBytes(xorBytes(c, data.subarray(lastBlockStart, lastBlockStart + cipher.blockSize)), Kd)));
|
|
24
|
+
return c.slice(0, q);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
else
|
|
28
|
+
return Object.freeze({
|
|
29
|
+
compute: (msg) => {
|
|
30
|
+
const paddedLength = Math.max(8, (msg.length + 7) & ~7);
|
|
31
|
+
const paddedData = new Uint8Array(paddedLength);
|
|
32
|
+
paddedData.set(msg);
|
|
33
|
+
let prev0 = 0n, prev1 = 0n;
|
|
34
|
+
const feedback = new Uint8Array(cipher.blockSize);
|
|
35
|
+
for (let i = 0; i < paddedData.length; i += cipher.blockSize) {
|
|
36
|
+
feedback.set(numberToBytesLE(prev1, 4), 0);
|
|
37
|
+
feedback.set(numberToBytesLE(prev0, 4), 4);
|
|
38
|
+
const out = cipher.proceedBlock(xorBytes(paddedData.subarray(i, i + cipher.blockSize), feedback), keySequences.MAC);
|
|
39
|
+
prev0 = bytesToNumberLE(out.subarray(0, 4));
|
|
40
|
+
prev1 = bytesToNumberLE(out.subarray(4, 8));
|
|
41
|
+
}
|
|
42
|
+
return concatBytes(numberToBytesLE(prev1, 4), numberToBytesLE(prev0, 4)).slice(0, q);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
};
|
package/modes/ctr.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { type TArg } from "@noble/hashes/utils.js";
|
|
2
|
-
import type {
|
|
3
|
-
import type { StreamMode } from "../types.js";
|
|
2
|
+
import type { Cipher, StreamMode } from "../types.js";
|
|
4
3
|
/** Counter (CTR) mode */
|
|
5
|
-
export declare const ctr: (cipher:
|
|
4
|
+
export declare const ctr: (cipher: Cipher, iv: TArg<Uint8Array>) => StreamMode;
|
package/modes/ctr.js
CHANGED
|
@@ -1,24 +1,35 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { xorBytes } from "../utils.js";
|
|
3
|
-
const
|
|
4
|
-
let j = pos;
|
|
5
|
-
while (j < ctr.length)
|
|
6
|
-
if (++ctr[j++] != 0)
|
|
7
|
-
break;
|
|
8
|
-
};
|
|
1
|
+
import { createView } from "@noble/hashes/utils.js";
|
|
2
|
+
import { isKalyna, xorBytes } from "../utils.js";
|
|
3
|
+
const C1 = 0x01010104, C2 = 0x01010101;
|
|
9
4
|
/** Counter (CTR) mode */
|
|
10
5
|
export const ctr = (cipher, iv) => {
|
|
11
6
|
if (iv.length !== cipher.blockSize)
|
|
12
7
|
throw new Error("Invalid IV size");
|
|
8
|
+
const incrementCounter = (ctr) => {
|
|
9
|
+
if (isKalyna(cipher)) {
|
|
10
|
+
let j = 0;
|
|
11
|
+
while (j < ctr.length)
|
|
12
|
+
if (++ctr[j++] != 0)
|
|
13
|
+
break;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
const view = createView(ctr);
|
|
17
|
+
view.setUint32(0, (view.getUint32(0, true) + C2) >>> 0, true);
|
|
18
|
+
let s2 = view.getUint32(4, true) + C1;
|
|
19
|
+
if (s2 >= 0xFFFFFFFF)
|
|
20
|
+
s2 -= 0xFFFFFFFF;
|
|
21
|
+
view.setUint32(4, s2 >>> 0, true);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
13
24
|
return Object.freeze({
|
|
14
25
|
crypt: (msg) => {
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
26
|
+
const buf = cipher.encrypt(iv), output = new Uint8Array(msg.length);
|
|
27
|
+
for (let i = 0; i < msg.length; i += cipher.blockSize) {
|
|
28
|
+
incrementCounter(buf);
|
|
29
|
+
const ct = xorBytes(cipher.encrypt(buf), msg.subarray(i, i + cipher.blockSize));
|
|
30
|
+
output.set(ct, i);
|
|
20
31
|
}
|
|
21
|
-
return
|
|
32
|
+
return output;
|
|
22
33
|
}
|
|
23
34
|
});
|
|
24
35
|
};
|
package/modes/ecb.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { BlockMode } from "../types.js";
|
|
1
|
+
import type { BlockMode, Cipher } from "../types.js";
|
|
3
2
|
/** Electronic Codebook (ECB) mode */
|
|
4
|
-
export declare const ecb: (cipher:
|
|
3
|
+
export declare const ecb: (cipher: Cipher) => BlockMode;
|
package/modes/gcm.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { type TArg } from "@noble/hashes/utils.js";
|
|
2
|
-
import type {
|
|
3
|
-
import type { AEADMode } from "../types.js";
|
|
2
|
+
import type { AEADMode, Cipher } from "../types.js";
|
|
4
3
|
/** Galois counter (GCM) mode (AEAD) */
|
|
5
|
-
export declare const gcm: (cipher:
|
|
4
|
+
export declare const gcm: (cipher: Cipher, iv: TArg<Uint8Array>, q?: number) => AEADMode;
|
package/modes/gcm.js
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
import { concatBytes } from "@noble/hashes/utils.js";
|
|
2
2
|
import { ctr } from "./ctr.js";
|
|
3
|
-
import { gmac } from "./
|
|
3
|
+
import { gmac } from "./gmac.js";
|
|
4
4
|
import { equalBytes } from "@noble/curves/utils.js";
|
|
5
|
+
import { assertKalyna } from "../utils.js";
|
|
5
6
|
/** Galois counter (GCM) mode (AEAD) */
|
|
6
|
-
export const gcm = (cipher, iv, q = 16) =>
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
7
|
+
export const gcm = (cipher, iv, q = 16) => {
|
|
8
|
+
assertKalyna(cipher);
|
|
9
|
+
return Object.freeze({
|
|
10
|
+
seal: (plaintext, aad) => {
|
|
11
|
+
const enc = ctr(cipher, iv).crypt(plaintext);
|
|
12
|
+
return concatBytes(enc, gmac(cipher, q).compute(enc, aad));
|
|
13
|
+
},
|
|
14
|
+
open: (ciphertext, aad) => {
|
|
15
|
+
const enc = ciphertext.subarray(0, -q);
|
|
16
|
+
const hC = gmac(cipher, q).compute(enc, aad);
|
|
17
|
+
if (!equalBytes(ciphertext.subarray(-q), hC))
|
|
18
|
+
throw new Error("Invalid MAC");
|
|
19
|
+
return ctr(cipher, iv).crypt(enc);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
};
|
package/modes/gmac.d.ts
ADDED
package/modes/gmac.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { concatBytes } from "@noble/hashes/utils.js";
|
|
2
|
+
import { assertKalyna, gf2mMul } from "../utils.js";
|
|
3
|
+
import { numberToBytesLE } from "@noble/curves/utils.js";
|
|
4
|
+
const xorBytesInPlace = (a, b) => {
|
|
5
|
+
if (a.length != b.length)
|
|
6
|
+
throw new Error("Byte arrays must have same length");
|
|
7
|
+
for (let i = 0; i < a.length; i++)
|
|
8
|
+
a[i] ^= b[i];
|
|
9
|
+
};
|
|
10
|
+
/** Galois Message Authentication Code (GMAC) mode */
|
|
11
|
+
export const gmac = (cipher, q = 16) => {
|
|
12
|
+
assertKalyna(cipher);
|
|
13
|
+
const hblock = cipher.blockSize >> 1;
|
|
14
|
+
return Object.freeze({
|
|
15
|
+
compute: (msg, aad) => {
|
|
16
|
+
aad ??= new Uint8Array();
|
|
17
|
+
const H = cipher.encrypt(new Uint8Array(cipher.blockSize));
|
|
18
|
+
const B = new Uint8Array(cipher.blockSize);
|
|
19
|
+
const updateMac = (input) => {
|
|
20
|
+
let i = 0;
|
|
21
|
+
while (i < input.length) {
|
|
22
|
+
const blockSizeToProcess = Math.min(cipher.blockSize, input.length - i);
|
|
23
|
+
const block = new Uint8Array(cipher.blockSize);
|
|
24
|
+
for (let j = 0; j < blockSizeToProcess; j++)
|
|
25
|
+
block[j] = input[i + j];
|
|
26
|
+
if (blockSizeToProcess < cipher.blockSize)
|
|
27
|
+
block[blockSizeToProcess] = 0x80;
|
|
28
|
+
for (let j = 0; j < cipher.blockSize; j++)
|
|
29
|
+
B[j] ^= block[j];
|
|
30
|
+
B.set(gf2mMul(cipher.blockSize, B, H));
|
|
31
|
+
i += cipher.blockSize;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
updateMac(aad);
|
|
35
|
+
updateMac(msg);
|
|
36
|
+
xorBytesInPlace(B, concatBytes(numberToBytesLE(aad.length * 8, hblock), numberToBytesLE(msg.length * 8, hblock)));
|
|
37
|
+
return cipher.encrypt(B).slice(0, q);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
};
|
package/modes/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
export * from "./cbc.js";
|
|
2
2
|
export * from "./cfb.js";
|
|
3
|
+
export * from "./cmac.js";
|
|
3
4
|
export * from "./ctr.js";
|
|
4
5
|
export * from "./ecb.js";
|
|
5
6
|
export * from "./gcm.js";
|
|
7
|
+
export * from "./gmac.js";
|
|
6
8
|
export * from "./kw.js";
|
|
7
|
-
export * from "./mac.js";
|
|
8
9
|
export * from "./ofb.js";
|
|
10
|
+
export * from "./xts.js";
|
package/modes/index.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
export * from "./cbc.js";
|
|
2
2
|
export * from "./cfb.js";
|
|
3
|
+
export * from "./cmac.js";
|
|
3
4
|
export * from "./ctr.js";
|
|
4
5
|
export * from "./ecb.js";
|
|
5
6
|
export * from "./gcm.js";
|
|
7
|
+
export * from "./gmac.js";
|
|
6
8
|
export * from "./kw.js";
|
|
7
|
-
export * from "./mac.js";
|
|
8
9
|
export * from "./ofb.js";
|
|
10
|
+
export * from "./xts.js";
|
package/modes/kw.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type {
|
|
3
|
-
|
|
1
|
+
import { type TArg, type TRet } from "@noble/hashes/utils.js";
|
|
2
|
+
import type { Cipher } from "../types.js";
|
|
3
|
+
/** Key wrapping (KW) */
|
|
4
|
+
export declare const kw: (cipher: Cipher) => {
|
|
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,81 @@
|
|
|
1
|
+
import { concatBytes } from "@noble/hashes/utils.js";
|
|
1
2
|
import { pad, unpad } from "../padding.js";
|
|
3
|
+
import { assertKalyna } from "../utils.js";
|
|
4
|
+
/** Key wrapping (KW) */
|
|
2
5
|
export const kw = (cipher) => {
|
|
3
|
-
|
|
6
|
+
assertKalyna(cipher);
|
|
7
|
+
const half = cipher.blockSize >> 1;
|
|
4
8
|
return Object.freeze({
|
|
5
9
|
wrap: (key) => {
|
|
6
|
-
let
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
let plaintext;
|
|
11
|
+
if (key.length % cipher.blockSize === 0)
|
|
12
|
+
plaintext = key;
|
|
13
|
+
else {
|
|
14
|
+
const withLength = new Uint8Array(key.length + half);
|
|
15
|
+
withLength.set(key);
|
|
16
|
+
let bitLength = key.length << 3;
|
|
17
|
+
let i = 0;
|
|
12
18
|
while (bitLength > 0) {
|
|
13
|
-
|
|
19
|
+
withLength[key.length + i] = bitLength & 0xff;
|
|
14
20
|
i++;
|
|
15
21
|
bitLength >>>= 8;
|
|
16
22
|
}
|
|
17
|
-
|
|
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;
|
|
23
|
+
plaintext = pad(withLength, cipher.blockSize);
|
|
21
24
|
}
|
|
22
|
-
const r =
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
B.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
25
|
+
const r = plaintext.length / cipher.blockSize;
|
|
26
|
+
const n = 2 * (r + 1);
|
|
27
|
+
const v = (n - 1) * 6;
|
|
28
|
+
const qLen = (n - 1) * half;
|
|
29
|
+
const B = plaintext.slice(0, half);
|
|
30
|
+
const q = new Uint8Array(qLen);
|
|
31
|
+
q.set(plaintext.subarray(half));
|
|
32
|
+
const block = new Uint8Array(cipher.blockSize);
|
|
33
|
+
for (let i = 1; i <= v; i++) {
|
|
34
|
+
block.set(B);
|
|
35
|
+
block.set(q.subarray(0, half), half);
|
|
36
|
+
const encrypted = cipher.encrypt(block);
|
|
37
|
+
q.copyWithin(0, half);
|
|
38
|
+
q.set(encrypted.subarray(0, half), qLen - half);
|
|
39
|
+
B.set(encrypted.subarray(half));
|
|
40
|
+
B[0] ^= i & 0xff;
|
|
41
|
+
B[1] ^= (i >>> 8) & 0xff;
|
|
42
|
+
B[2] ^= (i >>> 16) & 0xff;
|
|
43
|
+
B[3] ^= (i >>> 24) & 0xff;
|
|
37
44
|
}
|
|
38
|
-
|
|
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);
|
|
45
|
+
return concatBytes(B, q);
|
|
41
46
|
},
|
|
42
47
|
unwrap: (wrappedKey) => {
|
|
43
|
-
|
|
44
|
-
if (cipher_data.length < 2 * cipher.blockSize)
|
|
48
|
+
if (wrappedKey.length < 2 * cipher.blockSize)
|
|
45
49
|
throw new Error("Invalid input length: must be at least 2 blocks");
|
|
46
|
-
const r =
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const
|
|
50
|
-
B.
|
|
51
|
-
const
|
|
52
|
-
const
|
|
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);
|
|
50
|
+
const r = wrappedKey.length / cipher.blockSize - 1;
|
|
51
|
+
const n = 2 * (r + 1);
|
|
52
|
+
const v = (n - 1) * 6;
|
|
53
|
+
const qLen = (n - 1) * half;
|
|
54
|
+
const B = wrappedKey.slice(0, half);
|
|
55
|
+
const q = wrappedKey.slice(half, half + qLen);
|
|
56
|
+
const block = new Uint8Array(cipher.blockSize);
|
|
56
57
|
for (let i = v; i >= 1; i--) {
|
|
57
|
-
|
|
58
|
-
B
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
58
|
+
block.set(q.subarray(qLen - half));
|
|
59
|
+
block.set(B, half);
|
|
60
|
+
block[half] ^= i & 0xff;
|
|
61
|
+
block[half + 1] ^= (i >>> 8) & 0xff;
|
|
62
|
+
block[half + 2] ^= (i >>> 16) & 0xff;
|
|
63
|
+
block[half + 3] ^= (i >>> 24) & 0xff;
|
|
64
|
+
const decrypted = cipher.decrypt(block);
|
|
65
|
+
q.copyWithin(half, 0, qLen - half);
|
|
66
|
+
q.set(decrypted.subarray(half));
|
|
67
|
+
B.set(decrypted.subarray(0, half));
|
|
65
68
|
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
+
let mismatch = 0;
|
|
70
|
+
for (let k = qLen - cipher.blockSize; k < qLen; k++)
|
|
71
|
+
mismatch |= q[k];
|
|
72
|
+
if (mismatch != 0)
|
|
73
|
+
throw new Error("KW checksum mismatch");
|
|
74
|
+
const recovered = concatBytes(B, q);
|
|
75
|
+
let current_length = unpad(recovered, cipher.blockSize).length;
|
|
69
76
|
if (current_length % cipher.blockSize !== 0)
|
|
70
|
-
current_length -=
|
|
71
|
-
return
|
|
72
|
-
}
|
|
77
|
+
current_length -= half + 1;
|
|
78
|
+
return recovered.slice(0, current_length);
|
|
79
|
+
},
|
|
73
80
|
});
|
|
74
81
|
};
|
package/modes/ofb.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { type TArg } from "@noble/hashes/utils.js";
|
|
2
|
-
import type {
|
|
3
|
-
import type { StreamMode } from "../types.js";
|
|
2
|
+
import type { Cipher, StreamMode } from "../types.js";
|
|
4
3
|
/** Output Feedback (OFB) mode */
|
|
5
|
-
export declare const ofb: (cipher:
|
|
4
|
+
export declare const ofb: (cipher: Cipher, iv: TArg<Uint8Array>) => StreamMode;
|
package/modes/ofb.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import {} from "@noble/hashes/utils.js";
|
|
2
|
-
import { xorBytes } from "../utils.js";
|
|
2
|
+
import { assertKalyna, xorBytes } from "../utils.js";
|
|
3
3
|
/** Output Feedback (OFB) mode */
|
|
4
4
|
export const ofb = (cipher, iv) => {
|
|
5
|
+
assertKalyna(cipher);
|
|
5
6
|
if (iv.length !== cipher.blockSize)
|
|
6
7
|
throw new Error("Invalid IV size");
|
|
7
8
|
return Object.freeze({
|
|
8
9
|
crypt: (msg) => {
|
|
9
|
-
let buf =
|
|
10
|
+
let buf = iv;
|
|
10
11
|
const output = new Uint8Array(msg.length);
|
|
11
12
|
for (let i = 0; i < msg.length; i += cipher.blockSize) {
|
|
12
13
|
const enc = cipher.encrypt(buf);
|
package/modes/xts.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type TArg, type TRet } from "@noble/hashes/utils.js";
|
|
2
|
+
import type { Cipher } from "../types.js";
|
|
3
|
+
/** XEX Tweakable Block Ciphertext Stealing (XTS) */
|
|
4
|
+
export declare const xts: (cipher: Cipher) => {
|
|
5
|
+
encrypt: (plaintext: TArg<Uint8Array>, tweak: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
6
|
+
decrypt: (ciphertext: TArg<Uint8Array>, iv: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
7
|
+
};
|