| 1 | #pragma once |
| 2 | |
| 3 | #include <hpke/digest.h> |
| 4 | #include <hpke/hpke.h> |
| 5 | #include <hpke/signature.h> |
| 6 | |
| 7 | #include "openssl_common.h" |
| 8 | #include <openssl/evp.h> |
| 9 | #include <openssl/rsa.h> |
| 10 | |
| 11 | namespace mlspp::hpke { |
| 12 | |
| 13 | // XXX(RLB): There is a lot of code in RSASignature that is duplicated in |
| 14 | // EVPGroup. I have allowed this duplication rather than factoring it out |
| 15 | // because I would like to be able to cleanly remove RSA later. |
| 16 | struct RSASignature : public Signature |
| 17 | { |
| 18 | struct PublicKey : public Signature::PublicKey |
| 19 | { |
| 20 | explicit PublicKey(EVP_PKEY* pkey_in) |
| 21 | : pkey(pkey_in, typed_delete<EVP_PKEY>) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | ~PublicKey() override = default; |
| 26 | |
| 27 | typed_unique_ptr<EVP_PKEY> pkey; |
| 28 | }; |
| 29 | |
| 30 | struct PrivateKey : public Signature::PrivateKey |
| 31 | { |
| 32 | explicit PrivateKey(EVP_PKEY* pkey_in) |
| 33 | : pkey(pkey_in, typed_delete<EVP_PKEY>) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | ~PrivateKey() override = default; |
| 38 | |
| 39 | std::unique_ptr<Signature::PublicKey> public_key() const override |
| 40 | { |
| 41 | if (1 != EVP_PKEY_up_ref(pkey.get())) { |
| 42 | throw openssl_error(); |
| 43 | } |
| 44 | return std::make_unique<PublicKey>(pkey.get()); |
| 45 | } |
| 46 | |
| 47 | typed_unique_ptr<EVP_PKEY> pkey; |
| 48 | }; |
| 49 | |
| 50 | explicit RSASignature(Digest::ID digest) |
| 51 | : Signature(digest_to_sig(digest)) |
| 52 | , md(digest_to_md(digest)) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | std::unique_ptr<Signature::PrivateKey> generate_key_pair() const override; |
| 57 | |
| 58 | std::unique_ptr<Signature::PrivateKey> derive_key_pair( |
| 59 | const bytes& /*ikm*/) const override; |
| 60 | |
| 61 | static std::unique_ptr<Signature::PrivateKey> generate_key_pair(size_t bits); |
| 62 | |
| 63 | // TODO(rlb): Implement derive() with sizes |
| 64 | |
| 65 | bytes serialize(const Signature::PublicKey& pk) const override; |
| 66 | |
| 67 | std::unique_ptr<Signature::PublicKey> deserialize( |
| 68 | const bytes& enc) const override; |
| 69 | |
| 70 | bytes serialize_private(const Signature::PrivateKey& sk) const override; |
| 71 | |
| 72 | std::unique_ptr<Signature::PrivateKey> deserialize_private( |
| 73 | const bytes& skm) const override; |
| 74 | |
| 75 | bytes sign(const bytes& data, const Signature::PrivateKey& sk) const override; |
| 76 | |
| 77 | bool verify(const bytes& data, |
| 78 | const bytes& sig, |
| 79 | const Signature::PublicKey& pk) const override; |
| 80 | |
| 81 | std::unique_ptr<Signature::PrivateKey> import_jwk_private( |
| 82 | const std::string& json_str) const override; |
| 83 | std::unique_ptr<Signature::PublicKey> import_jwk( |
| 84 | const std::string& json_str) const override; |
| 85 | std::string export_jwk_private( |
| 86 | const Signature::PrivateKey& sk) const override; |
| 87 | std::string export_jwk(const Signature::PublicKey& pk) const override; |
| 88 | |
| 89 | private: |
| 90 | const EVP_MD* md; |
| 91 | |
| 92 | static const EVP_MD* digest_to_md(Digest::ID digest); |
| 93 | |
| 94 | static Signature::ID digest_to_sig(Digest::ID digest); |
| 95 | }; |
| 96 | |
| 97 | } // namespace mlspp::hpke |