CryptoAlgorithmAES_CFBMac.cpp [plain text]
#include "config.h"
#include "CryptoAlgorithmAES_CFB.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoAlgorithmAesCbcCfbParams.h"
#include "CryptoKeyAES.h"
#include "ExceptionCode.h"
#include "ScriptExecutionContext.h"
#include <CommonCrypto/CommonCrypto.h>
namespace WebCore {
static ExceptionOr<Vector<uint8_t>> transformAES_CFB(CCOperation operation, const Vector<uint8_t>& iv, const Vector<uint8_t>& key, const Vector<uint8_t>& data)
{
CCCryptorRef cryptor;
CCCryptorStatus status = CCCryptorCreateWithMode(operation, kCCModeCFB8, kCCAlgorithmAES, ccNoPadding, iv.data(), key.data(), key.size(), 0, 0, 0, 0, &cryptor);
if (status)
return Exception { OperationError };
Vector<uint8_t> result(CCCryptorGetOutputLength(cryptor, data.size(), true));
size_t bytesWritten;
status = CCCryptorUpdate(cryptor, data.data(), data.size(), result.data(), result.size(), &bytesWritten);
if (status)
return Exception { OperationError };
uint8_t* p = result.data() + bytesWritten;
status = CCCryptorFinal(cryptor, p, result.end() - p, &bytesWritten);
p += bytesWritten;
if (status)
return Exception { OperationError };
ASSERT(p <= result.end());
result.shrink(p - result.begin());
CCCryptorRelease(cryptor);
return WTFMove(result);
}
void CryptoAlgorithmAES_CFB::platformEncrypt(std::unique_ptr<CryptoAlgorithmParameters>&& parameters, Ref<CryptoKey>&& key, Vector<uint8_t>&& plainText, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
{
context.ref();
workQueue.dispatch([parameters = WTFMove(parameters), key = WTFMove(key), plainText = WTFMove(plainText), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
auto& aesParameters = downcast<CryptoAlgorithmAesCbcCfbParams>(*parameters);
auto& aesKey = downcast<CryptoKeyAES>(key.get());
ASSERT(aesParameters.ivVector().size() == kCCBlockSizeAES128);
auto result = transformAES_CFB(kCCEncrypt, aesParameters.ivVector(), aesKey.key(), plainText);
if (result.hasException()) {
context.postTask([exceptionCallback = WTFMove(exceptionCallback), ec = result.releaseException().code(), callback = WTFMove(callback)](ScriptExecutionContext& context) {
exceptionCallback(ec);
context.deref();
});
return;
}
context.postTask([callback = WTFMove(callback), result = result.releaseReturnValue(), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
callback(result);
context.deref();
});
});
}
void CryptoAlgorithmAES_CFB::platformDecrypt(std::unique_ptr<CryptoAlgorithmParameters>&& parameters, Ref<CryptoKey>&& key, Vector<uint8_t>&& cipherText, VectorCallback&& callback, ExceptionCallback&& exceptionCallback, ScriptExecutionContext& context, WorkQueue& workQueue)
{
context.ref();
workQueue.dispatch([parameters = WTFMove(parameters), key = WTFMove(key), cipherText = WTFMove(cipherText), callback = WTFMove(callback), exceptionCallback = WTFMove(exceptionCallback), &context]() mutable {
auto& aesParameters = downcast<CryptoAlgorithmAesCbcCfbParams>(*parameters);
auto& aesKey = downcast<CryptoKeyAES>(key.get());
assert(aesParameters.ivVector().size() == kCCBlockSizeAES128);
auto result = transformAES_CFB(kCCDecrypt, aesParameters.ivVector(), aesKey.key(), cipherText);
if (result.hasException()) {
context.postTask([exceptionCallback = WTFMove(exceptionCallback), ec = result.releaseException().code(), callback = WTFMove(callback)](ScriptExecutionContext& context) {
exceptionCallback(ec);
context.deref();
});
return;
}
context.postTask([callback = WTFMove(callback), result = result.releaseReturnValue(), exceptionCallback = WTFMove(exceptionCallback)](ScriptExecutionContext& context) {
callback(result);
context.deref();
});
});
}
}
#endif // ENABLE(SUBTLE_CRYPTO)