• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 21428849131

28 Jan 2026 07:14AM UTC coverage: 90.066% (-0.01%) from 90.077%
21428849131

Pull #5124

github

web-flow
Merge 6e78eba88 into 0d718b146
Pull Request #5124: Cooperative Cancellation in PasswordHash

102157 of 113424 relevant lines covered (90.07%)

11542296.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.04
/src/lib/ffi/ffi.cpp
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

9
#include <botan/base64.h>
10
#include <botan/hex.h>
11
#include <botan/mem_ops.h>
12
#include <botan/version.h>
13
#include <botan/internal/ct_utils.h>
14
#include <botan/internal/ffi_util.h>
15
#include <cstdio>
16
#include <cstdlib>
17

18
#if defined(BOTAN_HAS_OS_UTILS)
19
   #include <botan/internal/os_utils.h>
20
#endif
21

22
namespace Botan_FFI {
23

24
namespace {
25

26
// NOLINTNEXTLINE(*-avoid-non-const-global-variables)
27
thread_local std::string g_last_exception_what;
97,958✔
28

29
int ffi_map_error_type(Botan::ErrorType err) {
13✔
30
   switch(err) {
13✔
31
      case Botan::ErrorType::Unknown:
32
         return BOTAN_FFI_ERROR_UNKNOWN_ERROR;
33

34
      case Botan::ErrorType::SystemError:
×
35
      case Botan::ErrorType::IoError:
×
36
      case Botan::ErrorType::Pkcs11Error:
×
37
      case Botan::ErrorType::CommonCryptoError:
×
38
      case Botan::ErrorType::ZlibError:
×
39
      case Botan::ErrorType::Bzip2Error:
×
40
      case Botan::ErrorType::LzmaError:
×
41
      case Botan::ErrorType::DatabaseError:
×
42
         return BOTAN_FFI_ERROR_SYSTEM_ERROR;
×
43

44
      case Botan::ErrorType::TPMError:
×
45
         return BOTAN_FFI_ERROR_TPM_ERROR;
×
46

47
      case Botan::ErrorType::NotImplemented:
48
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
49
      case Botan::ErrorType::OutOfMemory:
×
50
         return BOTAN_FFI_ERROR_OUT_OF_MEMORY;
×
51
      case Botan::ErrorType::InternalError:
×
52
         return BOTAN_FFI_ERROR_INTERNAL_ERROR;
×
53
      case Botan::ErrorType::OperationCanceled:
4✔
54
      case Botan::ErrorType::InvalidObjectState:
4✔
55
         return BOTAN_FFI_ERROR_INVALID_OBJECT_STATE;
4✔
56
      case Botan::ErrorType::KeyNotSet:
2✔
57
         return BOTAN_FFI_ERROR_KEY_NOT_SET;
2✔
58
      case Botan::ErrorType::InvalidArgument:
4✔
59
      case Botan::ErrorType::InvalidNonceLength:
4✔
60
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
4✔
61

62
      case Botan::ErrorType::EncodingFailure:
1✔
63
      case Botan::ErrorType::DecodingFailure:
1✔
64
         return BOTAN_FFI_ERROR_INVALID_INPUT;
1✔
65

66
      case Botan::ErrorType::InvalidTag:
×
67
         return BOTAN_FFI_ERROR_BAD_MAC;
×
68

69
      case Botan::ErrorType::InvalidKeyLength:
×
70
         return BOTAN_FFI_ERROR_INVALID_KEY_LENGTH;
×
71
      case Botan::ErrorType::LookupError:
72
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
73

74
      case Botan::ErrorType::HttpError:
×
75
         return BOTAN_FFI_ERROR_HTTP_ERROR;
×
76
      case Botan::ErrorType::TLSError:
×
77
         return BOTAN_FFI_ERROR_TLS_ERROR;
×
78
      case Botan::ErrorType::RoughtimeError:
×
79
         return BOTAN_FFI_ERROR_ROUGHTIME_ERROR;
×
80
   }
81

82
   return BOTAN_FFI_ERROR_UNKNOWN_ERROR;
83
}
84

85
}  // namespace
86

87
void ffi_clear_last_exception() {
97,931✔
88
   g_last_exception_what.clear();
97,931✔
89
}
97,922✔
90

91
int ffi_error_exception_thrown(const char* func_name, const char* exn, int rc) {
20✔
92
   g_last_exception_what.assign(exn);
20✔
93

94
#if defined(BOTAN_HAS_OS_UTILS)
95
   std::string val;
20✔
96
   if(Botan::OS::read_env_variable(val, "BOTAN_FFI_PRINT_EXCEPTIONS") && !val.empty()) {
20✔
97
      // NOLINTNEXTLINE(*-vararg)
98
      static_cast<void>(std::fprintf(stderr, "in %s exception '%s' returning %d\n", func_name, exn, rc));
×
99
   }
100
#endif
101

102
   return rc;
20✔
103
}
20✔
104

105
int ffi_error_exception_thrown(const char* func_name, const char* exn, Botan::ErrorType err) {
13✔
106
   return ffi_error_exception_thrown(func_name, exn, ffi_map_error_type(err));
13✔
107
}
108

109
int botan_view_str_bounce_fn(botan_view_ctx vctx, const char* str, size_t len) {
74✔
110
   return botan_view_bin_bounce_fn(vctx, reinterpret_cast<const uint8_t*>(str), len);
74✔
111
}
112

113
int botan_view_bin_bounce_fn(botan_view_ctx vctx, const uint8_t* buf, size_t len) {
168✔
114
   if(vctx == nullptr || buf == nullptr) {
168✔
115
      return BOTAN_FFI_ERROR_NULL_POINTER;
116
   }
117

118
   const botan_view_bounce_struct* ctx = static_cast<botan_view_bounce_struct*>(vctx);
168✔
119

120
   const size_t avail = *ctx->out_len;
168✔
121
   *ctx->out_len = len;
168✔
122

123
   if(avail < len || ctx->out_ptr == nullptr) {
168✔
124
      if(ctx->out_ptr != nullptr) {
78✔
125
         Botan::clear_mem(ctx->out_ptr, avail);
×
126
      }
127
      return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE;
78✔
128
   } else {
129
      Botan::copy_mem(ctx->out_ptr, buf, len);
90✔
130
      return BOTAN_FFI_SUCCESS;
90✔
131
   }
132
}
133

134
}  // namespace Botan_FFI
135

136
extern "C" {
137

138
using namespace Botan_FFI;
139

140
const char* botan_error_last_exception_message() {
7✔
141
   return g_last_exception_what.c_str();
7✔
142
}
143

144
const char* botan_error_description(int err) {
158✔
145
   switch(err) {
158✔
146
      case BOTAN_FFI_SUCCESS:
147
         return "OK";
148

149
      case BOTAN_FFI_INVALID_VERIFIER:
1✔
150
         return "Invalid verifier";
1✔
151

152
      case BOTAN_FFI_ERROR_INVALID_INPUT:
2✔
153
         return "Invalid input";
2✔
154

155
      case BOTAN_FFI_ERROR_BAD_MAC:
1✔
156
         return "Invalid authentication code";
1✔
157

158
      case BOTAN_FFI_ERROR_NO_VALUE:
2✔
159
         return "No value available";
2✔
160

161
      case BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE:
1✔
162
         return "Insufficient buffer space";
1✔
163

164
      case BOTAN_FFI_ERROR_STRING_CONVERSION_ERROR:
1✔
165
         return "String conversion error";
1✔
166

167
      case BOTAN_FFI_ERROR_EXCEPTION_THROWN:
1✔
168
         return "Exception thrown";
1✔
169

170
      case BOTAN_FFI_ERROR_OUT_OF_MEMORY:
1✔
171
         return "Out of memory";
1✔
172

173
      case BOTAN_FFI_ERROR_SYSTEM_ERROR:
1✔
174
         return "Error while calling system API";
1✔
175

176
      case BOTAN_FFI_ERROR_INTERNAL_ERROR:
1✔
177
         return "Internal error";
1✔
178

179
      case BOTAN_FFI_ERROR_BAD_FLAG:
1✔
180
         return "Bad flag";
1✔
181

182
      case BOTAN_FFI_ERROR_NULL_POINTER:
1✔
183
         return "Null pointer argument";
1✔
184

185
      case BOTAN_FFI_ERROR_BAD_PARAMETER:
1✔
186
         return "Bad parameter";
1✔
187

188
      case BOTAN_FFI_ERROR_KEY_NOT_SET:
1✔
189
         return "Key not set on object";
1✔
190

191
      case BOTAN_FFI_ERROR_INVALID_KEY_LENGTH:
1✔
192
         return "Invalid key length";
1✔
193

194
      case BOTAN_FFI_ERROR_INVALID_OBJECT_STATE:
3✔
195
         return "Invalid object state";
3✔
196

197
      case BOTAN_FFI_ERROR_OUT_OF_RANGE:
1✔
198
         return "Index out of range";
1✔
199

200
      case BOTAN_FFI_ERROR_NOT_IMPLEMENTED:
5✔
201
         return "Not implemented";
5✔
202

203
      case BOTAN_FFI_ERROR_INVALID_OBJECT:
1✔
204
         return "Invalid object handle";
1✔
205

206
      case BOTAN_FFI_ERROR_TLS_ERROR:
1✔
207
         return "TLS error";
1✔
208

209
      case BOTAN_FFI_ERROR_HTTP_ERROR:
1✔
210
         return "HTTP error";
1✔
211

212
      case BOTAN_FFI_ERROR_UNKNOWN_ERROR:
128✔
213
      default:
128✔
214
         return "Unknown error";
128✔
215
   }
216
}
217

218
/*
219
* Versioning
220
*/
221
uint32_t botan_ffi_api_version() {
4✔
222
   return BOTAN_HAS_FFI;
4✔
223
}
224

225
int botan_ffi_supports_api(uint32_t api_version) {
7✔
226
   // This is the API introduced in 3.11
227
   if(api_version == 20260203) {
7✔
228
      return BOTAN_FFI_SUCCESS;
229
   }
230

231
   // This is the API introduced in 3.10
232
   if(api_version == 20250829) {
5✔
233
      return BOTAN_FFI_SUCCESS;
234
   }
235

236
   // This is the API introduced in 3.8
237
   if(api_version == 20250506) {
5✔
238
      return BOTAN_FFI_SUCCESS;
239
   }
240

241
   // This is the API introduced in 3.4
242
   if(api_version == 20240408) {
5✔
243
      return BOTAN_FFI_SUCCESS;
244
   }
245

246
   // This is the API introduced in 3.2
247
   if(api_version == 20231009) {
5✔
248
      return BOTAN_FFI_SUCCESS;
249
   }
250

251
   // This is the API introduced in 3.1
252
   if(api_version == 20230711) {
5✔
253
      return BOTAN_FFI_SUCCESS;
254
   }
255

256
   // This is the API introduced in 3.0
257
   if(api_version == 20230403) {
5✔
258
      return BOTAN_FFI_SUCCESS;
259
   }
260

261
   // This is the API introduced in 2.18
262
   if(api_version == 20210220) {
5✔
263
      return BOTAN_FFI_SUCCESS;
264
   }
265

266
   // This is the API introduced in 2.13
267
   if(api_version == 20191214) {
5✔
268
      return BOTAN_FFI_SUCCESS;
269
   }
270

271
   // This is the API introduced in 2.8
272
   if(api_version == 20180713) {
5✔
273
      return BOTAN_FFI_SUCCESS;
274
   }
275

276
   // This is the API introduced in 2.3
277
   if(api_version == 20170815) {
4✔
278
      return BOTAN_FFI_SUCCESS;
279
   }
280

281
   // This is the API introduced in 2.1
282
   if(api_version == 20170327) {
3✔
283
      return BOTAN_FFI_SUCCESS;
284
   }
285

286
   // This is the API introduced in 2.0
287
   if(api_version == 20150515) {
2✔
288
      return BOTAN_FFI_SUCCESS;
1✔
289
   }
290

291
   // Something else:
292
   return -1;
293
}
294

295
const char* botan_version_string() {
2✔
296
   return Botan::version_cstr();
2✔
297
}
298

299
uint32_t botan_version_major() {
2✔
300
   return Botan::version_major();
2✔
301
}
302

303
uint32_t botan_version_minor() {
2✔
304
   return Botan::version_minor();
2✔
305
}
306

307
uint32_t botan_version_patch() {
1✔
308
   return Botan::version_patch();
1✔
309
}
310

311
uint32_t botan_version_datestamp() {
1✔
312
   return Botan::version_datestamp();
1✔
313
}
314

315
int botan_constant_time_compare(const uint8_t* x, const uint8_t* y, size_t len) {
28✔
316
   auto same = Botan::CT::is_equal(x, y, len);
28✔
317
   // Return 0 if same or -1 otherwise
318
   return static_cast<int>(same.select(1, 0)) - 1;
28✔
319
}
320

321
int botan_same_mem(const uint8_t* x, const uint8_t* y, size_t len) {
×
322
   return botan_constant_time_compare(x, y, len);
×
323
}
324

325
int botan_scrub_mem(void* mem, size_t bytes) {
1✔
326
   Botan::secure_scrub_memory(mem, bytes);
1✔
327
   return BOTAN_FFI_SUCCESS;
1✔
328
}
329

330
int botan_hex_encode(const uint8_t* in, size_t len, char* out, uint32_t flags) {
3✔
331
   return ffi_guard_thunk(__func__, [=]() -> int {
3✔
332
      const bool uppercase = (flags & BOTAN_FFI_HEX_LOWER_CASE) == 0;
3✔
333
      Botan::hex_encode(out, in, len, uppercase);
3✔
334
      return BOTAN_FFI_SUCCESS;
335
   });
3✔
336
}
337

338
int botan_hex_decode(const char* hex_str, size_t in_len, uint8_t* out, size_t* out_len) {
2✔
339
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
340
      const std::vector<uint8_t> bin = Botan::hex_decode(hex_str, in_len);
2✔
341
      return Botan_FFI::write_vec_output(out, out_len, bin);
2✔
342
   });
2✔
343
}
344

345
int botan_base64_encode(const uint8_t* in, size_t len, char* out, size_t* out_len) {
2✔
346
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
347
      const std::string base64 = Botan::base64_encode(in, len);
2✔
348
      return Botan_FFI::write_str_output(out, out_len, base64);
2✔
349
   });
2✔
350
}
351

352
int botan_base64_decode(const char* base64_str, size_t in_len, uint8_t* out, size_t* out_len) {
2✔
353
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
354
      if(*out_len < Botan::base64_decode_max_output(in_len)) {
2✔
355
         *out_len = Botan::base64_decode_max_output(in_len);
1✔
356
         return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE;
1✔
357
      }
358

359
      *out_len = Botan::base64_decode(out, std::string(base64_str, in_len));
1✔
360
      return BOTAN_FFI_SUCCESS;
1✔
361
   });
2✔
362
}
363
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc