@cloudpss/crypto 0.5.25 → 0.5.26
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/benchmark.js +2 -2
- package/dist/encryption/browser.js +1 -2
- package/dist/encryption/browser.js.map +1 -1
- package/dist/encryption/common.js +4 -2
- package/dist/encryption/common.js.map +1 -1
- package/dist/encryption/module.d.ts +1 -1
- package/dist/encryption/module.js.map +1 -1
- package/dist/encryption/{pure-js.d.ts → wasm.d.ts} +2 -2
- package/dist/encryption/wasm.js +21 -0
- package/dist/encryption/wasm.js.map +1 -0
- package/lib/wasm.d.ts +26 -0
- package/lib/wasm.js +149 -0
- package/package.json +10 -12
- package/src/encryption/browser.ts +1 -2
- package/src/encryption/common.ts +6 -2
- package/src/encryption/module.ts +1 -1
- package/src/encryption/wasm.ts +46 -0
- package/tests/encryption.js +48 -29
- package/tsconfig.json +2 -1
- package/wasm-build.js +30 -0
- package/dist/encryption/js/aes.d.ts +0 -20
- package/dist/encryption/js/aes.js +0 -151
- package/dist/encryption/js/aes.js.map +0 -1
- package/dist/encryption/js/gcm.d.ts +0 -26
- package/dist/encryption/js/gcm.js +0 -226
- package/dist/encryption/js/gcm.js.map +0 -1
- package/dist/encryption/pure-js.js +0 -82
- package/dist/encryption/pure-js.js.map +0 -1
- package/src/encryption/js/aes.ts +0 -191
- package/src/encryption/js/gcm.ts +0 -258
- package/src/encryption/pure-js.ts +0 -105
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import sjcl from 'sjcl';
|
|
2
|
-
import { NONCE_SIZE, AES_KEY_SIZE, AES_TAG_SIZE, PBKDF2_ITERATIONS, } from './common.js';
|
|
3
|
-
// Load unminified version for debugging
|
|
4
|
-
// globalThis.sjcl = sjcl;
|
|
5
|
-
// // @ts-expect-error sjcl is not a module
|
|
6
|
-
// await import('sjcl/core/aes.js');
|
|
7
|
-
// // @ts-expect-error sjcl is not a module
|
|
8
|
-
// await import('sjcl/core/gcm.js');
|
|
9
|
-
// // @ts-expect-error sjcl is not a module
|
|
10
|
-
// await import('sjcl/core/bitArray.js');
|
|
11
|
-
// // @ts-expect-error sjcl is not a module
|
|
12
|
-
// await import('sjcl/core/pbkdf2.js');
|
|
13
|
-
// // @ts-expect-error sjcl is not a module
|
|
14
|
-
// await import('sjcl/core/hmac.js');
|
|
15
|
-
// // @ts-expect-error sjcl is not a module
|
|
16
|
-
// await import('sjcl/core/sha256.js');
|
|
17
|
-
/** Convert word array to buffer data */
|
|
18
|
-
function wordArrayToBuffer(bitArray) {
|
|
19
|
-
const len = sjcl.bitArray.bitLength(bitArray) / 8;
|
|
20
|
-
const out = new Uint8Array(len);
|
|
21
|
-
for (let i = 0; i < len; i += 4) {
|
|
22
|
-
const tmp = bitArray[i / 4];
|
|
23
|
-
out[i] = (tmp >>> 24) & 0xff;
|
|
24
|
-
out[i + 1] = (tmp >>> 16) & 0xff;
|
|
25
|
-
out[i + 2] = (tmp >>> 8) & 0xff;
|
|
26
|
-
out[i + 3] = tmp & 0xff;
|
|
27
|
-
}
|
|
28
|
-
return out;
|
|
29
|
-
}
|
|
30
|
-
/** Convert buffer data to word array */
|
|
31
|
-
function bufferToWordArray(buffer) {
|
|
32
|
-
const out = [];
|
|
33
|
-
const length = buffer.byteLength;
|
|
34
|
-
for (let i = 0; i < length; i += 4) {
|
|
35
|
-
out.push((buffer[i] << 24) | (buffer[i + 1] << 16) | (buffer[i + 2] << 8) | buffer[i + 3]);
|
|
36
|
-
}
|
|
37
|
-
if (length & 3) {
|
|
38
|
-
out[out.length - 1] = sjcl.bitArray.partial(8 * (length & 3), out[out.length - 1], 1);
|
|
39
|
-
}
|
|
40
|
-
return out;
|
|
41
|
-
}
|
|
42
|
-
/** Create aes params */
|
|
43
|
-
function aesKdfJs(passphrase, salt) {
|
|
44
|
-
return sjcl.misc.pbkdf2(passphrase, salt, PBKDF2_ITERATIONS, AES_KEY_SIZE * 8, sjcl.misc.hmac);
|
|
45
|
-
}
|
|
46
|
-
/** wrap non-error thrown */
|
|
47
|
-
function wrapError(error) {
|
|
48
|
-
if (error instanceof Error) {
|
|
49
|
-
return error;
|
|
50
|
-
}
|
|
51
|
-
return new Error(String(error), { cause: error });
|
|
52
|
-
}
|
|
53
|
-
/** crypto-js encrypt */
|
|
54
|
-
export async function encrypt({ data, aad }, passphrase) {
|
|
55
|
-
try {
|
|
56
|
-
const nonce = sjcl.random.randomWords(NONCE_SIZE / 4);
|
|
57
|
-
const key = aesKdfJs(passphrase, nonce);
|
|
58
|
-
const encrypted = sjcl.mode.gcm.encrypt(new sjcl.cipher.aes(key), bufferToWordArray(data), nonce, aad ? bufferToWordArray(aad) : undefined, AES_TAG_SIZE * 8);
|
|
59
|
-
return await Promise.resolve({
|
|
60
|
-
nonce: wordArrayToBuffer(nonce),
|
|
61
|
-
data: wordArrayToBuffer(encrypted),
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
catch (ex) {
|
|
65
|
-
throw wrapError(ex);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
/** crypto-js decrypt */
|
|
69
|
-
export async function decrypt({ data, aad, nonce }, passphrase) {
|
|
70
|
-
try {
|
|
71
|
-
const n = bufferToWordArray(nonce);
|
|
72
|
-
const key = aesKdfJs(passphrase, n);
|
|
73
|
-
const decrypted = sjcl.mode.gcm.decrypt(new sjcl.cipher.aes(key), bufferToWordArray(data), n, aad ? bufferToWordArray(aad) : undefined, AES_TAG_SIZE * 8);
|
|
74
|
-
return await Promise.resolve({
|
|
75
|
-
data: wordArrayToBuffer(decrypted),
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
catch (ex) {
|
|
79
|
-
throw wrapError(ex);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
//# sourceMappingURL=pure-js.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pure-js.js","sourceRoot":"","sources":["../../src/encryption/pure-js.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,M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CAAC;QACF,OAAO,MAAM,OAAO,CAAC,OAAO,CAAC;YACzB,IAAI,EAAE,iBAAiB,CAAC,SAAS,CAAC;SACrC,CAAC,CAAC;IACP,CAAC;IAAC,OAAO,EAAE,EAAE,CAAC;QACV,MAAM,SAAS,CAAC,EAAE,CAAC,CAAC;IACxB,CAAC;AACL,CAAC"}
|
package/src/encryption/js/aes.ts
DELETED
|
@@ -1,191 +0,0 @@
|
|
|
1
|
-
/** AES S-box Tables */
|
|
2
|
-
type SBoxTable = [t0: Uint32Array, t1: Uint32Array, t2: Uint32Array, t3: Uint32Array, t4: Uint8Array];
|
|
3
|
-
|
|
4
|
-
/** Compute AES S-box Tables */
|
|
5
|
-
function createSBox(): [SBoxTable, SBoxTable] {
|
|
6
|
-
const encTable: SBoxTable = [
|
|
7
|
-
new Uint32Array(256),
|
|
8
|
-
new Uint32Array(256),
|
|
9
|
-
new Uint32Array(256),
|
|
10
|
-
new Uint32Array(256),
|
|
11
|
-
new Uint8Array(256),
|
|
12
|
-
];
|
|
13
|
-
const decTable: SBoxTable = [
|
|
14
|
-
new Uint32Array(256),
|
|
15
|
-
new Uint32Array(256),
|
|
16
|
-
new Uint32Array(256),
|
|
17
|
-
new Uint32Array(256),
|
|
18
|
-
new Uint8Array(256),
|
|
19
|
-
];
|
|
20
|
-
|
|
21
|
-
const sbox = encTable[4];
|
|
22
|
-
const sboxInv = decTable[4];
|
|
23
|
-
|
|
24
|
-
const d = new Uint8Array(256);
|
|
25
|
-
const th = new Uint8Array(256);
|
|
26
|
-
|
|
27
|
-
// Compute double and third tables
|
|
28
|
-
for (let i = 0; i < 256; i++) {
|
|
29
|
-
d[i] = (i << 1) ^ ((i >> 7) * 283);
|
|
30
|
-
th[d[i] ^ i] = i;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
let x = 0,
|
|
34
|
-
xInv = 0,
|
|
35
|
-
x2 = 0,
|
|
36
|
-
x4 = 0,
|
|
37
|
-
x8 = 0;
|
|
38
|
-
for (; !sbox[x]; x ^= x2 || 1, xInv = th[xInv] || 1) {
|
|
39
|
-
// Compute sbox
|
|
40
|
-
let s = xInv ^ (xInv << 1) ^ (xInv << 2) ^ (xInv << 3) ^ (xInv << 4);
|
|
41
|
-
s = (s >> 8) ^ (s & 255) ^ 99;
|
|
42
|
-
sbox[x] = s;
|
|
43
|
-
sboxInv[s] = x;
|
|
44
|
-
|
|
45
|
-
// Compute MixColumns
|
|
46
|
-
x8 = d[(x4 = d[(x2 = d[x])])];
|
|
47
|
-
let tDec = (x8 * 0x101_0101) ^ (x4 * 0x1_0001) ^ (x2 * 0x101) ^ (x * 0x101_0100);
|
|
48
|
-
let tEnc = (d[s] * 0x101) ^ (s * 0x101_0100);
|
|
49
|
-
|
|
50
|
-
for (let i = 0; i < 4; i++) {
|
|
51
|
-
encTable[i][x] = tEnc = (tEnc << 24) ^ (tEnc >>> 8);
|
|
52
|
-
decTable[i][s] = tDec = (tDec << 24) ^ (tDec >>> 8);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return [encTable, decTable];
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
let encryptTable: SBoxTable;
|
|
60
|
-
let decryptTable: SBoxTable;
|
|
61
|
-
/** 初始化 */
|
|
62
|
-
function init(): void {
|
|
63
|
-
if (!encryptTable) {
|
|
64
|
-
[encryptTable, decryptTable] = createSBox();
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
/** AES 算法 */
|
|
68
|
-
export class AES {
|
|
69
|
-
/** 加密密钥 */
|
|
70
|
-
private readonly encKey: Uint32Array;
|
|
71
|
-
/** 解密密钥 */
|
|
72
|
-
private readonly decKey: Uint32Array;
|
|
73
|
-
constructor(key: Uint32Array) {
|
|
74
|
-
if (key.length !== 4 && key.length !== 6 && key.length !== 8) {
|
|
75
|
-
throw new TypeError('Invalid aes key length');
|
|
76
|
-
}
|
|
77
|
-
init();
|
|
78
|
-
|
|
79
|
-
const sbox = encryptTable[4],
|
|
80
|
-
decTable = decryptTable,
|
|
81
|
-
keyLen = key.length,
|
|
82
|
-
rKeyLen = 4 * key.length + 28;
|
|
83
|
-
|
|
84
|
-
this.encKey = new Uint32Array(rKeyLen);
|
|
85
|
-
this.decKey = new Uint32Array(rKeyLen);
|
|
86
|
-
const { encKey, decKey } = this;
|
|
87
|
-
|
|
88
|
-
encKey.set(key);
|
|
89
|
-
|
|
90
|
-
// schedule encryption keys
|
|
91
|
-
let rcon = 1;
|
|
92
|
-
for (let i = keyLen; i < rKeyLen; i++) {
|
|
93
|
-
let tmp = this.encKey[i - 1];
|
|
94
|
-
|
|
95
|
-
// apply sbox
|
|
96
|
-
if (i % keyLen === 0 || (keyLen === 8 && i % keyLen === 4)) {
|
|
97
|
-
tmp =
|
|
98
|
-
(sbox[tmp >>> 24] << 24) ^
|
|
99
|
-
(sbox[(tmp >> 16) & 255] << 16) ^
|
|
100
|
-
(sbox[(tmp >> 8) & 255] << 8) ^
|
|
101
|
-
sbox[tmp & 255];
|
|
102
|
-
|
|
103
|
-
// shift rows and add rcon
|
|
104
|
-
if (i % keyLen === 0) {
|
|
105
|
-
tmp = (tmp << 8) ^ (tmp >>> 24) ^ (rcon << 24);
|
|
106
|
-
rcon = (rcon << 1) ^ ((rcon >> 7) * 283);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
encKey[i] = encKey[i - keyLen] ^ tmp;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
// schedule decryption keys
|
|
114
|
-
for (let i = rKeyLen, j = 0; i; j++, i--) {
|
|
115
|
-
const tmp = encKey[j & 3 ? i : i - 4];
|
|
116
|
-
if (i <= 4 || j < 4) {
|
|
117
|
-
decKey[j] = tmp;
|
|
118
|
-
} else {
|
|
119
|
-
decKey[j] =
|
|
120
|
-
decTable[0][sbox[tmp >>> 24]] ^
|
|
121
|
-
decTable[1][sbox[(tmp >> 16) & 255]] ^
|
|
122
|
-
decTable[2][sbox[(tmp >> 8) & 255]] ^
|
|
123
|
-
decTable[3][sbox[tmp & 255]];
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Encryption and decryption core.
|
|
130
|
-
*/
|
|
131
|
-
private crypt(
|
|
132
|
-
input: Uint32Array,
|
|
133
|
-
inputOffset: number,
|
|
134
|
-
output: Uint32Array,
|
|
135
|
-
outputOffset: number,
|
|
136
|
-
decrypt: boolean,
|
|
137
|
-
): void {
|
|
138
|
-
const key = decrypt ? this.decKey : this.encKey;
|
|
139
|
-
const [t0, t1, t2, t3, sbox] = decrypt ? decryptTable : encryptTable;
|
|
140
|
-
|
|
141
|
-
// state variables a,b,c,d are loaded with pre-whitened data
|
|
142
|
-
let a = input[inputOffset] ^ key[0],
|
|
143
|
-
b = input[inputOffset + (decrypt ? 3 : 1)] ^ key[1],
|
|
144
|
-
c = input[inputOffset + 2] ^ key[2],
|
|
145
|
-
d = input[inputOffset + (decrypt ? 1 : 3)] ^ key[3];
|
|
146
|
-
|
|
147
|
-
let kIndex = 4;
|
|
148
|
-
|
|
149
|
-
// Inner rounds. Cribbed from OpenSSL.
|
|
150
|
-
const nInnerRounds = key.length / 4 - 2;
|
|
151
|
-
for (let i = 0; i < nInnerRounds; i++) {
|
|
152
|
-
const a2 = t0[a >>> 24] ^ t1[(b >> 16) & 255] ^ t2[(c >> 8) & 255] ^ t3[d & 255] ^ key[kIndex];
|
|
153
|
-
const b2 = t0[b >>> 24] ^ t1[(c >> 16) & 255] ^ t2[(d >> 8) & 255] ^ t3[a & 255] ^ key[kIndex + 1];
|
|
154
|
-
const c2 = t0[c >>> 24] ^ t1[(d >> 16) & 255] ^ t2[(a >> 8) & 255] ^ t3[b & 255] ^ key[kIndex + 2];
|
|
155
|
-
d = t0[d >>> 24] ^ t1[(a >> 16) & 255] ^ t2[(b >> 8) & 255] ^ t3[c & 255] ^ key[kIndex + 3];
|
|
156
|
-
a = a2;
|
|
157
|
-
b = b2;
|
|
158
|
-
c = c2;
|
|
159
|
-
kIndex += 4;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Last round.
|
|
163
|
-
for (let i = 0; i < 4; i++) {
|
|
164
|
-
output[outputOffset + (decrypt ? 3 & -i : i)] =
|
|
165
|
-
(sbox[a >>> 24] << 24) ^
|
|
166
|
-
(sbox[(b >> 16) & 255] << 16) ^
|
|
167
|
-
(sbox[(c >> 8) & 255] << 8) ^
|
|
168
|
-
sbox[d & 255] ^
|
|
169
|
-
key[kIndex++];
|
|
170
|
-
const a2 = a;
|
|
171
|
-
a = b;
|
|
172
|
-
b = c;
|
|
173
|
-
c = d;
|
|
174
|
-
d = a2;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Encrypt a block of plain text.
|
|
180
|
-
*/
|
|
181
|
-
encrypt(input: Uint32Array, inputOffset: number, output: Uint32Array, outputOffset: number): void {
|
|
182
|
-
return this.crypt(input, inputOffset, output, outputOffset, false);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Decrypt a block of cipher text.
|
|
187
|
-
*/
|
|
188
|
-
decrypt(input: Uint32Array, inputOffset: number, output: Uint32Array, outputOffset: number): void {
|
|
189
|
-
return this.crypt(input, inputOffset, output, outputOffset, true);
|
|
190
|
-
}
|
|
191
|
-
}
|
package/src/encryption/js/gcm.ts
DELETED
|
@@ -1,258 +0,0 @@
|
|
|
1
|
-
import { AES } from './aes.js';
|
|
2
|
-
|
|
3
|
-
// const console = { log() {} };
|
|
4
|
-
const EMPTY = new Uint8Array(0);
|
|
5
|
-
|
|
6
|
-
/** GCM (Galois/Counter Mode) */
|
|
7
|
-
export class GCM {
|
|
8
|
-
constructor(
|
|
9
|
-
readonly cipher: AES,
|
|
10
|
-
readonly iv: Uint8Array,
|
|
11
|
-
readonly tagLength = 128,
|
|
12
|
-
readonly aad = EMPTY,
|
|
13
|
-
) {
|
|
14
|
-
this.H = new Uint32Array(4);
|
|
15
|
-
this.cipher.encrypt(new Uint32Array(4), 0, this.H, 0);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/** Convert a Uint8Array to a Uint32Array */
|
|
19
|
-
private toUint32Array(data: Uint8Array): Uint32Array {
|
|
20
|
-
const out = new Uint32Array(Math.ceil(data.byteLength / 4));
|
|
21
|
-
for (let i = 0; i < out.length; i++) {
|
|
22
|
-
out[i] = (data[i * 4] << 24) | (data[i * 4 + 1] << 16) | (data[i * 4 + 2] << 8) | data[i * 4 + 3];
|
|
23
|
-
}
|
|
24
|
-
return out;
|
|
25
|
-
}
|
|
26
|
-
/** Convert a Uint32Array to a Uint8Array */
|
|
27
|
-
private toUint8Array(data: Uint32Array, byteLength: number): Uint8Array {
|
|
28
|
-
const out = new Uint8Array(byteLength);
|
|
29
|
-
for (let i = 0; i < byteLength; i++) {
|
|
30
|
-
out[i] = (data[Math.trunc(i / 4)] >>> (24 - (i % 4) * 8)) & 0xff;
|
|
31
|
-
}
|
|
32
|
-
return out;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** Set out of range bytes to 0 */
|
|
36
|
-
private clamp(data: Uint32Array, byteLength: number): void {
|
|
37
|
-
const mask = 0xffff_ffff << (32 - (byteLength % 4) * 8);
|
|
38
|
-
data[Math.trunc(byteLength / 4)] &= mask;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
private readonly H: Uint32Array;
|
|
42
|
-
/** Compute the galois multiplication of X and Y */
|
|
43
|
-
private galoisMultiply(x_r: Uint32Array, y: Uint32Array): void {
|
|
44
|
-
let Zi0 = 0,
|
|
45
|
-
Zi1 = 0,
|
|
46
|
-
Zi2 = 0,
|
|
47
|
-
Zi3 = 0;
|
|
48
|
-
let Vi0 = y[0],
|
|
49
|
-
Vi1 = y[1],
|
|
50
|
-
Vi2 = y[2],
|
|
51
|
-
Vi3 = y[3];
|
|
52
|
-
|
|
53
|
-
// Block size is 128 bits, run 128 times to get Z_128
|
|
54
|
-
for (let i = 0; i < 128; i++) {
|
|
55
|
-
const xi = (x_r[i >> 5] & (1 << (31 - (i % 32)))) !== 0;
|
|
56
|
-
if (xi) {
|
|
57
|
-
// Z_i+1 = Z_i ^ V_i
|
|
58
|
-
Zi0 ^= Vi0;
|
|
59
|
-
Zi1 ^= Vi1;
|
|
60
|
-
Zi2 ^= Vi2;
|
|
61
|
-
Zi3 ^= Vi3;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// Store the value of LSB(V_i)
|
|
65
|
-
const lsb_Vi = (Vi3 & 1) !== 0;
|
|
66
|
-
|
|
67
|
-
// V_i+1 = V_i >> 1
|
|
68
|
-
Vi3 = (Vi3 >>> 1) | ((Vi2 & 1) << 31);
|
|
69
|
-
Vi2 = (Vi2 >>> 1) | ((Vi1 & 1) << 31);
|
|
70
|
-
Vi1 = (Vi1 >>> 1) | ((Vi0 & 1) << 31);
|
|
71
|
-
Vi0 = Vi0 >>> 1;
|
|
72
|
-
|
|
73
|
-
// If LSB(V_i) is 1, V_i+1 = (V_i >> 1) ^ R
|
|
74
|
-
if (lsb_Vi) {
|
|
75
|
-
Vi0 = Vi0 ^ (0xe1 << 24);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
x_r[0] = Zi0;
|
|
79
|
-
x_r[1] = Zi1;
|
|
80
|
-
x_r[2] = Zi2;
|
|
81
|
-
x_r[3] = Zi3;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** Ghash */
|
|
85
|
-
private ghash(Y: Uint32Array, data: Uint32Array): void {
|
|
86
|
-
const l = data.length;
|
|
87
|
-
for (let i = 0; i < l; i += 4) {
|
|
88
|
-
Y[0] ^= 0xffff_ffff & data[i];
|
|
89
|
-
Y[1] ^= 0xffff_ffff & data[i + 1];
|
|
90
|
-
Y[2] ^= 0xffff_ffff & data[i + 2];
|
|
91
|
-
Y[3] ^= 0xffff_ffff & data[i + 3];
|
|
92
|
-
this.galoisMultiply(Y, this.H);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/** GCM CTR mode. */
|
|
97
|
-
private ctr(encrypt: boolean, data: Uint32Array, length: number): { data: Uint32Array; tag: Uint8Array } {
|
|
98
|
-
// console.log('data inpm', toHex(data));
|
|
99
|
-
// Calculate data lengths
|
|
100
|
-
const l = length / 4;
|
|
101
|
-
const bl = l * 32;
|
|
102
|
-
const abl = this.aad.byteLength * 8;
|
|
103
|
-
const ivbl = this.iv.byteLength * 8;
|
|
104
|
-
|
|
105
|
-
// Calculate the parameters
|
|
106
|
-
const J0 = new Uint32Array(4);
|
|
107
|
-
if (ivbl === 96) {
|
|
108
|
-
new Uint8Array(J0.buffer, J0.byteOffset).set(this.iv);
|
|
109
|
-
J0[3] = 1;
|
|
110
|
-
} else {
|
|
111
|
-
this.ghash(J0, this.toUint32Array(this.iv));
|
|
112
|
-
this.ghash(J0, new Uint32Array([0, 0, Math.trunc(ivbl / 0x1_0000_0000), ivbl & 0xffff_ffff]));
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const S0 = new Uint32Array(4);
|
|
116
|
-
this.ghash(S0, this.toUint32Array(this.aad));
|
|
117
|
-
|
|
118
|
-
// Initialize ctr and tag
|
|
119
|
-
const tag = S0.slice(0);
|
|
120
|
-
|
|
121
|
-
// If decrypting, calculate hash
|
|
122
|
-
if (!encrypt) {
|
|
123
|
-
this.ghash(tag, data);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Encrypt all the data
|
|
127
|
-
const ctr = J0.slice(0);
|
|
128
|
-
const enc = new Uint32Array(4);
|
|
129
|
-
for (let i = 0; i < l; i += 4) {
|
|
130
|
-
ctr[3]++;
|
|
131
|
-
this.cipher.encrypt(ctr, 0, enc, 0);
|
|
132
|
-
|
|
133
|
-
data[i] ^= enc[0];
|
|
134
|
-
data[i + 1] ^= enc[1];
|
|
135
|
-
data[i + 2] ^= enc[2];
|
|
136
|
-
data[i + 3] ^= enc[3];
|
|
137
|
-
}
|
|
138
|
-
this.clamp(data, length);
|
|
139
|
-
// console.log('data inpm', toHex(data));
|
|
140
|
-
|
|
141
|
-
// console.log('H impl', toHex(this.H));
|
|
142
|
-
// console.log('tag impl', toHex(tag));
|
|
143
|
-
// If encrypting, calculate hash
|
|
144
|
-
if (encrypt) {
|
|
145
|
-
this.ghash(tag, data);
|
|
146
|
-
}
|
|
147
|
-
// console.log('tag impl', toHex(tag));
|
|
148
|
-
|
|
149
|
-
// Calculate last block from bit lengths, ugly because bitwise operations are 32-bit
|
|
150
|
-
// Calculate the final tag block
|
|
151
|
-
this.ghash(
|
|
152
|
-
tag,
|
|
153
|
-
new Uint32Array([
|
|
154
|
-
Math.trunc(abl / 0x1_0000_0000),
|
|
155
|
-
abl & 0xffff_ffff,
|
|
156
|
-
Math.trunc(bl / 0x1_0000_0000),
|
|
157
|
-
bl & 0xffff_ffff,
|
|
158
|
-
]),
|
|
159
|
-
);
|
|
160
|
-
|
|
161
|
-
this.cipher.encrypt(J0, 0, enc, 0);
|
|
162
|
-
tag[0] ^= enc[0];
|
|
163
|
-
tag[1] ^= enc[1];
|
|
164
|
-
tag[2] ^= enc[2];
|
|
165
|
-
tag[3] ^= enc[3];
|
|
166
|
-
|
|
167
|
-
// console.log('tag impl', toHex(tag));
|
|
168
|
-
return {
|
|
169
|
-
tag: new Uint8Array(tag.buffer, tag.byteOffset, this.tagLength / 8),
|
|
170
|
-
data,
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
/** 加密 */
|
|
175
|
-
encrypt(data: Uint8Array): Uint8Array {
|
|
176
|
-
const length = data.byteLength;
|
|
177
|
-
const data32 = this.toUint32Array(data);
|
|
178
|
-
const { data: out, tag } = this.ctr(true, data32, length);
|
|
179
|
-
const result = new Uint8Array(length + tag.byteLength);
|
|
180
|
-
result.set(this.toUint8Array(out, length), 0);
|
|
181
|
-
result.set(tag, length);
|
|
182
|
-
return result;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/** 解密 */
|
|
186
|
-
decrypt(data: Uint8Array): Uint8Array {
|
|
187
|
-
let tag, data32, length;
|
|
188
|
-
if (this.tagLength / 8 > data.byteLength) {
|
|
189
|
-
throw new Error('GCM: invalid data length');
|
|
190
|
-
} else if (this.tagLength / 8 === data.byteLength) {
|
|
191
|
-
length = 0;
|
|
192
|
-
tag = data;
|
|
193
|
-
data32 = new Uint32Array(0);
|
|
194
|
-
} else {
|
|
195
|
-
length = data.byteLength - this.tagLength / 8;
|
|
196
|
-
tag = data.subarray(length);
|
|
197
|
-
data32 = this.toUint32Array(data.subarray(0, length));
|
|
198
|
-
}
|
|
199
|
-
const { data: out, tag: tag2 } = this.ctr(false, data32, length);
|
|
200
|
-
if (tag2.some((v, i) => v !== tag[i])) {
|
|
201
|
-
throw new Error('GCM: tag does not match');
|
|
202
|
-
}
|
|
203
|
-
return this.toUint8Array(out, length);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
// import sjcl from 'sjcl';
|
|
208
|
-
// global.sjcl = sjcl;
|
|
209
|
-
|
|
210
|
-
// // @ts-expect-error sjcl is not a module
|
|
211
|
-
// await import('sjcl/core/aes.js');
|
|
212
|
-
// // @ts-expect-error sjcl is not a module
|
|
213
|
-
// await import('sjcl/core/gcm.js');
|
|
214
|
-
// // @ts-expect-error sjcl is not a module
|
|
215
|
-
// await import('sjcl/core/bitArray.js');
|
|
216
|
-
// // @ts-expect-error sjcl is not a module
|
|
217
|
-
// await import('sjcl/core/pbkdf2.js');
|
|
218
|
-
// // @ts-expect-error sjcl is not a module
|
|
219
|
-
// await import('sjcl/core/hmac.js');
|
|
220
|
-
// // @ts-expect-error sjcl is not a module
|
|
221
|
-
// await import('sjcl/core/sha256.js');
|
|
222
|
-
// // @ts-expect-error sjcl is not a module
|
|
223
|
-
// await import('sjcl/core/codecBytes.js');
|
|
224
|
-
|
|
225
|
-
// function toHex(arr: number[] | Uint32Array | Int32Array | Uint8Array): string[] {
|
|
226
|
-
// if (arr instanceof Uint8Array) {
|
|
227
|
-
// const result = [];
|
|
228
|
-
// for (let i = 0; i < arr.length; i += 4) {
|
|
229
|
-
// const a = arr[i].toString(16).padStart(2, '0');
|
|
230
|
-
// const b = arr[i + 1]?.toString(16).padStart(2, '0') ?? '';
|
|
231
|
-
// const c = arr[i + 2]?.toString(16).padStart(2, '0') ?? '';
|
|
232
|
-
// const d = arr[i + 3]?.toString(16).padStart(2, '0') ?? '';
|
|
233
|
-
// result.push(a + b + c + d);
|
|
234
|
-
// }
|
|
235
|
-
// return result;
|
|
236
|
-
// }
|
|
237
|
-
// return [...arr].map((x) => {
|
|
238
|
-
// if (x < 0) x = 0xffffffff + x + 1;
|
|
239
|
-
// return x.toString(16).padStart(8, '0');
|
|
240
|
-
// });
|
|
241
|
-
// }
|
|
242
|
-
// // @ts-expect-error sjcl is not a module
|
|
243
|
-
// global.toHex = toHex;
|
|
244
|
-
|
|
245
|
-
// const data = new Uint8Array([1, 2, 3, 4, 5, 6, 7]);
|
|
246
|
-
// const key = [1, 2, 3, 4];
|
|
247
|
-
// const iv = [9, 8, 7];
|
|
248
|
-
|
|
249
|
-
// const aes = new AES(new Uint32Array(key));
|
|
250
|
-
// const gcm = new GCM(aes, new Uint8Array(new Uint32Array(iv).buffer));
|
|
251
|
-
// const e1 = gcm.encrypt(data);
|
|
252
|
-
// const e2 = sjcl.mode.gcm.encrypt(new sjcl.cipher.aes(key), sjcl.codec.bytes.toBits([...data]), iv);
|
|
253
|
-
// console.log({ d: toHex(e1), l: e1.byteLength }, { d: toHex(e2), l: sjcl.bitArray.bitLength(e2) / 8 });
|
|
254
|
-
|
|
255
|
-
// const d1 = gcm.decrypt(e1);
|
|
256
|
-
// const d2 = sjcl.mode.gcm.decrypt(new sjcl.cipher.aes(key), e2, iv);
|
|
257
|
-
|
|
258
|
-
// console.log({ d: toHex(d1), l: d1.byteLength }, { d: toHex(d2), l: sjcl.bitArray.bitLength(d2) / 8 });
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import sjcl from 'sjcl';
|
|
2
|
-
import {
|
|
3
|
-
NONCE_SIZE,
|
|
4
|
-
AES_KEY_SIZE,
|
|
5
|
-
AES_TAG_SIZE,
|
|
6
|
-
type EncryptedData,
|
|
7
|
-
PBKDF2_ITERATIONS,
|
|
8
|
-
type PlainData,
|
|
9
|
-
} from './common.js';
|
|
10
|
-
|
|
11
|
-
// Load unminified version for debugging
|
|
12
|
-
// globalThis.sjcl = sjcl;
|
|
13
|
-
// // @ts-expect-error sjcl is not a module
|
|
14
|
-
// await import('sjcl/core/aes.js');
|
|
15
|
-
// // @ts-expect-error sjcl is not a module
|
|
16
|
-
// await import('sjcl/core/gcm.js');
|
|
17
|
-
// // @ts-expect-error sjcl is not a module
|
|
18
|
-
// await import('sjcl/core/bitArray.js');
|
|
19
|
-
// // @ts-expect-error sjcl is not a module
|
|
20
|
-
// await import('sjcl/core/pbkdf2.js');
|
|
21
|
-
// // @ts-expect-error sjcl is not a module
|
|
22
|
-
// await import('sjcl/core/hmac.js');
|
|
23
|
-
// // @ts-expect-error sjcl is not a module
|
|
24
|
-
// await import('sjcl/core/sha256.js');
|
|
25
|
-
|
|
26
|
-
/** Convert word array to buffer data */
|
|
27
|
-
function wordArrayToBuffer(bitArray: sjcl.BitArray): Uint8Array {
|
|
28
|
-
const len = sjcl.bitArray.bitLength(bitArray) / 8;
|
|
29
|
-
const out = new Uint8Array(len);
|
|
30
|
-
for (let i = 0; i < len; i += 4) {
|
|
31
|
-
const tmp = bitArray[i / 4];
|
|
32
|
-
out[i] = (tmp >>> 24) & 0xff;
|
|
33
|
-
out[i + 1] = (tmp >>> 16) & 0xff;
|
|
34
|
-
out[i + 2] = (tmp >>> 8) & 0xff;
|
|
35
|
-
out[i + 3] = tmp & 0xff;
|
|
36
|
-
}
|
|
37
|
-
return out;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** Convert buffer data to word array */
|
|
41
|
-
function bufferToWordArray(buffer: Uint8Array): sjcl.BitArray {
|
|
42
|
-
const out = [];
|
|
43
|
-
const length = buffer.byteLength;
|
|
44
|
-
for (let i = 0; i < length; i += 4) {
|
|
45
|
-
out.push((buffer[i] << 24) | (buffer[i + 1] << 16) | (buffer[i + 2] << 8) | buffer[i + 3]);
|
|
46
|
-
}
|
|
47
|
-
if (length & 3) {
|
|
48
|
-
out[out.length - 1] = sjcl.bitArray.partial(8 * (length & 3), out[out.length - 1], 1);
|
|
49
|
-
}
|
|
50
|
-
return out;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** Create aes params */
|
|
54
|
-
function aesKdfJs(passphrase: string, salt: sjcl.BitArray): sjcl.BitArray {
|
|
55
|
-
return sjcl.misc.pbkdf2(passphrase, salt, PBKDF2_ITERATIONS, AES_KEY_SIZE * 8, sjcl.misc.hmac);
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** wrap non-error thrown */
|
|
59
|
-
function wrapError(error: unknown): Error {
|
|
60
|
-
if (error instanceof Error) {
|
|
61
|
-
return error;
|
|
62
|
-
}
|
|
63
|
-
return new Error(String(error), { cause: error });
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/** crypto-js encrypt */
|
|
67
|
-
export async function encrypt({ data, aad }: PlainData, passphrase: string): Promise<EncryptedData> {
|
|
68
|
-
try {
|
|
69
|
-
const nonce = sjcl.random.randomWords(NONCE_SIZE / 4);
|
|
70
|
-
const key = aesKdfJs(passphrase, nonce);
|
|
71
|
-
const encrypted = sjcl.mode.gcm.encrypt(
|
|
72
|
-
new sjcl.cipher.aes(key),
|
|
73
|
-
bufferToWordArray(data),
|
|
74
|
-
nonce,
|
|
75
|
-
aad ? bufferToWordArray(aad) : undefined,
|
|
76
|
-
AES_TAG_SIZE * 8,
|
|
77
|
-
);
|
|
78
|
-
return await Promise.resolve({
|
|
79
|
-
nonce: wordArrayToBuffer(nonce),
|
|
80
|
-
data: wordArrayToBuffer(encrypted),
|
|
81
|
-
});
|
|
82
|
-
} catch (ex) {
|
|
83
|
-
throw wrapError(ex);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/** crypto-js decrypt */
|
|
88
|
-
export async function decrypt({ data, aad, nonce }: EncryptedData, passphrase: string): Promise<PlainData> {
|
|
89
|
-
try {
|
|
90
|
-
const n = bufferToWordArray(nonce);
|
|
91
|
-
const key = aesKdfJs(passphrase, n);
|
|
92
|
-
const decrypted = sjcl.mode.gcm.decrypt(
|
|
93
|
-
new sjcl.cipher.aes(key),
|
|
94
|
-
bufferToWordArray(data),
|
|
95
|
-
n,
|
|
96
|
-
aad ? bufferToWordArray(aad) : undefined,
|
|
97
|
-
AES_TAG_SIZE * 8,
|
|
98
|
-
);
|
|
99
|
-
return await Promise.resolve({
|
|
100
|
-
data: wordArrayToBuffer(decrypted),
|
|
101
|
-
});
|
|
102
|
-
} catch (ex) {
|
|
103
|
-
throw wrapError(ex);
|
|
104
|
-
}
|
|
105
|
-
}
|