16 using namespace folly;
22 static const EVP_MD*
getHash(
int hashNid) {
23 const auto hash = EVP_get_digestbynid(hashNid);
25 throw std::runtime_error(
"Invalid hash. Have you initialized openssl?");
30 std::unique_ptr<folly::IOBuf>
ecSign(
36 throw std::runtime_error(
42 if (EVP_SignInit(mdCtx.get(), hash) != 1) {
43 throw std::runtime_error(
"Could not initialize signature");
45 if (EVP_SignUpdate(mdCtx.get(), data.
data(), data.
size()) != 1) {
46 throw std::runtime_error(
50 unsigned int bytesWritten = 0;
52 mdCtx.get(), out->writableData(), &bytesWritten, pkey.get()) != 1) {
53 throw std::runtime_error(
"Failed to sign");
55 out->append(bytesWritten);
67 throw std::runtime_error(
71 if (EVP_VerifyInit(mdCtx.get(), hash) != 1) {
72 throw std::runtime_error(
"Could not initialize verification");
75 if (EVP_VerifyUpdate(mdCtx.get(), data.
data(), data.
size()) != 1) {
76 throw std::runtime_error(
"Could not update verification");
80 mdCtx.get(), signature.
data(), signature.
size(), pkey.get()) != 1) {
81 throw std::runtime_error(
"Signature verification failed");
92 throw std::runtime_error(
97 if (EVP_DigestSignInit(mdCtx.get(), &ctx, hash,
nullptr, pkey.get()) != 1) {
98 throw std::runtime_error(
"Could not initialize signature");
101 if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) <= 0) {
102 throw std::runtime_error(
"Could not set pss padding");
105 if (EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, -1) <= 0) {
106 throw std::runtime_error(
"Could not set pss salt length");
109 if (EVP_DigestSignUpdate(mdCtx.get(), data.
data(), data.
size()) != 1) {
110 throw std::runtime_error(
"Could not update signature");
113 size_t bytesWritten = EVP_PKEY_size(pkey.get());
115 if (EVP_DigestSignFinal(mdCtx.get(), out->writableData(), &bytesWritten) !=
117 throw std::runtime_error(
"Failed to sign");
119 out->append(bytesWritten);
131 throw std::runtime_error(
136 if (EVP_DigestVerifyInit(mdCtx.get(), &ctx, hash,
nullptr, pkey.get()) != 1) {
137 throw std::runtime_error(
"Could not initialize verification");
140 if (EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PSS_PADDING) <= 0) {
141 throw std::runtime_error(
"Could not set pss padding");
144 if (EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, -1) <= 0) {
145 throw std::runtime_error(
"Could not set pss salt length");
148 if (EVP_DigestVerifyUpdate(mdCtx.get(), data.
data(), data.
size()) != 1) {
149 throw std::runtime_error(
"Could not update verification");
152 if (EVP_DigestVerifyFinal(mdCtx.get(), signature.
data(), signature.
size()) !=
154 throw std::runtime_error(
"Signature verification failed");
static std::unique_ptr< IOBuf > create(std::size_t capacity)
void rsaPssVerify(folly::ByteRange data, folly::ByteRange signature, const folly::ssl::EvpPkeyUniquePtr &pkey, int hashNid)
static const EVP_MD * getHash(int hashNid)
constexpr size_type size() const
std::unique_ptr< EVP_PKEY, EvpPkeyDeleter > EvpPkeyUniquePtr
—— Concurrent Priority Queue Implementation ——
constexpr Iter data() const
constexpr auto data(C &c) -> decltype(c.data())
std::unique_ptr< folly::IOBuf > ecSign(folly::ByteRange data, const folly::ssl::EvpPkeyUniquePtr &pkey, int hashNid)
std::string getOpenSSLError()
void ecVerify(folly::ByteRange data, folly::ByteRange signature, const folly::ssl::EvpPkeyUniquePtr &pkey, int hashNid)
EVP_MD_CTX * EVP_MD_CTX_new()
std::unique_ptr< EVP_MD_CTX, EvpMdCtxDeleter > EvpMdCtxUniquePtr
std::unique_ptr< folly::IOBuf > rsaPssSign(folly::ByteRange data, const folly::ssl::EvpPkeyUniquePtr &pkey, int hashNid)