Botan  2.7.0
Crypto and TLS for C++11
ffi.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2015,2017 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6 
7 #include <botan/ffi.h>
8 #include <botan/internal/ffi_util.h>
9 #include <botan/version.h>
10 #include <botan/mem_ops.h>
11 #include <botan/hex.h>
12 #include <botan/base64.h>
13 #include <cstdio>
14 
15 namespace Botan_FFI {
16 
17 int ffi_error_exception_thrown(const char* func_name, const char* exn)
18  {
19  fprintf(stderr, "in %s exception %s\n", func_name, exn);
21  }
22 
23 }
24 
25 extern "C" {
26 
27 using namespace Botan_FFI;
28 
29 const char* botan_error_description(int err)
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  }
72 
73 /*
74 * Versioning
75 */
77  {
78  return BOTAN_HAS_FFI;
79  }
80 
81 int botan_ffi_supports_api(uint32_t api_version)
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  }
99 
100 const char* botan_version_string()
101  {
102  return Botan::version_cstr();
103  }
104 
109 
110 int botan_constant_time_compare(const uint8_t* x, const uint8_t* y, size_t len)
111  {
112  return Botan::constant_time_compare(x, y, len) ? 0 : -1;
113  }
114 
115 int botan_same_mem(const uint8_t* x, const uint8_t* y, size_t len)
116  {
117  return botan_constant_time_compare(x, y, len);
118  }
119 
120 int botan_scrub_mem(void* mem, size_t bytes)
121  {
122  Botan::secure_scrub_memory(mem, bytes);
123  return BOTAN_FFI_SUCCESS;
124  }
125 
126 int botan_hex_encode(const uint8_t* in, size_t len, char* out, uint32_t flags)
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  }
134 
135 int botan_hex_decode(const char* hex_str, size_t in_len, uint8_t* out, size_t* out_len)
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  }
142 
143 int botan_base64_encode(const uint8_t* in, size_t len, char* out, size_t* out_len)
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  }
150 
151 int botan_base64_decode(const char* base64_str, size_t in_len,
152  uint8_t* out, size_t* out_len)
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  }
165 
166 }
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14
int botan_hex_encode(const uint8_t *in, size_t len, char *out, uint32_t flags)
Definition: ffi.cpp:126
size_t base64_decode_max_output(size_t input_length)
Definition: base64.cpp:256
int ffi_error_exception_thrown(const char *func_name, const char *exn)
Definition: ffi.cpp:17
int ffi_guard_thunk(const char *func_name, Thunk thunk)
Definition: ffi_util.h:64
Flags flags(Flag flags)
Definition: p11.h:858
bool constant_time_compare(const uint8_t x[], const uint8_t y[], size_t len)
Definition: mem_ops.cpp:51
int write_vec_output(uint8_t out[], size_t *out_len, const std::vector< uint8_t, Alloc > &buf)
Definition: ffi_util.h:146
#define BOTAN_FFI_HEX_LOWER_CASE
Definition: ffi.h:146
int botan_base64_encode(const uint8_t *in, size_t len, char *out, size_t *out_len)
Definition: ffi.cpp:143
uint32_t botan_version_patch()
Definition: ffi.cpp:107
int botan_scrub_mem(void *mem, size_t bytes)
Definition: ffi.cpp:120
uint32_t version_major()
Definition: version.cpp:72
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
uint32_t botan_ffi_api_version()
Definition: ffi.cpp:76
uint32_t version_patch()
Definition: version.cpp:74
uint32_t botan_version_major()
Definition: ffi.cpp:105
int botan_same_mem(const uint8_t *x, const uint8_t *y, size_t len)
Definition: ffi.cpp:115
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
uint32_t version_minor()
Definition: version.cpp:73
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
const char * botan_error_description(int err)
Definition: ffi.cpp:29
uint32_t botan_version_minor()
Definition: ffi.cpp:106
uint32_t version_datestamp()
Definition: version.cpp:67
int botan_base64_decode(const char *base64_str, size_t in_len, uint8_t *out, size_t *out_len)
Definition: ffi.cpp:151
void secure_scrub_memory(void *ptr, size_t n)
Definition: os_utils.cpp:37
uint32_t botan_version_datestamp()
Definition: ffi.cpp:108
int botan_hex_decode(const char *hex_str, size_t in_len, uint8_t *out, size_t *out_len)
Definition: ffi.cpp:135
int botan_ffi_supports_api(uint32_t api_version)
Definition: ffi.cpp:81
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:143
int botan_constant_time_compare(const uint8_t *x, const uint8_t *y, size_t len)
Definition: ffi.cpp:110
const char * version_cstr()
Definition: version.cpp:29
const char * botan_version_string()
Definition: ffi.cpp:100