Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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

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

  out | buffer holding the password hash, should be of length 64 bytes
  out_len | the desired output length in bytes
  password | the password
  rng | a random number generator
  work_factor | how much work to do to slow down guessing attacks
  flags | should 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::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)
161 return BOTAN_FFI_ERROR_NULL_POINTER;
162
163 if(flags != 0)
164 return BOTAN_FFI_ERROR_BAD_FLAG;
165
166 if(wf < 4 || wf > 18)
167 return BOTAN_FFI_ERROR_BAD_PARAMETER;
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
174 return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
175 #endif
176 }
Botan_FFI::ffi_guard_thunk
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:89
Botan::RandomNumberGenerator
Definition: rng.h:24
Botan::PKCS11::flags
Flags flags(Flag flags)
Definition: p11.h:858
BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:82
Botan_FFI::write_str_output
int write_str_output(uint8_t out[], size_t *out_len, const std::string &str)
Definition: ffi_util.h:160
BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:76
BOTAN_FFI_ERROR_BAD_PARAMETER
Definition: ffi.h:77
Botan_FFI::safe_get
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:61
BOTAN_FFI_ERROR_BAD_FLAG
Definition: ffi.h:75
Botan::generate_bcrypt
std::string generate_bcrypt(const std::string &pass, RandomNumberGenerator &rng, uint16_t work_factor, char version)
Definition: bcrypt.cpp:137

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

Check a previously created password hash

Parameters

  pass | the password to check against
  hash | the 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(), and Botan_FFI::ffi_guard_thunk().

179 {
180 #if defined(BOTAN_HAS_BCRYPT)
181 return ffi_guard_thunk(__func__, [=]() -> int {
182 return Botan::check_bcrypt(pass, hash) ? BOTAN_FFI_SUCCESS : BOTAN_FFI_INVALID_VERIFIER;
183 });
184 #else
185 return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
186 #endif
187 }
Botan_FFI::ffi_guard_thunk
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:89
BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:82
BOTAN_FFI_SUCCESS
Definition: ffi.h:62
Botan::check_bcrypt
bool check_bcrypt(const std::string &pass, const std::string &hash)
Definition: bcrypt.cpp:155
BOTAN_FFI_INVALID_VERIFIER
Definition: ffi.h:63
hash
MechanismType hash
Definition: p11_mechanism.cpp:64

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_algo | KDF algorithm, e.g., "SP800-56C"
  out | buffer holding the derived key, must be of length out_len
  out_len | the desired output length in bytes
  secret | the secret input
  secret_len | size of secret in bytes
  salt | a diversifier
  salt_len | size of salt in bytes
  label | purpose for the derived keying material
  label_len | size 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(), and Botan::get_kdf().

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 }
Botan_FFI::ffi_guard_thunk
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:89
Botan::get_kdf
KDF * get_kdf(const std::string &algo_spec)
Definition: kdf.cpp:241
salt_len
size_t salt_len
Definition: x509_obj.cpp:25
BOTAN_FFI_SUCCESS
Definition: ffi.h:62

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().

25 {
26 return botan_pwdhash(algo,
27 iterations,
28 0,
29 0,
30 out, out_len,
31 pass, 0,
32 salt, salt_len);
33 }
salt_len
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: ffi_kdf.cpp:52

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_algo | PBKDF algorithm, e.g., "PBKDF2(SHA-256)"
  out | buffer to store the derived key, must be of out_len bytes
  out_len | the desired length of the key to produce
  passphrase | the password to derive the key from
  salt | a randomly chosen salt
  salt_len | length of salt in bytes
  milliseconds_to_run | if iterations is zero, then instead the PBKDF is run until milliseconds_to_run milliseconds has passed
  out_iterations_used | set 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().

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 }
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: ffi_kdf.cpp:86
salt_len
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 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(), and Botan_FFI::ffi_guard_thunk().

Referenced by botan_pbkdf(), and botan_scrypt().

63 {
64 if(algo == nullptr || password == nullptr)
65 return BOTAN_FFI_ERROR_NULL_POINTER;
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)
74 return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
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 }
Botan_FFI::ffi_guard_thunk
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:89
BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:82
BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:76
salt_len
size_t salt_len
Definition: x509_obj.cpp:25
Botan::PasswordHashFamily::create
static std::unique_ptr< PasswordHashFamily > create(const std::string &algo_spec, const std::string &provider="")
Definition: pwdhash.cpp:33
BOTAN_FFI_SUCCESS
Definition: ffi.h:62

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(), and Botan_FFI::ffi_guard_thunk().

Referenced by botan_pbkdf_timed().

98 {
99 if(algo == nullptr || password == nullptr)
100 return BOTAN_FFI_ERROR_NULL_POINTER;
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)
110 return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
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 }
Botan_FFI::ffi_guard_thunk
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:89
BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:82
BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:76
salt_len
size_t salt_len
Definition: x509_obj.cpp:25
Botan::PasswordHashFamily::create
static std::unique_ptr< PasswordHashFamily > create(const std::string &algo_spec, const std::string &provider="")
Definition: pwdhash.cpp:33
BOTAN_FFI_SUCCESS
Definition: ffi.h:62

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().

146 {
147 return botan_pwdhash("Scrypt", N, r, p,
148 out, out_len,
149 password, 0,
150 salt, salt_len);
151 }
salt_len
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: ffi_kdf.cpp:52