#include "config.h"
#include "CryptoKeyAES.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoAlgorithmDescriptionBuilder.h"
#include "CryptoAlgorithmRegistry.h"
#include "CryptoKeyDataOctetSequence.h"
#include <wtf/text/WTFString.h>
namespace WebCore {
CryptoKeyAES::CryptoKeyAES(CryptoAlgorithmIdentifier algorithm, const Vector<uint8_t>& key, bool extractable, CryptoKeyUsage usage)
: CryptoKey(algorithm, CryptoKeyType::Secret, extractable, usage)
, m_key(key)
{
ASSERT(isValidAESAlgorithm(algorithm));
}
CryptoKeyAES::~CryptoKeyAES()
{
}
bool CryptoKeyAES::isValidAESAlgorithm(CryptoAlgorithmIdentifier algorithm)
{
return algorithm == CryptoAlgorithmIdentifier::AES_CTR
|| algorithm == CryptoAlgorithmIdentifier::AES_CBC
|| algorithm == CryptoAlgorithmIdentifier::AES_CMAC
|| algorithm == CryptoAlgorithmIdentifier::AES_GCM
|| algorithm == CryptoAlgorithmIdentifier::AES_CFB
|| algorithm == CryptoAlgorithmIdentifier::AES_KW;
}
PassRefPtr<CryptoKeyAES> CryptoKeyAES::generate(CryptoAlgorithmIdentifier algorithm, size_t lengthBits, bool extractable, CryptoKeyUsage usages)
{
if (lengthBits % 8)
return nullptr;
return adoptRef(new CryptoKeyAES(algorithm, randomData(lengthBits / 8), extractable, usages));
}
void CryptoKeyAES::buildAlgorithmDescription(CryptoAlgorithmDescriptionBuilder& builder) const
{
CryptoKey::buildAlgorithmDescription(builder);
builder.add("length", m_key.size() * 8);
}
std::unique_ptr<CryptoKeyData> CryptoKeyAES::exportData() const
{
ASSERT(extractable());
return CryptoKeyDataOctetSequence::create(m_key);
}
}
#endif // ENABLE(SUBTLE_CRYPTO)