Botan  2.1.0
Crypto and TLS for C++11
ffi.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/system_rng.h>
9 #include <botan/exceptn.h>
10 #include <botan/auto_rng.h>
11 #include <botan/aead.h>
12 #include <botan/block_cipher.h>
13 #include <botan/hash.h>
14 #include <botan/mac.h>
15 #include <botan/pbkdf.h>
16 #include <botan/version.h>
17 #include <botan/pkcs8.h>
18 #include <botan/x509cert.h>
19 #include <botan/data_src.h>
20 #include <botan/pubkey.h>
21 #include <botan/hex.h>
22 #include <botan/mem_ops.h>
23 #include <botan/x509_key.h>
24 #include <botan/pk_algs.h>
25 #include <botan/bigint.h>
26 #include <botan/reducer.h>
27 #include <botan/numthry.h>
28 #include <botan/divide.h>
29 #include <cstring>
30 #include <memory>
31 
32 #if defined(BOTAN_HAS_RSA)
33  #include <botan/rsa.h>
34 #endif
35 
36 #if defined(BOTAN_HAS_DSA)
37  #include <botan/dsa.h>
38 #endif
39 
40 #if defined(BOTAN_HAS_ECDSA)
41  #include <botan/ecdsa.h>
42 #endif
43 
44 #if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
45  #include <botan/ecc_key.h>
46 #endif
47 
48 #if defined(BOTAN_HAS_DL_PUBLIC_KEY_FAMILY)
49  #include <botan/dl_algo.h>
50 #endif
51 
52 #if defined(BOTAN_HAS_ECDH)
53  #include <botan/ecdh.h>
54 #endif
55 
56 #if defined(BOTAN_HAS_CURVE_25519)
57  #include <botan/curve25519.h>
58 #endif
59 
60 #if defined(BOTAN_HAS_MCELIECE)
61  #include <botan/mceliece.h>
62 #endif
63 
64 #if defined(BOTAN_HAS_MCEIES)
65  #include <botan/mceies.h>
66 #endif
67 
68 #if defined(BOTAN_HAS_BCRYPT)
69  #include <botan/bcrypt.h>
70 #endif
71 
72 #if defined(BOTAN_HAS_TLS)
73  #include <botan/tls_client.h>
74  #include <botan/tls_server.h>
75 #endif
76 
77 namespace {
78 
79 #define BOTAN_ASSERT_ARG_NON_NULL(p) \
80  do { if(!p) throw Botan::Invalid_Argument("Argument " #p " is null"); } while(0)
81 
82 class FFI_Error : public Botan::Exception
83  {
84  public:
85  explicit FFI_Error(const std::string& what) : Exception("FFI error", what) {}
86  };
87 
88 template<typename T, uint32_t MAGIC>
89 struct botan_struct
90  {
91  public:
92  botan_struct(T* obj) : m_magic(MAGIC), m_obj(obj) {}
93  ~botan_struct() { m_magic = 0; m_obj.reset(); }
94 
95  T* get() const
96  {
97  if(m_magic != MAGIC)
98  throw FFI_Error("Bad magic " + std::to_string(m_magic) +
99  " in ffi object expected " + std::to_string(MAGIC));
100  return m_obj.get();
101  }
102  private:
103  uint32_t m_magic = 0;
104  std::unique_ptr<T> m_obj;
105  };
106 
107 void log_exception(const char* func_name, const char* what)
108  {
109  fprintf(stderr, "%s: %s\n", func_name, what);
110  }
111 
112 int ffi_error_exception_thrown(const char* exn)
113  {
114  fprintf(stderr, "exception %s\n", exn);
116  }
117 
118 template<typename T, uint32_t M>
119 T& safe_get(botan_struct<T,M>* p)
120  {
121  if(!p)
122  throw FFI_Error("Null pointer argument");
123  if(T* t = p->get())
124  return *t;
125  throw FFI_Error("Invalid object pointer");
126  }
127 
128 template<typename T, uint32_t M>
129 const T& safe_get(const botan_struct<T,M>* p)
130  {
131  if(!p)
132  throw FFI_Error("Null pointer argument");
133  if(const T* t = p->get())
134  return *t;
135  throw FFI_Error("Invalid object pointer");
136  }
137 
138 template<typename T, uint32_t M, typename F>
139 int apply_fn(botan_struct<T, M>* o, const char* func_name, F func)
140  {
141  try
142  {
143  if(!o)
144  throw FFI_Error("Null object to " + std::string(func_name));
145  if(T* t = o->get())
146  return func(*t);
147  }
148  catch(std::exception& e)
149  {
150  log_exception(func_name, e.what());
151  return -1;
152  }
153  catch(...)
154  {
155  log_exception(func_name, "unknown exception type");
156  return -2;
157  }
158 
159  return -1;
160  }
161 
162 inline int write_output(uint8_t out[], size_t* out_len, const uint8_t buf[], size_t buf_len)
163  {
164  const size_t avail = *out_len;
165  *out_len = buf_len;
166 
167  if(avail >= buf_len)
168  {
169  Botan::copy_mem(out, buf, buf_len);
170  return 0;
171  }
172  else
173  {
174  Botan::clear_mem(out, avail);
176  }
177  }
178 
179 template<typename Alloc>
180 int write_vec_output(uint8_t out[], size_t* out_len, const std::vector<uint8_t, Alloc>& buf)
181  {
182  return write_output(out, out_len, buf.data(), buf.size());
183  }
184 
185 inline int write_str_output(uint8_t out[], size_t* out_len, const std::string& str)
186  {
187  return write_output(out, out_len,
188  reinterpret_cast<const uint8_t*>(str.c_str()),
189  str.size() + 1);
190  }
191 
192 inline int write_str_output(char out[], size_t* out_len, const std::string& str)
193  {
194  return write_str_output(reinterpret_cast<uint8_t*>(out), out_len, str);
195  }
196 
197 inline int write_str_output(char out[], size_t* out_len, const std::vector<uint8_t>& str_vec)
198  {
199  return write_output(reinterpret_cast<uint8_t*>(out), out_len,
200  reinterpret_cast<const uint8_t*>(str_vec.data()),
201  str_vec.size());
202  }
203 
204 #define BOTAN_FFI_DO(T, obj, param, block) apply_fn(obj, BOTAN_CURRENT_FUNCTION, [=](T& param) -> int { do { block } while(0); return 0; })
205 
206 }
207 
208 extern "C" {
209 
210 #define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC) \
211  struct NAME : public botan_struct<TYPE, MAGIC> { explicit NAME(TYPE* x) : botan_struct(x) {} }
212 
213 struct botan_cipher_struct : public botan_struct<Botan::Cipher_Mode, 0xB4A2BF9C>
214  {
215  explicit botan_cipher_struct(Botan::Cipher_Mode* x) : botan_struct(x) {}
217  };
218 
219 BOTAN_FFI_DECLARE_STRUCT(botan_rng_struct, Botan::RandomNumberGenerator, 0x4901F9C1);
220 BOTAN_FFI_DECLARE_STRUCT(botan_mp_struct, Botan::BigInt, 0xC828B9D2);
221 BOTAN_FFI_DECLARE_STRUCT(botan_block_cipher_struct, Botan::BlockCipher, 0x64C29716);
222 BOTAN_FFI_DECLARE_STRUCT(botan_hash_struct, Botan::HashFunction, 0x1F0A4F84);
223 BOTAN_FFI_DECLARE_STRUCT(botan_mac_struct, Botan::MessageAuthenticationCode, 0xA06E8FC1);
224 BOTAN_FFI_DECLARE_STRUCT(botan_pubkey_struct, Botan::Public_Key, 0x2C286519);
225 BOTAN_FFI_DECLARE_STRUCT(botan_privkey_struct, Botan::Private_Key, 0x7F96385E);
226 BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_encrypt_struct, Botan::PK_Encryptor, 0x891F3FC3);
227 BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_decrypt_struct, Botan::PK_Decryptor, 0x912F3C37);
228 BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_sign_struct, Botan::PK_Signer, 0x1AF0C39F);
229 BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_verify_struct, Botan::PK_Verifier, 0x2B91F936);
230 BOTAN_FFI_DECLARE_STRUCT(botan_pk_op_ka_struct, Botan::PK_Key_Agreement, 0x2939CAB1);
231 
232 BOTAN_FFI_DECLARE_STRUCT(botan_x509_cert_struct, Botan::X509_Certificate, 0x8F628937);
233 
234 
235 #if defined(BOTAN_HAS_TLS)
236 BOTAN_FFI_DECLARE_STRUCT(botan_tls_channel_struct, Botan::TLS::Channel, 0x0212FE99);
237 #endif
238 
239 /*
240 * Versioning
241 */
243  {
244  return BOTAN_HAS_FFI;
245  }
246 
247 int botan_ffi_supports_api(uint32_t api_version)
248  {
249  /*
250  * In the future if multiple versions are supported, this
251  * function would accept any of them.
252  */
253  if(api_version == BOTAN_HAS_FFI)
254  return 0;
255  return -1;
256  }
257 
258 const char* botan_version_string()
259  {
260  return Botan::version_cstr();
261  }
262 
267 
268 int botan_same_mem(const uint8_t* x, const uint8_t* y, size_t len)
269  {
270  return Botan::same_mem(x, y, len) ? 0 : -1;
271  }
272 
273 int botan_hex_encode(const uint8_t* in, size_t len, char* out, uint32_t flags)
274  {
275  try
276  {
277  const bool uppercase = (flags & BOTAN_FFI_HEX_LOWER_CASE) == 0;
278  Botan::hex_encode(out, in, len, uppercase);
279  return 0;
280  }
281  catch(std::exception& e)
282  {
283  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
284  }
285 
286  return 1;
287  }
288 
289 int botan_rng_init(botan_rng_t* rng_out, const char* rng_type)
290  {
291  try
292  {
293  BOTAN_ASSERT_ARG_NON_NULL(rng_out);
294 
295  if(rng_type == nullptr || *rng_type == 0)
296  rng_type = "system";
297 
298  const std::string rng_type_s(rng_type);
299 
300  std::unique_ptr<Botan::RandomNumberGenerator> rng;
301 
302  if(rng_type_s == "system")
303  rng.reset(new Botan::System_RNG);
304  else if(rng_type_s == "user")
305  rng.reset(new Botan::AutoSeeded_RNG);
306 
307  if(rng)
308  {
309  *rng_out = new botan_rng_struct(rng.release());
310  return 0;
311  }
312  }
313  catch(std::exception& e)
314  {
315  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
316  }
317  catch(...)
318  {
319  log_exception(BOTAN_CURRENT_FUNCTION, "unknown");
320  }
321 
322  return -1;
323  }
324 
326  {
327  delete rng;
328  return 0;
329  }
330 
331 int botan_rng_get(botan_rng_t rng, uint8_t* out, size_t out_len)
332  {
333  return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.randomize(out, out_len); });
334  }
335 
336 int botan_rng_reseed(botan_rng_t rng, size_t bits)
337  {
339  }
340 
342  {
343  *mp = new botan_mp_struct(new Botan::BigInt);
344  return 0;
345  }
346 
348  {
349  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { bn.clear(); });
350  }
351 
352 int botan_mp_set_from_int(botan_mp_t mp, int initial_value)
353  {
354  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, {
355  if(initial_value >= 0)
356  {
357  bn = Botan::BigInt(static_cast<uint64_t>(initial_value));
358  }
359  else
360  {
361  bn = Botan::BigInt(static_cast<uint64_t>(-initial_value));
362  bn.flip_sign();
363  }
364  });
365  }
366 
367 int botan_mp_set_from_str(botan_mp_t mp, const char* str)
368  {
369  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { bn = Botan::BigInt(str); });
370  }
371 
372 int botan_mp_set_from_radix_str(botan_mp_t mp, const char* str, size_t radix)
373  {
374  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, {
375  Botan::BigInt::Base base;
376  if(radix == 10)
377  base = Botan::BigInt::Decimal;
378  else if(radix == 16)
380  else
382 
383  const uint8_t* bytes = reinterpret_cast<const uint8_t*>(str);
384  const size_t len = strlen(str);
385 
386  bn = Botan::BigInt::decode(bytes, len, base);
387  });
388  }
389 
391  {
392  return BOTAN_FFI_DO(Botan::BigInt, dest, bn, { bn = safe_get(source); });
393  }
394 
396  {
397  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { return bn.is_negative() ? 1 : 0; });
398  }
399 
401  {
402  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { return bn.is_positive() ? 1 : 0; });
403  }
404 
406  {
407  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { bn.flip_sign(); });
408  }
409 
410 int botan_mp_from_bin(botan_mp_t mp, const uint8_t bin[], size_t bin_len)
411  {
412  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { bn.binary_decode(bin, bin_len); });
413  }
414 
415 int botan_mp_to_hex(const botan_mp_t mp, char* out)
416  {
417  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, {
418  std::vector<uint8_t> hex = Botan::BigInt::encode(bn, Botan::BigInt::Hexadecimal);
419  std::memcpy(out, hex.data(), hex.size());
420  out[hex.size()] = 0; // null terminate
421  });
422  }
423 
424 int botan_mp_to_str(const botan_mp_t mp, uint8_t digit_base, char* out, size_t* out_len)
425  {
426  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, {
427  Botan::BigInt::Base base;
428  if(digit_base == 0 || digit_base == 10)
429  base = Botan::BigInt::Decimal;
430  else if(digit_base == 16)
432  else
433  throw FFI_Error("botan_mp_to_str invalid digit base");
434 
435  std::vector<uint8_t> hex = Botan::BigInt::encode(bn, base);
436  hex.push_back(0); // null terminator
437  write_str_output(out, out_len, hex);
438  });
439  }
440 
441 int botan_mp_to_bin(const botan_mp_t mp, uint8_t vec[])
442  {
443  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { bn.binary_encode(vec); });
444  }
445 
446 int botan_mp_to_uint32(const botan_mp_t mp, uint32_t* val)
447  {
448  if(val == nullptr) {
450  }
451  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { *val = bn.to_u32bit(); });
452  }
453 
455  {
456  delete mp;
457  return 0;
458  }
459 
460 int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
461  {
462  return BOTAN_FFI_DO(Botan::BigInt, result, res, { res = safe_get(x) + safe_get(y); });
463  }
464 
465 int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
466  {
467  return BOTAN_FFI_DO(Botan::BigInt, result, res, { res = safe_get(x) - safe_get(y); });
468  }
469 
470 int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
471  {
472  return BOTAN_FFI_DO(Botan::BigInt, result, res, { res = safe_get(x) * safe_get(y); });
473  }
474 
476  botan_mp_t remainder,
477  const botan_mp_t x, const botan_mp_t y)
478  {
479  return BOTAN_FFI_DO(Botan::BigInt, quotient, q, {
480  Botan::BigInt r;
481  Botan::divide(safe_get(x), safe_get(y), q, r);
482  safe_get(remainder) = r;
483  });
484  }
485 
486 int botan_mp_equal(const botan_mp_t x_w, const botan_mp_t y_w)
487  {
488  return BOTAN_FFI_DO(Botan::BigInt, x_w, x, { return x == safe_get(y_w); });
489  }
490 
492  {
493  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { return bn.is_zero(); });
494  }
495 
497  {
498  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { return bn.is_odd(); });
499  }
500 
502  {
503  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { return bn.is_even(); });
504  }
505 
506 int botan_mp_cmp(int* result, const botan_mp_t x_w, const botan_mp_t y_w)
507  {
508  return BOTAN_FFI_DO(Botan::BigInt, x_w, x, { *result = x.cmp(safe_get(y_w)); });
509  }
510 
512  {
513  return BOTAN_FFI_DO(Botan::BigInt, x_w, x, { x.swap(safe_get(y_w)); });
514  }
515 
516 // Return (base^exponent) % modulus
517 int botan_mp_powmod(botan_mp_t out, const botan_mp_t base, const botan_mp_t exponent, const botan_mp_t modulus)
518  {
519  return BOTAN_FFI_DO(Botan::BigInt, out, o,
520  { o = Botan::power_mod(safe_get(base), safe_get(exponent), safe_get(modulus)); });
521  }
522 
523 int botan_mp_lshift(botan_mp_t out, const botan_mp_t in, size_t shift)
524  {
525  return BOTAN_FFI_DO(Botan::BigInt, out, o, { o = safe_get(in) << shift; });
526  }
527 
528 int botan_mp_rshift(botan_mp_t out, const botan_mp_t in, size_t shift)
529  {
530  return BOTAN_FFI_DO(Botan::BigInt, out, o, { o = safe_get(in) >> shift; });
531  }
532 
533 int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus)
534  {
535  return BOTAN_FFI_DO(Botan::BigInt, out, o, { o = Botan::inverse_mod(safe_get(in), safe_get(modulus)); });
536  }
537 
538 int botan_mp_mod_mul(botan_mp_t out, const botan_mp_t x, const botan_mp_t y, const botan_mp_t modulus)
539  {
540  return BOTAN_FFI_DO(Botan::BigInt, out, o, {
541  Botan::Modular_Reducer reducer(safe_get(modulus));
542  o = reducer.multiply(safe_get(x), safe_get(y));
543  });
544  }
545 
546 int botan_mp_rand_bits(botan_mp_t rand_out, botan_rng_t rng, size_t bits)
547  {
549  safe_get(rand_out).randomize(r, bits); });
550  }
551 
553  botan_rng_t rng,
554  const botan_mp_t lower,
555  const botan_mp_t upper)
556  {
558  safe_get(rand_out) = Botan::BigInt::random_integer(r, safe_get(lower), safe_get(upper)); });
559  }
560 
561 int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y)
562  {
563  return BOTAN_FFI_DO(Botan::BigInt, out, o, {
564  o = Botan::gcd(safe_get(x), safe_get(y)); });
565  }
566 
567 int botan_mp_is_prime(const botan_mp_t mp, botan_rng_t rng, size_t test_prob)
568  {
569  return BOTAN_FFI_DO(Botan::BigInt, mp, n,
570  { return (Botan::is_prime(n, safe_get(rng), test_prob)) ? 1 : 0; });
571  }
572 
573 int botan_mp_get_bit(const botan_mp_t mp, size_t bit)
574  {
575  return BOTAN_FFI_DO(Botan::BigInt, mp, n, { return (n.get_bit(bit)); });
576  }
577 
578 int botan_mp_set_bit(botan_mp_t mp, size_t bit)
579  {
580  return BOTAN_FFI_DO(Botan::BigInt, mp, n, { n.set_bit(bit); });
581  }
582 
583 int botan_mp_clear_bit(botan_mp_t mp, size_t bit)
584  {
585  return BOTAN_FFI_DO(Botan::BigInt, mp, n, { n.clear_bit(bit); });
586  }
587 
588 int botan_mp_num_bits(const botan_mp_t mp, size_t* bits)
589  {
590  return BOTAN_FFI_DO(Botan::BigInt, mp, n, { *bits = n.bits(); });
591  }
592 
593 int botan_mp_num_bytes(const botan_mp_t mp, size_t* bytes)
594  {
595  return BOTAN_FFI_DO(Botan::BigInt, mp, n, { *bytes = n.bytes(); });
596  }
597 
598 int botan_block_cipher_init(botan_block_cipher_t* bc, const char* bc_name)
599  {
600  try
601  {
602  if(bc == nullptr || bc_name == nullptr || *bc_name == 0)
604 
605  std::unique_ptr<Botan::BlockCipher> cipher(Botan::BlockCipher::create(bc_name));
606  if(cipher)
607  {
608  *bc = new botan_block_cipher_struct(cipher.release());
609  return 0;
610  }
611  }
612  catch(std::exception& e)
613  {
614  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
615  }
616  catch(...)
617  {
618  log_exception(BOTAN_CURRENT_FUNCTION, "unknown");
619  }
620 
622 
623  }
624 
625 /**
626 * Destroy a block cipher object
627 */
629  {
630  delete bc;
631  return 0;
632  }
633 
635  {
636  return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.clear(); });
637  }
638 
639 /**
640 * Set the key for a block cipher instance
641 */
643  const uint8_t key[], size_t len)
644  {
645  return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.set_key(key, len); });
646  }
647 
648 /**
649 * Return the positive block size of this block cipher, or negative to
650 * indicate an error
651 */
653  {
654  return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { return b.block_size(); });
655  }
656 
658  const uint8_t in[],
659  uint8_t out[],
660  size_t blocks)
661  {
662  return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.encrypt_n(in, out, blocks); });
663  }
664 
666  const uint8_t in[],
667  uint8_t out[],
668  size_t blocks)
669  {
670  return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.decrypt_n(in, out, blocks); });
671  }
672 
673 int botan_hash_init(botan_hash_t* hash, const char* hash_name, uint32_t flags)
674  {
675  try
676  {
677  if(hash == nullptr || hash_name == nullptr || *hash_name == 0)
679  if(flags != 0)
681 
682  auto h = Botan::HashFunction::create(hash_name);
683  if(h)
684  {
685  *hash = new botan_hash_struct(h.release());
686  return 0;
687  }
688  }
689  catch(std::exception& e)
690  {
691  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
692  }
693  catch(...)
694  {
695  log_exception(BOTAN_CURRENT_FUNCTION, "unknown");
696  }
697 
699  }
700 
702  {
703  delete hash;
704  return 0;
705  }
706 
708  {
709  return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { *out = h.output_length(); });
710  }
711 
713  {
714  return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { h.clear(); });
715  }
716 
717 int botan_hash_update(botan_hash_t hash, const uint8_t* buf, size_t len)
718  {
719  return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { h.update(buf, len); });
720  }
721 
722 int botan_hash_final(botan_hash_t hash, uint8_t out[])
723  {
724  return BOTAN_FFI_DO(Botan::HashFunction, hash, h, { h.final(out); });
725  }
726 
727 int botan_mac_init(botan_mac_t* mac, const char* mac_name, uint32_t flags)
728  {
729  try
730  {
731  if(!mac || !mac_name || flags != 0)
732  return -1;
733 
734  auto m = Botan::MessageAuthenticationCode::create(mac_name);
735  if(m)
736  {
737  *mac = new botan_mac_struct(m.release());
738  return 0;
739  }
740  }
741  catch(std::exception& e)
742  {
743  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
744  }
745  catch(...)
746  {
747  log_exception(BOTAN_CURRENT_FUNCTION, "unknown");
748  }
749 
750  return -2;
751  }
752 
754  {
755  delete mac;
756  return 0;
757  }
758 
759 int botan_mac_set_key(botan_mac_t mac, const uint8_t* key, size_t key_len)
760  {
761  return BOTAN_FFI_DO(Botan::MessageAuthenticationCode, mac, m, { m.set_key(key, key_len); });
762  }
763 
765  {
766  return BOTAN_FFI_DO(Botan::MessageAuthenticationCode, mac, m, { *out = m.output_length(); });
767  }
768 
770  {
771  return BOTAN_FFI_DO(Botan::MessageAuthenticationCode, mac, m, { m.clear(); });
772  }
773 
774 int botan_mac_update(botan_mac_t mac, const uint8_t* buf, size_t len)
775  {
776  return BOTAN_FFI_DO(Botan::MessageAuthenticationCode, mac, m, { m.update(buf, len); });
777  }
778 
779 int botan_mac_final(botan_mac_t mac, uint8_t out[])
780  {
781  return BOTAN_FFI_DO(Botan::MessageAuthenticationCode, mac, m, { m.final(out); });
782  }
783 
784 int botan_cipher_init(botan_cipher_t* cipher, const char* cipher_name, uint32_t flags)
785  {
786  try
787  {
788  const bool encrypt_p = ((flags & BOTAN_CIPHER_INIT_FLAG_MASK_DIRECTION) == BOTAN_CIPHER_INIT_FLAG_ENCRYPT);
789  const Botan::Cipher_Dir dir = encrypt_p ? Botan::ENCRYPTION : Botan::DECRYPTION;
790  std::unique_ptr<Botan::Cipher_Mode> mode(Botan::get_cipher_mode(cipher_name, dir));
791  if(!mode)
792  return -1;
793  *cipher = new botan_cipher_struct(mode.release());
794  return 0;
795  }
796  catch(std::exception& e)
797  {
798  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
799  }
800  catch(...)
801  {
802  log_exception(BOTAN_CURRENT_FUNCTION, "unknown");
803  }
804 
805  return -1;
806  }
807 
809  {
810  delete cipher;
811  return 0;
812  }
813 
815  {
816  return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, { c.clear(); });
817  }
818 
820  size_t* out_minimum_keylength,
821  size_t* out_maximum_keylength)
822  {
823  return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, {
824  *out_minimum_keylength = c.key_spec().minimum_keylength();
825  *out_maximum_keylength = c.key_spec().maximum_keylength();
826  });
827  }
828 
830  const uint8_t* key, size_t key_len)
831  {
832  return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, { c.set_key(key, key_len); });
833  }
834 
836  const uint8_t* nonce, size_t nonce_len)
837  {
838  try
839  {
840  Botan::Cipher_Mode& cipher = safe_get(cipher_obj);
841  cipher.start(nonce, nonce_len);
842  cipher_obj->m_buf.reserve(cipher.update_granularity());
843  return 0;
844  }
845  catch(std::exception& e)
846  {
847  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
848  }
849 
850  return -1;
851  }
852 
854  uint32_t flags,
855  uint8_t output[],
856  size_t output_size,
857  size_t* output_written,
858  const uint8_t input[],
859  size_t input_size,
860  size_t* input_consumed)
861  {
862  using namespace Botan;
863 
864  try
865  {
866  Cipher_Mode& cipher = safe_get(cipher_obj);
867  secure_vector<uint8_t>& mbuf = cipher_obj->m_buf;
868 
869  const bool final_input = (flags & BOTAN_CIPHER_UPDATE_FLAG_FINAL);
870 
871  if(final_input)
872  {
873  mbuf.assign(input, input + input_size);
874  *input_consumed = input_size;
875  *output_written = 0;
876 
877  try
878  {
879  cipher.finish(mbuf);
880  }
881  catch(Integrity_Failure& e)
882  {
883  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
884  return -2;
885  }
886 
887  *output_written = mbuf.size();
888 
889  if(mbuf.size() <= output_size)
890  {
891  copy_mem(output, mbuf.data(), mbuf.size());
892  mbuf.clear();
893  return 0;
894  }
895 
896  return -1;
897  }
898 
899  if(input_size == 0)
900  {
901  // Currently must take entire buffer in this case
902  *output_written = mbuf.size();
903  if(output_size >= mbuf.size())
904  {
905  copy_mem(output, mbuf.data(), mbuf.size());
906  mbuf.clear();
907  return 0;
908  }
909 
910  return -1;
911  }
912 
913  const size_t ud = cipher.update_granularity();
914  BOTAN_ASSERT(cipher.update_granularity() > cipher.minimum_final_size(), "logic error");
915 
916 #if 0
917  // Avoiding double copy:
918  if(Online_Cipher_Mode* ocm = dynamic_cast<Online_Cipher_Mode*>(&cipher))
919  {
920  const size_t taken = round_down(input_size, ud);
921  *input_consumed = taken;
922  *output_size = taken;
923  copy_mem(output, input, taken);
924  ocm->update_in_place(output, taken);
925  return 0;
926  }
927 #endif
928 
929  mbuf.resize(ud);
930  size_t taken = 0, written = 0;
931 
932  while(input_size >= ud && output_size >= ud)
933  {
934  copy_mem(mbuf.data(), input, ud);
935  cipher.update(mbuf);
936 
937  input_size -= ud;
938  input += ud;
939  taken += ud;
940 
941  output_size -= ud;
942  output += ud;
943  written += ud;
944  }
945 
946  *output_written = written;
947  *input_consumed = taken;
948 
949  }
950  catch(std::exception& e)
951  {
952  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
953  }
954 
955  return -1;
956  }
957 
959  const uint8_t* ad,
960  size_t ad_len)
961  {
962  return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, {
963  if(Botan::AEAD_Mode* aead = dynamic_cast<Botan::AEAD_Mode*>(&c))
964  {
965  aead->set_associated_data(ad, ad_len);
966  return 0;
967  }
968  return -1;
969  });
970  }
971 
973  {
974  return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, { return c.valid_nonce_length(nl) ? 1 : 0; });
975  }
976 
978  {
979  return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, { *nl = c.default_nonce_length(); });
980  }
981 
983  {
984  return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, { *ug = c.update_granularity(); });
985  }
986 
988  {
989  return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, { *tl = c.tag_size(); });
990  }
991 
992 int botan_pbkdf(const char* pbkdf_algo, uint8_t out[], size_t out_len,
993  const char* pass, const uint8_t salt[], size_t salt_len,
994  size_t iterations)
995  {
996  try
997  {
998  std::unique_ptr<Botan::PBKDF> pbkdf(Botan::get_pbkdf(pbkdf_algo));
999  pbkdf->pbkdf_iterations(out, out_len, pass, salt, salt_len, iterations);
1000  return 0;
1001  }
1002  catch(std::exception& e)
1003  {
1004  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1005  }
1006 
1007  return -1;
1008  }
1009 
1010 int botan_pbkdf_timed(const char* pbkdf_algo,
1011  uint8_t out[], size_t out_len,
1012  const char* password,
1013  const uint8_t salt[], size_t salt_len,
1014  size_t ms_to_run,
1015  size_t* iterations_used)
1016  {
1017  try
1018  {
1019  std::unique_ptr<Botan::PBKDF> pbkdf(Botan::get_pbkdf(pbkdf_algo));
1020  pbkdf->pbkdf_timed(out, out_len, password, salt, salt_len,
1021  std::chrono::milliseconds(ms_to_run),
1022  *iterations_used);
1023  return 0;
1024  }
1025  catch(std::exception& e)
1026  {
1027  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1028  }
1029 
1030  return -1;
1031  }
1032 
1033 int botan_kdf(const char* kdf_algo,
1034  uint8_t out[], size_t out_len,
1035  const uint8_t secret[], size_t secret_len,
1036  const uint8_t salt[], size_t salt_len,
1037  const uint8_t label[], size_t label_len)
1038  {
1039  try
1040  {
1041  std::unique_ptr<Botan::KDF> kdf(Botan::get_kdf(kdf_algo));
1042  kdf->kdf(out, out_len, secret, secret_len, salt, salt_len, label, label_len);
1043  return 0;
1044  }
1045  catch(std::exception& e)
1046  {
1047  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1048  }
1049 
1050  return -1;
1051  }
1052 
1053 int botan_bcrypt_generate(uint8_t* out, size_t* out_len,
1054  const char* pass,
1055  botan_rng_t rng_obj, size_t wf,
1056  uint32_t flags)
1057  {
1058  try
1059  {
1061  BOTAN_ASSERT_ARG_NON_NULL(out_len);
1063 
1064  if(flags != 0)
1065  return BOTAN_FFI_ERROR_BAD_FLAG;
1066 
1067  if(wf < 2 || wf > 30)
1068  throw FFI_Error("Bad bcrypt work factor " + std::to_string(wf));
1069 
1070 #if defined(BOTAN_HAS_BCRYPT)
1071  Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
1072  const std::string bcrypt = Botan::generate_bcrypt(pass, rng, wf);
1073  return write_str_output(out, out_len, bcrypt);
1074 #else
1076 #endif
1077  }
1078  catch(std::exception& e)
1079  {
1080  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1081  }
1082  catch(...)
1083  {
1084  log_exception(BOTAN_CURRENT_FUNCTION, "unknown");
1085  }
1086 
1088  }
1089 
1090 int botan_bcrypt_is_valid(const char* pass, const char* hash)
1091  {
1092  try
1093  {
1094 #if defined(BOTAN_HAS_BCRYPT)
1095  if(Botan::check_bcrypt(pass, hash))
1096  return 0; // success
1097 #else
1099 #endif
1100  }
1101  catch(std::exception& e)
1102  {
1103  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1104  }
1105  catch(...)
1106  {
1107  log_exception(BOTAN_CURRENT_FUNCTION, "unknown");
1108  }
1109 
1111  }
1112 
1114  const char* algo_name,
1115  const char* algo_params,
1116  botan_rng_t rng_obj)
1117  {
1118  try
1119  {
1120  if(key_obj == nullptr)
1122 
1123  *key_obj = nullptr;
1124  if(rng_obj == nullptr)
1126 
1127  if(algo_name == nullptr)
1128  algo_name = "RSA";
1129  if(algo_params == nullptr)
1130  algo_params = "";
1131 
1132  Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
1133  std::unique_ptr<Botan::Private_Key> key(
1134  Botan::create_private_key(algo_name, rng, algo_params));
1135 
1136  if(key)
1137  {
1138  *key_obj = new botan_privkey_struct(key.release());
1139  return 0;
1140  }
1141  else
1142  {
1144  }
1145  }
1146  catch(std::exception& e)
1147  {
1148  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1149  }
1150 
1152  }
1153 
1154 int botan_privkey_create_rsa(botan_privkey_t* key_obj, botan_rng_t rng_obj, size_t n_bits)
1155  {
1156  try
1157  {
1158  if(key_obj == nullptr || rng_obj == nullptr)
1159  return -1;
1160  if(n_bits < 1024 || n_bits > 16*1024)
1161  return -2;
1162 
1163  *key_obj = nullptr;
1164 
1165 #if defined(BOTAN_HAS_RSA)
1166  Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
1167  std::unique_ptr<Botan::Private_Key> key(new Botan::RSA_PrivateKey(rng, n_bits));
1168  *key_obj = new botan_privkey_struct(key.release());
1169  return 0;
1170 #else
1172 #endif
1173  }
1174  catch(std::exception& e)
1175  {
1176  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1177  }
1178 
1180  }
1181 
1182 
1183 int botan_privkey_create_ecdsa(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str)
1184  {
1185  try
1186  {
1187  if(key_obj == nullptr || rng_obj == nullptr || param_str == nullptr || *param_str == 0)
1188  return -1;
1189 
1190  *key_obj = nullptr;
1191 
1192 #if defined(BOTAN_HAS_ECDSA)
1193  Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
1194  Botan::EC_Group grp(param_str);
1195  std::unique_ptr<Botan::Private_Key> key(new Botan::ECDSA_PrivateKey(rng, grp));
1196  *key_obj = new botan_privkey_struct(key.release());
1197  return 0;
1198 #else
1200 #endif
1201  }
1202  catch(std::exception& e)
1203  {
1204  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1205  }
1206 
1208  }
1209 
1210 int botan_privkey_create_mceliece(botan_privkey_t* key_obj, botan_rng_t rng_obj, size_t n, size_t t)
1211  {
1212  try
1213  {
1214  if(key_obj == nullptr || rng_obj == nullptr || n == 0 || t == 0)
1215  return -1;
1216 
1217  *key_obj = nullptr;
1218 
1219 #if defined(BOTAN_HAS_MCELIECE)
1220  Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
1221  std::unique_ptr<Botan::Private_Key> key(new Botan::McEliece_PrivateKey(rng, n, t));
1222  *key_obj = new botan_privkey_struct(key.release());
1223  return 0;
1224 #else
1226 #endif
1227  }
1228  catch(std::exception& e)
1229  {
1230  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1232  }
1233  }
1234 
1235 int botan_privkey_create_ecdh(botan_privkey_t* key_obj, botan_rng_t rng_obj, const char* param_str)
1236  {
1237  try
1238  {
1239  if(key_obj == nullptr || rng_obj == nullptr || param_str == nullptr || *param_str == 0)
1240  return -1;
1241 
1242  *key_obj = nullptr;
1243 
1244  const std::string params(param_str);
1245 
1246 #if defined(BOTAN_HAS_CURVE_25519)
1247  if(params == "curve25519")
1248  {
1249  std::unique_ptr<Botan::Private_Key> key(new Botan::Curve25519_PrivateKey(safe_get(rng_obj)));
1250  *key_obj = new botan_privkey_struct(key.release());
1251  return 0;
1252  }
1253 #endif
1254 
1255 #if defined(BOTAN_HAS_ECDH)
1256  Botan::EC_Group grp(params);
1257  std::unique_ptr<Botan::Private_Key> key(new Botan::ECDH_PrivateKey(safe_get(rng_obj), grp));
1258  *key_obj = new botan_privkey_struct(key.release());
1259  return 0;
1260 #endif
1261  }
1262  catch(std::exception& e)
1263  {
1264  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1265  }
1266 
1268  }
1269 
1271  const uint8_t bits[], size_t len,
1272  const char* password)
1273  {
1274  *key = nullptr;
1275 
1276  try
1277  {
1278  Botan::DataSource_Memory src(bits, len);
1279 
1280  if(password == nullptr)
1281  password = "";
1282 
1283  Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
1284 
1285  std::unique_ptr<Botan::PKCS8_PrivateKey> pkcs8;
1286  pkcs8.reset(Botan::PKCS8::load_key(src, rng, static_cast<std::string>(password)));
1287 
1288  if(pkcs8)
1289  {
1290  *key = new botan_privkey_struct(pkcs8.release());
1291  return 0;
1292  }
1293  }
1294  catch(std::exception& e)
1295  {
1296  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1297  }
1298 
1299  return -1;
1300  }
1301 
1303  const uint8_t bits[], size_t bits_len)
1304  {
1305  *key = nullptr;
1306 
1307  try
1308  {
1309  Botan::DataSource_Memory src(bits, bits_len);
1310  std::unique_ptr<Botan::Public_Key> pubkey(Botan::X509::load_key(src));
1311 
1312  if(pubkey)
1313  {
1314  *key = new botan_pubkey_struct(pubkey.release());
1315  return 0;
1316  }
1317  }
1318  catch(std::exception& e)
1319  {
1320  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1321  }
1322 
1323  return -1;
1324  }
1325 
1328  {
1329  *key = nullptr;
1330 
1331 #if defined(BOTAN_HAS_RSA)
1332  try
1333  {
1334  *key = new botan_privkey_struct(new Botan::RSA_PrivateKey(safe_get(p),
1335  safe_get(q),
1336  safe_get(d)));
1337  return 0;
1338  }
1339  catch(std::exception& e)
1340  {
1341  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1342  }
1343  return -1;
1344 #else
1346 #endif
1347  }
1348 
1350  botan_mp_t n, botan_mp_t e)
1351  {
1352  *key = nullptr;
1353 
1354 #if defined(BOTAN_HAS_RSA)
1355  try
1356  {
1357  *key = new botan_pubkey_struct(new Botan::RSA_PublicKey(safe_get(n), safe_get(e)));
1358  return 0;
1359  }
1360  catch(std::exception& exn)
1361  {
1362  log_exception(BOTAN_CURRENT_FUNCTION, exn.what());
1363  }
1364 
1365  return -1;
1366 #else
1368 #endif
1369  }
1370 
1373  {
1374  *key = nullptr;
1375 
1376 #if defined(BOTAN_HAS_DSA)
1377  try
1378  {
1379  Botan::Null_RNG null_rng;
1380  Botan::DL_Group group(safe_get(p), safe_get(q), safe_get(g));
1381  *key = new botan_privkey_struct(new Botan::DSA_PrivateKey(null_rng, group, safe_get(x)));
1382  return 0;
1383  }
1384  catch(std::exception& e)
1385  {
1386  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1387  }
1388  return -1;
1389 #else
1390  BOTAN_UNUSED(p);
1391  BOTAN_UNUSED(q);
1392  BOTAN_UNUSED(g);
1393  BOTAN_UNUSED(x);
1395 #endif
1396  }
1397 
1400  {
1401  *key = nullptr;
1402 
1403 #if defined(BOTAN_HAS_DSA)
1404  try
1405  {
1406  Botan::DL_Group group(safe_get(p), safe_get(q), safe_get(g));
1407  *key = new botan_pubkey_struct(new Botan::DSA_PublicKey(group, safe_get(y)));
1408  return 0;
1409  }
1410  catch(std::exception& exn)
1411  {
1412  log_exception(BOTAN_CURRENT_FUNCTION, exn.what());
1413  }
1414 
1415  return -1;
1416 #else
1417  BOTAN_UNUSED(p);
1418  BOTAN_UNUSED(q);
1419  BOTAN_UNUSED(g);
1420  BOTAN_UNUSED(y);
1422 #endif
1423  }
1424 
1425 namespace {
1426 
1427 Botan::BigInt botan_pubkey_do_get_field(const Botan::Public_Key& key,
1428  const std::string& field)
1429  {
1430  // Maybe this should be `return key.get_integer_field(field_name)`?
1431 
1432 #if defined(BOTAN_HAS_RSA)
1433  if(const Botan::RSA_PublicKey* rsa = dynamic_cast<const Botan::RSA_PublicKey*>(&key))
1434  {
1435  if(field == "n")
1436  return rsa->get_n();
1437  else if(field == "e")
1438  return rsa->get_e();
1439  else
1440  throw Botan::Exception("Field not supported");
1441  }
1442 #endif
1443 
1444 #if defined(BOTAN_HAS_DL_PUBLIC_KEY_FAMILY)
1445  // Handles DSA, ElGamal, etc
1446  if(const Botan::DL_Scheme_PublicKey* dl = dynamic_cast<const Botan::DL_Scheme_PublicKey*>(&key))
1447  {
1448  if(field == "p")
1449  return dl->group_p();
1450  else if(field == "q")
1451  return dl->group_q();
1452  else if(field == "g")
1453  return dl->group_g();
1454  else if(field == "y")
1455  return dl->get_y();
1456  else
1457  throw Botan::Exception("Field not supported");
1458  }
1459 #endif
1460 
1461 #if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
1462  if(const Botan::EC_PublicKey* ecc = dynamic_cast<const Botan::EC_PublicKey*>(&key))
1463  {
1464  if(field == "public_x")
1465  return ecc->public_point().get_affine_x();
1466  else if(field == "public_y")
1467  return ecc->public_point().get_affine_y();
1468  else if(field == "base_x")
1469  return ecc->domain().get_base_point().get_affine_x();
1470  else if(field == "base_y")
1471  return ecc->domain().get_base_point().get_affine_y();
1472  else if(field == "p")
1473  return ecc->domain().get_curve().get_p();
1474  else if(field == "a")
1475  return ecc->domain().get_curve().get_a();
1476  else if(field == "b")
1477  return ecc->domain().get_curve().get_b();
1478  else if(field == "cofactor")
1479  return ecc->domain().get_cofactor();
1480  else if(field == "order")
1481  return ecc->domain().get_order();
1482  else
1483  throw Botan::Exception("Field not supported");
1484  }
1485 #endif
1486 
1487  // Some other algorithm type not supported by this function
1488  throw Botan::Exception("Unsupported algorithm type for botan_pubkey_get_field");
1489  }
1490 
1491 Botan::BigInt botan_privkey_do_get_field(const Botan::Private_Key& key,
1492  const std::string& field)
1493  {
1494  //return key.get_integer_field(field);
1495 
1496 #if defined(BOTAN_HAS_RSA)
1497 
1498  if(const Botan::RSA_PrivateKey* rsa = dynamic_cast<const Botan::RSA_PrivateKey*>(&key))
1499  {
1500  if(field == "p")
1501  return rsa->get_p();
1502  else if(field == "q")
1503  return rsa->get_q();
1504  else if(field == "d")
1505  return rsa->get_d();
1506  else if(field == "c")
1507  return rsa->get_c();
1508  else if(field == "d1")
1509  return rsa->get_d1();
1510  else if(field == "d2")
1511  return rsa->get_d2();
1512  else
1513  return botan_pubkey_do_get_field(key, field);
1514  }
1515 #endif
1516 
1517 #if defined(BOTAN_HAS_DL_PUBLIC_KEY_FAMILY)
1518  // Handles DSA, ElGamal, etc
1519  if(const Botan::DL_Scheme_PrivateKey* dl = dynamic_cast<const Botan::DL_Scheme_PrivateKey*>(&key))
1520  {
1521  if(field == "x")
1522  return dl->get_x();
1523  else
1524  return botan_pubkey_do_get_field(key, field);
1525  }
1526 #endif
1527 
1528 #if defined(BOTAN_HAS_ECC_PUBLIC_KEY_CRYPTO)
1529  if(const Botan::EC_PrivateKey* ecc = dynamic_cast<const Botan::EC_PrivateKey*>(&key))
1530  {
1531  if(field == "x")
1532  return ecc->private_value();
1533  else
1534  return botan_pubkey_do_get_field(key, field);
1535  }
1536 #endif
1537 
1538  // Some other algorithm type not supported by this function
1539  throw Botan::Exception("Unsupported algorithm type for botan_privkey_get_field");
1540  }
1541 
1542 }
1543 
1545  botan_pubkey_t key,
1546  const char* field_name_cstr)
1547  {
1548  if(field_name_cstr == nullptr)
1550 
1551  const std::string field_name(field_name_cstr);
1552 
1553  return BOTAN_FFI_DO(Botan::Public_Key, key, k, {
1554  safe_get(output) = botan_pubkey_do_get_field(k, field_name);
1555  });
1556  }
1557 
1559  botan_privkey_t key,
1560  const char* field_name_cstr)
1561  {
1562  if(field_name_cstr == nullptr)
1564 
1565  const std::string field_name(field_name_cstr);
1566 
1567  return BOTAN_FFI_DO(Botan::Private_Key, key, k, {
1568  safe_get(output) = botan_privkey_do_get_field(k, field_name);
1569  });
1570  }
1571 
1573  {
1574  return botan_privkey_get_field(p, key, "p");
1575  }
1576 
1578  {
1579  return botan_privkey_get_field(q, key, "q");
1580  }
1581 
1583  {
1584  return botan_privkey_get_field(n, key, "n");
1585  }
1586 
1588  {
1589  return botan_privkey_get_field(e, key, "e");
1590  }
1591 
1593  {
1594  return botan_privkey_get_field(d, key, "d");
1595  }
1596 
1598  {
1599  return botan_pubkey_get_field(e, key, "e");
1600  }
1601 
1603  {
1604  return botan_pubkey_get_field(n, key, "n");
1605  }
1606 
1608  {
1609  return botan_privkey_get_field(x, key, "x");
1610  }
1611 
1613  {
1614  return botan_pubkey_get_field(p, key, "p");
1615  }
1617  {
1618  return botan_pubkey_get_field(q, key, "q");
1619  }
1621  {
1622  return botan_pubkey_get_field(g, key, "g");
1623  }
1625  {
1626  return botan_pubkey_get_field(y, key, "y");
1627  }
1628 
1629 
1631  {
1632  delete key;
1633  return 0;
1634  }
1635 
1637  {
1638  delete key;
1639  return 0;
1640  }
1641 
1643  {
1644  try
1645  {
1646  std::unique_ptr<Botan::Public_Key> pubkey(
1648  Botan::X509::BER_encode(safe_get(key_obj))));
1649  *pubout = new botan_pubkey_struct(pubkey.release());
1650  return 0;
1651  }
1652  catch(std::exception& e)
1653  {
1654  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1655  }
1656 
1658  }
1659 
1660 int botan_pubkey_algo_name(botan_pubkey_t key, char out[], size_t* out_len)
1661  {
1662  return BOTAN_FFI_DO(Botan::Public_Key, key, k, { return write_str_output(out, out_len, k.algo_name()); });
1663  }
1664 
1666  {
1667  const bool strong = (flags & BOTAN_CHECK_KEY_EXPENSIVE_TESTS);
1668 
1669  return BOTAN_FFI_DO(Botan::Public_Key, key, k,
1670  { return (k.check_key(safe_get(rng), strong) == true) ? 0 : -1; });
1671  }
1672 
1674  {
1675  const bool strong = (flags & BOTAN_CHECK_KEY_EXPENSIVE_TESTS);
1676  return BOTAN_FFI_DO(Botan::Private_Key, key, k,
1677  { return (k.check_key(safe_get(rng), strong) == true) ? 0 : -1; });
1678  }
1679 
1680 int botan_pubkey_export(botan_pubkey_t key, uint8_t out[], size_t* out_len, uint32_t flags)
1681  {
1682  return BOTAN_FFI_DO(Botan::Public_Key, key, k, {
1683  if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER)
1684  return write_vec_output(out, out_len, Botan::X509::BER_encode(k));
1685  else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM)
1686  return write_str_output(out, out_len, Botan::X509::PEM_encode(k));
1687  else
1688  return -2;
1689  });
1690  }
1691 
1692 int botan_privkey_export(botan_privkey_t key, uint8_t out[], size_t* out_len, uint32_t flags)
1693  {
1694  return BOTAN_FFI_DO(Botan::Private_Key, key, k, {
1695  if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER)
1696  return write_vec_output(out, out_len, Botan::PKCS8::BER_encode(k));
1697  else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM)
1698  return write_str_output(out, out_len, Botan::PKCS8::PEM_encode(k));
1699  else
1700  return -2;
1701  });
1702  }
1703 
1705  uint8_t out[], size_t* out_len,
1706  botan_rng_t rng_obj,
1707  const char* pass,
1708  const char* /*ignored - pbe*/,
1709  uint32_t flags)
1710  {
1711  return botan_privkey_export_encrypted_pbkdf_iter(key, out, out_len, rng_obj, pass, 100000, nullptr, nullptr, flags);
1712  }
1713 
1715  uint8_t out[], size_t* out_len,
1716  botan_rng_t rng_obj,
1717  const char* pass,
1718  uint32_t pbkdf_msec,
1719  size_t* pbkdf_iters_out,
1720  const char* maybe_cipher,
1721  const char* maybe_pbkdf_hash,
1722  uint32_t flags)
1723  {
1724  return BOTAN_FFI_DO(Botan::Private_Key, key, k, {
1725  const std::chrono::milliseconds pbkdf_time(pbkdf_msec);
1726  Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
1727 
1728  std::string cipher;
1729  if(maybe_cipher)
1730  {
1731  cipher = maybe_cipher;
1732  }
1733 
1734  std::string pbkdf_hash;
1735  if(maybe_pbkdf_hash)
1736  {
1737  pbkdf_hash = maybe_pbkdf_hash;
1738  }
1739 
1740  if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER)
1741  {
1742  return write_vec_output(out, out_len,
1743  Botan::PKCS8::BER_encode_encrypted_pbkdf_msec(k, rng, pass, pbkdf_time, pbkdf_iters_out, cipher, pbkdf_hash));
1744  }
1745  else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM)
1746  {
1747  return write_str_output(out, out_len,
1748  Botan::PKCS8::PEM_encode_encrypted_pbkdf_msec(k, rng, pass, pbkdf_time, pbkdf_iters_out, cipher, pbkdf_hash));
1749  }
1750  else
1751  {
1752  return -2;
1753  }
1754  });
1755  }
1756 
1758  uint8_t out[], size_t* out_len,
1759  botan_rng_t rng_obj,
1760  const char* pass,
1761  size_t pbkdf_iter,
1762  const char* maybe_cipher,
1763  const char* maybe_pbkdf_hash,
1764  uint32_t flags)
1765  {
1766  return BOTAN_FFI_DO(Botan::Private_Key, key, k, {
1767  Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
1768 
1769  std::string cipher;
1770  if(maybe_cipher)
1771  {
1772  cipher = maybe_cipher;
1773  }
1774 
1775  std::string pbkdf_hash;
1776  if(maybe_pbkdf_hash)
1777  {
1778  pbkdf_hash = maybe_pbkdf_hash;
1779  }
1780 
1781  if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER)
1782  {
1783  return write_vec_output(out, out_len,
1784  Botan::PKCS8::BER_encode_encrypted_pbkdf_iter(k, rng, pass, pbkdf_iter, cipher, pbkdf_hash));
1785  }
1786  else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM)
1787  {
1788  return write_str_output(out, out_len,
1789  Botan::PKCS8::PEM_encode_encrypted_pbkdf_iter(k, rng, pass, pbkdf_iter, cipher, pbkdf_hash));
1790  }
1791  else
1792  {
1793  return -2;
1794  }
1795  });
1796  }
1797 
1799  {
1800  return BOTAN_FFI_DO(Botan::Public_Key, key, k, { *estimate = k.estimated_strength(); });
1801  }
1802 
1803 int botan_pubkey_fingerprint(botan_pubkey_t key, const char* hash_fn,
1804  uint8_t out[], size_t* out_len)
1805  {
1806  return BOTAN_FFI_DO(Botan::Public_Key, key, k, {
1807  std::unique_ptr<Botan::HashFunction> h(Botan::HashFunction::create(hash_fn));
1808  return write_vec_output(out, out_len, h->process(k.public_key_bits()));
1809  });
1810  }
1811 
1813  botan_pubkey_t key_obj,
1814  const char* padding,
1815  uint32_t flags)
1816  {
1817  try
1818  {
1820 
1821  *op = nullptr;
1822 
1823  if(flags != 0)
1824  return BOTAN_FFI_ERROR_BAD_FLAG;
1825 
1826  std::unique_ptr<Botan::PK_Encryptor> pk(new Botan::PK_Encryptor_EME(safe_get(key_obj), Botan::system_rng(), padding));
1827  *op = new botan_pk_op_encrypt_struct(pk.release());
1828  return 0;
1829  }
1830  catch(std::exception& e)
1831  {
1832  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1833  }
1834 
1835  return -1;
1836  }
1837 
1839  {
1840  delete op;
1841  return 0;
1842  }
1843 
1845  botan_rng_t rng_obj,
1846  uint8_t out[], size_t* out_len,
1847  const uint8_t plaintext[], size_t plaintext_len)
1848  {
1849  return BOTAN_FFI_DO(Botan::PK_Encryptor, op, o, {
1850  return write_vec_output(out, out_len, o.encrypt(plaintext, plaintext_len, safe_get(rng_obj)));
1851  });
1852  }
1853 
1854 /*
1855 * Public Key Decryption
1856 */
1858  botan_privkey_t key_obj,
1859  const char* padding,
1860  uint32_t flags)
1861  {
1862  try
1863  {
1865 
1866  *op = nullptr;
1867 
1868  if(flags != 0)
1869  return BOTAN_FFI_ERROR_BAD_FLAG;
1870 
1871  std::unique_ptr<Botan::PK_Decryptor> pk(new Botan::PK_Decryptor_EME(safe_get(key_obj), Botan::system_rng(), padding));
1872  *op = new botan_pk_op_decrypt_struct(pk.release());
1873  return 0;
1874  }
1875  catch(std::exception& e)
1876  {
1877  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1878  }
1879 
1880  return -1;
1881  }
1882 
1884  {
1885  delete op;
1886  return 0;
1887  }
1888 
1890  uint8_t out[], size_t* out_len,
1891  uint8_t ciphertext[], size_t ciphertext_len)
1892  {
1893  return BOTAN_FFI_DO(Botan::PK_Decryptor, op, o, {
1894  return write_vec_output(out, out_len, o.decrypt(ciphertext, ciphertext_len));
1895  });
1896  }
1897 
1898 /*
1899 * Signature Generation
1900 */
1902  botan_privkey_t key_obj,
1903  const char* hash,
1904  uint32_t flags)
1905  {
1906  try
1907  {
1909 
1910  *op = nullptr;
1911 
1912  if(flags != 0)
1913  return BOTAN_FFI_ERROR_BAD_FLAG;
1914 
1915  std::unique_ptr<Botan::PK_Signer> pk(new Botan::PK_Signer(safe_get(key_obj),Botan::system_rng(), hash));
1916  *op = new botan_pk_op_sign_struct(pk.release());
1917  return 0;
1918  }
1919  catch(std::exception& e)
1920  {
1921  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1922  }
1923 
1925  }
1926 
1928  {
1929  delete op;
1930  return 0;
1931  }
1932 
1933 int botan_pk_op_sign_update(botan_pk_op_sign_t op, const uint8_t in[], size_t in_len)
1934  {
1935  return BOTAN_FFI_DO(Botan::PK_Signer, op, o, { o.update(in, in_len); });
1936  }
1937 
1938 int botan_pk_op_sign_finish(botan_pk_op_sign_t op, botan_rng_t rng_obj, uint8_t out[], size_t* out_len)
1939  {
1940  return BOTAN_FFI_DO(Botan::PK_Signer, op, o, {
1941  return write_vec_output(out, out_len, o.signature(safe_get(rng_obj)));
1942  });
1943  }
1944 
1946  botan_pubkey_t key_obj,
1947  const char* hash,
1948  uint32_t flags)
1949  {
1950  try
1951  {
1953 
1954  if(flags != 0)
1955  return BOTAN_FFI_ERROR_BAD_FLAG;
1956 
1957  std::unique_ptr<Botan::PK_Verifier> pk(new Botan::PK_Verifier(safe_get(key_obj), hash));
1958  *op = new botan_pk_op_verify_struct(pk.release());
1959  return 0;
1960  }
1961  catch(std::exception& e)
1962  {
1963  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
1964  }
1965 
1966  return -1;
1967  }
1968 
1970  {
1971  delete op;
1972  return 0;
1973  }
1974 
1975 int botan_pk_op_verify_update(botan_pk_op_verify_t op, const uint8_t in[], size_t in_len)
1976  {
1977  return BOTAN_FFI_DO(Botan::PK_Verifier, op, o, { o.update(in, in_len); });
1978  }
1979 
1980 int botan_pk_op_verify_finish(botan_pk_op_verify_t op, const uint8_t sig[], size_t sig_len)
1981  {
1982  return BOTAN_FFI_DO(Botan::PK_Verifier, op, o, {
1983  const bool legit = o.check_signature(sig, sig_len);
1984 
1985  if(legit)
1986  return 0;
1987  else
1988  return 1;
1989  });
1990  }
1991 
1993  botan_privkey_t key_obj,
1994  const char* kdf,
1995  uint32_t flags)
1996  {
1997  try
1998  {
2000 
2001  *op = nullptr;
2002 
2003  if(flags != 0)
2004  return BOTAN_FFI_ERROR_BAD_FLAG;
2005 
2006  std::unique_ptr<Botan::PK_Key_Agreement> pk(new Botan::PK_Key_Agreement(safe_get(key_obj), Botan::system_rng(), kdf));
2007  *op = new botan_pk_op_ka_struct(pk.release());
2008  return 0;
2009  }
2010  catch(std::exception& e)
2011  {
2012  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
2013  }
2014 
2015  return -1;
2016  }
2017 
2019  {
2020  delete op;
2021  return 0;
2022  }
2023 
2025  uint8_t out[], size_t* out_len)
2026  {
2027  return BOTAN_FFI_DO(Botan::Private_Key, key, k, {
2028  if(auto kak = dynamic_cast<const Botan::PK_Key_Agreement_Key*>(&k))
2029  return write_vec_output(out, out_len, kak->public_value());
2030  return -2;
2031  });
2032  }
2033 
2035  uint8_t out[], size_t* out_len,
2036  const uint8_t other_key[], size_t other_key_len,
2037  const uint8_t salt[], size_t salt_len)
2038  {
2039  return BOTAN_FFI_DO(Botan::PK_Key_Agreement, op, o, {
2040  auto k = o.derive_key(*out_len, other_key, other_key_len, salt, salt_len).bits_of();
2041  return write_vec_output(out, out_len, k);
2042  });
2043  }
2044 
2045 int botan_x509_cert_load_file(botan_x509_cert_t* cert_obj, const char* cert_path)
2046  {
2047  try
2048  {
2049  if(!cert_obj || !cert_path)
2050  return -1;
2051 
2052 #if defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
2053  std::unique_ptr<Botan::X509_Certificate> c(new Botan::X509_Certificate(cert_path));
2054 
2055  if(c)
2056  {
2057  *cert_obj = new botan_x509_cert_struct(c.release());
2058  return 0;
2059  }
2060 #else
2062 #endif
2063  }
2064  catch(std::exception& e)
2065  {
2066  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
2067  }
2068  catch(...)
2069  {
2070  log_exception(BOTAN_CURRENT_FUNCTION, "unknown");
2071  }
2072 
2073  return -2;
2074  }
2075 
2076 int botan_x509_cert_load(botan_x509_cert_t* cert_obj, const uint8_t cert_bits[], size_t cert_bits_len)
2077  {
2078  try
2079  {
2080  if(!cert_obj || !cert_bits)
2081  return -1;
2082 
2083  Botan::DataSource_Memory bits(cert_bits, cert_bits_len);
2084 
2085  std::unique_ptr<Botan::X509_Certificate> c(new Botan::X509_Certificate(bits));
2086 
2087  if(c)
2088  {
2089  *cert_obj = new botan_x509_cert_struct(c.release());
2090  return 0;
2091  }
2092  }
2093  catch(std::exception& e)
2094  {
2095  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
2096  }
2097  catch(...)
2098  {
2099  log_exception(BOTAN_CURRENT_FUNCTION, "unknown");
2100  }
2101 
2102  return -2;
2103 
2104  }
2105 
2107  {
2108  delete cert;
2109  return 0;
2110  }
2111 
2112 int botan_x509_cert_get_time_starts(botan_x509_cert_t cert, char out[], size_t* out_len)
2113  {
2114  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_str_output(out, out_len, c.start_time()); });
2115  }
2116 
2117 int botan_x509_cert_get_time_expires(botan_x509_cert_t cert, char out[], size_t* out_len)
2118  {
2119  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_str_output(out, out_len, c.end_time()); });
2120  }
2121 
2122 int botan_x509_cert_get_serial_number(botan_x509_cert_t cert, uint8_t out[], size_t* out_len)
2123  {
2124  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_vec_output(out, out_len, c.serial_number()); });
2125  }
2126 
2127 int botan_x509_cert_get_fingerprint(botan_x509_cert_t cert, const char* hash, uint8_t out[], size_t* out_len)
2128  {
2129  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_str_output(out, out_len, c.fingerprint(hash)); });
2130  }
2131 
2132 int botan_x509_cert_get_authority_key_id(botan_x509_cert_t cert, uint8_t out[], size_t* out_len)
2133  {
2134  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_vec_output(out, out_len, c.authority_key_id()); });
2135  }
2136 
2137 int botan_x509_cert_get_subject_key_id(botan_x509_cert_t cert, uint8_t out[], size_t* out_len)
2138  {
2139  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_vec_output(out, out_len, c.subject_key_id()); });
2140  }
2141 
2142 int botan_x509_cert_get_public_key_bits(botan_x509_cert_t cert, uint8_t out[], size_t* out_len)
2143  {
2144  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_vec_output(out, out_len, c.subject_public_key_bits()); });
2145  }
2146 
2147 
2148 /*
2149 int botan_x509_cert_path_verify(botan_x509_cert_t cert, const char* dir)
2150 {
2151 }
2152 */
2153 
2155  {
2156  try
2157  {
2158  if(key == nullptr)
2159  return -1;
2160 
2161  *key = nullptr;
2162 
2163 #if defined(BOTAN_HAS_RSA)
2164  std::unique_ptr<Botan::Public_Key> publicKey(safe_get(cert).subject_public_key());
2165  *key = new botan_pubkey_struct(publicKey.release());
2166  return 0;
2167 #else
2169 #endif
2170  }
2171  catch(std::exception& e)
2172  {
2173  log_exception(BOTAN_CURRENT_FUNCTION, e.what());
2174  }
2175 
2177  }
2178 
2180  const char* key, size_t index,
2181  uint8_t out[], size_t* out_len)
2182  {
2183  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_str_output(out, out_len, c.issuer_info(key).at(index)); });
2184  }
2185 
2187  const char* key, size_t index,
2188  uint8_t out[], size_t* out_len)
2189  {
2190  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_str_output(out, out_len, c.subject_info(key).at(index)); });
2191  }
2192 
2193 int botan_x509_cert_to_string(botan_x509_cert_t cert, char out[], size_t* out_len)
2194  {
2195  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, { return write_str_output(out, out_len, c.to_string()); });
2196  }
2197 
2198 int botan_x509_cert_allowed_usage(botan_x509_cert_t cert, unsigned int key_usage)
2199  {
2200  return BOTAN_FFI_DO(Botan::X509_Certificate, cert, c, {
2201  const Botan::Key_Constraints k = static_cast<Botan::Key_Constraints>(key_usage);
2202  if(c.allowed_usage(k))
2203  return 0;
2204  return 1;
2205  });
2206  }
2207 
2209  const char* aead,
2210  const uint8_t ct[], size_t ct_len,
2211  const uint8_t ad[], size_t ad_len,
2212  uint8_t out[], size_t* out_len)
2213  {
2214  try
2215  {
2216  Botan::Private_Key& key = safe_get(mce_key_obj);
2217 
2218 #if defined(BOTAN_HAS_MCELIECE) && defined(BOTAN_HAS_MCEIES)
2219  Botan::McEliece_PrivateKey* mce = dynamic_cast<Botan::McEliece_PrivateKey*>(&key);
2220  if(!mce)
2221  return -2;
2222 
2223  const Botan::secure_vector<uint8_t> pt = mceies_decrypt(*mce, ct, ct_len, ad, ad_len, aead);
2224  return write_vec_output(out, out_len, pt);
2225 #else
2227 #endif
2228  }
2229  catch(std::exception& e)
2230  {
2231  return ffi_error_exception_thrown(e.what());
2232  }
2233  }
2234 
2236  botan_rng_t rng_obj,
2237  const char* aead,
2238  const uint8_t pt[], size_t pt_len,
2239  const uint8_t ad[], size_t ad_len,
2240  uint8_t out[], size_t* out_len)
2241  {
2242  try
2243  {
2244  Botan::Public_Key& key = safe_get(mce_key_obj);
2245  Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
2246 
2247 #if defined(BOTAN_HAS_MCELIECE) && defined(BOTAN_HAS_MCEIES)
2248  Botan::McEliece_PublicKey* mce = dynamic_cast<Botan::McEliece_PublicKey*>(&key);
2249  if(!mce)
2250  return -2;
2251 
2252  Botan::secure_vector<uint8_t> ct = mceies_encrypt(*mce, pt, pt_len, ad, ad_len, rng, aead);
2253  return write_vec_output(out, out_len, ct);
2254 #else
2256 #endif
2257  }
2258  catch(std::exception& e)
2259  {
2260  return ffi_error_exception_thrown(e.what());
2261  }
2262  }
2263 
2264 /*
2265 int botan_tls_channel_init_client(botan_tls_channel_t* channel,
2266  botan_tls_channel_output_fn output_fn,
2267  botan_tls_channel_data_cb data_cb,
2268  botan_tls_channel_alert_cb alert_cb,
2269  botan_tls_channel_session_established session_cb,
2270  const char* server_name)
2271  {
2272 
2273  }
2274 */
2275 
2276 }
2277 
int botan_cipher_get_tag_length(botan_cipher_t cipher, size_t *tl)
Definition: ffi.cpp:987
#define BOTAN_CIPHER_INIT_FLAG_ENCRYPT
Definition: ffi.h:346
uint32_t version_minor()
Definition: version.cpp:61
std::vector< uint8_t > BER_encode_encrypted_pbkdf_iter(const Private_Key &key, RandomNumberGenerator &rng, const std::string &pass, size_t pbkdf_iterations, const std::string &cipher, const std::string &pbkdf_hash)
Definition: pkcs8.cpp:208
void binary_encode(uint8_t buf[]) const
Definition: bigint.cpp:270
int botan_block_cipher_encrypt_blocks(botan_block_cipher_t bc, const uint8_t in[], uint8_t out[], size_t blocks)
Definition: ffi.cpp:657
int botan_x509_cert_get_authority_key_id(botan_x509_cert_t cert, uint8_t out[], size_t *out_len)
Definition: ffi.cpp:2132
std::vector< uint8_t > BER_encode(const Public_Key &key)
Definition: x509_key.cpp:19
int botan_hex_encode(const uint8_t *in, size_t len, char *out, uint32_t flags)
Definition: ffi.cpp:273
int botan_pubkey_get_field(botan_mp_t output, botan_pubkey_t key, const char *field_name_cstr)
Definition: ffi.cpp:1544
T round_down(T n, T align_to)
Definition: rounding.h:38
bool is_odd() const
Definition: bigint.h:238
size_t minimum_keylength() const
Definition: key_spec.h:61
int botan_rng_reseed(botan_rng_t rng, size_t bits)
Definition: ffi.cpp:336
int botan_mp_set_from_radix_str(botan_mp_t mp, const char *str, size_t radix)
Definition: ffi.cpp:372
bool is_even() const
Definition: bigint.h:232
int botan_mp_is_odd(const botan_mp_t mp)
Definition: ffi.cpp:496
#define BOTAN_CIPHER_INIT_FLAG_MASK_DIRECTION
Definition: ffi.h:345
int botan_mp_lshift(botan_mp_t out, const botan_mp_t in, size_t shift)
Definition: ffi.cpp:523
int botan_pubkey_load(botan_pubkey_t *key, const uint8_t bits[], size_t bits_len)
Definition: ffi.cpp:1302
int botan_x509_cert_get_subject_dn(botan_x509_cert_t cert, const char *key, size_t index, uint8_t out[], size_t *out_len)
Definition: ffi.cpp:2186
int botan_mp_set_from_str(botan_mp_t mp, const char *str)
Definition: ffi.cpp:367
void divide(const BigInt &x, const BigInt &y_arg, BigInt &q, BigInt &r)
Definition: divide.cpp:58
secure_vector< uint8_t > mceies_decrypt(const McEliece_PrivateKey &privkey, const uint8_t ct[], size_t ct_len, const uint8_t ad[], size_t ad_len, const std::string &algo)
Definition: mceies.cpp:70
static std::unique_ptr< MessageAuthenticationCode > create(const std::string &algo_spec, const std::string &provider="")
Definition: mac.cpp:43
int botan_hash_final(botan_hash_t hash, uint8_t out[])
Definition: ffi.cpp:722
int botan_mp_rshift(botan_mp_t out, const botan_mp_t in, size_t shift)
Definition: ffi.cpp:528
int botan_mac_update(botan_mac_t mac, const uint8_t *buf, size_t len)
Definition: ffi.cpp:774
int botan_x509_cert_get_serial_number(botan_x509_cert_t cert, uint8_t out[], size_t *out_len)
Definition: ffi.cpp:2122
#define BOTAN_FFI_ERROR_BAD_FLAG
Definition: ffi.h:139
#define BOTAN_PRIVKEY_EXPORT_FLAG_PEM
Definition: ffi.h:659
bool same_mem(const T *p1, const T *p2, size_t n)
Definition: mem_ops.h:98
int botan_privkey_rsa_get_n(botan_mp_t n, botan_privkey_t key)
Definition: ffi.cpp:1582
int botan_rng_destroy(botan_rng_t rng)
Definition: ffi.cpp:325
int botan_mp_set_bit(botan_mp_t mp, size_t bit)
Definition: ffi.cpp:578
std::string generate_bcrypt(const std::string &pass, RandomNumberGenerator &rng, uint16_t work_factor)
Definition: bcrypt.cpp:125
int botan_cipher_update(botan_cipher_t cipher_obj, uint32_t flags, uint8_t output[], size_t output_size, size_t *output_written, const uint8_t input[], size_t input_size, size_t *input_consumed)
Definition: ffi.cpp:853
int botan_x509_cert_allowed_usage(botan_x509_cert_t cert, unsigned int key_usage)
Definition: ffi.cpp:2198
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)
Definition: ffi.cpp:1398
int botan_x509_cert_destroy(botan_x509_cert_t cert)
Definition: ffi.cpp:2106
int botan_block_cipher_destroy(botan_block_cipher_t bc)
Definition: ffi.cpp:628
virtual void randomize(uint8_t output[], size_t length)=0
Exception(const std::string &msg)
Definition: exceptn.h:24
uint32_t to_u32bit() const
Definition: bigint.cpp:141
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.cpp:1938
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.cpp:1844
virtual void clear()=0
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:57
struct botan_pk_op_encrypt_struct * botan_pk_op_encrypt_t
Definition: ffi.h:788
int botan_hash_update(botan_hash_t hash, const uint8_t *buf, size_t len)
Definition: ffi.cpp:717
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
Definition: numthry.cpp:441
void set_bit(size_t n)
Definition: bigint.cpp:157
BigInt gcd(const BigInt &a, const BigInt &b)
Definition: numthry.cpp:46
int botan_mp_is_positive(const botan_mp_t mp)
Definition: ffi.cpp:400
int botan_pk_op_encrypt_destroy(botan_pk_op_encrypt_t op)
Definition: ffi.cpp:1838
secure_vector< uint8_t > BER_encode(const Private_Key &key)
Definition: pkcs8.cpp:130
virtual void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const =0
Public_Key * load_key(DataSource &source)
Definition: x509_key.cpp:37
int botan_pubkey_destroy(botan_pubkey_t key)
Definition: ffi.cpp:1636
int botan_privkey_create_ecdsa(botan_privkey_t *key_obj, botan_rng_t rng_obj, const char *param_str)
Definition: ffi.cpp:1183
void update(secure_vector< uint8_t > &buffer, size_t offset=0)
Definition: cipher_mode.h:81
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.cpp:1857
virtual size_t update_granularity() const =0
int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition: ffi.cpp:470
int botan_mac_destroy(botan_mac_t mac)
Definition: ffi.cpp:753
int botan_mp_clear(botan_mp_t mp)
Definition: ffi.cpp:347
Cipher_Mode * get_cipher_mode(const std::string &algo, Cipher_Dir direction)
Definition: cipher_mode.cpp:39
struct botan_pk_op_sign_struct * botan_pk_op_sign_t
Definition: ffi.h:820
int botan_privkey_create(botan_privkey_t *key_obj, const char *algo_name, const char *algo_params, botan_rng_t rng_obj)
Definition: ffi.cpp:1113
Flags flags(Flag flags)
Definition: p11.h:858
int botan_privkey_create_rsa(botan_privkey_t *key_obj, botan_rng_t rng_obj, size_t n_bits)
Definition: ffi.cpp:1154
int botan_rng_init(botan_rng_t *rng_out, const char *rng_type)
Definition: ffi.cpp:289
std::string PEM_encode_encrypted_pbkdf_iter(const Private_Key &key, RandomNumberGenerator &rng, const std::string &pass, size_t pbkdf_iterations, const std::string &cipher, const std::string &pbkdf_hash)
Definition: pkcs8.cpp:233
int botan_privkey_rsa_get_e(botan_mp_t e, botan_privkey_t key)
Definition: ffi.cpp:1587
bool check_bcrypt(const std::string &pass, const std::string &hash)
Definition: bcrypt.cpp:132
int botan_block_cipher_clear(botan_block_cipher_t bc)
Definition: ffi.cpp:634
bool is_negative() const
Definition: bigint.h:348
int botan_pubkey_check_key(botan_pubkey_t key, botan_rng_t rng, uint32_t flags)
Definition: ffi.cpp:1665
int botan_pk_op_sign_update(botan_pk_op_sign_t op, const uint8_t in[], size_t in_len)
Definition: ffi.cpp:1933
int botan_pubkey_load_rsa(botan_pubkey_t *key, botan_mp_t n, botan_mp_t e)
Definition: ffi.cpp:1349
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:47
void swap(BigInt &other)
Definition: bigint.h:122
virtual Key_Length_Specification key_spec() const =0
std::string PEM_encode(const Private_Key &key)
Definition: pkcs8.cpp:139
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition: big_rand.cpp:45
const char * version_cstr()
Definition: version.cpp:28
int botan_x509_cert_get_fingerprint(botan_x509_cert_t cert, const char *hash, uint8_t out[], size_t *out_len)
Definition: ffi.cpp:2127
int botan_privkey_export_encrypted_pbkdf_iter(botan_privkey_t key, uint8_t out[], size_t *out_len, botan_rng_t rng_obj, const char *pass, size_t pbkdf_iter, const char *maybe_cipher, const char *maybe_pbkdf_hash, uint32_t flags)
Definition: ffi.cpp:1757
int botan_privkey_load(botan_privkey_t *key, botan_rng_t rng_obj, const uint8_t bits[], size_t len, const char *password)
Definition: ffi.cpp:1270
#define BOTAN_FFI_HEX_LOWER_CASE
Definition: ffi.h:150
int botan_x509_cert_get_public_key_bits(botan_x509_cert_t cert, uint8_t out[], size_t *out_len)
Definition: ffi.cpp:2142
int botan_hash_init(botan_hash_t *hash, const char *hash_name, uint32_t flags)
Definition: ffi.cpp:673
virtual size_t minimum_final_size() const =0
int botan_block_cipher_decrypt_blocks(botan_block_cipher_t bc, const uint8_t in[], uint8_t out[], size_t blocks)
Definition: ffi.cpp:665
int botan_hash_clear(botan_hash_t hash)
Definition: ffi.cpp:712
int botan_privkey_export_encrypted_pbkdf_msec(botan_privkey_t key, uint8_t out[], size_t *out_len, botan_rng_t rng_obj, const char *pass, uint32_t pbkdf_msec, size_t *pbkdf_iters_out, const char *maybe_cipher, const char *maybe_pbkdf_hash, uint32_t flags)
Definition: ffi.cpp:1714
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)
Definition: ffi.cpp:1371
int botan_mp_to_str(const botan_mp_t mp, uint8_t digit_base, char *out, size_t *out_len)
Definition: ffi.cpp:424
void start(const std::vector< uint8_t, Alloc > &nonce)
Definition: cipher_mode.h:38
int botan_block_cipher_init(botan_block_cipher_t *bc, const char *bc_name)
Definition: ffi.cpp:598
virtual size_t default_nonce_length() const =0
uint32_t botan_version_patch()
Definition: ffi.cpp:265
int botan_block_cipher_block_size(botan_block_cipher_t bc)
Definition: ffi.cpp:652
Private_Key * load_key(DataSource &source, RandomNumberGenerator &rng, std::function< std::string()> get_pass)
Definition: pkcs8.cpp:313
int botan_mp_rand_bits(botan_mp_t rand_out, botan_rng_t rng, size_t bits)
Definition: ffi.cpp:546
int botan_pk_op_sign_destroy(botan_pk_op_sign_t op)
Definition: ffi.cpp:1927
int botan_mceies_encrypt(botan_pubkey_t mce_key_obj, botan_rng_t rng_obj, const char *aead, const uint8_t pt[], size_t pt_len, const uint8_t ad[], size_t ad_len, uint8_t out[], size_t *out_len)
Definition: ffi.cpp:2235
int botan_x509_cert_get_time_expires(botan_x509_cert_t cert, char out[], size_t *out_len)
Definition: ffi.cpp:2117
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:27
int botan_cipher_query_keylen(botan_cipher_t cipher, size_t *out_minimum_keylength, size_t *out_maximum_keylength)
Definition: ffi.cpp:819
int botan_mac_init(botan_mac_t *mac, const char *mac_name, uint32_t flags)
Definition: ffi.cpp:727
int botan_rng_get(botan_rng_t rng, uint8_t *out, size_t out_len)
Definition: ffi.cpp:331
int botan_mp_is_prime(const botan_mp_t mp, botan_rng_t rng, size_t test_prob)
Definition: ffi.cpp:567
virtual size_t tag_size() const
Definition: cipher_mode.h:150
struct botan_mac_struct * botan_mac_t
Definition: ffi.h:277
int botan_mp_div(botan_mp_t quotient, botan_mp_t remainder, const botan_mp_t x, const botan_mp_t y)
Definition: ffi.cpp:475
void final(uint8_t out[])
Definition: buf_comp.h:89
int botan_privkey_create_ecdh(botan_privkey_t *key_obj, botan_rng_t rng_obj, const char *param_str)
Definition: ffi.cpp:1235
int32_t cmp(const BigInt &n, bool check_signs=true) const
Definition: bigint.cpp:98
size_t bits() const
Definition: bigint.cpp:184
#define BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:141
int botan_pk_op_verify_finish(botan_pk_op_verify_t op, const uint8_t sig[], size_t sig_len)
Definition: ffi.cpp:1980
int botan_pk_op_verify_destroy(botan_pk_op_verify_t op)
Definition: ffi.cpp:1969
uint32_t version_datestamp()
Definition: version.cpp:55
int botan_privkey_load_rsa(botan_privkey_t *key, botan_mp_t p, botan_mp_t q, botan_mp_t d)
Definition: ffi.cpp:1326
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
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.cpp:1992
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:66
int botan_pubkey_rsa_get_e(botan_mp_t e, botan_pubkey_t key)
Definition: ffi.cpp:1597
struct botan_pk_op_decrypt_struct * botan_pk_op_decrypt_t
Definition: ffi.h:805
struct botan_mp_struct * botan_mp_t
Definition: ffi.h:506
int botan_x509_cert_get_subject_key_id(botan_x509_cert_t cert, uint8_t out[], size_t *out_len)
Definition: ffi.cpp:2137
int botan_pbkdf(const char *pbkdf_algo, uint8_t out[], size_t out_len, const char *pass, const uint8_t salt[], size_t salt_len, size_t iterations)
Definition: ffi.cpp:992
int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus)
Definition: ffi.cpp:533
uint32_t version_major()
Definition: version.cpp:60
int botan_privkey_rsa_get_p(botan_mp_t p, botan_privkey_t key)
Definition: ffi.cpp:1572
#define BOTAN_ASSERT_NONNULL(ptr)
Definition: assert.h:79
int botan_cipher_set_key(botan_cipher_t cipher, const uint8_t *key, size_t key_len)
Definition: ffi.cpp:829
int botan_privkey_export(botan_privkey_t key, uint8_t out[], size_t *out_len, uint32_t flags)
Definition: ffi.cpp:1692
struct botan_x509_cert_struct * botan_x509_cert_t
Definition: ffi.h:886
uint32_t botan_ffi_api_version()
Definition: ffi.cpp:242
int botan_pbkdf_timed(const char *pbkdf_algo, uint8_t out[], size_t out_len, const char *password, const uint8_t salt[], size_t salt_len, size_t ms_to_run, size_t *iterations_used)
Definition: ffi.cpp:1010
secure_vector< uint8_t > mceies_encrypt(const McEliece_PublicKey &pubkey, const uint8_t pt[], size_t pt_len, const uint8_t ad[], size_t ad_len, RandomNumberGenerator &rng, const std::string &algo)
Definition: mceies.cpp:33
int botan_mp_flip_sign(botan_mp_t mp)
Definition: ffi.cpp:405
#define BOTAN_CIPHER_UPDATE_FLAG_FINAL
Definition: ffi.h:369
int botan_cipher_get_update_granularity(botan_cipher_t cipher, size_t *ug)
Definition: ffi.cpp:982
#define BOTAN_UNUSED(v)
Definition: assert.h:92
int botan_mp_powmod(botan_mp_t out, const botan_mp_t base, const botan_mp_t exponent, const botan_mp_t modulus)
Definition: ffi.cpp:517
BigInt multiply(const BigInt &x, const BigInt &y) const
Definition: reducer.h:31
#define BOTAN_CHECK_KEY_EXPENSIVE_TESTS
Definition: ffi.h:639
int botan_bcrypt_is_valid(const char *pass, const char *hash)
Definition: ffi.cpp:1090
int botan_cipher_start(botan_cipher_t cipher_obj, const uint8_t *nonce, size_t nonce_len)
Definition: ffi.cpp:835
#define BOTAN_PRIVKEY_EXPORT_FLAG_DER
Definition: ffi.h:658
int botan_privkey_rsa_get_q(botan_mp_t q, botan_privkey_t key)
Definition: ffi.cpp:1577
#define BOTAN_FFI_ERROR_EXCEPTION_THROWN
Definition: ffi.h:138
struct botan_hash_struct * botan_hash_t
Definition: ffi.h:213
int botan_mp_num_bytes(const botan_mp_t mp, size_t *bytes)
Definition: ffi.cpp:593
int botan_mp_destroy(botan_mp_t mp)
Definition: ffi.cpp:454
virtual void clear()=0
int botan_mp_get_bit(const botan_mp_t mp, size_t bit)
Definition: ffi.cpp:573
uint32_t botan_version_major()
Definition: ffi.cpp:263
PBKDF * get_pbkdf(const std::string &algo_spec, const std::string &provider="")
Definition: pbkdf.h:216
int botan_pubkey_algo_name(botan_pubkey_t key, char out[], size_t *out_len)
Definition: ffi.cpp:1660
int botan_same_mem(const uint8_t *x, const uint8_t *y, size_t len)
Definition: ffi.cpp:268
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:93
int botan_mp_cmp(int *result, const botan_mp_t x_w, const botan_mp_t y_w)
Definition: ffi.cpp:506
int botan_pk_op_decrypt(botan_pk_op_decrypt_t op, uint8_t out[], size_t *out_len, uint8_t ciphertext[], size_t ciphertext_len)
Definition: ffi.cpp:1889
const char * what() const BOTAN_NOEXCEPT override
Definition: exceptn.h:26
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.cpp:1901
int botan_mp_swap(botan_mp_t x_w, botan_mp_t y_w)
Definition: ffi.cpp:511
int botan_mp_clear_bit(botan_mp_t mp, size_t bit)
Definition: ffi.cpp:583
int botan_mp_is_negative(const botan_mp_t mp)
Definition: ffi.cpp:395
int botan_cipher_destroy(botan_cipher_t cipher)
Definition: ffi.cpp:808
int botan_pk_op_verify_update(botan_pk_op_verify_t op, const uint8_t in[], size_t in_len)
Definition: ffi.cpp:1975
int botan_pubkey_dsa_get_g(botan_mp_t g, botan_pubkey_t key)
Definition: ffi.cpp:1620
int botan_mp_to_hex(const botan_mp_t mp, char *out)
Definition: ffi.cpp:415
std::string PEM_encode(const Public_Key &key)
Definition: x509_key.cpp:28
struct botan_pk_op_verify_struct * botan_pk_op_verify_t
Definition: ffi.h:835
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:68
int botan_mac_clear(botan_mac_t mac)
Definition: ffi.cpp:769
int botan_hash_destroy(botan_hash_t hash)
Definition: ffi.cpp:701
Definition: alg_id.cpp:13
int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition: ffi.cpp:460
int botan_mp_equal(const botan_mp_t x_w, const botan_mp_t y_w)
Definition: ffi.cpp:486
int botan_pubkey_dsa_get_y(botan_mp_t y, botan_pubkey_t key)
Definition: ffi.cpp:1624
struct botan_privkey_struct * botan_privkey_t
Definition: ffi.h:632
virtual void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const =0
int botan_pk_op_key_agreement_destroy(botan_pk_op_ka_t op)
Definition: ffi.cpp:2018
int botan_pubkey_export(botan_pubkey_t key, uint8_t out[], size_t *out_len, uint32_t flags)
Definition: ffi.cpp:1680
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.cpp:1812
void clear()
Definition: bigint.h:217
static std::unique_ptr< BlockCipher > create(const std::string &algo_spec, const std::string &provider="")
int botan_pubkey_estimated_strength(botan_pubkey_t key, size_t *estimate)
Definition: ffi.cpp:1798
int botan_cipher_get_default_nonce_length(botan_cipher_t cipher, size_t *nl)
Definition: ffi.cpp:977
#define BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
Definition: ffi.h:137
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.cpp:2034
int botan_privkey_create_mceliece(botan_privkey_t *key_obj, botan_rng_t rng_obj, size_t n, size_t t)
Definition: ffi.cpp:1210
uint32_t botan_version_minor()
Definition: ffi.cpp:264
int botan_mp_is_even(const botan_mp_t mp)
Definition: ffi.cpp:501
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
Definition: rng.cpp:44
int botan_bcrypt_generate(uint8_t *out, size_t *out_len, const char *pass, botan_rng_t rng_obj, size_t wf, uint32_t flags)
Definition: ffi.cpp:1053
int botan_mp_is_zero(const botan_mp_t mp)
Definition: ffi.cpp:491
int botan_pubkey_rsa_get_n(botan_mp_t n, botan_pubkey_t key)
Definition: ffi.cpp:1602
virtual size_t estimated_strength() const =0
int botan_privkey_export_pubkey(botan_pubkey_t *pubout, botan_privkey_t key_obj)
Definition: ffi.cpp:1642
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:276
uint32_t botan_version_datestamp()
Definition: ffi.cpp:266
int botan_x509_cert_load(botan_x509_cert_t *cert_obj, const uint8_t cert_bits[], size_t cert_bits_len)
Definition: ffi.cpp:2076
void set_key(const std::vector< uint8_t, Alloc > &key)
Definition: cipher_mode.h:172
int botan_mac_set_key(botan_mac_t mac, const uint8_t *key, size_t key_len)
Definition: ffi.cpp:759
int botan_privkey_dsa_get_x(botan_mp_t x, botan_privkey_t key)
Definition: ffi.cpp:1607
struct botan_block_cipher_struct * botan_block_cipher_t
Definition: ffi.h:461
std::vector< uint8_t > BER_encode_encrypted_pbkdf_msec(const Private_Key &key, RandomNumberGenerator &rng, const std::string &pass, std::chrono::milliseconds pbkdf_msec, size_t *pbkdf_iterations, const std::string &cipher, const std::string &pbkdf_hash)
Definition: pkcs8.cpp:248
int botan_x509_cert_get_issuer_dn(botan_x509_cert_t cert, const char *key, size_t index, uint8_t out[], size_t *out_len)
Definition: ffi.cpp:2179
int botan_privkey_get_field(botan_mp_t output, botan_privkey_t key, const char *field_name_cstr)
Definition: ffi.cpp:1558
int botan_mp_mod_mul(botan_mp_t out, const botan_mp_t x, const botan_mp_t y, const botan_mp_t modulus)
Definition: ffi.cpp:538
int botan_ffi_supports_api(uint32_t api_version)
Definition: ffi.cpp:247
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:105
int botan_pk_op_key_agreement_export_public(botan_privkey_t key, uint8_t out[], size_t *out_len)
Definition: ffi.cpp:2024
void update(const uint8_t in[], size_t length)
Definition: buf_comp.h:34
int botan_cipher_set_associated_data(botan_cipher_t cipher, const uint8_t *ad, size_t ad_len)
Definition: ffi.cpp:958
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:196
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
Definition: numthry.cpp:373
int botan_x509_cert_to_string(botan_x509_cert_t cert, char out[], size_t *out_len)
Definition: ffi.cpp:2193
bool is_positive() const
Definition: bigint.h:354
int botan_mac_final(botan_mac_t mac, uint8_t out[])
Definition: ffi.cpp:779
int botan_hash_output_length(botan_hash_t hash, size_t *out)
Definition: ffi.cpp:707
int botan_pubkey_dsa_get_q(botan_mp_t q, botan_pubkey_t key)
Definition: ffi.cpp:1616
int botan_mp_to_uint32(const botan_mp_t mp, uint32_t *val)
Definition: ffi.cpp:446
int botan_mceies_decrypt(botan_privkey_t mce_key_obj, const char *aead, const uint8_t ct[], size_t ct_len, const uint8_t ad[], size_t ad_len, uint8_t out[], size_t *out_len)
Definition: ffi.cpp:2208
int botan_mp_rand_range(botan_mp_t rand_out, botan_rng_t rng, const botan_mp_t lower, const botan_mp_t upper)
Definition: ffi.cpp:552
int botan_cipher_init(botan_cipher_t *cipher, const char *cipher_name, uint32_t flags)
Definition: ffi.cpp:784
int botan_x509_cert_get_public_key(botan_x509_cert_t cert, botan_pubkey_t *key)
Definition: ffi.cpp:2154
void binary_decode(const uint8_t buf[], size_t length)
Definition: bigint.cpp:280
int botan_mp_num_bits(const botan_mp_t mp, size_t *bits)
Definition: ffi.cpp:588
int botan_mp_set_from_int(botan_mp_t mp, int initial_value)
Definition: ffi.cpp:352
virtual void clear()=0
struct botan_pk_op_ka_struct * botan_pk_op_ka_t
Definition: ffi.h:849
int botan_mp_set_from_mp(botan_mp_t dest, const botan_mp_t source)
Definition: ffi.cpp:390
int botan_block_cipher_set_key(botan_block_cipher_t bc, const uint8_t key[], size_t len)
Definition: ffi.cpp:642
int botan_mp_from_bin(botan_mp_t mp, const uint8_t bin[], size_t bin_len)
Definition: ffi.cpp:410
#define BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:140
int botan_privkey_export_encrypted(botan_privkey_t key, uint8_t out[], size_t *out_len, botan_rng_t rng_obj, const char *pass, const char *, uint32_t flags)
Definition: ffi.cpp:1704
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi.cpp:204
int botan_cipher_clear(botan_cipher_t cipher)
Definition: ffi.cpp:814
std::unique_ptr< Private_Key > create_private_key(const std::string &alg_name, RandomNumberGenerator &rng, const std::string &params)
Definition: pk_algs.cpp:204
int botan_kdf(const char *kdf_algo, uint8_t out[], size_t out_len, const uint8_t secret[], size_t secret_len, const uint8_t salt[], size_t salt_len, const uint8_t label[], size_t label_len)
Definition: ffi.cpp:1033
int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y)
Definition: ffi.cpp:561
int botan_privkey_check_key(botan_privkey_t key, botan_rng_t rng, uint32_t flags)
Definition: ffi.cpp:1673
void flip_sign()
Definition: bigint.cpp:226
int botan_x509_cert_load_file(botan_x509_cert_t *cert_obj, const char *cert_path)
Definition: ffi.cpp:2045
bool is_zero() const
Definition: bigint.h:250
static std::vector< uint8_t > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:54
void clear_bit(size_t n)
Definition: bigint.cpp:168
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.cpp:1945
#define BOTAN_FFI_DECLARE_STRUCT(NAME, TYPE, MAGIC)
Definition: ffi.cpp:210
struct botan_pubkey_struct * botan_pubkey_t
Definition: ffi.h:709
KDF * get_kdf(const std::string &algo_spec)
Definition: kdf.cpp:226
uint32_t version_patch()
Definition: version.cpp:62
int botan_mp_init(botan_mp_t *mp)
Definition: ffi.cpp:341
struct botan_cipher_struct * botan_cipher_t
Definition: ffi.h:343
int botan_cipher_valid_nonce_length(botan_cipher_t cipher, size_t nl)
Definition: ffi.cpp:972
virtual size_t block_size() const =0
virtual size_t output_length() const =0
MechanismType hash
int botan_pubkey_dsa_get_p(botan_mp_t p, botan_pubkey_t key)
Definition: ffi.cpp:1612
const char * botan_version_string()
Definition: ffi.cpp:258
int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition: ffi.cpp:465
struct botan_rng_struct * botan_rng_t
Definition: ffi.h:169
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:114
int botan_privkey_rsa_get_d(botan_mp_t d, botan_privkey_t key)
Definition: ffi.cpp:1592
virtual bool valid_nonce_length(size_t nonce_len) const =0
int botan_pubkey_fingerprint(botan_pubkey_t key, const char *hash_fn, uint8_t out[], size_t *out_len)
Definition: ffi.cpp:1803
virtual void finish(secure_vector< uint8_t > &final_block, size_t offset=0)=0
std::string PEM_encode_encrypted_pbkdf_msec(const Private_Key &key, RandomNumberGenerator &rng, const std::string &pass, std::chrono::milliseconds pbkdf_msec, size_t *pbkdf_iterations, const std::string &cipher, const std::string &pbkdf_hash)
Definition: pkcs8.cpp:274
int botan_x509_cert_get_time_starts(botan_x509_cert_t cert, char out[], size_t *out_len)
Definition: ffi.cpp:2112
#define BOTAN_ASSERT_ARG_NON_NULL(p)
Definition: ffi.cpp:79
int botan_privkey_destroy(botan_privkey_t key)
Definition: ffi.cpp:1630
int botan_pk_op_decrypt_destroy(botan_pk_op_decrypt_t op)
Definition: ffi.cpp:1883
int botan_mp_to_bin(const botan_mp_t mp, uint8_t vec[])
Definition: ffi.cpp:441
int botan_mac_output_length(botan_mac_t mac, size_t *out)
Definition: ffi.cpp:764
size_t bytes() const
Definition: bigint.cpp:176