-
-
Notifications
You must be signed in to change notification settings - Fork 35.7k
Expand file tree
/
Copy pathtest-webcrypto-crypto-job-mode.js
More file actions
228 lines (203 loc) Β· 6.62 KB
/
test-webcrypto-crypto-job-mode.js
File metadata and controls
228 lines (203 loc) Β· 6.62 KB
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
// Flags: --expose-internals
'use strict';
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const { hasOpenSSL } = require('../common/crypto');
const { types: { isCryptoKey } } = require('util');
const { internalBinding } = require('internal/test/binding');
const {
getCryptoKeyHandle,
} = require('internal/crypto/keys');
const {
getUsagesMask,
} = require('internal/crypto/util');
const {
aesCipher,
} = require('internal/crypto/aes');
const {
AESCipherJob,
EcKeyPairGenJob,
HashJob,
SecretKeyGenJob,
kCryptoJobWebCrypto,
kKeyVariantAES_CBC_128,
kWebCryptoCipherEncrypt,
} = internalBinding('crypto');
const { subtle } = globalThis.crypto;
// Defines Object.prototype setters that fail the test if native result objects
// carrying key or shared secret material use [[Set]].
async function withObjectPrototypeSetters(names, fn) {
const descriptors = new Map();
for (const name of names) {
descriptors.set(name, Object.getOwnPropertyDescriptor(Object.prototype, name));
Object.defineProperty(Object.prototype, name, {
__proto__: null,
configurable: true,
get: common.mustNotCall(`Object.prototype.${name} getter`),
set: common.mustNotCall(`Object.prototype.${name} setter`),
});
}
try {
return await fn();
} finally {
for (const name of names) {
const descriptor = descriptors.get(name);
if (descriptor === undefined) {
delete Object.prototype[name];
} else {
Object.defineProperty(Object.prototype, name, descriptor);
}
}
}
}
(async function() {
{
const promise = new HashJob(
kCryptoJobWebCrypto,
'sha256',
Buffer.from('hello'),
undefined).run();
assert.strictEqual(Object.getPrototypeOf(promise), Promise.prototype);
let settled = false;
promise.then(common.mustCall(() => { settled = true; }));
await Promise.resolve();
assert.strictEqual(settled, false);
const digest = await promise;
assert(digest instanceof ArrayBuffer);
assert.strictEqual(digest.byteLength, 32);
assert.strictEqual(Object.hasOwn(digest, 'then'), false);
}
{
const key = await new SecretKeyGenJob(
kCryptoJobWebCrypto,
128,
{ name: 'AES-CBC', length: 128 },
getUsagesMask(new Set(['encrypt'])),
true).run();
assert(isCryptoKey(key));
assert(key instanceof CryptoKey);
assert.strictEqual(key.type, 'secret');
assert.strictEqual(key.extractable, true);
assert.deepStrictEqual(key.usages, ['encrypt']);
}
{
const pair = await withObjectPrototypeSetters(
['publicKey', 'privateKey'],
() => new EcKeyPairGenJob(
kCryptoJobWebCrypto,
'P-256',
undefined,
{ name: 'ECDSA', namedCurve: 'P-256' },
getUsagesMask(new Set(['verify'])),
getUsagesMask(new Set(['sign'])),
true).run());
assert.strictEqual(Object.getPrototypeOf(pair), Object.prototype);
assert.strictEqual(Object.hasOwn(pair, 'then'), false);
assert(isCryptoKey(pair.publicKey));
assert(isCryptoKey(pair.privateKey));
assert(pair.publicKey instanceof CryptoKey);
assert(pair.privateKey instanceof CryptoKey);
assert.strictEqual(pair.publicKey.type, 'public');
assert.strictEqual(pair.privateKey.type, 'private');
assert.deepStrictEqual(pair.publicKey.usages, ['verify']);
assert.deepStrictEqual(pair.privateKey.usages, ['sign']);
}
{
const key = await subtle.generateKey(
{ name: 'AES-CBC', length: 128 },
false,
['encrypt']);
assert.throws(
() => new AESCipherJob(
kCryptoJobWebCrypto,
kWebCryptoCipherEncrypt,
getCryptoKeyHandle(key),
Buffer.alloc(16),
kKeyVariantAES_CBC_128,
Buffer.alloc(15)),
/Invalid initialization vector/);
const promise = aesCipher(
kWebCryptoCipherEncrypt,
key,
Buffer.alloc(16),
{ name: 'AES-CBC', iv: Buffer.alloc(15) });
assert.strictEqual(Object.getPrototypeOf(promise), Promise.prototype);
await assert.rejects(promise, (err) => {
assert.strictEqual(err.name, 'OperationError');
assert.strictEqual(
err.message,
'The operation failed for an operation-specific reason');
assert(err.cause);
assert.match(err.cause.message, /Invalid initialization vector/);
return true;
});
}
{
const key = await subtle.generateKey(
{ name: 'AES-CBC', length: 128 },
false,
['encrypt', 'decrypt']);
const iv = crypto.getRandomValues(new Uint8Array(16));
const ciphertext = new Uint8Array(await subtle.encrypt(
{ name: 'AES-CBC', iv },
key,
Buffer.alloc(16)));
ciphertext[0] ^= 0xff;
await assert.rejects(
subtle.decrypt({ name: 'AES-CBC', iv }, key, ciphertext),
(err) => {
assert.strictEqual(err.name, 'OperationError');
assert.strictEqual(
err.message,
'The operation failed for an operation-specific reason');
assert(err.cause);
assert.strictEqual(typeof err.cause.message, 'string');
assert.notStrictEqual(err.cause.message, '');
return true;
});
}
{
const key = await subtle.generateKey(
{ name: 'HMAC', hash: 'SHA-256' },
false,
['sign', 'verify']);
const data = Buffer.from('hello');
const signature = await subtle.sign('HMAC', key, data);
assert(signature instanceof ArrayBuffer);
assert.strictEqual(
typeof await subtle.verify('HMAC', key, signature, data),
'boolean');
}
{
Object.defineProperty(CryptoKey.prototype, 'then', {
__proto__: null,
configurable: true,
get: common.mustNotCall('CryptoKey.prototype.then getter'),
});
try {
const key = await subtle.generateKey(
{ name: 'AES-CBC', length: 128 },
true,
['encrypt']);
assert(isCryptoKey(key));
assert.strictEqual(Object.hasOwn(key, 'then'), false);
} finally {
delete CryptoKey.prototype.then;
}
}
if (hasOpenSSL(3, 5) || process.features.openssl_is_boringssl) {
const pair = await subtle.generateKey(
{ name: 'ML-KEM-768' },
true,
['encapsulateBits', 'decapsulateBits']);
const result = await withObjectPrototypeSetters(
['sharedKey', 'ciphertext'],
() => subtle.encapsulateBits({ name: 'ML-KEM-768' }, pair.publicKey));
assert.strictEqual(Object.getPrototypeOf(result), Object.prototype);
assert.strictEqual(Object.hasOwn(result, 'then'), false);
assert(result.sharedKey instanceof ArrayBuffer);
assert(result.ciphertext instanceof ArrayBuffer);
}
})().then(common.mustCall());