Botan  1.11.15
Defines | Functions
src/lib/ffi/ffi.cpp File Reference
#include <botan/ffi.h>
#include <botan/system_rng.h>
#include <botan/auto_rng.h>
#include <botan/lookup.h>
#include <botan/aead.h>
#include <botan/hash.h>
#include <botan/mac.h>
#include <botan/pbkdf.h>
#include <botan/version.h>
#include <botan/pubkey.h>
#include <botan/data_src.h>
#include <botan/mem_ops.h>
#include <cstring>
#include <memory>

Go to the source code of this file.

Defines

#define BOTAN_ASSERT_ARG_NON_NULL(p)   do { if(!p) throw std::invalid_argument("Argument " #p " is null"); } while(0)
#define BOTAN_FFI_DO(T, obj, block)   apply_fn(obj, BOTAN_CURRENT_FUNCTION, [=](T& obj) { do { block } while(0); return 0; })

Functions

int botan_cipher_clear (botan_cipher_t cipher)
int botan_cipher_destroy (botan_cipher_t cipher)
int botan_cipher_get_default_nonce_length (botan_cipher_t cipher, size_t *nl)
int botan_cipher_get_tag_length (botan_cipher_t cipher, size_t *tl)
int botan_cipher_init (botan_cipher_t *cipher, const char *cipher_name, uint32_t flags)
int botan_cipher_set_associated_data (botan_cipher_t cipher, const uint8_t *ad, size_t ad_len)
int botan_cipher_set_key (botan_cipher_t cipher, const uint8_t *key, size_t key_len)
int botan_cipher_start (botan_cipher_t cipher_obj, const uint8_t *nonce, size_t nonce_len)
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)
int botan_cipher_valid_nonce_length (botan_cipher_t cipher, size_t nl)
uint32_t botan_ffi_api_version ()
int botan_hash_clear (botan_hash_t hash)
int botan_hash_destroy (botan_hash_t hash)
int botan_hash_final (botan_hash_t hash, uint8_t out[])
int botan_hash_init (botan_hash_t *hash, const char *hash_name, uint32_t flags)
int botan_hash_output_length (botan_hash_t hash, size_t *out)
int botan_hash_update (botan_hash_t hash, const uint8_t *buf, size_t len)
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)
int botan_mac_clear (botan_mac_t mac)
int botan_mac_destroy (botan_mac_t mac)
int botan_mac_final (botan_mac_t mac, uint8_t out[])
int botan_mac_init (botan_mac_t *mac, const char *mac_name, uint32_t flags)
int botan_mac_output_length (botan_mac_t mac, size_t *out)
int botan_mac_set_key (botan_mac_t mac, const uint8_t *key, size_t key_len)
int botan_mac_update (botan_mac_t mac, const uint8_t *buf, size_t len)
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)
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)
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)
int botan_pk_op_decrypt_create (botan_pk_op_decrypt_t *op, botan_privkey_t key_obj, const char *padding, uint32_t flags)
int botan_pk_op_decrypt_destroy (botan_pk_op_decrypt_t op)
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)
int botan_pk_op_encrypt_create (botan_pk_op_encrypt_t *op, botan_pubkey_t key_obj, const char *padding, uint32_t flags)
int botan_pk_op_encrypt_destroy (botan_pk_op_encrypt_t op)
int botan_pk_op_key_agreement (botan_pk_op_ka_t op, uint8_t out[], size_t *out_len, const uint8_t other_key[], size_t other_key_len, const uint8_t salt[], size_t salt_len)
int botan_pk_op_key_agreement_create (botan_pk_op_ka_t *op, botan_privkey_t key_obj, const char *kdf, uint32_t flags)
int botan_pk_op_key_agreement_destroy (botan_pk_op_ka_t op)
int botan_pk_op_key_agreement_export_public (botan_privkey_t key, uint8_t out[], size_t *out_len)
int botan_pk_op_sign_create (botan_pk_op_sign_t *op, botan_privkey_t key_obj, const char *hash, uint32_t flags)
int botan_pk_op_sign_destroy (botan_pk_op_sign_t op)
int botan_pk_op_sign_finish (botan_pk_op_sign_t op, botan_rng_t rng_obj, uint8_t out[], size_t *out_len)
int botan_pk_op_sign_update (botan_pk_op_sign_t op, const uint8_t in[], size_t in_len)
int botan_pk_op_verify_create (botan_pk_op_verify_t *op, botan_pubkey_t key_obj, const char *hash, uint32_t flags)
int botan_pk_op_verify_destroy (botan_pk_op_verify_t op)
int botan_pk_op_verify_finish (botan_pk_op_verify_t op, const uint8_t sig[], size_t sig_len)
int botan_pk_op_verify_update (botan_pk_op_verify_t op, const uint8_t in[], size_t in_len)
int botan_privkey_create_ecdh (botan_privkey_t *key_obj, botan_rng_t rng_obj, const char *param_str)
int botan_privkey_create_ecdsa (botan_privkey_t *key_obj, botan_rng_t rng_obj, const char *param_str)
int botan_privkey_create_rsa (botan_privkey_t *key_obj, botan_rng_t rng_obj, size_t n_bits)
int botan_privkey_destroy (botan_privkey_t key)
int botan_privkey_export (botan_privkey_t key, uint8_t out[], size_t *out_len, uint32_t flags)
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 *pbe, uint32_t flags)
int botan_privkey_export_pubkey (botan_pubkey_t *pubout, botan_privkey_t key_obj)
int botan_privkey_load (botan_privkey_t *key, botan_rng_t rng_obj, const uint8_t bits[], size_t len, const char *password)
int botan_pubkey_algo_name (botan_pubkey_t key, char out[], size_t *out_len)
int botan_pubkey_destroy (botan_privkey_t key)
int botan_pubkey_estimated_strength (botan_pubkey_t key, size_t *estimate)
int botan_pubkey_export (botan_pubkey_t key, uint8_t out[], size_t *out_len, uint32_t flags)
int botan_pubkey_fingerprint (botan_pubkey_t key, const char *hash_fn, uint8_t out[], size_t *out_len)
int botan_rng_destroy (botan_rng_t rng)
int botan_rng_get (botan_rng_t rng, uint8_t *out, size_t out_len)
int botan_rng_init (botan_rng_t *rng_out, const char *rng_type)
int botan_rng_reseed (botan_rng_t rng, size_t bits)
uint32_t botan_version_datestamp ()
uint32_t botan_version_major ()
uint32_t botan_version_minor ()
uint32_t botan_version_patch ()
const char * botan_version_string ()

Define Documentation

#define BOTAN_ASSERT_ARG_NON_NULL (   p)    do { if(!p) throw std::invalid_argument("Argument " #p " is null"); } while(0)

Definition at line 44 of file ffi.cpp.

Referenced by botan_rng_init().

#define BOTAN_FFI_DO (   T,
  obj,
  block 
)    apply_fn(obj, BOTAN_CURRENT_FUNCTION, [=](T& obj) { do { block } while(0); return 0; })

Function Documentation

Definition at line 422 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::Transform::clear().

   {
   return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, { cipher.clear(); });
   }

Definition at line 416 of file ffi.cpp.

   {
   delete cipher;
   return 0;
   }
int botan_cipher_get_default_nonce_length ( botan_cipher_t  cipher,
size_t *  nl 
)

Definition at line 575 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::Transform::default_nonce_length().

   {
   return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, { *nl = cipher.default_nonce_length(); });
   }
int botan_cipher_get_tag_length ( botan_cipher_t  cipher,
size_t *  tl 
)

Definition at line 580 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::Cipher_Mode::tag_size().

   {
   return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, { *tl = cipher.tag_size(); });
   }
int botan_cipher_init ( botan_cipher_t cipher,
const char *  cipher_name,
uint32_t  flags 
)

Definition at line 392 of file ffi.cpp.

References BOTAN_CIPHER_INIT_FLAG_ENCRYPT, BOTAN_CIPHER_INIT_FLAG_MASK_DIRECTION, Botan::DECRYPTION, Botan::ENCRYPTION, and Botan::get_cipher_mode().

   {
   try
      {
      const bool encrypt_p = ((flags & BOTAN_CIPHER_INIT_FLAG_MASK_DIRECTION) == BOTAN_CIPHER_INIT_FLAG_ENCRYPT);
      const Botan::Cipher_Dir dir = encrypt_p ? Botan::ENCRYPTION : Botan::DECRYPTION;
      std::unique_ptr<Botan::Cipher_Mode> mode(Botan::get_cipher_mode(cipher_name, dir));
      if(!mode)
         return -1;
      *cipher = new botan_cipher_struct(mode.release());
      return 0;
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }
   catch(...)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, "unknown");
      }

   return -1;
   }
int botan_cipher_set_associated_data ( botan_cipher_t  cipher,
const uint8_t *  ad,
size_t  ad_len 
)

Definition at line 556 of file ffi.cpp.

References BOTAN_FFI_DO.

   {
   return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, {
      if(Botan::AEAD_Mode* aead = dynamic_cast<Botan::AEAD_Mode*>(&cipher))
         {
         aead->set_associated_data(ad, ad_len);
         return 0;
         }
      return -1;
      });
   }
int botan_cipher_set_key ( botan_cipher_t  cipher,
const uint8_t *  key,
size_t  key_len 
)

Definition at line 427 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::Keyed_Transform::set_key().

   {
   return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, { cipher.set_key(key, key_len); });
   }
int botan_cipher_start ( botan_cipher_t  cipher_obj,
const uint8_t *  nonce,
size_t  nonce_len 
)

Definition at line 433 of file ffi.cpp.

References BOTAN_ASSERT, Botan::Transform::start(), and Botan::Transform::update_granularity().

   {
   try
      {
      Botan::Cipher_Mode& cipher = safe_get(cipher_obj);
      BOTAN_ASSERT(cipher.start(nonce, nonce_len).empty(), "Ciphers have no prefix");
      cipher_obj->m_buf.reserve(cipher.update_granularity());
      return 0;
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }

   return -1;
   }
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 at line 451 of file ffi.cpp.

References BOTAN_ASSERT, BOTAN_CIPHER_UPDATE_FLAG_FINAL, Botan::copy_mem(), Botan::Transform::finish(), Botan::Transform::minimum_final_size(), Botan::round_down(), Botan::Transform::update(), and Botan::Transform::update_granularity().

   {
   using namespace Botan;

   try
      {
      Cipher_Mode& cipher = safe_get(cipher_obj);
      secure_vector<uint8_t>& mbuf = cipher_obj->m_buf;

      const bool final_input = (flags & BOTAN_CIPHER_UPDATE_FLAG_FINAL);

      if(final_input)
         {
         mbuf.assign(input, input + input_size);
         *input_consumed = input_size;
         *output_written = 0;

         try
            {
            cipher.finish(mbuf);
            }
         catch(Integrity_Failure& e)
            {
            log_exception(BOTAN_CURRENT_FUNCTION, e.what());
            return -2;
            }

         *output_written = mbuf.size();

         if(mbuf.size() <= output_size)
            {
            copy_mem(output, &mbuf[0], mbuf.size());
            mbuf.clear();
            return 0;
            }

         return -1;
         }

      if(input_size == 0)
         {
         // Currently must take entire buffer in this case
         *output_written = mbuf.size();
         if(output_size >= mbuf.size())
            {
            copy_mem(output, &mbuf[0], mbuf.size());
            mbuf.clear();
            return 0;
            }

         return -1;
         }

      const size_t ud = cipher.update_granularity();
      BOTAN_ASSERT(cipher.update_granularity() > cipher.minimum_final_size(), "logic error");

#if 0
      // Avoiding double copy:
      if(Online_Cipher_Mode* ocm = dynamic_cast<Online_Cipher_Mode*>(&cipher))
         {
         const size_t taken = round_down(input_size, ud);
         *input_consumed = taken;
         *output_size = taken;
         copy_mem(&output[0], input, taken);
         ocm->update_in_place(output, taken);
         return 0;
         }
#endif

      mbuf.resize(ud);
      size_t taken = 0, written = 0;

      while(input_size >= ud && output_size >= ud)
         {
         copy_mem(&mbuf[0], input, ud);
         cipher.update(mbuf);

         input_size -= ud;
         input += ud;
         taken += ud;

         output_size -= ud;
         output += ud;
         written += ud;
         }

      *output_written = written;
      *input_consumed = taken;

      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }

   return -1;
   }
int botan_cipher_valid_nonce_length ( botan_cipher_t  cipher,
size_t  nl 
)

Definition at line 570 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::Transform::valid_nonce_length().

   {
   return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, { return cipher.valid_nonce_length(nl) ? 1 : 0; });
   }
uint32_t botan_ffi_api_version ( )

Definition at line 201 of file ffi.cpp.

   {
   return 20150210; // should match value in info.txt
   }

Definition at line 321 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::HashFunction::clear().

   {
   return BOTAN_FFI_DO(Botan::HashFunction, hash, { hash.clear(); });
   }

Definition at line 310 of file ffi.cpp.

   {
   delete hash;
   return 0;
   }
int botan_hash_final ( botan_hash_t  hash,
uint8_t  out[] 
)

Definition at line 331 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::Buffered_Computation::final().

   {
   return BOTAN_FFI_DO(Botan::HashFunction, hash, { hash.final(out); });
   }
int botan_hash_init ( botan_hash_t hash,
const char *  hash_name,
uint32_t  flags 
)

Definition at line 283 of file ffi.cpp.

References BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_FFI_ERROR_EXCEPTION_THROWN, BOTAN_FFI_ERROR_NULL_POINTER, and Botan::get_hash_function().

   {
   try
      {
      if(hash == nullptr || hash_name == nullptr || *hash_name == 0)
         return BOTAN_FFI_ERROR_NULL_POINTER;
      if(flags != 0)
         return BOTAN_FFI_ERROR_BAD_FLAG;

      if(auto h = Botan::get_hash_function(hash_name))
         {
         *hash = new botan_hash_struct(h);
         return 0;
         }
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }
   catch(...)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, "unknown");
      }

   return BOTAN_FFI_ERROR_EXCEPTION_THROWN;
   }
int botan_hash_output_length ( botan_hash_t  hash,
size_t *  out 
)

Definition at line 316 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::Buffered_Computation::output_length().

   {
   return BOTAN_FFI_DO(Botan::HashFunction, hash, { *out = hash.output_length(); });
   }
int botan_hash_update ( botan_hash_t  hash,
const uint8_t *  buf,
size_t  len 
)

Definition at line 326 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::Buffered_Computation::update().

   {
   return BOTAN_FFI_DO(Botan::HashFunction, hash, { hash.update(buf, len); });
   }
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 
)

Definition at line 624 of file ffi.cpp.

References Botan::get_kdf().

   {
   try
      {
      std::unique_ptr<Botan::KDF> kdf(Botan::get_kdf(kdf_algo));
      kdf->kdf(out, out_len, secret, secret_len, salt, salt_len);
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }

   return -1;
   }

Definition at line 377 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::SymmetricAlgorithm::clear().

Definition at line 361 of file ffi.cpp.

References mac.

   {
   delete mac;
   return 0;
   }
int botan_mac_final ( botan_mac_t  mac,
uint8_t  out[] 
)

Definition at line 387 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::Buffered_Computation::final().

   {
   return BOTAN_FFI_DO(Botan::MessageAuthenticationCode, mac, { mac.final(out); });
   }
int botan_mac_init ( botan_mac_t mac,
const char *  mac_name,
uint32_t  flags 
)

Definition at line 336 of file ffi.cpp.

References Botan::get_mac().

   {
   try
      {
      if(!mac || !mac_name || flags != 0)
         return -1;

      if(auto m = Botan::get_mac(mac_name))
         {
         *mac = new botan_mac_struct(m);
         return 0;
         }
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }
   catch(...)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, "unknown");
      }

   return -2;
   }
int botan_mac_output_length ( botan_mac_t  mac,
size_t *  out 
)

Definition at line 372 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::Buffered_Computation::output_length().

   {
   return BOTAN_FFI_DO(Botan::MessageAuthenticationCode, mac, { *out = mac.output_length(); });
   }
int botan_mac_set_key ( botan_mac_t  mac,
const uint8_t *  key,
size_t  key_len 
)

Definition at line 367 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::SymmetricAlgorithm::set_key().

   {
   return BOTAN_FFI_DO(Botan::MessageAuthenticationCode, mac, { mac.set_key(key, key_len); });
   }
int botan_mac_update ( botan_mac_t  mac,
const uint8_t *  buf,
size_t  len 
)

Definition at line 382 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::Buffered_Computation::update().

   {
   return BOTAN_FFI_DO(Botan::MessageAuthenticationCode, mac, { mac.update(buf, len); });
   }
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 at line 585 of file ffi.cpp.

References Botan::get_pbkdf().

   {
   try
      {
      std::unique_ptr<Botan::PBKDF> pbkdf(Botan::get_pbkdf(pbkdf_algo));
      pbkdf->pbkdf_iterations(out, out_len, pass, salt, salt_len, iterations);
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }

   return -1;
   }
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 at line 602 of file ffi.cpp.

References Botan::get_pbkdf().

   {
   try
      {
      std::unique_ptr<Botan::PBKDF> pbkdf(Botan::get_pbkdf(pbkdf_algo));
      pbkdf->pbkdf_timed(out, out_len, password, salt, salt_len,
                         std::chrono::milliseconds(ms_to_run),
                         *iterations_used);
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }

   return -1;
   }
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 at line 983 of file ffi.cpp.

References BOTAN_FFI_DO.

   {
   return BOTAN_FFI_DO(Botan::PK_Decryptor, op, {
      return write_vec_output(out, out_len, op.decrypt(ciphertext, ciphertext_len));
      });
   }
int botan_pk_op_decrypt_create ( botan_pk_op_decrypt_t op,
botan_privkey_t  key_obj,
const char *  padding,
uint32_t  flags 
)

Definition at line 953 of file ffi.cpp.

References BOTAN_ASSERT_NONNULL, and BOTAN_FFI_ERROR_BAD_FLAG.

   {
   try
      {
      BOTAN_ASSERT_NONNULL(op);

      if(flags != 0)
         return BOTAN_FFI_ERROR_BAD_FLAG;

      std::unique_ptr<Botan::PK_Decryptor> pk(new Botan::PK_Decryptor_EME(safe_get(key_obj), padding));
      *op = new botan_pk_op_decrypt_struct(pk.release());
      return 0;
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }

   return -1;
   }

Definition at line 977 of file ffi.cpp.

   {
   delete op;
   return 0;
   }
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 at line 940 of file ffi.cpp.

References BOTAN_FFI_DO.

   {
   return BOTAN_FFI_DO(Botan::PK_Encryptor, op, {
      return write_vec_output(out, out_len, op.encrypt(plaintext, plaintext_len, safe_get(rng_obj)));
      });
   }
int botan_pk_op_encrypt_create ( botan_pk_op_encrypt_t op,
botan_pubkey_t  key_obj,
const char *  padding,
uint32_t  flags 
)

Definition at line 910 of file ffi.cpp.

References BOTAN_ASSERT_NONNULL, and BOTAN_FFI_ERROR_BAD_FLAG.

   {
   try
      {
      BOTAN_ASSERT_NONNULL(op);

      if(flags != 0)
         return BOTAN_FFI_ERROR_BAD_FLAG;

      std::unique_ptr<Botan::PK_Encryptor> pk(new Botan::PK_Encryptor_EME(safe_get(key_obj), padding));
      *op = new botan_pk_op_encrypt_struct(pk.release());
      return 0;
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }

   return -1;
   }

Definition at line 934 of file ffi.cpp.

   {
   delete op;
   return 0;
   }
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 at line 1124 of file ffi.cpp.

References Botan::OctetString::bits_of(), BOTAN_FFI_DO, and Botan::PK_Key_Agreement::derive_key().

   {
   return BOTAN_FFI_DO(Botan::PK_Key_Agreement, op, {
      auto k = op.derive_key(*out_len, other_key, other_key_len, salt, salt_len).bits_of();
      return write_vec_output(out, out_len, k);
      });
   }
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 at line 1084 of file ffi.cpp.

References BOTAN_ASSERT_NONNULL, and BOTAN_FFI_ERROR_BAD_FLAG.

   {
   try
      {
      BOTAN_ASSERT_NONNULL(op);

      if(flags != 0)
         return BOTAN_FFI_ERROR_BAD_FLAG;

      std::unique_ptr<Botan::PK_Key_Agreement> pk(new Botan::PK_Key_Agreement(safe_get(key_obj), kdf));
      *op = new botan_pk_op_ka_struct(pk.release());
      return 0;
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }

   return -1;
   }

Definition at line 1108 of file ffi.cpp.

   {
   delete op;
   return 0;
   }
int botan_pk_op_key_agreement_export_public ( botan_privkey_t  key,
uint8_t  out[],
size_t *  out_len 
)

Definition at line 1114 of file ffi.cpp.

References BOTAN_FFI_DO.

   {
   return BOTAN_FFI_DO(Botan::Private_Key, key, {
      if(auto kak = dynamic_cast<const Botan::PK_Key_Agreement_Key*>(&key))
         return write_vec_output(out, out_len, kak->public_value());
      return -2;
      });
   }
int botan_pk_op_sign_create ( botan_pk_op_sign_t op,
botan_privkey_t  key_obj,
const char *  hash,
uint32_t  flags 
)

Definition at line 995 of file ffi.cpp.

References BOTAN_ASSERT_NONNULL, BOTAN_FFI_ERROR_BAD_FLAG, and BOTAN_FFI_ERROR_EXCEPTION_THROWN.

   {
   try
      {
      BOTAN_ASSERT_NONNULL(op);

      if(flags != 0)
         return BOTAN_FFI_ERROR_BAD_FLAG;

      std::unique_ptr<Botan::PK_Signer> pk(new Botan::PK_Signer(safe_get(key_obj), hash));
      *op = new botan_pk_op_sign_struct(pk.release());
      return 0;
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }

   return BOTAN_FFI_ERROR_EXCEPTION_THROWN;
   }

Definition at line 1019 of file ffi.cpp.

   {
   delete op;
   return 0;
   }
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 at line 1030 of file ffi.cpp.

References BOTAN_FFI_DO.

   {
   return BOTAN_FFI_DO(Botan::PK_Signer, op, {
      return write_vec_output(out, out_len, op.signature(safe_get(rng_obj)));
      });
   }
int botan_pk_op_sign_update ( botan_pk_op_sign_t  op,
const uint8_t  in[],
size_t  in_len 
)

Definition at line 1025 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::PK_Signer::update().

   {
   return BOTAN_FFI_DO(Botan::PK_Signer, op, { op.update(in, in_len); });
   }
int botan_pk_op_verify_create ( botan_pk_op_verify_t op,
botan_pubkey_t  key_obj,
const char *  hash,
uint32_t  flags 
)

Definition at line 1037 of file ffi.cpp.

References BOTAN_ASSERT_NONNULL, and BOTAN_FFI_ERROR_BAD_FLAG.

   {
   try
      {
      BOTAN_ASSERT_NONNULL(op);

      if(flags != 0)
         return BOTAN_FFI_ERROR_BAD_FLAG;

      std::unique_ptr<Botan::PK_Verifier> pk(new Botan::PK_Verifier(safe_get(key_obj), hash));
      *op = new botan_pk_op_verify_struct(pk.release());
      return 0;
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }

   return -1;
   }

Definition at line 1061 of file ffi.cpp.

   {
   delete op;
   return 0;
   }
int botan_pk_op_verify_finish ( botan_pk_op_verify_t  op,
const uint8_t  sig[],
size_t  sig_len 
)

Definition at line 1072 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::PK_Verifier::check_signature().

   {
   return BOTAN_FFI_DO(Botan::PK_Verifier, op, {
      const bool legit = op.check_signature(sig, sig_len);

      if(legit)
         return 0;
      else
         return 1;
      });
   }
int botan_pk_op_verify_update ( botan_pk_op_verify_t  op,
const uint8_t  in[],
size_t  in_len 
)

Definition at line 1067 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::PK_Verifier::update().

   {
   return BOTAN_FFI_DO(Botan::PK_Verifier, op, { op.update(in, in_len); });
   }
int botan_privkey_create_ecdh ( botan_privkey_t key_obj,
botan_rng_t  rng_obj,
const char *  param_str 
)

Definition at line 750 of file ffi.cpp.

References BOTAN_FFI_ERROR_EXCEPTION_THROWN.

   {
   try
      {
      if(key_obj == nullptr || rng_obj == nullptr || param_str == nullptr || *param_str == 0)
         return -1;

      *key_obj = nullptr;

      const std::string params(param_str);

#if defined(BOTAN_HAS_CURVE_25519)
      if(params == "curve25519")
         {
         std::unique_ptr<Botan::Private_Key> key(new Botan::Curve25519_PrivateKey(safe_get(rng_obj)));
         *key_obj = new botan_privkey_struct(key.release());
         return 0;
         }
#endif

#if defined(BOTAN_HAS_ECDH)
      Botan::EC_Group grp(params);
      std::unique_ptr<Botan::Private_Key> key(new Botan::ECDH_PrivateKey(safe_get(rng_obj), grp));
      *key_obj = new botan_privkey_struct(key.release());
      return 0;
#endif
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }

   return BOTAN_FFI_ERROR_EXCEPTION_THROWN;
   }
int botan_privkey_create_ecdsa ( botan_privkey_t key_obj,
botan_rng_t  rng_obj,
const char *  param_str 
)

Definition at line 725 of file ffi.cpp.

References BOTAN_FFI_ERROR_EXCEPTION_THROWN.

   {
   try
      {
      if(key_obj == nullptr || rng_obj == nullptr || param_str == nullptr || *param_str == 0)
         return -1;

      *key_obj = nullptr;

#if defined(BOTAN_HAS_ECDSA)
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
      Botan::EC_Group grp(param_str);
      std::unique_ptr<Botan::Private_Key> key(new Botan::ECDSA_PrivateKey(rng, grp));
      *key_obj = new botan_privkey_struct(key.release());
      return 0;
#endif
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }

   return BOTAN_FFI_ERROR_EXCEPTION_THROWN;
   }
int botan_privkey_create_rsa ( botan_privkey_t key_obj,
botan_rng_t  rng_obj,
size_t  n_bits 
)

Definition at line 698 of file ffi.cpp.

References BOTAN_FFI_ERROR_EXCEPTION_THROWN.

   {
   try
      {
      if(key_obj == nullptr || rng_obj == nullptr)
         return -1;
      if(n_bits < 1024 || n_bits > 16*1024)
         return -2;

      *key_obj = nullptr;

#if defined(BOTAN_HAS_RSA)
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
      std::unique_ptr<Botan::Private_Key> key(new Botan::RSA_PrivateKey(rng, n_bits));
      *key_obj = new botan_privkey_struct(key.release());
      return 0;
#endif
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }

   return BOTAN_FFI_ERROR_EXCEPTION_THROWN;
   }

Definition at line 817 of file ffi.cpp.

   {
   delete key;
   return 0;
   }
int botan_privkey_export ( botan_privkey_t  key,
uint8_t  out[],
size_t *  out_len,
uint32_t  flags 
)

Definition at line 864 of file ffi.cpp.

References Botan::PKCS8::BER_encode(), BOTAN_FFI_DO, BOTAN_PRIVKEY_EXPORT_FLAG_DER, BOTAN_PRIVKEY_EXPORT_FLAG_PEM, and Botan::PKCS8::PEM_encode().

   {
   return BOTAN_FFI_DO(Botan::Private_Key, key, {
      if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER)
         return write_vec_output(out, out_len, Botan::PKCS8::BER_encode(key));
      else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM)
         return write_str_output(out, out_len, Botan::PKCS8::PEM_encode(key));
      else
         return -2;
      });
   }
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 *  pbe,
uint32_t  flags 
)

Definition at line 876 of file ffi.cpp.

References Botan::PKCS8::BER_encode(), BOTAN_FFI_DO, BOTAN_PRIVKEY_EXPORT_FLAG_DER, BOTAN_PRIVKEY_EXPORT_FLAG_PEM, and Botan::PKCS8::PEM_encode().

   {
   return BOTAN_FFI_DO(Botan::Private_Key, key, {
      auto pbkdf_time = std::chrono::milliseconds(300);
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);

      if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER)
         return write_vec_output(out, out_len, Botan::PKCS8::BER_encode(key, rng, pass, pbkdf_time, pbe));
      else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM)
         return write_str_output(out, out_len, Botan::PKCS8::PEM_encode(key, rng, pass, pbkdf_time, pbe));
      else
         return -2;
      });
   }

Definition at line 829 of file ffi.cpp.

References Botan::X509::BER_encode(), BOTAN_FFI_ERROR_EXCEPTION_THROWN, and Botan::X509::load_key().

   {
   try
      {
      std::unique_ptr<Botan::Public_Key> pubkey(
         Botan::X509::load_key(
            Botan::X509::BER_encode(safe_get(key_obj))));
      *pubout = new botan_pubkey_struct(pubkey.release());
      return 0;
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }

   return BOTAN_FFI_ERROR_EXCEPTION_THROWN;
   }
int botan_privkey_load ( botan_privkey_t key,
botan_rng_t  rng_obj,
const uint8_t  bits[],
size_t  len,
const char *  password 
)

Definition at line 785 of file ffi.cpp.

References BOTAN_FFI_ERROR_EXCEPTION_THROWN, and Botan::PKCS8::load_key().

   {
   try
      {
      Botan::DataSource_Memory src(bits, len);

      if(password == nullptr)
         password = "";

      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);

      std::unique_ptr<Botan::PKCS8_PrivateKey> pkcs8;
      pkcs8.reset(Botan::PKCS8::load_key(src, rng, password));

      if(pkcs8)
         {
         *key = new botan_privkey_struct(pkcs8.release());
         return 0;
         }
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }

   return -1;
   *key = nullptr;
   return BOTAN_FFI_ERROR_EXCEPTION_THROWN;
   }
int botan_pubkey_algo_name ( botan_pubkey_t  key,
char  out[],
size_t *  out_len 
)

Definition at line 847 of file ffi.cpp.

References BOTAN_FFI_DO.

   {
   return BOTAN_FFI_DO(Botan::Public_Key, key, { return write_str_output(out, out_len, key.algo_name()); });
   }

Definition at line 823 of file ffi.cpp.

   {
   delete key;
   return 0;
   }
int botan_pubkey_estimated_strength ( botan_pubkey_t  key,
size_t *  estimate 
)

Definition at line 896 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::Public_Key::estimated_strength().

   {
   return BOTAN_FFI_DO(Botan::Public_Key, key, { *estimate = key.estimated_strength(); });
   }
int botan_pubkey_export ( botan_pubkey_t  key,
uint8_t  out[],
size_t *  out_len,
uint32_t  flags 
)

Definition at line 852 of file ffi.cpp.

References Botan::X509::BER_encode(), BOTAN_FFI_DO, BOTAN_PRIVKEY_EXPORT_FLAG_DER, BOTAN_PRIVKEY_EXPORT_FLAG_PEM, and Botan::X509::PEM_encode().

   {
   return BOTAN_FFI_DO(Botan::Public_Key, key, {
      if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_DER)
         return write_vec_output(out, out_len, Botan::X509::BER_encode(key));
      else if(flags == BOTAN_PRIVKEY_EXPORT_FLAG_PEM)
         return write_str_output(out, out_len, Botan::X509::PEM_encode(key));
      else
         return -2;
      });
   }
int botan_pubkey_fingerprint ( botan_pubkey_t  key,
const char *  hash_fn,
uint8_t  out[],
size_t *  out_len 
)

Definition at line 901 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::get_hash().

   {
   return BOTAN_FFI_DO(Botan::Public_Key, key, {
      std::unique_ptr<Botan::HashFunction> h(Botan::get_hash(hash_fn));
      return write_vec_output(out, out_len, h->process(key.x509_subject_public_key()));
      });
   }

Definition at line 267 of file ffi.cpp.

   {
   delete rng;
   return 0;
   }
int botan_rng_get ( botan_rng_t  rng,
uint8_t *  out,
size_t  out_len 
)

Definition at line 273 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::RandomNumberGenerator::randomize().

   {
   return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, { rng.randomize(out, out_len); });
   }
int botan_rng_init ( botan_rng_t rng_out,
const char *  rng_type 
)

Definition at line 216 of file ffi.cpp.

References BOTAN_ASSERT_ARG_NON_NULL, and Botan::system_rng().

   {
   // Just gives unique_ptr something to delete, really
   class RNG_Wrapper : public Botan::RandomNumberGenerator
      {
      public:
         RNG_Wrapper(Botan::RandomNumberGenerator& rng) : m_rng(rng) {}
         void randomize(Botan::byte out[], size_t len) override { m_rng.randomize(out, len); }
         bool is_seeded() const override { return m_rng.is_seeded(); }
         void clear() override { m_rng.clear(); }
         std::string name() const { return m_rng.name(); }
         void reseed(size_t poll_bits = 256) { m_rng.reseed(poll_bits); }
         void add_entropy(const Botan::byte in[], size_t len) { m_rng.add_entropy(in, len); }
      private:
         Botan::RandomNumberGenerator& m_rng;
      };

   try
      {
      BOTAN_ASSERT_ARG_NON_NULL(rng_out);

      if(rng_type == nullptr || *rng_type == 0)
         rng_type = "system";

      const std::string rng_type_s(rng_type);

      std::unique_ptr<Botan::RandomNumberGenerator> rng;

      if(rng_type_s == "system")
         rng.reset(new RNG_Wrapper(Botan::system_rng()));
      else if(rng_type_s == "user")
         rng.reset(new Botan::AutoSeeded_RNG);

      if(rng)
         {
         *rng_out = new botan_rng_struct(rng.release());
         return 0;
         }
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }
   catch(...)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, "unknown");
      }

   return -1;
   }
int botan_rng_reseed ( botan_rng_t  rng,
size_t  bits 
)

Definition at line 278 of file ffi.cpp.

References BOTAN_FFI_DO, and Botan::RandomNumberGenerator::reseed().

   {
   return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, { rng.reseed(bits); });
   }

Definition at line 214 of file ffi.cpp.

References Botan::version_datestamp().

uint32_t botan_version_major ( )

Definition at line 211 of file ffi.cpp.

References Botan::version_major().

{ return Botan::version_major(); }
uint32_t botan_version_minor ( )

Definition at line 212 of file ffi.cpp.

References Botan::version_minor().

{ return Botan::version_minor(); }
uint32_t botan_version_patch ( )

Definition at line 213 of file ffi.cpp.

References Botan::version_patch().

{ return Botan::version_patch(); }
const char* botan_version_string ( )

Definition at line 206 of file ffi.cpp.

References Botan::version_cstr().

   {
   return Botan::version_cstr();
   }