@cloudpss/crypto 0.5.25 → 0.5.28
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.d.ts +2 -2
- package/dist/encryption/common.js +6 -4
- package/dist/encryption/common.js.map +1 -1
- package/dist/encryption/index.js +4 -1
- package/dist/encryption/index.js.map +1 -1
- package/dist/encryption/module.d.ts +1 -1
- package/dist/encryption/module.js +2 -2
- package/dist/encryption/module.js.map +1 -1
- package/dist/encryption/node.js +4 -3
- package/dist/encryption/node.js.map +1 -1
- package/dist/encryption/{pure-js.d.ts → wasm.d.ts} +2 -2
- package/dist/encryption/wasm.js +22 -0
- package/dist/encryption/wasm.js.map +1 -0
- package/dist/encryption/web.js +5 -1
- package/dist/encryption/web.js.map +1 -1
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +2 -0
- package/dist/utils.js.map +1 -1
- 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 +10 -6
- package/src/encryption/index.ts +3 -1
- package/src/encryption/module.ts +4 -4
- package/src/encryption/node.ts +4 -3
- package/src/encryption/wasm.ts +47 -0
- package/src/encryption/web.ts +5 -1
- package/src/utils.ts +3 -0
- package/tests/encryption.js +53 -32
- 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
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
|
-
}
|