CryptoAlgorithmAES_GCMOpenSSL.cpp [plain text]
#include "config.h"
#include "CryptoAlgorithmAES_GCM.h"
#if ENABLE(WEB_CRYPTO)
#include "CryptoAlgorithmAesGcmParams.h"
#include "CryptoKeyAES.h"
#include "OpenSSLCryptoUniquePtr.h"
#include <openssl/evp.h>
namespace WebCore {
static const EVP_CIPHER* aesAlgorithm(size_t keySize)
{
if (keySize * 8 == 128)
return EVP_aes_128_gcm();
if (keySize * 8 == 192)
return EVP_aes_192_gcm();
if (keySize * 8 == 256)
return EVP_aes_256_gcm();
return nullptr;
}
static Optional<Vector<uint8_t>> cryptEncrypt(const Vector<uint8_t>& key, const Vector<uint8_t>& iv, const Vector<uint8_t>& plainText, const Vector<uint8_t>& additionalData, uint8_t tagLength)
{
const EVP_CIPHER* algorithm = aesAlgorithm(key.size());
if (!algorithm)
return WTF::nullopt;
EvpCipherCtxPtr ctx;
int len;
Vector<uint8_t> cipherText(plainText.size() + tagLength);
size_t tagOffset = plainText.size();
if (!(ctx = EvpCipherCtxPtr(EVP_CIPHER_CTX_new())))
return WTF::nullopt;
if (1 != EVP_CIPHER_CTX_set_padding(ctx.get(), 0))
return WTF::nullopt;
if (1 != EVP_EncryptInit_ex(ctx.get(), algorithm, nullptr, nullptr, nullptr))
return WTF::nullopt;
if (1 != EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_IVLEN, iv.size(), nullptr))
return WTF::nullopt;
if (1 != EVP_EncryptInit_ex(ctx.get(), nullptr, nullptr, key.data(), iv.data()))
return WTF::nullopt;
if (additionalData.size() > 0) {
if (1 != EVP_EncryptUpdate(ctx.get(), nullptr, &len, additionalData.data(), additionalData.size()))
return WTF::nullopt;
}
if (1 != EVP_EncryptUpdate(ctx.get(), cipherText.data(), &len, plainText.data(), plainText.size()))
return WTF::nullopt;
if (1 != EVP_EncryptFinal_ex(ctx.get(), cipherText.data() + len, &len))
return WTF::nullopt;
if (1 != EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_GET_TAG, tagLength, cipherText.data() + tagOffset))
return WTF::nullopt;
return cipherText;
}
static Optional<Vector<uint8_t>> cryptDecrypt(const Vector<uint8_t>& key, const Vector<uint8_t>& iv, const Vector<uint8_t>& cipherText, const Vector<uint8_t>& additionalData, uint8_t tagLength)
{
const EVP_CIPHER* algorithm = aesAlgorithm(key.size());
if (!algorithm)
return WTF::nullopt;
EvpCipherCtxPtr ctx;
int len;
int plainTextLen;
int cipherTextLen = cipherText.size() - tagLength;
Vector<uint8_t> plainText(cipherText.size());
Vector<uint8_t> tag(tagLength);
memcpy(tag.data(), cipherText.data() + cipherTextLen, tagLength);
if (!(ctx = EvpCipherCtxPtr(EVP_CIPHER_CTX_new())))
return WTF::nullopt;
if (1 != EVP_CIPHER_CTX_set_padding(ctx.get(), 0))
return WTF::nullopt;
if (1 != EVP_DecryptInit_ex(ctx.get(), algorithm, nullptr, nullptr, nullptr))
return WTF::nullopt;
if (1 != EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_IVLEN, iv.size(), nullptr))
return WTF::nullopt;
if (1 != EVP_DecryptInit_ex(ctx.get(), nullptr, nullptr, key.data(), iv.data()))
return WTF::nullopt;
if (additionalData.size() > 0) {
if (1 != EVP_DecryptUpdate(ctx.get(), nullptr, &len, additionalData.data(), additionalData.size()))
return WTF::nullopt;
}
if (1 != EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_TAG, tag.size(), tag.data()))
return WTF::nullopt;
if (1 != EVP_DecryptUpdate(ctx.get(), plainText.data(), &len, cipherText.data(), cipherTextLen))
return WTF::nullopt;
plainTextLen = len;
if (1 != EVP_DecryptFinal_ex(ctx.get(), plainText.data() + len, &len))
return WTF::nullopt;
plainTextLen += len;
plainText.shrink(plainTextLen);
return plainText;
}
ExceptionOr<Vector<uint8_t>> CryptoAlgorithmAES_GCM::platformEncrypt(const CryptoAlgorithmAesGcmParams& parameters, const CryptoKeyAES& key, const Vector<uint8_t>& plainText)
{
auto output = cryptEncrypt(key.key(), parameters.ivVector(), plainText, parameters.additionalDataVector(), parameters.tagLength.valueOr(0) / 8);
if (!output)
return Exception { OperationError };
return WTFMove(*output);
}
ExceptionOr<Vector<uint8_t>> CryptoAlgorithmAES_GCM::platformDecrypt(const CryptoAlgorithmAesGcmParams& parameters, const CryptoKeyAES& key, const Vector<uint8_t>& cipherText)
{
auto output = cryptDecrypt(key.key(), parameters.ivVector(), cipherText, parameters.additionalDataVector(), parameters.tagLength.valueOr(0) / 8);
if (!output)
return Exception { OperationError };
return WTFMove(*output);
}
}
#endif // ENABLE(WEB_CRYPTO)