Debian Codegraph @

mlspp/lib/hpke/src/rsa.cpp

raw symbols SCIP
1 #include "rsa.h"
2
3 #include "common.h"
4 #include "openssl/rsa.h"
5 #include "openssl_common.h"
6
7 namespace mlspp::hpke {
8
9 std::unique_ptr<Signature::PrivateKey>
10 RSASignature::generate_key_pair() const
11 {
12 throw std::runtime_error("Not implemented");
13 }
14
15 std::unique_ptr<Signature::PrivateKey>
16 RSASignature::derive_key_pair(const bytes& /*ikm*/) const
17 {
18 throw std::runtime_error("Not implemented");
19 }
20
21 std::unique_ptr<Signature::PrivateKey>
22 RSASignature::generate_key_pair(size_t bits)
23 {
24 auto ctx = make_typed_unique(EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, nullptr));
25 if (ctx == nullptr) {
26 throw openssl_error();
27 }
28
29 if (EVP_PKEY_keygen_init(ctx.get()) <= 0) {
30 throw openssl_error();
31 }
32
33 // NOLINTNEXTLINE(hicpp-signed-bitwise)
34 if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx.get(), static_cast<int>(bits)) <=
35 0) {
36 throw openssl_error();
37 }
38
39 auto* pkey = static_cast<EVP_PKEY*>(nullptr);
40 if (EVP_PKEY_keygen(ctx.get(), &pkey) <= 0) {
41 throw openssl_error();
42 }
43
44 return std::make_unique<PrivateKey>(pkey);
45 }
46
47 // TODO(rlb): Implement derive() with sizes
48
49 bytes
50 RSASignature::serialize(const Signature::PublicKey& pk) const
51 {
52 const auto& rpk = dynamic_cast<const PublicKey&>(pk);
53 const int len = i2d_PublicKey(rpk.pkey.get(), nullptr);
54 auto raw = bytes(len);
55 auto* data_ptr = raw.data();
56 if (len != i2d_PublicKey(rpk.pkey.get(), &data_ptr)) {
57 throw openssl_error();
58 }
59 return raw;
60 }
61
62 std::unique_ptr<Signature::PublicKey>
63 RSASignature::deserialize(const bytes& enc) const
64 {
65 const auto* data_ptr = enc.data();
66 auto* pkey = d2i_PublicKey(
67 EVP_PKEY_RSA, nullptr, &data_ptr, static_cast<int>(enc.size()));
68 if (pkey == nullptr) {
69 throw openssl_error();
70 }
71 return std::make_unique<RSASignature::PublicKey>(pkey);
72 }
73
74 bytes
75 RSASignature::serialize_private(const Signature::PrivateKey& sk) const
76 {
77 const auto& rsk = dynamic_cast<const PrivateKey&>(sk);
78 const int len = i2d_PrivateKey(rsk.pkey.get(), nullptr);
79 auto raw = bytes(len);
80 auto* data_ptr = raw.data();
81 if (len != i2d_PrivateKey(rsk.pkey.get(), &data_ptr)) {
82 throw openssl_error();
83 }
84
85 return raw;
86 }
87
88 std::unique_ptr<Signature::PrivateKey>
89 RSASignature::deserialize_private(const bytes& skm) const
90 {
91 const auto* data_ptr = skm.data();
92 auto* pkey = d2i_PrivateKey(
93 EVP_PKEY_RSA, nullptr, &data_ptr, static_cast<int>(skm.size()));
94 if (pkey == nullptr) {
95 throw openssl_error();
96 }
97 return std::make_unique<RSASignature::PrivateKey>(pkey);
98 }
99
100 bytes
101 RSASignature::sign(const bytes& data, const Signature::PrivateKey& sk) const
102 {
103 const auto& rsk = dynamic_cast<const PrivateKey&>(sk);
104
105 auto ctx = make_typed_unique(EVP_MD_CTX_create());
106 if (ctx == nullptr) {
107 throw openssl_error();
108 }
109
110 if (1 !=
111 EVP_DigestSignInit(ctx.get(), nullptr, md, nullptr, rsk.pkey.get())) {
112 throw openssl_error();
113 }
114
115 size_t siglen = EVP_PKEY_size(rsk.pkey.get());
116 bytes sig(siglen);
117 if (1 != EVP_DigestSign(
118 ctx.get(), sig.data(), &siglen, data.data(), data.size())) {
119 throw openssl_error();
120 }
121
122 sig.resize(siglen);
123 return sig;
124 }
125
126 bool
127 RSASignature::verify(const bytes& data,
128 const bytes& sig,
129 const Signature::PublicKey& pk) const
130 {
131 const auto& rpk = dynamic_cast<const PublicKey&>(pk);
132
133 auto ctx = make_typed_unique(EVP_MD_CTX_create());
134 if (ctx == nullptr) {
135 throw openssl_error();
136 }
137
138 if (1 !=
139 EVP_DigestVerifyInit(ctx.get(), nullptr, md, nullptr, rpk.pkey.get())) {
140 throw openssl_error();
141 }
142
143 auto rv = EVP_DigestVerify(
144 ctx.get(), sig.data(), sig.size(), data.data(), data.size());
145
146 return rv == 1;
147 }
148
149 // TODO(RLB) Implement these methods. No concrete need, but might be nice for
150 // completeness.
151 std::unique_ptr<Signature::PrivateKey>
152 RSASignature::import_jwk_private(const std::string& /* json_str */) const
153 {
154 throw std::runtime_error("not implemented");
155 }
156
157 std::unique_ptr<Signature::PublicKey>
158 RSASignature::import_jwk(const std::string& /* json_str */) const
159 {
160 throw std::runtime_error("not implemented");
161 }
162
163 std::string
164 RSASignature::export_jwk_private(const Signature::PrivateKey& /* sk */) const
165 {
166 throw std::runtime_error("not implemented");
167 }
168
169 std::string
170 RSASignature::export_jwk(const Signature::PublicKey& /* pk */) const
171 {
172 throw std::runtime_error("not implemented");
173 }
174
175 const EVP_MD*
176 RSASignature::digest_to_md(Digest::ID digest)
177 {
178 // NOLINTNEXTLINE(hicpp-multiway-paths-covered)
179 switch (digest) {
180 case Digest::ID::SHA256:
181 return EVP_sha256();
182 case Digest::ID::SHA384:
183 return EVP_sha384();
184 case Digest::ID::SHA512:
185 return EVP_sha512();
186 default:
187 throw std::runtime_error("Unsupported digest");
188 }
189 }
190
191 Signature::ID
192 RSASignature::digest_to_sig(Digest::ID digest)
193 {
194 // NOLINTNEXTLINE(hicpp-multiway-paths-covered)
195 switch (digest) {
196 case Digest::ID::SHA256:
197 return Signature::ID::RSA_SHA256;
198 case Digest::ID::SHA384:
199 return Signature::ID::RSA_SHA384;
200 case Digest::ID::SHA512:
201 return Signature::ID::RSA_SHA512;
202 default:
203 throw std::runtime_error("Unsupported digest");
204 }
205 }
206
207 } // namespace mlspp::hpke