8#include <botan/internal/ffi_util.h>
15struct botan_cipher_struct
final :
public botan_struct<Botan::Cipher_Mode, 0xB4A2BF9C>
18 explicit botan_cipher_struct(std::unique_ptr<Botan::Cipher_Mode> x,
size_t update_size) :
20 m_update_size(update_size)
22 m_buf.reserve(m_update_size);
26 size_t update_size()
const {
return m_update_size; }
50 if(minimum_final_size == 0 || update_granularity > minimum_final_size)
53 return update_granularity;
56 size_t buf_size = std::max(update_granularity, minimum_final_size + 1);
57 if(buf_size % update_granularity != 0)
58 buf_size += update_granularity - (buf_size % update_granularity);
76 const size_t update_size = ffi_choose_update_size(*mode);
78 *cipher =
new botan_cipher_struct(std::move(mode), update_size);
100 if(out_len ==
nullptr)
103 return BOTAN_FFI_VISIT(cipher, [=](
const auto& c) { *out_len = c.output_length(in_len); });
107 size_t* out_minimum_keylength,
108 size_t* out_maximum_keylength)
111 *out_minimum_keylength = c.key_spec().minimum_keylength();
112 *out_maximum_keylength = c.key_spec().maximum_keylength();
117 size_t* out_minimum_keylength,
118 size_t* out_maximum_keylength,
119 size_t* out_keylength_modulo)
122 if(out_minimum_keylength)
123 *out_minimum_keylength = c.key_spec().minimum_keylength();
124 if(out_maximum_keylength)
125 *out_maximum_keylength = c.key_spec().maximum_keylength();
126 if(out_keylength_modulo)
127 *out_keylength_modulo = c.key_spec().keylength_multiple();
132 const uint8_t* key,
size_t key_len)
134 return BOTAN_FFI_VISIT(cipher, [=](
auto& c) { c.set_key(key, key_len); });
138 const uint8_t* nonce,
size_t nonce_len)
142 cipher.
start(nonce, nonce_len);
149 uint8_t output_ptr[],
150 size_t orig_output_size,
151 size_t* output_written,
152 const uint8_t input_ptr[],
153 size_t orig_input_size,
154 size_t* input_consumed)
158 size_t input_size = orig_input_size;
159 size_t output_size = orig_output_size;
160 const uint8_t* input = input_ptr;
161 uint8_t* output = output_ptr;
163 using namespace Botan;
171 mbuf.assign(input, input + input_size);
172 *input_consumed = input_size;
184 *output_written = mbuf.size();
186 if(mbuf.size() <= output_size)
188 copy_mem(output, mbuf.data(), mbuf.size());
199 *output_written = mbuf.size();
200 if(output_size >= mbuf.size())
202 copy_mem(output, mbuf.data(), mbuf.size());
210 const size_t ud = cipher_obj->update_size();
213 size_t taken = 0, written = 0;
215 while(input_size >= ud && output_size >= ud)
218 copy_mem(mbuf.data(), input, ud);
222 copy_mem(output, mbuf.data(), ud);
231 *output_written = written;
232 *input_consumed = taken;
245 aead->set_associated_data(ad, ad_len);
255 return c.valid_nonce_length(nl) ? 1 : 0;
261 return BOTAN_FFI_VISIT(cipher, [=](
const auto& c) { *nl = c.default_nonce_length(); });
266 return BOTAN_FFI_VISIT(cipher, [=](
const auto& ) { *ug = cipher->update_size(); });
271 return BOTAN_FFI_VISIT(cipher, [=](
const auto& c) { *ug = c.ideal_granularity(); });
276 return BOTAN_FFI_VISIT(cipher, [=](
const auto& c) { *tl = c.tag_size(); });
#define BOTAN_ASSERT_NOMSG(expr)
static std::unique_ptr< Cipher_Mode > create(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
void start(std::span< const uint8_t > nonce)
void finish(secure_vector< uint8_t > &final_block, size_t offset=0)
void update(T &buffer, size_t offset=0)
virtual size_t minimum_final_size() const =0
virtual size_t update_granularity() const =0
int(* final)(unsigned char *, CTX *)
#define BOTAN_CIPHER_INIT_FLAG_ENCRYPT
#define BOTAN_CIPHER_UPDATE_FLAG_FINAL
#define BOTAN_CIPHER_INIT_FLAG_MASK_DIRECTION
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
@ BOTAN_FFI_ERROR_NULL_POINTER
@ BOTAN_FFI_ERROR_BAD_MAC
@ BOTAN_FFI_ERROR_BAD_PARAMETER
struct botan_cipher_struct * botan_cipher_t
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)
int botan_cipher_output_length(botan_cipher_t cipher, size_t in_len, size_t *out_len)
int botan_cipher_reset(botan_cipher_t cipher)
int botan_cipher_destroy(botan_cipher_t cipher)
int botan_cipher_name(botan_cipher_t cipher, char *name, size_t *name_len)
int botan_cipher_set_associated_data(botan_cipher_t cipher, const uint8_t *ad, size_t ad_len)
int botan_cipher_start(botan_cipher_t cipher_obj, const uint8_t *nonce, size_t nonce_len)
int botan_cipher_get_tag_length(botan_cipher_t cipher, size_t *tl)
int botan_cipher_get_keyspec(botan_cipher_t cipher, size_t *out_minimum_keylength, size_t *out_maximum_keylength, size_t *out_keylength_modulo)
int botan_cipher_set_key(botan_cipher_t cipher, const uint8_t *key, size_t key_len)
int botan_cipher_get_ideal_update_granularity(botan_cipher_t cipher, size_t *ug)
int botan_cipher_clear(botan_cipher_t cipher)
int botan_cipher_get_default_nonce_length(botan_cipher_t cipher, size_t *nl)
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_get_update_granularity(botan_cipher_t cipher, size_t *ug)
#define BOTAN_FFI_VISIT(obj, lambda)
#define BOTAN_FFI_CHECKED_DELETE(o)
int write_str_output(uint8_t out[], size_t *out_len, std::string_view str)
T & safe_get(botan_struct< T, M > *p)
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
std::vector< T, secure_allocator< T > > secure_vector