• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.49
/src/lib/ffi/ffi_kdf.cpp
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/kdf.h>
10
#include <botan/pwdhash.h>
11
#include <botan/internal/ffi_rng.h>
12
#include <botan/internal/ffi_util.h>
13

14
#if defined(BOTAN_HAS_BCRYPT)
15
   #include <botan/bcrypt.h>
16
#endif
17

18
extern "C" {
19

20
using namespace Botan_FFI;
21

22
int botan_pbkdf(const char* algo,
1✔
23
                uint8_t out[],
24
                size_t out_len,
25
                const char* pass,
26
                const uint8_t salt[],
27
                size_t salt_len,
28
                size_t iterations) {
29
   return botan_pwdhash(algo, iterations, 0, 0, out, out_len, pass, 0, salt, salt_len);
1✔
30
}
31

32
int botan_pbkdf_timed(const char* algo,
2✔
33
                      uint8_t out[],
34
                      size_t out_len,
35
                      const char* password,
36
                      const uint8_t salt[],
37
                      size_t salt_len,
38
                      size_t ms_to_run,
39
                      size_t* iterations_used) {
40
   return botan_pwdhash_timed(algo,
2✔
41
                              static_cast<uint32_t>(ms_to_run),
42
                              iterations_used,
43
                              nullptr,
44
                              nullptr,
45
                              out,
46
                              out_len,
47
                              password,
48
                              0,
49
                              salt,
50
                              salt_len);
2✔
51
}
52

53
int botan_pwdhash(const char* algo,
7✔
54
                  size_t param1,
55
                  size_t param2,
56
                  size_t param3,
57
                  uint8_t out[],
58
                  size_t out_len,
59
                  const char* password,
60
                  size_t password_len,
61
                  const uint8_t salt[],
62
                  size_t salt_len) {
63
   if(algo == nullptr || password == nullptr)
7✔
64
      return BOTAN_FFI_ERROR_NULL_POINTER;
65

66
   if(password_len == 0)
7✔
67
      password_len = std::strlen(password);
4✔
68

69
   return ffi_guard_thunk(__func__, [=]() -> int {
14✔
70
      auto pwdhash_fam = Botan::PasswordHashFamily::create(algo);
7✔
71

72
      if(!pwdhash_fam)
7✔
73
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
74

75
      auto pwdhash = pwdhash_fam->from_params(param1, param2, param3);
7✔
76

77
      pwdhash->derive_key(out, out_len, password, password_len, salt, salt_len);
7✔
78

79
      return BOTAN_FFI_SUCCESS;
7✔
80
   });
14✔
81
}
82

83
int botan_pwdhash_timed(const char* algo,
4✔
84
                        uint32_t msec,
85
                        size_t* param1,
86
                        size_t* param2,
87
                        size_t* param3,
88
                        uint8_t out[],
89
                        size_t out_len,
90
                        const char* password,
91
                        size_t password_len,
92
                        const uint8_t salt[],
93
                        size_t salt_len) {
94
   if(algo == nullptr || password == nullptr)
4✔
95
      return BOTAN_FFI_ERROR_NULL_POINTER;
96

97
   if(password_len == 0)
4✔
98
      password_len = std::strlen(password);
2✔
99

100
   return ffi_guard_thunk(__func__, [=]() -> int {
8✔
101
      auto pwdhash_fam = Botan::PasswordHashFamily::create(algo);
4✔
102

103
      if(!pwdhash_fam)
4✔
104
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
105

106
      auto pwdhash = pwdhash_fam->tune(out_len, std::chrono::milliseconds(msec));
8✔
107

108
      if(param1)
4✔
109
         *param1 = pwdhash->iterations();
4✔
110
      if(param2)
4✔
111
         *param2 = pwdhash->parallelism();
1✔
112
      if(param3)
4✔
113
         *param3 = pwdhash->memory_param();
1✔
114

115
      pwdhash->derive_key(out, out_len, password, password_len, salt, salt_len);
4✔
116

117
      return BOTAN_FFI_SUCCESS;
4✔
118
   });
8✔
119
}
120

121
int botan_kdf(const char* kdf_algo,
2✔
122
              uint8_t out[],
123
              size_t out_len,
124
              const uint8_t secret[],
125
              size_t secret_len,
126
              const uint8_t salt[],
127
              size_t salt_len,
128
              const uint8_t label[],
129
              size_t label_len) {
130
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
131
      auto kdf = Botan::KDF::create_or_throw(kdf_algo);
4✔
132
      kdf->kdf(out, out_len, secret, secret_len, salt, salt_len, label, label_len);
2✔
133
      return BOTAN_FFI_SUCCESS;
2✔
134
   });
4✔
135
}
136

137
int botan_scrypt(uint8_t out[],
1✔
138
                 size_t out_len,
139
                 const char* password,
140
                 const uint8_t salt[],
141
                 size_t salt_len,
142
                 size_t N,
143
                 size_t r,
144
                 size_t p) {
145
   return botan_pwdhash("Scrypt", N, r, p, out, out_len, password, 0, salt, salt_len);
1✔
146
}
147

148
int botan_bcrypt_generate(
2✔
149
   uint8_t* out, size_t* out_len, const char* pass, botan_rng_t rng_obj, size_t wf, uint32_t flags) {
150
#if defined(BOTAN_HAS_BCRYPT)
151
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
152
      if(out == nullptr || out_len == nullptr || pass == nullptr)
4✔
153
         return BOTAN_FFI_ERROR_NULL_POINTER;
154

155
      if(flags != 0)
2✔
156
         return BOTAN_FFI_ERROR_BAD_FLAG;
157

158
      if(wf < 4 || wf > 18)
2✔
159
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
160

161
      if(*out_len < 61) {
2✔
162
         *out_len = 61;
×
163
         return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE;
×
164
      }
165

166
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
2✔
167
      const std::string bcrypt = Botan::generate_bcrypt(pass, rng, static_cast<uint16_t>(wf));
2✔
168
      return write_str_output(out, out_len, bcrypt);
2✔
169
   });
4✔
170
#else
171
   BOTAN_UNUSED(out, out_len, pass, rng_obj, wf, flags);
172
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
173
#endif
174
}
175

176
int botan_bcrypt_is_valid(const char* pass, const char* hash) {
5✔
177
#if defined(BOTAN_HAS_BCRYPT)
178
   return ffi_guard_thunk(__func__, [=]() -> int {
5✔
179
      return Botan::check_bcrypt(pass, hash) ? BOTAN_FFI_SUCCESS : BOTAN_FFI_INVALID_VERIFIER;
5✔
180
   });
5✔
181
#else
182
   BOTAN_UNUSED(pass, hash);
183
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
184
#endif
185
}
186
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc