Botan  2.12.1
Crypto and TLS for C++11
Functions
ffi_kdf.cpp File Reference
#include <botan/ffi.h>
#include <botan/internal/ffi_util.h>
#include <botan/internal/ffi_rng.h>
#include <botan/pbkdf.h>
#include <botan/pwdhash.h>
#include <botan/kdf.h>

Go to the source code of this file.

Functions

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)
 
int botan_bcrypt_is_valid (const char *pass, const char *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)
 
int botan_pbkdf (const char *algo, uint8_t out[], size_t out_len, const char *pass, const uint8_t salt[], size_t salt_len, size_t iterations)
 
int botan_pbkdf_timed (const char *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)
 
int botan_pwdhash (const char *algo, size_t param1, size_t param2, size_t param3, uint8_t out[], size_t out_len, const char *password, size_t password_len, const uint8_t salt[], size_t salt_len)
 
int botan_pwdhash_timed (const char *algo, uint32_t msec, size_t *param1, size_t *param2, size_t *param3, uint8_t out[], size_t out_len, const char *password, size_t password_len, const uint8_t salt[], size_t salt_len)
 
int botan_scrypt (uint8_t out[], size_t out_len, const char *password, const uint8_t salt[], size_t salt_len, size_t N, size_t r, size_t p)
 

Function Documentation

◆ botan_bcrypt_generate()

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 
)

Create a password hash using Bcrypt

Parameters
outbuffer holding the password hash, should be of length 64 bytes
out_lenthe desired output length in bytes
passwordthe password
rnga random number generator
work_factorhow much work to do to slow down guessing attacks
flagsshould be 0 in current API revision, all other uses are reserved and return BOTAN_FFI_ERROR_BAD_FLAG
Returns
0 on success, a negative value on failure

Output is formatted bcrypt $2a$...

Definition at line 153 of file ffi_kdf.cpp.

References BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, Botan_FFI::ffi_guard_thunk(), Botan::PKCS11::flags(), Botan::generate_bcrypt(), Botan_FFI::safe_get(), and Botan_FFI::write_str_output().

157  {
158 #if defined(BOTAN_HAS_BCRYPT)
159  return ffi_guard_thunk(__func__, [=]() -> int {
160  if(out == nullptr || out_len == nullptr || pass == nullptr)
162 
163  if(flags != 0)
165 
166  if(wf < 4 || wf > 18)
168 
169  Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
170  const std::string bcrypt = Botan::generate_bcrypt(pass, rng, static_cast<uint16_t>(wf));
171  return write_str_output(out, out_len, bcrypt);
172  });
173 #else
175 #endif
176  }
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:86
Flags flags(Flag flags)
Definition: p11.h:858
int write_str_output(uint8_t out[], size_t *out_len, const std::string &str)
Definition: ffi_util.h:151
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:61
std::string generate_bcrypt(const std::string &pass, RandomNumberGenerator &rng, uint16_t work_factor, char version)
Definition: bcrypt.cpp:137

◆ botan_bcrypt_is_valid()

int botan_bcrypt_is_valid ( const char *  pass,
const char *  hash 
)

Check a previously created password hash

Parameters
passthe password to check against
hashthe stored hash to check against
Returns
0 if if this password/hash combination is valid, 1 if the combination is not valid (but otherwise well formed), negative on error

Definition at line 178 of file ffi_kdf.cpp.

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_INVALID_VERIFIER, BOTAN_FFI_SUCCESS, Botan::check_bcrypt(), Botan_FFI::ffi_guard_thunk(), and hash.

179  {
180 #if defined(BOTAN_HAS_BCRYPT)
181  return ffi_guard_thunk(__func__, [=]() -> int {
183  });
184 #else
186 #endif
187  }
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:86
bool check_bcrypt(const std::string &pass, const std::string &hash)
Definition: bcrypt.cpp:155
MechanismType hash

◆ botan_kdf()

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 
)

Derive a key

Parameters
kdf_algoKDF algorithm, e.g., "SP800-56C"
outbuffer holding the derived key, must be of length out_len
out_lenthe desired output length in bytes
secretthe secret input
secret_lensize of secret in bytes
salta diversifier
salt_lensize of salt in bytes
labelpurpose for the derived keying material
label_lensize of label in bytes
Returns
0 on success, a negative value on failure

Definition at line 129 of file ffi_kdf.cpp.

References BOTAN_FFI_SUCCESS, Botan_FFI::ffi_guard_thunk(), Botan::get_kdf(), and salt_len.

134  {
135  return ffi_guard_thunk(__func__, [=]() -> int {
136  std::unique_ptr<Botan::KDF> kdf(Botan::get_kdf(kdf_algo));
137  kdf->kdf(out, out_len, secret, secret_len, salt, salt_len, label, label_len);
138  return BOTAN_FFI_SUCCESS;
139  });
140  }
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:86
KDF * get_kdf(const std::string &algo_spec)
Definition: kdf.cpp:237
size_t salt_len
Definition: x509_obj.cpp:25

◆ botan_pbkdf()

int botan_pbkdf ( const char *  algo,
uint8_t  out[],
size_t  out_len,
const char *  pass,
const uint8_t  salt[],
size_t  salt_len,
size_t  iterations 
)

Definition at line 22 of file ffi_kdf.cpp.

References botan_pwdhash(), and salt_len.

25  {
26  return botan_pwdhash(algo,
27  iterations,
28  0,
29  0,
30  out, out_len,
31  pass, 0,
32  salt, salt_len);
33  }
size_t salt_len
Definition: x509_obj.cpp:25
int botan_pwdhash(const char *algo, size_t param1, size_t param2, size_t param3, uint8_t out[], size_t out_len, const char *password, size_t password_len, const uint8_t salt[], size_t salt_len)
Definition: ffi_kdf.cpp:52

◆ botan_pbkdf_timed()

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 
)

Derive a key from a passphrase, running until msec time has elapsed.

Parameters
pbkdf_algoPBKDF algorithm, e.g., "PBKDF2(SHA-256)"
outbuffer to store the derived key, must be of out_len bytes
out_lenthe desired length of the key to produce
passphrasethe password to derive the key from
salta randomly chosen salt
salt_lenlength of salt in bytes
milliseconds_to_runif iterations is zero, then instead the PBKDF is run until milliseconds_to_run milliseconds has passed
out_iterations_usedset to the number iterations executed
Returns
0 on success, a negative value on failure

Deprecated: use

botan_pwdhash_timed(pbkdf_algo, static_cast<uint32_t>(ms_to_run), iterations_used, nullptr, nullptr, out, out_len, password, 0, salt, salt_len);

Definition at line 35 of file ffi_kdf.cpp.

References botan_pwdhash_timed(), and salt_len.

41  {
42  return botan_pwdhash_timed(algo,
43  static_cast<uint32_t>(ms_to_run),
44  iterations_used,
45  nullptr,
46  nullptr,
47  out, out_len,
48  password, 0,
49  salt, salt_len);
50  }
int botan_pwdhash_timed(const char *algo, uint32_t msec, size_t *param1, size_t *param2, size_t *param3, uint8_t out[], size_t out_len, const char *password, size_t password_len, const uint8_t salt[], size_t salt_len)
Definition: ffi_kdf.cpp:86
size_t salt_len
Definition: x509_obj.cpp:25

◆ botan_pwdhash()

int botan_pwdhash ( const char *  algo,
size_t  param1,
size_t  param2,
size_t  param3,
uint8_t  out[],
size_t  out_len,
const char *  password,
size_t  password_len,
const uint8_t  salt[],
size_t  salt_len 
)

Definition at line 52 of file ffi_kdf.cpp.

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, Botan::PasswordHashFamily::create(), Botan_FFI::ffi_guard_thunk(), and salt_len.

Referenced by botan_pbkdf(), and botan_scrypt().

63  {
64  if(algo == nullptr || password == nullptr)
66 
67  if(password_len == 0)
68  password_len = std::strlen(password);
69 
70  return ffi_guard_thunk(__func__, [=]() -> int {
71  auto pwdhash_fam = Botan::PasswordHashFamily::create(algo);
72 
73  if(!pwdhash_fam)
75 
76  auto pwdhash = pwdhash_fam->from_params(param1, param2, param3);
77 
78  pwdhash->derive_key(out, out_len,
79  password, password_len,
80  salt, salt_len);
81 
82  return BOTAN_FFI_SUCCESS;
83  });
84  }
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:86
size_t salt_len
Definition: x509_obj.cpp:25
static std::unique_ptr< PasswordHashFamily > create(const std::string &algo_spec, const std::string &provider="")
Definition: pwdhash.cpp:33

◆ botan_pwdhash_timed()

int botan_pwdhash_timed ( const char *  algo,
uint32_t  msec,
size_t *  param1,
size_t *  param2,
size_t *  param3,
uint8_t  out[],
size_t  out_len,
const char *  password,
size_t  password_len,
const uint8_t  salt[],
size_t  salt_len 
)

Definition at line 86 of file ffi_kdf.cpp.

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, Botan::PasswordHashFamily::create(), Botan_FFI::ffi_guard_thunk(), and salt_len.

Referenced by botan_pbkdf_timed().

98  {
99  if(algo == nullptr || password == nullptr)
101 
102  if(password_len == 0)
103  password_len = std::strlen(password);
104 
105  return ffi_guard_thunk(__func__, [=]() -> int {
106 
107  auto pwdhash_fam = Botan::PasswordHashFamily::create(algo);
108 
109  if(!pwdhash_fam)
111 
112  auto pwdhash = pwdhash_fam->tune(out_len, std::chrono::milliseconds(msec));
113 
114  if(param1)
115  *param1 = pwdhash->iterations();
116  if(param2)
117  *param2 = pwdhash->parallelism();
118  if(param3)
119  *param3 = pwdhash->memory_param();
120 
121  pwdhash->derive_key(out, out_len,
122  password, password_len,
123  salt, salt_len);
124 
125  return BOTAN_FFI_SUCCESS;
126  });
127  }
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:86
size_t salt_len
Definition: x509_obj.cpp:25
static std::unique_ptr< PasswordHashFamily > create(const std::string &algo_spec, const std::string &provider="")
Definition: pwdhash.cpp:33

◆ botan_scrypt()

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 using scrypt Deprecated; use botan_pwdhash("Scrypt", N, r, p, out, out_len, password, 0, salt, salt_len);

Definition at line 142 of file ffi_kdf.cpp.

References botan_pwdhash(), and salt_len.

146  {
147  return botan_pwdhash("Scrypt", N, r, p,
148  out, out_len,
149  password, 0,
150  salt, salt_len);
151  }
size_t salt_len
Definition: x509_obj.cpp:25
int botan_pwdhash(const char *algo, size_t param1, size_t param2, size_t param3, uint8_t out[], size_t out_len, const char *password, size_t password_len, const uint8_t salt[], size_t salt_len)
Definition: ffi_kdf.cpp:52