diff options
author | Jack Lloyd <[email protected]> | 2018-08-10 21:30:23 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-08-10 21:30:23 -0400 |
commit | 4bae26bc1a884d534dc482ba3cd4cbeaa50f4963 (patch) | |
tree | b61b2c328e8d479f019fddd97a260344d08575e8 /src/lib | |
parent | 4c1129afb9c712f3de01d47992c9f52edfb7eee0 (diff) |
Add some useful FFI functions
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/ffi/ffi.h | 22 | ||||
-rw-r--r-- | src/lib/ffi/ffi_kdf.cpp | 19 | ||||
-rw-r--r-- | src/lib/ffi/ffi_pkey.cpp | 5 | ||||
-rw-r--r-- | src/lib/ffi/ffi_rng.cpp | 9 |
4 files changed, 55 insertions, 0 deletions
diff --git a/src/lib/ffi/ffi.h b/src/lib/ffi/ffi.h index 89ac30e27..83c84b3fa 100644 --- a/src/lib/ffi/ffi.h +++ b/src/lib/ffi/ffi.h @@ -216,6 +216,18 @@ BOTAN_PUBLIC_API(2,0) int botan_rng_get(botan_rng_t rng, uint8_t* out, size_t ou BOTAN_PUBLIC_API(2,0) int botan_rng_reseed(botan_rng_t rng, size_t bits); /** +* Reseed a random number generator +* Uses the System_RNG as a seed generator. +* +* @param rng rng object +* @param bits number of bits to to reseed with +* @return 0 on success, a negative value on failure +*/ +BOTAN_PUBLIC_API(2,8) int botan_rng_reseed_from_rng(botan_rng_t rng, + botan_rng_t source_rng, + size_t bits); + +/** * Add some seed material to a random number generator * * @param rng rng object @@ -487,6 +499,14 @@ BOTAN_PUBLIC_API(2,0) int botan_pbkdf_timed(const char* pbkdf_algo, size_t milliseconds_to_run, size_t* out_iterations_used); + +/** +* Derive a key using scrypt +*/ +BOTAN_PUBLIC_API(2,8) int botan_scrypt(uint8_t out[], size_t out_len, + const char* passphrase, + const uint8_t salt[], size_t salt_len, + size_t N, size_t r, size_t p); /** * Derive a key * @param kdf_algo KDF algorithm, e.g., "SP800-56C" @@ -815,6 +835,8 @@ BOTAN_PUBLIC_API(2,0) int botan_privkey_export(botan_privkey_t key, uint8_t out[], size_t* out_len, uint32_t flags); +BOTAN_PUBLIC_API(2,8) int botan_privkey_algo_name(botan_privkey_t key, char out[], size_t* out_len); + /* * Set encryption_algo to NULL or "" to have the library choose a default (recommended) */ diff --git a/src/lib/ffi/ffi_kdf.cpp b/src/lib/ffi/ffi_kdf.cpp index 8a0ba9be4..3b907e967 100644 --- a/src/lib/ffi/ffi_kdf.cpp +++ b/src/lib/ffi/ffi_kdf.cpp @@ -14,6 +14,10 @@ #include <botan/bcrypt.h> #endif +#if defined(BOTAN_HAS_SCRYPT) + #include <botan/scrypt.h> +#endif + extern "C" { using namespace Botan_FFI; @@ -58,6 +62,21 @@ int botan_kdf(const char* kdf_algo, }); } +int botan_scrypt(uint8_t out[], size_t out_len, + const char* passphrase, + const uint8_t salt[], size_t salt_len, + size_t N, size_t r, size_t p) + { +#if defined(BOTAN_HAS_SCRYPT) + return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int { + Botan::scrypt(out, out_len, passphrase, salt, salt_len, N, r, p); + return BOTAN_FFI_SUCCESS; + }); +#else + return BOTAN_FFI_ERROR_NOT_IMPLEMENTED; +#endif + } + int botan_bcrypt_generate(uint8_t* out, size_t* out_len, const char* pass, botan_rng_t rng_obj, size_t wf, diff --git a/src/lib/ffi/ffi_pkey.cpp b/src/lib/ffi/ffi_pkey.cpp index fe5a47db1..584252f2b 100644 --- a/src/lib/ffi/ffi_pkey.cpp +++ b/src/lib/ffi/ffi_pkey.cpp @@ -123,6 +123,11 @@ int botan_privkey_export_pubkey(botan_pubkey_t* pubout, botan_privkey_t key_obj) }); } +int botan_privkey_algo_name(botan_privkey_t key, char out[], size_t* out_len) + { + return BOTAN_FFI_DO(Botan::Private_Key, key, k, { return write_str_output(out, out_len, k.algo_name()); }); + } + int botan_pubkey_algo_name(botan_pubkey_t key, char out[], size_t* out_len) { return BOTAN_FFI_DO(Botan::Public_Key, key, k, { return write_str_output(out, out_len, k.algo_name()); }); diff --git a/src/lib/ffi/ffi_rng.cpp b/src/lib/ffi/ffi_rng.cpp index a4dda59ec..7e185f75d 100644 --- a/src/lib/ffi/ffi_rng.cpp +++ b/src/lib/ffi/ffi_rng.cpp @@ -32,6 +32,10 @@ int botan_rng_init(botan_rng_t* rng_out, const char* rng_type) { rng.reset(new Botan::AutoSeeded_RNG); } + else if(rng_type_s == "null") + { + rng.reset(new Botan::Null_RNG); + } #if defined(BOTAN_TARGET_OS_HAS_THREADS) else if(rng_type_s == "user-threadsafe") { @@ -68,4 +72,9 @@ int botan_rng_add_entropy(botan_rng_t rng, const uint8_t* input, size_t len) return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.add_entropy(input, len); }); } +int botan_rng_reseed_from_rng(botan_rng_t rng, botan_rng_t source_rng, size_t bits) + { + return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.reseed_from_rng(safe_get(source_rng), bits); }); + } + } |