Botan 3.2.0
Crypto and TLS for C&
Namespaces | Functions | Variables
ffi.cpp File Reference
#include <botan/ffi.h>
#include <botan/base64.h>
#include <botan/hex.h>
#include <botan/mem_ops.h>
#include <botan/version.h>
#include <botan/internal/ffi_util.h>
#include <botan/internal/os_utils.h>
#include <cstdio>
#include <cstdlib>

Go to the source code of this file.

Namespaces

namespace  Botan_FFI
 

Functions

int botan_base64_decode (const char *base64_str, size_t in_len, uint8_t *out, size_t *out_len)
 
int botan_base64_encode (const uint8_t *in, size_t len, char *out, size_t *out_len)
 
int botan_constant_time_compare (const uint8_t *x, const uint8_t *y, size_t len)
 
const char * botan_error_description (int err)
 
const char * botan_error_last_exception_message ()
 
uint32_t botan_ffi_api_version ()
 
int botan_ffi_supports_api (uint32_t api_version)
 
int botan_hex_decode (const char *hex_str, size_t in_len, uint8_t *out, size_t *out_len)
 
int botan_hex_encode (const uint8_t *in, size_t len, char *out, uint32_t flags)
 
int botan_same_mem (const uint8_t *x, const uint8_t *y, size_t len)
 
int botan_scrub_mem (void *mem, size_t bytes)
 
uint32_t botan_version_datestamp ()
 
uint32_t botan_version_major ()
 
uint32_t botan_version_minor ()
 
uint32_t botan_version_patch ()
 
const char * botan_version_string ()
 
int Botan_FFI::botan_view_bin_bounce_fn (botan_view_ctx vctx, const uint8_t *buf, size_t len)
 
int Botan_FFI::botan_view_str_bounce_fn (botan_view_ctx vctx, const char *str, size_t len)
 
int Botan_FFI::ffi_error_exception_thrown (const char *func_name, const char *exn, int rc)
 
int Botan_FFI::ffi_guard_thunk (const char *func_name, const std::function< int()> &thunk)
 

Variables

thread_local std::string Botan_FFI::g_last_exception_what
 

Function Documentation

◆ botan_base64_decode()

int botan_base64_decode ( const char *  base64_str,
size_t  in_len,
uint8_t *  out,
size_t *  out_len 
)

Perform base64 decoding

Definition at line 327 of file ffi.cpp.

327 {
328 return ffi_guard_thunk(__func__, [=]() -> int {
329 if(*out_len < Botan::base64_decode_max_output(in_len)) {
330 *out_len = Botan::base64_decode_max_output(in_len);
332 }
333
334 *out_len = Botan::base64_decode(out, std::string(base64_str, in_len));
335 return BOTAN_FFI_SUCCESS;
336 });
337}
@ BOTAN_FFI_SUCCESS
Definition ffi.h:89
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
Definition ffi.h:95
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition ffi.cpp:115
size_t base64_decode(uint8_t out[], const char in[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
Definition base64.cpp:154
size_t base64_decode_max_output(size_t input_length)
Definition base64.cpp:186

References Botan::base64_decode(), Botan::base64_decode_max_output(), BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, BOTAN_FFI_SUCCESS, and Botan_FFI::ffi_guard_thunk().

◆ botan_base64_encode()

int botan_base64_encode ( const uint8_t *  x,
size_t  len,
char *  out,
size_t *  out_len 
)

Perform base64 encoding

Definition at line 320 of file ffi.cpp.

320 {
321 return ffi_guard_thunk(__func__, [=]() -> int {
322 const std::string base64 = Botan::base64_encode(in, len);
323 return Botan_FFI::write_str_output(out, out_len, base64);
324 });
325}
int write_str_output(uint8_t out[], size_t *out_len, std::string_view str)
Definition ffi_util.h:205
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition base64.cpp:146

References Botan::base64_encode(), Botan_FFI::ffi_guard_thunk(), and Botan_FFI::write_str_output().

◆ botan_constant_time_compare()

int botan_constant_time_compare ( const uint8_t *  x,
const uint8_t *  y,
size_t  len 
)

Returns 0 if x[0..len] == y[0..len], or otherwise -1

Definition at line 292 of file ffi.cpp.

292 {
293 return Botan::constant_time_compare(x, y, len) ? 0 : -1;
294}
bool constant_time_compare(const uint8_t x[], const uint8_t y[], size_t len)
Definition mem_ops.h:80

References Botan::constant_time_compare().

Referenced by botan_same_mem().

◆ botan_error_description()

const char * botan_error_description ( int  err)

Convert an error code into a string. Returns "Unknown error" if the error code is not a known one.

Definition at line 145 of file ffi.cpp.

145 {
146 switch(err) {
148 return "OK";
149
151 return "Invalid verifier";
152
154 return "Invalid input";
155
157 return "Invalid authentication code";
158
160 return "Insufficient buffer space";
161
163 return "String conversion error";
164
166 return "Exception thrown";
167
169 return "Out of memory";
170
172 return "Error while calling system API";
173
175 return "Internal error";
176
178 return "Bad flag";
179
181 return "Null pointer argument";
182
184 return "Bad parameter";
185
187 return "Key not set on object";
188
190 return "Invalid key length";
191
193 return "Invalid object state";
194
196 return "Not implemented";
197
199 return "Invalid object handle";
200
202 return "TLS error";
203
205 return "HTTP error";
206
208 return "Unknown error";
209
210 default:
211 return "Unknown error";
212 }
213}
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:110
@ BOTAN_FFI_ERROR_INVALID_KEY_LENGTH
Definition ffi.h:107
@ BOTAN_FFI_ERROR_KEY_NOT_SET
Definition ffi.h:106
@ BOTAN_FFI_ERROR_TLS_ERROR
Definition ffi.h:113
@ BOTAN_FFI_ERROR_EXCEPTION_THROWN
Definition ffi.h:98
@ BOTAN_FFI_ERROR_OUT_OF_MEMORY
Definition ffi.h:99
@ BOTAN_FFI_ERROR_INTERNAL_ERROR
Definition ffi.h:101
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:90
@ BOTAN_FFI_ERROR_INVALID_OBJECT
Definition ffi.h:111
@ BOTAN_FFI_ERROR_UNKNOWN_ERROR
Definition ffi.h:117
@ BOTAN_FFI_ERROR_HTTP_ERROR
Definition ffi.h:114
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:103
@ BOTAN_FFI_ERROR_INVALID_INPUT
Definition ffi.h:92
@ BOTAN_FFI_ERROR_STRING_CONVERSION_ERROR
Definition ffi.h:96
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:104
@ BOTAN_FFI_ERROR_SYSTEM_ERROR
Definition ffi.h:100
@ BOTAN_FFI_ERROR_INVALID_OBJECT_STATE
Definition ffi.h:108
@ BOTAN_FFI_ERROR_BAD_MAC
Definition ffi.h:93
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:105

References BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_FFI_ERROR_BAD_MAC, BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_EXCEPTION_THROWN, BOTAN_FFI_ERROR_HTTP_ERROR, BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, BOTAN_FFI_ERROR_INTERNAL_ERROR, BOTAN_FFI_ERROR_INVALID_INPUT, BOTAN_FFI_ERROR_INVALID_KEY_LENGTH, BOTAN_FFI_ERROR_INVALID_OBJECT, BOTAN_FFI_ERROR_INVALID_OBJECT_STATE, BOTAN_FFI_ERROR_KEY_NOT_SET, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_ERROR_OUT_OF_MEMORY, BOTAN_FFI_ERROR_STRING_CONVERSION_ERROR, BOTAN_FFI_ERROR_SYSTEM_ERROR, BOTAN_FFI_ERROR_TLS_ERROR, BOTAN_FFI_ERROR_UNKNOWN_ERROR, BOTAN_FFI_INVALID_VERIFIER, and BOTAN_FFI_SUCCESS.

◆ botan_error_last_exception_message()

const char * botan_error_last_exception_message ( void  )

Return the message of the last exception caught in this thread.

This pointer can/will be reallocated or overwritten the next time this thread calls any other Botan FFI function and must be copied to persistent storage first.

Definition at line 141 of file ffi.cpp.

141 {
142 return g_last_exception_what.c_str();
143}
thread_local std::string g_last_exception_what
Definition ffi.cpp:21

References Botan_FFI::g_last_exception_what.

◆ botan_ffi_api_version()

uint32_t botan_ffi_api_version ( void  )

Return the version of the currently supported FFI API. This is expressed in the form YYYYMMDD of the release date of this version of the API.

Definition at line 218 of file ffi.cpp.

218 {
219 return BOTAN_HAS_FFI;
220}
#define BOTAN_HAS_FFI
Definition build.h:215

References BOTAN_HAS_FFI.

◆ botan_ffi_supports_api()

int botan_ffi_supports_api ( uint32_t  api_version)

Return 0 (ok) if the version given is one this library supports. botan_ffi_supports_api(botan_ffi_api_version()) will always return 0.

Definition at line 222 of file ffi.cpp.

222 {
223 // This is the API introduced in 3.2
224 if(api_version == 20231009) {
225 return BOTAN_FFI_SUCCESS;
226 }
227
228 // This is the API introduced in 3.1
229 if(api_version == 20230711) {
230 return BOTAN_FFI_SUCCESS;
231 }
232
233 // This is the API introduced in 3.0
234 if(api_version == 20230403) {
235 return BOTAN_FFI_SUCCESS;
236 }
237
238 // This is the API introduced in 2.18
239 if(api_version == 20210220) {
240 return BOTAN_FFI_SUCCESS;
241 }
242
243 // This is the API introduced in 2.13
244 if(api_version == 20191214) {
245 return BOTAN_FFI_SUCCESS;
246 }
247
248 // This is the API introduced in 2.8
249 if(api_version == 20180713) {
250 return BOTAN_FFI_SUCCESS;
251 }
252
253 // This is the API introduced in 2.3
254 if(api_version == 20170815) {
255 return BOTAN_FFI_SUCCESS;
256 }
257
258 // This is the API introduced in 2.1
259 if(api_version == 20170327) {
260 return BOTAN_FFI_SUCCESS;
261 }
262
263 // This is the API introduced in 2.0
264 if(api_version == 20150515) {
265 return BOTAN_FFI_SUCCESS;
266 }
267
268 // Something else:
269 return -1;
270}

References BOTAN_FFI_SUCCESS.

◆ botan_hex_decode()

int botan_hex_decode ( const char *  hex_str,
size_t  in_len,
uint8_t *  out,
size_t *  out_len 
)

Perform hex decoding

Parameters
hex_stra string of hex chars (whitespace is ignored)
in_lenthe length of hex_str
outthe output buffer should be at least strlen(hex_str)/2 bytes
out_lenthe size of the output buffer on input, set to the number of bytes written
Returns
0 on success, a negative value on failure

Definition at line 313 of file ffi.cpp.

313 {
314 return ffi_guard_thunk(__func__, [=]() -> int {
315 const std::vector<uint8_t> bin = Botan::hex_decode(hex_str, in_len);
316 return Botan_FFI::write_vec_output(out, out_len, bin);
317 });
318}
int write_vec_output(uint8_t out[], size_t *out_len, const std::vector< uint8_t, Alloc > &buf)
Definition ffi_util.h:201
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition hex.cpp:81

References Botan_FFI::ffi_guard_thunk(), Botan::hex_decode(), and Botan_FFI::write_vec_output().

◆ botan_hex_encode()

int botan_hex_encode ( const uint8_t *  x,
size_t  len,
char *  out,
uint32_t  flags 
)

Perform hex encoding

Parameters
xis some binary data
lenlength of x in bytes
outan array of at least x*2 bytes
flagsflags out be upper or lower case?
Returns
0 on success, a negative value on failure

Definition at line 305 of file ffi.cpp.

305 {
306 return ffi_guard_thunk(__func__, [=]() -> int {
307 const bool uppercase = (flags & BOTAN_FFI_HEX_LOWER_CASE) == 0;
308 Botan::hex_encode(out, in, len, uppercase);
309 return BOTAN_FFI_SUCCESS;
310 });
311}
#define BOTAN_FFI_HEX_LOWER_CASE
Definition ffi.h:211
Flags flags(Flag flags)
Definition p11.h:836
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition hex.cpp:33

References BOTAN_FFI_HEX_LOWER_CASE, BOTAN_FFI_SUCCESS, Botan_FFI::ffi_guard_thunk(), and Botan::hex_encode().

◆ botan_same_mem()

int botan_same_mem ( const uint8_t *  x,
const uint8_t *  y,
size_t  len 
)

Deprecated equivalent to botan_constant_time_compare

Definition at line 296 of file ffi.cpp.

296 {
297 return botan_constant_time_compare(x, y, len);
298}
int botan_constant_time_compare(const uint8_t *x, const uint8_t *y, size_t len)
Definition ffi.cpp:292

References botan_constant_time_compare().

◆ botan_scrub_mem()

int botan_scrub_mem ( void *  mem,
size_t  bytes 
)

Clear out memory using a system specific approach to bypass elision by the compiler (currently using RtlSecureZeroMemory or tricks with volatile pointers).

Definition at line 300 of file ffi.cpp.

300 {
301 Botan::secure_scrub_memory(mem, bytes);
302 return BOTAN_FFI_SUCCESS;
303}
void secure_scrub_memory(void *ptr, size_t n)
Definition os_utils.cpp:83

References BOTAN_FFI_SUCCESS, and Botan::secure_scrub_memory().

◆ botan_version_datestamp()

uint32_t botan_version_datestamp ( void  )

Return the date this version was released as an integer, or 0 if an unreleased version

Definition at line 288 of file ffi.cpp.

288 {
290}
uint32_t version_datestamp()
Definition version.cpp:68

References Botan::version_datestamp().

◆ botan_version_major()

uint32_t botan_version_major ( void  )

Return the major version of the library

Definition at line 276 of file ffi.cpp.

276 {
277 return Botan::version_major();
278}
uint32_t version_major()
Definition version.cpp:75

References Botan::version_major().

◆ botan_version_minor()

uint32_t botan_version_minor ( void  )

Return the minor version of the library

Definition at line 280 of file ffi.cpp.

280 {
281 return Botan::version_minor();
282}
uint32_t version_minor()
Definition version.cpp:79

References Botan::version_minor().

◆ botan_version_patch()

uint32_t botan_version_patch ( void  )

Return the patch version of the library

Definition at line 284 of file ffi.cpp.

284 {
285 return Botan::version_patch();
286}
uint32_t version_patch()
Definition version.cpp:83

References Botan::version_patch().

◆ botan_version_string()

const char * botan_version_string ( void  )

Return a free-form version string, e.g., 2.0.0

Definition at line 272 of file ffi.cpp.

272 {
273 return Botan::version_cstr();
274}
const char * version_cstr()
Definition version.cpp:33

References Botan::version_cstr().