Botan  2.6.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(BOTAN_CURRENT_FUNCTION, [=]() -> 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  return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.set_key(key, len); });
54  }
55 
56 /**
57 * Return the positive block size of this block cipher, or negative to
58 * indicate an error
59 */
61  {
62  return BOTAN_FFI_DO(Botan::BlockCipher, bc, b,
63  { return static_cast<int>(b.block_size()); });
64  }
65 
67  const uint8_t in[],
68  uint8_t out[],
69  size_t blocks)
70  {
71  return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.encrypt_n(in, out, blocks); });
72  }
73 
75  const uint8_t in[],
76  uint8_t out[],
77  size_t blocks)
78  {
79  return BOTAN_FFI_DO(Botan::BlockCipher, bc, b, { b.decrypt_n(in, out, blocks); });
80  }
81 
82 }
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:66
int ffi_guard_thunk(const char *func_name, Thunk thunk)
Definition: ffi_util.h:64
int botan_block_cipher_block_size(botan_block_cipher_t bc)
Definition: ffi_block.cpp:60
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:66
int botan_block_cipher_clear(botan_block_cipher_t bc)
Definition: ffi_block.cpp:42
virtual void clear()=0
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:126
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:490
int botan_block_cipher_destroy(botan_block_cipher_t bc)
Definition: ffi_block.cpp:37
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:143
BOTAN_FFI_DECLARE_STRUCT(botan_block_cipher_struct, Botan::BlockCipher, 0x64C29716)
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98
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:74