Botan 3.9.0
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_STRUCT(botan_pk_op_encrypt_struct, Botan::PK_Encryptor, 0x891F3FC3);
20BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_decrypt_struct, Botan::PK_Decryptor, 0x912F3C37);
21BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_sign_struct, Botan::PK_Signer, 0x1AF0C39F);
22BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_verify_struct, Botan::PK_Verifier, 0x2B91F936);
23BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_ka_struct, Botan::PK_Key_Agreement, 0x2939CAB1);
24
25BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_kem_encrypt_struct, Botan::PK_KEM_Encryptor, 0x1D13A446);
26BOTAN_FFI_DECLARE_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(op == nullptr) {
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 return BOTAN_FFI_VISIT(op, [=](const auto& o) {
63 return write_vec_output(out, out_len, o.encrypt(plaintext, plaintext_len, safe_get(rng_obj)));
64 });
65}
66
67/*
68* Public Key Decryption
69*/
71 botan_privkey_t key_obj,
72 const char* padding,
73 uint32_t flags) {
74 if(op == nullptr) {
76 }
77
78 if(flags != 0) {
80 }
81
82 return ffi_guard_thunk(__func__, [=]() -> int {
83 *op = nullptr;
84
85 auto pk = std::make_unique<Botan::PK_Decryptor_EME>(safe_get(key_obj), Botan::system_rng(), padding);
86 return ffi_new_object(op, std::move(pk));
87 });
88}
89
93
94int botan_pk_op_decrypt_output_length(botan_pk_op_decrypt_t op, size_t ctext_len, size_t* ptext_len) {
95 if(ptext_len == nullptr) {
97 }
98 return BOTAN_FFI_VISIT(op, [=](const auto& o) { *ptext_len = o.plaintext_length(ctext_len); });
99}
100
102 botan_pk_op_decrypt_t op, uint8_t out[], size_t* out_len, const uint8_t ciphertext[], size_t ciphertext_len) {
103 return BOTAN_FFI_VISIT(
104 op, [=](const auto& o) { return write_vec_output(out, out_len, o.decrypt(ciphertext, ciphertext_len)); });
105}
106
107/*
108* Signature Generation
109*/
110int botan_pk_op_sign_create(botan_pk_op_sign_t* op, botan_privkey_t key_obj, const char* hash, uint32_t flags) {
111 if(op == nullptr) {
113 }
114
115 if(flags != 0 && flags != BOTAN_PUBKEY_DER_FORMAT_SIGNATURE) {
117 }
118
119 return ffi_guard_thunk(__func__, [=]() -> int {
120 *op = nullptr;
121
122 const bool use_der = (flags & BOTAN_PUBKEY_DER_FORMAT_SIGNATURE) != 0;
124
125 auto pk = std::make_unique<Botan::PK_Signer>(safe_get(key_obj), Botan::system_rng(), hash, format);
126 return ffi_new_object(op, std::move(pk));
127 });
128}
129
133
135 if(sig_len == nullptr) {
137 }
138
139 return BOTAN_FFI_VISIT(op, [=](const auto& o) { *sig_len = o.signature_length(); });
140}
141
142int botan_pk_op_sign_update(botan_pk_op_sign_t op, const uint8_t in[], size_t in_len) {
143 return BOTAN_FFI_VISIT(op, [=](auto& o) { o.update(in, in_len); });
144}
145
146int botan_pk_op_sign_finish(botan_pk_op_sign_t op, botan_rng_t rng_obj, uint8_t out[], size_t* out_len) {
147 return BOTAN_FFI_VISIT(op, [=](auto& o) { return write_vec_output(out, out_len, o.signature(safe_get(rng_obj))); });
148}
149
150int botan_pk_op_verify_create(botan_pk_op_verify_t* op, botan_pubkey_t key_obj, const char* hash, uint32_t flags) {
151 if(op == nullptr) {
153 }
154
155 if(flags != 0 && flags != BOTAN_PUBKEY_DER_FORMAT_SIGNATURE) {
157 }
158
159 return ffi_guard_thunk(__func__, [=]() -> int {
160 *op = nullptr;
161 const bool use_der = (flags & BOTAN_PUBKEY_DER_FORMAT_SIGNATURE) != 0;
163 auto pk = std::make_unique<Botan::PK_Verifier>(safe_get(key_obj), hash, format);
164 return ffi_new_object(op, std::move(pk));
165 });
166}
167
171
172int botan_pk_op_verify_update(botan_pk_op_verify_t op, const uint8_t in[], size_t in_len) {
173 return BOTAN_FFI_VISIT(op, [=](auto& o) { o.update(in, in_len); });
174}
175
176int botan_pk_op_verify_finish(botan_pk_op_verify_t op, const uint8_t sig[], size_t sig_len) {
177 return BOTAN_FFI_VISIT(op, [=](auto& o) {
178 const bool legit = o.check_signature(sig, sig_len);
179
180 if(legit) {
181 return BOTAN_FFI_SUCCESS;
182 } else {
184 }
185 });
186}
187
188int botan_pk_op_key_agreement_create(botan_pk_op_ka_t* op, botan_privkey_t key_obj, const char* kdf, uint32_t flags) {
189 if(op == nullptr) {
191 }
192
193 if(flags != 0) {
195 }
196
197 return ffi_guard_thunk(__func__, [=]() -> int {
198 *op = nullptr;
199 auto pk = std::make_unique<Botan::PK_Key_Agreement>(safe_get(key_obj), Botan::system_rng(), kdf);
200 return ffi_new_object(op, std::move(pk));
201 });
202}
203
207
208int botan_pk_op_key_agreement_export_public(botan_privkey_t key, uint8_t out[], size_t* out_len) {
209 return copy_view_bin(out, out_len, botan_pk_op_key_agreement_view_public, key);
210}
211
213 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
214 if(auto kak = dynamic_cast<const Botan::PK_Key_Agreement_Key*>(&k)) {
215 return invoke_view_callback(view, ctx, kak->public_value());
216 } else {
218 }
219 });
220}
221
223 return BOTAN_FFI_VISIT(op, [=](const auto& o) {
224 if(out_len == nullptr) {
226 }
227 *out_len = o.agreed_value_size();
228 return BOTAN_FFI_SUCCESS;
229 });
230}
231
233 uint8_t out[],
234 size_t* out_len,
235 const uint8_t other_key[],
236 size_t other_key_len,
237 const uint8_t salt[],
238 size_t salt_len) {
239 return BOTAN_FFI_VISIT(op, [=](const auto& o) {
240 auto k = o.derive_key(*out_len, other_key, other_key_len, salt, salt_len).bits_of();
241 return write_vec_output(out, out_len, k);
242 });
243}
244
246 if(op == nullptr || padding == nullptr) {
248 }
249
250 return ffi_guard_thunk(__func__, [=]() -> int {
251 auto pk = std::make_unique<Botan::PK_KEM_Encryptor>(safe_get(key_obj), padding);
252 return ffi_new_object(op, std::move(pk));
253 });
254}
255
259
261 size_t desired_shared_key_length,
262 size_t* output_shared_key_length) {
263 if(output_shared_key_length == nullptr) {
265 }
266
267 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
268 *output_shared_key_length = kem.shared_key_length(desired_shared_key_length);
269 return BOTAN_FFI_SUCCESS;
270 });
271}
272
274 size_t* output_encapsulated_key_length) {
275 if(output_encapsulated_key_length == nullptr) {
277 }
278
279 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
280 *output_encapsulated_key_length = kem.encapsulated_key_length();
281 return BOTAN_FFI_SUCCESS;
282 });
283}
284
286 botan_rng_t rng,
287 const uint8_t salt[],
288 size_t salt_len,
289 size_t desired_shared_key_len,
290 uint8_t shared_key_out[],
291 size_t* shared_key_len,
292 uint8_t encapsulated_key_out[],
293 size_t* encapsulated_key_len) {
294 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
295 const auto result = kem.encrypt(safe_get(rng), desired_shared_key_len, {salt, salt_len});
296
297 int rc = write_vec_output(encapsulated_key_out, encapsulated_key_len, result.encapsulated_shared_key());
298
299 if(rc != 0) {
300 return rc;
301 }
302
303 return write_vec_output(shared_key_out, shared_key_len, result.shared_key());
304 });
305}
306
308 if(op == nullptr || padding == nullptr) {
310 }
311
312 return ffi_guard_thunk(__func__, [=]() -> int {
313 auto pk = std::make_unique<Botan::PK_KEM_Decryptor>(safe_get(key_obj), Botan::system_rng(), padding);
314 return ffi_new_object(op, std::move(pk));
315 });
316}
317
319 size_t desired_shared_key_length,
320 size_t* output_shared_key_length) {
321 if(output_shared_key_length == nullptr) {
323 }
324
325 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
326 *output_shared_key_length = kem.shared_key_length(desired_shared_key_length);
327 return BOTAN_FFI_SUCCESS;
328 });
329}
330
332 const uint8_t salt[],
333 size_t salt_len,
334 const uint8_t encapsulated_key[],
335 size_t encapsulated_key_len,
336 size_t desired_shared_key_len,
337 uint8_t shared_key_out[],
338 size_t* shared_key_len) {
339 return BOTAN_FFI_VISIT(op, [=](auto& kem) {
340 const auto shared_key =
341 kem.decrypt(encapsulated_key, encapsulated_key_len, desired_shared_key_len, salt, salt_len);
342
343 return write_vec_output(shared_key_out, shared_key_len, shared_key);
344 });
345}
346
350}
struct botan_pk_op_kem_decrypt_struct * botan_pk_op_kem_decrypt_t
Definition ffi.h:2043
struct botan_pubkey_struct * botan_pubkey_t
Definition ffi.h:1537
#define BOTAN_PUBKEY_DER_FORMAT_SIGNATURE
Definition ffi.h:1943
struct botan_pk_op_decrypt_struct * botan_pk_op_decrypt_t
Definition ffi.h:1922
struct botan_privkey_struct * botan_privkey_t
Definition ffi.h:1304
struct botan_pk_op_encrypt_struct * botan_pk_op_encrypt_t
Definition ffi.h:1898
struct botan_pk_op_kem_encrypt_struct * botan_pk_op_kem_encrypt_t
Definition ffi.h:2013
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:1984
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:1945
void * botan_view_ctx
Definition ffi.h:152
struct botan_rng_struct * botan_rng_t
Definition ffi.h:289
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:117
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:131
@ BOTAN_FFI_ERROR_INVALID_INPUT
Definition ffi.h:119
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:132
@ BOTAN_FFI_SUCCESS
Definition ffi.h:115
struct botan_pk_op_verify_struct * botan_pk_op_verify_t
Definition ffi.h:1965
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:90
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:94
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:70
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:158
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:185
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition ffi_util.h:61
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
Definition ffi_util.h:187
int copy_view_bin(uint8_t out[], size_t *out_len, Fn fn, Args... args)
Definition ffi_util.h:211
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:79
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:178
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:95
int write_vec_output(uint8_t out[], size_t *out_len, std::span< const uint8_t > buf)
Definition ffi_util.h:247
RandomNumberGenerator & system_rng()