| 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/hmac.h> |
| 20 |
| 21 | #include "../internal.h" |
| 22 |
| 23 |
| 24 | int PKCS5_PBKDF2_HMAC(const char *password, size_t password_len, |
| 25 | const uint8_t *salt, size_t salt_len, uint32_t iterations, |
| 26 | const EVP_MD *digest, size_t key_len, uint8_t *out_key) { |
| 27 | // See RFC 8018, section 5.2. |
| 28 | bssl::ScopedHMAC_CTX hctx; |
| 29 | if (!HMAC_Init_ex(hctx.get(), password, password_len, digest, NULL)) { |
| 30 | return 0; |
| 31 | } |
| 32 |
| 33 | uint32_t i = 1; |
| 34 | size_t md_len = EVP_MD_size(digest); |
| 35 | while (key_len > 0) { |
| 36 | size_t todo = md_len; |
| 37 | if (todo > key_len) { |
| 38 | todo = key_len; |
| 39 | } |
| 40 |
| 41 | uint8_t i_buf[4]; |
| 42 | i_buf[0] = (uint8_t)((i >> 24) & 0xff); |
| 43 | i_buf[1] = (uint8_t)((i >> 16) & 0xff); |
| 44 | i_buf[2] = (uint8_t)((i >> 8) & 0xff); |
| 45 | i_buf[3] = (uint8_t)(i & 0xff); |
| 46 |
| 47 | // Compute U_1. |
| 48 | uint8_t digest_tmp[EVP_MAX_MD_SIZE]; |
| 49 | if (!HMAC_Init_ex(hctx.get(), NULL, 0, NULL, NULL) || |
| 50 | !HMAC_Update(hctx.get(), salt, salt_len) || |
| 51 | !HMAC_Update(hctx.get(), i_buf, 4) || |
| 52 | !HMAC_Final(hctx.get(), digest_tmp, NULL)) { |
| 53 | return 0; |
| 54 | } |
| 55 |
| 56 | OPENSSL_memcpy(out_key, digest_tmp, todo); |
| 57 | for (uint32_t j = 1; j < iterations; j++) { |
| 58 | // Compute the remaining U_* values and XOR. |
| 59 | if (!HMAC_Init_ex(hctx.get(), NULL, 0, NULL, NULL) || |
| 60 | !HMAC_Update(hctx.get(), digest_tmp, md_len) || |
| 61 | !HMAC_Final(hctx.get(), digest_tmp, NULL)) { |
| 62 | return 0; |
| 63 | } |
| 64 | for (size_t k = 0; k < todo; k++) { |
| 65 | out_key[k] ^= digest_tmp[k]; |
| 66 | } |
| 67 | } |
| 68 |
| 69 | key_len -= todo; |
| 70 | out_key += todo; |
| 71 | i++; |
| 72 | } |
| 73 |
| 74 | // RFC 8018 describes iterations (c) as being a "positive integer", so a |
| 75 | // value of 0 is an error. |
| 76 | // |
| 77 | // Unfortunately not all consumers of PKCS5_PBKDF2_HMAC() check their return |
| 78 | // value, expecting it to succeed and unconditionally using |out_key|. As a |
| 79 | // precaution for such callsites in external code, the old behavior of |
| 80 | // iterations < 1 being treated as iterations == 1 is preserved, but |
| 81 | // additionally an error result is returned. |
| 82 | // |
| 83 | // TODO(eroman): Figure out how to remove this compatibility hack, or change |
| 84 | // the default to something more sensible like 2048. |
| 85 | if (iterations == 0) { |
| 86 | return 0; |
| 87 | } |
| 88 |
| 89 | return 1; |
| 90 | } |
| 91 |
| 92 | int PKCS5_PBKDF2_HMAC_SHA1(const char *password, size_t password_len, |
| 93 | const uint8_t *salt, size_t salt_len, |
| 94 | uint32_t iterations, size_t key_len, |
| 95 | uint8_t *out_key) { |
| 96 | return PKCS5_PBKDF2_HMAC(password, password_len, salt, salt_len, iterations, |
| 97 | EVP_sha1(), key_len, out_key); |
| 98 | } |
| 99 |