CryptoAlgorithmPBKDF2GCrypt.cpp [plain text]
#include "config.h"
#include "CryptoAlgorithmPBKDF2.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoAlgorithmPbkdf2Params.h"
#include "CryptoKeyRaw.h"
#include "ExceptionCode.h"
#include "GCryptUtilities.h"
#include "ScriptExecutionContext.h"
namespace WebCore {
static std::optional<Vector<uint8_t>> gcryptDeriveBits(const Vector<uint8_t>& keyData, const Vector<uint8_t>& saltData, CryptoAlgorithmIdentifier hashIdentifier, size_t iterations, size_t length)
{
auto hashAlgorithm = digestAlgorithm(hashIdentifier);
if (!hashAlgorithm)
return std::nullopt;
ASSERT(!(length % 8));
Vector<uint8_t> result(length / 8);
gcry_error_t error = gcry_kdf_derive(keyData.data(), keyData.size(), GCRY_KDF_PBKDF2, *hashAlgorithm, saltData.data(), saltData.size(), iterations, result.size(), result.data());
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return std::nullopt;
}
return result;
}
void CryptoAlgorithmPBKDF2::platformDeriveBits(std::unique_ptr<CryptoAlgorithmParameters>&& parameters, Ref<CryptoKey>&& key, size_t length, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
{
context.ref();
workQueue.dispatch(
[parameters = WTFMove(parameters), key = WTFMove(key), length, callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
auto& pbkdf2Parameters = downcast<CryptoAlgorithmPbkdf2Params>(*parameters);
auto& rawKey = downcast<CryptoKeyRaw>(key.get());
auto output = gcryptDeriveBits(rawKey.key(), pbkdf2Parameters.saltVector(), pbkdf2Parameters.hashIdentifier, pbkdf2Parameters.iterations, length);
if (!output) {
context.postTask(
[callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
exceptionCallback(OperationError);
context.deref();
});
return;
}
context.postTask(
[output = WTFMove(*output), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
callback(output);
context.deref();
});
});
}
}
#endif // ENABLE(SUBTLE_CRYPTO)