proxygen
RSAPSSSignatureTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #include <gtest/gtest.h>
10 
12 
13 using namespace folly;
14 using namespace folly::ssl;
15 using namespace testing;
16 
17 namespace fizz {
18 namespace test {
19 
20 class RSAPSSTest : public Test {
21  void SetUp() override {
22  OpenSSL_add_all_algorithms();
23  }
24 };
25 
27  std::unique_ptr<
28  EVP_PKEY_CTX,
30  ctx(EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, nullptr));
31  EVP_PKEY_keygen_init(ctx.get());
32  EVP_PKEY_CTX_set_rsa_keygen_bits(ctx.get(), 2048);
33  EVP_PKEY* keyPtr{nullptr};
34  EVP_PKEY_keygen(ctx.get(), &keyPtr);
35  return EvpPkeyUniquePtr(keyPtr);
36 }
37 
38 TEST_F(RSAPSSTest, TestSignVerify) {
40  rsa.setKey(generateKey());
41  static constexpr StringPiece msg{"message"};
42  auto sig = rsa.sign<SignatureScheme::rsa_pss_sha256>(msg);
43  rsa.verify<SignatureScheme::rsa_pss_sha256>(msg, sig->coalesce());
44 }
45 
46 TEST_F(RSAPSSTest, TestVerifyDifferent) {
48  rsa.setKey(generateKey());
49  static constexpr StringPiece msg1{"message"};
50  static constexpr StringPiece msg2{"somethingelse"};
51  auto sig = rsa.sign<SignatureScheme::rsa_pss_sha256>(msg1);
53  rsa.verify<SignatureScheme::rsa_pss_sha256>(msg2, sig->coalesce()),
54  std::runtime_error);
55 }
56 
57 TEST_F(RSAPSSTest, TestVerifyFailure) {
59  rsa.setKey(generateKey());
60  static constexpr StringPiece msg{"message"};
61  auto sig = rsa.sign<SignatureScheme::rsa_pss_sha256>(msg);
62  sig->writableData()[1] ^= 0x2;
64  rsa.verify<SignatureScheme::rsa_pss_sha256>(msg, sig->coalesce()),
65  std::runtime_error);
66 }
67 } // namespace test
68 } // namespace fizz
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
std::unique_ptr< folly::IOBuf > sign(folly::ByteRange data) const
Definition: Signature-inl.h:69
std::unique_ptr< EVP_PKEY, EvpPkeyDeleter > EvpPkeyUniquePtr
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void verify(folly::ByteRange data, folly::ByteRange signature) const
Definition: Signature-inl.h:86
constexpr detail::Sig< Sig > const sig
Definition: Poly.h:1165
Definition: Actions.h:16
TEST_F(AsyncSSLSocketWriteTest, write_coalescing1)
static EvpPkeyUniquePtr generateKey()
void setKey(folly::ssl::EvpPkeyUniquePtr pkey)