10#include <botan/internal/ffi_util.h>
16struct 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_buf.reserve(m_update_size);
25 size_t update_size()
const {
return m_update_size; }
48 if(minimum_final_size == 0 || update_granularity > minimum_final_size) {
50 return update_granularity;
53 size_t buf_size = std::max(update_granularity, minimum_final_size + 1);
54 if(buf_size % update_granularity != 0) {
55 buf_size += update_granularity - (buf_size % update_granularity);
73 const size_t update_size = ffi_choose_update_size(*mode);
75 *cipher =
new botan_cipher_struct(std::move(mode), update_size);
93 if(out_len ==
nullptr) {
97 return BOTAN_FFI_VISIT(cipher, [=](
const auto& c) { *out_len = c.output_length(in_len); });
102 *out_minimum_keylength = c.key_spec().minimum_keylength();
103 *out_maximum_keylength = c.key_spec().maximum_keylength();
108 size_t* out_minimum_keylength,
109 size_t* out_maximum_keylength,
110 size_t* out_keylength_modulo) {
112 if(out_minimum_keylength)
113 *out_minimum_keylength = c.key_spec().minimum_keylength();
114 if(out_maximum_keylength)
115 *out_maximum_keylength = c.key_spec().maximum_keylength();
116 if(out_keylength_modulo)
117 *out_keylength_modulo = c.key_spec().keylength_multiple();
122 return BOTAN_FFI_VISIT(cipher, [=](
auto& c) { c.set_key(key, key_len); });
128 cipher.
start(nonce, nonce_len);
135 uint8_t output_ptr[],
136 size_t orig_output_size,
137 size_t* output_written,
138 const uint8_t input_ptr[],
139 size_t orig_input_size,
140 size_t* input_consumed) {
142 size_t input_size = orig_input_size;
143 size_t output_size = orig_output_size;
144 const uint8_t* input = input_ptr;
145 uint8_t* output = output_ptr;
147 using namespace Botan;
154 mbuf.assign(input, input + input_size);
155 *input_consumed = input_size;
164 *output_written = mbuf.size();
166 if(mbuf.size() <= output_size) {
167 copy_mem(output, mbuf.data(), mbuf.size());
175 if(input_size == 0) {
177 *output_written = mbuf.size();
178 if(output_size >= mbuf.size()) {
179 copy_mem(output, mbuf.data(), mbuf.size());
187 const size_t ud = cipher_obj->update_size();
190 size_t taken = 0, written = 0;
192 while(input_size >= ud && output_size >= ud) {
194 copy_mem(mbuf.data(), input, ud);
198 copy_mem(output, mbuf.data(), ud);
207 *output_written = written;
208 *input_consumed = taken;
217 aead->set_associated_data(ad, ad_len);
225 return BOTAN_FFI_VISIT(cipher, [=](
const auto& c) {
return c.valid_nonce_length(nl) ? 1 : 0; });
229 return BOTAN_FFI_VISIT(cipher, [=](
const auto& c) { *nl = c.default_nonce_length(); });
233 return BOTAN_FFI_VISIT(cipher, [=](
const auto& ) { *ug = cipher->update_size(); });
237 return BOTAN_FFI_VISIT(cipher, [=](
const auto& c) { *ug = c.ideal_granularity(); });
241 return BOTAN_FFI_VISIT(cipher, [=](
const auto& c) { *tl = c.tag_size(); });
245 return BOTAN_FFI_VISIT(cipher, [=](
const auto& c) {
return c.authenticated() ? 1 : 0; });
#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_is_authenticated(botan_cipher_t cipher)
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