Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(215)

Side by Side Diff: base/crypto/symmetric_key_openssl.cc

Issue 4691003: Implement symmetric key for openssl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Refactored tests to parameterized style, and working Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/err.h>
8 #include <openssl/evp.h>
9 #include <openssl/rand.h>
10
7 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/scoped_ptr.h"
13 #include "base/stl_util-inl.h"
8 14
9 namespace base { 15 namespace base {
10 16
17 SymmetricKey::SymmetricKey(std::string* key) {
18 key->swap(key_);
Ryan Sleevi 2010/11/10 23:18:46 Why the swap, as opposed to assignment or just tak
joth 2010/11/11 15:07:12 Just trying to reduce the number of copies of the
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);
Ryan Sleevi 2010/11/10 23:18:46 Why restrict |algorithm| to AES? See symmetric_key
joth 2010/11/11 15:07:12 Just copied this from the NSS version, I didn't wa
18 return NULL; 30 int key_size_in_bytes = key_size_in_bits / 8;
Ryan Sleevi 2010/11/10 23:18:46 My bit-byte sense tingles on this, though it may b
joth 2010/11/11 15:07:12 As above, although I originally had a CHECK that t
31
32 if (key_size_in_bits == 0)
33 return NULL;
34
35 std::string key;
36 key.resize(key_size_in_bytes);
37
38 unsigned char* key_data =
39 reinterpret_cast<unsigned char*>(string_as_array(&key));
40 int res = RAND_bytes(key_data, key.length());
41 if (res != 1) {
42 unsigned long err = ERR_get_error();
Ryan Sleevi 2010/11/10 23:18:46 As a defensive measure, whenever programming OpenS
joth 2010/11/11 15:07:12 Done.
43 LOG(ERROR) << "Could not obtain random bytes. res = " << res
44 << ", err = " << err << " " << ERR_error_string(err, NULL);
45 return NULL;
46 }
47 return new SymmetricKey(&key);
48
19 } 49 }
20 50
21 // static 51 // static
22 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, 52 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm,
23 const std::string& password, 53 const std::string& password,
24 const std::string& salt, 54 const std::string& salt,
25 size_t iterations, 55 size_t iterations,
26 size_t key_size_in_bits) { 56 size_t key_size_in_bits) {
27 NOTIMPLEMENTED(); 57 DCHECK(algorithm == AES || algorithm == HMAC_SHA1);
28 return NULL; 58 int key_size_in_bytes = key_size_in_bits / 8;
59
60 std::string key;
61 key.resize(key_size_in_bytes);
62
63 const unsigned char* salt_data =
64 reinterpret_cast<const unsigned char*>(salt.data());
65 unsigned char* key_data =
66 reinterpret_cast<unsigned char*>(string_as_array(&key));
67
68 int res = PKCS5_PBKDF2_HMAC_SHA1(password.data(), password.length(),
69 salt_data, salt.length(), iterations,
70 key.length(), key_data);
71 if (res != 1) {
72 NOTREACHED() << "HMAC SHA1 failed. res = " << res;
73 return NULL;
74 }
75 return new SymmetricKey(&key);
29 } 76 }
30 77
31 // static 78 // static
32 SymmetricKey* SymmetricKey::Import(Algorithm algorithm, 79 SymmetricKey* SymmetricKey::Import(Algorithm algorithm,
33 const std::string& raw_key) { 80 const std::string& raw_key) {
34 NOTIMPLEMENTED(); 81 std::string copy(raw_key);
35 return NULL; 82 return new SymmetricKey(&copy);
36 } 83 }
37 84
38 bool SymmetricKey::GetRawKey(std::string* raw_key) { 85 bool SymmetricKey::GetRawKey(std::string* raw_key) {
39 NOTIMPLEMENTED(); 86 *raw_key = key_;
40 return false; 87 return true;
41 } 88 }
42 89
43 } // namespace base 90 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698