CryptoAlgorithmAES_CBCMac.cpp [plain text]
#include "config.h"
#include "CryptoAlgorithmAES_CBC.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoAlgorithmAesCbcParams.h"
#include "CryptoKeyAES.h"
#include "ExceptionCode.h"
#include <CommonCrypto/CommonCrypto.h>
namespace WebCore {
static void transformAES_CBC(CCOperation operation, const CryptoAlgorithmAesCbcParams& parameters, const CryptoKeyAES& key, const CryptoOperationData& data, CryptoAlgorithm::VectorCallback&& callback, CryptoAlgorithm::VoidCallback&& failureCallback)
{
static_assert(sizeof(parameters.iv) == kCCBlockSizeAES128, "Initialization vector size must be the same as algorithm block size");
size_t keyLengthInBytes = key.key().size();
if (keyLengthInBytes != 16 && keyLengthInBytes != 24 && keyLengthInBytes != 32) {
failureCallback();
return;
}
CCCryptorRef cryptor;
#if PLATFORM(COCOA)
CCAlgorithm aesAlgorithm = kCCAlgorithmAES;
#else
CCAlgorithm aesAlgorithm = kCCAlgorithmAES128;
#endif
CCCryptorStatus status = CCCryptorCreate(operation, aesAlgorithm, kCCOptionPKCS7Padding, key.key().data(), keyLengthInBytes, parameters.iv.data(), &cryptor);
if (status) {
failureCallback();
return;
}
Vector<uint8_t> result(CCCryptorGetOutputLength(cryptor, data.second, true));
size_t bytesWritten;
status = CCCryptorUpdate(cryptor, data.first, data.second, result.data(), result.size(), &bytesWritten);
if (status) {
failureCallback();
return;
}
uint8_t* p = result.data() + bytesWritten;
status = CCCryptorFinal(cryptor, p, result.end() - p, &bytesWritten);
p += bytesWritten;
if (status) {
failureCallback();
return;
}
ASSERT(p <= result.end());
result.shrink(p - result.begin());
CCCryptorRelease(cryptor);
callback(result);
}
void CryptoAlgorithmAES_CBC::platformEncrypt(const CryptoAlgorithmAesCbcParams& parameters, const CryptoKeyAES& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback, ExceptionCode&)
{
transformAES_CBC(kCCEncrypt, parameters, key, data, WTFMove(callback), WTFMove(failureCallback));
}
void CryptoAlgorithmAES_CBC::platformDecrypt(const CryptoAlgorithmAesCbcParams& parameters, const CryptoKeyAES& key, const CryptoOperationData& data, VectorCallback&& callback, VoidCallback&& failureCallback, ExceptionCode&)
{
transformAES_CBC(kCCDecrypt, parameters, key, data, WTFMove(callback), WTFMove(failureCallback));
}
}
#endif // ENABLE(SUBTLE_CRYPTO)