• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "crypto/kdf.h"
6 
7 #include "crypto/openssl_util.h"
8 #include "third_party/boringssl/src/include/openssl/evp.h"
9 
10 namespace crypto::kdf {
11 
DeriveKeyPbkdf2HmacSha1(const Pbkdf2HmacSha1Params & params,base::span<const uint8_t> password,base::span<const uint8_t> salt,base::span<uint8_t> result,crypto::SubtlePassKey)12 void DeriveKeyPbkdf2HmacSha1(const Pbkdf2HmacSha1Params& params,
13                              base::span<const uint8_t> password,
14                              base::span<const uint8_t> salt,
15                              base::span<uint8_t> result,
16                              crypto::SubtlePassKey) {
17   OpenSSLErrStackTracer err_tracer(FROM_HERE);
18   int rv = PKCS5_PBKDF2_HMAC_SHA1(
19       base::as_chars(password).data(), password.size(), salt.data(),
20       salt.size(), params.iterations, result.size(), result.data());
21 
22   CHECK_EQ(rv, 1);
23 }
24 
DeriveKeyScrypt(const ScryptParams & params,base::span<const uint8_t> password,base::span<const uint8_t> salt,base::span<uint8_t> result,crypto::SubtlePassKey)25 void DeriveKeyScrypt(const ScryptParams& params,
26                      base::span<const uint8_t> password,
27                      base::span<const uint8_t> salt,
28                      base::span<uint8_t> result,
29                      crypto::SubtlePassKey) {
30   OpenSSLErrStackTracer err_tracer(FROM_HERE);
31   int rv =
32       EVP_PBE_scrypt(reinterpret_cast<const char*>(password.data()),
33                      password.size(), salt.data(), salt.size(), params.cost,
34                      params.block_size, params.parallelization,
35                      params.max_memory_bytes, result.data(), result.size());
36 
37   CHECK_EQ(rv, 1);
38 }
39 
40 }  // namespace crypto::kdf
41