1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "crypto/symmetric_key.h"
13 #include "base/logging.h"
14 #include "base/strings/string_util.h"
15 #include "crypto/openssl_util.h"
16 #include "third_party/boringssl/src/include/openssl/evp.h"
17 #include "third_party/boringssl/src/include/openssl/rand.h"
21 SymmetricKey::~SymmetricKey() {
22 std::fill(key_.begin(), key_.end(), '\0'); // Zero out the confidential key.
26 std::unique_ptr<SymmetricKey> SymmetricKey::GenerateRandomKey(
28 size_t key_size_in_bits) {
29 DCHECK_EQ(AES, algorithm);
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)
37 size_t key_size_in_bytes = key_size_in_bits / 8;
38 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8);
40 if (key_size_in_bytes == 0)
43 OpenSSLErrStackTracer err_tracer(FROM_HERE);
44 std::unique_ptr<SymmetricKey> key(new SymmetricKey);
45 uint8_t* key_data = reinterpret_cast<uint8_t*>(
46 base::WriteInto(&key->key_, key_size_in_bytes + 1));
48 int rv = RAND_bytes(key_data, static_cast<int>(key_size_in_bytes));
49 return rv == 1 ? std::move(key) : nullptr;
53 std::unique_ptr<SymmetricKey> SymmetricKey::DeriveKeyFromPassword(
55 const std::string& password,
56 const std::string& salt,
58 size_t key_size_in_bits) {
59 DCHECK(algorithm == AES || algorithm == HMAC_SHA1);
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)
69 size_t key_size_in_bytes = key_size_in_bits / 8;
70 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8);
72 if (key_size_in_bytes == 0)
75 OpenSSLErrStackTracer err_tracer(FROM_HERE);
76 std::unique_ptr<SymmetricKey> key(new SymmetricKey);
77 uint8_t* key_data = reinterpret_cast<uint8_t*>(
78 base::WriteInto(&key->key_, key_size_in_bytes + 1));
79 int rv = PKCS5_PBKDF2_HMAC_SHA1(
80 password.data(), password.length(),
81 reinterpret_cast<const uint8_t*>(salt.data()), salt.length(),
82 static_cast<unsigned>(iterations),
83 key_size_in_bytes, key_data);
84 return rv == 1 ? std::move(key) : nullptr;
88 std::unique_ptr<SymmetricKey> SymmetricKey::Import(Algorithm algorithm,
89 const std::string& raw_key) {
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)
98 std::unique_ptr<SymmetricKey> key(new SymmetricKey);
103 SymmetricKey::SymmetricKey() = default;
105 } // namespace crypto