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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 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

67
   if(password_len == 0) {
7✔
68
      password_len = std::strlen(password);
4✔
69
   }
70

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

74
      if(!pwdhash_fam) {
7✔
75
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
76
      }
77

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

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

82
      return BOTAN_FFI_SUCCESS;
7✔
83
   });
14✔
84
}
85

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

101
   if(password_len == 0) {
4✔
102
      password_len = std::strlen(password);
2✔
103
   }
104

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

108
      if(!pwdhash_fam) {
4✔
109
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
110
      }
111

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

114
      if(param1) {
4✔
115
         *param1 = pwdhash->iterations();
4✔
116
      }
117
      if(param2) {
4✔
118
         *param2 = pwdhash->parallelism();
1✔
119
      }
120
      if(param3) {
4✔
121
         *param3 = pwdhash->memory_param();
1✔
122
      }
123

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

126
      return BOTAN_FFI_SUCCESS;
4✔
127
   });
8✔
128
}
129

130
int botan_kdf(const char* kdf_algo,
2✔
131
              uint8_t out[],
132
              size_t out_len,
133
              const uint8_t secret[],
134
              size_t secret_len,
135
              const uint8_t salt[],
136
              size_t salt_len,
137
              const uint8_t label[],
138
              size_t label_len) {
139
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
140
      auto kdf = Botan::KDF::create_or_throw(kdf_algo);
2✔
141
      kdf->kdf(out, out_len, secret, secret_len, salt, salt_len, label, label_len);
2✔
142
      return BOTAN_FFI_SUCCESS;
2✔
143
   });
4✔
144
}
145

146
int botan_scrypt(uint8_t out[],
1✔
147
                 size_t out_len,
148
                 const char* password,
149
                 const uint8_t salt[],
150
                 size_t salt_len,
151
                 size_t N,
152
                 size_t r,
153
                 size_t p) {
154
   return botan_pwdhash("Scrypt", N, r, p, out, out_len, password, 0, salt, salt_len);
1✔
155
}
156

157
int botan_bcrypt_generate(
2✔
158
   uint8_t* out, size_t* out_len, const char* pass, botan_rng_t rng_obj, size_t wf, uint32_t flags) {
159
#if defined(BOTAN_HAS_BCRYPT)
160
   return ffi_guard_thunk(__func__, [=]() -> int {
2✔
161
      if(out == nullptr || out_len == nullptr || pass == nullptr) {
4✔
162
         return BOTAN_FFI_ERROR_NULL_POINTER;
163
      }
164

165
      if(flags != 0) {
2✔
166
         return BOTAN_FFI_ERROR_BAD_FLAG;
167
      }
168

169
      if(wf < 4 || wf > 18) {
2✔
170
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
171
      }
172

173
      if(*out_len < 61) {
2✔
174
         *out_len = 61;
×
175
         return BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE;
×
176
      }
177

178
      Botan::RandomNumberGenerator& rng = safe_get(rng_obj);
2✔
179
      const std::string bcrypt = Botan::generate_bcrypt(pass, rng, static_cast<uint16_t>(wf));
2✔
180
      return write_str_output(out, out_len, bcrypt);
2✔
181
   });
4✔
182
#else
183
   BOTAN_UNUSED(out, out_len, pass, rng_obj, wf, flags);
184
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
185
#endif
186
}
187

188
int botan_bcrypt_is_valid(const char* pass, const char* hash) {
5✔
189
#if defined(BOTAN_HAS_BCRYPT)
190
   return ffi_guard_thunk(__func__, [=]() -> int {
5✔
191
      return Botan::check_bcrypt(pass, hash) ? BOTAN_FFI_SUCCESS : BOTAN_FFI_INVALID_VERIFIER;
5✔
192
   });
5✔
193
#else
194
   BOTAN_UNUSED(pass, hash);
195
   return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
196
#endif
197
}
198
}
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