CryptoAlgorithmAES_CBCGCrypt.cpp [plain text]
#include "config.h"
#include "CryptoAlgorithmAES_CBC.h"
#if ENABLE(WEB_CRYPTO)
#include "CryptoAlgorithmAesCbcCfbParams.h"
#include "CryptoKeyAES.h"
#include "NotImplemented.h"
#include <pal/crypto/gcrypt/Handle.h>
#include <pal/crypto/gcrypt/Utilities.h>
namespace WebCore {
static Optional<Vector<uint8_t>> gcryptEncrypt(const Vector<uint8_t>& key, const Vector<uint8_t>& iv, Vector<uint8_t>&& plainText)
{
auto algorithm = PAL::GCrypt::aesAlgorithmForKeySize(key.size() * 8);
if (!algorithm)
return WTF::nullopt;
PAL::GCrypt::Handle<gcry_cipher_hd_t> handle;
gcry_error_t error = gcry_cipher_open(&handle, *algorithm, GCRY_CIPHER_MODE_CBC, 0);
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return WTF::nullopt;
}
error = gcry_cipher_setkey(handle, key.data(), key.size());
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return WTF::nullopt;
}
error = gcry_cipher_setiv(handle, iv.data(), iv.size());
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return WTF::nullopt;
}
{
size_t size = plainText.size();
size_t paddedSize = roundUpToMultipleOf(gcry_cipher_get_algo_blklen(*algorithm), size + 1);
ASSERT(paddedSize > size);
if (paddedSize - size > 255)
return WTF::nullopt;
uint8_t paddingValue = paddedSize - size;
plainText.grow(paddedSize);
std::memset(plainText.data() + size, paddingValue, paddingValue);
}
error = gcry_cipher_final(handle);
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return WTF::nullopt;
}
Vector<uint8_t> output(plainText.size());
error = gcry_cipher_encrypt(handle, output.data(), output.size(), plainText.data(), plainText.size());
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return WTF::nullopt;
}
return output;
}
static Optional<Vector<uint8_t>> gcryptDecrypt(const Vector<uint8_t>& key, const Vector<uint8_t>& iv, const Vector<uint8_t>& cipherText)
{
auto algorithm = PAL::GCrypt::aesAlgorithmForKeySize(key.size() * 8);
if (!algorithm)
return WTF::nullopt;
PAL::GCrypt::Handle<gcry_cipher_hd_t> handle;
gcry_error_t error = gcry_cipher_open(&handle, *algorithm, GCRY_CIPHER_MODE_CBC, 0);
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return WTF::nullopt;
}
error = gcry_cipher_setkey(handle, key.data(), key.size());
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return WTF::nullopt;
}
error = gcry_cipher_setiv(handle, iv.data(), iv.size());
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return WTF::nullopt;
}
error = gcry_cipher_final(handle);
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return WTF::nullopt;
}
Vector<uint8_t> output(cipherText.size());
error = gcry_cipher_decrypt(handle, output.data(), output.size(), cipherText.data(), cipherText.size());
if (error != GPG_ERR_NO_ERROR) {
PAL::GCrypt::logError(error);
return WTF::nullopt;
}
{
uint8_t paddingValue = output.last();
if (paddingValue > gcry_cipher_get_algo_blklen(*algorithm))
return WTF::nullopt;
size_t size = output.size();
if (paddingValue > size)
return WTF::nullopt;
if (std::count(output.end() - paddingValue, output.end(), paddingValue) != paddingValue)
return WTF::nullopt;
output.shrink(size - paddingValue);
}
return output;
}
ExceptionOr<Vector<uint8_t>> CryptoAlgorithmAES_CBC::platformEncrypt(const CryptoAlgorithmAesCbcCfbParams& parameters, const CryptoKeyAES& key, const Vector<uint8_t>& plainText)
{
auto output = gcryptEncrypt(key.key(), parameters.ivVector(), Vector<uint8_t>(plainText));
if (!output)
return Exception { OperationError };
return WTFMove(*output);
}
ExceptionOr<Vector<uint8_t>> CryptoAlgorithmAES_CBC::platformDecrypt(const CryptoAlgorithmAesCbcCfbParams& parameters, const CryptoKeyAES& key, const Vector<uint8_t>& cipherText)
{
auto output = gcryptDecrypt(key.key(), parameters.ivVector(), cipherText);
if (!output)
return Exception { OperationError };
return WTFMove(*output);
}
}
#endif // ENABLE(WEB_CRYPTO)