/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 | 4 | const EVP_MD *digest, size_t key_len, uint8_t *out_key) { |
30 | | // See RFC 8018, section 5.2. |
31 | 4 | ScopedHMAC_CTX hctx; |
32 | 4 | if (!HMAC_Init_ex(hctx.get(), password, password_len, digest, nullptr)) { |
33 | 0 | return 0; |
34 | 0 | } |
35 | | |
36 | 4 | uint32_t i = 1; |
37 | 4 | size_t md_len = EVP_MD_size(digest); |
38 | 4 | 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 | 8 | while (key_len > 0) { |
43 | 4 | size_t todo = md_len; |
44 | 4 | if (todo > key_len) { |
45 | 4 | todo = key_len; |
46 | 4 | } |
47 | | |
48 | 4 | uint8_t i_buf[4]; |
49 | 4 | i_buf[0] = (uint8_t)((i >> 24) & 0xff); |
50 | 4 | i_buf[1] = (uint8_t)((i >> 16) & 0xff); |
51 | 4 | i_buf[2] = (uint8_t)((i >> 8) & 0xff); |
52 | 4 | i_buf[3] = (uint8_t)(i & 0xff); |
53 | | |
54 | | // Compute U_1. |
55 | 4 | uint8_t digest_tmp[EVP_MAX_MD_SIZE]; |
56 | 4 | if (!HMAC_Init_ex(hctx.get(), nullptr, 0, nullptr, nullptr) || |
57 | 4 | !HMAC_Update(hctx.get(), salt, salt_len) || |
58 | 4 | !HMAC_Update(hctx.get(), i_buf, 4) || |
59 | 4 | !HMAC_Final(hctx.get(), digest_tmp, nullptr)) { |
60 | 0 | return 0; |
61 | 0 | } |
62 | | |
63 | 4 | OPENSSL_memcpy(out_key, digest_tmp, todo); |
64 | 8.19k | for (uint32_t j = 1; j < iterations; j++) { |
65 | | // Compute the remaining U_* values and XOR. |
66 | 8.18k | if (!HMAC_Init_ex(hctx.get(), nullptr, 0, nullptr, nullptr) || |
67 | 8.18k | !HMAC_Update(hctx.get(), digest_tmp, md_len) || |
68 | 8.18k | !HMAC_Final(hctx.get(), digest_tmp, nullptr)) { |
69 | 0 | return 0; |
70 | 0 | } |
71 | 139k | for (size_t k = 0; k < todo; k++) { |
72 | 131k | out_key[k] ^= digest_tmp[k]; |
73 | 131k | } |
74 | 8.18k | } |
75 | | |
76 | 4 | key_len -= todo; |
77 | 4 | out_key += todo; |
78 | 4 | i++; |
79 | 4 | } |
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 | 4 | if (iterations == 0) { |
93 | 0 | return 0; |
94 | 0 | } |
95 | | |
96 | 4 | return 1; |
97 | 4 | } |
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 | } |