CryptoAlgorithmHMACMac.cpp [plain text]
#include "config.h"
#include "CryptoAlgorithmHMAC.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CryptoAlgorithmHmacParams.h"
#include "CryptoKeyHMAC.h"
#include "ExceptionCode.h"
#include <CommonCrypto/CommonHMAC.h>
#include <wtf/CryptographicUtilities.h>
namespace WebCore {
static bool getCommonCryptoHMACAlgorithm(CryptoAlgorithmIdentifier hashFunction, CCHmacAlgorithm& algorithm)
{
switch (hashFunction) {
case CryptoAlgorithmIdentifier::SHA_1:
algorithm = kCCHmacAlgSHA1;
return true;
case CryptoAlgorithmIdentifier::SHA_224:
algorithm = kCCHmacAlgSHA224;
return true;
case CryptoAlgorithmIdentifier::SHA_256:
algorithm = kCCHmacAlgSHA256;
return true;
case CryptoAlgorithmIdentifier::SHA_384:
algorithm = kCCHmacAlgSHA384;
return true;
case CryptoAlgorithmIdentifier::SHA_512:
algorithm = kCCHmacAlgSHA512;
return true;
default:
return false;
}
}
static Vector<uint8_t> calculateSignature(CCHmacAlgorithm algorithm, const Vector<uint8_t>& key, const CryptoOperationData& data)
{
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);
const void* keyData = key.data() ? key.data() : reinterpret_cast<const uint8_t*>(""); CCHmac(algorithm, keyData, key.size(), data.first, data.second, result.data());
return result;
}
void CryptoAlgorithmHMAC::platformSign(const CryptoAlgorithmHmacParams& parameters, const CryptoKeyHMAC& key, const CryptoOperationData& data, VectorCallback callback, VoidCallback, ExceptionCode& ec)
{
CCHmacAlgorithm algorithm;
if (!getCommonCryptoHMACAlgorithm(parameters.hash, algorithm)) {
ec = NOT_SUPPORTED_ERR;
return;
}
Vector<uint8_t> signature = calculateSignature(algorithm, key.key(), data);
callback(signature);
}
void CryptoAlgorithmHMAC::platformVerify(const CryptoAlgorithmHmacParams& parameters, const CryptoKeyHMAC& key, const CryptoOperationData& expectedSignature, const CryptoOperationData& data, BoolCallback callback, VoidCallback, ExceptionCode& ec)
{
CCHmacAlgorithm algorithm;
if (!getCommonCryptoHMACAlgorithm(parameters.hash, algorithm)) {
ec = NOT_SUPPORTED_ERR;
return;
}
Vector<uint8_t> signature = calculateSignature(algorithm, key.key(), data);
bool result = signature.size() == expectedSignature.second && !constantTimeMemcmp(signature.data(), expectedSignature.first, signature.size());
callback(result);
}
}
#endif // ENABLE(SUBTLE_CRYPTO)