Botan 3.12.0
Crypto and TLS for C&
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
9#include <botan/assert.h>
10#include <botan/kdf.h>
11#include <botan/pwdhash.h>
12#include <botan/internal/ffi_rng.h>
13#include <botan/internal/ffi_util.h>
14
15#if defined(BOTAN_HAS_BCRYPT)
16 #include <botan/bcrypt.h>
17#endif
18
19extern "C" {
20
21using namespace Botan_FFI;
22
23int botan_pbkdf(const char* algo,
24 uint8_t out[],
25 size_t out_len,
26 const char* pass,
27 const uint8_t salt[],
28 size_t salt_len,
29 size_t iterations) {
30 return botan_pwdhash(algo, iterations, 0, 0, out, out_len, pass, 0, salt, salt_len);
31}
32
33int botan_pbkdf_timed(const char* algo,
34 uint8_t out[],
35 size_t out_len,
36 const char* password,
37 const uint8_t salt[],
38 size_t salt_len,
39 size_t ms_to_run,
40 size_t* iterations_used) {
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}
53
54int botan_pwdhash(const char* algo,
55 size_t param1,
56 size_t param2,
57 size_t param3,
58 uint8_t out[],
59 size_t out_len,
60 const char* password,
61 size_t password_len,
62 const uint8_t salt[],
63 size_t salt_len) {
64 if(algo == nullptr || password == nullptr) {
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}
92
93int botan_pwdhash_timed(const char* algo,
94 uint32_t msec,
95 size_t* param1,
96 size_t* param2,
97 size_t* param3,
98 uint8_t out[],
99 size_t out_len,
100 const char* password,
101 size_t password_len,
102 const uint8_t salt[],
103 size_t salt_len) {
104 if(algo == nullptr || password == nullptr) {
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}
142
143int botan_kdf(const char* kdf_algo,
144 uint8_t out[],
145 size_t out_len,
146 const uint8_t secret[],
147 size_t secret_len,
148 const uint8_t salt[],
149 size_t salt_len,
150 const uint8_t label[],
151 size_t label_len) {
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}
165
166int botan_scrypt(uint8_t out[],
167 size_t out_len,
168 const char* password,
169 const uint8_t salt[],
170 size_t salt_len,
171 size_t N,
172 size_t r,
173 size_t p) {
174 return botan_pwdhash("Scrypt", N, r, p, out, out_len, password, 0, salt, salt_len);
175}
176
178 uint8_t* out, size_t* out_len, const char* pass, botan_rng_t rng_obj, size_t wf, uint32_t flags) {
179#if defined(BOTAN_HAS_BCRYPT)
180 return ffi_guard_thunk(__func__, [=]() -> int {
181 if(out == nullptr || out_len == nullptr || pass == nullptr) {
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}
208
209int botan_bcrypt_is_valid(const char* pass, const char* hash) {
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}
222}
#define BOTAN_UNUSED
Definition assert.h:144
static std::unique_ptr< KDF > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition kdf.cpp:204
static std::unique_ptr< PasswordHashFamily > create(std::string_view algo_spec, std::string_view provider="")
Definition pwdhash.cpp:54
struct botan_rng_struct * botan_rng_t
Definition ffi.h:291
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:140
@ BOTAN_FFI_INVALID_VERIFIER
Definition ffi.h:118
@ BOTAN_FFI_ERROR_BAD_FLAG
Definition ffi.h:132
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:133
@ BOTAN_FFI_SUCCESS
Definition ffi.h:116
@ BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE
Definition ffi.h:124
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:134
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 ffi_kdf.cpp:23
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
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)
Definition ffi_kdf.cpp:33
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)
Definition ffi_kdf.cpp:166
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:177
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:143
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
int botan_bcrypt_is_valid(const char *pass, const char *hash)
Definition ffi_kdf.cpp:209
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:268
std::string generate_bcrypt(std::string_view pass, RandomNumberGenerator &rng, uint16_t work_factor, char version)
Definition bcrypt.cpp:146
bool check_bcrypt(std::string_view pass, std::string_view hash)
Definition bcrypt.cpp:161