blob: e268a1d0f98c1f3d85ed4b2b8915646211f83d5c [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
[email protected]ac0f8be2010-11-12 12:03:547#include <openssl/evp.h>
8#include <openssl/rand.h>
9
10#include <algorithm>
11
[email protected]70372d42010-10-22 13:12:3412#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/scoped_ptr.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"
[email protected]70372d42010-10-22 13:12:3416
[email protected]4b559b4d2011-04-14 17:37:1417namespace crypto {
[email protected]70372d42010-10-22 13:12:3418
19SymmetricKey::~SymmetricKey() {
[email protected]ac0f8be2010-11-12 12:03:5420 std::fill(key_.begin(), key_.end(), '\0'); // Zero out the confidential key.
[email protected]70372d42010-10-22 13:12:3421}
22
23// static
24SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm,
25 size_t key_size_in_bits) {
[email protected]ac0f8be2010-11-12 12:03:5426 DCHECK_EQ(AES, algorithm);
[email protected]a534bab2014-07-25 21:04:1527
28 // Whitelist supported key sizes to avoid accidentaly relying on
29 // algorithms available in NSS but not BoringSSL and vice
30 // versa. Note that BoringSSL does not support AES-192.
31 if (key_size_in_bits != 128 && key_size_in_bits != 256)
32 return NULL;
33
[email protected]fdce4782011-11-29 20:06:1834 size_t key_size_in_bytes = key_size_in_bits / 8;
35 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8);
[email protected]ac0f8be2010-11-12 12:03:5436
[email protected]fdce4782011-11-29 20:06:1837 if (key_size_in_bytes == 0)
[email protected]ac0f8be2010-11-12 12:03:5438 return NULL;
39
[email protected]be796bb2010-11-18 15:43:4340 OpenSSLErrStackTracer err_tracer(FROM_HERE);
[email protected]ac0f8be2010-11-12 12:03:5441 scoped_ptr<SymmetricKey> key(new SymmetricKey);
42 uint8* key_data =
43 reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1));
44
[email protected]fdce4782011-11-29 20:06:1845 int rv = RAND_bytes(key_data, static_cast<int>(key_size_in_bytes));
[email protected]be796bb2010-11-18 15:43:4346 return rv == 1 ? key.release() : NULL;
[email protected]70372d42010-10-22 13:12:3447}
48
49// static
50SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm,
51 const std::string& password,
52 const std::string& salt,
53 size_t iterations,
54 size_t key_size_in_bits) {
[email protected]ac0f8be2010-11-12 12:03:5455 DCHECK(algorithm == AES || algorithm == HMAC_SHA1);
[email protected]a534bab2014-07-25 21:04:1556
57 if (algorithm == AES) {
58 // Whitelist supported key sizes to avoid accidentaly relying on
59 // algorithms available in NSS but not BoringSSL and vice
60 // versa. Note that BoringSSL does not support AES-192.
61 if (key_size_in_bits != 128 && key_size_in_bits != 256)
62 return NULL;
63 }
64
[email protected]fdce4782011-11-29 20:06:1865 size_t key_size_in_bytes = key_size_in_bits / 8;
66 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8);
67
68 if (key_size_in_bytes == 0)
69 return NULL;
[email protected]ac0f8be2010-11-12 12:03:5470
[email protected]be796bb2010-11-18 15:43:4371 OpenSSLErrStackTracer err_tracer(FROM_HERE);
[email protected]ac0f8be2010-11-12 12:03:5472 scoped_ptr<SymmetricKey> key(new SymmetricKey);
73 uint8* key_data =
74 reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1));
[email protected]be796bb2010-11-18 15:43:4375 int rv = PKCS5_PBKDF2_HMAC_SHA1(password.data(), password.length(),
76 reinterpret_cast<const uint8*>(salt.data()),
77 salt.length(), iterations,
[email protected]fdce4782011-11-29 20:06:1878 static_cast<int>(key_size_in_bytes),
79 key_data);
[email protected]be796bb2010-11-18 15:43:4380 return rv == 1 ? key.release() : NULL;
[email protected]70372d42010-10-22 13:12:3481}
82
83// static
84SymmetricKey* SymmetricKey::Import(Algorithm algorithm,
85 const std::string& raw_key) {
[email protected]a534bab2014-07-25 21:04:1586 if (algorithm == AES) {
87 // Whitelist supported key sizes to avoid accidentaly relying on
88 // algorithms available in NSS but not BoringSSL and vice
89 // versa. Note that BoringSSL does not support AES-192.
90 if (raw_key.size() != 128/8 && raw_key.size() != 256/8)
91 return NULL;
92 }
93
[email protected]ac0f8be2010-11-12 12:03:5494 scoped_ptr<SymmetricKey> key(new SymmetricKey);
95 key->key_ = raw_key;
96 return key.release();
[email protected]70372d42010-10-22 13:12:3497}
98
99bool SymmetricKey::GetRawKey(std::string* raw_key) {
[email protected]ac0f8be2010-11-12 12:03:54100 *raw_key = key_;
101 return true;
[email protected]70372d42010-10-22 13:12:34102}
103
[email protected]4b559b4d2011-04-14 17:37:14104} // namespace crypto