Botan 3.14.1
Crypto and TLS for C&
ffi_pk_op.cpp
Go to the documentation of this file.
1/*
2* (C) 2015,2017 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/ffi.h>
8
9#include <botan/pubkey.h>
10#include <botan/system_rng.h>
11#include <botan/internal/ffi_pkey.h>
12#include <botan/internal/ffi_rng.h>
13#include <botan/internal/ffi_util.h>
14
15extern "C" {
16
17using namespace Botan_FFI;
18
19BOTAN_FFI_DECLARE_UNIQUE_STRUCT(botan_pk_op_encrypt_struct, Botan::PK_Encryptor, 0x891F3FC3);
20BOTAN_FFI_DECLARE_UNIQUE_STRUCT(botan_pk_op_decrypt_struct, Botan::PK_Decryptor, 0x912F3C37);
21BOTAN_FFI_DECLARE_UNIQUE_STRUCT(botan_pk_op_sign_struct, Botan::PK_Signer, 0x1AF0C39F);
22BOTAN_FFI_DECLARE_UNIQUE_STRUCT(botan_pk_op_verify_struct, Botan::PK_Verifier, 0x2B91F936);
23BOTAN_FFI_DECLARE_UNIQUE_STRUCT(botan_pk_op_ka_struct, Botan::PK_Key_Agreement, 0x2939CAB1);
24
25BOTAN_FFI_DECLARE_UNIQUE_STRUCT(botan_pk_op_kem_encrypt_struct, Botan::PK_KEM_Encryptor, 0x1D13A446);
26BOTAN_FFI_DECLARE_UNIQUE_STRUCT(botan_pk_op_kem_decrypt_struct, Botan::PK_KEM_Decryptor, 0x1743D8E6);
27
28int botan_pk_op_encrypt_create(botan_pk_op_encrypt_t* op, botan_pubkey_t key_obj, const char* padding, uint32_t flags) {
29 if(Botan::any_null_pointers(op, padding)) {
31 }
32
33 if(flags != 0 && flags != BOTAN_PUBKEY_DER_FORMAT_SIGNATURE) {
35 }
36
37 return ffi_guard_thunk(__func__, [=]() -> int {
38 *op = nullptr;
39
40 auto pk = std::make_unique<Botan::PK_Encryptor_EME>(safe_get(key_obj), Botan::system_rng(), padding);
41 return ffi_new_object(op, std::move(pk));
42 });
43}
44
48
49int botan_pk_op_encrypt_output_length(botan_pk_op_encrypt_t op, size_t ptext_len, size_t* ctext_len) {
50 if(ctext_len == nullptr) {
52 }
53 return BOTAN_FFI_VISIT(op, [=](const auto& o) { *ctext_len = o.ciphertext_length(ptext_len); });
54}
55
57 botan_rng_t rng_obj,
58 uint8_t out[],
59 size_t* out_len,
60 const uint8_t plaintext[],
61 size_t plaintext_len) {
62 if(plaintext_len > 0 && plaintext == nullptr) {
64 }
65
66 return BOTAN_FFI_VISIT(op, [=](const auto& o) {
67 return write_vec_output(out, out_len, o.encrypt(plaintext, plaintext_len, safe_get(rng_obj)));
68 });
69}
70
71/*
72* Public Key Decryption
73*/
75 botan_privkey_t key_obj,
76 const char* padding,
77 uint32_t flags) {
78 if(Botan::any_null_pointers(op, padding)) {
80 }
81
82 if(flags != 0) {
84 }
85
86 return ffi_guard_thunk(__func__, [=]() -> int {
87 *op = nullptr;
88
89 auto pk = std::make_unique<Botan::PK_Decryptor_EME>(safe_get(key_obj), Botan::system_rng(), padding);
90 return ffi_new_object(op, std::move(pk));
91 });
92}
93
97
98int botan_pk_op_decrypt_output_length(botan_pk_op_decrypt_t op, size_t ctext_len, size_t* ptext_len) {
99 if(ptext_len == nullptr) {
101 }
102 return BOTAN_FFI_VISIT(op, [=](const auto& o) { *ptext_len = o.plaintext_length(ctext_len); });
103}
104
106 botan_pk_op_decrypt_t op, uint8_t out[], size_t* out_len, const uint8_t ciphertext[], size_t ciphertext_len) {
107 if(ciphertext_len > 0 && ciphertext == nullptr) {
109 }
110
111 return BOTAN_FFI_VISIT(
112 op, [=](const auto& o) { return write_vec_output(out, out_len, o.decrypt(ciphertext, ciphertext_len)); });
113}
114
115/*
116* Signature Generation
117*/
118int botan_pk_op_sign_create(botan_pk_op_sign_t* op, botan_privkey_t key_obj, const char* hash, uint32_t flags) {
119 if(Botan::any_null_pointers(op, hash)) {
121 }
122
123 if(flags != 0 && flags != BOTAN_PUBKEY_DER_FORMAT_SIGNATURE) {
125 }
126
127 return ffi_guard_thunk(__func__, [=]() -> int {
128 *op = nullptr;
129
130 const bool use_der = (flags & BOTAN_PUBKEY_DER_FORMAT_SIGNATURE) != 0;
132
133 auto pk = std::make_unique<Botan::PK_Signer>(safe_get(key_obj), Botan::system_rng(), hash, format);
134 return ffi_new_object(op, std::move(pk));
135 });
136}
137
141
143 if(sig_len == nullptr) {
145 }
146
147 return BOTAN_FFI_VISIT(op, [=](const auto& o) { *sig_len = o.signature_length(); });
148}
149
150int botan_pk_op_sign_update(botan_pk_op_sign_t op, const uint8_t in[], size_t in_len) {
151 if(in_len > 0 && in == nullptr) {
153 }
154
155 return BOTAN_FFI_VISIT(op, [=](auto& o) { o.update(in, in_len); });
156}
157
158int botan_pk_op_sign_finish(botan_pk_op_sign_t op, botan_rng_t rng_obj, uint8_t out[], size_t* out_len) {
159 return BOTAN_FFI_VISIT(op, [=](auto& o) {
160 // Check before signing, which resets the message and may consume a stateful key index
161 if(const int rc = check_and_prepare_output_space(out, out_len, o.signature_length()); rc != BOTAN_FFI_SUCCESS) {
162 return rc;
163 }
164 return write_vec_output(out, out_len, o.signature(safe_get(rng_obj)));
165 });
166}
167
168int botan_pk_op_verify_create(botan_pk_op_verify_t* op, botan_pubkey_t key_obj, const char* hash, uint32_t flags) {
169 if(Botan::any_null_pointers(op, hash)) {
171 }
172
173 if(flags != 0 && flags != BOTAN_PUBKEY_DER_FORMAT_SIGNATURE) {
175 }
176
177 return ffi_guard_thunk(__func__, [=]() -> int {
178 *op = nullptr;
179 const bool use_der = (flags & BOTAN_PUBKEY_DER_FORMAT_SIGNATURE) != 0;
181 auto pk = std::make_unique<Botan::PK_Verifier>(safe_get(key_obj), hash, format);
182 return ffi_new_object(op, std::move(pk));
183 });
184}
185
189
190int botan_pk_op_verify_update(botan_pk_op_verify_t op, const uint8_t in[], size_t in_len) {
191 if(in_len > 0 && in == nullptr) {
193 }
194
195 return BOTAN_FFI_VISIT(op, [=](auto& o) { o.update(in, in_len); });
196}
197
198int botan_pk_op_verify_finish(botan_pk_op_verify_t op, const uint8_t sig[], size_t sig_len) {
199 if(sig_len > 0 && sig == nullptr) {
201 }
202
203 return BOTAN_FFI_VISIT(op, [=](auto& o) {
204 const bool legit = o.check_signature(sig, sig_len);
205
206 if(legit) {
207 return BOTAN_FFI_SUCCESS;
208 } else {
210 }
211 });
212}
213
214int botan_pk_op_key_agreement_create(botan_pk_op_ka_t* op, botan_privkey_t key_obj, const char* kdf, uint32_t flags) {
215 if(Botan::any_null_pointers(op, kdf)) {
217 }
218
219 if(flags != 0) {
221 }
222
223 return ffi_guard_thunk(__func__, [=]() -> int {
224 *op = nullptr;
225 auto pk = std::make_unique<Botan::PK_Key_Agreement>(safe_get(key_obj), Botan::system_rng(), kdf);
226 return ffi_new_object(op, std::move(pk));
227 });
228}
229
233
234int botan_pk_op_key_agreement_export_public(botan_privkey_t key, uint8_t out[], size_t* out_len) {
235 return copy_view_bin(out, out_len, botan_pk_op_key_agreement_view_public, key);
236}
237
239 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
240 if(auto kak = dynamic_cast<const Botan::PK_Key_Agreement_Key*>(&k)) {
241 return invoke_view_callback(view, ctx, kak->public_value());
242 } else {
244 }
245 });
246}
247
249 return BOTAN_FFI_VISIT(op, [=](const auto& o) {
250 if(out_len == nullptr) {
252 }
253 *out_len = o.agreed_value_size();
254 return BOTAN_FFI_SUCCESS;
255 });
256}
257
259 uint8_t out[],
260 size_t* out_len,
261 const uint8_t other_key[],
262 size_t other_key_len,
263 const uint8_t salt[],
264 size_t salt_len) {
265 if(out_len == nullptr) {
267 }
268 if(other_key_len > 0 && other_key == nullptr) {
270 }
271 if(salt_len > 0 && salt == nullptr) {
273 }
274
275 return BOTAN_FFI_VISIT(op, [=](const auto& o) {
276 auto k = o.derive_key(*out_len, other_key, other_key_len, salt, salt_len).bits_of();
277 return write_vec_output(out, out_len, k);
278 });
279}
280
282 if(Botan::any_null_pointers(op, padding)) {
284 }
285
286 return ffi_guard_thunk(__func__, [=]() -> int {
287 auto pk = std::make_unique<Botan::PK_KEM_Encryptor>(safe_get(key_obj), padding);
288 return ffi_new_object(op, std::move(pk));
289 });
290}
291
295
297 size_t desired_shared_key_length,
298 size_t* output_shared_key_length) {
299 if(output_shared_key_length == nullptr) {
301 }
302
303 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
304 *output_shared_key_length = kem.shared_key_length(desired_shared_key_length);
305 return BOTAN_FFI_SUCCESS;
306 });
307}
308
310 size_t* output_encapsulated_key_length) {
311 if(output_encapsulated_key_length == nullptr) {
313 }
314
315 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
316 *output_encapsulated_key_length = kem.encapsulated_key_length();
317 return BOTAN_FFI_SUCCESS;
318 });
319}
320
322 botan_rng_t rng,
323 const uint8_t salt[],
324 size_t salt_len,
325 size_t desired_shared_key_len,
326 uint8_t shared_key_out[],
327 size_t* shared_key_len,
328 uint8_t encapsulated_key_out[],
329 size_t* encapsulated_key_len) {
330 if(salt_len > 0 && salt == nullptr) {
332 }
333
334 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
335 const auto result = kem.encrypt(safe_get(rng), desired_shared_key_len, {salt, salt_len});
336
337 const int rc = write_vec_output(encapsulated_key_out, encapsulated_key_len, result.encapsulated_shared_key());
338
339 if(rc != 0) {
340 return rc;
341 }
342
343 return write_vec_output(shared_key_out, shared_key_len, result.shared_key());
344 });
345}
346
348 if(Botan::any_null_pointers(op, padding)) {
350 }
351
352 return ffi_guard_thunk(__func__, [=]() -> int {
353 auto pk = std::make_unique<Botan::PK_KEM_Decryptor>(safe_get(key_obj), Botan::system_rng(), padding);
354 return ffi_new_object(op, std::move(pk));
355 });
356}
357
359 size_t desired_shared_key_length,
360 size_t* output_shared_key_length) {
361 if(output_shared_key_length == nullptr) {
363 }
364
365 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
366 *output_shared_key_length = kem.shared_key_length(desired_shared_key_length);
367 return BOTAN_FFI_SUCCESS;
368 });
369}
370
372 const uint8_t salt[],
373 size_t salt_len,
374 const uint8_t encapsulated_key[],
375 size_t encapsulated_key_len,
376 size_t desired_shared_key_len,
377 uint8_t shared_key_out[],
378 size_t* shared_key_len) {
379 if(salt_len > 0 && salt == nullptr) {
381 }
382 if(Botan::any_null_pointers(encapsulated_key, shared_key_len)) {
384 }
385
386 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
387 const auto shared_key =
388 kem.decrypt(encapsulated_key, encapsulated_key_len, desired_shared_key_len, salt, salt_len);
389
390 return write_vec_output(shared_key_out, shared_key_len, shared_key);
391 });
392}
393
397}
struct botan_pk_op_kem_decrypt_struct * botan_pk_op_kem_decrypt_t
Definition ffi.h:3488
struct botan_pubkey_struct * botan_pubkey_t
Definition ffi.h:2224
#define BOTAN_PUBKEY_DER_FORMAT_SIGNATURE
Definition ffi.h:3244
struct botan_pk_op_decrypt_struct * botan_pk_op_decrypt_t
Definition ffi.h:3194
struct botan_privkey_struct * botan_privkey_t
Definition ffi.h:1874
struct botan_pk_op_encrypt_struct * botan_pk_op_encrypt_t
Definition ffi.h:3143
struct botan_pk_op_kem_encrypt_struct * botan_pk_op_kem_encrypt_t
Definition ffi.h:3420
int(* botan_view_bin_fn)(botan_view_ctx view_ctx, const uint8_t *data, size_t len)
Definition ffi.h:161
struct botan_pk_op_ka_struct * botan_pk_op_ka_t
Definition ffi.h:3349
int botan_pk_op_key_agreement_view_public(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
struct botan_pk_op_sign_struct * botan_pk_op_sign_t
Definition ffi.h:3249
void * botan_view_ctx
Definition ffi.h:152
struct botan_rng_struct * botan_rng_t
Definition ffi.h:343
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:116
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:130
@ BOTAN_FFI_ERROR_INVALID_INPUT
Definition ffi.h:118
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:131
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
struct botan_pk_op_verify_struct * botan_pk_op_verify_t
Definition ffi.h:3304
int botan_pk_op_encrypt(botan_pk_op_encrypt_t op, botan_rng_t rng_obj, uint8_t out[], size_t *out_len, const uint8_t plaintext[], size_t plaintext_len)
Definition ffi_pk_op.cpp:56
int botan_pk_op_decrypt(botan_pk_op_decrypt_t op, uint8_t out[], size_t *out_len, const uint8_t ciphertext[], size_t ciphertext_len)
int botan_pk_op_kem_decrypt_create(botan_pk_op_kem_decrypt_t *op, botan_privkey_t key_obj, const char *padding)
int botan_pk_op_sign_output_length(botan_pk_op_sign_t op, size_t *sig_len)
int botan_pk_op_key_agreement_export_public(botan_privkey_t key, uint8_t out[], size_t *out_len)
int botan_pk_op_kem_decrypt_shared_key(botan_pk_op_kem_decrypt_t op, const uint8_t salt[], size_t salt_len, const uint8_t encapsulated_key[], size_t encapsulated_key_len, size_t desired_shared_key_len, uint8_t shared_key_out[], size_t *shared_key_len)
int botan_pk_op_kem_encrypt_shared_key_length(botan_pk_op_kem_encrypt_t op, size_t desired_shared_key_length, size_t *output_shared_key_length)
int botan_pk_op_key_agreement_size(botan_pk_op_ka_t op, size_t *out_len)
int botan_pk_op_sign_update(botan_pk_op_sign_t op, const uint8_t in[], size_t in_len)
int botan_pk_op_decrypt_destroy(botan_pk_op_decrypt_t op)
Definition ffi_pk_op.cpp:94
int botan_pk_op_verify_create(botan_pk_op_verify_t *op, botan_pubkey_t key_obj, const char *hash, uint32_t flags)
int botan_pk_op_kem_encrypt_create_shared_key(botan_pk_op_kem_encrypt_t op, botan_rng_t rng, const uint8_t salt[], size_t salt_len, size_t desired_shared_key_len, uint8_t shared_key_out[], size_t *shared_key_len, uint8_t encapsulated_key_out[], size_t *encapsulated_key_len)
int botan_pk_op_key_agreement_destroy(botan_pk_op_ka_t op)
int botan_pk_op_sign_finish(botan_pk_op_sign_t op, botan_rng_t rng_obj, uint8_t out[], size_t *out_len)
int botan_pk_op_verify_update(botan_pk_op_verify_t op, const uint8_t in[], size_t in_len)
int botan_pk_op_encrypt_create(botan_pk_op_encrypt_t *op, botan_pubkey_t key_obj, const char *padding, uint32_t flags)
Definition ffi_pk_op.cpp:28
int botan_pk_op_verify_destroy(botan_pk_op_verify_t op)
int botan_pk_op_kem_encrypt_create(botan_pk_op_kem_encrypt_t *op, botan_pubkey_t key_obj, const char *padding)
int botan_pk_op_key_agreement_view_public(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
int botan_pk_op_kem_encrypt_encapsulated_key_length(botan_pk_op_kem_encrypt_t op, size_t *output_encapsulated_key_length)
int botan_pk_op_verify_finish(botan_pk_op_verify_t op, const uint8_t sig[], size_t sig_len)
int botan_pk_op_decrypt_output_length(botan_pk_op_decrypt_t op, size_t ctext_len, size_t *ptext_len)
Definition ffi_pk_op.cpp:98
int botan_pk_op_encrypt_output_length(botan_pk_op_encrypt_t op, size_t ptext_len, size_t *ctext_len)
Definition ffi_pk_op.cpp:49
int botan_pk_op_key_agreement(botan_pk_op_ka_t op, uint8_t out[], size_t *out_len, const uint8_t other_key[], size_t other_key_len, const uint8_t salt[], size_t salt_len)
int botan_pk_op_kem_encrypt_destroy(botan_pk_op_kem_encrypt_t op)
int botan_pk_op_decrypt_create(botan_pk_op_decrypt_t *op, botan_privkey_t key_obj, const char *padding, uint32_t flags)
Definition ffi_pk_op.cpp:74
int botan_pk_op_encrypt_destroy(botan_pk_op_encrypt_t op)
Definition ffi_pk_op.cpp:45
int botan_pk_op_kem_decrypt_shared_key_length(botan_pk_op_kem_decrypt_t op, size_t desired_shared_key_length, size_t *output_shared_key_length)
int botan_pk_op_sign_create(botan_pk_op_sign_t *op, botan_privkey_t key_obj, const char *hash, uint32_t flags)
int botan_pk_op_kem_decrypt_destroy(botan_pk_op_kem_decrypt_t op)
int botan_pk_op_sign_destroy(botan_pk_op_sign_t op)
int botan_pk_op_key_agreement_create(botan_pk_op_ka_t *op, botan_privkey_t key_obj, const char *kdf, uint32_t flags)
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:327
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:357
#define BOTAN_FFI_DECLARE_UNIQUE_STRUCT(NAME, TYPE, MAGIC)
Definition ffi_util.h:164
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
Definition ffi_util.h:359
int copy_view_bin(uint8_t out[], size_t *out_len, Fn fn, Args... args)
Definition ffi_util.h:383
auto safe_get(T *p) -> typename T::element_type &
Definition ffi_util.h:191
int check_and_prepare_output_space(T out[], size_t *out_len, size_t required_len)
Definition ffi_util.h:402
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:347
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:229
int write_vec_output(uint8_t out[], size_t *out_len, std::span< const uint8_t > buf)
Definition ffi_util.h:436
RandomNumberGenerator & system_rng()
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:54