Botan 3.13.0
Crypto and TLS for C&
ffi_kdf.cpp File Reference
#include <botan/ffi.h>
#include <botan/assert.h>
#include <botan/kdf.h>
#include <botan/pwdhash.h>
#include <botan/internal/ffi_rng.h>
#include <botan/internal/ffi_util.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
Random Number Generatorsa 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$...

TOD(Botan4) this should use char for the type of out

Definition at line 177 of file ffi_kdf.cpp.

178 {
179#if defined(BOTAN_HAS_BCRYPT)
180 return ffi_guard_thunk(__func__, [=]() -> int {
181 if(any_null_pointers(out, out_len, pass)) {
183 }
184
185 if(flags != 0) {
187 }
188
189 if(wf < 4 || wf > 18) {
191 }
192
193 if(*out_len < 61) {
194 *out_len = 61;
196 }
197
199 const std::string bcrypt = Botan::generate_bcrypt(pass, rng, static_cast<uint16_t>(wf));
200 // TODO(Botan4) change the type of out and remove this cast
201 return write_str_output(reinterpret_cast<char*>(out), out_len, bcrypt);
202 });
203#else
204 BOTAN_UNUSED(out, out_len, pass, rng_obj, wf, flags);
206#endif
207}
#define BOTAN_UNUSED
Definition assert.h:144
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:130
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:131
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
Definition ffi.h:122
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:132
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:79
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:95
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:54
int write_str_output(char out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:271
std::string generate_bcrypt(std::string_view pass, RandomNumberGenerator &rng, uint16_t work_factor, char version)
Definition bcrypt.cpp:148

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_BAD_FLAG, BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE, BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_UNUSED, Botan_FFI::ffi_guard_thunk(), Botan::generate_bcrypt(), Botan_FFI::safe_get(), and Botan_FFI::write_str_output().

◆ 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
Hashesthe 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 209 of file ffi_kdf.cpp.

209 {
210 if(any_null_pointers(pass, hash)) {
212 }
213#if defined(BOTAN_HAS_BCRYPT)
214 return ffi_guard_thunk(__func__, [=]() -> int {
216 });
217#else
218 BOTAN_UNUSED(pass, hash);
220#endif
221}
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:116
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
bool check_bcrypt(std::string_view pass, std::string_view hash)
Definition bcrypt.cpp:163

References Botan_FFI::any_null_pointers(), BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_INVALID_VERIFIER, BOTAN_FFI_SUCCESS, BOTAN_UNUSED, Botan::check_bcrypt(), and Botan_FFI::ffi_guard_thunk().

◆ 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 143 of file ffi_kdf.cpp.

151 {
152 if(kdf_algo == nullptr) {
154 }
155 if((out_len > 0 && out == nullptr) || (secret_len > 0 && secret == nullptr) || (salt_len > 0 && salt == nullptr) ||
156 (label_len > 0 && label == nullptr)) {
158 }
159 return ffi_guard_thunk(__func__, [=]() -> int {
160 auto kdf = Botan::KDF::create_or_throw(kdf_algo);
161 kdf->kdf(out, out_len, secret, secret_len, salt, salt_len, label, label_len);
162 return BOTAN_FFI_SUCCESS;
163 });
164}
static std::unique_ptr< KDF > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition kdf.cpp:208

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, Botan::KDF::create_or_throw(), and Botan_FFI::ffi_guard_thunk().

◆ botan_pbkdf()

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 )

Derive a key from a passphrase for a number of iterations

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
iterationsthe number of iterations to use (use 10K or more)
Returns
0 on success, a negative value on failure

Deprecated: use botan_pwdhash(pbkdf_algo, iterations, 0, 0, out, out_len, passphrase, 0, salt, salt_len);

Definition at line 23 of file ffi_kdf.cpp.

29 {
30 return botan_pwdhash(algo, iterations, 0, 0, out, out_len, pass, 0, salt, salt_len);
31}
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:54

References botan_pwdhash().

◆ 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 33 of file ffi_kdf.cpp.

40 {
41 return botan_pwdhash_timed(algo,
42 static_cast<uint32_t>(ms_to_run),
43 iterations_used,
44 nullptr,
45 nullptr,
46 out,
47 out_len,
48 password,
49 0,
50 salt,
51 salt_len);
52}
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:93

References botan_pwdhash_timed().

◆ 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 * passphrase,
size_t passphrase_len,
const uint8_t salt[],
size_t salt_len )

Derive a key from a passphrase

Parameters
algoPBKDF algorithm, e.g., "PBKDF2(SHA-256)" or "Scrypt"
param1the first PBKDF algorithm parameter
param2the second PBKDF algorithm parameter (may be zero if unneeded)
param3the third PBKDF algorithm parameter (may be zero if unneeded)
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
passphrase_lenif > 0, specifies length of password. If len == 0, then strlen will be called on passphrase to compute the length.
salta randomly chosen salt
salt_lenlength of salt in bytes
Returns
0 on success, a negative value on failure

Definition at line 54 of file ffi_kdf.cpp.

63 {
64 if(any_null_pointers(algo, password)) {
66 }
67 if(out_len > 0 && out == nullptr) {
69 }
70 if(salt_len > 0 && salt == nullptr) {
72 }
73
74 if(password_len == 0) {
75 password_len = std::strlen(password);
76 }
77
78 return ffi_guard_thunk(__func__, [=]() -> int {
79 auto pwdhash_fam = Botan::PasswordHashFamily::create(algo);
80
81 if(!pwdhash_fam) {
83 }
84
85 auto pwdhash = pwdhash_fam->from_params(param1, param2, param3);
86
87 pwdhash->derive_key(out, out_len, password, password_len, salt, salt_len);
88
89 return BOTAN_FFI_SUCCESS;
90 });
91}
static std::unique_ptr< PasswordHashFamily > create(std::string_view algo_spec, std::string_view provider="")
Definition pwdhash.cpp:58

References Botan_FFI::any_null_pointers(), 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().

◆ 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 * passphrase,
size_t passphrase_len,
const uint8_t salt[],
size_t salt_len )

Derive a key from a passphrase, choosing parameters to hit a target runtime

Parameters
algoPBKDF algorithm, e.g., "Scrypt" or "PBKDF2(SHA-256)"
msecthe desired runtime in milliseconds
param1will be set to the first password hash parameter
param2will be set to the second password hash parameter
param3will be set to the third password hash parameter
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
passphrase_lenif > 0, specifies length of password. If len == 0, then strlen will be called on passphrase to compute the length.
salta randomly chosen salt
salt_lenlength of salt in bytes
Returns
0 on success, a negative value on failure

Definition at line 93 of file ffi_kdf.cpp.

103 {
104 if(any_null_pointers(algo, password)) {
106 }
107 if(out_len > 0 && out == nullptr) {
109 }
110 if(salt_len > 0 && salt == nullptr) {
112 }
113
114 if(password_len == 0) {
115 password_len = std::strlen(password);
116 }
117
118 return ffi_guard_thunk(__func__, [=]() -> int {
119 auto pwdhash_fam = Botan::PasswordHashFamily::create(algo);
120
121 if(!pwdhash_fam) {
123 }
124
125 auto pwdhash = pwdhash_fam->tune_params(out_len, msec);
126
127 if(param1 != nullptr) {
128 *param1 = pwdhash->iterations();
129 }
130 if(param2 != nullptr) {
131 *param2 = pwdhash->parallelism();
132 }
133 if(param3 != nullptr) {
134 *param3 = pwdhash->memory_param();
135 }
136
137 pwdhash->derive_key(out, out_len, password, password_len, salt, salt_len);
138
139 return BOTAN_FFI_SUCCESS;
140 });
141}

References Botan_FFI::any_null_pointers(), 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().

◆ 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 166 of file ffi_kdf.cpp.

173 {
174 return botan_pwdhash("Scrypt", N, r, p, out, out_len, password, 0, salt, salt_len);
175}

References botan_pwdhash().