Botan 3.4.0
Crypto and TLS for C&
Functions
ffi_cipher.cpp File Reference
#include <botan/ffi.h>
#include <botan/aead.h>
#include <botan/internal/ffi_util.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_ideal_update_granularity (botan_cipher_t cipher, size_t *ug)
 
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_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_is_authenticated (botan_cipher_t cipher)
 
int botan_cipher_name (botan_cipher_t cipher, char *name, size_t *name_len)
 
int botan_cipher_output_length (botan_cipher_t cipher, size_t in_len, size_t *out_len)
 
int botan_cipher_query_keylen (botan_cipher_t cipher, size_t *out_minimum_keylength, size_t *out_maximum_keylength)
 
int botan_cipher_requires_entire_message (botan_cipher_t cipher)
 
int botan_cipher_reset (botan_cipher_t cipher)
 
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 hash)

Reset the key, nonce, AD and all other state on this cipher object

Definition at line 84 of file ffi_cipher.cpp.

84 {
85 return BOTAN_FFI_VISIT(cipher, [](auto& c) { c.clear(); });
86}
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:124

References BOTAN_FFI_VISIT.

◆ botan_cipher_destroy()

int botan_cipher_destroy ( botan_cipher_t cipher)

Destroy the cipher object

Returns
0 if success, error if invalid object handle

Definition at line 80 of file ffi_cipher.cpp.

80 {
81 return BOTAN_FFI_CHECKED_DELETE(cipher);
82}
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:143

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_cipher_get_default_nonce_length()

int botan_cipher_get_default_nonce_length ( botan_cipher_t cipher,
size_t * nl )

Get the default nonce length of this cipher

Definition at line 229 of file ffi_cipher.cpp.

229 {
230 return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { *nl = c.default_nonce_length(); });
231}

References BOTAN_FFI_VISIT.

◆ botan_cipher_get_ideal_update_granularity()

int botan_cipher_get_ideal_update_granularity ( botan_cipher_t cipher,
size_t * ug )

Return the ideal update granularity of the cipher. This is some multiple of the update granularity, reflecting possibilities for optimization.

Definition at line 237 of file ffi_cipher.cpp.

237 {
238 return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { *ug = c.ideal_granularity(); });
239}

References BOTAN_FFI_VISIT.

◆ botan_cipher_get_keyspec()

int botan_cipher_get_keyspec ( botan_cipher_t cipher,
size_t * min_keylen,
size_t * max_keylen,
size_t * mod_keylen )

Get information about the supported key lengths.

Definition at line 107 of file ffi_cipher.cpp.

110 {
111 return BOTAN_FFI_VISIT(cipher, [=](const auto& c) {
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();
118 });
119}

References BOTAN_FFI_VISIT.

◆ botan_cipher_get_tag_length()

int botan_cipher_get_tag_length ( botan_cipher_t cipher,
size_t * tag_size )

Get the tag length of the cipher (0 for non-AEAD modes)

Definition at line 241 of file ffi_cipher.cpp.

241 {
242 return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { *tl = c.tag_size(); });
243}

References BOTAN_FFI_VISIT.

◆ botan_cipher_get_update_granularity()

int botan_cipher_get_update_granularity ( botan_cipher_t cipher,
size_t * ug )

Return the update granularity of the cipher; botan_cipher_update must be called with blocks of this size, except for the final.

Definition at line 233 of file ffi_cipher.cpp.

233 {
234 return BOTAN_FFI_VISIT(cipher, [=](const auto& /*c*/) { *ug = cipher->update_size(); });
235}

References BOTAN_FFI_VISIT.

◆ botan_cipher_init()

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

Initialize a cipher object

Definition at line 63 of file ffi_cipher.cpp.

63 {
64 return ffi_guard_thunk(__func__, [=]() -> int {
67
68 std::unique_ptr<Botan::Cipher_Mode> mode(Botan::Cipher_Mode::create(cipher_name, dir));
69 if(!mode) {
71 }
72
73 const size_t update_size = ffi_choose_update_size(*mode);
74
75 *cipher = new botan_cipher_struct(std::move(mode), update_size);
76 return BOTAN_FFI_SUCCESS;
77 });
78}
static std::unique_ptr< Cipher_Mode > create(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
#define BOTAN_CIPHER_INIT_FLAG_ENCRYPT
Definition ffi.h:527
#define BOTAN_CIPHER_INIT_FLAG_MASK_DIRECTION
Definition ffi.h:526
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:124
@ BOTAN_FFI_SUCCESS
Definition ffi.h:103
Flags flags(Flag flags)
Definition p11.h:836
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition ffi.cpp:116

References BOTAN_CIPHER_INIT_FLAG_ENCRYPT, BOTAN_CIPHER_INIT_FLAG_MASK_DIRECTION, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_SUCCESS, Botan::Cipher_Mode::create(), Botan::Decryption, Botan::Encryption, and Botan_FFI::ffi_guard_thunk().

◆ botan_cipher_is_authenticated()

int botan_cipher_is_authenticated ( botan_cipher_t cipher)

Returns 1 iff the cipher provides authentication as well as confidentiality.

Definition at line 245 of file ffi_cipher.cpp.

245 {
246 return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { return c.authenticated() ? 1 : 0; });
247}

References BOTAN_FFI_VISIT.

◆ botan_cipher_name()

int botan_cipher_name ( botan_cipher_t cipher,
char * name,
size_t * name_len )

Return the name of the cipher object

Definition at line 253 of file ffi_cipher.cpp.

253 {
254 return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { return write_str_output(name, name_len, c.name()); });
255}
std::string name
int write_str_output(uint8_t out[], size_t *out_len, std::string_view str)
Definition ffi_util.h:205

References BOTAN_FFI_VISIT, name, and Botan_FFI::write_str_output().

◆ botan_cipher_output_length()

int botan_cipher_output_length ( botan_cipher_t cipher,
size_t in_len,
size_t * out_len )

Return the output length of this cipher, for a particular input length.

Definition at line 92 of file ffi_cipher.cpp.

92 {
93 if(out_len == nullptr) {
95 }
96
97 return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { *out_len = c.output_length(in_len); });
98}
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:118

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.

◆ botan_cipher_query_keylen()

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

Get information about the key lengths. Prefer botan_cipher_get_keyspec

Definition at line 100 of file ffi_cipher.cpp.

100 {
101 return BOTAN_FFI_VISIT(cipher, [=](const auto& c) {
102 *out_minimum_keylength = c.key_spec().minimum_keylength();
103 *out_maximum_keylength = c.key_spec().maximum_keylength();
104 });
105}

References BOTAN_FFI_VISIT.

◆ botan_cipher_requires_entire_message()

int botan_cipher_requires_entire_message ( botan_cipher_t cipher)

Returns 1 iff the cipher requires the entire message before any encryption or decryption can be performed. No output data will be produced in botan_cipher_update() until the final flag is set.

Definition at line 249 of file ffi_cipher.cpp.

249 {
250 return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { return c.requires_entire_message() ? 1 : 0; });
251}

References BOTAN_FFI_VISIT.

◆ botan_cipher_reset()

int botan_cipher_reset ( botan_cipher_t cipher)

Reset the message specific state for this cipher. Without resetting the keys, this resets the nonce, and any state associated with any message bits that have been processed so far.

It is conceptually equivalent to calling botan_cipher_clear followed by botan_cipher_set_key with the original key.

Definition at line 88 of file ffi_cipher.cpp.

88 {
89 return BOTAN_FFI_VISIT(cipher, [](auto& c) { c.reset(); });
90}

References BOTAN_FFI_VISIT.

◆ botan_cipher_set_associated_data()

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

Set the associated data. Will fail if cipher is not an AEAD

Definition at line 215 of file ffi_cipher.cpp.

215 {
216 return BOTAN_FFI_VISIT(cipher, [=](auto& c) {
217 if(Botan::AEAD_Mode* aead = dynamic_cast<Botan::AEAD_Mode*>(&c)) {
218 aead->set_associated_data(ad, ad_len);
219 return BOTAN_FFI_SUCCESS;
220 }
222 });
223}
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:119

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_SUCCESS, and BOTAN_FFI_VISIT.

◆ botan_cipher_set_key()

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

Set the key for this cipher object

Definition at line 121 of file ffi_cipher.cpp.

121 {
122 return BOTAN_FFI_VISIT(cipher, [=](auto& c) { c.set_key(key, key_len); });
123}

References BOTAN_FFI_VISIT.

◆ botan_cipher_start()

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

Begin processing a new message using the provided nonce

Definition at line 125 of file ffi_cipher.cpp.

125 {
126 return ffi_guard_thunk(__func__, [=]() -> int {
127 Botan::Cipher_Mode& cipher = safe_get(cipher_obj);
128 cipher.start(nonce, nonce_len);
129 return BOTAN_FFI_SUCCESS;
130 });
131}
void start(std::span< const uint8_t > nonce)
Definition cipher_mode.h:89
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:63

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

◆ botan_cipher_update()

int botan_cipher_update ( botan_cipher_t cipher,
uint32_t flags,
uint8_t output[],
size_t output_size,
size_t * output_written,
const uint8_t input_bytes[],
size_t input_size,
size_t * input_consumed )

Encrypt some data

Definition at line 133 of file ffi_cipher.cpp.

140 {
141 return ffi_guard_thunk(__func__, [=]() -> int {
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;
146
147 using namespace Botan;
148 Cipher_Mode& cipher = safe_get(cipher_obj);
149 secure_vector<uint8_t>& mbuf = cipher_obj->buf();
150
151 const bool final_input = (flags & BOTAN_CIPHER_UPDATE_FLAG_FINAL);
152
153 if(final_input) {
154 mbuf.assign(input, input + input_size);
155 *input_consumed = input_size;
156 *output_written = 0;
157
158 try {
159 cipher.finish(mbuf);
162 }
163
164 *output_written = mbuf.size();
165
166 if(mbuf.size() <= output_size) {
167 copy_mem(output, mbuf.data(), mbuf.size());
168 mbuf.clear();
169 return BOTAN_FFI_SUCCESS;
170 }
171
172 return -1;
173 }
174
175 if(input_size == 0) {
176 // Currently must take entire buffer in this case
177 *output_written = mbuf.size();
178 if(output_size >= mbuf.size()) {
179 copy_mem(output, mbuf.data(), mbuf.size());
180 mbuf.clear();
181 return BOTAN_FFI_SUCCESS;
182 }
183
184 return -1;
185 }
186
187 const size_t ud = cipher_obj->update_size();
188
189 mbuf.resize(ud);
190 size_t taken = 0, written = 0;
191
192 while(input_size >= ud && output_size >= ud) {
193 copy_mem(mbuf.data(), input, ud);
194 const size_t bytes_produced = cipher.process(mbuf);
195
196 input_size -= ud;
197 input += ud;
198 taken += ud;
199
200 if(bytes_produced > 0) {
201 copy_mem(output, mbuf.data(), bytes_produced);
202 output_size -= bytes_produced;
203 output += bytes_produced;
204 written += bytes_produced;
205 }
206 }
207
208 *output_written = written;
209 *input_consumed = taken;
210
211 return BOTAN_FFI_SUCCESS;
212 });
213}
void finish(secure_vector< uint8_t > &final_block, size_t offset=0)
size_t process(std::span< uint8_t > msg)
#define BOTAN_CIPHER_UPDATE_FLAG_FINAL
Definition ffi.h:621
@ BOTAN_FFI_ERROR_BAD_MAC
Definition ffi.h:107
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:146

References BOTAN_CIPHER_UPDATE_FLAG_FINAL, BOTAN_FFI_ERROR_BAD_MAC, BOTAN_FFI_SUCCESS, Botan_FFI::ffi_guard_thunk(), Botan::Cipher_Mode::finish(), Botan::Cipher_Mode::process(), and Botan_FFI::safe_get().

◆ botan_cipher_valid_nonce_length()

int botan_cipher_valid_nonce_length ( botan_cipher_t cipher,
size_t nl )

Return if the specified nonce length is valid for this cipher

Definition at line 225 of file ffi_cipher.cpp.

225 {
226 return BOTAN_FFI_VISIT(cipher, [=](const auto& c) { return c.valid_nonce_length(nl) ? 1 : 0; });
227}

References BOTAN_FFI_VISIT.