@li0ard/dstu 0.1.9 → 0.2.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/README.md +1 -1
- package/dstu4145/ec/index.d.ts +4 -0
- package/dstu4145/ec/index.js +19 -0
- package/dstu4145/ecies.js +11 -1
- package/dstu4145/index.js +14 -5
- package/dstu9041/curve.js +2 -2
- package/dstu9041/index.js +9 -5
- package/dstu9311/hash.d.ts +46 -12
- package/dstu9311/hash.js +89 -28
- package/dstu9311/index.js +4 -5
- package/iso15946.d.ts +1 -1
- package/iso15946.js +10 -3
- package/kalyna/index.d.ts +1 -1
- package/kalyna/index.js +6 -6
- package/keywrap.js +6 -3
- package/kupyna/index.d.ts +116 -67
- package/kupyna/index.js +122 -61
- package/kupyna/kmac.d.ts +6 -6
- package/kupyna/kmac.js +20 -15
- package/modes/cbc.d.ts +1 -1
- package/modes/cbc.js +5 -7
- package/modes/ccm.js +12 -3
- package/modes/cfb.js +42 -51
- package/modes/cmac.js +4 -1
- package/modes/ctr.js +4 -3
- package/modes/ecb.js +2 -2
- package/modes/gcm.d.ts +4 -1
- package/modes/gcm.js +32 -5
- package/modes/gmac.js +4 -1
- package/modes/index.d.ts +1 -1
- package/modes/index.js +1 -1
- package/modes/kw.js +9 -16
- package/modes/ofb.js +3 -3
- package/modes/xts.js +5 -1
- package/package.json +1 -1
- package/padding.d.ts +1 -1
- package/padding.js +5 -0
- package/strumok/index.d.ts +1 -1
- package/strumok/index.js +4 -2
- package/utils.d.ts +1 -0
- package/utils.js +14 -1
package/modes/cbc.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type TArg } from "@noble/hashes/utils.js";
|
|
2
2
|
import type { BlockMode, Cipher } from "../types.js";
|
|
3
3
|
/** Cipher Block Chaining (CBC) mode */
|
|
4
4
|
export declare const cbc: (cipher: Cipher, iv: TArg<Uint8Array>) => BlockMode;
|
package/modes/cbc.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { abytes } from "@noble/hashes/utils.js";
|
|
2
|
+
import { abytesAligned, assertKalyna, xorBytes } from "../utils.js";
|
|
2
3
|
/** Cipher Block Chaining (CBC) mode */
|
|
3
4
|
export const cbc = (cipher, iv) => {
|
|
4
5
|
assertKalyna(cipher);
|
|
5
|
-
|
|
6
|
-
throw new Error("Invalid IV size");
|
|
6
|
+
abytes(iv, cipher.blockSize, "iv");
|
|
7
7
|
return Object.freeze({
|
|
8
8
|
encrypt: (plaintext) => {
|
|
9
|
-
|
|
10
|
-
throw new Error("Plaintext not aligned");
|
|
9
|
+
abytesAligned(plaintext, cipher.blockSize, "plaintext");
|
|
11
10
|
let buf = iv;
|
|
12
11
|
const output = new Uint8Array(plaintext.length);
|
|
13
12
|
for (let i = 0; i < plaintext.length; i += cipher.blockSize) {
|
|
@@ -18,8 +17,7 @@ export const cbc = (cipher, iv) => {
|
|
|
18
17
|
return output;
|
|
19
18
|
},
|
|
20
19
|
decrypt: (ciphertext) => {
|
|
21
|
-
|
|
22
|
-
throw new Error("Ciphertext not aligned");
|
|
20
|
+
abytesAligned(ciphertext, cipher.blockSize, "ciphertext");
|
|
23
21
|
let buf = iv;
|
|
24
22
|
const output = new Uint8Array(ciphertext.length);
|
|
25
23
|
for (let i = 0; i < ciphertext.length; i += cipher.blockSize) {
|
package/modes/ccm.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { equalBytes, concatBytes } from "@noble/curves/utils.js";
|
|
1
|
+
import { abytes, anumber, equalBytes, concatBytes } from "@noble/curves/utils.js";
|
|
2
2
|
import { assertKalyna } from "../utils.js";
|
|
3
3
|
import { pad } from "../padding.js";
|
|
4
|
-
import {
|
|
4
|
+
import { half_ctr } from "./gcm.js";
|
|
5
5
|
/** Counter with CBC-MAC (CCM) mode (AEAD) */
|
|
6
6
|
export const ccm = (cipher, iv, q = 16, Nb = 4) => {
|
|
7
7
|
assertKalyna(cipher);
|
|
@@ -9,7 +9,10 @@ export const ccm = (cipher, iv, q = 16, Nb = 4) => {
|
|
|
9
9
|
throw new Error('q must be 8, 16, 32, 48, or 64');
|
|
10
10
|
if (cipher.blockSize < Nb + 1)
|
|
11
11
|
throw new Error('Cipher block size must be >= Nb + 1');
|
|
12
|
+
abytes(iv, cipher.blockSize, "iv");
|
|
12
13
|
const tmp = cipher.blockSize - 1, tmp2 = tmp - Nb;
|
|
14
|
+
anumber(q, "q");
|
|
15
|
+
anumber(Nb, "Nb");
|
|
13
16
|
const calculateMac = (msg, aad) => {
|
|
14
17
|
aad ??= new Uint8Array();
|
|
15
18
|
const G1 = new Uint8Array(cipher.blockSize), G2 = new Uint8Array(cipher.blockSize), B = new Uint8Array(cipher.blockSize);
|
|
@@ -29,13 +32,19 @@ export const ccm = (cipher, iv, q = 16, Nb = 4) => {
|
|
|
29
32
|
updateMac(pad(msg, cipher.blockSize));
|
|
30
33
|
return B.slice(0, q);
|
|
31
34
|
};
|
|
32
|
-
const mode =
|
|
35
|
+
const mode = half_ctr(cipher, iv);
|
|
33
36
|
return Object.freeze({
|
|
34
37
|
seal: (plaintext, aad) => {
|
|
38
|
+
abytes(plaintext, undefined, "plaintext");
|
|
39
|
+
if (aad)
|
|
40
|
+
abytes(aad, undefined, "aad");
|
|
35
41
|
const mac = calculateMac(plaintext, aad);
|
|
36
42
|
return mode.crypt(concatBytes(plaintext, mac));
|
|
37
43
|
},
|
|
38
44
|
open: (ciphertext, aad) => {
|
|
45
|
+
abytes(ciphertext, undefined, "ciphertext");
|
|
46
|
+
if (aad)
|
|
47
|
+
abytes(aad, undefined, "aad");
|
|
39
48
|
const decrypted = mode.crypt(ciphertext);
|
|
40
49
|
const plaintext = decrypted.slice(0, -q);
|
|
41
50
|
const hC = calculateMac(plaintext, aad);
|
package/modes/cfb.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { isKalyna, xorBytes } from "../utils.js";
|
|
1
|
+
import { abytes, anumber, copyBytes } from "@noble/hashes/utils.js";
|
|
2
|
+
import { abytesAligned, isKalyna, xorBytes } from "../utils.js";
|
|
3
3
|
/** Cipher Feedback (CFB) mode */
|
|
4
4
|
export const cfb = (cipher, iv, q = cipher.blockSize) => {
|
|
5
5
|
const _isKalyna = isKalyna(cipher);
|
|
@@ -7,61 +7,47 @@ export const cfb = (cipher, iv, q = cipher.blockSize) => {
|
|
|
7
7
|
throw new Error('q must be 1, 8, 16, 32, or 64');
|
|
8
8
|
if (_isKalyna && q > cipher.blockSize)
|
|
9
9
|
throw new Error('q cannot exceed block size');
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
abytesAligned(iv, cipher.blockSize, "iv");
|
|
11
|
+
anumber(q, "q");
|
|
12
12
|
if (_isKalyna)
|
|
13
13
|
return Object.freeze({
|
|
14
14
|
encrypt: (plaintext) => {
|
|
15
|
+
abytes(plaintext, undefined, "plaintext");
|
|
15
16
|
const gamma = cipher.encrypt(iv);
|
|
16
17
|
const feed = copyBytes(iv);
|
|
17
18
|
const result = new Uint8Array(plaintext.length);
|
|
18
|
-
let offset = 0
|
|
19
|
-
while (offset
|
|
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
|
-
}
|
|
26
|
-
}
|
|
27
|
-
while (dataOff + q <= plaintext.length) {
|
|
19
|
+
let offset = 0;
|
|
20
|
+
while (offset + q <= plaintext.length) {
|
|
28
21
|
for (let i = 0; i < q; i++)
|
|
29
|
-
result[
|
|
22
|
+
result[offset + i] = plaintext[offset + i] ^ gamma[cipher.blockSize - q + i];
|
|
30
23
|
feed.set(gamma.subarray(0, cipher.blockSize - q));
|
|
31
|
-
feed.set(result.subarray(
|
|
24
|
+
feed.set(result.subarray(offset, offset + q), cipher.blockSize - q);
|
|
32
25
|
gamma.set(cipher.encrypt(feed));
|
|
33
|
-
|
|
26
|
+
offset += q;
|
|
34
27
|
}
|
|
35
|
-
while (
|
|
36
|
-
result[
|
|
37
|
-
|
|
28
|
+
while (offset < plaintext.length) {
|
|
29
|
+
result[offset] = plaintext[offset] ^ gamma[cipher.blockSize - (plaintext.length - offset)];
|
|
30
|
+
offset++;
|
|
38
31
|
}
|
|
39
32
|
return result;
|
|
40
33
|
},
|
|
41
34
|
decrypt: (ciphertext) => {
|
|
35
|
+
abytes(ciphertext, undefined, "ciphertext");
|
|
42
36
|
const gamma = cipher.encrypt(iv);
|
|
43
37
|
const feed = copyBytes(iv);
|
|
44
38
|
const result = new Uint8Array(ciphertext.length);
|
|
45
|
-
let offset = 0
|
|
46
|
-
while (offset
|
|
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) {
|
|
39
|
+
let offset = 0;
|
|
40
|
+
while (offset + q <= ciphertext.length) {
|
|
55
41
|
for (let i = 0; i < q; i++)
|
|
56
|
-
result[
|
|
42
|
+
result[offset + i] = ciphertext[offset + i] ^ gamma[cipher.blockSize - q + i];
|
|
57
43
|
feed.set(gamma.subarray(0, cipher.blockSize - q));
|
|
58
|
-
feed.set(ciphertext.subarray(
|
|
44
|
+
feed.set(ciphertext.subarray(offset, offset + q), cipher.blockSize - q);
|
|
59
45
|
gamma.set(cipher.encrypt(feed));
|
|
60
|
-
|
|
46
|
+
offset += q;
|
|
61
47
|
}
|
|
62
|
-
while (
|
|
63
|
-
result[
|
|
64
|
-
|
|
48
|
+
while (offset < ciphertext.length) {
|
|
49
|
+
result[offset] = ciphertext[offset] ^ gamma[cipher.blockSize - (ciphertext.length - offset)];
|
|
50
|
+
offset++;
|
|
65
51
|
}
|
|
66
52
|
return result;
|
|
67
53
|
}
|
|
@@ -69,29 +55,34 @@ export const cfb = (cipher, iv, q = cipher.blockSize) => {
|
|
|
69
55
|
else
|
|
70
56
|
return Object.freeze({
|
|
71
57
|
encrypt: (plaintext) => {
|
|
72
|
-
|
|
58
|
+
abytes(plaintext, undefined, "plaintext");
|
|
59
|
+
const r = [];
|
|
73
60
|
for (let i = 0; i < iv.length; i += cipher.blockSize)
|
|
74
61
|
r.push(iv.subarray(i, i + cipher.blockSize));
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
62
|
+
const out = new Uint8Array(plaintext.length);
|
|
63
|
+
for (let n = 0; n < Math.ceil(plaintext.length / cipher.blockSize); n++) {
|
|
64
|
+
const i = n * cipher.blockSize;
|
|
65
|
+
const ct = xorBytes(plaintext.subarray(i, i + cipher.blockSize), cipher.encrypt(r[0]));
|
|
66
|
+
out.set(ct, i);
|
|
67
|
+
r.shift();
|
|
68
|
+
r.push(ct);
|
|
80
69
|
}
|
|
81
|
-
return
|
|
70
|
+
return out;
|
|
82
71
|
},
|
|
83
72
|
decrypt: (ciphertext) => {
|
|
84
|
-
|
|
73
|
+
abytes(ciphertext, undefined, "ciphertext");
|
|
74
|
+
const r = [];
|
|
85
75
|
for (let i = 0; i < iv.length; i += cipher.blockSize)
|
|
86
76
|
r.push(iv.subarray(i, i + cipher.blockSize));
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const blk = ciphertext.
|
|
91
|
-
|
|
92
|
-
r
|
|
77
|
+
const out = new Uint8Array(ciphertext.length);
|
|
78
|
+
for (let n = 0; n < Math.ceil(ciphertext.length / cipher.blockSize); n++) {
|
|
79
|
+
const i = n * cipher.blockSize;
|
|
80
|
+
const blk = ciphertext.subarray(i, i + cipher.blockSize);
|
|
81
|
+
out.set(xorBytes(blk, cipher.encrypt(r[0])), i);
|
|
82
|
+
r.shift();
|
|
83
|
+
r.push(blk);
|
|
93
84
|
}
|
|
94
|
-
return
|
|
85
|
+
return out;
|
|
95
86
|
}
|
|
96
87
|
});
|
|
97
88
|
};
|
package/modes/cmac.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import { concatBytes, bytesToNumberLE, numberToBytesLE } from "@noble/curves/utils.js";
|
|
1
|
+
import { anumber, concatBytes, bytesToNumberLE, numberToBytesLE, abytes } from "@noble/curves/utils.js";
|
|
2
2
|
import { pad } from "../padding.js";
|
|
3
3
|
import { isKalyna, xorBytes } from "../utils.js";
|
|
4
4
|
import { keySequences } from "../dstu9311/const.js";
|
|
5
5
|
export const cmac = (cipher, q = 16) => {
|
|
6
|
+
anumber(q, "q");
|
|
6
7
|
if (isKalyna(cipher))
|
|
7
8
|
return Object.freeze({
|
|
8
9
|
compute: (msg) => {
|
|
10
|
+
abytes(msg, undefined, "msg");
|
|
9
11
|
let data = msg;
|
|
10
12
|
const zeroBlock = new Uint8Array(cipher.blockSize);
|
|
11
13
|
if (data.length % cipher.blockSize !== 0) {
|
|
@@ -27,6 +29,7 @@ export const cmac = (cipher, q = 16) => {
|
|
|
27
29
|
else
|
|
28
30
|
return Object.freeze({
|
|
29
31
|
compute: (msg) => {
|
|
32
|
+
abytes(msg, undefined, "msg");
|
|
30
33
|
const paddedLength = Math.max(8, (msg.length + 7) & ~7);
|
|
31
34
|
const paddedData = new Uint8Array(paddedLength);
|
|
32
35
|
paddedData.set(msg);
|
package/modes/ctr.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { createView } from "@noble/hashes/utils.js";
|
|
1
|
+
import { abytes, createView } from "@noble/hashes/utils.js";
|
|
2
2
|
import { isKalyna, xorBytes } from "../utils.js";
|
|
3
3
|
const C1 = 0x01010104, C2 = 0x01010101;
|
|
4
4
|
/** Counter (CTR) mode */
|
|
5
5
|
export const ctr = (cipher, iv) => {
|
|
6
|
-
|
|
7
|
-
throw new Error("Invalid IV size");
|
|
6
|
+
abytes(iv, cipher.blockSize, "iv");
|
|
8
7
|
const incrementCounter = (ctr) => {
|
|
8
|
+
abytes(ctr);
|
|
9
9
|
if (isKalyna(cipher)) {
|
|
10
10
|
let j = 0;
|
|
11
11
|
while (j < ctr.length)
|
|
@@ -23,6 +23,7 @@ export const ctr = (cipher, iv) => {
|
|
|
23
23
|
};
|
|
24
24
|
return Object.freeze({
|
|
25
25
|
crypt: (msg) => {
|
|
26
|
+
abytes(msg, undefined, "msg");
|
|
26
27
|
const buf = cipher.encrypt(iv), output = new Uint8Array(msg.length);
|
|
27
28
|
for (let i = 0; i < msg.length; i += cipher.blockSize) {
|
|
28
29
|
incrementCounter(buf);
|
package/modes/ecb.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
+
import { abytesAligned } from "../utils.js";
|
|
1
2
|
/** Electronic Codebook (ECB) mode */
|
|
2
3
|
export const ecb = (cipher) => {
|
|
3
4
|
const encrypter = cipher.encrypt.bind(cipher);
|
|
4
5
|
const decrypter = cipher.decrypt.bind(cipher);
|
|
5
6
|
const core = (crypter, data) => {
|
|
6
|
-
|
|
7
|
-
throw new Error("Data not aligned");
|
|
7
|
+
abytesAligned(data, cipher.blockSize, "data");
|
|
8
8
|
const output = new Uint8Array(data.length);
|
|
9
9
|
for (let i = 0; i < data.length; i += cipher.blockSize)
|
|
10
10
|
output.set(crypter(data.subarray(i, i + cipher.blockSize)), i);
|
package/modes/gcm.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import { type TArg } from "@noble/curves/utils.js";
|
|
1
|
+
import { type TArg, type TRet } from "@noble/curves/utils.js";
|
|
2
2
|
import type { AEADMode, Cipher } from "../types.js";
|
|
3
|
+
export declare const half_ctr: (cipher: Cipher, iv: TArg<Uint8Array>) => Readonly<{
|
|
4
|
+
crypt: (msg: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
5
|
+
}>;
|
|
3
6
|
/** Galois counter (GCM) mode (AEAD) */
|
|
4
7
|
export declare const gcm: (cipher: Cipher, iv: TArg<Uint8Array>, q?: number) => AEADMode;
|
package/modes/gcm.js
CHANGED
|
@@ -1,21 +1,48 @@
|
|
|
1
|
-
import { equalBytes, concatBytes } from "@noble/curves/utils.js";
|
|
2
|
-
import { ctr } from "./ctr.js";
|
|
1
|
+
import { abytes, anumber, equalBytes, concatBytes } from "@noble/curves/utils.js";
|
|
3
2
|
import { gmac } from "./gmac.js";
|
|
4
|
-
import { assertKalyna } from "../utils.js";
|
|
3
|
+
import { assertKalyna, xorBytes } from "../utils.js";
|
|
4
|
+
// CTR for GCM and CCM
|
|
5
|
+
export const half_ctr = (cipher, iv) => {
|
|
6
|
+
abytes(iv, cipher.blockSize, "iv");
|
|
7
|
+
return Object.freeze({
|
|
8
|
+
crypt: (msg) => {
|
|
9
|
+
abytes(msg, undefined, "msg");
|
|
10
|
+
const buf = cipher.encrypt(iv), output = new Uint8Array(msg.length), half_len_word = cipher.blockSize >> 1;
|
|
11
|
+
for (let i = 0; i < msg.length; i += cipher.blockSize) {
|
|
12
|
+
for (let j = 0; j < half_len_word; j++) {
|
|
13
|
+
if (++buf[j] != 0)
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
const ct = xorBytes(cipher.encrypt(buf), msg.subarray(i, i + cipher.blockSize));
|
|
17
|
+
output.set(ct, i);
|
|
18
|
+
}
|
|
19
|
+
return output;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
};
|
|
5
23
|
/** Galois counter (GCM) mode (AEAD) */
|
|
6
24
|
export const gcm = (cipher, iv, q = 16) => {
|
|
7
25
|
assertKalyna(cipher);
|
|
26
|
+
abytes(iv, cipher.blockSize, "iv");
|
|
27
|
+
anumber(q, "q");
|
|
28
|
+
const mode = half_ctr(cipher, iv);
|
|
8
29
|
return Object.freeze({
|
|
9
30
|
seal: (plaintext, aad) => {
|
|
10
|
-
|
|
31
|
+
abytes(plaintext, undefined, "plaintext");
|
|
32
|
+
if (aad)
|
|
33
|
+
abytes(aad, undefined, "aad");
|
|
34
|
+
const enc = mode.crypt(plaintext);
|
|
11
35
|
return concatBytes(enc, gmac(cipher, q).compute(enc, aad));
|
|
12
36
|
},
|
|
13
37
|
open: (ciphertext, aad) => {
|
|
38
|
+
abytes(ciphertext, undefined, "ciphertext");
|
|
39
|
+
if (aad)
|
|
40
|
+
abytes(aad, undefined, "aad");
|
|
14
41
|
const enc = ciphertext.subarray(0, -q);
|
|
15
42
|
const hC = gmac(cipher, q).compute(enc, aad);
|
|
16
43
|
if (!equalBytes(ciphertext.subarray(-q), hC))
|
|
17
44
|
throw new Error("Invalid MAC");
|
|
18
|
-
return
|
|
45
|
+
return mode.crypt(enc);
|
|
19
46
|
}
|
|
20
47
|
});
|
|
21
48
|
};
|
package/modes/gmac.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { numberToBytesLE, concatBytes } from "@noble/curves/utils.js";
|
|
1
|
+
import { abytes, anumber, numberToBytesLE, concatBytes } from "@noble/curves/utils.js";
|
|
2
2
|
import { assertKalyna, gf2mMul } from "../utils.js";
|
|
3
3
|
const xorBytesInPlace = (a, b) => {
|
|
4
4
|
if (a.length != b.length)
|
|
@@ -9,10 +9,13 @@ const xorBytesInPlace = (a, b) => {
|
|
|
9
9
|
/** Galois Message Authentication Code (GMAC) mode */
|
|
10
10
|
export const gmac = (cipher, q = 16) => {
|
|
11
11
|
assertKalyna(cipher);
|
|
12
|
+
anumber(q, "q");
|
|
12
13
|
const hblock = cipher.blockSize >> 1;
|
|
13
14
|
return Object.freeze({
|
|
14
15
|
compute: (msg, aad) => {
|
|
15
16
|
aad ??= new Uint8Array();
|
|
17
|
+
abytes(msg, undefined, "msg");
|
|
18
|
+
abytes(aad, undefined, "aad");
|
|
16
19
|
const H = cipher.encrypt(new Uint8Array(cipher.blockSize));
|
|
17
20
|
const B = new Uint8Array(cipher.blockSize);
|
|
18
21
|
const updateMac = (input) => {
|
package/modes/index.d.ts
CHANGED
package/modes/index.js
CHANGED
package/modes/kw.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { concatBytes } from "@noble/hashes/utils.js";
|
|
1
|
+
import { abytes, concatBytes } from "@noble/hashes/utils.js";
|
|
2
2
|
import { pad, unpad } from "../padding.js";
|
|
3
3
|
import { assertKalyna } from "../utils.js";
|
|
4
4
|
/** Key wrapping (KW) */
|
|
@@ -7,14 +7,14 @@ export const kw = (cipher) => {
|
|
|
7
7
|
const half = cipher.blockSize >> 1;
|
|
8
8
|
return Object.freeze({
|
|
9
9
|
wrap: (key) => {
|
|
10
|
+
abytes(key, undefined, "key");
|
|
10
11
|
let plaintext;
|
|
11
12
|
if (key.length % cipher.blockSize === 0)
|
|
12
13
|
plaintext = key;
|
|
13
14
|
else {
|
|
14
15
|
const withLength = new Uint8Array(key.length + half);
|
|
15
16
|
withLength.set(key);
|
|
16
|
-
let bitLength = key.length << 3;
|
|
17
|
-
let i = 0;
|
|
17
|
+
let bitLength = key.length << 3, i = 0;
|
|
18
18
|
while (bitLength > 0) {
|
|
19
19
|
withLength[key.length + i] = bitLength & 0xff;
|
|
20
20
|
i++;
|
|
@@ -22,12 +22,8 @@ export const kw = (cipher) => {
|
|
|
22
22
|
}
|
|
23
23
|
plaintext = pad(withLength, cipher.blockSize);
|
|
24
24
|
}
|
|
25
|
-
const r = plaintext.length / cipher.blockSize;
|
|
26
|
-
const
|
|
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);
|
|
25
|
+
const r = plaintext.length / cipher.blockSize, n = 2 * (r + 1), v = (n - 1) * 6, qLen = (n - 1) * half;
|
|
26
|
+
const B = plaintext.slice(0, half), q = new Uint8Array(qLen);
|
|
31
27
|
q.set(plaintext.subarray(half));
|
|
32
28
|
const block = new Uint8Array(cipher.blockSize);
|
|
33
29
|
for (let i = 1; i <= v; i++) {
|
|
@@ -45,14 +41,11 @@ export const kw = (cipher) => {
|
|
|
45
41
|
return concatBytes(B, q);
|
|
46
42
|
},
|
|
47
43
|
unwrap: (wrappedKey) => {
|
|
44
|
+
abytes(wrappedKey, undefined, "wrappedKey");
|
|
48
45
|
if (wrappedKey.length < 2 * cipher.blockSize)
|
|
49
46
|
throw new Error("Invalid input length: must be at least 2 blocks");
|
|
50
|
-
const r = wrappedKey.length / cipher.blockSize - 1;
|
|
51
|
-
const
|
|
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);
|
|
47
|
+
const r = wrappedKey.length / cipher.blockSize - 1, n = 2 * (r + 1), v = (n - 1) * 6, qLen = (n - 1) * half;
|
|
48
|
+
const B = wrappedKey.slice(0, half), q = wrappedKey.slice(half, half + qLen);
|
|
56
49
|
const block = new Uint8Array(cipher.blockSize);
|
|
57
50
|
for (let i = v; i >= 1; i--) {
|
|
58
51
|
block.set(q.subarray(qLen - half));
|
|
@@ -76,6 +69,6 @@ export const kw = (cipher) => {
|
|
|
76
69
|
if (current_length % cipher.blockSize !== 0)
|
|
77
70
|
current_length -= half + 1;
|
|
78
71
|
return recovered.slice(0, current_length);
|
|
79
|
-
}
|
|
72
|
+
}
|
|
80
73
|
});
|
|
81
74
|
};
|
package/modes/ofb.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import {} from "@noble/hashes/utils.js";
|
|
1
|
+
import { abytes } from "@noble/hashes/utils.js";
|
|
2
2
|
import { assertKalyna, xorBytes } from "../utils.js";
|
|
3
3
|
/** Output Feedback (OFB) mode */
|
|
4
4
|
export const ofb = (cipher, iv) => {
|
|
5
5
|
assertKalyna(cipher);
|
|
6
|
-
|
|
7
|
-
throw new Error("Invalid IV size");
|
|
6
|
+
abytes(iv, cipher.blockSize, "iv");
|
|
8
7
|
return Object.freeze({
|
|
9
8
|
crypt: (msg) => {
|
|
9
|
+
abytes(msg, undefined, "msg");
|
|
10
10
|
let buf = iv;
|
|
11
11
|
const output = new Uint8Array(msg.length);
|
|
12
12
|
for (let i = 0; i < msg.length; i += cipher.blockSize) {
|
package/modes/xts.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { concatBytes, copyBytes } from "@noble/hashes/utils.js";
|
|
1
|
+
import { abytes, concatBytes, copyBytes } from "@noble/hashes/utils.js";
|
|
2
2
|
import { assertKalyna, getGf2mReductionBytes, xorBytes } from "../utils.js";
|
|
3
3
|
/** XEX Tweakable Block Ciphertext Stealing (XTS) */
|
|
4
4
|
export const xts = (cipher) => {
|
|
@@ -28,6 +28,8 @@ export const xts = (cipher) => {
|
|
|
28
28
|
};
|
|
29
29
|
return Object.freeze({
|
|
30
30
|
encrypt: (plaintext, tweak) => {
|
|
31
|
+
abytes(plaintext, undefined, "plaintext");
|
|
32
|
+
abytes(tweak, undefined, "tweak");
|
|
31
33
|
if (plaintext.length < cipher.blockSize)
|
|
32
34
|
throw new Error(`Invalid length (need at least ${cipher.blockSize}, got ${plaintext.length})`);
|
|
33
35
|
const gamma = init(tweak);
|
|
@@ -58,6 +60,8 @@ export const xts = (cipher) => {
|
|
|
58
60
|
return buffer;
|
|
59
61
|
},
|
|
60
62
|
decrypt: (ciphertext, tweak) => {
|
|
63
|
+
abytes(ciphertext, undefined, "ciphertext");
|
|
64
|
+
abytes(tweak, undefined, "tweak");
|
|
61
65
|
if (ciphertext.length < cipher.blockSize)
|
|
62
66
|
throw new Error(`Invalid length (need at least ${cipher.blockSize}, got ${ciphertext.length})`);
|
|
63
67
|
const gamma = init(tweak);
|
package/package.json
CHANGED
package/padding.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type TArg, type TRet } from "@noble/hashes/utils.js";
|
|
2
2
|
export declare const pad: (data: TArg<Uint8Array>, blockSize: number) => TRet<Uint8Array>;
|
|
3
3
|
export declare const unpad: (paddedData: TArg<Uint8Array>, blockSize: number) => TRet<Uint8Array>;
|
package/padding.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { abytes, anumber } from "@noble/hashes/utils.js";
|
|
1
2
|
export const pad = (data, blockSize) => {
|
|
3
|
+
abytes(data, undefined, "data");
|
|
4
|
+
anumber(blockSize, "blockSize");
|
|
2
5
|
const remainder = data.length % blockSize;
|
|
3
6
|
if (remainder === 0)
|
|
4
7
|
return data;
|
|
@@ -9,6 +12,8 @@ export const pad = (data, blockSize) => {
|
|
|
9
12
|
return padded;
|
|
10
13
|
};
|
|
11
14
|
export const unpad = (paddedData, blockSize) => {
|
|
15
|
+
abytes(paddedData, undefined, "paddedData");
|
|
16
|
+
anumber(blockSize, "blockSize");
|
|
12
17
|
if (paddedData.length % blockSize !== 0)
|
|
13
18
|
throw new Error("Padded data length must be multiple of block size");
|
|
14
19
|
let i = paddedData.length - 1;
|
package/strumok/index.d.ts
CHANGED
package/strumok/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { abytes } from "@noble/hashes/utils.js";
|
|
1
2
|
import { ALPHA_MUL, ALPHA_MUL_INV, T as T_ } from "../const.js";
|
|
2
3
|
import { bytesToUint64sBE, byte, xorBytes, uint64sToBytesBE } from "../utils.js";
|
|
3
4
|
const MAX_UINT64 = (1n << 64n) - 1n;
|
|
@@ -26,8 +27,8 @@ export class Strumok {
|
|
|
26
27
|
* @param key Encryption key (256 or 512 bit)
|
|
27
28
|
*/
|
|
28
29
|
constructor(key, iv) {
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
abytes(key, undefined, "key");
|
|
31
|
+
abytes(iv, 32, "iv");
|
|
31
32
|
this.key = bytesToUint64sBE(key);
|
|
32
33
|
this.iv = bytesToUint64sBE(iv);
|
|
33
34
|
this.keySize = key.length;
|
|
@@ -105,6 +106,7 @@ export class Strumok {
|
|
|
105
106
|
}
|
|
106
107
|
/** Perform encryption/decryption */
|
|
107
108
|
crypt(msg) {
|
|
109
|
+
abytes(msg, undefined, "msg");
|
|
108
110
|
const out = new Uint8Array(msg.length);
|
|
109
111
|
let offset = 0;
|
|
110
112
|
while (offset < msg.length) {
|
package/utils.d.ts
CHANGED
|
@@ -12,3 +12,4 @@ export declare const gf2mMul: (blockSize: number, a: TArg<Uint8Array>, b: TArg<U
|
|
|
12
12
|
export declare const isKalyna: (cipher: Cipher) => cipher is Kalyna;
|
|
13
13
|
export declare const assertKalyna: (cipher: Cipher) => void;
|
|
14
14
|
export declare const reverseBytes: (bytes: TArg<Uint8Array>) => TRet<Uint8Array>;
|
|
15
|
+
export declare const abytesAligned: (value: TArg<Uint8Array>, blockSize: number, title?: string) => TRet<Uint8Array>;
|
package/utils.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { copyBytes, bytesToNumberBE, bytesToNumberLE, numberToBytesBE, numberToBytesLE } from "@noble/curves/utils.js";
|
|
1
|
+
import { copyBytes, bytesToNumberBE, bytesToNumberLE, numberToBytesBE, numberToBytesLE, abytes, anumber } from "@noble/curves/utils.js";
|
|
2
2
|
import { Kalyna } from "./kalyna/index.js";
|
|
3
3
|
export const xorBytes = (a, b) => {
|
|
4
|
+
abytes(a);
|
|
5
|
+
abytes(b);
|
|
4
6
|
const mlen = Math.min(a.length, b.length);
|
|
5
7
|
const result = new Uint8Array(mlen);
|
|
6
8
|
for (let i = 0; i < mlen; i++)
|
|
@@ -69,3 +71,14 @@ export const assertKalyna = (cipher) => {
|
|
|
69
71
|
throw new Error("Cipher mode only for Kalyna");
|
|
70
72
|
};
|
|
71
73
|
export const reverseBytes = (bytes) => copyBytes(bytes).reverse();
|
|
74
|
+
const atitle = (title) => title ? `"${title}" ` : '';
|
|
75
|
+
export const abytesAligned = (value, blockSize, title = "") => {
|
|
76
|
+
abytes(value);
|
|
77
|
+
anumber(blockSize);
|
|
78
|
+
if (value.length !== 0 && value.length % blockSize === 0)
|
|
79
|
+
return value;
|
|
80
|
+
const ofLen = ` of length aligned to ${blockSize}`;
|
|
81
|
+
const got = `length=${value.length}`;
|
|
82
|
+
const message = atitle(title) + 'expected Uint8Array' + ofLen + ', got ' + got;
|
|
83
|
+
throw new RangeError(message);
|
|
84
|
+
};
|