OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "crypto/symmetric_key.h" | |
6 | |
7 #include <openssl/evp.h> | 5 #include <openssl/evp.h> |
8 #include <openssl/rand.h> | 6 #include <openssl/rand.h> |
9 #include <stddef.h> | 7 #include <stddef.h> |
10 #include <stdint.h> | 8 #include <stdint.h> |
11 | 9 |
12 #include <algorithm> | 10 #include <algorithm> |
11 #include <memory> | |
13 | 12 |
14 #include "base/logging.h" | 13 #include "base/logging.h" |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
17 #include "crypto/openssl_util.h" | 15 #include "crypto/openssl_util.h" |
16 #include "crypto/symmetric_key.h" | |
Ryan Sleevi
2016/04/08 21:15:26
This shouldn't have moved
Nico
2016/04/08 21:20:23
Oh, sorry. I tried to catch all these (the script
| |
18 | 17 |
19 namespace crypto { | 18 namespace crypto { |
20 | 19 |
21 SymmetricKey::~SymmetricKey() { | 20 SymmetricKey::~SymmetricKey() { |
22 std::fill(key_.begin(), key_.end(), '\0'); // Zero out the confidential key. | 21 std::fill(key_.begin(), key_.end(), '\0'); // Zero out the confidential key. |
23 } | 22 } |
24 | 23 |
25 // static | 24 // static |
26 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, | 25 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, |
27 size_t key_size_in_bits) { | 26 size_t key_size_in_bits) { |
28 DCHECK_EQ(AES, algorithm); | 27 DCHECK_EQ(AES, algorithm); |
29 | 28 |
30 // Whitelist supported key sizes to avoid accidentaly relying on | 29 // Whitelist supported key sizes to avoid accidentaly relying on |
31 // algorithms available in NSS but not BoringSSL and vice | 30 // algorithms available in NSS but not BoringSSL and vice |
32 // versa. Note that BoringSSL does not support AES-192. | 31 // versa. Note that BoringSSL does not support AES-192. |
33 if (key_size_in_bits != 128 && key_size_in_bits != 256) | 32 if (key_size_in_bits != 128 && key_size_in_bits != 256) |
34 return NULL; | 33 return NULL; |
35 | 34 |
36 size_t key_size_in_bytes = key_size_in_bits / 8; | 35 size_t key_size_in_bytes = key_size_in_bits / 8; |
37 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8); | 36 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8); |
38 | 37 |
39 if (key_size_in_bytes == 0) | 38 if (key_size_in_bytes == 0) |
40 return NULL; | 39 return NULL; |
41 | 40 |
42 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 41 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
43 scoped_ptr<SymmetricKey> key(new SymmetricKey); | 42 std::unique_ptr<SymmetricKey> key(new SymmetricKey); |
44 uint8_t* key_data = reinterpret_cast<uint8_t*>( | 43 uint8_t* key_data = reinterpret_cast<uint8_t*>( |
45 base::WriteInto(&key->key_, key_size_in_bytes + 1)); | 44 base::WriteInto(&key->key_, key_size_in_bytes + 1)); |
46 | 45 |
47 int rv = RAND_bytes(key_data, static_cast<int>(key_size_in_bytes)); | 46 int rv = RAND_bytes(key_data, static_cast<int>(key_size_in_bytes)); |
48 return rv == 1 ? key.release() : NULL; | 47 return rv == 1 ? key.release() : NULL; |
49 } | 48 } |
50 | 49 |
51 // static | 50 // static |
52 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, | 51 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, |
53 const std::string& password, | 52 const std::string& password, |
(...skipping 10 matching lines...) Expand all Loading... | |
64 return NULL; | 63 return NULL; |
65 } | 64 } |
66 | 65 |
67 size_t key_size_in_bytes = key_size_in_bits / 8; | 66 size_t key_size_in_bytes = key_size_in_bits / 8; |
68 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8); | 67 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8); |
69 | 68 |
70 if (key_size_in_bytes == 0) | 69 if (key_size_in_bytes == 0) |
71 return NULL; | 70 return NULL; |
72 | 71 |
73 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 72 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
74 scoped_ptr<SymmetricKey> key(new SymmetricKey); | 73 std::unique_ptr<SymmetricKey> key(new SymmetricKey); |
75 uint8_t* key_data = reinterpret_cast<uint8_t*>( | 74 uint8_t* key_data = reinterpret_cast<uint8_t*>( |
76 base::WriteInto(&key->key_, key_size_in_bytes + 1)); | 75 base::WriteInto(&key->key_, key_size_in_bytes + 1)); |
77 int rv = PKCS5_PBKDF2_HMAC_SHA1( | 76 int rv = PKCS5_PBKDF2_HMAC_SHA1( |
78 password.data(), password.length(), | 77 password.data(), password.length(), |
79 reinterpret_cast<const uint8_t*>(salt.data()), salt.length(), iterations, | 78 reinterpret_cast<const uint8_t*>(salt.data()), salt.length(), iterations, |
80 static_cast<int>(key_size_in_bytes), key_data); | 79 static_cast<int>(key_size_in_bytes), key_data); |
81 return rv == 1 ? key.release() : NULL; | 80 return rv == 1 ? key.release() : NULL; |
82 } | 81 } |
83 | 82 |
84 // static | 83 // static |
85 SymmetricKey* SymmetricKey::Import(Algorithm algorithm, | 84 SymmetricKey* SymmetricKey::Import(Algorithm algorithm, |
86 const std::string& raw_key) { | 85 const std::string& raw_key) { |
87 if (algorithm == AES) { | 86 if (algorithm == AES) { |
88 // Whitelist supported key sizes to avoid accidentaly relying on | 87 // Whitelist supported key sizes to avoid accidentaly relying on |
89 // algorithms available in NSS but not BoringSSL and vice | 88 // algorithms available in NSS but not BoringSSL and vice |
90 // versa. Note that BoringSSL does not support AES-192. | 89 // versa. Note that BoringSSL does not support AES-192. |
91 if (raw_key.size() != 128/8 && raw_key.size() != 256/8) | 90 if (raw_key.size() != 128/8 && raw_key.size() != 256/8) |
92 return NULL; | 91 return NULL; |
93 } | 92 } |
94 | 93 |
95 scoped_ptr<SymmetricKey> key(new SymmetricKey); | 94 std::unique_ptr<SymmetricKey> key(new SymmetricKey); |
96 key->key_ = raw_key; | 95 key->key_ = raw_key; |
97 return key.release(); | 96 return key.release(); |
98 } | 97 } |
99 | 98 |
100 bool SymmetricKey::GetRawKey(std::string* raw_key) { | 99 bool SymmetricKey::GetRawKey(std::string* raw_key) { |
101 *raw_key = key_; | 100 *raw_key = key_; |
102 return true; | 101 return true; |
103 } | 102 } |
104 | 103 |
105 } // namespace crypto | 104 } // namespace crypto |
OLD | NEW |