diff --git a/LayoutTests/crypto/subtle/derive-bits-malformed-parameters-expected.txt b/LayoutTests/crypto/subtle/derive-bits-malformed-parameters-expected.txt index 00b0fbd3ea126..263c85320f50f 100644 --- a/LayoutTests/crypto/subtle/derive-bits-malformed-parameters-expected.txt +++ b/LayoutTests/crypto/subtle/derive-bits-malformed-parameters-expected.txt @@ -5,7 +5,6 @@ On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE PASS crypto.subtle.deriveBits() rejected promise with TypeError: Not enough arguments. PASS crypto.subtle.deriveBits(1) rejected promise with TypeError: Not enough arguments. -PASS crypto.subtle.deriveBits(1, 2) rejected promise with TypeError: Not enough arguments. PASS crypto.subtle.deriveBits({ name:"ECDH", public:wrongKey }, wrongKey, 128) rejected promise with InvalidAccessError: CryptoKey doesn't match AlgorithmIdentifier. PASS crypto.subtle.deriveBits({ name:"ECDH", public:wrongKey }, wrongKey, 128) rejected promise with InvalidAccessError: CryptoKey doesn't support bits derivation. PASS successfullyParsed is true diff --git a/LayoutTests/crypto/subtle/derive-bits-malformed-parameters.html b/LayoutTests/crypto/subtle/derive-bits-malformed-parameters.html index 54e41e71e5ab1..740251ddab8eb 100644 --- a/LayoutTests/crypto/subtle/derive-bits-malformed-parameters.html +++ b/LayoutTests/crypto/subtle/derive-bits-malformed-parameters.html @@ -32,7 +32,6 @@ // Not enough arguments. shouldReject('crypto.subtle.deriveBits()'); shouldReject('crypto.subtle.deriveBits(1)'); -shouldReject('crypto.subtle.deriveBits(1, 2)'); crypto.subtle.importKey("raw", rawKey, hmacImportParams, extractable, ["sign", "verify"]).then(function(result) { wrongKey = result; diff --git a/LayoutTests/imported/w3c/web-platform-tests/WebCryptoAPI/derive_bits_keys/derived_bits_length.https.any-expected.txt b/LayoutTests/imported/w3c/web-platform-tests/WebCryptoAPI/derive_bits_keys/derived_bits_length.https.any-expected.txt index 136c5be6072e0..9c18c7abebfaa 100644 --- a/LayoutTests/imported/w3c/web-platform-tests/WebCryptoAPI/derive_bits_keys/derived_bits_length.https.any-expected.txt +++ b/LayoutTests/imported/w3c/web-platform-tests/WebCryptoAPI/derive_bits_keys/derived_bits_length.https.any-expected.txt @@ -4,20 +4,20 @@ PASS HKDF derivation with 256 as 'length' parameter PASS HKDF derivation with 0 as 'length' parameter PASS HKDF derivation with null as 'length' parameter PASS HKDF derivation with undefined as 'length' parameter -FAIL HKDF derivation with omitted as 'length' parameter assert_equals: deriveBits correctly threw OperationError: Not enough arguments expected "OperationError" but got "TypeError" +PASS HKDF derivation with omitted as 'length' parameter PASS PBKDF2 derivation with 256 as 'length' parameter PASS PBKDF2 derivation with 0 as 'length' parameter PASS PBKDF2 derivation with null as 'length' parameter PASS PBKDF2 derivation with undefined as 'length' parameter -FAIL PBKDF2 derivation with omitted as 'length' parameter assert_equals: deriveBits correctly threw OperationError: Not enough arguments expected "OperationError" but got "TypeError" +PASS PBKDF2 derivation with omitted as 'length' parameter PASS ECDH derivation with 256 as 'length' parameter FAIL ECDH derivation with 0 as 'length' parameter assert_array_equals: Derived bits do not match the expected result. lengths differ, expected array object "" length 0, got object "87,31,26,232,151,28,227,35,250,17,131,137,203,95,65,196,59,61,181,161" length 32 PASS ECDH derivation with null as 'length' parameter PASS ECDH derivation with undefined as 'length' parameter -FAIL ECDH derivation with omitted as 'length' parameter promise_test: Unhandled rejection with value: object "TypeError: Not enough arguments" +PASS ECDH derivation with omitted as 'length' parameter PASS X25519 derivation with 256 as 'length' parameter FAIL X25519 derivation with 0 as 'length' parameter assert_array_equals: Derived bits do not match the expected result. lengths differ, expected array object "" length 0, got object "63,245,136,2,149,247,97,118,8,143,137,228,61,254,190,126,161,149,0,8" length 32 PASS X25519 derivation with null as 'length' parameter PASS X25519 derivation with undefined as 'length' parameter -FAIL X25519 derivation with omitted as 'length' parameter promise_test: Unhandled rejection with value: object "TypeError: Not enough arguments" +PASS X25519 derivation with omitted as 'length' parameter diff --git a/LayoutTests/imported/w3c/web-platform-tests/WebCryptoAPI/derive_bits_keys/derived_bits_length.https.any.worker-expected.txt b/LayoutTests/imported/w3c/web-platform-tests/WebCryptoAPI/derive_bits_keys/derived_bits_length.https.any.worker-expected.txt index 136c5be6072e0..9c18c7abebfaa 100644 --- a/LayoutTests/imported/w3c/web-platform-tests/WebCryptoAPI/derive_bits_keys/derived_bits_length.https.any.worker-expected.txt +++ b/LayoutTests/imported/w3c/web-platform-tests/WebCryptoAPI/derive_bits_keys/derived_bits_length.https.any.worker-expected.txt @@ -4,20 +4,20 @@ PASS HKDF derivation with 256 as 'length' parameter PASS HKDF derivation with 0 as 'length' parameter PASS HKDF derivation with null as 'length' parameter PASS HKDF derivation with undefined as 'length' parameter -FAIL HKDF derivation with omitted as 'length' parameter assert_equals: deriveBits correctly threw OperationError: Not enough arguments expected "OperationError" but got "TypeError" +PASS HKDF derivation with omitted as 'length' parameter PASS PBKDF2 derivation with 256 as 'length' parameter PASS PBKDF2 derivation with 0 as 'length' parameter PASS PBKDF2 derivation with null as 'length' parameter PASS PBKDF2 derivation with undefined as 'length' parameter -FAIL PBKDF2 derivation with omitted as 'length' parameter assert_equals: deriveBits correctly threw OperationError: Not enough arguments expected "OperationError" but got "TypeError" +PASS PBKDF2 derivation with omitted as 'length' parameter PASS ECDH derivation with 256 as 'length' parameter FAIL ECDH derivation with 0 as 'length' parameter assert_array_equals: Derived bits do not match the expected result. lengths differ, expected array object "" length 0, got object "87,31,26,232,151,28,227,35,250,17,131,137,203,95,65,196,59,61,181,161" length 32 PASS ECDH derivation with null as 'length' parameter PASS ECDH derivation with undefined as 'length' parameter -FAIL ECDH derivation with omitted as 'length' parameter promise_test: Unhandled rejection with value: object "TypeError: Not enough arguments" +PASS ECDH derivation with omitted as 'length' parameter PASS X25519 derivation with 256 as 'length' parameter FAIL X25519 derivation with 0 as 'length' parameter assert_array_equals: Derived bits do not match the expected result. lengths differ, expected array object "" length 0, got object "63,245,136,2,149,247,97,118,8,143,137,228,61,254,190,126,161,149,0,8" length 32 PASS X25519 derivation with null as 'length' parameter PASS X25519 derivation with undefined as 'length' parameter -FAIL X25519 derivation with omitted as 'length' parameter promise_test: Unhandled rejection with value: object "TypeError: Not enough arguments" +PASS X25519 derivation with omitted as 'length' parameter diff --git a/LayoutTests/imported/w3c/web-platform-tests/WebCryptoAPI/idlharness.https.any-expected.txt b/LayoutTests/imported/w3c/web-platform-tests/WebCryptoAPI/idlharness.https.any-expected.txt index eab10a61942a6..00b4f5420fc04 100644 --- a/LayoutTests/imported/w3c/web-platform-tests/WebCryptoAPI/idlharness.https.any-expected.txt +++ b/LayoutTests/imported/w3c/web-platform-tests/WebCryptoAPI/idlharness.https.any-expected.txt @@ -49,7 +49,7 @@ PASS SubtleCrypto interface: operation verify(AlgorithmIdentifier, CryptoKey, Bu PASS SubtleCrypto interface: operation digest(AlgorithmIdentifier, BufferSource) PASS SubtleCrypto interface: operation generateKey(AlgorithmIdentifier, boolean, sequence) PASS SubtleCrypto interface: operation deriveKey(AlgorithmIdentifier, CryptoKey, AlgorithmIdentifier, boolean, sequence) -PASS SubtleCrypto interface: operation deriveBits(AlgorithmIdentifier, CryptoKey, unsigned long) +FAIL SubtleCrypto interface: operation deriveBits(AlgorithmIdentifier, CryptoKey, unsigned long) assert_equals: property has wrong .length expected 3 but got 2 PASS SubtleCrypto interface: operation importKey(KeyFormat, (BufferSource or JsonWebKey), AlgorithmIdentifier, boolean, sequence) PASS SubtleCrypto interface: operation exportKey(KeyFormat, CryptoKey) PASS SubtleCrypto interface: operation wrapKey(KeyFormat, CryptoKey, CryptoKey, AlgorithmIdentifier) diff --git a/LayoutTests/imported/w3c/web-platform-tests/WebCryptoAPI/idlharness.https.any.worker-expected.txt b/LayoutTests/imported/w3c/web-platform-tests/WebCryptoAPI/idlharness.https.any.worker-expected.txt index 7c32da145f2c6..14791e7765137 100644 --- a/LayoutTests/imported/w3c/web-platform-tests/WebCryptoAPI/idlharness.https.any.worker-expected.txt +++ b/LayoutTests/imported/w3c/web-platform-tests/WebCryptoAPI/idlharness.https.any.worker-expected.txt @@ -49,7 +49,7 @@ PASS SubtleCrypto interface: operation verify(AlgorithmIdentifier, CryptoKey, Bu PASS SubtleCrypto interface: operation digest(AlgorithmIdentifier, BufferSource) PASS SubtleCrypto interface: operation generateKey(AlgorithmIdentifier, boolean, sequence) PASS SubtleCrypto interface: operation deriveKey(AlgorithmIdentifier, CryptoKey, AlgorithmIdentifier, boolean, sequence) -PASS SubtleCrypto interface: operation deriveBits(AlgorithmIdentifier, CryptoKey, unsigned long) +FAIL SubtleCrypto interface: operation deriveBits(AlgorithmIdentifier, CryptoKey, unsigned long) assert_equals: property has wrong .length expected 3 but got 2 PASS SubtleCrypto interface: operation importKey(KeyFormat, (BufferSource or JsonWebKey), AlgorithmIdentifier, boolean, sequence) PASS SubtleCrypto interface: operation exportKey(KeyFormat, CryptoKey) PASS SubtleCrypto interface: operation wrapKey(KeyFormat, CryptoKey, CryptoKey, AlgorithmIdentifier) diff --git a/Source/WebCore/crypto/CryptoAlgorithm.cpp b/Source/WebCore/crypto/CryptoAlgorithm.cpp index 16ba2d81f4c7b..170b2e3515957 100644 --- a/Source/WebCore/crypto/CryptoAlgorithm.cpp +++ b/Source/WebCore/crypto/CryptoAlgorithm.cpp @@ -60,7 +60,7 @@ void CryptoAlgorithm::generateKey(const CryptoAlgorithmParameters&, bool, Crypto exceptionCallback(ExceptionCode::NotSupportedError); } -void CryptoAlgorithm::deriveBits(const CryptoAlgorithmParameters&, Ref&&, size_t, VectorCallback&&, ExceptionCallback&& exceptionCallback, ScriptExecutionContext&, WorkQueue&) +void CryptoAlgorithm::deriveBits(const CryptoAlgorithmParameters&, Ref&&, std::optional, VectorCallback&&, ExceptionCallback&& exceptionCallback, ScriptExecutionContext&, WorkQueue&) { exceptionCallback(ExceptionCode::NotSupportedError); } diff --git a/Source/WebCore/crypto/CryptoAlgorithm.h b/Source/WebCore/crypto/CryptoAlgorithm.h index 4ece71ab002dd..d63066360f2e1 100644 --- a/Source/WebCore/crypto/CryptoAlgorithm.h +++ b/Source/WebCore/crypto/CryptoAlgorithm.h @@ -68,7 +68,7 @@ class CryptoAlgorithm : public ThreadSafeRefCounted { virtual void verify(const CryptoAlgorithmParameters&, Ref&&, Vector&& signature, Vector&&, BoolCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&); virtual void digest(Vector&&, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&); virtual void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&); - virtual void deriveBits(const CryptoAlgorithmParameters&, Ref&&, size_t length, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&); + virtual void deriveBits(const CryptoAlgorithmParameters&, Ref&&, std::optional length, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&); // FIXME: https://bugs.webkit.org/show_bug.cgi?id=169262 virtual void importKey(CryptoKeyFormat, KeyData&&, const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&, UseCryptoKit); virtual void exportKey(CryptoKeyFormat, Ref&&, KeyDataCallback&&, ExceptionCallback&&, UseCryptoKit); diff --git a/Source/WebCore/crypto/SubtleCrypto.cpp b/Source/WebCore/crypto/SubtleCrypto.cpp index a389ba7d1b1dd..0a38e9e289328 100644 --- a/Source/WebCore/crypto/SubtleCrypto.cpp +++ b/Source/WebCore/crypto/SubtleCrypto.cpp @@ -936,7 +936,7 @@ void SubtleCrypto::deriveKey(JSC::JSGlobalObject& state, AlgorithmIdentifier&& a promise->reject(result.releaseException().code(), "Cannot get key length from derivedKeyType"_s); return; } - size_t length = result.releaseReturnValue(); + std::optional length = result.releaseReturnValue(); auto importAlgorithm = CryptoAlgorithmRegistry::singleton().create(importParams->identifier); auto algorithm = CryptoAlgorithmRegistry::singleton().create(params->identifier); @@ -972,7 +972,7 @@ void SubtleCrypto::deriveKey(JSC::JSGlobalObject& state, AlgorithmIdentifier&& a algorithm->deriveBits(*params, baseKey, length, WTFMove(callback), WTFMove(exceptionCallback), *scriptExecutionContext(), m_workQueue); } -void SubtleCrypto::deriveBits(JSC::JSGlobalObject& state, AlgorithmIdentifier&& algorithmIdentifier, CryptoKey& baseKey, unsigned length, Ref&& promise) +void SubtleCrypto::deriveBits(JSC::JSGlobalObject& state, AlgorithmIdentifier&& algorithmIdentifier, CryptoKey& baseKey, std::optional length, Ref&& promise) { auto paramsOrException = normalizeCryptoAlgorithmParameters(state, WTFMove(algorithmIdentifier), Operations::DeriveBits); if (paramsOrException.hasException()) { diff --git a/Source/WebCore/crypto/SubtleCrypto.h b/Source/WebCore/crypto/SubtleCrypto.h index 27bc24da77640..c76f7f1ab33ad 100644 --- a/Source/WebCore/crypto/SubtleCrypto.h +++ b/Source/WebCore/crypto/SubtleCrypto.h @@ -68,7 +68,7 @@ class SubtleCrypto : public ContextDestructionObserver, public RefCounted&&); void generateKey(JSC::JSGlobalObject&, AlgorithmIdentifier&&, bool extractable, Vector&& keyUsages, Ref&&); void deriveKey(JSC::JSGlobalObject&, AlgorithmIdentifier&&, CryptoKey& baseKey, AlgorithmIdentifier&& derivedKeyType, bool extractable, Vector&&, Ref&&); - void deriveBits(JSC::JSGlobalObject&, AlgorithmIdentifier&&, CryptoKey& baseKey, unsigned length, Ref&&); + void deriveBits(JSC::JSGlobalObject&, AlgorithmIdentifier&&, CryptoKey& baseKey, std::optional length, Ref&&); void importKey(JSC::JSGlobalObject&, KeyFormat, KeyDataVariant&&, AlgorithmIdentifier&&, bool extractable, Vector&&, Ref&&); void exportKey(KeyFormat, CryptoKey&, Ref&&); void wrapKey(JSC::JSGlobalObject&, KeyFormat, CryptoKey&, CryptoKey& wrappingKey, AlgorithmIdentifier&& wrapAlgorithm, Ref&&); diff --git a/Source/WebCore/crypto/SubtleCrypto.idl b/Source/WebCore/crypto/SubtleCrypto.idl index 923c2188131f0..8c2dc3b2d94d7 100644 --- a/Source/WebCore/crypto/SubtleCrypto.idl +++ b/Source/WebCore/crypto/SubtleCrypto.idl @@ -39,7 +39,7 @@ typedef (object or DOMString) AlgorithmIdentifier; [CallWith=CurrentGlobalObject] Promise digest(AlgorithmIdentifier algorithm, BufferSource data); [CallWith=CurrentGlobalObject] Promise generateKey(AlgorithmIdentifier algorithm, boolean extractable, sequence keyUsages); [CallWith=CurrentGlobalObject] Promise deriveKey(AlgorithmIdentifier algorithm, CryptoKey baseKey, AlgorithmIdentifier derivedKeyType, boolean extractable, sequence keyUsages); - [CallWith=CurrentGlobalObject] Promise deriveBits(AlgorithmIdentifier algorithm, CryptoKey baseKey, unsigned long length); + [CallWith=CurrentGlobalObject] Promise deriveBits(AlgorithmIdentifier algorithm, CryptoKey baseKey, optional unsigned long? length = null); [CallWith=CurrentGlobalObject] Promise importKey(KeyFormat format, (BufferSource or JsonWebKey) keyData, AlgorithmIdentifier algorithm, boolean extractable, sequence keyUsages); Promise exportKey(KeyFormat format, CryptoKey key); [CallWith=CurrentGlobalObject] Promise wrapKey(KeyFormat format, CryptoKey key, CryptoKey wrappingKey, AlgorithmIdentifier wrapAlgorithm); diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmECDH.cpp b/Source/WebCore/crypto/algorithms/CryptoAlgorithmECDH.cpp index 7c433fc8deedf..587cfe2407245 100644 --- a/Source/WebCore/crypto/algorithms/CryptoAlgorithmECDH.cpp +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmECDH.cpp @@ -66,7 +66,7 @@ void CryptoAlgorithmECDH::generateKey(const CryptoAlgorithmParameters& parameter callback(WTFMove(pair)); } -void CryptoAlgorithmECDH::deriveBits(const CryptoAlgorithmParameters& parameters, Ref&& baseKey, size_t length, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +void CryptoAlgorithmECDH::deriveBits(const CryptoAlgorithmParameters& parameters, Ref&& baseKey, std::optional length, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) { auto& ecParameters = downcast(parameters); @@ -90,16 +90,16 @@ void CryptoAlgorithmECDH::deriveBits(const CryptoAlgorithmParameters& parameters return; } - auto unifiedCallback = [callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](std::optional>&& derivedKey, size_t length) mutable { + auto unifiedCallback = [callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](std::optional>&& derivedKey, std::optional length) mutable { if (!derivedKey) { exceptionCallback(ExceptionCode::OperationError); return; } - if (!length) { + if (!length || !(*length)) { callback(WTFMove(*derivedKey)); return; } - auto lengthInBytes = std::ceil(length / 8.); + auto lengthInBytes = std::ceil(*length / 8.); if (lengthInBytes > (*derivedKey).size()) { exceptionCallback(ExceptionCode::OperationError); return; diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmECDH.h b/Source/WebCore/crypto/algorithms/CryptoAlgorithmECDH.h index ee59060b8d14c..b452fd1b5ae63 100644 --- a/Source/WebCore/crypto/algorithms/CryptoAlgorithmECDH.h +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmECDH.h @@ -45,7 +45,7 @@ class CryptoAlgorithmECDH final : public CryptoAlgorithm { CryptoAlgorithmIdentifier identifier() const final; void generateKey(const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyOrKeyPairCallback&&, ExceptionCallback&&, ScriptExecutionContext&) final; - void deriveBits(const CryptoAlgorithmParameters&, Ref&&, size_t length, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + void deriveBits(const CryptoAlgorithmParameters&, Ref&&, std::optional length, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; void importKey(CryptoKeyFormat, KeyData&&, const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&, UseCryptoKit) final; void exportKey(CryptoKeyFormat, Ref&&, KeyDataCallback&&, ExceptionCallback&&, UseCryptoKit) final; }; diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmHKDF.cpp b/Source/WebCore/crypto/algorithms/CryptoAlgorithmHKDF.cpp index 76ed3ee477d82..5c62a0bb0767d 100644 --- a/Source/WebCore/crypto/algorithms/CryptoAlgorithmHKDF.cpp +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmHKDF.cpp @@ -44,9 +44,9 @@ CryptoAlgorithmIdentifier CryptoAlgorithmHKDF::identifier() const return s_identifier; } -void CryptoAlgorithmHKDF::deriveBits(const CryptoAlgorithmParameters& parameters, Ref&& baseKey, size_t length, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +void CryptoAlgorithmHKDF::deriveBits(const CryptoAlgorithmParameters& parameters, Ref&& baseKey, std::optional length, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) { - if (!length || length % 8) { + if (!length || !(*length) || *length % 8) { exceptionCallback(ExceptionCode::OperationError); return; } @@ -54,7 +54,7 @@ void CryptoAlgorithmHKDF::deriveBits(const CryptoAlgorithmParameters& parameters UseCryptoKit useCryptoKit = context.settingsValues().cryptoKitEnabled ? UseCryptoKit::Yes : UseCryptoKit::No; dispatchOperationInWorkQueue(workQueue, context, WTFMove(callback), WTFMove(exceptionCallback), [parameters = crossThreadCopy(downcast(parameters)), baseKey = WTFMove(baseKey), length, useCryptoKit] { - return platformDeriveBits(parameters, downcast(baseKey.get()), length, useCryptoKit); + return platformDeriveBits(parameters, downcast(baseKey.get()), *length, useCryptoKit); }); } diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmHKDF.h b/Source/WebCore/crypto/algorithms/CryptoAlgorithmHKDF.h index c38e064642b5e..ebfc4daee327f 100644 --- a/Source/WebCore/crypto/algorithms/CryptoAlgorithmHKDF.h +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmHKDF.h @@ -42,7 +42,7 @@ class CryptoAlgorithmHKDF final : public CryptoAlgorithm { CryptoAlgorithmHKDF() = default; CryptoAlgorithmIdentifier identifier() const final; - void deriveBits(const CryptoAlgorithmParameters&, Ref&&, size_t length, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + void deriveBits(const CryptoAlgorithmParameters&, Ref&&, std::optional length, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; void importKey(CryptoKeyFormat, KeyData&&, const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&, UseCryptoKit = UseCryptoKit::No) final; ExceptionOr getKeyLength(const CryptoAlgorithmParameters&) final; diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmPBKDF2.cpp b/Source/WebCore/crypto/algorithms/CryptoAlgorithmPBKDF2.cpp index 3bf040f41088f..439f206ba4e91 100644 --- a/Source/WebCore/crypto/algorithms/CryptoAlgorithmPBKDF2.cpp +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmPBKDF2.cpp @@ -43,16 +43,16 @@ CryptoAlgorithmIdentifier CryptoAlgorithmPBKDF2::identifier() const return s_identifier; } -void CryptoAlgorithmPBKDF2::deriveBits(const CryptoAlgorithmParameters& parameters, Ref&& baseKey, size_t length, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +void CryptoAlgorithmPBKDF2::deriveBits(const CryptoAlgorithmParameters& parameters, Ref&& baseKey, std::optional length, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) { - if (!length || length % 8) { + if (!length || !(*length) || *length % 8) { exceptionCallback(ExceptionCode::OperationError); return; } dispatchOperationInWorkQueue(workQueue, context, WTFMove(callback), WTFMove(exceptionCallback), [parameters = crossThreadCopy(downcast(parameters)), baseKey = WTFMove(baseKey), length] { - return platformDeriveBits(parameters, downcast(baseKey.get()), length); + return platformDeriveBits(parameters, downcast(baseKey.get()), *length); }); } diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmPBKDF2.h b/Source/WebCore/crypto/algorithms/CryptoAlgorithmPBKDF2.h index d526a2499f9be..4d3dfda9ab4b3 100644 --- a/Source/WebCore/crypto/algorithms/CryptoAlgorithmPBKDF2.h +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmPBKDF2.h @@ -42,7 +42,7 @@ class CryptoAlgorithmPBKDF2 final : public CryptoAlgorithm { CryptoAlgorithmPBKDF2() = default; CryptoAlgorithmIdentifier identifier() const final; - void deriveBits(const CryptoAlgorithmParameters&, Ref&&, size_t length, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + void deriveBits(const CryptoAlgorithmParameters&, Ref&&, std::optional length, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; void importKey(CryptoKeyFormat, KeyData&&, const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&, UseCryptoKit = UseCryptoKit::No) final; ExceptionOr getKeyLength(const CryptoAlgorithmParameters&) final; diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmX25519.cpp b/Source/WebCore/crypto/algorithms/CryptoAlgorithmX25519.cpp index 15cedaf6f3098..7e0a0ad2dc154 100644 --- a/Source/WebCore/crypto/algorithms/CryptoAlgorithmX25519.cpp +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmX25519.cpp @@ -63,7 +63,7 @@ std::optional> CryptoAlgorithmX25519::platformDeriveBits(const C } #endif -void CryptoAlgorithmX25519::deriveBits(const CryptoAlgorithmParameters& parameters, Ref&& baseKey, size_t length, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) +void CryptoAlgorithmX25519::deriveBits(const CryptoAlgorithmParameters& parameters, Ref&& baseKey, std::optional length, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue) { if (baseKey->type() != CryptoKey::Type::Private) { exceptionCallback(ExceptionCode::InvalidAccessError); @@ -86,16 +86,16 @@ void CryptoAlgorithmX25519::deriveBits(const CryptoAlgorithmParameters& paramete return; } - auto unifiedCallback = [callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](std::optional>&& derivedKey, size_t length) mutable { + auto unifiedCallback = [callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](std::optional>&& derivedKey, std::optional length) mutable { if (!derivedKey) { exceptionCallback(ExceptionCode::OperationError); return; } - if (!length) { + if (!length || !(*length)) { callback(WTFMove(*derivedKey)); return; } - auto lengthInBytes = std::ceil(length / 8.); + auto lengthInBytes = std::ceil(*length / 8.); if (lengthInBytes > (*derivedKey).size()) { exceptionCallback(ExceptionCode::OperationError); return; diff --git a/Source/WebCore/crypto/algorithms/CryptoAlgorithmX25519.h b/Source/WebCore/crypto/algorithms/CryptoAlgorithmX25519.h index 0a1e1efd30c45..4b66e2ae5860e 100644 --- a/Source/WebCore/crypto/algorithms/CryptoAlgorithmX25519.h +++ b/Source/WebCore/crypto/algorithms/CryptoAlgorithmX25519.h @@ -36,7 +36,7 @@ class CryptoAlgorithmX25519 final : public CryptoAlgorithm { CryptoAlgorithmIdentifier identifier() const final; void generateKey(const CryptoAlgorithmParameters& , bool extractable, CryptoKeyUsageBitmap usages, KeyOrKeyPairCallback&& , ExceptionCallback&& , ScriptExecutionContext&); - void deriveBits(const CryptoAlgorithmParameters&, Ref&&, size_t length, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; + void deriveBits(const CryptoAlgorithmParameters&, Ref&&, std::optional length, VectorCallback&&, ExceptionCallback&&, ScriptExecutionContext&, WorkQueue&) final; void importKey(CryptoKeyFormat, KeyData&&, const CryptoAlgorithmParameters&, bool extractable, CryptoKeyUsageBitmap, KeyCallback&&, ExceptionCallback&&, UseCryptoKit) final; void exportKey(CryptoKeyFormat, Ref&&, KeyDataCallback&&, ExceptionCallback&&, UseCryptoKit) final;