Skip to content

Commit 58b1bdf

Browse files
committed
Pass hash size to pbkdf2
1 parent a068ae9 commit 58b1bdf

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/libmongoc/src/mongoc/mongoc-crypto-cng.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,13 @@ _bcrypt_derive_key_pbkdf2 (BCRYPT_ALG_HANDLE prf,
193193
#else
194194
/* Manually salts password if BCryptDeriveKeyPBKDF2 is unavailable */
195195
static bool
196-
_bcrypt_derive_key_pbkdf2 (mongoc_crypto_t *crypto,
196+
_bcrypt_derive_key_pbkdf2 (BCRYPT_ALG_HANDLE algorithm,
197197
const char *password,
198198
size_t password_len,
199199
const uint8_t *salt,
200200
size_t salt_len,
201201
uint32_t iterations,
202+
size_t output_len,
202203
unsigned char *output)
203204
{
204205
uint8_t intermediate_digest[MONGOC_SCRAM_HASH_MAX_SIZE];
@@ -211,11 +212,16 @@ _bcrypt_derive_key_pbkdf2 (mongoc_crypto_t *crypto,
211212
start_key[salt_len + 2] = 0;
212213
start_key[salt_len + 3] = 1;
213214

214-
crypto->hmac (crypto, password, password_len, start_key, hash_size, output);
215+
if (!_mongoc_crypto_cng_hmac_or_hash (algorithm, password, password_len, start_key, hash_size, output)) {
216+
return false;
217+
}
215218
memcpy (intermediate_digest, output, hash_size);
216219

217220
for (uint32_t i = 2u; i <= iterations; i++) {
218-
crypto->hmac (crypto, password, password_len, intermediate_digest, hash_size, intermediate_digest);
221+
if (!_mongoc_crypto_cng_hmac_or_hash (
222+
algorithm, password, password_len, intermediate_digest, hash_size, output)) {
223+
return false;
224+
}
219225

220226
for (int k = 0; k < hash_size; k++) {
221227
output[k] ^= intermediate_digest[k];
@@ -235,12 +241,8 @@ mongoc_crypto_cng_pbkdf2_hmac_sha1 (mongoc_crypto_t *crypto,
235241
size_t output_len,
236242
unsigned char *output)
237243
{
238-
#if defined(MONGOC_HAVE_BCRYPT_PBKDF2)
239244
return _bcrypt_derive_key_pbkdf2 (
240245
_sha1_hmac_algo, password, password_len, salt, salt_len, iterations, output_len, output);
241-
#else
242-
return _bcrypt_derive_key_pbkdf2 (crypto, password, password_len, salt, salt_len, iterations, output);
243-
#endif
244246
}
245247

246248
void
@@ -284,12 +286,8 @@ mongoc_crypto_cng_pbkdf2_hmac_sha256 (mongoc_crypto_t *crypto,
284286
size_t output_len,
285287
unsigned char *output)
286288
{
287-
#if defined(MONGOC_HAVE_BCRYPT_PBKDF2)
288289
return _bcrypt_derive_key_pbkdf2 (
289290
_sha256_hmac_algo, password, password_len, salt, salt_len, iterations, output_len, output);
290-
#else
291-
return _bcrypt_derive_key_pbkdf2 (crypto, password, password_len, salt, salt_len, iterations, output);
292-
#endif
293291
}
294292

295293
void

0 commit comments

Comments
 (0)