Botan  2.7.0
Crypto and TLS for C++11
Functions
ffi_cipher.cpp File Reference
#include <botan/ffi.h>
#include <botan/internal/ffi_util.h>
#include <botan/aead.h>

Go to the source code of this file.

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_get_update_granularity (botan_cipher_t cipher, size_t *ug)
 
int botan_cipher_init (botan_cipher_t *cipher, const char *cipher_name, uint32_t flags)
 
int botan_cipher_query_keylen (botan_cipher_t cipher, size_t *out_minimum_keylength, size_t *out_maximum_keylength)
 
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_ptr[], size_t orig_output_size, size_t *output_written, const uint8_t input_ptr[], size_t orig_input_size, size_t *input_consumed)
 
int botan_cipher_valid_nonce_length (botan_cipher_t cipher, size_t nl)
 

Function Documentation

◆ botan_cipher_clear()

int botan_cipher_clear ( botan_cipher_t  cipher)

Definition at line 39 of file ffi_cipher.cpp.

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

40  {
41  return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, { c.clear(); });
42  }
virtual void clear()=0
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98

◆ botan_cipher_destroy()

int botan_cipher_destroy ( botan_cipher_t  cipher)

Definition at line 34 of file ffi_cipher.cpp.

References BOTAN_FFI_CHECKED_DELETE.

35  {
36  return BOTAN_FFI_CHECKED_DELETE(cipher);
37  }
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:126

◆ botan_cipher_get_default_nonce_length()

int botan_cipher_get_default_nonce_length ( botan_cipher_t  cipher,
size_t *  nl 
)

Definition at line 181 of file ffi_cipher.cpp.

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

182  {
183  return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, { *nl = c.default_nonce_length(); });
184  }
virtual size_t default_nonce_length() const =0
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98

◆ botan_cipher_get_tag_length()

int botan_cipher_get_tag_length ( botan_cipher_t  cipher,
size_t *  tl 
)

Definition at line 191 of file ffi_cipher.cpp.

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

192  {
193  return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, { *tl = c.tag_size(); });
194  }
virtual size_t tag_size() const
Definition: cipher_mode.h:184
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98

◆ botan_cipher_get_update_granularity()

int botan_cipher_get_update_granularity ( botan_cipher_t  cipher,
size_t *  ug 
)

Definition at line 186 of file ffi_cipher.cpp.

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

187  {
188  return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, { *ug = c.update_granularity(); });
189  }
virtual size_t update_granularity() const =0
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98

◆ botan_cipher_init()

int botan_cipher_init ( botan_cipher_t cipher,
const char *  cipher_name,
uint32_t  flags 
)

Definition at line 21 of file ffi_cipher.cpp.

References BOTAN_CIPHER_INIT_FLAG_ENCRYPT, BOTAN_CIPHER_INIT_FLAG_MASK_DIRECTION, BOTAN_CURRENT_FUNCTION, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_SUCCESS, Botan::Cipher_Mode::create(), Botan::DECRYPTION, Botan::ENCRYPTION, Botan_FFI::ffi_guard_thunk(), and Botan::PKCS11::flags().

22  {
23  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
25  const Botan::Cipher_Dir dir = encrypt_p ? Botan::ENCRYPTION : Botan::DECRYPTION;
26  std::unique_ptr<Botan::Cipher_Mode> mode(Botan::Cipher_Mode::create(cipher_name, dir));
27  if(!mode)
29  *cipher = new botan_cipher_struct(mode.release());
30  return BOTAN_FFI_SUCCESS;
31  });
32  }
#define BOTAN_CIPHER_INIT_FLAG_ENCRYPT
Definition: ffi.h:375
#define BOTAN_CIPHER_INIT_FLAG_MASK_DIRECTION
Definition: ffi.h:374
int ffi_guard_thunk(const char *func_name, Thunk thunk)
Definition: ffi_util.h:64
Flags flags(Flag flags)
Definition: p11.h:858
static std::unique_ptr< Cipher_Mode > create(const std::string &algo, Cipher_Dir direction, const std::string &provider="")
Definition: cipher_mode.cpp:50
Cipher_Dir
Definition: cipher_mode.h:24
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:143

◆ botan_cipher_query_keylen()

int botan_cipher_query_keylen ( botan_cipher_t  cipher,
size_t *  out_minimum_keylength,
size_t *  out_maximum_keylength 
)

Definition at line 44 of file ffi_cipher.cpp.

References BOTAN_FFI_DO, Botan::Cipher_Mode::key_spec(), and Botan::Key_Length_Specification::minimum_keylength().

47  {
48  return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, {
49  *out_minimum_keylength = c.key_spec().minimum_keylength();
50  *out_maximum_keylength = c.key_spec().maximum_keylength();
51  });
52  }
virtual Key_Length_Specification key_spec() const =0
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98
size_t minimum_keylength() const
Definition: key_spec.h:61

◆ botan_cipher_set_associated_data()

int botan_cipher_set_associated_data ( botan_cipher_t  cipher,
const uint8_t *  ad,
size_t  ad_len 
)

Definition at line 162 of file ffi_cipher.cpp.

References BOTAN_FFI_DO, BOTAN_FFI_ERROR_BAD_PARAMETER, and BOTAN_FFI_SUCCESS.

165  {
166  return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, {
167  if(Botan::AEAD_Mode* aead = dynamic_cast<Botan::AEAD_Mode*>(&c))
168  {
169  aead->set_associated_data(ad, ad_len);
170  return BOTAN_FFI_SUCCESS;
171  }
173  });
174  }
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98

◆ botan_cipher_set_key()

int botan_cipher_set_key ( botan_cipher_t  cipher,
const uint8_t *  key,
size_t  key_len 
)

Definition at line 54 of file ffi_cipher.cpp.

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

56  {
57  return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, { c.set_key(key, key_len); });
58  }
void set_key(const std::vector< uint8_t, Alloc > &key)
Definition: cipher_mode.h:206
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98

◆ botan_cipher_start()

int botan_cipher_start ( botan_cipher_t  cipher_obj,
const uint8_t *  nonce,
size_t  nonce_len 
)

Definition at line 60 of file ffi_cipher.cpp.

References BOTAN_CURRENT_FUNCTION, BOTAN_FFI_SUCCESS, Botan_FFI::ffi_guard_thunk(), Botan_FFI::safe_get(), Botan::Cipher_Mode::start(), and Botan::Cipher_Mode::update_granularity().

62  {
63  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
64  Botan::Cipher_Mode& cipher = safe_get(cipher_obj);
65  cipher.start(nonce, nonce_len);
66  cipher_obj->m_buf.reserve(cipher.update_granularity());
67  return BOTAN_FFI_SUCCESS;
68  });
69  }
virtual size_t update_granularity() const =0
int ffi_guard_thunk(const char *func_name, Thunk thunk)
Definition: ffi_util.h:64
void start(const std::vector< uint8_t, Alloc > &nonce)
Definition: cipher_mode.h:72
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:49
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:143

◆ botan_cipher_update()

int botan_cipher_update ( botan_cipher_t  cipher_obj,
uint32_t  flags,
uint8_t  output_ptr[],
size_t  orig_output_size,
size_t *  output_written,
const uint8_t  input_ptr[],
size_t  orig_input_size,
size_t *  input_consumed 
)

Definition at line 71 of file ffi_cipher.cpp.

References BOTAN_ASSERT, BOTAN_CIPHER_UPDATE_FLAG_FINAL, BOTAN_CURRENT_FUNCTION, BOTAN_FFI_ERROR_BAD_MAC, BOTAN_FFI_SUCCESS, Botan::copy_mem(), Botan_FFI::ffi_guard_thunk(), Botan::Cipher_Mode::finish(), Botan::PKCS11::flags(), Botan::Cipher_Mode::minimum_final_size(), Botan_FFI::safe_get(), Botan::Cipher_Mode::update(), and Botan::Cipher_Mode::update_granularity().

79  {
80  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
81 
82  size_t input_size = orig_input_size;
83  size_t output_size = orig_output_size;
84  const uint8_t* input = input_ptr;
85  uint8_t* output = output_ptr;
86 
87  using namespace Botan;
88  Cipher_Mode& cipher = safe_get(cipher_obj);
89  secure_vector<uint8_t>& mbuf = cipher_obj->m_buf;
90 
91  const bool final_input = (flags & BOTAN_CIPHER_UPDATE_FLAG_FINAL);
92 
93  if(final_input)
94  {
95  mbuf.assign(input, input + input_size);
96  *input_consumed = input_size;
97  *output_written = 0;
98 
99  try
100  {
101  cipher.finish(mbuf);
102  }
103  catch(Integrity_Failure&)
104  {
106  }
107 
108  *output_written = mbuf.size();
109 
110  if(mbuf.size() <= output_size)
111  {
112  copy_mem(output, mbuf.data(), mbuf.size());
113  mbuf.clear();
114  return BOTAN_FFI_SUCCESS;
115  }
116 
117  return -1;
118  }
119 
120  if(input_size == 0)
121  {
122  // Currently must take entire buffer in this case
123  *output_written = mbuf.size();
124  if(output_size >= mbuf.size())
125  {
126  copy_mem(output, mbuf.data(), mbuf.size());
127  mbuf.clear();
128  return BOTAN_FFI_SUCCESS;
129  }
130 
131  return -1;
132  }
133 
134  const size_t ud = cipher.update_granularity();
135  BOTAN_ASSERT(cipher.update_granularity() > cipher.minimum_final_size(), "logic error");
136 
137  mbuf.resize(ud);
138  size_t taken = 0, written = 0;
139 
140  while(input_size >= ud && output_size >= ud)
141  {
142  copy_mem(mbuf.data(), input, ud);
143  cipher.update(mbuf);
144 
145  input_size -= ud;
146  copy_mem(output, mbuf.data(), ud);
147  input += ud;
148  taken += ud;
149 
150  output_size -= ud;
151  output += ud;
152  written += ud;
153  }
154 
155  *output_written = written;
156  *input_consumed = taken;
157 
158  return BOTAN_FFI_SUCCESS;
159  });
160  }
void update(secure_vector< uint8_t > &buffer, size_t offset=0)
Definition: cipher_mode.h:115
virtual size_t update_granularity() const =0
int ffi_guard_thunk(const char *func_name, Thunk thunk)
Definition: ffi_util.h:64
Flags flags(Flag flags)
Definition: p11.h:858
virtual size_t minimum_final_size() const =0
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:43
#define BOTAN_CIPHER_UPDATE_FLAG_FINAL
Definition: ffi.h:398
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:108
Definition: alg_id.cpp:13
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:49
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:143
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
virtual void finish(secure_vector< uint8_t > &final_block, size_t offset=0)=0

◆ botan_cipher_valid_nonce_length()

int botan_cipher_valid_nonce_length ( botan_cipher_t  cipher,
size_t  nl 
)

Definition at line 176 of file ffi_cipher.cpp.

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

177  {
178  return BOTAN_FFI_DO(Botan::Cipher_Mode, cipher, c, { return c.valid_nonce_length(nl) ? 1 : 0; });
179  }
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98
virtual bool valid_nonce_length(size_t nonce_len) const =0