Debian Codegraph @

mlspp/lib/hpke/src/rsa.cpp

raw symbols SCIP
1 #cxx . . $ `<file>/mlspp/lib/hpke/src/rsa.cpp`/include "rsa.h"cxx . . $ `<file>/mlspp/lib/hpke/src/rsa.h`/
2
3 #include "common.h"cxx . . $ `<file>/mlspp/lib/hpke/src/common.h`/
4 #include "openssl/rsa.h"
5 #include "openssl_common.h"cxx . . $ `<file>/mlspp/lib/hpke/src/openssl_common.h`/
6
7 namespace mlsppcxx . . $ mlspp/::hpkecxx . . $ mlspp/hpke/ {
8
9 stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<Signaturecxx . . $ mlspp/hpke/Signature#::PrivateKeycxx . . $ mlspp/hpke/Signature#PrivateKey#>
10 RSASignaturecxx . . $ mlspp/hpke/RSASignature#::generate_key_paircxx . . $ mlspp/hpke/RSASignature#generate_key_pair(8d27edcb683c805).() const
11 {
12 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Not implemented");
13 }
14
15 stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<Signaturecxx . . $ mlspp/hpke/Signature#::PrivateKeycxx . . $ mlspp/hpke/Signature#PrivateKey#>
16 RSASignaturecxx . . $ mlspp/hpke/RSASignature#::derive_key_paircxx . . $ mlspp/hpke/RSASignature#derive_key_pair(5746faca826a5ccd).(const bytescxx . . $ mlspp/bytes_ns/bytes#& /*ikm*/) const
17 {
18 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Not implemented");
19 }
20
21 stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<Signaturecxx . . $ mlspp/hpke/Signature#::PrivateKeycxx . . $ mlspp/hpke/Signature#PrivateKey#>
22 RSASignaturecxx . . $ mlspp/hpke/RSASignature#::generate_key_paircxx . . $ mlspp/hpke/RSASignature#generate_key_pair(d159d62625d1ec8f).(size_tcxx . . $ size_t# bits)
23 {
24 auto ctx = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EVP_PKEY_CTX_new_id(EVP_PKEY_RSAcxx . . $ `<external>/2497497914425334111/evp.h:63:9`!, nullptr));
25 if (ctx ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) {
26 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
27 }
28
29 if (EVP_PKEY_keygen_init(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()) <= 0) {
30 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
31 }
32
33 // NOLINTNEXTLINE(hicpp-signed-bitwise)
34 if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), static_cast<int>(bits)) <=
35 0) {
36 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
37 }
38
39 auto* pkey = static_cast<EVP_PKEY*>(nullptr);
40 if (EVP_PKEY_keygen(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), &pkey) <= 0) {
41 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
42 }
43
44 return stdcxx . . $ std/unique_ptr#unique_ptr(8bcafd3b0b660df8).::make_uniquecxx . . $ std/make_unique(5cd499e8540a10bb).<PrivateKeycxx . . $ mlspp/hpke/RSASignature#PrivateKey#>(pkey);
45 }
46
47 // TODO(rlb): Implement derive() with sizes
48
49 bytescxx . . $ mlspp/bytes_ns/bytes#
50 RSASignaturecxx . . $ mlspp/hpke/RSASignature#::serializecxx . . $ mlspp/hpke/RSASignature#serialize(85b79100656811f8).(const Signaturecxx . . $ mlspp/hpke/Signature#::PublicKeycxx . . $ mlspp/hpke/Signature#PublicKey#& pk) const
51 {
52 const auto& rpk = dynamic_cast<const PublicKeycxx . . $ mlspp/hpke/RSASignature#PublicKey#&>(pk);
53 const int len = i2d_PublicKey(rpk.pkeycxx . . $ mlspp/hpke/RSASignature#PublicKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), nullptr);
54 auto raw = bytescxx . . $ mlspp/bytes_ns/bytes#(len);
55 auto* data_ptr = raw.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).();
56 if (len != i2d_PublicKey(rpk.pkeycxx . . $ mlspp/hpke/RSASignature#PublicKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), &data_ptr)) {
57 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
58 }
59 return rawcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).;
60 }
61
62 stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<Signaturecxx . . $ mlspp/hpke/Signature#::PublicKeycxx . . $ mlspp/hpke/Signature#PublicKey#>
63 RSASignaturecxx . . $ mlspp/hpke/RSASignature#::deserializecxx . . $ mlspp/hpke/RSASignature#deserialize(321ec828a61140d).(const bytescxx . . $ mlspp/bytes_ns/bytes#& enc) const
64 {
65 const auto* data_ptr = enc.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).();
66 auto* pkey = d2i_PublicKey(
67 EVP_PKEY_RSAcxx . . $ `<external>/2497497914425334111/evp.h:63:9`!, nullptr, &data_ptr, static_cast<int>(enc.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()));
68 if (pkey == nullptr) {
69 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
70 }
71 return stdcxx . . $ std/unique_ptr#unique_ptr(8bcafd3b0b660df8).::make_uniquecxx . . $ std/make_unique(5cd499e8540a10bb).<RSASignaturecxx . . $ mlspp/hpke/RSASignature#::PublicKeycxx . . $ mlspp/hpke/RSASignature#PublicKey#>(pkey);
72 }
73
74 bytescxx . . $ mlspp/bytes_ns/bytes#
75 RSASignaturecxx . . $ mlspp/hpke/RSASignature#::serialize_privatecxx . . $ mlspp/hpke/RSASignature#serialize_private(46640a7490dcfaec).(const Signaturecxx . . $ mlspp/hpke/Signature#::PrivateKeycxx . . $ mlspp/hpke/Signature#PrivateKey#& sk) const
76 {
77 const auto& rsk = dynamic_cast<const PrivateKeycxx . . $ mlspp/hpke/RSASignature#PrivateKey#&>(sk);
78 const int len = i2d_PrivateKey(rsk.pkeycxx . . $ mlspp/hpke/RSASignature#PrivateKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), nullptr);
79 auto raw = bytescxx . . $ mlspp/bytes_ns/bytes#(len);
80 auto* data_ptr = raw.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).();
81 if (len != i2d_PrivateKey(rsk.pkeycxx . . $ mlspp/hpke/RSASignature#PrivateKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), &data_ptr)) {
82 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
83 }
84
85 return rawcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).;
86 }
87
88 stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<Signaturecxx . . $ mlspp/hpke/Signature#::PrivateKeycxx . . $ mlspp/hpke/Signature#PrivateKey#>
89 RSASignaturecxx . . $ mlspp/hpke/RSASignature#::deserialize_privatecxx . . $ mlspp/hpke/RSASignature#deserialize_private(5746faca826a5ccd).(const bytescxx . . $ mlspp/bytes_ns/bytes#& skm) const
90 {
91 const auto* data_ptr = skm.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).();
92 auto* pkey = d2i_PrivateKey(
93 EVP_PKEY_RSAcxx . . $ `<external>/2497497914425334111/evp.h:63:9`!, nullptr, &data_ptr, static_cast<int>(skm.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()));
94 if (pkey == nullptr) {
95 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
96 }
97 return stdcxx . . $ std/unique_ptr#unique_ptr(8bcafd3b0b660df8).::make_uniquecxx . . $ std/make_unique(5cd499e8540a10bb).<RSASignaturecxx . . $ mlspp/hpke/RSASignature#::PrivateKeycxx . . $ mlspp/hpke/RSASignature#PrivateKey#>(pkey);
98 }
99
100 bytescxx . . $ mlspp/bytes_ns/bytes#
101 RSASignaturecxx . . $ mlspp/hpke/RSASignature#::signcxx . . $ mlspp/hpke/RSASignature#sign(4ebbf3240e588aaf).(const bytescxx . . $ mlspp/bytes_ns/bytes#& data, const Signaturecxx . . $ mlspp/hpke/Signature#::PrivateKeycxx . . $ mlspp/hpke/Signature#PrivateKey#& sk) const
102 {
103 const auto& rsk = dynamic_cast<const PrivateKeycxx . . $ mlspp/hpke/RSASignature#PrivateKey#&>(sk);
104
105 auto ctx = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EVP_MD_CTX_createcxx . . $ `<external>/2497497914425334111/evp.h:725:9`!());
106 if (ctx ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) {
107 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
108 }
109
110 if (1 !=
111 EVP_DigestSignInit(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), nullptr, mdcxx . . $ mlspp/hpke/RSASignature#md., nullptr, rsk.pkeycxx . . $ mlspp/hpke/RSASignature#PrivateKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).())) {
112 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
113 }
114
115 size_tcxx . . $ size_t# siglen = EVP_PKEY_sizecxx . . $ `<external>/2497497914425334111/evp.h:1374:9`!(rsk.pkeycxx . . $ mlspp/hpke/RSASignature#PrivateKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).());
116 bytescxx . . $ mlspp/bytes_ns/bytes# sigcxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).(siglen);
117 if (1 != EVP_DigestSign(
118 ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), sig.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(), &siglen, data.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), data.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).())) {
119 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
120 }
121
122 sig.resizecxx . . $ mlspp/bytes_ns/bytes#resize(2c44cd906ae4e829).(siglen);
123 return sigcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).;
124 }
125
126 bool
127 RSASignaturecxx . . $ mlspp/hpke/RSASignature#::verifycxx . . $ mlspp/hpke/RSASignature#verify(c2ebb5c61451baa6).(const bytescxx . . $ mlspp/bytes_ns/bytes#& data,
128 const bytescxx . . $ mlspp/bytes_ns/bytes#& sig,
129 const Signaturecxx . . $ mlspp/hpke/Signature#::PublicKeycxx . . $ mlspp/hpke/Signature#PublicKey#& pk) const
130 {
131 const auto& rpk = dynamic_cast<const PublicKeycxx . . $ mlspp/hpke/RSASignature#PublicKey#&>(pk);
132
133 auto ctx = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EVP_MD_CTX_createcxx . . $ `<external>/2497497914425334111/evp.h:725:9`!());
134 if (ctx ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) {
135 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
136 }
137
138 if (1 !=
139 EVP_DigestVerifyInit(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), nullptr, mdcxx . . $ mlspp/hpke/RSASignature#md., nullptr, rpk.pkeycxx . . $ mlspp/hpke/RSASignature#PublicKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).())) {
140 throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).();
141 }
142
143 auto rv = EVP_DigestVerify(
144 ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), sig.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), sig.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).(), data.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), data.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).());
145
146 return rv == 1;
147 }
148
149 // TODO(RLB) Implement these methods. No concrete need, but might be nice for
150 // completeness.
151 stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<Signaturecxx . . $ mlspp/hpke/Signature#::PrivateKeycxx . . $ mlspp/hpke/Signature#PrivateKey#>
152 RSASignaturecxx . . $ mlspp/hpke/RSASignature#::import_jwk_privatecxx . . $ mlspp/hpke/RSASignature#import_jwk_private(8828a0bfc3408e3c).(const stdcxx . . $ std/::stringcxx . . $ std/string#& /* json_str */) const
153 {
154 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("not implemented");
155 }
156
157 stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<Signaturecxx . . $ mlspp/hpke/Signature#::PublicKeycxx . . $ mlspp/hpke/Signature#PublicKey#>
158 RSASignaturecxx . . $ mlspp/hpke/RSASignature#::import_jwkcxx . . $ mlspp/hpke/RSASignature#import_jwk(a53063c3da59064e).(const stdcxx . . $ std/::stringcxx . . $ std/string#& /* json_str */) const
159 {
160 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("not implemented");
161 }
162
163 stdcxx . . $ std/::stringcxx . . $ std/string#
164 RSASignaturecxx . . $ mlspp/hpke/RSASignature#::export_jwk_privatecxx . . $ mlspp/hpke/RSASignature#export_jwk_private(9eb010d7f7e5c82e).(const Signaturecxx . . $ mlspp/hpke/Signature#::PrivateKeycxx . . $ mlspp/hpke/Signature#PrivateKey#& /* sk */) const
165 {
166 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("not implemented");
167 }
168
169 stdcxx . . $ std/::stringcxx . . $ std/string#
170 RSASignaturecxx . . $ mlspp/hpke/RSASignature#::export_jwkcxx . . $ mlspp/hpke/RSASignature#export_jwk(f9fdd4f558be28e4).(const Signaturecxx . . $ mlspp/hpke/Signature#::PublicKeycxx . . $ mlspp/hpke/Signature#PublicKey#& /* pk */) const
171 {
172 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("not implemented");
173 }
174
175 const EVP_MD*
176 RSASignaturecxx . . $ mlspp/hpke/RSASignature#::digest_to_mdcxx . . $ mlspp/hpke/RSASignature#digest_to_md(1f8ed912f6c8613e).(Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID# digest)
177 {
178 // NOLINTNEXTLINE(hicpp-multiway-paths-covered)
179 switch (digest) {
180 case Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA256cxx . . $ mlspp/hpke/Digest#ID#SHA256.:
181 return EVP_sha256();
182 case Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA384cxx . . $ mlspp/hpke/Digest#ID#SHA384.:
183 return EVP_sha384();
184 case Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA512cxx . . $ mlspp/hpke/Digest#ID#SHA512.:
185 return EVP_sha512();
186 default:
187 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unsupported digest");
188 }
189 }
190
191 Signaturecxx . . $ mlspp/hpke/Signature#::IDcxx . . $ mlspp/hpke/Signature#ID#
192 RSASignaturecxx . . $ mlspp/hpke/RSASignature#::digest_to_sigcxx . . $ mlspp/hpke/RSASignature#digest_to_sig(2a00588ea9a0b069).(Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID# digest)
193 {
194 // NOLINTNEXTLINE(hicpp-multiway-paths-covered)
195 switch (digest) {
196 case Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA256cxx . . $ mlspp/hpke/Digest#ID#SHA256.:
197 return Signaturecxx . . $ mlspp/hpke/Signature#::IDcxx . . $ mlspp/hpke/Signature#ID#::RSA_SHA256cxx . . $ mlspp/hpke/Signature#ID#RSA_SHA256.;
198 case Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA384cxx . . $ mlspp/hpke/Digest#ID#SHA384.:
199 return Signaturecxx . . $ mlspp/hpke/Signature#::IDcxx . . $ mlspp/hpke/Signature#ID#::RSA_SHA384cxx . . $ mlspp/hpke/Signature#ID#RSA_SHA384.;
200 case Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA512cxx . . $ mlspp/hpke/Digest#ID#SHA512.:
201 return Signaturecxx . . $ mlspp/hpke/Signature#::IDcxx . . $ mlspp/hpke/Signature#ID#::RSA_SHA512cxx . . $ mlspp/hpke/Signature#ID#RSA_SHA512.;
202 default:
203 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unsupported digest");
204 }
205 }
206
207 } // namespace mlspp::hpke