Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 16d2fd3

Browse filesBrowse files
Anshikakalpanaaduh95
authored andcommitted
crypto: align verifyOneShot accepted types
Signed-off-by: anshikakalpana <anshikajain196872@gmail.com> PR-URL: #63280 Fixes: #62903 Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 02f73c7 commit 16d2fd3
Copy full SHA for 16d2fd3

3 files changed

+42-20Lines changed: 42 additions & 20 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/crypto.md‎

Copy file name to clipboardExpand all lines: doc/api/crypto.md
+3-3Lines changed: 3 additions & 3 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -6090,7 +6090,7 @@ changes:
60906090
<!--lint disable maximum-line-length remark-lint-->
60916091

60926092
* `algorithm` {string | null | undefined}
6093-
* `data` {ArrayBuffer|Buffer|TypedArray|DataView}
6093+
* `data` {ArrayBuffer|Buffer|SharedArrayBuffer|TypedArray|DataView|string}
60946094
* `key` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey}
60956095
* `callback` {Function}
60966096
* `err` {Error}
@@ -6222,9 +6222,9 @@ changes:
62226222
<!--lint disable maximum-line-length remark-lint-->
62236223

62246224
* `algorithm` {string|null|undefined}
6225-
* `data` {ArrayBuffer| Buffer|TypedArray|DataView}
6225+
* `data` {ArrayBuffer|Buffer|SharedArrayBuffer|TypedArray|DataView|string}
62266226
* `key` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey}
6227-
* `signature` {ArrayBuffer|Buffer|TypedArray|DataView}
6227+
* `signature` {ArrayBuffer|Buffer|SharedArrayBuffer|TypedArray|DataView}
62286228
* `callback` {Function}
62296229
* `err` {Error}
62306230
* `result` {boolean}
Collapse file

‎lib/internal/crypto/sig.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/sig.js
+1-15Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,6 @@ function verifyOneShot(algorithm, data, key, signature, callback) {
257257

258258
data = getArrayBufferOrView(data, 'data');
259259

260-
if (!isArrayBufferView(data)) {
261-
throw new ERR_INVALID_ARG_TYPE(
262-
'data',
263-
['Buffer', 'TypedArray', 'DataView'],
264-
data,
265-
);
266-
}
267-
268260
// Options specific to RSA
269261
const rsaPadding = getPadding(key);
270262
const pssSaltLength = getSaltLength(key);
@@ -275,13 +267,7 @@ function verifyOneShot(algorithm, data, key, signature, callback) {
275267
// Options specific to Ed448 and ML-DSA
276268
const context = getContext(key);
277269

278-
if (!isArrayBufferView(signature)) {
279-
throw new ERR_INVALID_ARG_TYPE(
280-
'signature',
281-
['Buffer', 'TypedArray', 'DataView'],
282-
signature,
283-
);
284-
}
270+
signature = getArrayBufferOrView(signature, 'signature');
285271

286272
const {
287273
data: keyData,
Collapse file

‎test/parallel/test-crypto-sign-verify.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-crypto-sign-verify.js
+38-2Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,8 +607,8 @@ if (hasOpenSSL(3, 2)) {
607607
assert.throws(() => crypto.sign(null, data, input), errObj);
608608
assert.throws(() => crypto.verify(null, data, input, sig), errObj);
609609

610-
errObj.message = 'The "signature" argument must be an instance of ' +
611-
'Buffer, TypedArray, or DataView.' +
610+
errObj.message = 'The "signature" argument must be of type string or an instance of ' +
611+
'ArrayBuffer, Buffer, TypedArray, or DataView.' +
612612
common.invalidArgTypeHelper(input);
613613
assert.throws(() => crypto.verify(null, data, 'test', input), errObj);
614614
});
@@ -932,3 +932,39 @@ if (hasOpenSSL(3, 2)) {
932932
}, { code: 'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE', message: /operation not supported for this keytype/ });
933933
}
934934
}
935+
936+
// crypto.verify accepts ArrayBuffer and SharedArrayBuffer for data and signature
937+
{
938+
const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 2048 });
939+
const dataBuffer = Buffer.from('Hello world');
940+
941+
// Data as ArrayBuffer
942+
{
943+
const ab = dataBuffer.buffer.slice(dataBuffer.byteOffset, dataBuffer.byteOffset + dataBuffer.byteLength);
944+
const sig = crypto.sign('SHA256', dataBuffer, privateKey);
945+
assert.strictEqual(crypto.verify('SHA256', ab, publicKey, sig), true);
946+
}
947+
948+
// Data as SharedArrayBuffer
949+
{
950+
const sab = new SharedArrayBuffer(dataBuffer.length);
951+
new Uint8Array(sab).set(dataBuffer);
952+
const sig = crypto.sign('SHA256', dataBuffer, privateKey);
953+
assert.strictEqual(crypto.verify('SHA256', sab, publicKey, sig), true);
954+
}
955+
956+
// Signature as ArrayBuffer
957+
{
958+
const sig = crypto.sign('SHA256', dataBuffer, privateKey);
959+
const sigAB = sig.buffer.slice(sig.byteOffset, sig.byteOffset + sig.byteLength);
960+
assert.strictEqual(crypto.verify('SHA256', dataBuffer, publicKey, sigAB), true);
961+
}
962+
963+
// Signature as SharedArrayBuffer
964+
{
965+
const sig = crypto.sign('SHA256', dataBuffer, privateKey);
966+
const sigSAB = new SharedArrayBuffer(sig.length);
967+
new Uint8Array(sigSAB).set(sig);
968+
assert.strictEqual(crypto.verify('SHA256', dataBuffer, publicKey, sigSAB), true);
969+
}
970+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.