CryptoAlgorithmHMAC.cpp [plain text]
#include "config.h"
#include "CryptoAlgorithmHMAC.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoAlgorithmHmacKeyParams.h"
#include "CryptoAlgorithmHmacParams.h"
#include "CryptoKeyDataOctetSequence.h"
#include "CryptoKeyHMAC.h"
#include "ExceptionCode.h"
namespace WebCore {
const char* const CryptoAlgorithmHMAC::s_name = "HMAC";
CryptoAlgorithmHMAC::CryptoAlgorithmHMAC()
{
}
CryptoAlgorithmHMAC::~CryptoAlgorithmHMAC()
{
}
std::unique_ptr<CryptoAlgorithm> CryptoAlgorithmHMAC::create()
{
return std::unique_ptr<CryptoAlgorithm>(new CryptoAlgorithmHMAC);
}
CryptoAlgorithmIdentifier CryptoAlgorithmHMAC::identifier() const
{
return s_identifier;
}
bool CryptoAlgorithmHMAC::keyAlgorithmMatches(const CryptoAlgorithmHmacParams& parameters, const CryptoKey& key) const
{
if (key.algorithmIdentifier() != s_identifier)
return false;
ASSERT(isCryptoKeyHMAC(key));
if (toCryptoKeyHMAC(key).hashAlgorithmIdentifier() != parameters.hash)
return false;
return true;
}
void CryptoAlgorithmHMAC::sign(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
{
const CryptoAlgorithmHmacParams& hmacParameters = toCryptoAlgorithmHmacParams(parameters);
if (!keyAlgorithmMatches(hmacParameters, key)) {
ec = NOT_SUPPORTED_ERR;
return;
}
platformSign(hmacParameters, toCryptoKeyHMAC(key), data, WTF::move(callback), WTF::move(failureCallback), ec);
}
void CryptoAlgorithmHMAC::verify(const CryptoAlgorithmParameters& parameters, const CryptoKey& key, const CryptoOperationData& expectedSignature, const CryptoOperationData& data, BoolCallback callback, VoidCallback failureCallback, ExceptionCode& ec)
{
const CryptoAlgorithmHmacParams& hmacParameters = toCryptoAlgorithmHmacParams(parameters);
if (!keyAlgorithmMatches(hmacParameters, key)) {
ec = NOT_SUPPORTED_ERR;
return;
}
platformVerify(hmacParameters, toCryptoKeyHMAC(key), expectedSignature, data, WTF::move(callback), WTF::move(failureCallback), ec);
}
void CryptoAlgorithmHMAC::generateKey(const CryptoAlgorithmParameters& parameters, bool extractable, CryptoKeyUsage usages, KeyOrKeyPairCallback callback, VoidCallback failureCallback, ExceptionCode&)
{
const CryptoAlgorithmHmacKeyParams& hmacParameters = toCryptoAlgorithmHmacKeyParams(parameters);
RefPtr<CryptoKeyHMAC> result = CryptoKeyHMAC::generate(hmacParameters.hasLength ? hmacParameters.length : 0, hmacParameters.hash, extractable, usages);
if (!result) {
failureCallback();
return;
}
callback(result.get(), nullptr);
}
void CryptoAlgorithmHMAC::importKey(const CryptoAlgorithmParameters& parameters, const CryptoKeyData& keyData, bool extractable, CryptoKeyUsage usage, KeyCallback callback, VoidCallback, ExceptionCode& ec)
{
if (keyData.format() != CryptoKeyData::Format::OctetSequence) {
ec = NOT_SUPPORTED_ERR;
return;
}
const CryptoKeyDataOctetSequence& keyDataOctetSequence = toCryptoKeyDataOctetSequence(keyData);
const CryptoAlgorithmHmacParams& hmacParameters = toCryptoAlgorithmHmacParams(parameters);
RefPtr<CryptoKeyHMAC> result = CryptoKeyHMAC::create(keyDataOctetSequence.octetSequence(), hmacParameters.hash, extractable, usage);
callback(*result);
}
}
#endif // ENABLE(SUBTLE_CRYPTO)