Coverage Report

Created: 2026-10-31 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/evp/pbkdf.cc
Line
Count
Source
1
// Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <openssl/evp.h>
16
17
#include <string.h>
18
19
#include <openssl/err.h>
20
#include <openssl/hmac.h>
21
22
#include "../internal.h"
23
24
25
using namespace bssl;
26
27
int PKCS5_PBKDF2_HMAC(const char *password, size_t password_len,
28
                      const uint8_t *salt, size_t salt_len, uint32_t iterations,
29
2
                      const EVP_MD *digest, size_t key_len, uint8_t *out_key) {
30
  // See RFC 8018, section 5.2.
31
2
  ScopedHMAC_CTX hctx;
32
2
  if (!HMAC_Init_ex(hctx.get(), password, password_len, digest, nullptr)) {
33
0
    return 0;
34
0
  }
35
36
2
  uint32_t i = 1;
37
2
  size_t md_len = EVP_MD_size(digest);
38
2
  if (key_len > ((uint64_t{1} << 32) - 1) * md_len) {
39
0
    OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_SECRET_LENGTH);
40
0
    return 0;
41
0
  }
42
4
  while (key_len > 0) {
43
2
    size_t todo = md_len;
44
2
    if (todo > key_len) {
45
2
      todo = key_len;
46
2
    }
47
48
2
    uint8_t i_buf[4];
49
2
    i_buf[0] = (uint8_t)((i >> 24) & 0xff);
50
2
    i_buf[1] = (uint8_t)((i >> 16) & 0xff);
51
2
    i_buf[2] = (uint8_t)((i >> 8) & 0xff);
52
2
    i_buf[3] = (uint8_t)(i & 0xff);
53
54
    // Compute U_1.
55
2
    uint8_t digest_tmp[EVP_MAX_MD_SIZE];
56
2
    if (!HMAC_Init_ex(hctx.get(), nullptr, 0, nullptr, nullptr) ||
57
2
        !HMAC_Update(hctx.get(), salt, salt_len) ||
58
2
        !HMAC_Update(hctx.get(), i_buf, 4) ||
59
2
        !HMAC_Final(hctx.get(), digest_tmp, nullptr)) {
60
0
      return 0;
61
0
    }
62
63
2
    OPENSSL_memcpy(out_key, digest_tmp, todo);
64
4.09k
    for (uint32_t j = 1; j < iterations; j++) {
65
      // Compute the remaining U_* values and XOR.
66
4.09k
      if (!HMAC_Init_ex(hctx.get(), nullptr, 0, nullptr, nullptr) ||
67
4.09k
          !HMAC_Update(hctx.get(), digest_tmp, md_len) ||
68
4.09k
          !HMAC_Final(hctx.get(), digest_tmp, nullptr)) {
69
0
        return 0;
70
0
      }
71
69.5k
      for (size_t k = 0; k < todo; k++) {
72
65.5k
        out_key[k] ^= digest_tmp[k];
73
65.5k
      }
74
4.09k
    }
75
76
2
    key_len -= todo;
77
2
    out_key += todo;
78
2
    i++;
79
2
  }
80
81
  // RFC 8018 describes iterations (c) as being a "positive integer", so a
82
  // value of 0 is an error.
83
  //
84
  // Unfortunately not all consumers of PKCS5_PBKDF2_HMAC() check their return
85
  // value, expecting it to succeed and unconditionally using `out_key`.  As a
86
  // precaution for such callsites in external code, the old behavior of
87
  // iterations < 1 being treated as iterations == 1 is preserved, but
88
  // additionally an error result is returned.
89
  //
90
  // TODO(eroman): Figure out how to remove this compatibility hack, or change
91
  // the default to something more sensible like 2048.
92
2
  if (iterations == 0) {
93
0
    return 0;
94
0
  }
95
96
2
  return 1;
97
2
}
98
99
int PKCS5_PBKDF2_HMAC_SHA1(const char *password, size_t password_len,
100
                           const uint8_t *salt, size_t salt_len,
101
                           uint32_t iterations, size_t key_len,
102
0
                           uint8_t *out_key) {
103
0
  return PKCS5_PBKDF2_HMAC(password, password_len, salt, salt_len, iterations,
104
0
                           EVP_sha1(), key_len, out_key);
105
0
}