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

Side by Side Diff: chrome/browser/prefs/pref_hash_calculator.cc

Issue 110523006: Fix the hash generation algorithm to be consistent with prior implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use Verify instead of manual hash comparison. Created 6 years, 11 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 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/prefs/pref_hash_calculator.h" 5 #include "chrome/browser/prefs/pref_hash_calculator.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/json/json_string_value_serializer.h" 9 #include "base/json/json_string_value_serializer.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "base/values.h" 14 #include "base/values.h"
15 #include "crypto/hmac.h" 15 #include "crypto/hmac.h"
16 16
17 namespace { 17 namespace {
18 18
19 // Calculates an HMAC of |message| using |key|, encoded as a hexadecimal string.
20 std::string GetDigestString(const std::string& key,
21 const std::string& message) {
22 crypto::HMAC hmac(crypto::HMAC::SHA256);
23 std::vector<uint8> digest(hmac.DigestLength());
24 if (!hmac.Init(key) || !hmac.Sign(message, &digest[0], digest.size())) {
erikwright (departed) 2014/01/10 15:20:02 After discussion with Ryan, I understand the follo
Bernhard Bauer 2014/01/10 15:51:57 NOTREACHED() is fine with me.
25 NOTREACHED();
26 return std::string();
27 }
28 return base::HexEncode(digest.data(), digest.size());
29 }
30
31 // Validates an HMAC of |message| using |key|, encoded as a hexadecimal string.
gab 2014/01/10 15:56:45 Seems like you just took GetDigestString's comment
erikwright (departed) 2014/01/10 16:10:34 I did, indeed. This method is GetDigestString with
32 bool VerifyDigestString(const std::string& key,
33 const std::string& message,
34 const std::string& digest_string) {
35 crypto::HMAC hmac(crypto::HMAC::SHA256);
36 std::vector<uint8> digest;
37 return base::HexStringToBytes(digest_string, &digest) &&
38 hmac.Init(key) &&
39 hmac.Verify(message,
40 base::StringPiece(reinterpret_cast<char*>(&digest[0]),
41 digest.size()));
42 }
43
19 // Renders |value| as a string. |value| may be NULL, in which case the result 44 // Renders |value| as a string. |value| may be NULL, in which case the result
20 // is an empty string. 45 // is an empty string.
21 std::string ValueAsString(const base::Value* value) { 46 std::string ValueAsString(const base::Value* value) {
22 // Dictionary values may contain empty lists and sub-dictionaries. Make a 47 // Dictionary values may contain empty lists and sub-dictionaries. Make a
23 // deep copy with those removed to make the hash more stable. 48 // deep copy with those removed to make the hash more stable.
24 const base::DictionaryValue* dict_value; 49 const base::DictionaryValue* dict_value;
25 scoped_ptr<DictionaryValue> canonical_dict_value; 50 scoped_ptr<DictionaryValue> canonical_dict_value;
26 if (value && value->GetAsDictionary(&dict_value)) { 51 if (value && value->GetAsDictionary(&dict_value)) {
27 canonical_dict_value.reset(dict_value->DeepCopyWithoutEmptyChildren()); 52 canonical_dict_value.reset(dict_value->DeepCopyWithoutEmptyChildren());
28 value = canonical_dict_value.get(); 53 value = canonical_dict_value.get();
29 } 54 }
30 55
31 std::string value_as_string; 56 std::string value_as_string;
32 if (value) { 57 if (value) {
33 JSONStringValueSerializer serializer(&value_as_string); 58 JSONStringValueSerializer serializer(&value_as_string);
34 serializer.Serialize(*value); 59 serializer.Serialize(*value);
35 } 60 }
36 61
37 return value_as_string; 62 return value_as_string;
38 } 63 }
39 64
40 // Common helper for all hash algorithms. 65 // Common helper for all hash algorithms.
41 std::string CalculateFromValueAndComponents( 66 std::string GetMessageFromValueAndComponents(
42 const std::string& seed,
43 const base::Value* value, 67 const base::Value* value,
44 const std::vector<std::string>& extra_components) { 68 const std::vector<std::string>& extra_components) {
45 static const size_t kSHA256DigestSize = 32; 69 return JoinString(extra_components, "") + ValueAsString(value);
70 }
46 71
47 std::string message = JoinString(extra_components, "") + ValueAsString(value);
48 72
49 crypto::HMAC hmac(crypto::HMAC::SHA256); 73 // Generates a device ID based on the input device ID. The derived device ID has
50 unsigned char digest[kSHA256DigestSize]; 74 // no useful properties beyond those of the input device ID except that it is
51 if (!hmac.Init(seed) || !hmac.Sign(message, digest, arraysize(digest))) { 75 // consistent with previous implementations.
52 NOTREACHED(); 76 std::string GenerateDeviceIdLikePrefMetricsServiceDid(
77 const std::string& original_device_id) {
78 if (original_device_id.empty())
53 return std::string(); 79 return std::string();
54 } 80 return StringToLowerASCII(
81 GetDigestString(original_device_id, "PrefMetricsService"));
82 }
55 83
56 return base::HexEncode(digest, arraysize(digest)); 84 // Verifies a hash using a deprecated hash algorithm. For validating old
85 // hashes during migration.
86 bool VerifyLegacyHash(const std::string& seed,
87 const base::Value* value,
88 const std::string& digest_string) {
89 return VerifyDigestString(
90 seed,
91 GetMessageFromValueAndComponents(value, std::vector<std::string>()),
92 digest_string);
57 } 93 }
58 94
59 } // namespace 95 } // namespace
60 96
61 PrefHashCalculator::PrefHashCalculator(const std::string& seed, 97 PrefHashCalculator::PrefHashCalculator(const std::string& seed,
62 const std::string& device_id) 98 const std::string& device_id)
63 : seed_(seed), device_id_(device_id) {} 99 : seed_(seed),
100 device_id_(GenerateDeviceIdLikePrefMetricsServiceDid(device_id)) {}
64 101
65 std::string PrefHashCalculator::Calculate(const std::string& path, 102 std::string PrefHashCalculator::Calculate(const std::string& path,
66 const base::Value* value) const { 103 const base::Value* value) const {
104 return GetDigestString(seed_, GetMessage(path, value));
105 }
106
107 std::string PrefHashCalculator::GetMessage(const std::string& path,
gab 2014/01/10 15:56:45 Put GetMessage() impl at bottom of this file to ma
erikwright (departed) 2014/01/10 16:10:34 Done.
108 const base::Value* value) const {
67 std::vector<std::string> components; 109 std::vector<std::string> components;
68 if (!device_id_.empty()) 110 if (!device_id_.empty())
69 components.push_back(device_id_); 111 components.push_back(device_id_);
70 components.push_back(path); 112 components.push_back(path);
71 return CalculateFromValueAndComponents(seed_, value, components); 113 return GetMessageFromValueAndComponents(value, components);
72 } 114 }
73 115
74 PrefHashCalculator::ValidationResult PrefHashCalculator::Validate( 116 PrefHashCalculator::ValidationResult PrefHashCalculator::Validate(
75 const std::string& path, 117 const std::string& path,
76 const base::Value* value, 118 const base::Value* value,
77 const std::string& hash) const { 119 const std::string& digest_string) const {
78 if (hash == Calculate(path, value)) 120 if (VerifyDigestString(seed_, GetMessage(path, value), digest_string))
79 return VALID; 121 return VALID;
80 if (hash == CalculateLegacyHash(path, value)) 122 if (VerifyLegacyHash(seed_, value, digest_string))
81 return VALID_LEGACY; 123 return VALID_LEGACY;
82 return INVALID; 124 return INVALID;
83 } 125 }
84
85 std::string PrefHashCalculator::CalculateLegacyHash(
86 const std::string& path, const base::Value* value) const {
87 return CalculateFromValueAndComponents(seed_,
88 value,
89 std::vector<std::string>());
90 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698