-
Notifications
You must be signed in to change notification settings - Fork 22
/
helper.ts
366 lines (318 loc) · 11.7 KB
/
helper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import assert from "assert";
import { Convert } from "pvtsutils";
import { Crypto, CryptoKey } from "webcrypto-core";
/**
* Returns true if blobs from keys are equal
* @param a Crypto key
* @param b Crypto key
*/
export function isKeyEqual(a: CryptoKey, b: CryptoKey) {
if (a instanceof CryptoKey && b instanceof CryptoKey) {
return (a as any).data.equals((b as any).data);
}
return false;
}
export interface ITestAction {
name?: string;
only?: boolean;
skip?: boolean;
error?: any;
}
export interface ITestGenerateKeyAction extends ITestAction {
algorithm: Algorithm;
extractable: boolean;
keyUsages: KeyUsage[];
}
export interface IImportKeyParams {
format: KeyFormat;
data: JsonWebKey | BufferSource;
algorithm: AlgorithmIdentifier;
extractable: boolean;
keyUsages: KeyUsage[];
}
export interface IImportKeyPairParams {
privateKey: IImportKeyParams;
publicKey: IImportKeyParams;
}
export interface ITestEncryptAction extends ITestAction {
algorithm: Algorithm;
data: BufferSource;
encData: BufferSource;
key: IImportKeyParams | IImportKeyPairParams;
}
export interface ITestSignAction extends ITestAction {
algorithm: Algorithm;
data: BufferSource;
signature: BufferSource;
key: IImportKeyParams | IImportKeyPairParams;
}
export interface ITestDeriveBitsAction extends ITestAction {
algorithm: Algorithm;
key: IImportKeyParams | IImportKeyPairParams;
data: BufferSource;
length: number;
}
export interface ITestDeriveKeyAction extends ITestAction {
algorithm: Algorithm;
key: IImportKeyParams | IImportKeyPairParams;
derivedKeyType: Algorithm;
keyUsages: KeyUsage[];
format: KeyFormat;
keyData: BufferSource | JsonWebKey;
}
export interface ITestWrapKeyAction extends ITestAction {
key: IImportKeyParams | IImportKeyPairParams;
algorithm: Algorithm;
wKey: IImportKeyParams;
wrappedKey?: BufferSource;
}
export interface ITestImportAction extends IImportKeyParams, ITestAction {
}
export interface ITestDigestAction extends ITestAction {
algorithm: AlgorithmIdentifier;
data: BufferSource;
hash: BufferSource;
}
export interface ITestActions {
generateKey?: ITestGenerateKeyAction[];
encrypt?: ITestEncryptAction[];
wrapKey?: ITestWrapKeyAction[];
sign?: ITestSignAction[];
import?: ITestImportAction[];
deriveBits?: ITestDeriveBitsAction[];
deriveKey?: ITestDeriveKeyAction[];
digest?: ITestDigestAction[];
}
export interface ITestParams {
name: string;
only?: boolean;
actions: ITestActions;
}
async function getKeys(crypto: Crypto, key: IImportKeyParams | IImportKeyPairParams) {
const keys = {} as CryptoKeyPair;
if ("privateKey" in key) {
keys.privateKey = await crypto.subtle.importKey(
key.privateKey.format,
key.privateKey.data,
key.privateKey.algorithm,
key.privateKey.extractable,
key.privateKey.keyUsages);
keys.publicKey = await crypto.subtle.importKey(
key.publicKey.format,
key.publicKey.data,
key.publicKey.algorithm,
key.publicKey.extractable,
key.publicKey.keyUsages);
} else {
keys.privateKey = keys.publicKey = await crypto.subtle.importKey(
key.format,
key.data,
key.algorithm,
key.extractable,
key.keyUsages);
}
return keys;
}
async function wrapTest(promise: () => Promise<void>, action: ITestAction, index: number) {
const test = action.skip
? it.skip
: action.only
? it.only
: it;
test(action.name || `#${index + 1}`, async () => {
if (action.error) {
await assert.rejects(promise(), action.error);
} else {
await promise();
}
});
}
export function testCrypto(crypto: Crypto, params: ITestParams[]) {
params.forEach((param) => {
context(param.name, () => {
//#region Generate key
if (param.actions.generateKey) {
context("Generate Key", () => {
param.actions.generateKey!.forEach((action, index) => {
wrapTest(async () => {
const algorithm = Object.assign({}, action.algorithm);
algorithm.name = algorithm.name.toLowerCase();
const key = await crypto.subtle.generateKey(
algorithm,
action.extractable,
action.keyUsages,
);
assert(key);
if (key instanceof CryptoKey) {
assert.equal(key.algorithm.name, action.algorithm.name, "Algorithm name MUST be equal to incoming algorithm and in the same case");
assert.equal(key.extractable, action.extractable);
assert.deepEqual(key.usages, action.keyUsages);
} else {
assert(key.privateKey);
assert.equal(key.privateKey.algorithm.name, action.algorithm.name, "Algorithm name MUST be equal to incoming algorithm and in the same case");
assert.equal(key.privateKey.extractable, action.extractable);
assert(key.publicKey);
assert.equal(key.publicKey.algorithm.name, action.algorithm.name, "Algorithm name MUST be equal to incoming algorithm and in the same case");
assert.equal(key.publicKey.extractable, true);
}
}, action, index);
});
});
}
//#endregion
//#region encrypt
if (param.actions.encrypt) {
context("Encrypt/Decrypt", () => {
param.actions.encrypt!.forEach((action, index) => {
wrapTest(async () => {
// import keys
const keys = await getKeys(crypto, action.key);
const encKey = keys.publicKey;
const decKey = keys.privateKey;
const algorithm = Object.assign({}, action.algorithm);
algorithm.name = algorithm.name.toLowerCase();
// encrypt
const enc = await crypto.subtle.encrypt(algorithm, encKey, action.data);
// decrypt
let dec = await crypto.subtle.decrypt(algorithm, decKey, enc);
assert.equal(Convert.ToHex(dec), Convert.ToHex(action.data));
dec = await crypto.subtle.decrypt(algorithm, decKey, action.encData);
assert.equal(Convert.ToHex(dec), Convert.ToHex(action.data));
}, action, index);
});
});
}
//#endregion
//#region Import/Export
if (param.actions.import) {
context("Import/Export", () => {
param.actions.import!.forEach((action, index) => {
wrapTest(async () => {
const importedKey = await crypto.subtle.importKey(
action.format,
action.data,
action.algorithm,
action.extractable,
action.keyUsages);
// Can't continue if key is not exctractable.
if (!action.extractable) {
return;
}
const exportedData = await crypto.subtle.exportKey(
action.format,
importedKey);
if (action.format === "jwk") {
assert.deepEqual(exportedData, action.data);
} else {
assert.equal(Buffer.from(exportedData as ArrayBuffer).toString("hex"), Buffer.from(action.data as ArrayBuffer).toString("hex"));
}
}, action, index);
});
});
}
//#endregion
//#region Sign/Verify
if (param.actions.sign) {
context("Sign/Verify", () => {
param.actions.sign!.forEach((action, index) => {
wrapTest(async () => {
// import keys
const keys = await getKeys(crypto, action.key);
const verifyKey = keys.publicKey;
const signKey = keys.privateKey;
const algorithm = Object.assign({}, action.algorithm);
algorithm.name = algorithm.name.toLowerCase();
// sign
const signature = await crypto.subtle.sign(algorithm, signKey, action.data);
// verify
let ok = await crypto.subtle.verify(algorithm, verifyKey, signature, action.data);
assert.equal(true, ok, "Cannot verify signature from Action data");
ok = await crypto.subtle.verify(algorithm, verifyKey, action.signature, action.data);
if (!ok) {
assert.equal(Convert.ToHex(signature), Convert.ToHex(action.signature));
}
assert.equal(true, ok);
}, action, index);
});
});
}
//#endregion
//#region Derive bits
if (param.actions.deriveBits) {
context("Derive bits", () => {
param.actions.deriveBits!.forEach((action, index) => {
wrapTest(async () => {
// import keys
const keys = await getKeys(crypto, action.key);
const algorithm = Object.assign({}, action.algorithm, { public: keys.publicKey }) as any;
algorithm.name = algorithm.name.toLowerCase();
// derive bits
const derivedBits = await crypto.subtle.deriveBits(algorithm, keys.privateKey, action.length);
assert.equal(Convert.ToHex(derivedBits), Convert.ToHex(action.data));
}, action, index);
});
});
}
//#endregion
//#region Derive key
if (param.actions.deriveKey) {
context("Derive key", () => {
param.actions.deriveKey!.forEach((action, index) => {
wrapTest(async () => {
// import keys
const keys = await getKeys(crypto, action.key);
const algorithm = Object.assign({}, action.algorithm, { public: keys.publicKey }) as any;
algorithm.name = algorithm.name.toLowerCase();
// derive key
const derivedKey = await crypto.subtle.deriveKey(algorithm, keys.privateKey, action.derivedKeyType, true, action.keyUsages);
const keyData = await crypto.subtle.exportKey(action.format, derivedKey);
if (action.format === "jwk") {
assert.deepEqual(keyData, action.keyData);
} else {
assert.equal(Convert.ToHex(keyData as ArrayBuffer), Convert.ToHex(action.keyData as ArrayBuffer));
}
}, action, index);
});
});
}
//#endregion
//#region Digest
if (param.actions.digest) {
context("Digest", () => {
param.actions.digest!.forEach((action, index) => {
wrapTest(async () => {
const hash = await crypto.subtle.digest(action.algorithm, action.data);
assert.equal(Convert.ToHex(hash), Convert.ToHex(action.hash));
}, action, index);
});
});
}
//#endregion
//#region Wrap/Unwrap key
if (param.actions.wrapKey) {
context("Wrap/Unwrap key", () => {
param.actions.wrapKey!.forEach((action, index) => {
wrapTest(async () => {
const wKey = (await getKeys(crypto, action.wKey)).privateKey;
const key = await getKeys(crypto, action.key);
const wrappedKey = await crypto.subtle.wrapKey(action.wKey.format, wKey, key.publicKey, action.algorithm);
if (action.wrappedKey) {
assert.equal(Convert.ToHex(wrappedKey), Convert.ToHex(action.wrappedKey));
}
const unwrappedKey = await crypto.subtle.unwrapKey(
action.wKey.format,
wrappedKey,
key.privateKey,
action.algorithm,
action.wKey.algorithm,
action.wKey.extractable,
action.wKey.keyUsages);
assert.deepEqual(unwrappedKey.algorithm, wKey.algorithm);
}, action, index);
});
});
}
//#endregion
});
});
}