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

Side by Side Diff: chrome/browser/signin/local_auth.cc

Issue 875983008: Revert "Only store leading 13 bits of password hash." (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 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
« no previous file with comments | « chrome/browser/signin/local_auth.h ('k') | chrome/browser/signin/local_auth_unittest.cc » ('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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/signin/local_auth.h" 5 #include "chrome/browser/signin/local_auth.h"
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
11 #include "base/prefs/pref_service.h" 11 #include "base/prefs/pref_service.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "chrome/browser/browser_process.h" 13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_manager.h" 15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/common/pref_names.h" 16 #include "chrome/common/pref_names.h"
17 #include "components/os_crypt/os_crypt.h" 17 #include "components/os_crypt/os_crypt.h"
18 #include "components/pref_registry/pref_registry_syncable.h" 18 #include "components/pref_registry/pref_registry_syncable.h"
19 #include "crypto/random.h" 19 #include "crypto/random.h"
20 #include "crypto/secure_util.h" 20 #include "crypto/secure_util.h"
21 #include "crypto/symmetric_key.h" 21 #include "crypto/symmetric_key.h"
22 22
23 namespace { 23 namespace {
24 24
25 struct HashEncoding {
26 char version;
27 unsigned hash_bits;
28 unsigned hash_bytes;
29 unsigned iteration_count;
30 unsigned stored_bits;
31 unsigned stored_bytes;
32
33 public:
34 HashEncoding(char version,
35 unsigned hash_bits,
36 unsigned hash_bytes,
37 unsigned iteration_count,
38 unsigned stored_bits,
39 unsigned stored_bytes) :
40 version(version),
41 hash_bits(hash_bits),
42 hash_bytes(hash_bytes),
43 iteration_count(iteration_count),
44 stored_bits(stored_bits),
45 stored_bytes(stored_bytes) {}
46 };
47
48 // WARNING: Changing these values will make it impossible to do off-line 25 // WARNING: Changing these values will make it impossible to do off-line
49 // authentication until the next successful on-line authentication. To change 26 // authentication until the next successful on-line authentication. To change
50 // these safely, add a new HashEncoding object below and increment 27 // these safely, change the "encoding" version below and make verification
51 // NUM_HASH_ENCODINGS. 28 // handle multiple values.
52 const char kHash1Version = '1'; 29 const char kHash1Encoding = '1';
53 const unsigned kHash1Bits = 256; 30 const unsigned kHash1Bits = 256;
54 const unsigned kHash1Bytes = kHash1Bits / 8; 31 const unsigned kHash1Bytes = kHash1Bits / 8;
55 const unsigned kHash1IterationCount = 100000; 32 const unsigned kHash1IterationCount = 100000;
56 33
57 // Store 13 bits to provide pin-like security (8192 possible values), without
58 // providing a complete oracle for the user's GAIA password.
59 const char kHash2Version = '2';
60 const unsigned kHash2Bits = 256;
61 const unsigned kHash2Bytes = kHash2Bits / 8;
62 const unsigned kHash2IterationCount = 100000;
63 const unsigned kHash2StoredBits = 13;
64 const unsigned kHash2StoredBytes = (kHash2StoredBits + 7) / 8;
65
66 const int NUM_HASH_ENCODINGS = 2;
67 HashEncoding encodings[NUM_HASH_ENCODINGS] = {
68 HashEncoding(
69 kHash1Version, kHash1Bits, kHash1Bytes, kHash1IterationCount, 0, 0),
70 HashEncoding(
71 kHash2Version, kHash2Bits, kHash2Bytes, kHash2IterationCount,
72 kHash2StoredBits, kHash2StoredBytes)
73 };
74
75 const HashEncoding* GetEncodingForVersion(char version) {
76 // Note that versions are 1-indexed.
77 DCHECK(version > '0' && version <= ('0' + NUM_HASH_ENCODINGS));
78 return &encodings[(version - '0') - 1];
79 }
80
81 std::string TruncateStringByBits(const std::string& str,
82 const size_t len_bits) {
83 if (len_bits % 8 == 0)
84 return str.substr(0, len_bits / 8);
85
86 // The initial truncation copies whole bytes
87 int number_bytes = (len_bits + 7) / 8;
88 std::string truncated_string = str.substr(0, number_bytes);
89
90 // Keep the prescribed number of bits from the last byte.
91 unsigned last_char_bitmask = (1 << (len_bits % 8)) - 1;
92 truncated_string[number_bytes - 1] &= last_char_bitmask;
93 return truncated_string;
94 }
95
96 std::string CreateSecurePasswordHash(const std::string& salt, 34 std::string CreateSecurePasswordHash(const std::string& salt,
97 const std::string& password, 35 const std::string& password,
98 const HashEncoding& encoding) { 36 char encoding) {
99 DCHECK_EQ(encoding.hash_bytes, salt.length()); 37 DCHECK_EQ(kHash1Bytes, salt.length());
38 DCHECK_EQ(kHash1Encoding, encoding); // Currently support only one method.
39
100 base::Time start_time = base::Time::Now(); 40 base::Time start_time = base::Time::Now();
101 41
102 // Library call to create secure password hash as SymmetricKey (uses PBKDF2). 42 // Library call to create secure password hash as SymmetricKey (uses PBKDF2).
103 scoped_ptr<crypto::SymmetricKey> password_key( 43 scoped_ptr<crypto::SymmetricKey> password_key(
104 crypto::SymmetricKey::DeriveKeyFromPassword( 44 crypto::SymmetricKey::DeriveKeyFromPassword(
105 crypto::SymmetricKey::AES, 45 crypto::SymmetricKey::AES,
106 password, salt, 46 password, salt,
107 encoding.iteration_count, encoding.hash_bits)); 47 kHash1IterationCount, kHash1Bits));
108 std::string password_hash; 48 std::string password_hash;
109 const bool success = password_key->GetRawKey(&password_hash); 49 const bool success = password_key->GetRawKey(&password_hash);
110 DCHECK(success); 50 DCHECK(success);
111 DCHECK_EQ(encoding.hash_bytes, password_hash.length()); 51 DCHECK_EQ(kHash1Bytes, password_hash.length());
112 52
113 UMA_HISTOGRAM_TIMES("PasswordHash.CreateTime", 53 UMA_HISTOGRAM_TIMES("PasswordHash.CreateTime",
114 base::Time::Now() - start_time); 54 base::Time::Now() - start_time);
115 55
116 if (encoding.stored_bits) {
117 password_hash = TruncateStringByBits(password_hash, encoding.stored_bits);
118 DCHECK_EQ(encoding.stored_bytes, password_hash.length());
119 }
120 DCHECK_EQ(encoding.stored_bytes ? encoding.stored_bytes : encoding.hash_bytes,
121 password_hash.length());
122 return password_hash; 56 return password_hash;
123 } 57 }
124 58
125 std::string EncodePasswordHashRecord(const std::string& record, 59 std::string EncodePasswordHashRecord(const std::string& record,
126 const HashEncoding& encoding) { 60 char encoding) {
61 DCHECK_EQ(kHash1Encoding, encoding); // Currently support only one method.
62
127 // Encrypt the hash using the OS account-password protection (if available). 63 // Encrypt the hash using the OS account-password protection (if available).
128 std::string encoded; 64 std::string encoded;
129 const bool success = OSCrypt::EncryptString(record, &encoded); 65 const bool success = OSCrypt::EncryptString(record, &encoded);
130 DCHECK(success); 66 DCHECK(success);
131 67
132 // Convert binary record to text for preference database. 68 // Convert binary record to text for preference database.
133 std::string encoded64; 69 std::string encoded64;
134 base::Base64Encode(encoded, &encoded64); 70 base::Base64Encode(encoded, &encoded64);
135 71
136 // Stuff the "encoding" value into the first byte. 72 // Stuff the "encoding" value into the first byte.
137 encoded64.insert(0, &encoding.version, sizeof(encoding.version)); 73 encoded64.insert(0, &encoding, sizeof(encoding));
138 74
139 return encoded64; 75 return encoded64;
140 } 76 }
141 77
142 bool DecodePasswordHashRecord(const std::string& encoded, 78 bool DecodePasswordHashRecord(const std::string& encoded,
143 std::string* decoded, 79 std::string* decoded,
144 char* encoding) { 80 char* encoding) {
145 // Extract the "encoding" value from the first byte and validate. 81 // Extract the "encoding" value from the first byte and validate.
146 if (encoded.length() < 1) 82 if (encoded.length() < 1)
147 return false; 83 return false;
148 *encoding = encoded[0]; 84 *encoding = encoded[0];
149 if (!GetEncodingForVersion(*encoding)) 85 if (*encoding != kHash1Encoding)
150 return false; 86 return false;
151 87
152 // Stored record is base64; convert to binary. 88 // Stored record is base64; convert to binary.
153 std::string unbase64; 89 std::string unbase64;
154 if (!base::Base64Decode(encoded.substr(1), &unbase64)) 90 if (!base::Base64Decode(encoded.substr(1), &unbase64))
155 return false; 91 return false;
156 92
157 // Decrypt the record using the OS account-password protection (if available). 93 // Decrypt the record using the OS account-password protection (if available).
158 return OSCrypt::DecryptString(unbase64, decoded); 94 return OSCrypt::DecryptString(unbase64, decoded);
159 } 95 }
160 96
161 size_t GetProfileInfoIndexOfProfile(const Profile* profile) { 97 size_t GetProfileInfoIndexOfProfile(const Profile* profile) {
162 DCHECK(profile); 98 DCHECK(profile);
163 99
164 ProfileInfoCache& info = 100 ProfileInfoCache& info =
165 g_browser_process->profile_manager()->GetProfileInfoCache(); 101 g_browser_process->profile_manager()->GetProfileInfoCache();
166 return info.GetIndexOfProfileWithPath(profile->GetPath()); 102 return info.GetIndexOfProfileWithPath(profile->GetPath());
167 } 103 }
168 104
169 } // namespace 105 } // namespace
170 106
171 std::string LocalAuth::TruncateStringByBits(const std::string& str, 107 namespace chrome {
172 const size_t len_bits) {
173 return ::TruncateStringByBits(str, len_bits);
174 }
175 108
176 void LocalAuth::RegisterLocalAuthPrefs( 109 void RegisterLocalAuthPrefs(user_prefs::PrefRegistrySyncable* registry) {
177 user_prefs::PrefRegistrySyncable* registry) {
178 registry->RegisterStringPref( 110 registry->RegisterStringPref(
179 prefs::kGoogleServicesPasswordHash, 111 prefs::kGoogleServicesPasswordHash,
180 std::string(), 112 std::string(),
181 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 113 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
182 } 114 }
183 115
184 void LocalAuth::SetLocalAuthCredentialsWithEncoding(size_t info_index, 116 void SetLocalAuthCredentials(size_t info_index,
185 const std::string& password, 117 const std::string& password) {
186 char encoding_version) { 118 if (info_index == std::string::npos) {
187 const HashEncoding& encoding = encodings[(encoding_version - '0') - 1]; 119 NOTREACHED();
120 return;
121 }
122 DCHECK(password.length());
188 123
189 // Salt should be random data, as long as the hash length, and different with 124 // Salt should be random data, as long as the hash length, and different with
190 // every save. 125 // every save.
191 std::string salt_str; 126 std::string salt_str;
192 crypto::RandBytes(WriteInto(&salt_str, encoding.hash_bytes + 1), 127 crypto::RandBytes(WriteInto(&salt_str, kHash1Bytes + 1), kHash1Bytes);
193 encoding.hash_bytes); 128 DCHECK_EQ(kHash1Bytes, salt_str.length());
194 129
195 // Perform secure hash of password for storage. 130 // Perform secure hash of password for storage.
196 std::string password_hash = CreateSecurePasswordHash( 131 std::string password_hash = CreateSecurePasswordHash(
197 salt_str, password, encoding); 132 salt_str, password, kHash1Encoding);
133 DCHECK_EQ(kHash1Bytes, password_hash.length());
198 134
199 // Group all fields into a single record for storage; 135 // Group all fields into a single record for storage;
200 std::string record; 136 std::string record;
201 record.append(salt_str); 137 record.append(salt_str);
202 record.append(password_hash); 138 record.append(password_hash);
203 139
204 // Encode it and store it. 140 // Encode it and store it.
205 std::string encoded = EncodePasswordHashRecord(record, encoding); 141 std::string encoded = EncodePasswordHashRecord(record, kHash1Encoding);
206 ProfileInfoCache& info = 142 ProfileInfoCache& info =
207 g_browser_process->profile_manager()->GetProfileInfoCache(); 143 g_browser_process->profile_manager()->GetProfileInfoCache();
208 info.SetLocalAuthCredentialsOfProfileAtIndex(info_index, encoded); 144 info.SetLocalAuthCredentialsOfProfileAtIndex(info_index, encoded);
209 } 145 }
210 146
211 void LocalAuth::SetLocalAuthCredentials(size_t info_index, 147 void SetLocalAuthCredentials(const Profile* profile,
212 const std::string& password) { 148 const std::string& password) {
213 if (info_index == std::string::npos) {
214 NOTREACHED();
215 return;
216 }
217 DCHECK(password.length());
218 SetLocalAuthCredentialsWithEncoding(
219 info_index, password, '0' + NUM_HASH_ENCODINGS);
220 }
221
222 void LocalAuth::SetLocalAuthCredentials(const Profile* profile,
223 const std::string& password) {
224 SetLocalAuthCredentials(GetProfileInfoIndexOfProfile(profile), password); 149 SetLocalAuthCredentials(GetProfileInfoIndexOfProfile(profile), password);
225 } 150 }
226 151
227 bool LocalAuth::ValidateLocalAuthCredentials(size_t info_index, 152 bool ValidateLocalAuthCredentials(size_t info_index,
228 const std::string& password) { 153 const std::string& password) {
229 if (info_index == std::string::npos) { 154 if (info_index == std::string::npos) {
230 NOTREACHED(); 155 NOTREACHED();
231 return false; 156 return false;
232 } 157 }
233 158
234 std::string record; 159 std::string record;
235 char encoding; 160 char encoding;
236 161
237 ProfileInfoCache& info = 162 ProfileInfoCache& info =
238 g_browser_process->profile_manager()->GetProfileInfoCache(); 163 g_browser_process->profile_manager()->GetProfileInfoCache();
239 164
240 std::string encodedhash = 165 std::string encodedhash =
241 info.GetLocalAuthCredentialsOfProfileAtIndex(info_index); 166 info.GetLocalAuthCredentialsOfProfileAtIndex(info_index);
242 if (encodedhash.length() == 0 && password.length() == 0) 167 if (encodedhash.length() == 0 && password.length() == 0)
243 return true; 168 return true;
244 if (!DecodePasswordHashRecord(encodedhash, &record, &encoding)) 169 if (!DecodePasswordHashRecord(encodedhash, &record, &encoding))
245 return false; 170 return false;
246 171
247 std::string password_hash; 172 std::string password_hash;
248 const char* password_saved; 173 const char* password_saved;
249 const char* password_check; 174 const char* password_check;
250 size_t password_length; 175 size_t password_length;
251 176
252 const HashEncoding* hash_encoding = GetEncodingForVersion(encoding); 177 if (encoding == '1') {
253 if (!hash_encoding) { 178 // Validate correct length; extract salt and password hash.
254 // Unknown encoding. 179 if (record.length() != 2 * kHash1Bytes)
180 return false;
181 std::string salt_str(record.data(), kHash1Bytes);
182 password_saved = record.data() + kHash1Bytes;
183 password_hash = CreateSecurePasswordHash(salt_str, password, encoding);
184 password_check = password_hash.data();
185 password_length = kHash1Bytes;
186 } else {
187 // unknown encoding
255 return false; 188 return false;
256 } 189 }
257 190
258 // Extract salt. 191 return crypto::SecureMemEqual(password_saved, password_check,
259 std::string salt_str(record.data(), hash_encoding->hash_bytes); 192 password_length);
260 // Extract password.
261 password_saved = record.data() + hash_encoding->hash_bytes;
262 password_hash = CreateSecurePasswordHash(salt_str, password, *hash_encoding);
263 password_length = hash_encoding->stored_bytes;
264 password_check = password_hash.data();
265
266 bool passwords_match = crypto::SecureMemEqual(
267 password_saved, password_check, password_length);
268
269 // Update the stored credentials to the latest encoding if necessary.
270 if (passwords_match && (hash_encoding->version - '0') != NUM_HASH_ENCODINGS)
271 SetLocalAuthCredentials(info_index, password);
272 return passwords_match;
273 } 193 }
274 194
275 bool LocalAuth::ValidateLocalAuthCredentials(const Profile* profile, 195 bool ValidateLocalAuthCredentials(const Profile* profile,
276 const std::string& password) { 196 const std::string& password) {
277 return ValidateLocalAuthCredentials(GetProfileInfoIndexOfProfile(profile), 197 return ValidateLocalAuthCredentials(GetProfileInfoIndexOfProfile(profile),
278 password); 198 password);
279 } 199 }
200
201 } // namespace chrome
OLDNEW
« no previous file with comments | « chrome/browser/signin/local_auth.h ('k') | chrome/browser/signin/local_auth_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698