OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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> | |
8 #include <openssl/rand.h> | |
9 | |
7 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/openssl_util.h" | |
12 #include "base/scoped_ptr.h" | |
13 #include "base/string_util.h" | |
8 | 14 |
9 namespace base { | 15 namespace base { |
10 | 16 |
17 SymmetricKey::SymmetricKey(std::string* key) { | |
18 key->swap(key_); | |
19 } | |
20 | |
11 SymmetricKey::~SymmetricKey() { | 21 SymmetricKey::~SymmetricKey() { |
22 // Zero out the content. | |
23 key_.assign(key_.length(), '\0'); | |
12 } | 24 } |
13 | 25 |
14 // static | 26 // static |
15 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, | 27 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, |
16 size_t key_size_in_bits) { | 28 size_t key_size_in_bits) { |
17 NOTIMPLEMENTED(); | 29 DCHECK_EQ(AES, algorithm); |
18 return NULL; | 30 int key_size_in_bytes = key_size_in_bits / 8; |
31 DCHECK_EQ(static_cast<int>(key_size_in_bits), key_size_in_bytes * 8); | |
32 ScopedERRStackClearer err_stack; | |
33 | |
34 if (key_size_in_bits == 0) | |
35 return NULL; | |
36 | |
37 std::string key; | |
38 uint8* key_data = | |
39 reinterpret_cast<uint8*>(WriteInto(&key, key_size_in_bytes + 1)); | |
40 | |
41 int res = RAND_bytes(key_data, key.length()); | |
42 if (res != 1) | |
43 return NULL; | |
44 return new SymmetricKey(&key); | |
19 } | 45 } |
20 | 46 |
21 // static | 47 // static |
22 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, | 48 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, |
23 const std::string& password, | 49 const std::string& password, |
24 const std::string& salt, | 50 const std::string& salt, |
25 size_t iterations, | 51 size_t iterations, |
26 size_t key_size_in_bits) { | 52 size_t key_size_in_bits) { |
27 NOTIMPLEMENTED(); | 53 DCHECK(algorithm == AES || algorithm == HMAC_SHA1); |
28 return NULL; | 54 int key_size_in_bytes = key_size_in_bits / 8; |
55 DCHECK_EQ(static_cast<int>(key_size_in_bits), key_size_in_bytes * 8); | |
56 ScopedERRStackClearer err_stack; | |
57 | |
58 std::string key; | |
59 uint8* key_data = | |
60 reinterpret_cast<uint8*>(WriteInto(&key, key_size_in_bytes + 1)); | |
61 int res = PKCS5_PBKDF2_HMAC_SHA1(password.data(), password.length(), | |
62 reinterpret_cast<const uint8*>(salt.data()), | |
63 salt.length(), iterations, | |
64 key.length(), key_data); | |
65 if (res != 1) { | |
66 NOTREACHED() << "HMAC SHA1 failed. res = " << res; | |
Ryan Sleevi
2010/11/11 18:07:16
nit: The other impl's don't NOTREACHED(). DLOG(ERR
joth
2010/11/11 19:54:36
Done.
| |
67 return NULL; | |
68 } | |
69 return new SymmetricKey(&key); | |
29 } | 70 } |
30 | 71 |
31 // static | 72 // static |
32 SymmetricKey* SymmetricKey::Import(Algorithm algorithm, | 73 SymmetricKey* SymmetricKey::Import(Algorithm algorithm, |
33 const std::string& raw_key) { | 74 const std::string& raw_key) { |
34 NOTIMPLEMENTED(); | 75 std::string copy(raw_key); |
35 return NULL; | 76 return new SymmetricKey(©); |
36 } | 77 } |
37 | 78 |
38 bool SymmetricKey::GetRawKey(std::string* raw_key) { | 79 bool SymmetricKey::GetRawKey(std::string* raw_key) { |
39 NOTIMPLEMENTED(); | 80 *raw_key = key_; |
40 return false; | 81 return true; |
41 } | 82 } |
42 | 83 |
43 } // namespace base | 84 } // namespace base |
OLD | NEW |