OLD | NEW |
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 "components/user_prefs/tracked/pref_hash_calculator.h" | 5 #include "components/user_prefs/tracked/pref_hash_calculator.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
7 #include <vector> | 9 #include <vector> |
8 | 10 |
9 #include "base/bind.h" | 11 #include "base/bind.h" |
10 #include "base/json/json_string_value_serializer.h" | 12 #include "base/json/json_string_value_serializer.h" |
11 #include "base/logging.h" | 13 #include "base/logging.h" |
12 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
14 #include "base/values.h" | 16 #include "base/values.h" |
15 #include "crypto/hmac.h" | 17 #include "crypto/hmac.h" |
16 | 18 |
17 namespace { | 19 namespace { |
18 | 20 |
19 // Calculates an HMAC of |message| using |key|, encoded as a hexadecimal string. | 21 // Calculates an HMAC of |message| using |key|, encoded as a hexadecimal string. |
20 std::string GetDigestString(const std::string& key, | 22 std::string GetDigestString(const std::string& key, |
21 const std::string& message) { | 23 const std::string& message) { |
22 crypto::HMAC hmac(crypto::HMAC::SHA256); | 24 crypto::HMAC hmac(crypto::HMAC::SHA256); |
23 std::vector<uint8> digest(hmac.DigestLength()); | 25 std::vector<uint8_t> digest(hmac.DigestLength()); |
24 if (!hmac.Init(key) || !hmac.Sign(message, &digest[0], digest.size())) { | 26 if (!hmac.Init(key) || !hmac.Sign(message, &digest[0], digest.size())) { |
25 NOTREACHED(); | 27 NOTREACHED(); |
26 return std::string(); | 28 return std::string(); |
27 } | 29 } |
28 return base::HexEncode(&digest[0], digest.size()); | 30 return base::HexEncode(&digest[0], digest.size()); |
29 } | 31 } |
30 | 32 |
31 // Verifies that |digest_string| is a valid HMAC of |message| using |key|. | 33 // Verifies that |digest_string| is a valid HMAC of |message| using |key|. |
32 // |digest_string| must be encoded as a hexadecimal string. | 34 // |digest_string| must be encoded as a hexadecimal string. |
33 bool VerifyDigestString(const std::string& key, | 35 bool VerifyDigestString(const std::string& key, |
34 const std::string& message, | 36 const std::string& message, |
35 const std::string& digest_string) { | 37 const std::string& digest_string) { |
36 crypto::HMAC hmac(crypto::HMAC::SHA256); | 38 crypto::HMAC hmac(crypto::HMAC::SHA256); |
37 std::vector<uint8> digest; | 39 std::vector<uint8_t> digest; |
38 return base::HexStringToBytes(digest_string, &digest) && hmac.Init(key) && | 40 return base::HexStringToBytes(digest_string, &digest) && hmac.Init(key) && |
39 hmac.Verify(message, | 41 hmac.Verify(message, |
40 base::StringPiece(reinterpret_cast<char*>(&digest[0]), | 42 base::StringPiece(reinterpret_cast<char*>(&digest[0]), |
41 digest.size())); | 43 digest.size())); |
42 } | 44 } |
43 | 45 |
44 // Renders |value| as a string. |value| may be NULL, in which case the result | 46 // Renders |value| as a string. |value| may be NULL, in which case the result |
45 // is an empty string. This method can be expensive and its result should be | 47 // is an empty string. This method can be expensive and its result should be |
46 // re-used rather than recomputed where possible. | 48 // re-used rather than recomputed where possible. |
47 std::string ValueAsString(const base::Value* value) { | 49 std::string ValueAsString(const base::Value* value) { |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 digest_string)) { | 118 digest_string)) { |
117 return VALID; | 119 return VALID; |
118 } | 120 } |
119 if (VerifyDigestString(seed_, | 121 if (VerifyDigestString(seed_, |
120 GetMessage(legacy_device_id_, path, value_as_string), | 122 GetMessage(legacy_device_id_, path, value_as_string), |
121 digest_string)) { | 123 digest_string)) { |
122 return VALID_SECURE_LEGACY; | 124 return VALID_SECURE_LEGACY; |
123 } | 125 } |
124 return INVALID; | 126 return INVALID; |
125 } | 127 } |
OLD | NEW |