CryptoAlgorithmECDSAGCrypt.cpp [plain text]
#include "config.h"
#include "CryptoAlgorithmECDSA.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoAlgorithmEcdsaParams.h"
#include "CryptoKeyEC.h"
#include "GCryptUtilities.h"
#include <pal/crypto/CryptoDigest.h>
namespace WebCore {
static bool extractECDSASignatureInteger(Vector<uint8_t>& signature, gcry_sexp_t signatureSexp, const char* integerName, size_t keySizeInBytes)
{
PAL::GCrypt::Handle<gcry_sexp_t> integerSexp(gcry_sexp_find_token(signatureSexp, integerName, 0));
if (!integerSexp)
return false;
auto integerData = mpiData(integerSexp);
if (!integerData)
return false;
size_t dataSize = integerData->size();
if (dataSize >= keySizeInBytes) {
signature.append(&integerData->at(dataSize - keySizeInBytes), keySizeInBytes);
} else {
for (size_t paddingSize = keySizeInBytes - dataSize; paddingSize > 0; --paddingSize)
signature.uncheckedAppend(0x00);
signature.appendVector(*integerData);
}
return true;
}
static std::optional<Vector<uint8_t>> gcryptSign(gcry_sexp_t keySexp, const Vector<uint8_t>& data, CryptoAlgorithmIdentifier hashAlgorithmIdentifier, size_t keySizeInBytes)
{
Vector<uint8_t> dataHash;
{
auto digestAlgorithm = hashCryptoDigestAlgorithm(hashAlgorithmIdentifier);
if (!digestAlgorithm)
return std::nullopt;
auto digest = PAL::CryptoDigest::create(*digestAlgorithm);
if (!digest)
return std::nullopt;
digest->addBytes(data.data(), data.size());
dataHash = digest->computeHash();
}
PAL::GCrypt::Handle<gcry_sexp_t> dataSexp;
{
auto shaAlgorithm = hashAlgorithmName(hashAlgorithmIdentifier);
if (!shaAlgorithm)
return std::nullopt;
gcry_error_t error = gcry_sexp_build(&dataSexp, nullptr, "(data(flags raw)(hash %s %b))",
*shaAlgorithm, dataHash.size(), dataHash.data());
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return std::nullopt;
}
}
PAL::GCrypt::Handle<gcry_sexp_t> signatureSexp;
gcry_error_t error = gcry_pk_sign(&signatureSexp, dataSexp, keySexp);
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return std::nullopt;
}
Vector<uint8_t> signature;
signature.reserveInitialCapacity(keySizeInBytes * 2);
if (!extractECDSASignatureInteger(signature, signatureSexp, "r", keySizeInBytes)
|| !extractECDSASignatureInteger(signature, signatureSexp, "s", keySizeInBytes))
return std::nullopt;
return signature;
}
static std::optional<bool> gcryptVerify(gcry_sexp_t keySexp, const Vector<uint8_t>& signature, const Vector<uint8_t>& data, CryptoAlgorithmIdentifier hashAlgorithmIdentifier, size_t keySizeInBytes)
{
if (signature.size() != keySizeInBytes * 2)
return false;
Vector<uint8_t> dataHash;
{
auto digestAlgorithm = hashCryptoDigestAlgorithm(hashAlgorithmIdentifier);
if (!digestAlgorithm)
return std::nullopt;
auto digest = PAL::CryptoDigest::create(*digestAlgorithm);
if (!digest)
return std::nullopt;
digest->addBytes(data.data(), data.size());
dataHash = digest->computeHash();
}
PAL::GCrypt::Handle<gcry_sexp_t> signatureSexp;
gcry_error_t error = gcry_sexp_build(&signatureSexp, nullptr, "(sig-val(ecdsa(r %b)(s %b)))",
keySizeInBytes, signature.data(), keySizeInBytes, signature.data() + keySizeInBytes);
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return std::nullopt;
}
PAL::GCrypt::Handle<gcry_sexp_t> dataSexp;
{
auto shaAlgorithm = hashAlgorithmName(hashAlgorithmIdentifier);
if (!shaAlgorithm)
return std::nullopt;
error = gcry_sexp_build(&dataSexp, nullptr, "(data(flags raw)(hash %s %b))",
*shaAlgorithm, dataHash.size(), dataHash.data());
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return std::nullopt;
}
}
error = gcry_pk_verify(signatureSexp, dataSexp, keySexp);
return { error == GPG_ERR_NO_ERROR };
}
ExceptionOr<Vector<uint8_t>> CryptoAlgorithmECDSA::platformSign(const CryptoAlgorithmEcdsaParams& parameters, const CryptoKeyEC& key, const Vector<uint8_t>& data)
{
auto output = gcryptSign(key.platformKey(), data, parameters.hashIdentifier, (key.keySizeInBits() + 7) / 8);
if (!output)
return Exception { OperationError };
return WTFMove(*output);
}
ExceptionOr<bool> CryptoAlgorithmECDSA::platformVerify(const CryptoAlgorithmEcdsaParams& parameters, const CryptoKeyEC& key, const Vector<uint8_t>& signature, const Vector<uint8_t>& data)
{
auto output = gcryptVerify(key.platformKey(), signature, data, parameters.hashIdentifier, (key.keySizeInBits() + 7)/ 8);
if (!output)
return Exception { OperationError };
return WTFMove(*output);
}
}
#endif // ENABLE(SUBTLE_CRYPTO)