CryptoAlgorithmAES_CTRMac.cpp [plain text]
#include "config.h"
#include "CryptoAlgorithmAES_CTR.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoAlgorithmAesCtrParams.h"
#include "CryptoKeyAES.h"
#include "ExceptionCode.h"
#include "ScriptExecutionContext.h"
#include <CommonCrypto/CommonCrypto.h>
namespace WebCore {
static size_t bigIntegerToSizeT(const Vector<uint8_t>& bigInteger)
{
size_t result = 0;
for (size_t i = bigInteger.size() - (__WORDSIZE / 8); i < bigInteger.size(); ++i) {
result <<= 8;
result += bigInteger[i];
}
return result;
}
static ExceptionOr<Vector<uint8_t>> transformAES_CTR(CCOperation operation, const Vector<uint8_t>& counter, size_t counterLength, const Vector<uint8_t>& key, const Vector<uint8_t>& data)
{
size_t numberOfBlocks = data.size() % kCCBlockSizeAES128 ? data.size() / kCCBlockSizeAES128 + 1 : data.size() / kCCBlockSizeAES128;
if (counterLength < __WORDSIZE && numberOfBlocks > (1 << counterLength))
return Exception { OperationError };
size_t rightMost = bigIntegerToSizeT(counter); size_t capacity = numberOfBlocks; if (counterLength < __WORDSIZE) {
size_t mask = SIZE_MAX << counterLength; capacity = SIZE_MAX - (rightMost| mask) + 1;
}
if (counterLength == __WORDSIZE)
capacity = SIZE_MAX - rightMost + 1;
size_t headSize = data.size();
if (capacity < numberOfBlocks)
headSize = capacity * kCCBlockSizeAES128;
CCCryptorRef cryptor;
CCCryptorStatus status = CCCryptorCreateWithMode(operation, kCCModeCTR, kCCAlgorithmAES128, ccNoPadding, counter.data(), key.data(), key.size(), 0, 0, 0, kCCModeOptionCTR_BE, &cryptor);
if (status)
return Exception { OperationError };
Vector<uint8_t> head(CCCryptorGetOutputLength(cryptor, headSize, true));
size_t bytesWritten;
status = CCCryptorUpdate(cryptor, data.data(), headSize, head.data(), head.size(), &bytesWritten);
if (status)
return Exception { OperationError };
uint8_t* p = head.data() + bytesWritten;
status = CCCryptorFinal(cryptor, p, head.end() - p, &bytesWritten);
p += bytesWritten;
if (status)
return Exception { OperationError };
ASSERT_WITH_SECURITY_IMPLICATION(p <= head.end());
head.shrink(p - head.begin());
CCCryptorRelease(cryptor);
if (capacity >= numberOfBlocks)
return WTFMove(head);
Vector<uint8_t> remainingCounter(counter.size());
size_t counterOffset = counterLength % 8;
size_t nonceOffset = counter.size() - counterLength / 8 - !!counterOffset;
memcpy(remainingCounter.data(), counter.data(), nonceOffset); if (!!counterOffset) {
size_t mask = SIZE_MAX << counterOffset;
remainingCounter[nonceOffset] = counter[nonceOffset] & mask;
}
memset(remainingCounter.data() + nonceOffset + !!counterOffset, 0, counterLength / 8);
status = CCCryptorCreateWithMode(operation, kCCModeCTR, kCCAlgorithmAES128, ccNoPadding, remainingCounter.data(), key.data(), key.size(), 0, 0, 0, kCCModeOptionCTR_BE, &cryptor);
if (status)
return Exception { OperationError };
size_t tailSize = data.size() - headSize;
Vector<uint8_t> tail(CCCryptorGetOutputLength(cryptor, tailSize, true));
status = CCCryptorUpdate(cryptor, data.data() + headSize, tailSize, tail.data(), tail.size(), &bytesWritten);
if (status)
return Exception { OperationError };
p = tail.data() + bytesWritten;
status = CCCryptorFinal(cryptor, p, tail.end() - p, &bytesWritten);
p += bytesWritten;
if (status)
return Exception { OperationError };
ASSERT_WITH_SECURITY_IMPLICATION(p <= tail.end());
tail.shrink(p - tail.begin());
CCCryptorRelease(cryptor);
head.appendVector(tail);
return WTFMove(head);
}
void CryptoAlgorithmAES_CTR::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<CryptoAlgorithmAesCtrParams>(*parameters);
auto& aesKey = downcast<CryptoKeyAES>(key.get());
ASSERT(aesParameters.counterVector().size() == kCCBlockSizeAES128);
auto result = transformAES_CTR(kCCEncrypt, aesParameters.counterVector(), aesParameters.length, 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_CTR::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<CryptoAlgorithmAesCtrParams>(*parameters);
auto& aesKey = downcast<CryptoKeyAES>(key.get());
assert(aesParameters.counterVector().size() == kCCBlockSizeAES128);
auto result = transformAES_CTR(kCCDecrypt, aesParameters.counterVector(), aesParameters.length, 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)