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

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

Issue 4963002: Refactor EnsureOpenSSLInit and openssl_util into base (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review comments 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
« no previous file with comments | « base/crypto/encryptor_openssl.cc ('k') | base/openssl_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/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>
(...skipping 12 matching lines...) Expand all
23 // static 23 // static
24 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, 24 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm,
25 size_t key_size_in_bits) { 25 size_t key_size_in_bits) {
26 DCHECK_EQ(AES, algorithm); 26 DCHECK_EQ(AES, algorithm);
27 int key_size_in_bytes = key_size_in_bits / 8; 27 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); 28 DCHECK_EQ(static_cast<int>(key_size_in_bits), key_size_in_bytes * 8);
29 29
30 if (key_size_in_bits == 0) 30 if (key_size_in_bits == 0)
31 return NULL; 31 return NULL;
32 32
33 EnsureOpenSSLInit();
33 scoped_ptr<SymmetricKey> key(new SymmetricKey); 34 scoped_ptr<SymmetricKey> key(new SymmetricKey);
34 uint8* key_data = 35 uint8* key_data =
35 reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1)); 36 reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1));
36 37
37 int res = RAND_bytes(key_data, key_size_in_bytes); 38 int res = RAND_bytes(key_data, key_size_in_bytes);
38 if (res != 1) { 39 if (res != 1) {
39 DLOG(ERROR) << "RAND_bytes failed. res = " << res; 40 DLOG(ERROR) << "RAND_bytes failed. res = " << res;
40 ClearOpenSSLERRStack(); 41 ClearOpenSSLERRStack();
41 return NULL; 42 return NULL;
42 } 43 }
43 return key.release(); 44 return key.release();
44 } 45 }
45 46
46 // static 47 // static
47 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, 48 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm,
48 const std::string& password, 49 const std::string& password,
49 const std::string& salt, 50 const std::string& salt,
50 size_t iterations, 51 size_t iterations,
51 size_t key_size_in_bits) { 52 size_t key_size_in_bits) {
52 DCHECK(algorithm == AES || algorithm == HMAC_SHA1); 53 DCHECK(algorithm == AES || algorithm == HMAC_SHA1);
53 int key_size_in_bytes = key_size_in_bits / 8; 54 int key_size_in_bytes = key_size_in_bits / 8;
54 DCHECK_EQ(static_cast<int>(key_size_in_bits), key_size_in_bytes * 8); 55 DCHECK_EQ(static_cast<int>(key_size_in_bits), key_size_in_bytes * 8);
55 56
57 EnsureOpenSSLInit();
56 scoped_ptr<SymmetricKey> key(new SymmetricKey); 58 scoped_ptr<SymmetricKey> key(new SymmetricKey);
57 uint8* key_data = 59 uint8* key_data =
58 reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1)); 60 reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1));
59 int res = PKCS5_PBKDF2_HMAC_SHA1(password.data(), password.length(), 61 int res = PKCS5_PBKDF2_HMAC_SHA1(password.data(), password.length(),
60 reinterpret_cast<const uint8*>(salt.data()), 62 reinterpret_cast<const uint8*>(salt.data()),
61 salt.length(), iterations, 63 salt.length(), iterations,
62 key_size_in_bytes, key_data); 64 key_size_in_bytes, key_data);
63 if (res != 1) { 65 if (res != 1) {
64 DLOG(ERROR) << "HMAC SHA1 failed. res = " << res; 66 DLOG(ERROR) << "HMAC SHA1 failed. res = " << res;
65 ClearOpenSSLERRStack(); 67 ClearOpenSSLERRStack();
66 return NULL; 68 return NULL;
67 } 69 }
68 return key.release(); 70 return key.release();
69 } 71 }
70 72
71 // static 73 // static
72 SymmetricKey* SymmetricKey::Import(Algorithm algorithm, 74 SymmetricKey* SymmetricKey::Import(Algorithm algorithm,
73 const std::string& raw_key) { 75 const std::string& raw_key) {
74 scoped_ptr<SymmetricKey> key(new SymmetricKey); 76 scoped_ptr<SymmetricKey> key(new SymmetricKey);
75 key->key_ = raw_key; 77 key->key_ = raw_key;
76 return key.release(); 78 return key.release();
77 } 79 }
78 80
79 bool SymmetricKey::GetRawKey(std::string* raw_key) { 81 bool SymmetricKey::GetRawKey(std::string* raw_key) {
80 *raw_key = key_; 82 *raw_key = key_;
81 return true; 83 return true;
82 } 84 }
83 85
84 } // namespace base 86 } // namespace base
OLDNEW
« no previous file with comments | « base/crypto/encryptor_openssl.cc ('k') | base/openssl_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698