summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/util
diff options
context:
space:
mode:
authorqsr@google.com <qsr@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-05 08:46:11 +0000
committerqsr@google.com <qsr@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-05 08:46:11 +0000
commit51a018181c93b3c190146432805836155d69effa (patch)
treea9ed7d4a2acdbfad83f4c3063250ef283b15405b /chrome/browser/sync/util
parent1817055ea2667eda23f5d53d623b3a547a7d19ee (diff)
downloadchromium_src-51a018181c93b3c190146432805836155d69effa.zip
chromium_src-51a018181c93b3c190146432805836155d69effa.tar.gz
chromium_src-51a018181c93b3c190146432805836155d69effa.tar.bz2
Move crypto_helpers from sync to crypto
crypto_helpers only depends on resources in base and is used by sync and password_manager. BUG= TEST= Review URL: http://codereview.chromium.org/6873156 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@84223 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/sync/util')
-rw-r--r--chrome/browser/sync/util/crypto_helpers.cc71
-rw-r--r--chrome/browser/sync/util/crypto_helpers.h39
-rw-r--r--chrome/browser/sync/util/crypto_helpers_unittest.cc27
-rw-r--r--chrome/browser/sync/util/user_settings.cc40
-rw-r--r--chrome/browser/sync/util/user_settings_win.cc3
5 files changed, 25 insertions, 155 deletions
diff --git a/chrome/browser/sync/util/crypto_helpers.cc b/chrome/browser/sync/util/crypto_helpers.cc
deleted file mode 100644
index 36c5586..0000000
--- a/chrome/browser/sync/util/crypto_helpers.cc
+++ /dev/null
@@ -1,71 +0,0 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/sync/util/crypto_helpers.h"
-
-#include <string>
-#include <vector>
-
-#include "base/basictypes.h"
-#include "base/format_macros.h"
-#include "base/logging.h"
-#include "base/base64.h"
-#include "base/rand_util.h"
-#include "base/string_number_conversions.h"
-#include "base/string_util.h"
-
-using std::string;
-using std::vector;
-
-MD5Calculator::MD5Calculator() {
- MD5Init(&context_);
-}
-
-MD5Calculator::~MD5Calculator() {}
-
-void MD5Calculator::AddData(const unsigned char* data, int length) {
- CHECK(bin_digest_.empty());
- MD5Update(&context_, data, length);
-}
-
-void MD5Calculator::CalcDigest() {
- if (bin_digest_.empty()) {
- MD5Digest digest;
- MD5Final(&digest, &context_);
- bin_digest_.assign(digest.a, digest.a + arraysize(digest.a));
- }
-}
-
-const vector<uint8>& MD5Calculator::GetDigest() {
- CalcDigest();
- return bin_digest_;
-}
-
-std::string MD5Calculator::GetHexDigest() {
- CalcDigest();
- string hex = base::HexEncode(reinterpret_cast<char*>(&bin_digest_.front()),
- bin_digest_.size());
- StringToLowerASCII(&hex);
- return hex;
-}
-
-void GetRandomBytes(char* output, int output_length) {
- uint64 random_int;
- const char* random_int_bytes = reinterpret_cast<const char*>(&random_int);
- int random_int_size = sizeof(random_int);
- for (int i = 0; i < output_length; i += random_int_size) {
- random_int = base::RandUint64();
- int copy_count = std::min(output_length - i, random_int_size);
- memcpy(output + i, random_int_bytes, copy_count);
- }
-}
-
-string Generate128BitRandomHexString() {
- const int kNumberBytes = 128 / 8;
- std::string random_bytes(kNumberBytes, ' ');
- GetRandomBytes(&random_bytes[0], kNumberBytes);
- std::string base64_encoded_bytes;
- base::Base64Encode(random_bytes, &base64_encoded_bytes);
- return base64_encoded_bytes;
-}
diff --git a/chrome/browser/sync/util/crypto_helpers.h b/chrome/browser/sync/util/crypto_helpers.h
deleted file mode 100644
index 34b36f4..0000000
--- a/chrome/browser/sync/util/crypto_helpers.h
+++ /dev/null
@@ -1,39 +0,0 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef CHROME_BROWSER_SYNC_UTIL_CRYPTO_HELPERS_H_
-#define CHROME_BROWSER_SYNC_UTIL_CRYPTO_HELPERS_H_
-#pragma once
-
-#include <string>
-#include <vector>
-
-// An object to handle calculation of MD5 sums.
-#include "base/basictypes.h"
-#include "base/md5.h"
-#include "base/port.h"
-
-class MD5Calculator {
- protected:
- MD5Context context_;
- std::vector<uint8> bin_digest_;
-
- void CalcDigest();
- public:
- MD5Calculator();
- ~MD5Calculator();
- void AddData(const uint8* data, int length);
- void AddData(const char* data, int length) {
- AddData(reinterpret_cast<const uint8*>(data), length);
- }
- std::string GetHexDigest();
- const std::vector<uint8>& GetDigest();
- private:
- DISALLOW_COPY_AND_ASSIGN(MD5Calculator);
-};
-
-void GetRandomBytes(char* output, int output_length);
-std::string Generate128BitRandomHexString();
-
-#endif // CHROME_BROWSER_SYNC_UTIL_CRYPTO_HELPERS_H_
diff --git a/chrome/browser/sync/util/crypto_helpers_unittest.cc b/chrome/browser/sync/util/crypto_helpers_unittest.cc
deleted file mode 100644
index 7be1270..0000000
--- a/chrome/browser/sync/util/crypto_helpers_unittest.cc
+++ /dev/null
@@ -1,27 +0,0 @@
-// Copyright (c) 2009 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "chrome/browser/sync/util/crypto_helpers.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-TEST(ChecksumTest, MD5ChecksumTest) {
- uint8 buffer[256];
- for (unsigned int i = 0; i < arraysize(buffer); ++i) {
- buffer[i] = i;
- }
- MD5Calculator md5;
- md5.AddData(buffer, arraysize(buffer));
- std::string checksum("e2c865db4162bed963bfaa9ef6ac18f0");
- ASSERT_EQ(checksum, md5.GetHexDigest());
-}
-
-TEST(CryptoHelpers, GetRandomBytes) {
- for (int i = 1; i < 25; ++i) {
- std::string random_bytes(i+1, ' ');
- do {
- GetRandomBytes(&random_bytes[0], i);
- ASSERT_EQ(random_bytes[i], ' ');
- } while (random_bytes[i - 1] == ' ');
- }
-}
diff --git a/chrome/browser/sync/util/user_settings.cc b/chrome/browser/sync/util/user_settings.cc
index d49a071..b842c43 100644
--- a/chrome/browser/sync/util/user_settings.cc
+++ b/chrome/browser/sync/util/user_settings.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
@@ -18,10 +18,12 @@
#include <vector>
#include "base/file_util.h"
+#include "base/md5.h"
+#include "base/rand_util.h"
#include "base/string_util.h"
#include "chrome/browser/sync/syncable/directory_manager.h" // For migration.
-#include "chrome/browser/sync/util/crypto_helpers.h"
#include "chrome/browser/sync/util/data_encryption.h"
+#include "chrome/common/random.h"
#include "chrome/common/sqlite_utils.h"
using std::numeric_limits;
@@ -166,7 +168,7 @@ static void MakeClientIDTable(sqlite3* const dbhandle) {
SQLStatement statement;
statement.prepare(dbhandle,
"INSERT INTO client_id values ( ? )");
- statement.bind_string(0, Generate128BitRandomHexString());
+ statement.bind_string(0, Generate128BitRandomBase64String());
if (SQLITE_DONE != statement.step()) {
LOG(FATAL) << "INSERT INTO client_id\n" << sqlite3_errmsg(dbhandle);
}
@@ -270,13 +272,12 @@ const int32 kInvalidHash = 0xFFFFFFFF;
// We use 10 bits of data from the MD5 digest as the hash.
const int32 kHashMask = 0x3FF;
-int32 GetHashFromDigest(const vector<uint8>& digest) {
+int32 GetHashFromDigest(MD5Digest& digest) {
int32 hash = 0;
int32 mask = kHashMask;
- for (vector<uint8>::const_iterator i = digest.begin(); i != digest.end();
- ++i) {
+ for (size_t i = 0; i < sizeof(digest.a); ++i) {
hash = hash << 8;
- hash = hash | (*i & kHashMask);
+ hash = hash | (digest.a[i] & kHashMask);
mask = mask >> 8;
if (0 == mask)
break;
@@ -351,12 +352,16 @@ void UserSettings::StoreHashedPassword(const string& email,
const string& password) {
// Save one-way hashed password:
char binary_salt[kSaltSize];
- GetRandomBytes(binary_salt, sizeof(binary_salt));
+ base::RandBytes(binary_salt, sizeof(binary_salt));
const string salt = APEncode(string(binary_salt, sizeof(binary_salt)));
- MD5Calculator md5;
- md5.AddData(salt.data(), salt.size());
- md5.AddData(password.data(), password.size());
+ MD5Context md5_context;
+ MD5Init(&md5_context);
+ MD5Update(&md5_context, salt.data(), salt.size());
+ MD5Update(&md5_context, password.data(), password.size());
+ MD5Digest md5_digest;
+ MD5Final(&md5_digest, &md5_context);
+
ScopedDBHandle dbhandle(this);
SQLTransaction transaction(dbhandle.get());
transaction.BeginExclusive();
@@ -367,7 +372,7 @@ void UserSettings::StoreHashedPassword(const string& email,
" values ( ?, ?, ? )");
statement.bind_string(0, email);
statement.bind_string(1, PASSWORD_HASH);
- statement.bind_int(2, GetHashFromDigest(md5.GetDigest()));
+ statement.bind_int(2, GetHashFromDigest(md5_digest));
if (SQLITE_DONE != statement.step()) {
LOG(FATAL) << sqlite3_errmsg(dbhandle.get());
}
@@ -413,10 +418,13 @@ bool UserSettings::VerifyAgainstStoredHash(const string& email,
CHECK(SQLITE_DONE == query_result);
if (salt.empty() || hash == kInvalidHash)
return false;
- MD5Calculator md5;
- md5.AddData(salt.data(), salt.size());
- md5.AddData(password.data(), password.size());
- return hash == GetHashFromDigest(md5.GetDigest());
+ MD5Context md5_context;
+ MD5Init(&md5_context);
+ MD5Update(&md5_context, salt.data(), salt.size());
+ MD5Update(&md5_context, password.data(), password.size());
+ MD5Digest md5_digest;
+ MD5Final(&md5_digest, &md5_context);
+ return hash == GetHashFromDigest(md5_digest);
}
void UserSettings::SwitchUser(const string& username) {
diff --git a/chrome/browser/sync/util/user_settings_win.cc b/chrome/browser/sync/util/user_settings_win.cc
index 94090ac..d2b0447 100644
--- a/chrome/browser/sync/util/user_settings_win.cc
+++ b/chrome/browser/sync/util/user_settings_win.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -7,7 +7,6 @@
#include <string>
#include "base/logging.h"
-#include "chrome/browser/sync/util/crypto_helpers.h"
#include "chrome/browser/sync/util/data_encryption.h"
#include "chrome/common/sqlite_utils.h"