Skip to content

Commit 78c1a36

Browse files
committed
wip
1 parent 27d6d3f commit 78c1a36

14 files changed

Lines changed: 199 additions & 86 deletions

File tree

benchmark/crypto/_webcrypto_sync_fast_path_common.js

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const kThresholdSizeLabels = [
1515
'after-threshold',
1616
];
1717

18+
const kParallelism = 4;
19+
1820
function ptn(size) {
1921
const buffer = Buffer.allocUnsafe(size);
2022
for (let i = 0; i < size; i++) {
@@ -98,11 +100,44 @@ function isSupported(operation, algorithm) {
98100

99101
async function measureAsync(bench, n, mode, fn) {
100102
if (mode === 'parallel') {
101-
const promises = new Array(n);
103+
const parallelism = Math.min(kParallelism, n);
104+
let started = 0;
105+
let completed = 0;
102106
bench.start();
103-
for (let i = 0; i < n; i++)
104-
promises[i] = fn(i);
105-
await Promise.all(promises);
107+
await new Promise((resolve, reject) => {
108+
let failed = false;
109+
function fail(err) {
110+
failed = true;
111+
reject(err);
112+
}
113+
114+
function launch() {
115+
if (failed)
116+
return;
117+
118+
const i = started++;
119+
let promise;
120+
try {
121+
promise = fn(i);
122+
} catch (err) {
123+
fail(err);
124+
return;
125+
}
126+
127+
Promise.resolve(promise).then(() => {
128+
if (failed)
129+
return;
130+
if (++completed === n) {
131+
resolve();
132+
} else if (started < n) {
133+
launch();
134+
}
135+
}, fail);
136+
}
137+
138+
for (let i = 0; i < parallelism; i++)
139+
launch();
140+
});
106141
bench.end(n);
107142
return;
108143
}

lib/internal/crypto/aes.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {
77

88
const {
99
AESCipherJob,
10-
kCryptoJobSyncWebCrypto,
10+
kCryptoJobWebCrypto,
1111
kKeyVariantAES_CTR_128,
1212
kKeyVariantAES_CBC_128,
1313
kKeyVariantAES_GCM_128,
@@ -28,9 +28,10 @@ const {
2828

2929
const {
3030
getUsagesMask,
31-
getWebCryptoJobModeForInputLength,
31+
getWebCryptoJobMode,
3232
hasAnyNotIn,
3333
jobPromise,
34+
kWebCryptoDefaultSyncThreshold,
3435
} = require('internal/crypto/util');
3536

3637
const {
@@ -107,8 +108,10 @@ function getVariant(name, length) {
107108
}
108109

109110
function asyncAesCtrCipher(mode, key, data, algorithm) {
111+
const jobMode = data.byteLength <= kWebCryptoDefaultSyncThreshold ?
112+
getWebCryptoJobMode() : kCryptoJobWebCrypto;
110113
return jobPromise(() => new AESCipherJob(
111-
getWebCryptoJobModeForInputLength(data.byteLength),
114+
jobMode,
112115
mode,
113116
getCryptoKeyHandle(key),
114117
data,
@@ -118,8 +121,10 @@ function asyncAesCtrCipher(mode, key, data, algorithm) {
118121
}
119122

120123
function asyncAesCbcCipher(mode, key, data, algorithm) {
124+
const jobMode = data.byteLength <= kWebCryptoDefaultSyncThreshold ?
125+
getWebCryptoJobMode() : kCryptoJobWebCrypto;
121126
return jobPromise(() => new AESCipherJob(
122-
getWebCryptoJobModeForInputLength(data.byteLength),
127+
jobMode,
123128
mode,
124129
getCryptoKeyHandle(key),
125130
data,
@@ -128,8 +133,10 @@ function asyncAesCbcCipher(mode, key, data, algorithm) {
128133
}
129134

130135
function asyncAesKwCipher(mode, key, data) {
136+
const jobMode = data.byteLength <= kWebCryptoDefaultSyncThreshold ?
137+
getWebCryptoJobMode() : kCryptoJobWebCrypto;
131138
return jobPromise(() => new AESCipherJob(
132-
getWebCryptoJobModeForInputLength(data.byteLength),
139+
jobMode,
133140
mode,
134141
getCryptoKeyHandle(key),
135142
data,
@@ -140,10 +147,12 @@ function asyncAesGcmCipher(mode, key, data, algorithm) {
140147
const { tagLength = 128 } = algorithm;
141148
const tagByteLength = tagLength / 8;
142149
const additionalDataLength = algorithm.additionalData?.byteLength ?? 0;
150+
const inputByteLength = data.byteLength + additionalDataLength;
151+
const jobMode = inputByteLength <= kWebCryptoDefaultSyncThreshold ?
152+
getWebCryptoJobMode() : kCryptoJobWebCrypto;
143153

144154
return jobPromise(() => new AESCipherJob(
145-
getWebCryptoJobModeForInputLength(
146-
data.byteLength + additionalDataLength),
155+
jobMode,
147156
mode,
148157
getCryptoKeyHandle(key),
149158
data,
@@ -157,10 +166,12 @@ function asyncAesOcbCipher(mode, key, data, algorithm) {
157166
const { tagLength = 128 } = algorithm;
158167
const tagByteLength = tagLength / 8;
159168
const additionalDataLength = algorithm.additionalData?.byteLength ?? 0;
169+
const inputByteLength = data.byteLength + additionalDataLength;
170+
const jobMode = inputByteLength <= kWebCryptoDefaultSyncThreshold ?
171+
getWebCryptoJobMode() : kCryptoJobWebCrypto;
160172

161173
return jobPromise(() => new AESCipherJob(
162-
getWebCryptoJobModeForInputLength(
163-
data.byteLength + additionalDataLength),
174+
jobMode,
164175
mode,
165176
getCryptoKeyHandle(key),
166177
data,
@@ -199,8 +210,9 @@ function aesGenerateKey(algorithm, extractable, usages) {
199210
'SyntaxError');
200211
}
201212

213+
const jobMode = getWebCryptoJobMode();
202214
return jobPromise(() => new SecretKeyGenJob(
203-
kCryptoJobSyncWebCrypto,
215+
jobMode,
204216
length,
205217
{ name, length },
206218
getUsagesMask(usagesSet),

lib/internal/crypto/cfrg.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const {
88

99
const {
1010
SignJob,
11-
kCryptoJobSyncWebCrypto,
1211
kKeyFormatDER,
1312
kKeyFormatRawPublic,
1413
kSignJobModeSign,
@@ -26,6 +25,7 @@ const {
2625
const {
2726
getUsagesMask,
2827
getUsagesUnion,
28+
getWebCryptoJobMode,
2929
getWebCryptoJobModeForInputLength,
3030
hasAnyNotIn,
3131
jobPromise,
@@ -131,8 +131,9 @@ function cfrgGenerateKey(algorithm, extractable, usages) {
131131
'SyntaxError');
132132
}
133133

134+
const jobMode = getWebCryptoJobMode();
134135
return jobPromise(() => new NidKeyPairGenJob(
135-
kCryptoJobSyncWebCrypto,
136+
jobMode,
136137
nid,
137138
keyAlgorithm,
138139
getUsagesMask(publicUsages),
@@ -239,8 +240,9 @@ function eddsaSignVerify(key, data, algorithm, signature) {
239240
if (getCryptoKeyType(key) !== type)
240241
throw lazyDOMException(`Key must be a ${type} key`, 'InvalidAccessError');
241242

243+
const jobMode = getWebCryptoJobModeForInputLength(data.byteLength);
242244
return jobPromise(() => new SignJob(
243-
getWebCryptoJobModeForInputLength(data.byteLength),
245+
jobMode,
244246
mode,
245247
getCryptoKeyHandle(key),
246248
undefined,

lib/internal/crypto/chacha20_poly1305.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ const {
66

77
const {
88
ChaCha20Poly1305CipherJob,
9+
kCryptoJobWebCrypto,
910
SecretKeyGenJob,
10-
kCryptoJobSyncWebCrypto,
1111
} = internalBinding('crypto');
1212

1313
const {
1414
getUsagesMask,
15-
getWebCryptoJobModeForInputLength,
15+
getWebCryptoJobMode,
1616
hasAnyNotIn,
1717
jobPromise,
18+
kWebCryptoDefaultSyncThreshold,
1819
} = require('internal/crypto/util');
1920

2021
const {
@@ -40,9 +41,11 @@ function validateKeyLength(length) {
4041

4142
function c20pCipher(mode, key, data, algorithm) {
4243
const additionalDataLength = algorithm.additionalData?.byteLength ?? 0;
44+
const inputByteLength = data.byteLength + additionalDataLength;
45+
const jobMode = inputByteLength <= kWebCryptoDefaultSyncThreshold ?
46+
getWebCryptoJobMode() : kCryptoJobWebCrypto;
4347
return jobPromise(() => new ChaCha20Poly1305CipherJob(
44-
getWebCryptoJobModeForInputLength(
45-
data.byteLength + additionalDataLength),
48+
jobMode,
4649
mode,
4750
getCryptoKeyHandle(key),
4851
data,
@@ -67,8 +70,9 @@ function c20pGenerateKey(algorithm, extractable, usages) {
6770
'SyntaxError');
6871
}
6972

73+
const jobMode = getWebCryptoJobMode();
7074
return jobPromise(() => new SecretKeyGenJob(
71-
kCryptoJobSyncWebCrypto,
75+
jobMode,
7276
256,
7377
{ name },
7478
getUsagesMask(usagesSet),

lib/internal/crypto/diffiehellman.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ const {
1919
ECDHConvertKey: _ECDHConvertKey,
2020
kCryptoJobAsync,
2121
kCryptoJobSync,
22-
kCryptoJobSyncWebCrypto,
2322
kCryptoJobWebCrypto,
2423
} = internalBinding('crypto');
2524

@@ -58,6 +57,7 @@ const {
5857

5958
const {
6059
getArrayBufferOrView,
60+
getWebCryptoJobMode,
6161
jobPromise,
6262
jobPromiseThen,
6363
toBuf,
@@ -356,7 +356,7 @@ function ecdhDeriveBits(algorithm, baseKey, length) {
356356
keyAlgorithm.name === 'X25519' ||
357357
keyAlgorithm.name === 'X448' ||
358358
(keyAlgorithm.name === 'ECDH' && keyAlgorithm.namedCurve === 'P-256') ?
359-
kCryptoJobSyncWebCrypto : kCryptoJobWebCrypto;
359+
getWebCryptoJobMode() : kCryptoJobWebCrypto;
360360

361361
const bits = jobPromise(() => new DHBitsJob(
362362
jobMode,

lib/internal/crypto/ec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const {
1010
EcKeyPairGenJob,
1111
KeyObjectHandle,
1212
SignJob,
13-
kCryptoJobSyncWebCrypto,
1413
kCryptoJobWebCrypto,
1514
kKeyFormatDER,
1615
kKeyFormatRawPublic,
@@ -32,6 +31,7 @@ const {
3231
const {
3332
getUsagesMask,
3433
getUsagesUnion,
34+
getWebCryptoJobMode,
3535
getWebCryptoJobModeForInputLength,
3636
hasAnyNotIn,
3737
jobPromise,
@@ -121,8 +121,9 @@ function ecGenerateKey(algorithm, extractable, usages) {
121121
'SyntaxError');
122122
}
123123

124+
const jobMode = getWebCryptoJobMode();
124125
return jobPromise(() => new EcKeyPairGenJob(
125-
kCryptoJobSyncWebCrypto,
126+
jobMode,
126127
namedCurve,
127128
undefined,
128129
keyAlgorithm,

lib/internal/crypto/hkdf.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,10 @@ function hkdfDeriveBits(algorithm, baseKey, length) {
156156
if (length === 0)
157157
return PromiseResolve(new ArrayBuffer(0));
158158

159+
const jobMode = getWebCryptoJobModeForInputLength(
160+
salt.byteLength + info.byteLength + length / 8);
159161
return jobPromise(() => new HKDFJob(
160-
getWebCryptoJobModeForInputLength(
161-
salt.byteLength + info.byteLength + length / 8),
162+
jobMode,
162163
normalizeHashName(hash.name),
163164
getCryptoKeyHandle(baseKey),
164165
salt,

lib/internal/crypto/mac.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const {
88
const {
99
HmacJob,
1010
KmacJob,
11-
kCryptoJobSyncWebCrypto,
1211
kSignJobModeSign,
1312
kSignJobModeVerify,
1413
SecretKeyGenJob,
@@ -17,6 +16,7 @@ const {
1716
const {
1817
getBlockSize,
1918
getUsagesMask,
19+
getWebCryptoJobMode,
2020
getWebCryptoJobModeForInputLength,
2121
hasAnyNotIn,
2222
jobPromise,
@@ -60,8 +60,9 @@ function hmacGenerateKey(algorithm, extractable, usages) {
6060
'SyntaxError');
6161
}
6262

63+
const jobMode = getWebCryptoJobMode();
6364
return jobPromise(() => new SecretKeyGenJob(
64-
kCryptoJobSyncWebCrypto,
65+
jobMode,
6566
length,
6667
{ name, length, hash },
6768
getUsagesMask(usageSet),
@@ -90,8 +91,9 @@ function kmacGenerateKey(algorithm, extractable, usages) {
9091
'SyntaxError');
9192
}
9293

94+
const jobMode = getWebCryptoJobMode();
9395
return jobPromise(() => new SecretKeyGenJob(
94-
kCryptoJobSyncWebCrypto,
96+
jobMode,
9597
length,
9698
{ name, length },
9799
getUsagesMask(usageSet),
@@ -177,8 +179,10 @@ function macImportKey(
177179
function hmacSignVerify(key, data, algorithm, signature) {
178180
const mode = signature === undefined ? kSignJobModeSign : kSignJobModeVerify;
179181
const signatureLength = signature?.byteLength ?? 0;
182+
const jobMode = getWebCryptoJobModeForInputLength(
183+
data.byteLength + signatureLength);
180184
return jobPromise(() => new HmacJob(
181-
getWebCryptoJobModeForInputLength(data.byteLength + signatureLength),
185+
jobMode,
182186
mode,
183187
normalizeHashName(getCryptoKeyAlgorithm(key).hash.name),
184188
getCryptoKeyHandle(key),
@@ -190,9 +194,10 @@ function kmacSignVerify(key, data, algorithm, signature) {
190194
const mode = signature === undefined ? kSignJobModeSign : kSignJobModeVerify;
191195
const signatureLength = signature?.byteLength ?? 0;
192196
const outputByteLength = algorithm.outputLength / 8;
197+
const jobMode = getWebCryptoJobModeForInputLength(
198+
data.byteLength + signatureLength + outputByteLength);
193199
return jobPromise(() => new KmacJob(
194-
getWebCryptoJobModeForInputLength(
195-
data.byteLength + signatureLength + outputByteLength),
200+
jobMode,
196201
mode,
197202
getCryptoKeyHandle(key),
198203
algorithm.name,

lib/internal/crypto/ml_dsa.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const {
99

1010
const {
1111
SignJob,
12-
kCryptoJobSyncWebCrypto,
1312
kCryptoJobWebCrypto,
1413
kKeyFormatDER,
1514
kKeyFormatRawPublic,
@@ -28,6 +27,7 @@ const {
2827
const {
2928
getUsagesMask,
3029
getUsagesUnion,
30+
getWebCryptoJobMode,
3131
hasAnyNotIn,
3232
jobPromise,
3333
} = require('internal/crypto/util');
@@ -88,8 +88,9 @@ function mlDsaGenerateKey(algorithm, extractable, usages) {
8888
'SyntaxError');
8989
}
9090

91+
const jobMode = getWebCryptoJobMode();
9192
return jobPromise(() => new NidKeyPairGenJob(
92-
kCryptoJobSyncWebCrypto,
93+
jobMode,
9394
nid,
9495
keyAlgorithm,
9596
getUsagesMask(publicUsages),

0 commit comments

Comments
 (0)