blob: 6a19f84089657ae096b38bedeed1a2d0631ecc0a [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]70372d42010-10-22 13:12:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4b559b4d2011-04-14 17:37:145#include "crypto/symmetric_key.h"
[email protected]70372d42010-10-22 13:12:346
avidd373b8b2015-12-21 21:34:437#include <stddef.h>
8#include <stdint.h>
[email protected]ac0f8be2010-11-12 12:03:549
10#include <algorithm>
rsleeviffe5a132016-06-28 01:51:5211#include <utility>
[email protected]ac0f8be2010-11-12 12:03:5412
[email protected]70372d42010-10-22 13:12:3413#include "base/logging.h"
[email protected]0d8db082013-06-11 07:27:0114#include "base/strings/string_util.h"
[email protected]4b559b4d2011-04-14 17:37:1415#include "crypto/openssl_util.h"
tfarina29a3a1742016-10-28 18:47:3316#include "third_party/boringssl/src/include/openssl/evp.h"
17#include "third_party/boringssl/src/include/openssl/rand.h"
[email protected]70372d42010-10-22 13:12:3418
[email protected]4b559b4d2011-04-14 17:37:1419namespace crypto {
[email protected]70372d42010-10-22 13:12:3420
21SymmetricKey::~SymmetricKey() {
[email protected]ac0f8be2010-11-12 12:03:5422 std::fill(key_.begin(), key_.end(), '\0'); // Zero out the confidential key.
[email protected]70372d42010-10-22 13:12:3423}
24
25// static
rsleeviffe5a132016-06-28 01:51:5226std::unique_ptr<SymmetricKey> SymmetricKey::GenerateRandomKey(
27 Algorithm algorithm,
28 size_t key_size_in_bits) {
[email protected]ac0f8be2010-11-12 12:03:5429 DCHECK_EQ(AES, algorithm);
[email protected]a534bab2014-07-25 21:04:1530
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)
rsleeviffe5a132016-06-28 01:51:5235 return nullptr;
[email protected]a534bab2014-07-25 21:04:1536
[email protected]fdce4782011-11-29 20:06:1837 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]ac0f8be2010-11-12 12:03:5439
[email protected]fdce4782011-11-29 20:06:1840 if (key_size_in_bytes == 0)
rsleeviffe5a132016-06-28 01:51:5241 return nullptr;
[email protected]ac0f8be2010-11-12 12:03:5442
[email protected]be796bb2010-11-18 15:43:4343 OpenSSLErrStackTracer err_tracer(FROM_HERE);
thakisd1a18472016-04-08 22:30:4144 std::unique_ptr<SymmetricKey> key(new SymmetricKey);
avidd373b8b2015-12-21 21:34:4345 uint8_t* key_data = reinterpret_cast<uint8_t*>(
Brett Wilsone3c4d1a2015-07-07 23:38:0946 base::WriteInto(&key->key_, key_size_in_bytes + 1));
[email protected]ac0f8be2010-11-12 12:03:5447
[email protected]fdce4782011-11-29 20:06:1848 int rv = RAND_bytes(key_data, static_cast<int>(key_size_in_bytes));
rsleeviffe5a132016-06-28 01:51:5249 return rv == 1 ? std::move(key) : nullptr;
[email protected]70372d42010-10-22 13:12:3450}
51
52// static
rsleeviffe5a132016-06-28 01:51:5253std::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]ac0f8be2010-11-12 12:03:5459 DCHECK(algorithm == AES || algorithm == HMAC_SHA1);
[email protected]a534bab2014-07-25 21:04:1560
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)
rsleeviffe5a132016-06-28 01:51:5266 return nullptr;
[email protected]a534bab2014-07-25 21:04:1567 }
68
[email protected]fdce4782011-11-29 20:06:1869 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)
rsleeviffe5a132016-06-28 01:51:5273 return nullptr;
[email protected]ac0f8be2010-11-12 12:03:5474
[email protected]be796bb2010-11-18 15:43:4375 OpenSSLErrStackTracer err_tracer(FROM_HERE);
thakisd1a18472016-04-08 22:30:4176 std::unique_ptr<SymmetricKey> key(new SymmetricKey);
avidd373b8b2015-12-21 21:34:4377 uint8_t* key_data = reinterpret_cast<uint8_t*>(
Brett Wilsone3c4d1a2015-07-07 23:38:0978 base::WriteInto(&key->key_, key_size_in_bytes + 1));
avidd373b8b2015-12-21 21:34:4379 int rv = PKCS5_PBKDF2_HMAC_SHA1(
80 password.data(), password.length(),
svaldez9c641462016-05-02 20:49:0581 reinterpret_cast<const uint8_t*>(salt.data()), salt.length(),
82 static_cast<unsigned>(iterations),
83 key_size_in_bytes, key_data);
rsleeviffe5a132016-06-28 01:51:5284 return rv == 1 ? std::move(key) : nullptr;
[email protected]70372d42010-10-22 13:12:3485}
86
87// static
rsleeviffe5a132016-06-28 01:51:5288std::unique_ptr<SymmetricKey> SymmetricKey::Import(Algorithm algorithm,
89 const std::string& raw_key) {
[email protected]a534bab2014-07-25 21:04:1590 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)
rsleeviffe5a132016-06-28 01:51:5295 return nullptr;
[email protected]a534bab2014-07-25 21:04:1596 }
97
thakisd1a18472016-04-08 22:30:4198 std::unique_ptr<SymmetricKey> key(new SymmetricKey);
[email protected]ac0f8be2010-11-12 12:03:5499 key->key_ = raw_key;
rsleeviffe5a132016-06-28 01:51:52100 return key;
[email protected]70372d42010-10-22 13:12:34101}
102
103bool SymmetricKey::GetRawKey(std::string* raw_key) {
[email protected]ac0f8be2010-11-12 12:03:54104 *raw_key = key_;
105 return true;
[email protected]70372d42010-10-22 13:12:34106}
107
rsleeviffe5a132016-06-28 01:51:52108SymmetricKey::SymmetricKey() = default;
109
[email protected]4b559b4d2011-04-14 17:37:14110} // namespace crypto