Skip to content

Commit 8ed4587

Browse files
Renegade334targos
authored andcommitted
crypto: use async functions for non-stub Promise-returning functions
These were intended to mimic simple async functions, but exceptions thrown in the function body would be returned synchronously, not wrapped in a rejected Promise. PR-URL: #59841 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Filip Skokan <panva.ip@gmail.com>
1 parent bb051c5 commit 8ed4587

File tree

7 files changed

+15
-17
lines changed

7 files changed

+15
-17
lines changed

lib/internal/crypto/aes.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const {
55
ArrayBufferPrototypeSlice,
66
ArrayFrom,
77
ArrayPrototypePush,
8-
PromiseReject,
98
SafeSet,
109
TypedArrayPrototypeSlice,
1110
} = primordials;
@@ -144,7 +143,7 @@ function asyncAesKwCipher(mode, key, data) {
144143
getVariant('AES-KW', key[kAlgorithm].length)));
145144
}
146145

147-
function asyncAesGcmCipher(mode, key, data, algorithm) {
146+
async function asyncAesGcmCipher(mode, key, data, algorithm) {
148147
const { tagLength = 128 } = algorithm;
149148

150149
const tagByteLength = tagLength / 8;
@@ -160,9 +159,9 @@ function asyncAesGcmCipher(mode, key, data, algorithm) {
160159
// > If *plaintext* has a length less than *tagLength* bits, then `throw`
161160
// > an `OperationError`.
162161
if (tagByteLength > tag.byteLength) {
163-
return PromiseReject(lazyDOMException(
162+
throw lazyDOMException(
164163
'The provided data is too small.',
165-
'OperationError'));
164+
'OperationError');
166165
}
167166

168167
data = slice(data, 0, -tagByteLength);
@@ -184,7 +183,7 @@ function asyncAesGcmCipher(mode, key, data, algorithm) {
184183
algorithm.additionalData));
185184
}
186185

187-
function asyncAesOcbCipher(mode, key, data, algorithm) {
186+
async function asyncAesOcbCipher(mode, key, data, algorithm) {
188187
const { tagLength = 128 } = algorithm;
189188

190189
const tagByteLength = tagLength / 8;
@@ -197,9 +196,9 @@ function asyncAesOcbCipher(mode, key, data, algorithm) {
197196

198197
// Similar to GCM, OCB requires the tag to be present for decryption
199198
if (tagByteLength > tag.byteLength) {
200-
return PromiseReject(lazyDOMException(
199+
throw lazyDOMException(
201200
'The provided data is too small.',
202-
'OperationError'));
201+
'OperationError');
203202
}
204203

205204
data = slice(data, 0, -tagByteLength);

lib/internal/crypto/cfrg.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ function cfrgImportKey(
343343
extractable);
344344
}
345345

346-
function eddsaSignVerify(key, data, algorithm, signature) {
346+
async function eddsaSignVerify(key, data, algorithm, signature) {
347347
const mode = signature === undefined ? kSignJobModeSign : kSignJobModeVerify;
348348
const type = mode === kSignJobModeSign ? 'private' : 'public';
349349

lib/internal/crypto/chacha20_poly1305.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ const {
44
ArrayBufferIsView,
55
ArrayBufferPrototypeSlice,
66
ArrayFrom,
7-
PromiseReject,
87
SafeSet,
98
TypedArrayPrototypeSlice,
109
} = primordials;
@@ -47,17 +46,17 @@ function validateKeyLength(length) {
4746
throw lazyDOMException('Invalid key length', 'DataError');
4847
}
4948

50-
function c20pCipher(mode, key, data, algorithm) {
49+
async function c20pCipher(mode, key, data, algorithm) {
5150
let tag;
5251
switch (mode) {
5352
case kWebCryptoCipherDecrypt: {
5453
const slice = ArrayBufferIsView(data) ?
5554
TypedArrayPrototypeSlice : ArrayBufferPrototypeSlice;
5655

5756
if (data.byteLength < 16) {
58-
return PromiseReject(lazyDOMException(
57+
throw lazyDOMException(
5958
'The provided data is too small.',
60-
'OperationError'));
59+
'OperationError');
6160
}
6261

6362
tag = slice(data, -16);

lib/internal/crypto/ec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ function ecImportKey(
284284
extractable);
285285
}
286286

287-
function ecdsaSignVerify(key, data, { name, hash }, signature) {
287+
async function ecdsaSignVerify(key, data, { name, hash }, signature) {
288288
const mode = signature === undefined ? kSignJobModeSign : kSignJobModeVerify;
289289
const type = mode === kSignJobModeSign ? 'private' : 'public';
290290

lib/internal/crypto/ml_dsa.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ function mlDsaImportKey(
287287
extractable);
288288
}
289289

290-
function mlDsaSignVerify(key, data, algorithm, signature) {
290+
async function mlDsaSignVerify(key, data, algorithm, signature) {
291291
const mode = signature === undefined ? kSignJobModeSign : kSignJobModeVerify;
292292
const type = mode === kSignJobModeSign ? 'private' : 'public';
293293

lib/internal/crypto/rsa.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function validateRsaOaepAlgorithm(algorithm) {
9393
}
9494
}
9595

96-
function rsaOaepCipher(mode, key, data, algorithm) {
96+
async function rsaOaepCipher(mode, key, data, algorithm) {
9797
validateRsaOaepAlgorithm(algorithm);
9898

9999
const type = mode === kWebCryptoCipherEncrypt ? 'public' : 'private';
@@ -330,7 +330,7 @@ function rsaImportKey(
330330
}, keyUsages, extractable);
331331
}
332332

333-
function rsaSignVerify(key, data, { saltLength }, signature) {
333+
async function rsaSignVerify(key, data, { saltLength }, signature) {
334334
const mode = signature === undefined ? kSignJobModeSign : kSignJobModeVerify;
335335
const type = mode === kSignJobModeSign ? 'private' : 'public';
336336

lib/internal/crypto/webcrypto.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ async function unwrapKey(
10221022
);
10231023
}
10241024

1025-
function signVerify(algorithm, key, data, signature) {
1025+
async function signVerify(algorithm, key, data, signature) {
10261026
let usage = 'sign';
10271027
if (signature !== undefined) {
10281028
usage = 'verify';

0 commit comments

Comments
 (0)