Botan  2.4.0
Crypto and TLS for C++11
ffi.h
Go to the documentation of this file.
1 /*
2 * FFI (C89 API)
3 * (C) 2015,2017 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #ifndef BOTAN_FFI_H_
9 #define BOTAN_FFI_H_
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 /*
16 This header exports some of botan's functionality via a C89
17 interface. This API is uesd by the Python and OCaml bindings via those
18 languages respective ctypes libraries.
19 
20 The API is intended to be as easy as possible to call from other
21 languages, which often have easy ways to call C, because C. But some C
22 code is easier to deal with than others, so to make things easy this
23 API follows a few simple rules:
24 
25 - All interactions are via pointers to opaque structs. No need to worry about
26  structure padding issues and the like.
27 
28 - All functions return an int error code (except the version calls, which are
29  assumed to always have something to say).
30 
31 - Use simple types: size_t for lengths, const char* NULL terminated strings,
32  uint8_t for binary.
33 
34 - No ownership of memory transfers across the API boundary. The API will
35  consume data from const pointers, and will produce output by writing to
36  variables provided by the caller.
37 
38 - If exporting a value (a string or a blob) the function takes a pointer to the
39  output array and a read/write pointer to the length. If the length is insufficient, an
40  error is returned. So passing nullptr/0 allows querying the final value.
41 
42  Note this does not apply to all functions, like `botan_hash_final`
43  which is not idempotent and are documented specially. But it's a
44  general theory of operation.
45 
46 The API is not currently documented, nor should it be considered
47 stable. It is buggy as heck, most likely, and error handling is a
48 mess. However the goal is to provide a long term API usable for
49 language bindings, or for use by systems written in C. Suggestions on
50 how to provide the cleanest API for such users would be most welcome.
51 
52 * TODO:
53 * - Better error reporting
54 * - User callback for exception logging?
55 * - Doxygen comments for all functions/params
56 * - X.509 certs and PKIX path validation goo
57 * - TLS
58 */
59 
60 #include <botan/build.h>
61 #include <stdint.h>
62 #include <stddef.h>
63 
64 /**
65 * Return the version of the currently supported FFI API. This is
66 * expressed in the form YYYYMMDD of the release date of this version
67 * of the API.
68 */
70 
71 /**
72 * Return 0 (ok) if the version given is one this library supports.
73 * botan_ffi_supports_api(botan_ffi_api_version()) will always return 0.
74 */
75 BOTAN_PUBLIC_API(2,0) int botan_ffi_supports_api(uint32_t api_version);
76 
77 /**
78 * Return a free-form version string, e.g., 2.0.0
79 */
80 BOTAN_PUBLIC_API(2,0) const char* botan_version_string();
81 
82 /**
83 * Return the major version of the library
84 */
86 
87 /**
88 * Return the minor version of the library
89 */
91 
92 /**
93 * Return the patch version of the library
94 */
96 
97 /**
98 * Return the date this version was released as
99 * an integer, or 0 if an unreleased version
100 */
102 
103 /*
104 * Error handling
105 *
106 * Some way of exporting these values to other languages would be useful
107 
108 
109  THIS FUNCTION ASSUMES BOTH ARGUMENTS ARE LITERAL STRINGS
110  so it retains only the pointers and does not make a copy.
111 
112 int botan_make_error(const char* msg, const char* func, int line);
113 * This value is returned to callers ^^
114 
115  normally called like
116  return botan_make_error(BOTAN_ERROR_STRING_NOT_IMPLEMENTED, BOTAN_FUNCTION, __LINE__);
117 
118 // This would seem to require both saving the message permanently
119 catch(std::exception& e) {
120 return botan_make_error_from_transient_string(e.what(), BOTAN_FUNCTION, __LINE__);
121 }
122 
123 #define botan_make_error_inf(s) return botan_make_error(s, BOTAN_FUNCTION, __LINE__);
124 
125 Easier to return a const char* from each function directly? However,
126 
127 catch(std::exception& e) { return e.what(); }
128 
129 doesn't exactly work well either!
130 
131 *
132 * Later call:
133 * const char* botan_get_error_str(int);
134 * To recover the msg, func, and line
135 
136 */
137 #define BOTAN_FFI_SUCCESS (0)
138 
139 #define BOTAN_FFI_INVALID_VERIFIER (1)
140 
141 #define BOTAN_FFI_ERROR_INVALID_INPUT (-1)
142 #define BOTAN_FFI_ERROR_BAD_MAC (-2)
143 
144 #define BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE (-10)
145 #define BOTAN_FFI_ERROR_EXCEPTION_THROWN (-20)
146 #define BOTAN_FFI_ERROR_BAD_FLAG (-30)
147 #define BOTAN_FFI_ERROR_NULL_POINTER (-31)
148 #define BOTAN_FFI_ERROR_BAD_PARAMETER (-32)
149 #define BOTAN_FFI_ERROR_NOT_IMPLEMENTED (-40)
150 #define BOTAN_FFI_ERROR_INVALID_OBJECT (-50)
151 
152 #define BOTAN_FFI_ERROR_UNKNOWN_ERROR (-100)
153 
154 //const char* botan_error_description(int err);
155 
156 /**
157 * Returns 0 if x[0..len] == y[0..len], or otherwise -1
158 */
159 BOTAN_PUBLIC_API(2,3) int botan_constant_time_compare(const uint8_t* x, const uint8_t* y, size_t len);
160 
161 /**
162 * Deprecated equivalent to botan_constant_time_compare
163 */
164 BOTAN_PUBLIC_API(2,0) int botan_same_mem(const uint8_t* x, const uint8_t* y, size_t len);
165 
166 /**
167 * Clear out memory using a system specific approach to bypass elision by the
168 * compiler (currently using RtlSecureZeroMemory or tricks with volatile pointers).
169 */
170 BOTAN_PUBLIC_API(2,2) int botan_scrub_mem(void* mem, size_t bytes);
171 
172 #define BOTAN_FFI_HEX_LOWER_CASE 1
173 
174 /**
175 * Perform hex encoding
176 * @param x is some binary data
177 * @param len length of x in bytes
178 * @param out an array of at least x*2 bytes
179 * @param flags flags out be upper or lower case?
180 * @return 0 on success, 1 on failure
181 */
182 BOTAN_PUBLIC_API(2,0) int botan_hex_encode(const uint8_t* x, size_t len, char* out, uint32_t flags);
183 
184 /**
185 * Perform hex decoding
186 * @param hex_str a string of hex chars (whitespace is ignored)
187 * @param in_len the length of hex_str
188 * @param out the output buffer should be at least strlen(hex_str)/2 bytes
189 * @param out_len the size of out
190 */
191 BOTAN_PUBLIC_API(2,3) int botan_hex_decode(const char* hex_str, size_t in_len, uint8_t* out, size_t* out_len);
192 
193 /**
194 * Perform base64 encoding
195 */
196 BOTAN_PUBLIC_API(2,3) int botan_base64_encode(const uint8_t* x, size_t len, char* out, size_t* out_len);
197 
198 
199 /**
200 * Perform base64 decoding
201 */
202 BOTAN_PUBLIC_API(2,3) int botan_base64_decode(const char* base64_str, size_t in_len,
203  uint8_t* out, size_t* out_len);
204 
205 /**
206 * RNG type
207 */
208 typedef struct botan_rng_struct* botan_rng_t;
209 
210 /**
211 * Initialize a random number generator object
212 * @param rng rng object
213 * @param rng_type type of the rng, possible values:
214 * "system": System_RNG, "user": AutoSeeded_RNG
215 * Set rng_type to null or empty string to let the library choose
216 *
217 * TODO: replace rng_type with simple flags?
218 */
219 BOTAN_PUBLIC_API(2,0) int botan_rng_init(botan_rng_t* rng, const char* rng_type);
220 
221 /**
222 * Get random bytes from a random number generator
223 * @param rng rng object
224 * @param out output buffer of size out_len
225 * @param out_len number of requested bytes
226 * @return 0 on success, negative on failure
227 *
228 * TODO: better name
229 */
230 BOTAN_PUBLIC_API(2,0) int botan_rng_get(botan_rng_t rng, uint8_t* out, size_t out_len);
231 
232 /**
233 * Reseed a random number generator
234 * Uses the System_RNG as a seed generator.
235 *
236 * @param rng rng object
237 * @param bits number of bits to to reseed with
238 * @return 0 on success, a negative value on failure
239 */
240 BOTAN_PUBLIC_API(2,0) int botan_rng_reseed(botan_rng_t rng, size_t bits);
241 
242 /**
243 * Frees all resources of the random number generator object
244 * @param rng rng object
245 * @return always returns 0
246 */
247 BOTAN_PUBLIC_API(2,0) int botan_rng_destroy(botan_rng_t rng);
248 
249 /*
250 * Hash type
251 */
252 typedef struct botan_hash_struct* botan_hash_t;
253 
254 /**
255 * Initialize a hash function object
256 * @param hash hash object
257 * @param hash_name name of the hash function, e.g., "SHA-384"
258 * @param flags should be 0 in current API revision, all other uses are reserved
259 * and return BOTAN_FFI_ERROR_BAD_FLAG
260 *
261 * TODO: since output_length is effectively required to use this API,
262 * return it from init as an output parameter
263 */
264 BOTAN_PUBLIC_API(2,0) int botan_hash_init(botan_hash_t* hash, const char* hash_name, uint32_t flags);
265 
266 /**
267 * Copy the state of a hash function object
268 * @param dest destination hash object
269 * @param source source hash object
270 * @return 0 on success, a negative value on failure
271 */
272 BOTAN_PUBLIC_API(2,2) int botan_hash_copy_state(botan_hash_t *dest, const botan_hash_t source);
273 
274 /**
275 * Writes the output length of the hash function to *output_length
276 * @param hash hash object
277 * @param output_length output buffer to hold the hash function output length
278 * @return 0 on success, a negative value on failure
279 */
280 BOTAN_PUBLIC_API(2,0) int botan_hash_output_length(botan_hash_t hash, size_t* output_length);
281 
282 /**
283 * Writes the block size of the hash function to *block_size
284 * @param hash hash object
285 * @param block_size output buffer to hold the hash function output length
286 * @return 0 on success, a negative value on failure
287 */
288 BOTAN_PUBLIC_API(2,2) int botan_hash_block_size(botan_hash_t hash, size_t* block_size);
289 
290 /**
291 * Send more input to the hash function
292 * @param hash hash object
293 * @param in input buffer
294 * @param in_len number of bytes to read from the input buffer
295 * @return 0 on success, a negative value on failure
296 */
297 BOTAN_PUBLIC_API(2,0) int botan_hash_update(botan_hash_t hash, const uint8_t* in, size_t in_len);
298 
299 /**
300 * Finalizes the hash computation and writes the output to
301 * out[0:botan_hash_output_length()] then reinitializes for computing
302 * another digest as if botan_hash_clear had been called.
303 * @param hash hash object
304 * @param out output buffer
305 * @return 0 on success, a negative value on failure
306 */
307 BOTAN_PUBLIC_API(2,0) int botan_hash_final(botan_hash_t hash, uint8_t out[]);
308 
309 /**
310 * Reinitializes the state of the hash computation. A hash can
311 * be computed (with update/final) immediately.
312 * @param hash hash object
313 * @return 0 on success, a negative value on failure
314 */
315 BOTAN_PUBLIC_API(2,0) int botan_hash_clear(botan_hash_t hash);
316 
317 /**
318 * Frees all resources of the hash object
319 * @param hash hash object
320 * @return always returns 0
321 */
322 BOTAN_PUBLIC_API(2,0) int botan_hash_destroy(botan_hash_t hash);
323 
324 /**
325 * TODO has no implementation
326 */
327 BOTAN_PUBLIC_API(2,0) int botan_hash_name(botan_hash_t hash, char* name, size_t name_len);
328 
329 /*
330 * Message Authentication type
331 */
332 typedef struct botan_mac_struct* botan_mac_t;
333 
334 /**
335 * Initialize a message authentication code object
336 * @param mac mac object
337 * @param mac_name name of the hash function, e.g., "HMAC(SHA-384)"
338 * @param flags should be 0 in current API revision, all other uses are reserved
339 * and return a negative value (error code)
340 * @return 0 on success, a negative value on failure
341 */
342 BOTAN_PUBLIC_API(2,0) int botan_mac_init(botan_mac_t* mac, const char* mac_name, uint32_t flags);
343 
344 /**
345 * Writes the output length of the message authentication code to *output_length
346 * @param mac mac object
347 * @param output_length output buffer to hold the MAC output length
348 * @return 0 on success, a negative value on failure
349 */
350 BOTAN_PUBLIC_API(2,0) int botan_mac_output_length(botan_mac_t mac, size_t* output_length);
351 
352 /**
353 * Sets the key on the MAC
354 * @param mac mac object
355 * @param key buffer holding the key
356 * @param key_len size of the key buffer in bytes
357 * @return 0 on success, a negative value on failure
358 */
359 BOTAN_PUBLIC_API(2,0) int botan_mac_set_key(botan_mac_t mac, const uint8_t* key, size_t key_len);
360 
361 /**
362 * Send more input to the message authentication code
363 * @param mac mac object
364 * @param buf input buffer
365 * @param len number of bytes to read from the input buffer
366 * @return 0 on success, a negative value on failure
367 */
368 BOTAN_PUBLIC_API(2,0) int botan_mac_update(botan_mac_t mac, const uint8_t* buf, size_t len);
369 
370 /**
371 * Finalizes the MAC computation and writes the output to
372 * out[0:botan_mac_output_length()] then reinitializes for computing
373 * another MAC as if botan_mac_clear had been called.
374 * @param mac mac object
375 * @param out output buffer
376 * @return 0 on success, a negative value on failure
377 */
378 BOTAN_PUBLIC_API(2,0) int botan_mac_final(botan_mac_t mac, uint8_t out[]);
379 
380 /**
381 * Reinitializes the state of the MAC computation. A MAC can
382 * be computed (with update/final) immediately.
383 * @param mac mac object
384 * @return 0 on success, a negative value on failure
385 */
386 BOTAN_PUBLIC_API(2,0) int botan_mac_clear(botan_mac_t mac);
387 
388 /**
389 * Frees all resources of the MAC object
390 * @param mac mac object
391 * @return always returns 0
392 */
393 BOTAN_PUBLIC_API(2,0) int botan_mac_destroy(botan_mac_t mac);
394 
395 /*
396 * Cipher modes
397 */
398 typedef struct botan_cipher_struct* botan_cipher_t;
399 
400 #define BOTAN_CIPHER_INIT_FLAG_MASK_DIRECTION 1
401 #define BOTAN_CIPHER_INIT_FLAG_ENCRYPT 0
402 #define BOTAN_CIPHER_INIT_FLAG_DECRYPT 1
403 
404 BOTAN_PUBLIC_API(2,0) int botan_cipher_init(botan_cipher_t* cipher, const char* name, uint32_t flags);
405 
406 BOTAN_PUBLIC_API(2,0) int botan_cipher_valid_nonce_length(botan_cipher_t cipher, size_t nl);
407 BOTAN_PUBLIC_API(2,0) int botan_cipher_get_tag_length(botan_cipher_t cipher, size_t* tag_size);
408 BOTAN_PUBLIC_API(2,0) int botan_cipher_get_default_nonce_length(botan_cipher_t cipher, size_t* nl);
409 BOTAN_PUBLIC_API(2,0) int botan_cipher_get_update_granularity(botan_cipher_t cipher, size_t* ug);
410 
411 BOTAN_PUBLIC_API(2,0) int botan_cipher_query_keylen(botan_cipher_t,
412  size_t* out_minimum_keylength,
413  size_t* out_maximum_keylength);
414 
415 BOTAN_PUBLIC_API(2,0) int botan_cipher_set_key(botan_cipher_t cipher,
416  const uint8_t* key, size_t key_len);
417 
418 BOTAN_PUBLIC_API(2,0) int botan_cipher_set_associated_data(botan_cipher_t cipher,
419  const uint8_t* ad, size_t ad_len);
420 
421 BOTAN_PUBLIC_API(2,0) int botan_cipher_start(botan_cipher_t cipher,
422  const uint8_t* nonce, size_t nonce_len);
423 
424 #define BOTAN_CIPHER_UPDATE_FLAG_FINAL (1U << 0)
425 
426 BOTAN_PUBLIC_API(2,0) int botan_cipher_update(botan_cipher_t cipher,
427  uint32_t flags,
428  uint8_t output[],
429  size_t output_size,
430  size_t* output_written,
431  const uint8_t input_bytes[],
432  size_t input_size,
433  size_t* input_consumed);
434 
435 BOTAN_PUBLIC_API(2,0) int botan_cipher_clear(botan_cipher_t hash);
436 BOTAN_PUBLIC_API(2,0) int botan_cipher_destroy(botan_cipher_t cipher);
437 
438 /*
439 * Derive a key from a passphrase for a number of iterations
440 * @param pbkdf_algo PBKDF algorithm, e.g., "PBKDF2"
441 * @param out buffer to store the derived key, must be of out_len bytes
442 * @param out_len the desired length of the key to produce
443 * @param passphrase the password to derive the key from
444 * @param salt a randomly chosen salt
445 * @param salt_len length of salt in bytes
446 * @param iterations the number of iterations to use (use 10K or more)
447 * @return 0 on success, a negative value on failure
448 */
449 BOTAN_PUBLIC_API(2,0) int botan_pbkdf(const char* pbkdf_algo,
450  uint8_t out[], size_t out_len,
451  const char* passphrase,
452  const uint8_t salt[], size_t salt_len,
453  size_t iterations);
454 
455 /**
456 * Derive a key from a passphrase, running until msec time has elapsed.
457 * @param pbkdf_algo PBKDF algorithm, e.g., "PBKDF2"
458 * @param out buffer to store the derived key, must be of out_len bytes
459 * @param out_len the desired length of the key to produce
460 * @param passphrase the password to derive the key from
461 * @param salt a randomly chosen salt
462 * @param salt_len length of salt in bytes
463 * @param milliseconds_to_run if iterations is zero, then instead the PBKDF is
464 * run until milliseconds_to_run milliseconds has passed
465 * @param out_iterations_used set to the number iterations executed
466 * @return 0 on success, a negative value on failure
467 */
468 BOTAN_PUBLIC_API(2,0) int botan_pbkdf_timed(const char* pbkdf_algo,
469  uint8_t out[], size_t out_len,
470  const char* passphrase,
471  const uint8_t salt[], size_t salt_len,
472  size_t milliseconds_to_run,
473  size_t* out_iterations_used);
474 
475 /**
476 * Derive a key
477 * @param kdf_algo KDF algorithm, e.g., "SP800-56C"
478 * @param out buffer holding the derived key, must be of length out_len
479 * @param out_len the desired output length in bytes
480 * @param secret the secret input
481 * @param secret_len size of secret in bytes
482 * @param salt a diversifier
483 * @param salt_len size of salt in bytes
484 * @param label purpose for the derived keying material
485 * @param label_len size of label in bytes
486 * @return 0 on success, a negative value on failure
487 */
488 BOTAN_PUBLIC_API(2,0) int botan_kdf(const char* kdf_algo,
489  uint8_t out[], size_t out_len,
490  const uint8_t secret[], size_t secret_len,
491  const uint8_t salt[], size_t salt_len,
492  const uint8_t label[], size_t label_len);
493 
494 /**
495 * Create a password hash using Bcrypt
496 * @param out buffer holding the password hash, should be of length 64 bytes
497 * @param out_len the desired output length in bytes
498 * @param password the password
499 * @param rng a random number generator
500 * @param work_factor how much work to do to slow down guessing attacks
501 * @param flags should be 0 in current API revision, all other uses are reserved
502 * and return BOTAN_FFI_ERROR_BAD_FLAG
503 * @return 0 on success, a negative value on failure
504 
505 * Output is formatted bcrypt $2a$...
506 */
507 BOTAN_PUBLIC_API(2,0) int botan_bcrypt_generate(uint8_t* out, size_t* out_len,
508  const char* password,
509  botan_rng_t rng,
510  size_t work_factor,
511  uint32_t flags);
512 
513 /*
514 * Raw Block Cipher (PRP) interface
515 */
516 typedef struct botan_block_cipher_struct* botan_block_cipher_t;
517 
518 /**
519 * Initialize a block cipher object
520 */
521 BOTAN_PUBLIC_API(2,1) int botan_block_cipher_init(botan_block_cipher_t* bc,
522  const char* cipher_name);
523 
524 /**
525 * Destroy a block cipher object
526 */
527 BOTAN_PUBLIC_API(2,1) int botan_block_cipher_destroy(botan_block_cipher_t bc);
528 
529 /**
530 * Reinitializes the block cipher
531 * @return 0 on success, a negative value on failure
532 */
533 BOTAN_PUBLIC_API(2,1) int botan_block_cipher_clear(botan_block_cipher_t bc);
534 
535 /**
536 * Set the key for a block cipher instance
537 */
538 BOTAN_PUBLIC_API(2,1) int botan_block_cipher_set_key(botan_block_cipher_t bc,
539  const uint8_t key[], size_t len);
540 
541 /**
542 * Return the positive block size of this block cipher, or negative to
543 * indicate an error
544 */
545 BOTAN_PUBLIC_API(2,1) int botan_block_cipher_block_size(botan_block_cipher_t bc);
546 
547 BOTAN_PUBLIC_API(2,1) int botan_block_cipher_encrypt_blocks(botan_block_cipher_t bc,
548  const uint8_t in[],
549  uint8_t out[],
550  size_t blocks);
551 
552 BOTAN_PUBLIC_API(2,1) int botan_block_cipher_decrypt_blocks(botan_block_cipher_t bc,
553  const uint8_t in[],
554  uint8_t out[],
555  size_t blocks);
556 
557 
558 /*
559 * Multiple precision integers
560 */
561 typedef struct botan_mp_struct* botan_mp_t;
562 
563 BOTAN_PUBLIC_API(2,1) int botan_mp_init(botan_mp_t* mp);
564 BOTAN_PUBLIC_API(2,1) int botan_mp_destroy(botan_mp_t mp);
565 
566 // writes botan_mp_num_bytes(mp)*2 + 1 bytes to out[]
567 BOTAN_PUBLIC_API(2,1) int botan_mp_to_hex(const botan_mp_t mp, char* out);
568 BOTAN_PUBLIC_API(2,1) int botan_mp_to_str(const botan_mp_t mp, uint8_t base, char* out, size_t* out_len);
569 
570 BOTAN_PUBLIC_API(2,1) int botan_mp_clear(botan_mp_t mp);
571 
572 BOTAN_PUBLIC_API(2,1) int botan_mp_set_from_int(botan_mp_t mp, int initial_value);
573 BOTAN_PUBLIC_API(2,1) int botan_mp_set_from_mp(botan_mp_t dest, const botan_mp_t source);
574 BOTAN_PUBLIC_API(2,1) int botan_mp_set_from_str(botan_mp_t dest, const char* str);
575 BOTAN_PUBLIC_API(2,1) int botan_mp_set_from_radix_str(botan_mp_t dest, const char* str, size_t radix);
576 
577 BOTAN_PUBLIC_API(2,1) int botan_mp_num_bits(const botan_mp_t n, size_t* bits);
578 BOTAN_PUBLIC_API(2,1) int botan_mp_num_bytes(const botan_mp_t n, size_t* bytes);
579 
580 // Writes botan_mp_num_bytes(mp) to vec
581 BOTAN_PUBLIC_API(2,1) int botan_mp_to_bin(const botan_mp_t mp, uint8_t vec[]);
582 BOTAN_PUBLIC_API(2,1) int botan_mp_from_bin(const botan_mp_t mp, const uint8_t vec[], size_t vec_len);
583 
584 BOTAN_PUBLIC_API(2,1) int botan_mp_to_uint32(const botan_mp_t mp, uint32_t* val);
585 
586 /**
587 * This function is not well named. Returns 1 iff mp is greater than
588 * *or equal to* zero. Use botan_mp_is_negative to detect negative
589 * numbers, botan_mp_is_zero to check for zero.
590 */
591 BOTAN_PUBLIC_API(2,1) int botan_mp_is_positive(const botan_mp_t mp);
592 
593 /**
594 * Return 1 iff mp is less than 0
595 */
596 BOTAN_PUBLIC_API(2,1) int botan_mp_is_negative(const botan_mp_t mp);
597 
598 BOTAN_PUBLIC_API(2,1) int botan_mp_flip_sign(botan_mp_t mp);
599 //BOTAN_PUBLIC_API(2,1) int botan_mp_set_negative(botan_mp_t mp);
600 
601 BOTAN_PUBLIC_API(2,1) int botan_mp_is_zero(const botan_mp_t mp);
602 BOTAN_PUBLIC_API(2,1) int botan_mp_is_odd(const botan_mp_t mp);
603 BOTAN_PUBLIC_API(2,1) int botan_mp_is_even(const botan_mp_t mp);
604 
605 BOTAN_PUBLIC_API(2,1) int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y);
606 BOTAN_PUBLIC_API(2,1) int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y);
607 BOTAN_PUBLIC_API(2,1) int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y);
608 
609 BOTAN_PUBLIC_API(2,1) int botan_mp_div(botan_mp_t quotient,
610  botan_mp_t remainder,
611  const botan_mp_t x, const botan_mp_t y);
612 
613 BOTAN_PUBLIC_API(2,1) int botan_mp_mod_mul(botan_mp_t result, const botan_mp_t x,
614  const botan_mp_t y, const botan_mp_t mod);
615 
616 /*
617 * Returns 0 if x != y
618 * Returns 1 if x == y
619 * Returns negative number on error
620 */
621 BOTAN_PUBLIC_API(2,1) int botan_mp_equal(const botan_mp_t x, const botan_mp_t y);
622 
623 /*
624 * Sets *result to comparison result:
625 * -1 if x < y, 0 if x == y, 1 if x > y
626 * Returns negative number on error or zero on success
627 */
628 BOTAN_PUBLIC_API(2,1) int botan_mp_cmp(int* result, const botan_mp_t x, const botan_mp_t y);
629 
630 /*
631 * Swap two botan_mp_t
632 */
633 BOTAN_PUBLIC_API(2,1) int botan_mp_swap(botan_mp_t x, botan_mp_t y);
634 
635 // Return (base^exponent) % modulus
636 BOTAN_PUBLIC_API(2,1) int botan_mp_powmod(botan_mp_t out, const botan_mp_t base, const botan_mp_t exponent, const botan_mp_t modulus);
637 
638 BOTAN_PUBLIC_API(2,1) int botan_mp_lshift(botan_mp_t out, const botan_mp_t in, size_t shift);
639 BOTAN_PUBLIC_API(2,1) int botan_mp_rshift(botan_mp_t out, const botan_mp_t in, size_t shift);
640 
641 BOTAN_PUBLIC_API(2,1) int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus);
642 
643 BOTAN_PUBLIC_API(2,1) int botan_mp_rand_bits(botan_mp_t rand_out, botan_rng_t rng, size_t bits);
644 
645 BOTAN_PUBLIC_API(2,1) int botan_mp_rand_range(botan_mp_t rand_out, botan_rng_t rng,
646  const botan_mp_t lower_bound, const botan_mp_t upper_bound);
647 
648 BOTAN_PUBLIC_API(2,1) int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y);
649 
650 /**
651 * Returns 0 if n is not prime
652 * Returns 1 if n is prime
653 * Returns negative number on error
654 */
655 BOTAN_PUBLIC_API(2,1) int botan_mp_is_prime(const botan_mp_t n, botan_rng_t rng, size_t test_prob);
656 
657 /**
658 * Returns 0 if specified bit of n is not set
659 * Returns 1 if specified bit of n is set
660 * Returns negative number on error
661 */
662 BOTAN_PUBLIC_API(2,1) int botan_mp_get_bit(const botan_mp_t n, size_t bit);
663 
664 /**
665 * Set the specified bit
666 */
667 BOTAN_PUBLIC_API(2,1) int botan_mp_set_bit(botan_mp_t n, size_t bit);
668 
669 /**
670 * Clear the specified bit
671 */
672 BOTAN_PUBLIC_API(2,1) int botan_mp_clear_bit(botan_mp_t n, size_t bit);
673 
674 /* Bcrypt password hashing */
675 
676 /**
677 * Check a previously created password hash
678 * @param pass the password to check against
679 * @param hash the stored hash to check against
680 * @return 0 if if this password/hash combination is valid,
681 * 1 if the combination is not valid (but otherwise well formed),
682 * negative on error
683 */
684 BOTAN_PUBLIC_API(2,0) int botan_bcrypt_is_valid(const char* pass, const char* hash);
685 
686 /*
687 * Public/private key creation, import, ...
688 */
689 typedef struct botan_privkey_struct* botan_privkey_t;
690 
691 BOTAN_PUBLIC_API(2,0) int botan_privkey_create(botan_privkey_t* key,
692  const char* algo_name,
693  const char* algo_params,
694  botan_rng_t rng);
695 
696 #define BOTAN_CHECK_KEY_EXPENSIVE_TESTS 1
697 
698 BOTAN_PUBLIC_API(2,0) int botan_privkey_check_key(botan_privkey_t key, botan_rng_t rng, uint32_t flags);
699 
700 BOTAN_PUBLIC_API(2,0) int botan_privkey_create_rsa(botan_privkey_t* key, botan_rng_t rng, size_t n_bits);
701 BOTAN_PUBLIC_API(2,0) int botan_privkey_create_ecdsa(botan_privkey_t* key, botan_rng_t rng, const char* params);
702 BOTAN_PUBLIC_API(2,0) int botan_privkey_create_ecdh(botan_privkey_t* key, botan_rng_t rng, const char* params);
703 BOTAN_PUBLIC_API(2,0) int botan_privkey_create_mceliece(botan_privkey_t* key, botan_rng_t rng, size_t n, size_t t);
704 BOTAN_PUBLIC_API(2,0) int botan_privkey_create_dh(botan_privkey_t* key, botan_rng_t rng, const char* param);
705 
706 /*
707 * Input currently assumed to be PKCS #8 structure;
708 * Set password to NULL to indicate no encryption expected
709 */
710 BOTAN_PUBLIC_API(2,0) int botan_privkey_load(botan_privkey_t* key, botan_rng_t rng,
711  const uint8_t bits[], size_t len,
712  const char* password);
713 
714 BOTAN_PUBLIC_API(2,0) int botan_privkey_destroy(botan_privkey_t key);
715 
716 #define BOTAN_PRIVKEY_EXPORT_FLAG_DER 0
717 #define BOTAN_PRIVKEY_EXPORT_FLAG_PEM 1
718 
719 /*
720 * On input *out_len is number of bytes in out[]
721 * On output *out_len is number of bytes written (or required)
722 * If out is not big enough no output is written, *out_len is set and 1 is returned
723 * Returns 0 on success and sets
724 * If some other error occurs a negative integer is returned.
725 */
726 BOTAN_PUBLIC_API(2,0) int botan_privkey_export(botan_privkey_t key,
727  uint8_t out[], size_t* out_len,
728  uint32_t flags);
729 
730 /*
731 * Set encryption_algo to NULL or "" to have the library choose a default (recommended)
732 */
733 BOTAN_DEPRECATED("Use botan_privkey_export_encrypted_pbkdf_{msec,iter}")
734 BOTAN_PUBLIC_API(2,0) int botan_privkey_export_encrypted(botan_privkey_t key,
735  uint8_t out[], size_t* out_len,
736  botan_rng_t rng,
737  const char* passphrase,
738  const char* encryption_algo,
739  uint32_t flags);
740 
741 /*
742 * Export a private key, running PBKDF for specified amount of time
743 * @param key the private key to export
744 */
745 BOTAN_PUBLIC_API(2,0) int botan_privkey_export_encrypted_pbkdf_msec(botan_privkey_t key,
746  uint8_t out[], size_t* out_len,
747  botan_rng_t rng,
748  const char* passphrase,
749  uint32_t pbkdf_msec_runtime,
750  size_t* pbkdf_iterations_out,
751  const char* cipher_algo,
752  const char* pbkdf_algo,
753  uint32_t flags);
754 
755 /*
756 * Export a private key using the specified number of iterations.
757 */
758 BOTAN_PUBLIC_API(2,0) int botan_privkey_export_encrypted_pbkdf_iter(botan_privkey_t key,
759  uint8_t out[], size_t* out_len,
760  botan_rng_t rng,
761  const char* passphrase,
762  size_t pbkdf_iterations,
763  const char* cipher_algo,
764  const char* pbkdf_algo,
765  uint32_t flags);
766 
767 typedef struct botan_pubkey_struct* botan_pubkey_t;
768 
769 BOTAN_PUBLIC_API(2,0) int botan_pubkey_load(botan_pubkey_t* key, const uint8_t bits[], size_t len);
770 
771 BOTAN_PUBLIC_API(2,0) int botan_privkey_export_pubkey(botan_pubkey_t* out, botan_privkey_t in);
772 
773 BOTAN_PUBLIC_API(2,0) int botan_pubkey_export(botan_pubkey_t key, uint8_t out[], size_t* out_len, uint32_t flags);
774 
775 BOTAN_PUBLIC_API(2,0) int botan_pubkey_algo_name(botan_pubkey_t key, char out[], size_t* out_len);
776 
777 /**
778 * Returns 0 if key is valid, negative if invalid key or some other error
779 */
780 BOTAN_PUBLIC_API(2,0) int botan_pubkey_check_key(botan_pubkey_t key, botan_rng_t rng, uint32_t flags);
781 
782 BOTAN_PUBLIC_API(2,0) int botan_pubkey_estimated_strength(botan_pubkey_t key, size_t* estimate);
783 
784 BOTAN_PUBLIC_API(2,0) int botan_pubkey_fingerprint(botan_pubkey_t key, const char* hash,
785  uint8_t out[], size_t* out_len);
786 
787 BOTAN_PUBLIC_API(2,0) int botan_pubkey_destroy(botan_pubkey_t key);
788 
789 /*
790 * Get arbitrary named fields from public or privat keys
791 */
792 BOTAN_PUBLIC_API(2,0) int botan_pubkey_get_field(botan_mp_t output,
793  botan_pubkey_t key,
794  const char* field_name);
795 
796 BOTAN_PUBLIC_API(2,0) int botan_privkey_get_field(botan_mp_t output,
797  botan_privkey_t key,
798  const char* field_name);
799 
800 /*
801 * Algorithm specific key operations: RSA
802 */
803 BOTAN_PUBLIC_API(2,0) int botan_privkey_load_rsa(botan_privkey_t* key,
804  botan_mp_t p,
805  botan_mp_t q,
806  botan_mp_t e);
807 
808 BOTAN_PUBLIC_API(2,0) int botan_privkey_rsa_get_p(botan_mp_t p, botan_privkey_t rsa_key);
809 BOTAN_PUBLIC_API(2,0) int botan_privkey_rsa_get_q(botan_mp_t q, botan_privkey_t rsa_key);
810 BOTAN_PUBLIC_API(2,0) int botan_privkey_rsa_get_d(botan_mp_t d, botan_privkey_t rsa_key);
811 BOTAN_PUBLIC_API(2,0) int botan_privkey_rsa_get_n(botan_mp_t n, botan_privkey_t rsa_key);
812 BOTAN_PUBLIC_API(2,0) int botan_privkey_rsa_get_e(botan_mp_t e, botan_privkey_t rsa_key);
813 
814 BOTAN_PUBLIC_API(2,0) int botan_pubkey_load_rsa(botan_pubkey_t* key,
815  botan_mp_t n,
816  botan_mp_t e);
817 
818 BOTAN_PUBLIC_API(2,0) int botan_pubkey_rsa_get_e(botan_mp_t e, botan_pubkey_t rsa_key);
819 BOTAN_PUBLIC_API(2,0) int botan_pubkey_rsa_get_n(botan_mp_t n, botan_pubkey_t rsa_key);
820 
821 /*
822 * Algorithm specific key operations: DSA
823 */
824 BOTAN_PUBLIC_API(2,0) int botan_privkey_load_dsa(botan_privkey_t* key,
825  botan_mp_t p,
826  botan_mp_t q,
827  botan_mp_t g,
828  botan_mp_t x);
829 
830 BOTAN_PUBLIC_API(2,0) int botan_pubkey_load_dsa(botan_pubkey_t* key,
831  botan_mp_t p,
832  botan_mp_t q,
833  botan_mp_t g,
834  botan_mp_t y);
835 
836 BOTAN_PUBLIC_API(2,0) int botan_privkey_dsa_get_x(botan_mp_t n, botan_privkey_t key);
837 
838 BOTAN_PUBLIC_API(2,0) int botan_pubkey_dsa_get_p(botan_mp_t p, botan_pubkey_t key);
839 BOTAN_PUBLIC_API(2,0) int botan_pubkey_dsa_get_q(botan_mp_t q, botan_pubkey_t key);
840 BOTAN_PUBLIC_API(2,0) int botan_pubkey_dsa_get_g(botan_mp_t d, botan_pubkey_t key);
841 BOTAN_PUBLIC_API(2,0) int botan_pubkey_dsa_get_y(botan_mp_t y, botan_pubkey_t key);
842 
843 /*
844 * Loads Diffie Hellman private key
845 *
846 * @param key variable populated with key material
847 * @param p prime order of a Z_p group
848 * @param g group generator
849 * @param x private key
850 *
851 * @pre key is NULL on input
852 * @post function allocates memory and assigns to `key'
853 *
854 * @return 0 on success, a negative value on failure
855 */
856 BOTAN_PUBLIC_API(2,0) int botan_privkey_load_dh(botan_privkey_t* key,
857  botan_mp_t p,
858  botan_mp_t g,
859  botan_mp_t x);
860 /*
861 * Loads Diffie Hellman public key
862 *
863 * @param key variable populated with key material
864 * @param p prime order of a Z_p group
865 * @param g group generator
866 * @param y public key
867 *
868 * @pre key is NULL on input
869 * @post function allocates memory and assigns to `key'
870 *
871 * @return 0 on success, a negative value on failure
872 */
873 BOTAN_PUBLIC_API(2,0) int botan_pubkey_load_dh(botan_pubkey_t* key,
874  botan_mp_t p,
875  botan_mp_t g,
876  botan_mp_t y);
877 
878 /*
879 * Algorithm specific key operations: ElGamal
880 */
881 
882 /*
883 * Loads ElGamal public key
884 * @param key variable populated with key material
885 * @param p prime order of a Z_p group
886 * @param g group generator
887 * @param y public key
888 *
889 * @pre key is NULL on input
890 * @post function allocates memory and assigns to `key'
891 *
892 * @return 0 on success, a negative value on failure
893 */
894 BOTAN_PUBLIC_API(2,0) int botan_pubkey_load_elgamal(botan_pubkey_t* key,
895  botan_mp_t p,
896  botan_mp_t g,
897  botan_mp_t y);
898 
899 /*
900 * Loads ElGamal private key
901 *
902 * @param key variable populated with key material
903 * @param p prime order of a Z_p group
904 * @param g group generator
905 * @param x private key
906 *
907 * @pre key is NULL on input
908 * @post function allocates memory and assigns to `key'
909 *
910 * @return 0 on success, a negative value on failure
911 */
912 BOTAN_PUBLIC_API(2,0) int botan_privkey_load_elgamal(botan_privkey_t* key,
913  botan_mp_t p,
914  botan_mp_t g,
915  botan_mp_t x);
916 
917 /*
918 * Algorithm specific key operations: Ed25519
919 */
920 
921 BOTAN_PUBLIC_API(2,2) int botan_privkey_load_ed25519(botan_privkey_t* key,
922  const uint8_t privkey[32]);
923 
924 BOTAN_PUBLIC_API(2,2) int botan_pubkey_load_ed25519(botan_pubkey_t* key,
925  const uint8_t pubkey[32]);
926 
927 BOTAN_PUBLIC_API(2,2) int botan_privkey_ed25519_get_privkey(botan_privkey_t key,
928  uint8_t output[64]);
929 
930 BOTAN_PUBLIC_API(2,2) int botan_pubkey_ed25519_get_pubkey(botan_pubkey_t key,
931  uint8_t pubkey[32]);
932 
933 /*
934 * Algorithm specific key operations: ECDSA and ECDH
935 */
936 BOTAN_PUBLIC_API(2,2)
937 int botan_privkey_load_ecdsa(botan_privkey_t* key,
938  const botan_mp_t scalar,
939  const char* curve_name);
940 
941 BOTAN_PUBLIC_API(2,2)
942 int botan_pubkey_load_ecdsa(botan_pubkey_t* key,
943  const botan_mp_t public_x,
944  const botan_mp_t public_y,
945  const char* curve_name);
946 
947 BOTAN_PUBLIC_API(2,2)
948 int botan_pubkey_load_ecdh(botan_pubkey_t* key,
949  const botan_mp_t public_x,
950  const botan_mp_t public_y,
951  const char* curve_name);
952 
953 BOTAN_PUBLIC_API(2,2)
954 int botan_privkey_load_ecdh(botan_privkey_t* key,
955  const botan_mp_t scalar,
956  const char* curve_name);
957 
958 BOTAN_PUBLIC_API(2,2)
959 int botan_pubkey_load_sm2(botan_pubkey_t* key,
960  const botan_mp_t public_x,
961  const botan_mp_t public_y,
962  const char* curve_name);
963 
964 BOTAN_PUBLIC_API(2,2)
965 int botan_privkey_load_sm2(botan_privkey_t* key,
966  const botan_mp_t scalar,
967  const char* curve_name);
968 
969 BOTAN_PUBLIC_API(2,2)
970 int botan_pubkey_load_sm2_enc(botan_pubkey_t* key,
971  const botan_mp_t public_x,
972  const botan_mp_t public_y,
973  const char* curve_name);
974 
975 BOTAN_PUBLIC_API(2,2)
976 int botan_privkey_load_sm2_enc(botan_privkey_t* key,
977  const botan_mp_t scalar,
978  const char* curve_name);
979 
980 BOTAN_PUBLIC_API(2,3)
981 int botan_pubkey_sm2_compute_za(uint8_t out[],
982  size_t* out_len,
983  const char* ident,
984  const char* hash_algo,
985  const botan_pubkey_t key);
986 
987 /*
988 * Public Key Encryption
989 */
990 typedef struct botan_pk_op_encrypt_struct* botan_pk_op_encrypt_t;
991 
992 BOTAN_PUBLIC_API(2,0) int botan_pk_op_encrypt_create(botan_pk_op_encrypt_t* op,
993  botan_pubkey_t key,
994  const char* padding,
995  uint32_t flags);
996 
997 BOTAN_PUBLIC_API(2,0) int botan_pk_op_encrypt_destroy(botan_pk_op_encrypt_t op);
998 
999 BOTAN_PUBLIC_API(2,0) int botan_pk_op_encrypt(botan_pk_op_encrypt_t op,
1000  botan_rng_t rng,
1001  uint8_t out[], size_t* out_len,
1002  const uint8_t plaintext[], size_t plaintext_len);
1003 
1004 /*
1005 * Public Key Decryption
1006 */
1007 typedef struct botan_pk_op_decrypt_struct* botan_pk_op_decrypt_t;
1008 
1009 BOTAN_PUBLIC_API(2,0) int botan_pk_op_decrypt_create(botan_pk_op_decrypt_t* op,
1010  botan_privkey_t key,
1011  const char* padding,
1012  uint32_t flags);
1013 BOTAN_PUBLIC_API(2,0) int botan_pk_op_decrypt_destroy(botan_pk_op_decrypt_t op);
1014 
1015 BOTAN_PUBLIC_API(2,0) int botan_pk_op_decrypt(botan_pk_op_decrypt_t op,
1016  uint8_t out[], size_t* out_len,
1017  const uint8_t ciphertext[], size_t ciphertext_len);
1018 
1019 /*
1020 * Signature Generation
1021 */
1022 typedef struct botan_pk_op_sign_struct* botan_pk_op_sign_t;
1023 
1024 BOTAN_PUBLIC_API(2,0)
1025 int botan_pk_op_sign_create(botan_pk_op_sign_t* op,
1026  botan_privkey_t key,
1027  const char* hash_and_padding,
1028  uint32_t flags);
1029 
1030 BOTAN_PUBLIC_API(2,0) int botan_pk_op_sign_destroy(botan_pk_op_sign_t op);
1031 
1032 BOTAN_PUBLIC_API(2,0) int botan_pk_op_sign_update(botan_pk_op_sign_t op, const uint8_t in[], size_t in_len);
1033 
1034 BOTAN_PUBLIC_API(2,0)
1035 int botan_pk_op_sign_finish(botan_pk_op_sign_t op, botan_rng_t rng,
1036  uint8_t sig[], size_t* sig_len);
1037 
1038 /*
1039 * Signature Verification
1040 */
1041 typedef struct botan_pk_op_verify_struct* botan_pk_op_verify_t;
1042 
1043 BOTAN_PUBLIC_API(2,0)
1044 int botan_pk_op_verify_create(botan_pk_op_verify_t* op,
1045  botan_pubkey_t key,
1046  const char* hash_and_padding,
1047  uint32_t flags);
1048 
1049 BOTAN_PUBLIC_API(2,0) int botan_pk_op_verify_destroy(botan_pk_op_verify_t op);
1050 
1051 BOTAN_PUBLIC_API(2,0) int botan_pk_op_verify_update(botan_pk_op_verify_t op, const uint8_t in[], size_t in_len);
1052 BOTAN_PUBLIC_API(2,0) int botan_pk_op_verify_finish(botan_pk_op_verify_t op, const uint8_t sig[], size_t sig_len);
1053 
1054 /*
1055 * Key Agreement
1056 */
1057 typedef struct botan_pk_op_ka_struct* botan_pk_op_ka_t;
1058 
1059 BOTAN_PUBLIC_API(2,0)
1060 int botan_pk_op_key_agreement_create(botan_pk_op_ka_t* op,
1061  botan_privkey_t key,
1062  const char* kdf,
1063  uint32_t flags);
1064 
1065 BOTAN_PUBLIC_API(2,0) int botan_pk_op_key_agreement_destroy(botan_pk_op_ka_t op);
1066 
1067 BOTAN_PUBLIC_API(2,0) int botan_pk_op_key_agreement_export_public(botan_privkey_t key,
1068  uint8_t out[], size_t* out_len);
1069 
1070 BOTAN_PUBLIC_API(2,0)
1071 int botan_pk_op_key_agreement(botan_pk_op_ka_t op,
1072  uint8_t out[], size_t* out_len,
1073  const uint8_t other_key[], size_t other_key_len,
1074  const uint8_t salt[], size_t salt_len);
1075 
1076 BOTAN_PUBLIC_API(2,0) int botan_pkcs_hash_id(const char* hash_name, uint8_t pkcs_id[], size_t* pkcs_id_len);
1077 
1078 
1079 /*
1080 *
1081 * @param mce_key must be a McEliece key
1082 * ct_len should be pt_len + n/8 + a few?
1083 */
1084 BOTAN_PUBLIC_API(2,0)
1085 int botan_mceies_encrypt(botan_pubkey_t mce_key,
1086  botan_rng_t rng,
1087  const char* aead,
1088  const uint8_t pt[], size_t pt_len,
1089  const uint8_t ad[], size_t ad_len,
1090  uint8_t ct[], size_t* ct_len);
1091 
1092 BOTAN_PUBLIC_API(2,0)
1093 int botan_mceies_decrypt(botan_privkey_t mce_key,
1094  const char* aead,
1095  const uint8_t ct[], size_t ct_len,
1096  const uint8_t ad[], size_t ad_len,
1097  uint8_t pt[], size_t* pt_len);
1098 
1099 typedef struct botan_x509_cert_struct* botan_x509_cert_t;
1100 BOTAN_PUBLIC_API(2,0) int botan_x509_cert_load(botan_x509_cert_t* cert_obj, const uint8_t cert[], size_t cert_len);
1101 BOTAN_PUBLIC_API(2,0) int botan_x509_cert_load_file(botan_x509_cert_t* cert_obj, const char* filename);
1102 BOTAN_PUBLIC_API(2,0) int botan_x509_cert_destroy(botan_x509_cert_t cert);
1103 
1104 BOTAN_PUBLIC_API(2,0)
1105 int botan_x509_cert_gen_selfsigned(botan_x509_cert_t* cert,
1106  botan_privkey_t key,
1107  botan_rng_t rng,
1108  const char* common_name,
1109  const char* org_name);
1110 
1111 // TODO: return botan_time_struct instead
1112 BOTAN_PUBLIC_API(2,0) int botan_x509_cert_get_time_starts(botan_x509_cert_t cert, char out[], size_t* out_len);
1113 BOTAN_PUBLIC_API(2,0) int botan_x509_cert_get_time_expires(botan_x509_cert_t cert, char out[], size_t* out_len);
1114 
1115 BOTAN_PUBLIC_API(2,0) int botan_x509_cert_get_fingerprint(botan_x509_cert_t cert, const char* hash, uint8_t out[], size_t* out_len);
1116 
1117 BOTAN_PUBLIC_API(2,0) int botan_x509_cert_get_serial_number(botan_x509_cert_t cert, uint8_t out[], size_t* out_len);
1118 BOTAN_PUBLIC_API(2,0) int botan_x509_cert_get_authority_key_id(botan_x509_cert_t cert, uint8_t out[], size_t* out_len);
1119 BOTAN_PUBLIC_API(2,0) int botan_x509_cert_get_subject_key_id(botan_x509_cert_t cert, uint8_t out[], size_t* out_len);
1120 
1121 BOTAN_PUBLIC_API(2,0) int botan_x509_cert_get_public_key_bits(botan_x509_cert_t cert,
1122  uint8_t out[], size_t* out_len);
1123 
1124 BOTAN_PUBLIC_API(2,0) int botan_x509_cert_get_public_key(botan_x509_cert_t cert, botan_pubkey_t* key);
1125 
1126 BOTAN_PUBLIC_API(2,0)
1127 int botan_x509_cert_get_issuer_dn(botan_x509_cert_t cert,
1128  const char* key, size_t index,
1129  uint8_t out[], size_t* out_len);
1130 
1131 BOTAN_PUBLIC_API(2,0)
1132 int botan_x509_cert_get_subject_dn(botan_x509_cert_t cert,
1133  const char* key, size_t index,
1134  uint8_t out[], size_t* out_len);
1135 
1136 BOTAN_PUBLIC_API(2,0) int botan_x509_cert_to_string(botan_x509_cert_t cert, char out[], size_t* out_len);
1137 
1138 // Must match values of Key_Constraints in key_constraints.h
1139 enum botan_x509_cert_key_constraints {
1140  NO_CONSTRAINTS = 0,
1141  DIGITAL_SIGNATURE = 32768,
1142  NON_REPUDIATION = 16384,
1143  KEY_ENCIPHERMENT = 8192,
1144  DATA_ENCIPHERMENT = 4096,
1145  KEY_AGREEMENT = 2048,
1146  KEY_CERT_SIGN = 1024,
1147  CRL_SIGN = 512,
1148  ENCIPHER_ONLY = 256,
1149  DECIPHER_ONLY = 128
1150 };
1151 
1152 BOTAN_PUBLIC_API(2,0) int botan_x509_cert_allowed_usage(botan_x509_cert_t cert, unsigned int key_usage);
1153 
1154 /**
1155  * Key wrapping as per RFC 3394
1156  */
1157 BOTAN_PUBLIC_API(2,2)
1158 int botan_key_wrap3394(const uint8_t key[], size_t key_len,
1159  const uint8_t kek[], size_t kek_len,
1160  uint8_t wrapped_key[], size_t *wrapped_key_len);
1161 
1162 BOTAN_PUBLIC_API(2,2)
1163 int botan_key_unwrap3394(const uint8_t wrapped_key[], size_t wrapped_key_len,
1164  const uint8_t kek[], size_t kek_len,
1165  uint8_t key[], size_t *key_len);
1166 
1167 /*
1168 * TLS (WIP)
1169 */
1170 #if defined(BOTAN_HAS_TLS) && 0
1171 
1172 typedef struct botan_tls_session_struct* botan_tls_session_t;
1173 
1174 BOTAN_TEST_API int botan_tls_session_decrypt(botan_tls_session_t* session,
1175  const uint8_t key[], size_t key_len,
1176  const uint8_t blob[], size_t blob_len);
1177 
1178 BOTAN_TEST_API int botan_tls_session_get_version(botan_tls_session_t session, uint16_t* tls_version);
1179 BOTAN_TEST_API int botan_tls_session_get_ciphersuite(botan_tls_session_t session, uint16_t* ciphersuite);
1180 BOTAN_TEST_API int botan_tls_session_encrypt(botan_tls_session_t session, botan_rng_t rng, uint8_t key[], size_t* key_len);
1181 
1182 BOTAN_TEST_API int botan_tls_session_get_peer_certs(botan_tls_session_t session, botan_x509_cert_t certs[], size_t* cert_len);
1183 
1184 // TODO: peer certs, validation, ...
1185 
1186 typedef struct botan_tls_channel_struct* botan_tls_channel_t;
1187 
1188 typedef void (*botan_tls_channel_output_fn)(void* application_data, const uint8_t* data, size_t data_len);
1189 
1190 typedef void (*botan_tls_channel_data_cb)(void* application_data, const uint8_t* data, size_t data_len);
1191 
1192 typedef void (*botan_tls_channel_alert_cb)(void* application_data, uint16_t alert_code);
1193 
1194 typedef void (*botan_tls_channel_session_established)(void* application_data,
1195  botan_tls_channel_t channel,
1196  botan_tls_session_t session);
1197 
1198 BOTAN_TEST_API int botan_tls_channel_init_client(botan_tls_channel_t* channel,
1199  botan_tls_channel_output_fn output_fn,
1200  botan_tls_channel_data_cb data_cb,
1201  botan_tls_channel_alert_cb alert_cb,
1202  botan_tls_channel_session_established session_cb,
1203  const char* server_name);
1204 
1205 BOTAN_TEST_API int botan_tls_channel_init_server(botan_tls_channel_t* channel,
1206  botan_tls_channel_output_fn output_fn,
1207  botan_tls_channel_data_cb data_cb,
1208  botan_tls_channel_alert_cb alert_cb,
1209  botan_tls_channel_session_established session_cb);
1210 
1211 BOTAN_TEST_API int botan_tls_channel_received_data(botan_tls_channel_t chan,
1212  const uint8_t input[], size_t len);
1213 
1214 /**
1215 * Returns 0 for client, 1 for server, negative for error
1216 */
1217 BOTAN_TEST_API int botan_tls_channel_type(botan_tls_channel_t chan);
1218 
1219 BOTAN_TEST_API int botan_tls_channel_send(botan_tls_channel_t chan,
1220  const uint8_t input[], size_t len);
1221 
1222 BOTAN_TEST_API int botan_tls_channel_close(botan_tls_channel_t chan);
1223 
1224 BOTAN_TEST_API int botan_tls_channel_destroy(botan_tls_channel_t chan);
1225 
1226 #endif
1227 #ifdef __cplusplus
1228 }
1229 #endif
1230 
1231 #endif
int botan_mp_init(botan_mp_t *mp)
Definition: ffi_mp.cpp:20
int botan_cipher_query_keylen(botan_cipher_t, size_t *out_minimum_keylength, size_t *out_maximum_keylength)
Definition: ffi_cipher.cpp:44
int botan_cipher_init(botan_cipher_t *cipher, const char *name, uint32_t flags)
Definition: ffi_cipher.cpp:21
int botan_mp_to_str(const botan_mp_t mp, uint8_t base, char *out, size_t *out_len)
Definition: ffi_mp.cpp:108
int botan_privkey_export(botan_privkey_t key, uint8_t out[], size_t *out_len, uint32_t flags)
Definition: ffi_pkey.cpp:158
int botan_hash_clear(botan_hash_t hash)
Definition: ffi_hash.cpp:49
uint32_t botan_version_datestamp()
Definition: ffi.cpp:64
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)
Definition: ffi_cipher.cpp:71
int botan_block_cipher_init(botan_block_cipher_t *bc, const char *cipher_name)
Definition: ffi_block.cpp:17
int botan_constant_time_compare(const uint8_t *x, const uint8_t *y, size_t len)
Definition: ffi.cpp:66
int botan_privkey_create_rsa(botan_privkey_t *key, botan_rng_t rng, size_t n_bits)
int botan_mp_clear(botan_mp_t mp)
Definition: ffi_mp.cpp:31
int botan_base64_decode(const char *base64_str, size_t in_len, uint8_t *out, size_t *out_len)
Definition: ffi.cpp:107
int botan_kdf(const char *kdf_algo, uint8_t out[], size_t out_len, const uint8_t secret[], size_t secret_len, const uint8_t salt[], size_t salt_len, const uint8_t label[], size_t label_len)
Definition: ffi_kdf.cpp:48
int botan_mp_flip_sign(botan_mp_t mp)
Definition: ffi_mp.cpp:89
int botan_mp_to_uint32(const botan_mp_t mp, uint32_t *val)
Definition: ffi_mp.cpp:130
int botan_mp_equal(const botan_mp_t x, const botan_mp_t y)
Definition: ffi_mp.cpp:169
int botan_hash_block_size(botan_hash_t hash, size_t *block_size)
Definition: ffi_hash.cpp:44
int botan_block_cipher_set_key(botan_block_cipher_t bc, const uint8_t key[], size_t len)
Definition: ffi_block.cpp:50
uint32_t botan_version_minor()
Definition: ffi.cpp:62
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
int botan_mp_is_negative(const botan_mp_t mp)
Definition: ffi_mp.cpp:79
Flags flags(Flag flags)
Definition: p11.h:858
int botan_mac_final(botan_mac_t mac, uint8_t out[])
Definition: ffi_mac.cpp:59
int botan_mac_init(botan_mac_t *mac, const char *mac_name, uint32_t flags)
Definition: ffi_mac.cpp:17
int botan_mp_set_from_mp(botan_mp_t dest, const botan_mp_t source)
Definition: ffi_mp.cpp:74
int botan_same_mem(const uint8_t *x, const uint8_t *y, size_t len)
Definition: ffi.cpp:71
int botan_mp_destroy(botan_mp_t mp)
Definition: ffi_mp.cpp:138
int botan_hash_copy_state(botan_hash_t *dest, const botan_hash_t source)
Definition: ffi_hash.cpp:64
int botan_mp_rand_bits(botan_mp_t rand_out, botan_rng_t rng, size_t bits)
Definition: ffi_mp.cpp:229
int botan_cipher_get_update_granularity(botan_cipher_t cipher, size_t *ug)
Definition: ffi_cipher.cpp:186
int botan_cipher_set_associated_data(botan_cipher_t cipher, const uint8_t *ad, size_t ad_len)
Definition: ffi_cipher.cpp:162
int botan_mp_to_bin(const botan_mp_t mp, uint8_t vec[])
Definition: ffi_mp.cpp:125
int botan_block_cipher_destroy(botan_block_cipher_t bc)
Definition: ffi_block.cpp:37
int botan_privkey_create_mceliece(botan_privkey_t *key, botan_rng_t rng, size_t n, size_t t)
int botan_bcrypt_generate(uint8_t *out, size_t *out_len, const char *password, botan_rng_t rng, size_t work_factor, uint32_t flags)
Definition: ffi_kdf.cpp:61
int botan_mac_destroy(botan_mac_t mac)
Definition: ffi_mac.cpp:34
int botan_mp_set_from_radix_str(botan_mp_t dest, const char *str, size_t radix)
Definition: ffi_mp.cpp:56
struct botan_mac_struct * botan_mac_t
Definition: ffi.h:332
const char * botan_version_string()
Definition: ffi.cpp:56
int botan_block_cipher_clear(botan_block_cipher_t bc)
Definition: ffi_block.cpp:42
int botan_mp_set_bit(botan_mp_t n, size_t bit)
Definition: ffi_mp.cpp:261
int botan_mp_mod_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y, const botan_mp_t mod)
Definition: ffi_mp.cpp:221
int botan_mac_set_key(botan_mac_t mac, const uint8_t *key, size_t key_len)
Definition: ffi_mac.cpp:39
int botan_rng_destroy(botan_rng_t rng)
Definition: ffi_rng.cpp:39
int botan_base64_encode(const uint8_t *x, size_t len, char *out, size_t *out_len)
Definition: ffi.cpp:99
int botan_mp_is_positive(const botan_mp_t mp)
Definition: ffi_mp.cpp:84
int botan_mac_output_length(botan_mac_t mac, size_t *output_length)
Definition: ffi_mac.cpp:44
int botan_hash_update(botan_hash_t hash, const uint8_t *in, size_t in_len)
Definition: ffi_hash.cpp:54
struct botan_mp_struct * botan_mp_t
Definition: ffi.h:561
int botan_mp_swap(botan_mp_t x, botan_mp_t y)
Definition: ffi_mp.cpp:194
int botan_hash_final(botan_hash_t hash, uint8_t out[])
Definition: ffi_hash.cpp:59
int botan_hash_output_length(botan_hash_t hash, size_t *output_length)
Definition: ffi_hash.cpp:39
int botan_mp_rshift(botan_mp_t out, const botan_mp_t in, size_t shift)
Definition: ffi_mp.cpp:211
int botan_pbkdf_timed(const char *pbkdf_algo, uint8_t out[], size_t out_len, const char *passphrase, const uint8_t salt[], size_t salt_len, size_t milliseconds_to_run, size_t *out_iterations_used)
Definition: ffi_kdf.cpp:32
int botan_mp_is_even(const botan_mp_t mp)
Definition: ffi_mp.cpp:184
int botan_mp_get_bit(const botan_mp_t n, size_t bit)
Definition: ffi_mp.cpp:256
int botan_hex_encode(const uint8_t *x, size_t len, char *out, uint32_t flags)
Definition: ffi.cpp:82
int botan_mp_to_hex(const botan_mp_t mp, char *out)
Definition: ffi_mp.cpp:99
uint32_t botan_ffi_api_version()
Definition: ffi.cpp:32
struct botan_hash_struct * botan_hash_t
Definition: ffi.h:252
int botan_privkey_create_ecdh(botan_privkey_t *key, botan_rng_t rng, const char *params)
int botan_rng_init(botan_rng_t *rng, const char *rng_type)
Definition: ffi_rng.cpp:17
int botan_hash_name(botan_hash_t hash, char *name, size_t name_len)
int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition: ffi_mp.cpp:148
int botan_cipher_destroy(botan_cipher_t cipher)
Definition: ffi_cipher.cpp:34
int botan_cipher_set_key(botan_cipher_t cipher, const uint8_t *key, size_t key_len)
Definition: ffi_cipher.cpp:54
int botan_hash_init(botan_hash_t *hash, const char *hash_name, uint32_t flags)
Definition: ffi_hash.cpp:17
uint32_t botan_version_patch()
Definition: ffi.cpp:63
int botan_mac_update(botan_mac_t mac, const uint8_t *buf, size_t len)
Definition: ffi_mac.cpp:54
int botan_hex_decode(const char *hex_str, size_t in_len, uint8_t *out, size_t *out_len)
Definition: ffi.cpp:91
size_t salt_len
Definition: x509_obj.cpp:25
int botan_hash_destroy(botan_hash_t hash)
Definition: ffi_hash.cpp:34
int botan_block_cipher_decrypt_blocks(botan_block_cipher_t bc, const uint8_t in[], uint8_t out[], size_t blocks)
Definition: ffi_block.cpp:74
int botan_cipher_valid_nonce_length(botan_cipher_t cipher, size_t nl)
Definition: ffi_cipher.cpp:176
int botan_mp_set_from_int(botan_mp_t mp, int initial_value)
Definition: ffi_mp.cpp:36
struct botan_privkey_struct * botan_privkey_t
Definition: ffi.h:689
int botan_cipher_get_tag_length(botan_cipher_t cipher, size_t *tag_size)
Definition: ffi_cipher.cpp:191
uint32_t botan_version_major()
Definition: ffi.cpp:61
int botan_privkey_create(botan_privkey_t *key, const char *algo_name, const char *algo_params, botan_rng_t rng)
Definition: ffi_pkey.cpp:26
int botan_mp_rand_range(botan_mp_t rand_out, botan_rng_t rng, const botan_mp_t lower_bound, const botan_mp_t upper_bound)
Definition: ffi_mp.cpp:235
int botan_mp_is_zero(const botan_mp_t mp)
Definition: ffi_mp.cpp:174
int botan_privkey_create_dh(botan_privkey_t *key, botan_rng_t rng, const char *param)
int botan_mp_is_prime(const botan_mp_t n, botan_rng_t rng, size_t test_prob)
Definition: ffi_mp.cpp:250
int botan_rng_reseed(botan_rng_t rng, size_t bits)
Definition: ffi_rng.cpp:49
int botan_privkey_create_ecdsa(botan_privkey_t *key, botan_rng_t rng, const char *params)
int botan_mp_num_bytes(const botan_mp_t n, size_t *bytes)
Definition: ffi_mp.cpp:276
struct botan_block_cipher_struct * botan_block_cipher_t
Definition: ffi.h:516
int botan_cipher_start(botan_cipher_t cipher, const uint8_t *nonce, size_t nonce_len)
Definition: ffi_cipher.cpp:60
int botan_privkey_load(botan_privkey_t *key, botan_rng_t rng, const uint8_t bits[], size_t len, const char *password)
Definition: ffi_pkey.cpp:57
int botan_scrub_mem(void *mem, size_t bytes)
Definition: ffi.cpp:76
int botan_block_cipher_encrypt_blocks(botan_block_cipher_t bc, const uint8_t in[], uint8_t out[], size_t blocks)
Definition: ffi_block.cpp:66
int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition: ffi_mp.cpp:153
int botan_block_cipher_block_size(botan_block_cipher_t bc)
Definition: ffi_block.cpp:60
int botan_cipher_clear(botan_cipher_t hash)
Definition: ffi_cipher.cpp:39
int botan_mp_is_odd(const botan_mp_t mp)
Definition: ffi_mp.cpp:179
int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus)
Definition: ffi_mp.cpp:216
int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition: ffi_mp.cpp:143
int botan_mp_lshift(botan_mp_t out, const botan_mp_t in, size_t shift)
Definition: ffi_mp.cpp:206
int botan_privkey_check_key(botan_privkey_t key, botan_rng_t rng, uint32_t flags)
Definition: ffi_pkey.cpp:139
int botan_mp_cmp(int *result, const botan_mp_t x, const botan_mp_t y)
Definition: ffi_mp.cpp:189
int botan_mp_div(botan_mp_t quotient, botan_mp_t remainder, const botan_mp_t x, const botan_mp_t y)
Definition: ffi_mp.cpp:158
int botan_bcrypt_is_valid(const char *pass, const char *hash)
Definition: ffi_kdf.cpp:86
int botan_mac_clear(botan_mac_t mac)
Definition: ffi_mac.cpp:49
int botan_rng_get(botan_rng_t rng, uint8_t *out, size_t out_len)
Definition: ffi_rng.cpp:44
struct botan_cipher_struct * botan_cipher_t
Definition: ffi.h:398
int botan_mp_powmod(botan_mp_t out, const botan_mp_t base, const botan_mp_t exponent, const botan_mp_t modulus)
Definition: ffi_mp.cpp:200
int botan_pbkdf(const char *pbkdf_algo, uint8_t out[], size_t out_len, const char *passphrase, const uint8_t salt[], size_t salt_len, size_t iterations)
Definition: ffi_kdf.cpp:21
int botan_mp_from_bin(const botan_mp_t mp, const uint8_t vec[], size_t vec_len)
Definition: ffi_mp.cpp:94
int botan_ffi_supports_api(uint32_t api_version)
Definition: ffi.cpp:37
int botan_mp_clear_bit(botan_mp_t n, size_t bit)
Definition: ffi_mp.cpp:266
int botan_mp_num_bits(const botan_mp_t n, size_t *bits)
Definition: ffi_mp.cpp:271
MechanismType hash
int botan_cipher_get_default_nonce_length(botan_cipher_t cipher, size_t *nl)
Definition: ffi_cipher.cpp:181
struct botan_rng_struct * botan_rng_t
Definition: ffi.h:208
int botan_mp_set_from_str(botan_mp_t dest, const char *str)
Definition: ffi_mp.cpp:51
int botan_privkey_destroy(botan_privkey_t key)
Definition: ffi_pkey.cpp:88
int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y)
Definition: ffi_mp.cpp:244