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 "base/crypto/symmetric_key.h" | 5 #include "base/crypto/symmetric_key.h" |
6 | 6 |
7 #include <openssl/evp.h> | 7 #include <openssl/evp.h> |
8 #include <openssl/rand.h> | 8 #include <openssl/rand.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 | 11 |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/openssl_util.h" | 14 #include "base/openssl_util.h" |
15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 | 18 |
19 SymmetricKey::~SymmetricKey() { | 19 SymmetricKey::~SymmetricKey() { |
20 std::fill(key_.begin(), key_.end(), '\0'); // Zero out the confidential key. | 20 std::fill(key_.begin(), key_.end(), '\0'); // Zero out the confidential key. |
21 } | 21 } |
22 | 22 |
23 // static | 23 // static |
| 24 bool SymmetricKey::GenerateRandomBytes(size_t num_bytes, uint8* out) { |
| 25 LOG(ERROR) << "GenerateRandomBytes openssl" << num_bytes; |
| 26 if (num_bytes == 0) |
| 27 return true; |
| 28 if (out == NULL) |
| 29 return false; |
| 30 LOG(ERROR) << "GenerateRandomBytes openssl pnt1"; |
| 31 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 32 return RAND_bytes(out, num_bytes) == 1; |
| 33 } |
| 34 |
| 35 // static |
24 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, | 36 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, |
25 size_t key_size_in_bits) { | 37 size_t key_size_in_bits) { |
26 DCHECK_EQ(AES, algorithm); | 38 DCHECK_EQ(AES, algorithm); |
27 int key_size_in_bytes = key_size_in_bits / 8; | 39 int key_size_in_bytes = key_size_in_bits / 8; |
28 DCHECK_EQ(static_cast<int>(key_size_in_bits), key_size_in_bytes * 8); | 40 DCHECK_EQ(static_cast<int>(key_size_in_bits), key_size_in_bytes * 8); |
29 | 41 |
30 if (key_size_in_bits == 0) | 42 if (key_size_in_bits == 0) |
31 return NULL; | 43 return NULL; |
32 | |
33 OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
34 scoped_ptr<SymmetricKey> key(new SymmetricKey); | 44 scoped_ptr<SymmetricKey> key(new SymmetricKey); |
35 uint8* key_data = | 45 if (GenerateRandomBytes( |
36 reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1)); | 46 key_size_in_bytes, |
37 | 47 reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1)))) { |
38 int rv = RAND_bytes(key_data, key_size_in_bytes); | 48 return key.release(); |
39 return rv == 1 ? key.release() : NULL; | 49 } else { |
| 50 return NULL; |
| 51 } |
40 } | 52 } |
41 | 53 |
42 // static | 54 // static |
43 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, | 55 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, |
44 const std::string& password, | 56 const std::string& password, |
45 const std::string& salt, | 57 const std::string& salt, |
46 size_t iterations, | 58 size_t iterations, |
47 size_t key_size_in_bits) { | 59 size_t key_size_in_bits) { |
48 DCHECK(algorithm == AES || algorithm == HMAC_SHA1); | 60 DCHECK(algorithm == AES || algorithm == HMAC_SHA1); |
49 int key_size_in_bytes = key_size_in_bits / 8; | 61 int key_size_in_bytes = key_size_in_bits / 8; |
(...skipping 17 matching lines...) Expand all Loading... |
67 key->key_ = raw_key; | 79 key->key_ = raw_key; |
68 return key.release(); | 80 return key.release(); |
69 } | 81 } |
70 | 82 |
71 bool SymmetricKey::GetRawKey(std::string* raw_key) { | 83 bool SymmetricKey::GetRawKey(std::string* raw_key) { |
72 *raw_key = key_; | 84 *raw_key = key_; |
73 return true; | 85 return true; |
74 } | 86 } |
75 | 87 |
76 } // namespace base | 88 } // namespace base |
OLD | NEW |