#include "config.h"
#include "CryptoKeyHMAC.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoAlgorithmDescriptionBuilder.h"
#include "CryptoAlgorithmRegistry.h"
#include "CryptoKeyDataOctetSequence.h"
#include <wtf/text/WTFString.h>
namespace WebCore {
CryptoKeyHMAC::CryptoKeyHMAC(const Vector<uint8_t>& key, CryptoAlgorithmIdentifier hash, bool extractable, CryptoKeyUsage usage)
: CryptoKey(CryptoAlgorithmIdentifier::HMAC, CryptoKeyType::Secret, extractable, usage)
, m_hash(hash)
, m_key(key)
{
}
CryptoKeyHMAC::~CryptoKeyHMAC()
{
}
PassRefPtr<CryptoKeyHMAC> CryptoKeyHMAC::generate(size_t lengthBytes, CryptoAlgorithmIdentifier hash, bool extractable, CryptoKeyUsage usages)
{
if (!lengthBytes) {
switch (hash) {
case CryptoAlgorithmIdentifier::SHA_1:
case CryptoAlgorithmIdentifier::SHA_224:
case CryptoAlgorithmIdentifier::SHA_256:
lengthBytes = 64;
break;
case CryptoAlgorithmIdentifier::SHA_384:
case CryptoAlgorithmIdentifier::SHA_512:
lengthBytes = 128;
break;
default:
return nullptr;
}
}
return adoptRef(new CryptoKeyHMAC(randomData(lengthBytes), hash, extractable, usages));
}
void CryptoKeyHMAC::buildAlgorithmDescription(CryptoAlgorithmDescriptionBuilder& builder) const
{
CryptoKey::buildAlgorithmDescription(builder);
auto hashDescriptionBuilder = builder.createEmptyClone();
hashDescriptionBuilder->add("name", CryptoAlgorithmRegistry::shared().nameForIdentifier(m_hash));
builder.add("hash", *hashDescriptionBuilder);
builder.add("length", m_key.size());
}
std::unique_ptr<CryptoKeyData> CryptoKeyHMAC::exportData() const
{
ASSERT(extractable());
return CryptoKeyDataOctetSequence::create(m_key);
}
}
#endif // ENABLE(SUBTLE_CRYPTO)