CryptoAlgorithmRSASSA_PKCS1_v1_5Mac.cpp [plain text]
#include "config.h"
#include "CryptoAlgorithmRSASSA_PKCS1_v1_5.h"
#if ENABLE(SUBTLE_CRYPTO)
#include "CommonCryptoUtilities.h"
#include "CryptoDigestAlgorithm.h"
#include "CryptoKeyRSA.h"
namespace WebCore {
static ExceptionOr<Vector<uint8_t>> signRSASSA_PKCS1_v1_5(CryptoAlgorithmIdentifier hash, const PlatformRSAKey key, size_t keyLength, const Vector<uint8_t>& data)
{
CCDigestAlgorithm digestAlgorithm;
if (!getCommonCryptoDigestAlgorithm(hash, digestAlgorithm))
return Exception { OperationError };
auto cryptoDigestAlgorithm = WebCore::cryptoDigestAlgorithm(hash);
if (!cryptoDigestAlgorithm)
return Exception { OperationError };
auto digest = PAL::CryptoDigest::create(*cryptoDigestAlgorithm);
if (!digest)
return Exception { OperationError };
digest->addBytes(data.data(), data.size());
auto digestData = digest->computeHash();
Vector<uint8_t> signature(keyLength / 8); size_t signatureSize = signature.size();
CCCryptorStatus status = CCRSACryptorSign(key, ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.data(), &signatureSize);
if (status)
return Exception { OperationError };
return WTFMove(signature);
}
static ExceptionOr<bool> verifyRSASSA_PKCS1_v1_5(CryptoAlgorithmIdentifier hash, const PlatformRSAKey key, const Vector<uint8_t>& signature, const Vector<uint8_t>& data)
{
CCDigestAlgorithm digestAlgorithm;
if (!getCommonCryptoDigestAlgorithm(hash, digestAlgorithm))
return Exception { OperationError };
auto cryptoDigestAlgorithm = WebCore::cryptoDigestAlgorithm(hash);
if (!cryptoDigestAlgorithm)
return Exception { OperationError };
auto digest = PAL::CryptoDigest::create(*cryptoDigestAlgorithm);
if (!digest)
return Exception { OperationError };
digest->addBytes(data.data(), data.size());
auto digestData = digest->computeHash();
auto status = CCRSACryptorVerify(key, ccPKCS1Padding, digestData.data(), digestData.size(), digestAlgorithm, 0, signature.data(), signature.size());
if (!status)
return true;
if (status == kCCNotVerified || status == kCCDecodeError) return false;
return Exception { OperationError };
}
ExceptionOr<Vector<uint8_t>> CryptoAlgorithmRSASSA_PKCS1_v1_5::platformSign(const CryptoKeyRSA& key, const Vector<uint8_t>& data)
{
return signRSASSA_PKCS1_v1_5(key.hashAlgorithmIdentifier(), key.platformKey(), key.keySizeInBits(), data);
}
ExceptionOr<bool> CryptoAlgorithmRSASSA_PKCS1_v1_5::platformVerify(const CryptoKeyRSA& key, const Vector<uint8_t>& signature, const Vector<uint8_t>& data)
{
return verifyRSASSA_PKCS1_v1_5(key.hashAlgorithmIdentifier(), key.platformKey(), signature, data);
}
}
#endif // ENABLE(SUBTLE_CRYPTO)