/src/Botan-3.13.0/src/lib/ffi/ffi_pkey_algs.cpp
Line | Count | Source |
1 | | /* |
2 | | * (C) 2015,2017 Jack Lloyd |
3 | | * (C) 2017 Ribose Inc |
4 | | * (C) 2018 René Korthaus, Rohde & Schwarz Cybersecurity |
5 | | * |
6 | | * Botan is released under the Simplified BSD License (see license.txt) |
7 | | */ |
8 | | |
9 | | #include <botan/ffi.h> |
10 | | |
11 | | #include <botan/assert.h> |
12 | | #include <botan/ec_group.h> |
13 | | #include <botan/hash.h> |
14 | | #include <botan/mem_ops.h> |
15 | | #include <botan/pem.h> |
16 | | #include <botan/internal/ffi_ec.h> |
17 | | #include <botan/internal/ffi_mp.h> |
18 | | #include <botan/internal/ffi_pkey.h> |
19 | | #include <botan/internal/ffi_rng.h> |
20 | | #include <botan/internal/ffi_util.h> |
21 | | |
22 | | #if defined(BOTAN_HAS_DL_GROUP) |
23 | | #include <botan/dl_group.h> |
24 | | #endif |
25 | | |
26 | | #if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO) |
27 | | #include <botan/ecc_key.h> |
28 | | #endif |
29 | | |
30 | | #if defined(BOTAN_HAS_RSA) |
31 | | #include <botan/rsa.h> |
32 | | #endif |
33 | | |
34 | | #if defined(BOTAN_HAS_ELGAMAL) |
35 | | #include <botan/elgamal.h> |
36 | | #endif |
37 | | |
38 | | #if defined(BOTAN_HAS_DSA) |
39 | | #include <botan/dsa.h> |
40 | | #endif |
41 | | |
42 | | #if defined(BOTAN_HAS_ECDSA) |
43 | | #include <botan/ecdsa.h> |
44 | | #endif |
45 | | |
46 | | #if defined(BOTAN_HAS_SM2) |
47 | | #include <botan/sm2.h> |
48 | | #endif |
49 | | |
50 | | #if defined(BOTAN_HAS_ECDH) |
51 | | #include <botan/ecdh.h> |
52 | | #endif |
53 | | |
54 | | #if defined(BOTAN_HAS_X25519) |
55 | | #include <botan/x25519.h> |
56 | | #endif |
57 | | |
58 | | #if defined(BOTAN_HAS_X448) |
59 | | #include <botan/x448.h> |
60 | | #endif |
61 | | |
62 | | #if defined(BOTAN_HAS_ED25519) |
63 | | #include <botan/ed25519.h> |
64 | | #endif |
65 | | |
66 | | #if defined(BOTAN_HAS_ED448) |
67 | | #include <botan/ed448.h> |
68 | | #endif |
69 | | |
70 | | #if defined(BOTAN_HAS_DIFFIE_HELLMAN) |
71 | | #include <botan/dh.h> |
72 | | #endif |
73 | | |
74 | | #if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S) |
75 | | #include <botan/kyber.h> |
76 | | #endif |
77 | | |
78 | | #if defined(BOTAN_HAS_ML_KEM) |
79 | | #include <botan/ml_kem.h> |
80 | | #endif |
81 | | |
82 | | #if defined(BOTAN_HAS_FRODOKEM) |
83 | | #include <botan/frodokem.h> |
84 | | #endif |
85 | | |
86 | | #if defined(BOTAN_HAS_ML_DSA) |
87 | | #include <botan/ml_dsa.h> |
88 | | #endif |
89 | | |
90 | | #if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE) |
91 | | #include <botan/slh_dsa.h> |
92 | | #endif |
93 | | |
94 | | #if defined(BOTAN_HAS_CLASSICMCELIECE) |
95 | | #include <botan/cmce.h> |
96 | | #endif |
97 | | |
98 | | namespace { |
99 | | |
100 | | #if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO) |
101 | | |
102 | | // These are always called within an existing try/catch block |
103 | | |
104 | | template <class ECPrivateKey_t> |
105 | 0 | int privkey_load_ec(std::unique_ptr<ECPrivateKey_t>& key, const Botan::BigInt& scalar, const char* curve_name) { |
106 | 0 | if(curve_name == nullptr) { |
107 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
108 | 0 | } |
109 | 0 | if(!Botan::EC_Group::supports_named_group(curve_name)) { |
110 | 0 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
111 | 0 | } |
112 | | |
113 | 0 | Botan::Null_RNG null_rng; |
114 | 0 | const auto grp = Botan::EC_Group::from_name(curve_name); |
115 | 0 | key.reset(new ECPrivateKey_t(null_rng, grp, scalar)); |
116 | 0 | return BOTAN_FFI_SUCCESS; |
117 | 0 | } Unexecuted instantiation: ffi_pkey_algs.cpp:int (anonymous namespace)::privkey_load_ec<Botan::ECDSA_PrivateKey>(std::__1::unique_ptr<Botan::ECDSA_PrivateKey, std::__1::default_delete<Botan::ECDSA_PrivateKey> >&, Botan::BigInt const&, char const*) Unexecuted instantiation: ffi_pkey_algs.cpp:int (anonymous namespace)::privkey_load_ec<Botan::ECDH_PrivateKey>(std::__1::unique_ptr<Botan::ECDH_PrivateKey, std::__1::default_delete<Botan::ECDH_PrivateKey> >&, Botan::BigInt const&, char const*) Unexecuted instantiation: ffi_pkey_algs.cpp:int (anonymous namespace)::privkey_load_ec<Botan::SM2_PrivateKey>(std::__1::unique_ptr<Botan::SM2_PrivateKey, std::__1::default_delete<Botan::SM2_PrivateKey> >&, Botan::BigInt const&, char const*) |
118 | | |
119 | | template <class ECPublicKey_t> |
120 | | int pubkey_load_ec(std::unique_ptr<ECPublicKey_t>& key, |
121 | | const Botan::BigInt& public_x, |
122 | | const Botan::BigInt& public_y, |
123 | 165k | const char* curve_name) { |
124 | 165k | if(curve_name == nullptr) { |
125 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
126 | 0 | } |
127 | | |
128 | 165k | if(!Botan::EC_Group::supports_named_group(curve_name)) { |
129 | 10 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
130 | 10 | } |
131 | | |
132 | 165k | const auto group = Botan::EC_Group::from_name(curve_name); |
133 | | |
134 | 165k | if(auto pt = Botan::EC_AffinePoint::from_bigint_xy(group, public_x, public_y)) { |
135 | 163k | key.reset(new ECPublicKey_t(group, pt.value())); |
136 | 163k | return BOTAN_FFI_SUCCESS; |
137 | 163k | } else { |
138 | 1.67k | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
139 | 1.67k | } |
140 | 165k | } ffi_pkey_algs.cpp:int (anonymous namespace)::pubkey_load_ec<Botan::ECDSA_PublicKey>(std::__1::unique_ptr<Botan::ECDSA_PublicKey, std::__1::default_delete<Botan::ECDSA_PublicKey> >&, Botan::BigInt const&, Botan::BigInt const&, char const*) Line | Count | Source | 123 | 117k | const char* curve_name) { | 124 | 117k | if(curve_name == nullptr) { | 125 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; | 126 | 0 | } | 127 | | | 128 | 117k | if(!Botan::EC_Group::supports_named_group(curve_name)) { | 129 | 3 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; | 130 | 3 | } | 131 | | | 132 | 117k | const auto group = Botan::EC_Group::from_name(curve_name); | 133 | | | 134 | 117k | if(auto pt = Botan::EC_AffinePoint::from_bigint_xy(group, public_x, public_y)) { | 135 | 117k | key.reset(new ECPublicKey_t(group, pt.value())); | 136 | 117k | return BOTAN_FFI_SUCCESS; | 137 | 117k | } else { | 138 | 413 | return BOTAN_FFI_ERROR_BAD_PARAMETER; | 139 | 413 | } | 140 | 117k | } |
Unexecuted instantiation: ffi_pkey_algs.cpp:int (anonymous namespace)::pubkey_load_ec<Botan::ECDH_PublicKey>(std::__1::unique_ptr<Botan::ECDH_PublicKey, std::__1::default_delete<Botan::ECDH_PublicKey> >&, Botan::BigInt const&, Botan::BigInt const&, char const*) ffi_pkey_algs.cpp:int (anonymous namespace)::pubkey_load_ec<Botan::SM2_PublicKey>(std::__1::unique_ptr<Botan::SM2_PublicKey, std::__1::default_delete<Botan::SM2_PublicKey> >&, Botan::BigInt const&, Botan::BigInt const&, char const*) Line | Count | Source | 123 | 47.4k | const char* curve_name) { | 124 | 47.4k | if(curve_name == nullptr) { | 125 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; | 126 | 0 | } | 127 | | | 128 | 47.4k | if(!Botan::EC_Group::supports_named_group(curve_name)) { | 129 | 7 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; | 130 | 7 | } | 131 | | | 132 | 47.4k | const auto group = Botan::EC_Group::from_name(curve_name); | 133 | | | 134 | 47.4k | if(auto pt = Botan::EC_AffinePoint::from_bigint_xy(group, public_x, public_y)) { | 135 | 46.2k | key.reset(new ECPublicKey_t(group, pt.value())); | 136 | 46.2k | return BOTAN_FFI_SUCCESS; | 137 | 46.2k | } else { | 138 | 1.26k | return BOTAN_FFI_ERROR_BAD_PARAMETER; | 139 | 1.26k | } | 140 | 47.4k | } |
|
141 | | |
142 | | template <class ECPublicKey_t> |
143 | | int pubkey_load_ec_sec1(std::unique_ptr<ECPublicKey_t>& key, |
144 | | std::span<const uint8_t> sec1, |
145 | 4.19k | std::string_view curve_name) { |
146 | 4.19k | if(!Botan::EC_Group::supports_named_group(curve_name)) { |
147 | 0 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
148 | 0 | } |
149 | | |
150 | 4.19k | const auto group = Botan::EC_Group::from_name(curve_name); |
151 | | |
152 | 4.19k | if(auto pt = Botan::EC_AffinePoint::deserialize(group, sec1)) { |
153 | 4.19k | key.reset(new ECPublicKey_t(group, pt.value())); |
154 | 4.19k | return BOTAN_FFI_SUCCESS; |
155 | 4.19k | } else { |
156 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
157 | 0 | } |
158 | 4.19k | } ffi_pkey_algs.cpp:int (anonymous namespace)::pubkey_load_ec_sec1<Botan::ECDSA_PublicKey>(std::__1::unique_ptr<Botan::ECDSA_PublicKey, std::__1::default_delete<Botan::ECDSA_PublicKey> >&, std::__1::span<unsigned char const, 18446744073709551615ul>, std::__1::basic_string_view<char, std::__1::char_traits<char> >) Line | Count | Source | 145 | 4.19k | std::string_view curve_name) { | 146 | 4.19k | if(!Botan::EC_Group::supports_named_group(curve_name)) { | 147 | 0 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; | 148 | 0 | } | 149 | | | 150 | 4.19k | const auto group = Botan::EC_Group::from_name(curve_name); | 151 | | | 152 | 4.19k | if(auto pt = Botan::EC_AffinePoint::deserialize(group, sec1)) { | 153 | 4.19k | key.reset(new ECPublicKey_t(group, pt.value())); | 154 | 4.19k | return BOTAN_FFI_SUCCESS; | 155 | 4.19k | } else { | 156 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; | 157 | 0 | } | 158 | 4.19k | } |
Unexecuted instantiation: ffi_pkey_algs.cpp:int (anonymous namespace)::pubkey_load_ec_sec1<Botan::ECDH_PublicKey>(std::__1::unique_ptr<Botan::ECDH_PublicKey, std::__1::default_delete<Botan::ECDH_PublicKey> >&, std::__1::span<unsigned char const, 18446744073709551615ul>, std::__1::basic_string_view<char, std::__1::char_traits<char> >) Unexecuted instantiation: ffi_pkey_algs.cpp:int (anonymous namespace)::pubkey_load_ec_sec1<Botan::SM2_PublicKey>(std::__1::unique_ptr<Botan::SM2_PublicKey, std::__1::default_delete<Botan::SM2_PublicKey> >&, std::__1::span<unsigned char const, 18446744073709551615ul>, std::__1::basic_string_view<char, std::__1::char_traits<char> >) |
159 | | |
160 | | #endif |
161 | | |
162 | 0 | Botan::BigInt pubkey_get_field(const Botan::Public_Key& key, std::string_view field) { |
163 | 0 | #if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO) |
164 | | // Not currently handled by get_int_field |
165 | 0 | if(const Botan::EC_PublicKey* ecc = dynamic_cast<const Botan::EC_PublicKey*>(&key)) { |
166 | 0 | if(field == "public_x") { |
167 | 0 | return Botan::BigInt::from_bytes(ecc->_public_ec_point().x_bytes()); |
168 | 0 | } else if(field == "public_y") { |
169 | 0 | return Botan::BigInt::from_bytes(ecc->_public_ec_point().y_bytes()); |
170 | 0 | } |
171 | 0 | } |
172 | 0 | #endif |
173 | | |
174 | 0 | try { |
175 | 0 | return key.get_int_field(field); |
176 | 0 | } catch(Botan::Unknown_PK_Field_Name&) { |
177 | 0 | throw Botan_FFI::FFI_Error("Unknown key field", BOTAN_FFI_ERROR_BAD_PARAMETER); |
178 | 0 | } |
179 | 0 | } |
180 | | |
181 | 0 | Botan::BigInt privkey_get_field(const Botan::Private_Key& key, std::string_view field) { |
182 | 0 | #if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO) |
183 | | // Not currently handled by get_int_field |
184 | 0 | if(const Botan::EC_PublicKey* ecc = dynamic_cast<const Botan::EC_PublicKey*>(&key)) { |
185 | 0 | if(field == "public_x") { |
186 | 0 | return Botan::BigInt::from_bytes(ecc->_public_ec_point().x_bytes()); |
187 | 0 | } else if(field == "public_y") { |
188 | 0 | return Botan::BigInt::from_bytes(ecc->_public_ec_point().y_bytes()); |
189 | 0 | } |
190 | 0 | } |
191 | 0 | #endif |
192 | | |
193 | 0 | try { |
194 | 0 | return key.get_int_field(field); |
195 | 0 | } catch(Botan::Unknown_PK_Field_Name&) { |
196 | 0 | throw Botan_FFI::FFI_Error("Unknown key field", BOTAN_FFI_ERROR_BAD_PARAMETER); |
197 | 0 | } |
198 | 0 | } |
199 | | |
200 | | } // namespace |
201 | | |
202 | | extern "C" { |
203 | | |
204 | | using namespace Botan_FFI; |
205 | | |
206 | 0 | int botan_pubkey_get_field(botan_mp_t output, botan_pubkey_t key, const char* field_name_cstr) { |
207 | 0 | if(field_name_cstr == nullptr) { |
208 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
209 | 0 | } |
210 | | |
211 | 0 | const std::string field_name(field_name_cstr); |
212 | |
|
213 | 0 | return BOTAN_FFI_VISIT(key, [=](const auto& k) { safe_get(output) = pubkey_get_field(k, field_name); }); |
214 | 0 | } |
215 | | |
216 | 0 | int botan_privkey_get_field(botan_mp_t output, botan_privkey_t key, const char* field_name_cstr) { |
217 | 0 | if(field_name_cstr == nullptr) { |
218 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
219 | 0 | } |
220 | | |
221 | 0 | const std::string field_name(field_name_cstr); |
222 | |
|
223 | 0 | return BOTAN_FFI_VISIT(key, [=](const auto& k) { safe_get(output) = privkey_get_field(k, field_name); }); |
224 | 0 | } |
225 | | |
226 | | /* RSA specific operations */ |
227 | | |
228 | 0 | int botan_privkey_create_rsa(botan_privkey_t* key_obj, botan_rng_t rng_obj, size_t n_bits) { |
229 | 0 | if(n_bits < 1024 || n_bits > 16 * 1024) { |
230 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
231 | 0 | } |
232 | | |
233 | 0 | const std::string n_str = std::to_string(n_bits); |
234 | |
|
235 | 0 | return botan_privkey_create(key_obj, "RSA", n_str.c_str(), rng_obj); |
236 | 0 | } |
237 | | |
238 | 0 | int botan_privkey_load_rsa(botan_privkey_t* key, botan_mp_t rsa_p, botan_mp_t rsa_q, botan_mp_t rsa_e) { |
239 | 0 | #if defined(BOTAN_HAS_RSA) |
240 | 0 | if(key == nullptr) { |
241 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
242 | 0 | } |
243 | 0 | *key = nullptr; |
244 | |
|
245 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
246 | 0 | auto rsa = std::make_unique<Botan::RSA_PrivateKey>(safe_get(rsa_p), safe_get(rsa_q), safe_get(rsa_e)); |
247 | 0 | return ffi_new_object(key, std::move(rsa)); |
248 | 0 | }); |
249 | | #else |
250 | | BOTAN_UNUSED(key, rsa_p, rsa_q, rsa_e); |
251 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
252 | | #endif |
253 | 0 | } |
254 | | |
255 | 0 | int botan_privkey_load_rsa_pkcs1(botan_privkey_t* key, const uint8_t bits[], size_t len) { |
256 | 0 | #if defined(BOTAN_HAS_RSA) |
257 | 0 | if(Botan::any_null_pointers(key, bits)) { |
258 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
259 | 0 | } |
260 | 0 | *key = nullptr; |
261 | |
|
262 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
263 | 0 | const Botan::AlgorithmIdentifier alg_id("RSA", Botan::AlgorithmIdentifier::USE_NULL_PARAM); |
264 | 0 | auto rsa = std::make_unique<Botan::RSA_PrivateKey>(alg_id, std::span{bits, len}); |
265 | 0 | return ffi_new_object(key, std::move(rsa)); |
266 | 0 | }); |
267 | | #else |
268 | | BOTAN_UNUSED(key, bits, len); |
269 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
270 | | #endif |
271 | 0 | } |
272 | | |
273 | 53.5k | int botan_pubkey_load_rsa(botan_pubkey_t* key, botan_mp_t n, botan_mp_t e) { |
274 | 53.5k | #if defined(BOTAN_HAS_RSA) |
275 | 53.5k | if(key == nullptr) { |
276 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
277 | 0 | } |
278 | 53.5k | *key = nullptr; |
279 | 53.5k | return ffi_guard_thunk(__func__, [=]() -> int { |
280 | 53.5k | auto rsa = std::make_unique<Botan::RSA_PublicKey>(safe_get(n), safe_get(e)); |
281 | 53.5k | return ffi_new_object(key, std::move(rsa)); |
282 | 53.5k | }); |
283 | | #else |
284 | | BOTAN_UNUSED(key, n, e); |
285 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
286 | | #endif |
287 | 53.5k | } |
288 | | |
289 | 0 | int botan_pubkey_load_rsa_pkcs1(botan_pubkey_t* key, const uint8_t bits[], size_t len) { |
290 | 0 | #if defined(BOTAN_HAS_RSA) |
291 | 0 | if(Botan::any_null_pointers(key, bits)) { |
292 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
293 | 0 | } |
294 | 0 | *key = nullptr; |
295 | |
|
296 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
297 | 0 | const Botan::AlgorithmIdentifier alg_id("RSA", Botan::AlgorithmIdentifier::USE_NULL_PARAM); |
298 | 0 | auto rsa = std::make_unique<Botan::RSA_PublicKey>(alg_id, std::span{bits, len}); |
299 | 0 | return ffi_new_object(key, std::move(rsa)); |
300 | 0 | }); |
301 | | #else |
302 | | BOTAN_UNUSED(key, bits, len); |
303 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
304 | | #endif |
305 | 0 | } |
306 | | |
307 | 0 | int botan_privkey_rsa_get_p(botan_mp_t p, botan_privkey_t key) { |
308 | 0 | return botan_privkey_get_field(p, key, "p"); |
309 | 0 | } |
310 | | |
311 | 0 | int botan_privkey_rsa_get_q(botan_mp_t q, botan_privkey_t key) { |
312 | 0 | return botan_privkey_get_field(q, key, "q"); |
313 | 0 | } |
314 | | |
315 | 0 | int botan_privkey_rsa_get_n(botan_mp_t n, botan_privkey_t key) { |
316 | 0 | return botan_privkey_get_field(n, key, "n"); |
317 | 0 | } |
318 | | |
319 | 0 | int botan_privkey_rsa_get_e(botan_mp_t e, botan_privkey_t key) { |
320 | 0 | return botan_privkey_get_field(e, key, "e"); |
321 | 0 | } |
322 | | |
323 | 0 | int botan_privkey_rsa_get_d(botan_mp_t d, botan_privkey_t key) { |
324 | 0 | return botan_privkey_get_field(d, key, "d"); |
325 | 0 | } |
326 | | |
327 | 0 | int botan_pubkey_rsa_get_e(botan_mp_t e, botan_pubkey_t key) { |
328 | 0 | return botan_pubkey_get_field(e, key, "e"); |
329 | 0 | } |
330 | | |
331 | 0 | int botan_pubkey_rsa_get_n(botan_mp_t n, botan_pubkey_t key) { |
332 | 0 | return botan_pubkey_get_field(n, key, "n"); |
333 | 0 | } |
334 | | |
335 | 0 | int botan_privkey_rsa_get_privkey(botan_privkey_t rsa_key, uint8_t out[], size_t* out_len, uint32_t flags) { |
336 | 0 | #if defined(BOTAN_HAS_RSA) |
337 | 0 | return BOTAN_FFI_VISIT(rsa_key, [=](const auto& k) -> int { |
338 | 0 | if(const Botan::RSA_PrivateKey* rsa = dynamic_cast<const Botan::RSA_PrivateKey*>(&k)) { |
339 | 0 | if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER) { |
340 | 0 | return write_vec_output(out, out_len, rsa->private_key_bits()); |
341 | 0 | } else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM) { |
342 | | // TODO define new generic functions for this |
343 | 0 | return write_str_output(reinterpret_cast<char*>(out), |
344 | 0 | out_len, |
345 | 0 | Botan::PEM_Code::encode(rsa->private_key_bits(), "RSA PRIVATE KEY")); |
346 | 0 | } else { |
347 | 0 | return BOTAN_FFI_ERROR_BAD_FLAG; |
348 | 0 | } |
349 | 0 | } else { |
350 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
351 | 0 | } |
352 | 0 | }); |
353 | | #else |
354 | | BOTAN_UNUSED(rsa_key, out, out_len, flags); |
355 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
356 | | #endif |
357 | 0 | } |
358 | | |
359 | | /* DSA specific operations */ |
360 | 0 | int botan_privkey_create_dsa(botan_privkey_t* key, botan_rng_t rng_obj, size_t pbits, size_t qbits) { |
361 | 0 | #if defined(BOTAN_HAS_DSA) |
362 | |
|
363 | 0 | if(Botan::any_null_pointers(rng_obj, key)) { |
364 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
365 | 0 | } |
366 | | |
367 | 0 | if((pbits % 64 != 0) || (qbits % 8 != 0) || (pbits < 1024) || (pbits > 3072) || (qbits < 160) || (qbits > 256)) { |
368 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
369 | 0 | } |
370 | | |
371 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
372 | 0 | Botan::RandomNumberGenerator& rng = safe_get(rng_obj); |
373 | 0 | const Botan::DL_Group group(rng, Botan::DL_Group::Prime_Subgroup, pbits, qbits); |
374 | 0 | auto dsa = std::make_unique<Botan::DSA_PrivateKey>(rng, group); |
375 | 0 | return ffi_new_object(key, std::move(dsa)); |
376 | 0 | }); |
377 | | #else |
378 | | BOTAN_UNUSED(key, rng_obj, pbits, qbits); |
379 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
380 | | #endif |
381 | 0 | } |
382 | | |
383 | 0 | int botan_privkey_load_dsa(botan_privkey_t* key, botan_mp_t p, botan_mp_t q, botan_mp_t g, botan_mp_t x) { |
384 | 0 | #if defined(BOTAN_HAS_DSA) |
385 | 0 | if(key == nullptr) { |
386 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
387 | 0 | } |
388 | 0 | *key = nullptr; |
389 | |
|
390 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
391 | 0 | const Botan::DL_Group group(safe_get(p), safe_get(q), safe_get(g)); |
392 | 0 | auto dsa = std::make_unique<Botan::DSA_PrivateKey>(group, safe_get(x)); |
393 | 0 | return ffi_new_object(key, std::move(dsa)); |
394 | 0 | }); |
395 | | #else |
396 | | BOTAN_UNUSED(key, p, q, g, x); |
397 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
398 | | #endif |
399 | 0 | } |
400 | | |
401 | 13.6k | int botan_pubkey_load_dsa(botan_pubkey_t* key, botan_mp_t p, botan_mp_t q, botan_mp_t g, botan_mp_t y) { |
402 | 13.6k | #if defined(BOTAN_HAS_DSA) |
403 | 13.6k | if(key == nullptr) { |
404 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
405 | 0 | } |
406 | 13.6k | *key = nullptr; |
407 | | |
408 | 13.6k | return ffi_guard_thunk(__func__, [=]() -> int { |
409 | 13.6k | const Botan::DL_Group group(safe_get(p), safe_get(q), safe_get(g)); |
410 | 13.6k | auto dsa = std::make_unique<Botan::DSA_PublicKey>(group, safe_get(y)); |
411 | 13.6k | return ffi_new_object(key, std::move(dsa)); |
412 | 13.6k | }); |
413 | | #else |
414 | | BOTAN_UNUSED(key, p, q, g, y); |
415 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
416 | | #endif |
417 | 13.6k | } |
418 | | |
419 | 0 | int botan_privkey_dsa_get_x(botan_mp_t x, botan_privkey_t key) { |
420 | 0 | return botan_privkey_get_field(x, key, "x"); |
421 | 0 | } |
422 | | |
423 | 0 | int botan_pubkey_dsa_get_p(botan_mp_t p, botan_pubkey_t key) { |
424 | 0 | return botan_pubkey_get_field(p, key, "p"); |
425 | 0 | } |
426 | | |
427 | 0 | int botan_pubkey_dsa_get_q(botan_mp_t q, botan_pubkey_t key) { |
428 | 0 | return botan_pubkey_get_field(q, key, "q"); |
429 | 0 | } |
430 | | |
431 | 0 | int botan_pubkey_dsa_get_g(botan_mp_t g, botan_pubkey_t key) { |
432 | 0 | return botan_pubkey_get_field(g, key, "g"); |
433 | 0 | } |
434 | | |
435 | 0 | int botan_pubkey_dsa_get_y(botan_mp_t y, botan_pubkey_t key) { |
436 | 0 | return botan_pubkey_get_field(y, key, "y"); |
437 | 0 | } |
438 | | |
439 | 0 | int botan_privkey_create_ecdsa(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) { |
440 | 0 | return botan_privkey_create(key_obj, "ECDSA", param_str, rng_obj); |
441 | 0 | } |
442 | | |
443 | | /* ECDSA specific operations */ |
444 | | |
445 | 0 | int botan_pubkey_ecc_key_used_explicit_encoding(botan_pubkey_t key) { |
446 | 0 | #if defined(BOTAN_HAS_ECC_KEY) |
447 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
448 | 0 | const Botan::Public_Key& pub_key = safe_get(key); |
449 | 0 | const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&pub_key); |
450 | |
|
451 | 0 | if(ec_key == nullptr) { |
452 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
453 | 0 | } |
454 | | |
455 | 0 | return ec_key->domain().used_explicit_encoding() ? 1 : 0; |
456 | 0 | }); |
457 | | #else |
458 | | BOTAN_UNUSED(key); |
459 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
460 | | #endif |
461 | 0 | } |
462 | | |
463 | | // NOLINTBEGIN(misc-misplaced-const) |
464 | | |
465 | | int botan_pubkey_load_ecdsa(botan_pubkey_t* key, |
466 | | const botan_mp_t public_x, |
467 | | const botan_mp_t public_y, |
468 | 117k | const char* curve_name) { |
469 | 117k | #if defined(BOTAN_HAS_ECDSA) |
470 | 117k | if(Botan::any_null_pointers(key, curve_name)) { |
471 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
472 | 0 | } |
473 | 117k | *key = nullptr; |
474 | | |
475 | 117k | return ffi_guard_thunk(__func__, [=]() -> int { |
476 | 117k | std::unique_ptr<Botan::ECDSA_PublicKey> p_key; |
477 | | |
478 | 117k | const int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name); |
479 | 117k | if(rc == BOTAN_FFI_SUCCESS) { |
480 | 117k | ffi_new_object(key, std::move(p_key)); |
481 | 117k | } |
482 | | |
483 | 117k | return rc; |
484 | 117k | }); |
485 | | #else |
486 | | BOTAN_UNUSED(key, public_x, public_y, curve_name); |
487 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
488 | | #endif |
489 | 117k | } |
490 | | |
491 | 4.19k | int botan_pubkey_load_ecdsa_sec1(botan_pubkey_t* key, const uint8_t sec1[], size_t sec1_len, const char* curve_name) { |
492 | 4.19k | #if defined(BOTAN_HAS_ECDSA) |
493 | 4.19k | if(Botan::any_null_pointers(key, sec1, curve_name)) { |
494 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
495 | 0 | } |
496 | 4.19k | *key = nullptr; |
497 | | |
498 | 4.19k | return ffi_guard_thunk(__func__, [=]() -> int { |
499 | 4.19k | std::unique_ptr<Botan::ECDSA_PublicKey> p_key; |
500 | | |
501 | 4.19k | const int rc = pubkey_load_ec_sec1(p_key, {sec1, sec1_len}, curve_name); |
502 | 4.19k | if(rc == BOTAN_FFI_SUCCESS) { |
503 | 4.19k | ffi_new_object(key, std::move(p_key)); |
504 | 4.19k | } |
505 | | |
506 | 4.19k | return rc; |
507 | 4.19k | }); |
508 | | #else |
509 | | BOTAN_UNUSED(key, sec1, sec1_len, curve_name); |
510 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
511 | | #endif |
512 | 4.19k | } |
513 | | |
514 | 0 | int botan_privkey_load_ecdsa(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) { |
515 | 0 | #if defined(BOTAN_HAS_ECDSA) |
516 | 0 | if(Botan::any_null_pointers(key, curve_name)) { |
517 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
518 | 0 | } |
519 | 0 | *key = nullptr; |
520 | |
|
521 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
522 | 0 | std::unique_ptr<Botan::ECDSA_PrivateKey> p_key; |
523 | 0 | const int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name); |
524 | 0 | if(rc == BOTAN_FFI_SUCCESS) { |
525 | 0 | ffi_new_object(key, std::move(p_key)); |
526 | 0 | } |
527 | 0 | return rc; |
528 | 0 | }); |
529 | | #else |
530 | | BOTAN_UNUSED(key, scalar, curve_name); |
531 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
532 | | #endif |
533 | 0 | } |
534 | | |
535 | | /* ElGamal specific operations */ |
536 | 0 | int botan_privkey_create_elgamal(botan_privkey_t* key, botan_rng_t rng_obj, size_t pbits, size_t qbits) { |
537 | 0 | #if defined(BOTAN_HAS_ELGAMAL) |
538 | 0 | if(Botan::any_null_pointers(key, rng_obj)) { |
539 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
540 | 0 | } |
541 | 0 | *key = nullptr; |
542 | |
|
543 | 0 | if(pbits < 1024 || qbits < 160) { |
544 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
545 | 0 | } |
546 | | |
547 | 0 | const Botan::DL_Group::PrimeType prime_type = |
548 | 0 | ((pbits - 1) == qbits) ? Botan::DL_Group::Strong : Botan::DL_Group::Prime_Subgroup; |
549 | |
|
550 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
551 | 0 | Botan::RandomNumberGenerator& rng = safe_get(rng_obj); |
552 | 0 | const Botan::DL_Group group(rng, prime_type, pbits, qbits); |
553 | 0 | auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(rng, group); |
554 | 0 | return ffi_new_object(key, std::move(elg)); |
555 | 0 | }); |
556 | | #else |
557 | | BOTAN_UNUSED(key, rng_obj, pbits, qbits); |
558 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
559 | | #endif |
560 | 0 | } |
561 | | |
562 | 41 | int botan_pubkey_load_elgamal(botan_pubkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t y) { |
563 | 41 | #if defined(BOTAN_HAS_ELGAMAL) |
564 | 41 | if(key == nullptr) { |
565 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
566 | 0 | } |
567 | 41 | *key = nullptr; |
568 | 41 | return ffi_guard_thunk(__func__, [=]() -> int { |
569 | 41 | const Botan::DL_Group group(safe_get(p), safe_get(g)); |
570 | 41 | auto elg = std::make_unique<Botan::ElGamal_PublicKey>(group, safe_get(y)); |
571 | 41 | return ffi_new_object(key, std::move(elg)); |
572 | 41 | }); |
573 | | #else |
574 | | BOTAN_UNUSED(key, p, g, y); |
575 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
576 | | #endif |
577 | 41 | } |
578 | | |
579 | 0 | int botan_privkey_load_elgamal(botan_privkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t x) { |
580 | 0 | #if defined(BOTAN_HAS_ELGAMAL) |
581 | 0 | if(key == nullptr) { |
582 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
583 | 0 | } |
584 | 0 | *key = nullptr; |
585 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
586 | 0 | const Botan::DL_Group group(safe_get(p), safe_get(g)); |
587 | 0 | auto elg = std::make_unique<Botan::ElGamal_PrivateKey>(group, safe_get(x)); |
588 | 0 | return ffi_new_object(key, std::move(elg)); |
589 | 0 | }); |
590 | | #else |
591 | | BOTAN_UNUSED(key, p, g, x); |
592 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
593 | | #endif |
594 | 0 | } |
595 | | |
596 | | /* Diffie Hellman specific operations */ |
597 | | |
598 | 0 | int botan_privkey_create_dh(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) { |
599 | 0 | return botan_privkey_create(key_obj, "DH", param_str, rng_obj); |
600 | 0 | } |
601 | | |
602 | 0 | int botan_privkey_load_dh(botan_privkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t x) { |
603 | | #if defined(BOTAN_HAS_DIFFIE_HELLMAN) |
604 | | if(key == nullptr) { |
605 | | return BOTAN_FFI_ERROR_NULL_POINTER; |
606 | | } |
607 | | *key = nullptr; |
608 | | return ffi_guard_thunk(__func__, [=]() -> int { |
609 | | const Botan::DL_Group group(safe_get(p), safe_get(g)); |
610 | | auto dh = std::make_unique<Botan::DH_PrivateKey>(group, safe_get(x)); |
611 | | return ffi_new_object(key, std::move(dh)); |
612 | | }); |
613 | | #else |
614 | 0 | BOTAN_UNUSED(key, p, g, x); |
615 | 0 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
616 | 0 | #endif |
617 | 0 | } |
618 | | |
619 | 0 | int botan_pubkey_load_dh(botan_pubkey_t* key, botan_mp_t p, botan_mp_t g, botan_mp_t y) { |
620 | | #if defined(BOTAN_HAS_DIFFIE_HELLMAN) |
621 | | if(key == nullptr) { |
622 | | return BOTAN_FFI_ERROR_NULL_POINTER; |
623 | | } |
624 | | *key = nullptr; |
625 | | return ffi_guard_thunk(__func__, [=]() -> int { |
626 | | const Botan::DL_Group group(safe_get(p), safe_get(g)); |
627 | | auto dh = std::make_unique<Botan::DH_PublicKey>(group, safe_get(y)); |
628 | | return ffi_new_object(key, std::move(dh)); |
629 | | }); |
630 | | #else |
631 | 0 | BOTAN_UNUSED(key, p, g, y); |
632 | 0 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
633 | 0 | #endif |
634 | 0 | } |
635 | | |
636 | | /* ECDH + x25519/x448 specific operations */ |
637 | | |
638 | 0 | int botan_privkey_create_ecdh(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str) { |
639 | 0 | if(Botan::any_null_pointers(key_obj, param_str)) { |
640 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
641 | 0 | } |
642 | 0 | *key_obj = nullptr; |
643 | |
|
644 | 0 | const std::string params(param_str); |
645 | |
|
646 | 0 | if(params == "X25519" || params == "x25519" || params == "curve25519") { |
647 | 0 | return botan_privkey_create(key_obj, "X25519", "", rng_obj); |
648 | 0 | } |
649 | | |
650 | 0 | if(params == "X448" || params == "x448") { |
651 | 0 | return botan_privkey_create(key_obj, "X448", "", rng_obj); |
652 | 0 | } |
653 | | |
654 | 0 | return botan_privkey_create(key_obj, "ECDH", param_str, rng_obj); |
655 | 0 | } |
656 | | |
657 | | int botan_pubkey_load_ecdh(botan_pubkey_t* key, |
658 | | const botan_mp_t public_x, |
659 | | const botan_mp_t public_y, |
660 | 0 | const char* curve_name) { |
661 | 0 | #if defined(BOTAN_HAS_ECDH) |
662 | 0 | if(Botan::any_null_pointers(key, curve_name)) { |
663 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
664 | 0 | } |
665 | 0 | *key = nullptr; |
666 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
667 | 0 | std::unique_ptr<Botan::ECDH_PublicKey> p_key; |
668 | 0 | const int rc = pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name); |
669 | |
|
670 | 0 | if(rc == BOTAN_FFI_SUCCESS) { |
671 | 0 | ffi_new_object(key, std::move(p_key)); |
672 | 0 | } |
673 | 0 | return rc; |
674 | 0 | }); |
675 | | #else |
676 | | BOTAN_UNUSED(key, public_x, public_y, curve_name); |
677 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
678 | | #endif |
679 | 0 | } |
680 | | |
681 | 0 | int botan_pubkey_load_ecdh_sec1(botan_pubkey_t* key, const uint8_t sec1[], size_t sec1_len, const char* curve_name) { |
682 | 0 | #if defined(BOTAN_HAS_ECDH) |
683 | 0 | if(Botan::any_null_pointers(key, sec1, curve_name)) { |
684 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
685 | 0 | } |
686 | 0 | *key = nullptr; |
687 | |
|
688 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
689 | 0 | std::unique_ptr<Botan::ECDH_PublicKey> p_key; |
690 | |
|
691 | 0 | const int rc = pubkey_load_ec_sec1(p_key, {sec1, sec1_len}, curve_name); |
692 | 0 | if(rc == BOTAN_FFI_SUCCESS) { |
693 | 0 | ffi_new_object(key, std::move(p_key)); |
694 | 0 | } |
695 | |
|
696 | 0 | return rc; |
697 | 0 | }); |
698 | | #else |
699 | | BOTAN_UNUSED(key, sec1, sec1_len, curve_name); |
700 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
701 | | #endif |
702 | 0 | } |
703 | | |
704 | 0 | int botan_privkey_load_ecdh(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) { |
705 | 0 | #if defined(BOTAN_HAS_ECDH) |
706 | 0 | if(Botan::any_null_pointers(key, curve_name)) { |
707 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
708 | 0 | } |
709 | 0 | *key = nullptr; |
710 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
711 | 0 | std::unique_ptr<Botan::ECDH_PrivateKey> p_key; |
712 | 0 | const int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name); |
713 | 0 | if(rc == BOTAN_FFI_SUCCESS) { |
714 | 0 | ffi_new_object(key, std::move(p_key)); |
715 | 0 | } |
716 | 0 | return rc; |
717 | 0 | }); |
718 | | #else |
719 | | BOTAN_UNUSED(key, scalar, curve_name); |
720 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
721 | | #endif |
722 | 0 | } |
723 | | |
724 | | /* SM2 specific operations */ |
725 | | |
726 | | int botan_pubkey_sm2_compute_za( |
727 | 23.1k | uint8_t out[], size_t* out_len, const char* ident, const char* hash_algo, const botan_pubkey_t key) { |
728 | 23.1k | if(Botan::any_null_pointers(out, out_len, ident, hash_algo, key)) { |
729 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
730 | 0 | } |
731 | | |
732 | 23.1k | #if defined(BOTAN_HAS_SM2) |
733 | 23.1k | return ffi_guard_thunk(__func__, [=]() -> int { |
734 | 23.1k | const Botan::Public_Key& pub_key = safe_get(key); |
735 | 23.1k | const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&pub_key); |
736 | | |
737 | 23.1k | if(ec_key == nullptr) { |
738 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
739 | 0 | } |
740 | | |
741 | 23.1k | if(ec_key->algo_name() != "SM2") { |
742 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
743 | 0 | } |
744 | | |
745 | 23.1k | const std::string ident_str(ident); |
746 | 23.1k | std::unique_ptr<Botan::HashFunction> hash = Botan::HashFunction::create_or_throw(hash_algo); |
747 | | |
748 | 23.1k | const auto& pt = ec_key->_public_ec_point(); |
749 | | |
750 | 23.1k | const auto za = Botan::sm2_compute_za(*hash, ident_str, ec_key->domain(), pt); |
751 | | |
752 | 23.1k | return write_vec_output(out, out_len, za); |
753 | 23.1k | }); |
754 | | #else |
755 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
756 | | #endif |
757 | 23.1k | } |
758 | | |
759 | | int botan_pubkey_load_sm2(botan_pubkey_t* key, |
760 | | const botan_mp_t public_x, |
761 | | const botan_mp_t public_y, |
762 | 47.4k | const char* curve_name) { |
763 | 47.4k | #if defined(BOTAN_HAS_SM2) |
764 | 47.4k | if(Botan::any_null_pointers(key, curve_name)) { |
765 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
766 | 0 | } |
767 | 47.4k | *key = nullptr; |
768 | | |
769 | 47.4k | return ffi_guard_thunk(__func__, [=]() -> int { |
770 | 47.4k | std::unique_ptr<Botan::SM2_PublicKey> p_key; |
771 | 47.4k | if(pubkey_load_ec(p_key, safe_get(public_x), safe_get(public_y), curve_name) == 0) { |
772 | 46.2k | return ffi_new_object(key, std::move(p_key)); |
773 | 46.2k | } else { |
774 | 1.27k | return BOTAN_FFI_ERROR_UNKNOWN_ERROR; |
775 | 1.27k | } |
776 | 47.4k | }); |
777 | | #else |
778 | | BOTAN_UNUSED(key, public_x, public_y, curve_name); |
779 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
780 | | #endif |
781 | 47.4k | } |
782 | | |
783 | 0 | int botan_pubkey_load_sm2_sec1(botan_pubkey_t* key, const uint8_t sec1[], size_t sec1_len, const char* curve_name) { |
784 | 0 | #if defined(BOTAN_HAS_SM2) |
785 | 0 | if(Botan::any_null_pointers(key, sec1, curve_name)) { |
786 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
787 | 0 | } |
788 | 0 | *key = nullptr; |
789 | |
|
790 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
791 | 0 | std::unique_ptr<Botan::SM2_PublicKey> p_key; |
792 | |
|
793 | 0 | const int rc = pubkey_load_ec_sec1(p_key, {sec1, sec1_len}, curve_name); |
794 | 0 | if(rc == BOTAN_FFI_SUCCESS) { |
795 | 0 | ffi_new_object(key, std::move(p_key)); |
796 | 0 | } |
797 | |
|
798 | 0 | return rc; |
799 | 0 | }); |
800 | | #else |
801 | | BOTAN_UNUSED(key, sec1, sec1_len, curve_name); |
802 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
803 | | #endif |
804 | 0 | } |
805 | | |
806 | 0 | int botan_privkey_load_sm2(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) { |
807 | 0 | #if defined(BOTAN_HAS_SM2) |
808 | 0 | if(Botan::any_null_pointers(key, curve_name)) { |
809 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
810 | 0 | } |
811 | 0 | *key = nullptr; |
812 | |
|
813 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
814 | 0 | std::unique_ptr<Botan::SM2_PrivateKey> p_key; |
815 | 0 | const int rc = privkey_load_ec(p_key, safe_get(scalar), curve_name); |
816 | |
|
817 | 0 | if(rc == BOTAN_FFI_SUCCESS) { |
818 | 0 | ffi_new_object(key, std::move(p_key)); |
819 | 0 | } |
820 | 0 | return rc; |
821 | 0 | }); |
822 | | #else |
823 | | BOTAN_UNUSED(key, scalar, curve_name); |
824 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
825 | | #endif |
826 | 0 | } |
827 | | |
828 | | int botan_pubkey_load_sm2_enc(botan_pubkey_t* key, |
829 | | const botan_mp_t public_x, |
830 | | const botan_mp_t public_y, |
831 | 0 | const char* curve_name) { |
832 | 0 | return botan_pubkey_load_sm2(key, public_x, public_y, curve_name); |
833 | 0 | } |
834 | | |
835 | 0 | int botan_privkey_load_sm2_enc(botan_privkey_t* key, const botan_mp_t scalar, const char* curve_name) { |
836 | 0 | return botan_privkey_load_sm2(key, scalar, curve_name); |
837 | 0 | } |
838 | | |
839 | | /* EC key specific operations */ |
840 | | |
841 | 0 | int botan_ec_privkey_get_private_key(botan_privkey_t key, botan_ec_scalar_t* value) { |
842 | 0 | if(Botan::any_null_pointers(value)) { |
843 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
844 | 0 | } |
845 | 0 | #if defined(BOTAN_HAS_ECC_KEY) |
846 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
847 | 0 | const Botan::EC_PrivateKey* ec_key = dynamic_cast<const Botan::EC_PrivateKey*>(&safe_get(key)); |
848 | 0 | if(ec_key == nullptr) { |
849 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
850 | 0 | } |
851 | 0 | return ffi_new_object(value, std::make_unique<Botan::EC_Scalar>(ec_key->_private_key())); |
852 | 0 | }); |
853 | | #else |
854 | | BOTAN_UNUSED(key, value); |
855 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
856 | | #endif |
857 | 0 | } |
858 | | |
859 | 0 | int botan_ec_privkey_get_group(botan_privkey_t key, botan_ec_group_t* ec_group) { |
860 | 0 | if(Botan::any_null_pointers(ec_group)) { |
861 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
862 | 0 | } |
863 | | |
864 | 0 | #if defined(BOTAN_HAS_ECC_KEY) |
865 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
866 | 0 | const Botan::EC_PrivateKey* ec_key = dynamic_cast<const Botan::EC_PrivateKey*>(&safe_get(key)); |
867 | 0 | if(ec_key == nullptr) { |
868 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
869 | 0 | } |
870 | 0 | return ffi_new_object(ec_group, std::make_unique<Botan::EC_Group>(ec_key->domain())); |
871 | 0 | }); |
872 | | #else |
873 | | BOTAN_UNUSED(key, ec_group); |
874 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
875 | | #endif |
876 | 0 | } |
877 | | |
878 | 0 | int botan_ec_pubkey_get_group(botan_pubkey_t key, botan_ec_group_t* ec_group) { |
879 | 0 | if(Botan::any_null_pointers(ec_group)) { |
880 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
881 | 0 | } |
882 | 0 | #if defined(BOTAN_HAS_ECC_KEY) |
883 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
884 | 0 | const Botan::EC_PublicKey* ec_key = dynamic_cast<const Botan::EC_PublicKey*>(&safe_get(key)); |
885 | 0 | if(ec_key == nullptr) { |
886 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
887 | 0 | } |
888 | 0 | return ffi_new_object(ec_group, std::make_unique<Botan::EC_Group>(ec_key->domain())); |
889 | 0 | }); |
890 | | #else |
891 | | BOTAN_UNUSED(key, ec_group); |
892 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
893 | | #endif |
894 | 0 | } |
895 | | |
896 | | /* Ed25519 specific operations */ |
897 | | |
898 | 0 | int botan_privkey_load_ed25519(botan_privkey_t* key, const uint8_t privkey[32]) { |
899 | 0 | #if defined(BOTAN_HAS_ED25519) |
900 | 0 | if(any_null_pointers(key, privkey)) { |
901 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
902 | 0 | } |
903 | 0 | *key = nullptr; |
904 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
905 | 0 | auto ed25519 = |
906 | 0 | std::make_unique<Botan::Ed25519_PrivateKey>(Botan::Ed25519_PrivateKey::from_seed(std::span{privkey, 32})); |
907 | 0 | return ffi_new_object(key, std::move(ed25519)); |
908 | 0 | }); |
909 | | #else |
910 | | BOTAN_UNUSED(key, privkey); |
911 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
912 | | #endif |
913 | 0 | } |
914 | | |
915 | 83.3k | int botan_pubkey_load_ed25519(botan_pubkey_t* key, const uint8_t pubkey[32]) { |
916 | 83.3k | #if defined(BOTAN_HAS_ED25519) |
917 | 83.3k | if(any_null_pointers(key, pubkey)) { |
918 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
919 | 0 | } |
920 | 83.3k | *key = nullptr; |
921 | 83.3k | return ffi_guard_thunk(__func__, [=]() -> int { |
922 | 83.3k | const std::vector<uint8_t> pubkey_vec(pubkey, pubkey + 32); |
923 | 83.3k | auto ed25519 = std::make_unique<Botan::Ed25519_PublicKey>(pubkey_vec); |
924 | 83.3k | return ffi_new_object(key, std::move(ed25519)); |
925 | 83.3k | }); |
926 | | #else |
927 | | BOTAN_UNUSED(key, pubkey); |
928 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
929 | | #endif |
930 | 83.3k | } |
931 | | |
932 | 0 | int botan_privkey_ed25519_get_privkey(botan_privkey_t key, uint8_t output[64]) { |
933 | 0 | if(output == nullptr) { |
934 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
935 | 0 | } |
936 | 0 | #if defined(BOTAN_HAS_ED25519) |
937 | 0 | return BOTAN_FFI_VISIT(key, [=](const auto& k) { |
938 | 0 | if(auto ed = dynamic_cast<const Botan::Ed25519_PrivateKey*>(&k)) { |
939 | 0 | const auto ed_key = ed->raw_private_key_bits(); |
940 | 0 | if(ed_key.size() != 64) { |
941 | 0 | return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE; |
942 | 0 | } |
943 | 0 | Botan::copy_mem(output, ed_key.data(), ed_key.size()); |
944 | 0 | return BOTAN_FFI_SUCCESS; |
945 | 0 | } else { |
946 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
947 | 0 | } |
948 | 0 | }); |
949 | | #else |
950 | | BOTAN_UNUSED(key, output); |
951 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
952 | | #endif |
953 | 0 | } |
954 | | |
955 | 0 | int botan_pubkey_ed25519_get_pubkey(botan_pubkey_t key, uint8_t output[32]) { |
956 | 0 | if(output == nullptr) { |
957 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
958 | 0 | } |
959 | 0 | #if defined(BOTAN_HAS_ED25519) |
960 | 0 | return BOTAN_FFI_VISIT(key, [=](const auto& k) { |
961 | 0 | if(auto ed = dynamic_cast<const Botan::Ed25519_PublicKey*>(&k)) { |
962 | 0 | const std::vector<uint8_t>& ed_key = ed->get_public_key(); |
963 | 0 | if(ed_key.size() != 32) { |
964 | 0 | return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE; |
965 | 0 | } |
966 | 0 | Botan::copy_mem(output, ed_key.data(), ed_key.size()); |
967 | 0 | return BOTAN_FFI_SUCCESS; |
968 | 0 | } else { |
969 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
970 | 0 | } |
971 | 0 | }); |
972 | | #else |
973 | | BOTAN_UNUSED(key, output); |
974 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
975 | | #endif |
976 | 0 | } |
977 | | |
978 | | /* Ed448 specific operations */ |
979 | | |
980 | 0 | int botan_privkey_load_ed448(botan_privkey_t* key, const uint8_t privkey[57]) { |
981 | 0 | #if defined(BOTAN_HAS_ED448) |
982 | 0 | if(any_null_pointers(key, privkey)) { |
983 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
984 | 0 | } |
985 | 0 | *key = nullptr; |
986 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
987 | 0 | auto ed448 = std::make_unique<Botan::Ed448_PrivateKey>(std::span(privkey, 57)); |
988 | 0 | return ffi_new_object(key, std::move(ed448)); |
989 | 0 | }); |
990 | | #else |
991 | | BOTAN_UNUSED(key, privkey); |
992 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
993 | | #endif |
994 | 0 | } |
995 | | |
996 | 1.98k | int botan_pubkey_load_ed448(botan_pubkey_t* key, const uint8_t pubkey[57]) { |
997 | 1.98k | #if defined(BOTAN_HAS_ED448) |
998 | 1.98k | if(any_null_pointers(key, pubkey)) { |
999 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1000 | 0 | } |
1001 | 1.98k | *key = nullptr; |
1002 | 1.98k | return ffi_guard_thunk(__func__, [=]() -> int { |
1003 | 1.98k | auto ed448 = std::make_unique<Botan::Ed448_PublicKey>(std::span(pubkey, 57)); |
1004 | 1.98k | return ffi_new_object(key, std::move(ed448)); |
1005 | 1.98k | }); |
1006 | | #else |
1007 | | BOTAN_UNUSED(key, pubkey); |
1008 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1009 | | #endif |
1010 | 1.98k | } |
1011 | | |
1012 | 0 | int botan_privkey_ed448_get_privkey(botan_privkey_t key, uint8_t output[57]) { |
1013 | 0 | if(output == nullptr) { |
1014 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1015 | 0 | } |
1016 | 0 | #if defined(BOTAN_HAS_ED448) |
1017 | 0 | return BOTAN_FFI_VISIT(key, [=](const auto& k) { |
1018 | 0 | if(auto ed = dynamic_cast<const Botan::Ed448_PrivateKey*>(&k)) { |
1019 | 0 | const auto ed_key = ed->raw_private_key_bits(); |
1020 | 0 | Botan::copy_mem(std::span(output, 57), ed_key); |
1021 | 0 | return BOTAN_FFI_SUCCESS; |
1022 | 0 | } else { |
1023 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
1024 | 0 | } |
1025 | 0 | }); |
1026 | | #else |
1027 | | BOTAN_UNUSED(key, output); |
1028 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1029 | | #endif |
1030 | 0 | } |
1031 | | |
1032 | 0 | int botan_pubkey_ed448_get_pubkey(botan_pubkey_t key, uint8_t output[57]) { |
1033 | 0 | if(output == nullptr) { |
1034 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1035 | 0 | } |
1036 | 0 | #if defined(BOTAN_HAS_ED448) |
1037 | 0 | return BOTAN_FFI_VISIT(key, [=](const auto& k) { |
1038 | 0 | if(auto ed = dynamic_cast<const Botan::Ed448_PublicKey*>(&k)) { |
1039 | 0 | const auto ed_key = ed->public_key_bits(); |
1040 | 0 | Botan::copy_mem(std::span(output, 57), ed_key); |
1041 | 0 | return BOTAN_FFI_SUCCESS; |
1042 | 0 | } else { |
1043 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
1044 | 0 | } |
1045 | 0 | }); |
1046 | | #else |
1047 | | BOTAN_UNUSED(key, output); |
1048 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1049 | | #endif |
1050 | 0 | } |
1051 | | |
1052 | | /* X25519 specific operations */ |
1053 | | |
1054 | 0 | int botan_privkey_load_x25519(botan_privkey_t* key, const uint8_t privkey[32]) { |
1055 | 0 | #if defined(BOTAN_HAS_X25519) |
1056 | 0 | if(any_null_pointers(key, privkey)) { |
1057 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1058 | 0 | } |
1059 | 0 | *key = nullptr; |
1060 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
1061 | 0 | auto x25519 = std::make_unique<Botan::X25519_PrivateKey>(std::span{privkey, 32}); |
1062 | 0 | return ffi_new_object(key, std::move(x25519)); |
1063 | 0 | }); |
1064 | | #else |
1065 | | BOTAN_UNUSED(key, privkey); |
1066 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1067 | | #endif |
1068 | 0 | } |
1069 | | |
1070 | 0 | int botan_pubkey_load_x25519(botan_pubkey_t* key, const uint8_t pubkey[32]) { |
1071 | 0 | #if defined(BOTAN_HAS_X25519) |
1072 | 0 | if(any_null_pointers(key, pubkey)) { |
1073 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1074 | 0 | } |
1075 | 0 | *key = nullptr; |
1076 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
1077 | 0 | auto x25519 = std::make_unique<Botan::X25519_PublicKey>(std::span{pubkey, 32}); |
1078 | 0 | return ffi_new_object(key, std::move(x25519)); |
1079 | 0 | }); |
1080 | | #else |
1081 | | BOTAN_UNUSED(key, pubkey); |
1082 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1083 | | #endif |
1084 | 0 | } |
1085 | | |
1086 | 0 | int botan_privkey_x25519_get_privkey(botan_privkey_t key, uint8_t output[32]) { |
1087 | 0 | if(output == nullptr) { |
1088 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1089 | 0 | } |
1090 | 0 | #if defined(BOTAN_HAS_X25519) |
1091 | 0 | return BOTAN_FFI_VISIT(key, [=](const auto& k) { |
1092 | 0 | if(auto x25519 = dynamic_cast<const Botan::X25519_PrivateKey*>(&k)) { |
1093 | 0 | const auto x25519_key = x25519->raw_private_key_bits(); |
1094 | 0 | if(x25519_key.size() != 32) { |
1095 | 0 | return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE; |
1096 | 0 | } |
1097 | 0 | Botan::copy_mem(output, x25519_key.data(), x25519_key.size()); |
1098 | 0 | return BOTAN_FFI_SUCCESS; |
1099 | 0 | } else { |
1100 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
1101 | 0 | } |
1102 | 0 | }); |
1103 | | #else |
1104 | | BOTAN_UNUSED(key, output); |
1105 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1106 | | #endif |
1107 | 0 | } |
1108 | | |
1109 | 0 | int botan_pubkey_x25519_get_pubkey(botan_pubkey_t key, uint8_t output[32]) { |
1110 | 0 | if(output == nullptr) { |
1111 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1112 | 0 | } |
1113 | 0 | #if defined(BOTAN_HAS_X25519) |
1114 | 0 | return BOTAN_FFI_VISIT(key, [=](const auto& k) { |
1115 | 0 | if(auto x25519 = dynamic_cast<const Botan::X25519_PublicKey*>(&k)) { |
1116 | 0 | Botan::copy_mem(std::span{output, 32}, x25519->raw_public_key_bits()); |
1117 | 0 | return BOTAN_FFI_SUCCESS; |
1118 | 0 | } else { |
1119 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
1120 | 0 | } |
1121 | 0 | }); |
1122 | | #else |
1123 | | BOTAN_UNUSED(key, output); |
1124 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1125 | | #endif |
1126 | 0 | } |
1127 | | |
1128 | | /* X448 specific operations */ |
1129 | | |
1130 | 0 | int botan_privkey_load_x448(botan_privkey_t* key, const uint8_t privkey[56]) { |
1131 | 0 | #if defined(BOTAN_HAS_X448) |
1132 | 0 | if(any_null_pointers(key, privkey)) { |
1133 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1134 | 0 | } |
1135 | 0 | *key = nullptr; |
1136 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
1137 | 0 | auto x448 = std::make_unique<Botan::X448_PrivateKey>(std::span{privkey, 56}); |
1138 | 0 | return ffi_new_object(key, std::move(x448)); |
1139 | 0 | }); |
1140 | | #else |
1141 | | BOTAN_UNUSED(key, privkey); |
1142 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1143 | | #endif |
1144 | 0 | } |
1145 | | |
1146 | 0 | int botan_pubkey_load_x448(botan_pubkey_t* key, const uint8_t pubkey[56]) { |
1147 | 0 | #if defined(BOTAN_HAS_X448) |
1148 | 0 | if(any_null_pointers(key, pubkey)) { |
1149 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1150 | 0 | } |
1151 | 0 | *key = nullptr; |
1152 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
1153 | 0 | auto x448 = std::make_unique<Botan::X448_PublicKey>(std::span{pubkey, 56}); |
1154 | 0 | return ffi_new_object(key, std::move(x448)); |
1155 | 0 | }); |
1156 | | #else |
1157 | | BOTAN_UNUSED(key, pubkey); |
1158 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1159 | | #endif |
1160 | 0 | } |
1161 | | |
1162 | 0 | int botan_privkey_x448_get_privkey(botan_privkey_t key, uint8_t output[56]) { |
1163 | 0 | if(output == nullptr) { |
1164 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1165 | 0 | } |
1166 | 0 | #if defined(BOTAN_HAS_X448) |
1167 | 0 | return BOTAN_FFI_VISIT(key, [=](const auto& k) { |
1168 | 0 | if(auto x448 = dynamic_cast<const Botan::X448_PrivateKey*>(&k)) { |
1169 | 0 | const auto x448_key = x448->raw_private_key_bits(); |
1170 | 0 | Botan::copy_mem(std::span{output, 56}, x448_key); |
1171 | 0 | return BOTAN_FFI_SUCCESS; |
1172 | 0 | } else { |
1173 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
1174 | 0 | } |
1175 | 0 | }); |
1176 | | #else |
1177 | | BOTAN_UNUSED(key, output); |
1178 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1179 | | #endif |
1180 | 0 | } |
1181 | | |
1182 | 0 | int botan_pubkey_x448_get_pubkey(botan_pubkey_t key, uint8_t output[56]) { |
1183 | 0 | if(output == nullptr) { |
1184 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1185 | 0 | } |
1186 | 0 | #if defined(BOTAN_HAS_X448) |
1187 | 0 | return BOTAN_FFI_VISIT(key, [=](const auto& k) { |
1188 | 0 | if(auto x448 = dynamic_cast<const Botan::X448_PublicKey*>(&k)) { |
1189 | 0 | Botan::copy_mem(std::span{output, 56}, x448->raw_public_key_bits()); |
1190 | 0 | return BOTAN_FFI_SUCCESS; |
1191 | 0 | } else { |
1192 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
1193 | 0 | } |
1194 | 0 | }); |
1195 | | #else |
1196 | | BOTAN_UNUSED(key, output); |
1197 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1198 | | #endif |
1199 | 0 | } |
1200 | | |
1201 | | /* |
1202 | | * Algorithm specific key operations: Kyber |
1203 | | */ |
1204 | | |
1205 | 0 | int botan_privkey_load_kyber(botan_privkey_t* key, const uint8_t privkey[], size_t key_len) { |
1206 | 0 | #if defined(BOTAN_HAS_KYBER) |
1207 | 0 | if(Botan::any_null_pointers(key, privkey)) { |
1208 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1209 | 0 | } |
1210 | 0 | *key = nullptr; |
1211 | |
|
1212 | 0 | const auto mode = [](size_t len) -> std::optional<Botan::KyberMode> { |
1213 | 0 | if(len == 1632) { |
1214 | 0 | return Botan::KyberMode::Kyber512_R3; |
1215 | 0 | } else if(len == 2400) { |
1216 | 0 | return Botan::KyberMode::Kyber768_R3; |
1217 | 0 | } else if(len == 3168) { |
1218 | 0 | return Botan::KyberMode::Kyber1024_R3; |
1219 | 0 | } else { |
1220 | 0 | return {}; |
1221 | 0 | } |
1222 | 0 | }(key_len); |
1223 | |
|
1224 | 0 | if(mode.has_value()) { |
1225 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
1226 | 0 | auto kyber = std::make_unique<Botan::Kyber_PrivateKey>(std::span{privkey, key_len}, *mode); |
1227 | 0 | return ffi_new_object(key, std::move(kyber)); |
1228 | 0 | }); |
1229 | 0 | } else { |
1230 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
1231 | 0 | } |
1232 | | #else |
1233 | | BOTAN_UNUSED(key, key_len, privkey); |
1234 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1235 | | #endif |
1236 | 0 | } |
1237 | | |
1238 | 0 | int botan_pubkey_load_kyber(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len) { |
1239 | 0 | #if defined(BOTAN_HAS_KYBER) |
1240 | 0 | if(Botan::any_null_pointers(key, pubkey)) { |
1241 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1242 | 0 | } |
1243 | 0 | *key = nullptr; |
1244 | |
|
1245 | 0 | const auto mode = [](size_t len) -> std::optional<Botan::KyberMode> { |
1246 | 0 | if(len == 800) { |
1247 | 0 | return Botan::KyberMode::Kyber512_R3; |
1248 | 0 | } else if(len == 1184) { |
1249 | 0 | return Botan::KyberMode::Kyber768_R3; |
1250 | 0 | } else if(len == 1568) { |
1251 | 0 | return Botan::KyberMode::Kyber1024_R3; |
1252 | 0 | } else { |
1253 | 0 | return {}; |
1254 | 0 | } |
1255 | 0 | }(key_len); |
1256 | |
|
1257 | 0 | if(mode.has_value()) { |
1258 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
1259 | 0 | auto kyber = std::make_unique<Botan::Kyber_PublicKey>(std::span{pubkey, key_len}, *mode); |
1260 | 0 | return ffi_new_object(key, std::move(kyber)); |
1261 | 0 | }); |
1262 | 0 | } else { |
1263 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
1264 | 0 | } |
1265 | | #else |
1266 | | BOTAN_UNUSED(key, pubkey, key_len); |
1267 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1268 | | #endif |
1269 | 0 | } |
1270 | | |
1271 | 0 | int botan_privkey_view_kyber_raw_key(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) { |
1272 | 0 | #if defined(BOTAN_HAS_KYBER) |
1273 | 0 | return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int { |
1274 | 0 | if(auto kyber = dynamic_cast<const Botan::Kyber_PrivateKey*>(&k)) { |
1275 | 0 | return invoke_view_callback(view, ctx, kyber->raw_private_key_bits()); |
1276 | 0 | } else { |
1277 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
1278 | 0 | } |
1279 | 0 | }); |
1280 | | #else |
1281 | | BOTAN_UNUSED(key, ctx, view); |
1282 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1283 | | #endif |
1284 | 0 | } |
1285 | | |
1286 | 0 | int botan_pubkey_view_kyber_raw_key(botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) { |
1287 | 0 | #if defined(BOTAN_HAS_KYBER) |
1288 | 0 | return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int { |
1289 | 0 | if(auto kyber = dynamic_cast<const Botan::Kyber_PublicKey*>(&k)) { |
1290 | 0 | return invoke_view_callback(view, ctx, kyber->public_key_bits()); |
1291 | 0 | } else { |
1292 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
1293 | 0 | } |
1294 | 0 | }); |
1295 | | #else |
1296 | | BOTAN_UNUSED(key, ctx, view); |
1297 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1298 | | #endif |
1299 | 0 | } |
1300 | | |
1301 | | /* |
1302 | | * Algorithm specific key operations: ML-KEM |
1303 | | */ |
1304 | | |
1305 | 0 | int botan_privkey_load_ml_kem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mlkem_mode) { |
1306 | 0 | #if defined(BOTAN_HAS_ML_KEM) |
1307 | 0 | if(Botan::any_null_pointers(key, privkey, mlkem_mode)) { |
1308 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1309 | 0 | } |
1310 | | |
1311 | 0 | *key = nullptr; |
1312 | |
|
1313 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
1314 | 0 | auto mode = Botan::ML_KEM_Mode(mlkem_mode); |
1315 | 0 | if(!mode.is_ml_kem()) { |
1316 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
1317 | 0 | } |
1318 | | |
1319 | 0 | auto mlkem_key = std::make_unique<Botan::ML_KEM_PrivateKey>(std::span{privkey, key_len}, mode); |
1320 | 0 | return ffi_new_object(key, std::move(mlkem_key)); |
1321 | 0 | }); |
1322 | | #else |
1323 | | BOTAN_UNUSED(key, key_len, privkey, mlkem_mode); |
1324 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1325 | | #endif |
1326 | 0 | } |
1327 | | |
1328 | 0 | int botan_pubkey_load_ml_kem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mlkem_mode) { |
1329 | 0 | #if defined(BOTAN_HAS_ML_KEM) |
1330 | 0 | if(Botan::any_null_pointers(key, pubkey, mlkem_mode)) { |
1331 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1332 | 0 | } |
1333 | | |
1334 | 0 | *key = nullptr; |
1335 | |
|
1336 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
1337 | 0 | auto mode = Botan::ML_KEM_Mode(mlkem_mode); |
1338 | 0 | if(!mode.is_ml_kem()) { |
1339 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
1340 | 0 | } |
1341 | | |
1342 | 0 | auto mlkem_key = std::make_unique<Botan::ML_KEM_PublicKey>(std::span{pubkey, key_len}, mode.mode()); |
1343 | 0 | return ffi_new_object(key, std::move(mlkem_key)); |
1344 | 0 | }); |
1345 | | #else |
1346 | | BOTAN_UNUSED(key, key_len, pubkey, mlkem_mode); |
1347 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1348 | | #endif |
1349 | 0 | } |
1350 | | |
1351 | | /* |
1352 | | * Algorithm specific key operations: ML-DSA |
1353 | | */ |
1354 | | |
1355 | 0 | int botan_privkey_load_ml_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* mldsa_mode) { |
1356 | 0 | #if defined(BOTAN_HAS_ML_DSA) |
1357 | 0 | if(Botan::any_null_pointers(key, privkey, mldsa_mode)) { |
1358 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1359 | 0 | } |
1360 | | |
1361 | 0 | *key = nullptr; |
1362 | |
|
1363 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
1364 | 0 | auto mode = Botan::ML_DSA_Mode(mldsa_mode); |
1365 | 0 | if(!mode.is_ml_dsa()) { |
1366 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
1367 | 0 | } |
1368 | | |
1369 | 0 | auto mldsa_key = std::make_unique<Botan::ML_DSA_PrivateKey>(std::span{privkey, key_len}, mode); |
1370 | 0 | return ffi_new_object(key, std::move(mldsa_key)); |
1371 | 0 | }); |
1372 | | #else |
1373 | | BOTAN_UNUSED(key, key_len, privkey, mldsa_mode); |
1374 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1375 | | #endif |
1376 | 0 | } |
1377 | | |
1378 | 4.10k | int botan_pubkey_load_ml_dsa(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* mldsa_mode) { |
1379 | 4.10k | #if defined(BOTAN_HAS_ML_DSA) |
1380 | 4.10k | if(Botan::any_null_pointers(key, pubkey, mldsa_mode)) { |
1381 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1382 | 0 | } |
1383 | | |
1384 | 4.10k | *key = nullptr; |
1385 | | |
1386 | 4.10k | return ffi_guard_thunk(__func__, [=]() -> int { |
1387 | 4.10k | auto mode = Botan::ML_DSA_Mode(mldsa_mode); |
1388 | 4.10k | if(!mode.is_ml_dsa()) { |
1389 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
1390 | 0 | } |
1391 | | |
1392 | 4.10k | auto mldsa_key = std::make_unique<Botan::ML_DSA_PublicKey>(std::span{pubkey, key_len}, mode); |
1393 | 4.10k | return ffi_new_object(key, std::move(mldsa_key)); |
1394 | 4.10k | }); |
1395 | | #else |
1396 | | BOTAN_UNUSED(key, key_len, pubkey, mldsa_mode); |
1397 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1398 | | #endif |
1399 | 4.10k | } |
1400 | | |
1401 | | /* |
1402 | | * Algorithm specific key operations: SLH-DSA |
1403 | | */ |
1404 | | |
1405 | 0 | int botan_privkey_load_slh_dsa(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* slhdsa_mode) { |
1406 | 0 | #if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE) |
1407 | 0 | if(Botan::any_null_pointers(key, privkey, slhdsa_mode)) { |
1408 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1409 | 0 | } |
1410 | | |
1411 | 0 | *key = nullptr; |
1412 | |
|
1413 | 0 | return ffi_guard_thunk(__func__, [=]() -> int { |
1414 | 0 | auto mode = Botan::SLH_DSA_Parameters::create(slhdsa_mode); |
1415 | 0 | if(!mode.is_slh_dsa()) { |
1416 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
1417 | 0 | } |
1418 | | |
1419 | 0 | auto slhdsa_key = std::make_unique<Botan::SLH_DSA_PrivateKey>(std::span{privkey, key_len}, mode); |
1420 | 0 | return ffi_new_object(key, std::move(slhdsa_key)); |
1421 | 0 | }); |
1422 | | #else |
1423 | | BOTAN_UNUSED(key, key_len, privkey, slhdsa_mode); |
1424 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1425 | | #endif |
1426 | 0 | } |
1427 | | |
1428 | 532 | int botan_pubkey_load_slh_dsa(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* slhdsa_mode) { |
1429 | 532 | #if defined(BOTAN_HAS_SLH_DSA_WITH_SHA2) || defined(BOTAN_HAS_SLH_DSA_WITH_SHAKE) |
1430 | 532 | if(Botan::any_null_pointers(key, pubkey, slhdsa_mode)) { |
1431 | 0 | return BOTAN_FFI_ERROR_NULL_POINTER; |
1432 | 0 | } |
1433 | | |
1434 | 532 | *key = nullptr; |
1435 | | |
1436 | 532 | return ffi_guard_thunk(__func__, [=]() -> int { |
1437 | 532 | auto mode = Botan::SLH_DSA_Parameters::create(slhdsa_mode); |
1438 | 532 | if(!mode.is_slh_dsa()) { |
1439 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
1440 | 0 | } |
1441 | | |
1442 | 532 | auto mldsa_key = std::make_unique<Botan::SLH_DSA_PublicKey>(std::span{pubkey, key_len}, mode); |
1443 | 532 | return ffi_new_object(key, std::move(mldsa_key)); |
1444 | 532 | }); |
1445 | | #else |
1446 | | BOTAN_UNUSED(key, key_len, pubkey, slhdsa_mode); |
1447 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1448 | | #endif |
1449 | 532 | } |
1450 | | |
1451 | | /* |
1452 | | * Algorithm specific key operations: FrodoKEM |
1453 | | */ |
1454 | | |
1455 | 0 | int botan_privkey_load_frodokem(botan_privkey_t* key, const uint8_t privkey[], size_t key_len, const char* frodo_mode) { |
1456 | | #if defined(BOTAN_HAS_FRODOKEM) |
1457 | | if(Botan::any_null_pointers(key, privkey, frodo_mode)) { |
1458 | | return BOTAN_FFI_ERROR_NULL_POINTER; |
1459 | | } |
1460 | | |
1461 | | *key = nullptr; |
1462 | | |
1463 | | return ffi_guard_thunk(__func__, [=]() -> int { |
1464 | | const auto mode = Botan::FrodoKEMMode(frodo_mode); |
1465 | | auto frodo_key = std::make_unique<Botan::FrodoKEM_PrivateKey>(std::span{privkey, key_len}, mode); |
1466 | | return ffi_new_object(key, std::move(frodo_key)); |
1467 | | }); |
1468 | | #else |
1469 | 0 | BOTAN_UNUSED(key, privkey, key_len, frodo_mode); |
1470 | 0 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1471 | 0 | #endif |
1472 | 0 | } |
1473 | | |
1474 | 0 | int botan_pubkey_load_frodokem(botan_pubkey_t* key, const uint8_t pubkey[], size_t key_len, const char* frodo_mode) { |
1475 | | #if defined(BOTAN_HAS_FRODOKEM) |
1476 | | if(Botan::any_null_pointers(key, pubkey, frodo_mode)) { |
1477 | | return BOTAN_FFI_ERROR_NULL_POINTER; |
1478 | | } |
1479 | | |
1480 | | *key = nullptr; |
1481 | | |
1482 | | return ffi_guard_thunk(__func__, [=]() -> int { |
1483 | | const auto mode = Botan::FrodoKEMMode(frodo_mode); |
1484 | | auto frodo_key = std::make_unique<Botan::FrodoKEM_PublicKey>(std::span{pubkey, key_len}, mode); |
1485 | | return ffi_new_object(key, std::move(frodo_key)); |
1486 | | }); |
1487 | | #else |
1488 | 0 | BOTAN_UNUSED(key, pubkey, key_len, frodo_mode); |
1489 | 0 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1490 | 0 | #endif |
1491 | 0 | } |
1492 | | |
1493 | | /* |
1494 | | * Algorithm specific key operations : Classic McEliece |
1495 | | */ |
1496 | | |
1497 | | int botan_privkey_load_classic_mceliece(botan_privkey_t* key, |
1498 | | const uint8_t privkey[], |
1499 | | size_t key_len, |
1500 | 0 | const char* cmce_mode) { |
1501 | | #if defined(BOTAN_HAS_CLASSICMCELIECE) |
1502 | | if(Botan::any_null_pointers(key, privkey, cmce_mode)) { |
1503 | | return BOTAN_FFI_ERROR_NULL_POINTER; |
1504 | | } |
1505 | | |
1506 | | *key = nullptr; |
1507 | | |
1508 | | return ffi_guard_thunk(__func__, [=]() -> int { |
1509 | | const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode); |
1510 | | auto cmce_key = std::make_unique<Botan::Classic_McEliece_PrivateKey>(std::span{privkey, key_len}, mode); |
1511 | | return ffi_new_object(key, std::move(cmce_key)); |
1512 | | }); |
1513 | | #else |
1514 | 0 | BOTAN_UNUSED(key, privkey, key_len, cmce_mode); |
1515 | 0 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1516 | 0 | #endif |
1517 | 0 | } |
1518 | | |
1519 | | int botan_pubkey_load_classic_mceliece(botan_pubkey_t* key, |
1520 | | const uint8_t pubkey[], |
1521 | | size_t key_len, |
1522 | 0 | const char* cmce_mode) { |
1523 | | #if defined(BOTAN_HAS_CLASSICMCELIECE) |
1524 | | if(Botan::any_null_pointers(key, pubkey, cmce_mode)) { |
1525 | | return BOTAN_FFI_ERROR_NULL_POINTER; |
1526 | | } |
1527 | | |
1528 | | *key = nullptr; |
1529 | | |
1530 | | return ffi_guard_thunk(__func__, [=]() -> int { |
1531 | | const auto mode = Botan::Classic_McEliece_Parameter_Set::from_string(cmce_mode); |
1532 | | auto cmce_key = std::make_unique<Botan::Classic_McEliece_PublicKey>(std::span{pubkey, key_len}, mode); |
1533 | | return ffi_new_object(key, std::move(cmce_key)); |
1534 | | }); |
1535 | | #else |
1536 | 0 | BOTAN_UNUSED(key, pubkey, key_len, cmce_mode); |
1537 | 0 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1538 | 0 | #endif |
1539 | 0 | } |
1540 | | |
1541 | 0 | int botan_pubkey_view_ec_public_point(const botan_pubkey_t key, botan_view_ctx ctx, botan_view_bin_fn view) { |
1542 | 0 | #if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO) |
1543 | 0 | return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int { |
1544 | 0 | if(auto ecc = dynamic_cast<const Botan::EC_PublicKey*>(&k)) { |
1545 | 0 | auto pt = ecc->_public_ec_point().serialize_uncompressed(); |
1546 | 0 | return invoke_view_callback(view, ctx, pt); |
1547 | 0 | } else { |
1548 | 0 | return BOTAN_FFI_ERROR_BAD_PARAMETER; |
1549 | 0 | } |
1550 | 0 | }); |
1551 | | #else |
1552 | | BOTAN_UNUSED(key, view, ctx); |
1553 | | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1554 | | #endif |
1555 | 0 | } |
1556 | | |
1557 | | // NOLINTEND(misc-misplaced-const) |
1558 | | |
1559 | 0 | int botan_privkey_create_mceliece(botan_privkey_t* key_obj, botan_rng_t rng_obj, size_t n, size_t t) { |
1560 | 0 | const std::string mce_params = std::to_string(n) + "," + std::to_string(t); |
1561 | 0 | return botan_privkey_create(key_obj, "McEliece", mce_params.c_str(), rng_obj); |
1562 | 0 | } |
1563 | | |
1564 | | int botan_mceies_decrypt(botan_privkey_t mce_key_obj, |
1565 | | const char* aead, |
1566 | | const uint8_t ct[], |
1567 | | size_t ct_len, |
1568 | | const uint8_t ad[], |
1569 | | size_t ad_len, |
1570 | | uint8_t out[], |
1571 | 0 | size_t* out_len) { |
1572 | 0 | BOTAN_UNUSED(mce_key_obj, aead, ct, ct_len, ad, ad_len, out, out_len); |
1573 | 0 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1574 | 0 | } |
1575 | | |
1576 | | int botan_mceies_encrypt(botan_pubkey_t mce_key_obj, |
1577 | | botan_rng_t rng_obj, |
1578 | | const char* aead, |
1579 | | const uint8_t pt[], |
1580 | | size_t pt_len, |
1581 | | const uint8_t ad[], |
1582 | | size_t ad_len, |
1583 | | uint8_t out[], |
1584 | 0 | size_t* out_len) { |
1585 | 0 | BOTAN_UNUSED(mce_key_obj, rng_obj, aead, pt, pt_len, ad, ad_len, out, out_len); |
1586 | 0 | return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; |
1587 | 0 | } |
1588 | | } |