CryptoUtilitiesCocoa.cpp [plain text]
#include "config.h"
#include "CryptoUtilitiesCocoa.h"
#if ENABLE(WEB_CRYPTO) || ENABLE(WEB_RTC)
#include "CryptoAlgorithmAES_CTR.h"
#include <CommonCrypto/CommonCrypto.h>
namespace WebCore {
ExceptionOr<Vector<uint8_t>> transformAES_CTR(CCOperation operation, const Vector<uint8_t>& counter, size_t counterLength, const Vector<uint8_t>& key, const uint8_t* data, size_t dataSize)
{
size_t numberOfBlocks = dataSize % kCCBlockSizeAES128 ? dataSize / kCCBlockSizeAES128 + 1 : dataSize / kCCBlockSizeAES128;
if (counterLength < sizeof(size_t) * 8 && numberOfBlocks > (static_cast<size_t>(1) << counterLength))
return Exception { OperationError };
CryptoAlgorithmAES_CTR::CounterBlockHelper counterBlockHelper(counter, counterLength);
size_t capacity = counterBlockHelper.countToOverflowSaturating();
size_t headSize = dataSize;
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, 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 = counterBlockHelper.counterVectorAfterOverflow();
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 = dataSize - headSize;
Vector<uint8_t> tail(CCCryptorGetOutputLength(cryptor, tailSize, true));
status = CCCryptorUpdate(cryptor, 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);
}
ExceptionOr<Vector<uint8_t>> deriveHDKFBits(CCDigestAlgorithm digestAlgorithm, const uint8_t* key, size_t keySize, const uint8_t* salt, size_t saltSize, const uint8_t* info, size_t infoSize, size_t length)
{
Vector<uint8_t> result(length / 8);
Vector<uint8_t> infoVector;
ALLOW_DEPRECATED_DECLARATIONS_BEGIN
if (CCKeyDerivationHMac(kCCKDFAlgorithmHKDF, digestAlgorithm, 0, key, keySize, 0, 0, info, infoSize, 0, 0, salt, saltSize, result.data(), result.size()))
return Exception { OperationError };
ALLOW_DEPRECATED_DECLARATIONS_END
return WTFMove(result);
}
ExceptionOr<Vector<uint8_t>> deriveHDKFSHA256Bits(const uint8_t* key, size_t keySize, const uint8_t* salt, size_t saltSize, const uint8_t* info, size_t infoSize, size_t length)
{
return deriveHDKFBits(kCCDigestSHA256, key, keySize, salt, saltSize, info, infoSize, length);
}
Vector<uint8_t> calculateHMACSignature(CCHmacAlgorithm algorithm, const Vector<uint8_t>& key, const uint8_t* data, size_t size)
{
size_t digestLength;
switch (algorithm) {
case kCCHmacAlgSHA1:
digestLength = CC_SHA1_DIGEST_LENGTH;
break;
case kCCHmacAlgSHA224:
digestLength = CC_SHA224_DIGEST_LENGTH;
break;
case kCCHmacAlgSHA256:
digestLength = CC_SHA256_DIGEST_LENGTH;
break;
case kCCHmacAlgSHA384:
digestLength = CC_SHA384_DIGEST_LENGTH;
break;
case kCCHmacAlgSHA512:
digestLength = CC_SHA512_DIGEST_LENGTH;
break;
default:
ASSERT_NOT_REACHED();
return Vector<uint8_t>();
}
Vector<uint8_t> result(digestLength);
CCHmac(algorithm, key.data(), key.size(), data, size, result.data());
return result;
}
Vector<uint8_t> calculateSHA256Signature(const Vector<uint8_t>& key, const uint8_t* data, size_t size)
{
return calculateHMACSignature(kCCHmacAlgSHA256, key, data, size);
}
}
#endif // ENABLE(WEB_CRYPTO)