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

Side by Side Diff: chrome/browser/password_manager/encryptor_mac.mm

Issue 6805019: Move crypto files out of base, to a top level directory. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Fixes comments by eroman Created 9 years, 8 months 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) 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 "chrome/browser/password_manager/encryptor.h" 5 #include "chrome/browser/password_manager/encryptor.h"
6 6
7 #include <CommonCrypto/CommonCryptor.h> // for kCCBlockSizeAES128 7 #include <CommonCrypto/CommonCryptor.h> // for kCCBlockSizeAES128
8 8
9 #include "base/crypto/encryptor.h"
10 #include "base/crypto/symmetric_key.h"
11 #include "base/logging.h" 9 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
13 #include "base/utf_string_conversions.h" 11 #include "base/utf_string_conversions.h"
12 #include "crypto/encryptor.h"
13 #include "crypto/symmetric_key.h"
14 #include "chrome/browser/password_manager/encryptor_password_mac.h" 14 #include "chrome/browser/password_manager/encryptor_password_mac.h"
15 #include "chrome/browser/keychain_mac.h" 15 #include "chrome/browser/keychain_mac.h"
16 16
17 namespace { 17 namespace {
18 18
19 // Salt for Symmetric key derivation. 19 // Salt for Symmetric key derivation.
20 const char kSalt[] = "saltysalt"; 20 const char kSalt[] = "saltysalt";
21 21
22 // Key size required for 128 bit AES. 22 // Key size required for 128 bit AES.
23 const size_t kDerivedKeySizeInBits = 128; 23 const size_t kDerivedKeySizeInBits = 128;
24 24
25 // Constant for Symmetic key derivation. 25 // Constant for Symmetic key derivation.
26 const size_t kEncryptionIterations = 1003; 26 const size_t kEncryptionIterations = 1003;
27 27
28 // TODO(dhollowa): Refactor to allow dependency injection of Keychain. 28 // TODO(dhollowa): Refactor to allow dependency injection of Keychain.
29 static bool use_mock_keychain = false; 29 static bool use_mock_keychain = false;
30 30
31 // Prefix for cypher text returned by current encryption version. We prefix 31 // Prefix for cypher text returned by current encryption version. We prefix
32 // the cypher text with this string so that future data migration can detect 32 // the cypher text with this string so that future data migration can detect
33 // this and migrate to different encryption without data loss. 33 // this and migrate to different encryption without data loss.
34 const char kEncryptionVersionPrefix[] = "v10"; 34 const char kEncryptionVersionPrefix[] = "v10";
35 35
36 // Generates a newly allocated SymmetricKey object based on the password found 36 // Generates a newly allocated SymmetricKey object based on the password found
37 // in the Keychain. The generated key is for AES encryption. Ownership of the 37 // in the Keychain. The generated key is for AES encryption. Ownership of the
38 // key is passed to the caller. Returns NULL key in the case password access 38 // key is passed to the caller. Returns NULL key in the case password access
39 // is denied or key generation error occurs. 39 // is denied or key generation error occurs.
40 base::SymmetricKey* GetEncryptionKey() { 40 crypto::SymmetricKey* GetEncryptionKey() {
41 41
42 std::string password; 42 std::string password;
43 if (use_mock_keychain) { 43 if (use_mock_keychain) {
44 password = "mock_password"; 44 password = "mock_password";
45 } else { 45 } else {
46 MacKeychain keychain; 46 MacKeychain keychain;
47 EncryptorPassword encryptor_password(keychain); 47 EncryptorPassword encryptor_password(keychain);
48 password = encryptor_password.GetEncryptorPassword(); 48 password = encryptor_password.GetEncryptorPassword();
49 } 49 }
50 50
51 if (password.empty()) 51 if (password.empty())
52 return NULL; 52 return NULL;
53 53
54 std::string salt(kSalt); 54 std::string salt(kSalt);
55 55
56 // Create an encryption key from our password and salt. 56 // Create an encryption key from our password and salt.
57 scoped_ptr<base::SymmetricKey> encryption_key( 57 scoped_ptr<crypto::SymmetricKey> encryption_key(
58 base::SymmetricKey::DeriveKeyFromPassword(base::SymmetricKey::AES, 58 crypto::SymmetricKey::DeriveKeyFromPassword(crypto::SymmetricKey::AES,
59 password, 59 password,
60 salt, 60 salt,
61 kEncryptionIterations, 61 kEncryptionIterations,
62 kDerivedKeySizeInBits)); 62 kDerivedKeySizeInBits));
63 DCHECK(encryption_key.get()); 63 DCHECK(encryption_key.get());
64 64
65 return encryption_key.release(); 65 return encryption_key.release();
66 } 66 }
67 67
68 } // namespace 68 } // namespace
69 69
70 bool Encryptor::EncryptString16(const string16& plaintext, 70 bool Encryptor::EncryptString16(const string16& plaintext,
71 std::string* ciphertext) { 71 std::string* ciphertext) {
72 return EncryptString(UTF16ToUTF8(plaintext), ciphertext); 72 return EncryptString(UTF16ToUTF8(plaintext), ciphertext);
73 } 73 }
74 74
75 bool Encryptor::DecryptString16(const std::string& ciphertext, 75 bool Encryptor::DecryptString16(const std::string& ciphertext,
76 string16* plaintext) { 76 string16* plaintext) {
77 std::string utf8; 77 std::string utf8;
78 if (!DecryptString(ciphertext, &utf8)) 78 if (!DecryptString(ciphertext, &utf8))
79 return false; 79 return false;
80 80
81 *plaintext = UTF8ToUTF16(utf8); 81 *plaintext = UTF8ToUTF16(utf8);
82 return true; 82 return true;
83 } 83 }
84 84
85 bool Encryptor::EncryptString(const std::string& plaintext, 85 bool Encryptor::EncryptString(const std::string& plaintext,
86 std::string* ciphertext) { 86 std::string* ciphertext) {
87 if (plaintext.empty()) { 87 if (plaintext.empty()) {
88 *ciphertext = std::string(); 88 *ciphertext = std::string();
89 return true; 89 return true;
90 } 90 }
91 91
92 scoped_ptr<base::SymmetricKey> encryption_key(GetEncryptionKey()); 92 scoped_ptr<crypto::SymmetricKey> encryption_key(GetEncryptionKey());
93 if (!encryption_key.get()) 93 if (!encryption_key.get())
94 return false; 94 return false;
95 95
96 std::string iv(kCCBlockSizeAES128, ' '); 96 std::string iv(kCCBlockSizeAES128, ' ');
97 base::Encryptor encryptor; 97 crypto::Encryptor encryptor;
98 if (!encryptor.Init(encryption_key.get(), base::Encryptor::CBC, iv)) 98 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv))
99 return false; 99 return false;
100 100
101 if (!encryptor.Encrypt(plaintext, ciphertext)) 101 if (!encryptor.Encrypt(plaintext, ciphertext))
102 return false; 102 return false;
103 103
104 // Prefix the cypher text with version information. 104 // Prefix the cypher text with version information.
105 ciphertext->insert(0, kEncryptionVersionPrefix); 105 ciphertext->insert(0, kEncryptionVersionPrefix);
106 return true; 106 return true;
107 } 107 }
108 108
(...skipping 11 matching lines...) Expand all
120 // won't happen. 120 // won't happen.
121 if (ciphertext.find(kEncryptionVersionPrefix) != 0) { 121 if (ciphertext.find(kEncryptionVersionPrefix) != 0) {
122 *plaintext = ciphertext; 122 *plaintext = ciphertext;
123 return true; 123 return true;
124 } 124 }
125 125
126 // Strip off the versioning prefix before decrypting. 126 // Strip off the versioning prefix before decrypting.
127 std::string raw_ciphertext = 127 std::string raw_ciphertext =
128 ciphertext.substr(strlen(kEncryptionVersionPrefix)); 128 ciphertext.substr(strlen(kEncryptionVersionPrefix));
129 129
130 scoped_ptr<base::SymmetricKey> encryption_key(GetEncryptionKey()); 130 scoped_ptr<crypto::SymmetricKey> encryption_key(GetEncryptionKey());
131 if (!encryption_key.get()) 131 if (!encryption_key.get())
132 return false; 132 return false;
133 133
134 std::string iv(kCCBlockSizeAES128, ' '); 134 std::string iv(kCCBlockSizeAES128, ' ');
135 base::Encryptor encryptor; 135 crypto::Encryptor encryptor;
136 if (!encryptor.Init(encryption_key.get(), base::Encryptor::CBC, iv)) 136 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv))
137 return false; 137 return false;
138 138
139 if (!encryptor.Decrypt(raw_ciphertext, plaintext)) 139 if (!encryptor.Decrypt(raw_ciphertext, plaintext))
140 return false; 140 return false;
141 141
142 return true; 142 return true;
143 } 143 }
144 144
145 void Encryptor::UseMockKeychain(bool use_mock) { 145 void Encryptor::UseMockKeychain(bool use_mock) {
146 use_mock_keychain = use_mock; 146 use_mock_keychain = use_mock;
147 } 147 }
148 148
OLDNEW
« no previous file with comments | « chrome/browser/password_manager/encryptor_linux.cc ('k') | chrome/browser/safe_browsing/filter_false_positive_perftest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698