Botan 3.0.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#include <botan/internal/ffi_util.h>
9#include <botan/internal/ffi_pkey.h>
10#include <botan/internal/ffi_rng.h>
11#include <botan/pubkey.h>
12#include <botan/system_rng.h>
13
14extern "C" {
15
16using namespace Botan_FFI;
17
18BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_encrypt_struct, Botan::PK_Encryptor, 0x891F3FC3);
19BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_decrypt_struct, Botan::PK_Decryptor, 0x912F3C37);
20BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_sign_struct, Botan::PK_Signer, 0x1AF0C39F);
21BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_verify_struct, Botan::PK_Verifier, 0x2B91F936);
22BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_ka_struct, Botan::PK_Key_Agreement, 0x2939CAB1);
23
24BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_kem_encrypt_struct, Botan::PK_KEM_Encryptor, 0x1D13A446);
25BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_kem_decrypt_struct, Botan::PK_KEM_Decryptor, 0x1743D8E6);
26
28 botan_pubkey_t key_obj,
29 const char* padding,
30 uint32_t flags)
31 {
32 if(op == nullptr)
34
35 if(flags != 0 && flags != BOTAN_PUBKEY_DER_FORMAT_SIGNATURE)
37
38 return ffi_guard_thunk(__func__, [=]() -> int {
39 *op = nullptr;
40
41 auto pk = std::make_unique<Botan::PK_Encryptor_EME>(safe_get(key_obj), Botan::system_rng(), padding);
42 *op = new botan_pk_op_encrypt_struct(std::move(pk));
43 return BOTAN_FFI_SUCCESS;
44 });
45 }
46
48 {
49 return BOTAN_FFI_CHECKED_DELETE(op);
50 }
51
52int botan_pk_op_encrypt_output_length(botan_pk_op_encrypt_t op, size_t ptext_len, size_t* ctext_len)
53 {
54 if(ctext_len == nullptr)
56 return BOTAN_FFI_VISIT(op, [=](const auto& o) { *ctext_len = o.ciphertext_length(ptext_len); });
57 }
58
60 botan_rng_t rng_obj,
61 uint8_t out[], size_t* out_len,
62 const uint8_t plaintext[], size_t plaintext_len)
63 {
64 return BOTAN_FFI_VISIT(op, [=](const auto& o) {
65 return write_vec_output(out, out_len, o.encrypt(plaintext, plaintext_len, safe_get(rng_obj)));
66 });
67 }
68
69/*
70* Public Key Decryption
71*/
73 botan_privkey_t key_obj,
74 const char* padding,
75 uint32_t flags)
76 {
77 if(op == nullptr)
79
80 if(flags != 0)
82
83 return ffi_guard_thunk(__func__, [=]() -> int {
84 *op = nullptr;
85
86 auto pk = std::make_unique<Botan::PK_Decryptor_EME>(safe_get(key_obj), Botan::system_rng(), padding);
87 *op = new botan_pk_op_decrypt_struct(std::move(pk));
88 return BOTAN_FFI_SUCCESS;
89 });
90 }
91
93 {
94 return BOTAN_FFI_CHECKED_DELETE(op);
95 }
96
97int botan_pk_op_decrypt_output_length(botan_pk_op_decrypt_t op, size_t ctext_len, size_t* ptext_len)
98 {
99 if(ptext_len == nullptr)
101 return BOTAN_FFI_VISIT(op, [=](const auto& o) { *ptext_len = o.plaintext_length(ctext_len); });
102 }
103
105 uint8_t out[], size_t* out_len,
106 const uint8_t ciphertext[], size_t ciphertext_len)
107 {
108 return BOTAN_FFI_VISIT(op, [=](const auto& o) {
109 return write_vec_output(out, out_len, o.decrypt(ciphertext, ciphertext_len));
110 });
111 }
112
113/*
114* Signature Generation
115*/
117 botan_privkey_t key_obj,
118 const char* hash,
119 uint32_t flags)
120 {
121 if(op == nullptr)
123
124 if(flags != 0 && flags != BOTAN_PUBKEY_DER_FORMAT_SIGNATURE)
126
127 return ffi_guard_thunk(__func__, [=]() -> int {
128 *op = nullptr;
129
131
132 auto pk = std::make_unique<Botan::PK_Signer>(safe_get(key_obj), Botan::system_rng(), hash, format);
133 *op = new botan_pk_op_sign_struct(std::move(pk));
134 return BOTAN_FFI_SUCCESS;
135 });
136 }
137
139 {
140 return BOTAN_FFI_CHECKED_DELETE(op);
141 }
142
144 {
145 if(sig_len == nullptr)
147
148 return BOTAN_FFI_VISIT(op, [=](const auto& o) { *sig_len = o.signature_length(); });
149 }
150
151int botan_pk_op_sign_update(botan_pk_op_sign_t op, const uint8_t in[], size_t in_len)
152 {
153 return BOTAN_FFI_VISIT(op, [=](auto& o) { o.update(in, in_len); });
154 }
155
156int botan_pk_op_sign_finish(botan_pk_op_sign_t op, botan_rng_t rng_obj, uint8_t out[], size_t* out_len)
157 {
158 return BOTAN_FFI_VISIT(op, [=](auto& o) {
159 return write_vec_output(out, out_len, o.signature(safe_get(rng_obj)));
160 });
161 }
162
164 botan_pubkey_t key_obj,
165 const char* hash,
166 uint32_t flags)
167 {
168 if(op == nullptr)
170
171 if(flags != 0 && flags != BOTAN_PUBKEY_DER_FORMAT_SIGNATURE)
173
174 return ffi_guard_thunk(__func__, [=]() -> int {
175 *op = nullptr;
177 auto pk = std::make_unique<Botan::PK_Verifier>(safe_get(key_obj), hash, format);
178 *op = new botan_pk_op_verify_struct(std::move(pk));
179 return BOTAN_FFI_SUCCESS;
180 });
181 }
182
184 {
185 return BOTAN_FFI_CHECKED_DELETE(op);
186 }
187
188int botan_pk_op_verify_update(botan_pk_op_verify_t op, const uint8_t in[], size_t in_len)
189 {
190 return BOTAN_FFI_VISIT(op, [=](auto& o) { o.update(in, in_len); });
191 }
192
193int botan_pk_op_verify_finish(botan_pk_op_verify_t op, const uint8_t sig[], size_t sig_len)
194 {
195 return BOTAN_FFI_VISIT(op, [=](auto& o) {
196 const bool legit = o.check_signature(sig, sig_len);
197
198 if(legit)
199 return BOTAN_FFI_SUCCESS;
200 else
202 });
203 }
204
206 botan_privkey_t key_obj,
207 const char* kdf,
208 uint32_t flags)
209 {
210 if(op == nullptr)
212
213 if(flags != 0)
215
216 return ffi_guard_thunk(__func__, [=]() -> int {
217 *op = nullptr;
218 auto pk = std::make_unique<Botan::PK_Key_Agreement>(safe_get(key_obj), Botan::system_rng(), kdf);
219 *op = new botan_pk_op_ka_struct(std::move(pk));
220 return BOTAN_FFI_SUCCESS;
221 });
222 }
223
225 {
226 return BOTAN_FFI_CHECKED_DELETE(op);
227 }
228
230 uint8_t out[], size_t* out_len)
231 {
232 return copy_view_bin(out, out_len, botan_pk_op_key_agreement_view_public, key);
233 }
234
236 botan_privkey_t key,
237 botan_view_ctx ctx,
239 {
240 return BOTAN_FFI_VISIT(key, [=](const auto& k) -> int {
241 if(auto kak = dynamic_cast<const Botan::PK_Key_Agreement_Key*>(&k))
242 return invoke_view_callback(view, ctx, kak->public_value());
243 else
245 });
246 }
247
249 {
250 return BOTAN_FFI_VISIT(op, [=](const auto& o) {
251 if(out_len == nullptr)
253 *out_len = o.agreed_value_size();
254 return BOTAN_FFI_SUCCESS;
255 });
256 }
257
259 uint8_t out[], size_t* out_len,
260 const uint8_t other_key[], size_t other_key_len,
261 const uint8_t salt[], size_t salt_len)
262 {
263 return BOTAN_FFI_VISIT(op, [=](const auto& o) {
264 auto k = o.derive_key(*out_len, other_key, other_key_len, salt, salt_len).bits_of();
265 return write_vec_output(out, out_len, k);
266 });
267 }
268
270 botan_pubkey_t key_obj,
271 const char* padding)
272 {
273 if(op == nullptr || padding == nullptr)
275
276 return ffi_guard_thunk(__func__, [=]() -> int {
277 auto pk = std::make_unique<Botan::PK_KEM_Encryptor>(safe_get(key_obj), padding);
278 *op = new botan_pk_op_kem_encrypt_struct(std::move(pk));
279 return BOTAN_FFI_SUCCESS;
280 });
281 }
282
284 {
285 return BOTAN_FFI_CHECKED_DELETE(op);
286 }
287
289 size_t desired_shared_key_length,
290 size_t* output_shared_key_length)
291 {
292 if(output_shared_key_length == nullptr)
294
295 return BOTAN_FFI_VISIT(op, [=](auto& kem)
296 {
297 *output_shared_key_length = kem.shared_key_length(desired_shared_key_length);
298 return BOTAN_FFI_SUCCESS;
299 });
300 }
301
303 size_t* output_encapsulated_key_length)
304 {
305 if(output_encapsulated_key_length == nullptr)
307
308 return BOTAN_FFI_VISIT(op, [=](auto& kem)
309 {
310 *output_encapsulated_key_length = kem.encapsulated_key_length();
311 return BOTAN_FFI_SUCCESS;
312 });
313 }
314
317 botan_rng_t rng,
318 const uint8_t salt[],
319 size_t salt_len,
320 size_t desired_shared_key_len,
321 uint8_t shared_key_out[],
322 size_t* shared_key_len,
323 uint8_t encapsulated_key_out[],
324 size_t* encapsulated_key_len)
325 {
326 return BOTAN_FFI_VISIT(op, [=](auto& kem)
327 {
328 Botan::secure_vector<uint8_t> encapsulated_key;
330
331 kem.encrypt(encapsulated_key, shared_key,
332 desired_shared_key_len,
333 safe_get(rng),
334 salt,
335 salt_len);
336
337 int rc = write_vec_output(encapsulated_key_out,
338 encapsulated_key_len,
339 encapsulated_key);
340
341 if(rc != 0)
342 return rc;
343
344 return write_vec_output(shared_key_out, shared_key_len, shared_key);
345 });
346 }
347
349 botan_privkey_t key_obj,
350 const char* padding)
351 {
352 if(op == nullptr || padding == nullptr)
354
355 return ffi_guard_thunk(__func__, [=]() -> int {
356 auto pk = std::make_unique<Botan::PK_KEM_Decryptor>(safe_get(key_obj), Botan::system_rng(), padding);
357 *op = new botan_pk_op_kem_decrypt_struct(std::move(pk));
358 return BOTAN_FFI_SUCCESS;
359 });
360 }
361
363 size_t desired_shared_key_length,
364 size_t* output_shared_key_length)
365 {
366 if(output_shared_key_length == nullptr)
368
369 return BOTAN_FFI_VISIT(op, [=](auto& kem)
370 {
371 *output_shared_key_length = kem.shared_key_length(desired_shared_key_length);
372 return BOTAN_FFI_SUCCESS;
373 });
374 }
375
378 const uint8_t salt[],
379 size_t salt_len,
380 const uint8_t encapsulated_key[],
381 size_t encapsulated_key_len,
382 size_t desired_shared_key_len,
383 uint8_t shared_key_out[],
384 size_t* shared_key_len)
385 {
386 return BOTAN_FFI_VISIT(op, [=](auto& kem)
387 {
388 const auto shared_key =
389 kem.decrypt(encapsulated_key,
390 encapsulated_key_len,
391 desired_shared_key_len,
392 salt,
393 salt_len);
394
395 write_vec_output(shared_key_out, shared_key_len, shared_key);
396 });
397 }
398
400 {
401 return BOTAN_FFI_CHECKED_DELETE(op);
402 }
403
404}
struct botan_pk_op_kem_decrypt_struct * botan_pk_op_kem_decrypt_t
Definition: ffi.h:1703
struct botan_pubkey_struct * botan_pubkey_t
Definition: ffi.h:1255
#define BOTAN_PUBKEY_DER_FORMAT_SIGNATURE
Definition: ffi.h:1592
struct botan_pk_op_decrypt_struct * botan_pk_op_decrypt_t
Definition: ffi.h:1568
struct botan_privkey_struct * botan_privkey_t
Definition: ffi.h:1028
struct botan_pk_op_encrypt_struct * botan_pk_op_encrypt_t
Definition: ffi.h:1542
struct botan_pk_op_kem_encrypt_struct * botan_pk_op_kem_encrypt_t
Definition: ffi.h:1669
int(* botan_view_bin_fn)(botan_view_ctx view_ctx, const uint8_t *data, size_t len)
Definition: ffi.h:110
struct botan_pk_op_ka_struct * botan_pk_op_ka_t
Definition: ffi.h:1637
struct botan_pk_op_sign_struct * botan_pk_op_sign_t
Definition: ffi.h:1594
void * botan_view_ctx
Definition: ffi.h:101
struct botan_rng_struct * botan_rng_t
Definition: ffi.h:229
@ BOTAN_FFI_INVALID_VERIFIER
Definition: ffi.h:71
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition: ffi.h:84
@ BOTAN_FFI_ERROR_INVALID_INPUT
Definition: ffi.h:73
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:85
@ BOTAN_FFI_SUCCESS
Definition: ffi.h:70
struct botan_pk_op_verify_struct * botan_pk_op_verify_t
Definition: ffi.h:1618
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:59
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)
Definition: ffi_pk_op.cpp:104
int botan_pk_op_kem_decrypt_create(botan_pk_op_kem_decrypt_t *op, botan_privkey_t key_obj, const char *padding)
Definition: ffi_pk_op.cpp:348
int botan_pk_op_sign_output_length(botan_pk_op_sign_t op, size_t *sig_len)
Definition: ffi_pk_op.cpp:143
int botan_pk_op_key_agreement_export_public(botan_privkey_t key, uint8_t out[], size_t *out_len)
Definition: ffi_pk_op.cpp:229
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)
Definition: ffi_pk_op.cpp:376
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)
Definition: ffi_pk_op.cpp:288
int botan_pk_op_key_agreement_size(botan_pk_op_ka_t op, size_t *out_len)
Definition: ffi_pk_op.cpp:248
int botan_pk_op_sign_update(botan_pk_op_sign_t op, const uint8_t in[], size_t in_len)
Definition: ffi_pk_op.cpp:151
int botan_pk_op_decrypt_destroy(botan_pk_op_decrypt_t op)
Definition: ffi_pk_op.cpp:92
int botan_pk_op_verify_create(botan_pk_op_verify_t *op, botan_pubkey_t key_obj, const char *hash, uint32_t flags)
Definition: ffi_pk_op.cpp:163
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)
Definition: ffi_pk_op.cpp:315
int botan_pk_op_key_agreement_destroy(botan_pk_op_ka_t op)
Definition: ffi_pk_op.cpp:224
int botan_pk_op_sign_finish(botan_pk_op_sign_t op, botan_rng_t rng_obj, uint8_t out[], size_t *out_len)
Definition: ffi_pk_op.cpp:156
int botan_pk_op_verify_update(botan_pk_op_verify_t op, const uint8_t in[], size_t in_len)
Definition: ffi_pk_op.cpp:188
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:27
int botan_pk_op_verify_destroy(botan_pk_op_verify_t op)
Definition: ffi_pk_op.cpp:183
int botan_pk_op_kem_encrypt_create(botan_pk_op_kem_encrypt_t *op, botan_pubkey_t key_obj, const char *padding)
Definition: ffi_pk_op.cpp:269
int botan_pk_op_key_agreement_view_public(botan_privkey_t key, botan_view_ctx ctx, botan_view_bin_fn view)
Definition: ffi_pk_op.cpp:235
int botan_pk_op_kem_encrypt_encapsulated_key_length(botan_pk_op_kem_encrypt_t op, size_t *output_encapsulated_key_length)
Definition: ffi_pk_op.cpp:302
int botan_pk_op_verify_finish(botan_pk_op_verify_t op, const uint8_t sig[], size_t sig_len)
Definition: ffi_pk_op.cpp:193
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:97
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:52
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)
Definition: ffi_pk_op.cpp:258
int botan_pk_op_kem_encrypt_destroy(botan_pk_op_kem_encrypt_t op)
Definition: ffi_pk_op.cpp:283
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:72
int botan_pk_op_encrypt_destroy(botan_pk_op_encrypt_t op)
Definition: ffi_pk_op.cpp:47
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)
Definition: ffi_pk_op.cpp:362
int botan_pk_op_sign_create(botan_pk_op_sign_t *op, botan_privkey_t key_obj, const char *hash, uint32_t flags)
Definition: ffi_pk_op.cpp:116
int botan_pk_op_kem_decrypt_destroy(botan_pk_op_kem_decrypt_t op)
Definition: ffi_pk_op.cpp:399
int botan_pk_op_sign_destroy(botan_pk_op_sign_t op)
Definition: ffi_pk_op.cpp:138
int botan_pk_op_key_agreement_create(botan_pk_op_ka_t *op, botan_privkey_t key_obj, const char *kdf, uint32_t flags)
Definition: ffi_pk_op.cpp:205
#define BOTAN_FFI_VISIT(obj, lambda)
Definition: ffi_util.h:126
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:145
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition: ffi_util.h:55
int copy_view_bin(uint8_t out[], size_t *out_len, Fn fn, Args... args)
Definition: ffi_util.h:171
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:69
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, const std::vector< uint8_t, Alloc > &buf)
Definition: ffi_util.h:148
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition: ffi.cpp:120
int write_vec_output(uint8_t out[], size_t *out_len, const std::vector< uint8_t, Alloc > &buf)
Definition: ffi_util.h:214
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:376
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:64