Botan  2.7.0
Crypto and TLS for C++11
Namespaces | Functions
ffi.cpp File Reference
#include <botan/ffi.h>
#include <botan/internal/ffi_util.h>
#include <botan/version.h>
#include <botan/mem_ops.h>
#include <botan/hex.h>
#include <botan/base64.h>
#include <cstdio>

Go to the source code of this file.

Namespaces

 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)
 
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::ffi_error_exception_thrown (const char *func_name, const char *exn)
 

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 151 of file ffi.cpp.

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

153  {
154  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
155  if(*out_len < Botan::base64_decode_max_output(in_len))
156  {
157  *out_len = Botan::base64_decode_max_output(in_len);
159  }
160 
161  *out_len = Botan::base64_decode(out, std::string(base64_str, in_len));
162  return BOTAN_FFI_SUCCESS;
163  });
164  }
size_t base64_decode_max_output(size_t input_length)
Definition: base64.cpp:256
int ffi_guard_thunk(const char *func_name, Thunk thunk)
Definition: ffi_util.h:64
size_t base64_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool final_inputs, bool ignore_ws)
Definition: base64.cpp:100
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:143

◆ 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 143 of file ffi.cpp.

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

144  {
145  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
146  const std::string base64 = Botan::base64_encode(in, len);
147  return Botan_FFI::write_str_output(out, out_len, base64);
148  });
149  }
int ffi_guard_thunk(const char *func_name, Thunk thunk)
Definition: ffi_util.h:64
int write_str_output(uint8_t out[], size_t *out_len, const std::string &str)
Definition: ffi_util.h:151
size_t base64_encode(char out[], const uint8_t in[], size_t input_length, size_t &input_consumed, bool final_inputs)
Definition: base64.cpp:35
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:143

◆ 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 110 of file ffi.cpp.

References Botan::constant_time_compare().

Referenced by botan_same_mem().

111  {
112  return Botan::constant_time_compare(x, y, len) ? 0 : -1;
113  }
bool constant_time_compare(const uint8_t x[], const uint8_t y[], size_t len)
Definition: mem_ops.cpp:51

◆ 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 29 of file ffi.cpp.

References BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_FFI_ERROR_BAD_MAC, BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_EXCEPTION_THROWN, BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, BOTAN_FFI_ERROR_INVALID_INPUT, BOTAN_FFI_ERROR_INVALID_OBJECT, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_ERROR_UNKNOWN_ERROR, BOTAN_FFI_INVALID_VERIFIER, and BOTAN_FFI_SUCCESS.

30  {
31  switch(err)
32  {
33  case BOTAN_FFI_SUCCESS:
34  return "OK";
35 
37  return "Invalid verifier";
38 
40  return "Invalid input";
41 
43  return "Invalid authentication code";
44 
46  return "Insufficient buffer space";
47 
49  return "Exception thrown";
50 
52  return "Bad flag";
53 
55  return "Null pointer argument";
56 
58  return "Bad parameter";
59 
61  return "Not implemented";
62 
64  return "Invalid object handle";
65 
67  return "Unknown error";
68  }
69 
70  return "Unknown error";
71  }

◆ botan_ffi_api_version()

uint32_t botan_ffi_api_version ( )

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 76 of file ffi.cpp.

77  {
78  return BOTAN_HAS_FFI;
79  }

◆ 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 81 of file ffi.cpp.

References BOTAN_FFI_SUCCESS.

82  {
83  // Current API version
84  if(api_version == BOTAN_HAS_FFI)
85  return BOTAN_FFI_SUCCESS;
86 
87  // Older versions that are still supported
88 
89  // This is the 2.1/2.2 API
90  if(api_version == 20170327)
91  return BOTAN_FFI_SUCCESS;
92 
93  // This is the 2.0 API
94  if(api_version == 20150515)
95  return BOTAN_FFI_SUCCESS;
96 
97  return -1;
98  }

◆ 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 out

Definition at line 135 of file ffi.cpp.

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

136  {
137  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
138  const std::vector<uint8_t> bin = Botan::hex_decode(hex_str, in_len);
139  return Botan_FFI::write_vec_output(out, out_len, bin);
140  });
141  }
int ffi_guard_thunk(const char *func_name, Thunk thunk)
Definition: ffi_util.h:64
int write_vec_output(uint8_t out[], size_t *out_len, const std::vector< uint8_t, Alloc > &buf)
Definition: ffi_util.h:146
size_t hex_decode(uint8_t output[], const char input[], size_t input_length, size_t &input_consumed, bool ignore_ws)
Definition: hex.cpp:49
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:143

◆ 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, 1 on failure

Definition at line 126 of file ffi.cpp.

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

127  {
128  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
129  const bool uppercase = (flags & BOTAN_FFI_HEX_LOWER_CASE) == 0;
130  Botan::hex_encode(out, in, len, uppercase);
131  return BOTAN_FFI_SUCCESS;
132  });
133  }
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14
int ffi_guard_thunk(const char *func_name, Thunk thunk)
Definition: ffi_util.h:64
Flags flags(Flag flags)
Definition: p11.h:858
#define BOTAN_FFI_HEX_LOWER_CASE
Definition: ffi.h:146
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:143

◆ 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 115 of file ffi.cpp.

References botan_constant_time_compare().

116  {
117  return botan_constant_time_compare(x, y, len);
118  }
int botan_constant_time_compare(const uint8_t *x, const uint8_t *y, size_t len)
Definition: ffi.cpp:110

◆ 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 120 of file ffi.cpp.

References BOTAN_FFI_SUCCESS, and Botan::secure_scrub_memory().

121  {
122  Botan::secure_scrub_memory(mem, bytes);
123  return BOTAN_FFI_SUCCESS;
124  }
void secure_scrub_memory(void *ptr, size_t n)
Definition: os_utils.cpp:37

◆ botan_version_datestamp()

uint32_t botan_version_datestamp ( )

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

Definition at line 108 of file ffi.cpp.

References Botan::version_datestamp().

108 { return Botan::version_datestamp(); }
uint32_t version_datestamp()
Definition: version.cpp:67

◆ botan_version_major()

uint32_t botan_version_major ( )

Return the major version of the library

Definition at line 105 of file ffi.cpp.

References Botan::version_major().

105 { return Botan::version_major(); }
uint32_t version_major()
Definition: version.cpp:72

◆ botan_version_minor()

uint32_t botan_version_minor ( )

Return the minor version of the library

Definition at line 106 of file ffi.cpp.

References Botan::version_minor().

106 { return Botan::version_minor(); }
uint32_t version_minor()
Definition: version.cpp:73

◆ botan_version_patch()

uint32_t botan_version_patch ( )

Return the patch version of the library

Definition at line 107 of file ffi.cpp.

References Botan::version_patch().

107 { return Botan::version_patch(); }
uint32_t version_patch()
Definition: version.cpp:74

◆ botan_version_string()

const char* botan_version_string ( )

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

Definition at line 100 of file ffi.cpp.

References Botan::version_cstr().

101  {
102  return Botan::version_cstr();
103  }
const char * version_cstr()
Definition: version.cpp:29