Botan  2.11.0
Crypto and TLS for C++11
ffi_block.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2015,2017 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6 
7 #include <botan/ffi.h>
8 #include <botan/internal/ffi_util.h>
9 #include <botan/block_cipher.h>
10 
11 extern "C" {
12 
13 using namespace Botan_FFI;
14 
15 BOTAN_FFI_DECLARE_STRUCT(botan_block_cipher_struct, Botan::BlockCipher, 0x64C29716);
16 
17 int botan_block_cipher_init(botan_block_cipher_t* bc, const char* bc_name)
18  {
19  return ffi_guard_thunk(__func__, [=]() -> int {
20  if(bc == nullptr || bc_name == nullptr || *bc_name == 0)
22 
23  *bc = nullptr;
24 
25  std::unique_ptr<Botan::BlockCipher> cipher(Botan::BlockCipher::create(bc_name));
26  if(cipher == nullptr)
28 
29  *bc = new botan_block_cipher_struct(cipher.release());
30  return BOTAN_FFI_SUCCESS;
31  });
32  }
33 
34 /**
35 * Destroy a block cipher object
36 */
38  {
39  return BOTAN_FFI_CHECKED_DELETE(bc);
40  }
41 
43  {
44  return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.clear(); });
45  }
46 
47 /**
48 * Set the key for a block cipher instance
49 */
51  const uint8_t key[], size_t len)
52  {
53  if(key == nullptr)
55  return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.set_key(key, len); });
56  }
57 
58 /**
59 * Return the positive block size of this block cipher, or negative to
60 * indicate an error
61 */
63  {
64  return BOTAN_FFI_DO(Botan::BlockCipher, bc, b,
65  { return static_cast<int>(b.block_size()); });
66  }
67 
69  const uint8_t in[],
70  uint8_t out[],
71  size_t blocks)
72  {
73  if(in == nullptr || out == nullptr)
75  return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.encrypt_n(in, out, blocks); });
76  }
77 
79  const uint8_t in[],
80  uint8_t out[],
81  size_t blocks)
82  {
83  if(in == nullptr || out == nullptr)
85  return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.decrypt_n(in, out, blocks); });
86  }
87 
88 int botan_block_cipher_name(botan_block_cipher_t cipher, char* name, size_t* name_len)
89  {
90  if(name_len == nullptr)
92 
93  return BOTAN_FFI_DO(Botan::BlockCipher, cipher, bc, {
94  return write_str_output(name, name_len, bc.name()); });
95  }
96 
98  size_t* out_minimum_keylength,
99  size_t* out_maximum_keylength,
100  size_t* out_keylength_modulo)
101  {
102  return BOTAN_FFI_DO(Botan::BlockCipher, cipher, bc, {
103  if(out_minimum_keylength)
104  *out_minimum_keylength = bc.minimum_keylength();
105  if(out_maximum_keylength)
106  *out_maximum_keylength = bc.maximum_keylength();
107  if(out_keylength_modulo)
108  *out_keylength_modulo = bc.key_spec().keylength_multiple();
109  });
110  }
111 
112 }
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:86
int botan_block_cipher_name(botan_block_cipher_t cipher, char *name, size_t *name_len)
Definition: ffi_block.cpp:88
virtual void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const =0
int botan_block_cipher_encrypt_blocks(botan_block_cipher_t bc, const uint8_t in[], uint8_t out[], size_t blocks)
Definition: ffi_block.cpp:68
int botan_block_cipher_block_size(botan_block_cipher_t bc)
Definition: ffi_block.cpp:62
int write_str_output(uint8_t out[], size_t *out_len, const std::string &str)
Definition: ffi_util.h:151
std::string name
int botan_block_cipher_init(botan_block_cipher_t *bc, const char *bc_name)
Definition: ffi_block.cpp:17
void set_key(const SymmetricKey &key)
Definition: sym_algo.h:65
int botan_block_cipher_clear(botan_block_cipher_t bc)
Definition: ffi_block.cpp:42
int botan_block_cipher_get_keyspec(botan_block_cipher_t cipher, size_t *out_minimum_keylength, size_t *out_maximum_keylength, size_t *out_keylength_modulo)
Definition: ffi_block.cpp:97
virtual void clear()=0
size_t minimum_keylength() const
Definition: sym_algo.h:46
int botan_block_cipher_set_key(botan_block_cipher_t bc, const uint8_t key[], size_t len)
Definition: ffi_block.cpp:50
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:120
virtual void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const =0
static std::unique_ptr< BlockCipher > create(const std::string &algo_spec, const std::string &provider="")
struct botan_block_cipher_struct * botan_block_cipher_t
Definition: ffi.h:655
int botan_block_cipher_destroy(botan_block_cipher_t bc)
Definition: ffi_block.cpp:37
BOTAN_FFI_DECLARE_STRUCT(botan_block_cipher_struct, Botan::BlockCipher, 0x64C29716)
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:92
int botan_block_cipher_decrypt_blocks(botan_block_cipher_t bc, const uint8_t in[], uint8_t out[], size_t blocks)
Definition: ffi_block.cpp:78