Botan  2.4.0
Crypto and TLS for C++11
ffi_kdf.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/internal/ffi_rng.h>
10 #include <botan/pbkdf.h>
11 #include <botan/kdf.h>
12 
13 #if defined(BOTAN_HAS_BCRYPT)
14  #include <botan/bcrypt.h>
15 #endif
16 
17 extern "C" {
18 
19 using namespace Botan_FFI;
20 
21 int botan_pbkdf(const char* pbkdf_algo, uint8_t out[], size_t out_len,
22  const char* pass, const uint8_t salt[], size_t salt_len,
23  size_t iterations)
24  {
25  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
26  std::unique_ptr<Botan::PBKDF> pbkdf(Botan::get_pbkdf(pbkdf_algo));
27  pbkdf->pbkdf_iterations(out, out_len, pass, salt, salt_len, iterations);
28  return BOTAN_FFI_SUCCESS;
29  });
30  }
31 
32 int botan_pbkdf_timed(const char* pbkdf_algo,
33  uint8_t out[], size_t out_len,
34  const char* password,
35  const uint8_t salt[], size_t salt_len,
36  size_t ms_to_run,
37  size_t* iterations_used)
38  {
39  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
40  std::unique_ptr<Botan::PBKDF> pbkdf(Botan::get_pbkdf(pbkdf_algo));
41  pbkdf->pbkdf_timed(out, out_len, password, salt, salt_len,
42  std::chrono::milliseconds(ms_to_run),
43  *iterations_used);
44  return BOTAN_FFI_SUCCESS;
45  });
46  }
47 
48 int botan_kdf(const char* kdf_algo,
49  uint8_t out[], size_t out_len,
50  const uint8_t secret[], size_t secret_len,
51  const uint8_t salt[], size_t salt_len,
52  const uint8_t label[], size_t label_len)
53  {
54  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
55  std::unique_ptr<Botan::KDF> kdf(Botan::get_kdf(kdf_algo));
56  kdf->kdf(out, out_len, secret, secret_len, salt, salt_len, label, label_len);
57  return BOTAN_FFI_SUCCESS;
58  });
59  }
60 
61 int botan_bcrypt_generate(uint8_t* out, size_t* out_len,
62  const char* pass,
63  botan_rng_t rng_obj, size_t wf,
64  uint32_t flags)
65  {
66 #if defined(BOTAN_HAS_BCRYPT)
67  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
68  if(out == nullptr || out_len == nullptr || pass == nullptr)
70 
71  if(flags != 0)
73 
74  if(wf < 4 || wf > 18)
75  throw FFI_Error("Bad bcrypt work factor " + std::to_string(wf));
76 
77  Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
78  const std::string bcrypt = Botan::generate_bcrypt(pass, rng, static_cast<uint16_t>(wf));
79  return write_str_output(out, out_len, bcrypt);
80  });
81 #else
83 #endif
84  }
85 
86 int botan_bcrypt_is_valid(const char* pass, const char* hash)
87  {
88 #if defined(BOTAN_HAS_BCRYPT)
89  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
91  });
92 #else
94 #endif
95  }
96 
97 }
#define BOTAN_FFI_INVALID_VERIFIER
Definition: ffi.h:139
#define BOTAN_FFI_ERROR_BAD_FLAG
Definition: ffi.h:146
int botan_bcrypt_generate(uint8_t *out, size_t *out_len, const char *pass, botan_rng_t rng_obj, size_t wf, uint32_t flags)
Definition: ffi_kdf.cpp:61
int botan_pbkdf_timed(const char *pbkdf_algo, uint8_t out[], size_t out_len, const char *password, const uint8_t salt[], size_t salt_len, size_t ms_to_run, size_t *iterations_used)
Definition: ffi_kdf.cpp:32
#define BOTAN_FFI_SUCCESS
Definition: ffi.h:137
int ffi_guard_thunk(const char *func_name, Thunk thunk)
Definition: ffi_util.h:64
Flags flags(Flag flags)
Definition: p11.h:858
std::string generate_bcrypt(const std::string &pass, RandomNumberGenerator &rng, uint16_t work_factor)
Definition: bcrypt.cpp:126
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:108
int write_str_output(uint8_t out[], size_t *out_len, const std::string &str)
Definition: ffi_util.h:151
#define BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:149
int botan_pbkdf(const char *pbkdf_algo, uint8_t out[], size_t out_len, const char *pass, const uint8_t salt[], size_t salt_len, size_t iterations)
Definition: ffi_kdf.cpp:21
PBKDF * get_pbkdf(const std::string &algo_spec, const std::string &provider="")
Definition: pbkdf.h:230
KDF * get_kdf(const std::string &algo_spec)
Definition: kdf.cpp:236
size_t salt_len
Definition: x509_obj.cpp:25
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:49
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:143
#define BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:147
bool check_bcrypt(const std::string &pass, const std::string &hash)
Definition: bcrypt.cpp:133
int botan_bcrypt_is_valid(const char *pass, const char *hash)
Definition: ffi_kdf.cpp:86
MechanismType hash
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
struct botan_rng_struct * botan_rng_t
Definition: ffi.h:208