[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #include "crypto/symmetric_key.h" |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 6 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 9 | |
| 10 | #include <algorithm> |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 11 | #include <utility> |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 12 | |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 13 | #include "base/logging.h" |
[email protected] | 0d8db08 | 2013-06-11 07:27:01 | [diff] [blame] | 14 | #include "base/strings/string_util.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 15 | #include "crypto/openssl_util.h" |
tfarina | 29a3a174 | 2016-10-28 18:47:33 | [diff] [blame] | 16 | #include "third_party/boringssl/src/include/openssl/evp.h" |
| 17 | #include "third_party/boringssl/src/include/openssl/rand.h" |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 18 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 19 | namespace crypto { |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 20 | |
| 21 | SymmetricKey::~SymmetricKey() { |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 22 | std::fill(key_.begin(), key_.end(), '\0'); // Zero out the confidential key. |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | // static |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 26 | std::unique_ptr<SymmetricKey> SymmetricKey::GenerateRandomKey( |
| 27 | Algorithm algorithm, |
| 28 | size_t key_size_in_bits) { |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 29 | DCHECK_EQ(AES, algorithm); |
[email protected] | a534bab | 2014-07-25 21:04:15 | [diff] [blame] | 30 | |
| 31 | // Whitelist supported key sizes to avoid accidentaly relying on |
| 32 | // algorithms available in NSS but not BoringSSL and vice |
| 33 | // versa. Note that BoringSSL does not support AES-192. |
| 34 | if (key_size_in_bits != 128 && key_size_in_bits != 256) |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 35 | return nullptr; |
[email protected] | a534bab | 2014-07-25 21:04:15 | [diff] [blame] | 36 | |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 37 | size_t key_size_in_bytes = key_size_in_bits / 8; |
| 38 | DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8); |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 39 | |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 40 | if (key_size_in_bytes == 0) |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 41 | return nullptr; |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 42 | |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 43 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 44 | std::unique_ptr<SymmetricKey> key(new SymmetricKey); |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 45 | uint8_t* key_data = reinterpret_cast<uint8_t*>( |
Brett Wilson | e3c4d1a | 2015-07-07 23:38:09 | [diff] [blame] | 46 | base::WriteInto(&key->key_, key_size_in_bytes + 1)); |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 47 | |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 48 | int rv = RAND_bytes(key_data, static_cast<int>(key_size_in_bytes)); |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 49 | return rv == 1 ? std::move(key) : nullptr; |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // static |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 53 | std::unique_ptr<SymmetricKey> SymmetricKey::DeriveKeyFromPassword( |
| 54 | Algorithm algorithm, |
| 55 | const std::string& password, |
| 56 | const std::string& salt, |
| 57 | size_t iterations, |
| 58 | size_t key_size_in_bits) { |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 59 | DCHECK(algorithm == AES || algorithm == HMAC_SHA1); |
[email protected] | a534bab | 2014-07-25 21:04:15 | [diff] [blame] | 60 | |
| 61 | if (algorithm == AES) { |
| 62 | // Whitelist supported key sizes to avoid accidentaly relying on |
| 63 | // algorithms available in NSS but not BoringSSL and vice |
| 64 | // versa. Note that BoringSSL does not support AES-192. |
| 65 | if (key_size_in_bits != 128 && key_size_in_bits != 256) |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 66 | return nullptr; |
[email protected] | a534bab | 2014-07-25 21:04:15 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 69 | size_t key_size_in_bytes = key_size_in_bits / 8; |
| 70 | DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8); |
| 71 | |
| 72 | if (key_size_in_bytes == 0) |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 73 | return nullptr; |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 74 | |
[email protected] | be796bb | 2010-11-18 15:43:43 | [diff] [blame] | 75 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 76 | std::unique_ptr<SymmetricKey> key(new SymmetricKey); |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 77 | uint8_t* key_data = reinterpret_cast<uint8_t*>( |
Brett Wilson | e3c4d1a | 2015-07-07 23:38:09 | [diff] [blame] | 78 | base::WriteInto(&key->key_, key_size_in_bytes + 1)); |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 79 | int rv = PKCS5_PBKDF2_HMAC_SHA1( |
| 80 | password.data(), password.length(), |
svaldez | 9c64146 | 2016-05-02 20:49:05 | [diff] [blame] | 81 | reinterpret_cast<const uint8_t*>(salt.data()), salt.length(), |
| 82 | static_cast<unsigned>(iterations), |
| 83 | key_size_in_bytes, key_data); |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 84 | return rv == 1 ? std::move(key) : nullptr; |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // static |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 88 | std::unique_ptr<SymmetricKey> SymmetricKey::Import(Algorithm algorithm, |
| 89 | const std::string& raw_key) { |
[email protected] | a534bab | 2014-07-25 21:04:15 | [diff] [blame] | 90 | if (algorithm == AES) { |
| 91 | // Whitelist supported key sizes to avoid accidentaly relying on |
| 92 | // algorithms available in NSS but not BoringSSL and vice |
| 93 | // versa. Note that BoringSSL does not support AES-192. |
| 94 | if (raw_key.size() != 128/8 && raw_key.size() != 256/8) |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 95 | return nullptr; |
[email protected] | a534bab | 2014-07-25 21:04:15 | [diff] [blame] | 96 | } |
| 97 | |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 98 | std::unique_ptr<SymmetricKey> key(new SymmetricKey); |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 99 | key->key_ = raw_key; |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 100 | return key; |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | bool SymmetricKey::GetRawKey(std::string* raw_key) { |
[email protected] | ac0f8be | 2010-11-12 12:03:54 | [diff] [blame] | 104 | *raw_key = key_; |
| 105 | return true; |
[email protected] | 70372d4 | 2010-10-22 13:12:34 | [diff] [blame] | 106 | } |
| 107 | |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 108 | SymmetricKey::SymmetricKey() = default; |
| 109 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 110 | } // namespace crypto |