Index: net/cert/ct_log_verifier_unittest.cc |
diff --git a/net/cert/ct_log_verifier_unittest.cc b/net/cert/ct_log_verifier_unittest.cc |
index 24c704a8565545c4c6c3efc6a1d367a750646d82..8be1a40d1996e74c50592bf89c1dacbef8fa0697 100644 |
--- a/net/cert/ct_log_verifier_unittest.cc |
+++ b/net/cert/ct_log_verifier_unittest.cc |
@@ -37,6 +37,11 @@ uint64_t CalculateNearestPowerOfTwo(uint64_t n) { |
return ret; |
} |
+// Copies a raw SHA-256 hash (32 bytes) into a std::string. |
+std::string HashAsStr(const uint8_t (&hash)[32]) { |
+ return std::string(std::begin(hash), std::end(hash)); |
+} |
+ |
// A single consistency proof. Contains the old and new tree sizes |
// (snapshot1 and snapshot2), the length of the proof (proof_length) and |
// at most 3 proof nodes (all test proofs will be for a tree of size 8). |
@@ -44,7 +49,7 @@ struct ProofTestVector { |
uint64_t snapshot1; |
uint64_t snapshot2; |
size_t proof_length; |
- const char* const proof[3]; |
+ const uint8_t proof[3][32]; |
}; |
// All test data replicated from |
@@ -57,15 +62,31 @@ const uint8_t kSHA256EmptyTreeHash[32] = { |
// Node hashes for a sample tree of size 8 (each element in this array is |
// a node hash, not leaf data; order represents order of the nodes in the tree). |
-const char* const kSHA256Roots[8] = { |
- "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", |
- "fac54203e7cc696cf0dfcb42c92a1d9dbaf70ad9e621f4bd8d98662f00e3c125", |
- "aeb6bcfe274b70a14fb067a5e5578264db0fa9b51af5e0ba159158f329e06e77", |
- "d37ee418976dd95753c1c73862b9398fa2a2cf9b4ff0fdfe8b30cd95209614b7", |
- "4e3bbb1f7b478dcfe71fb631631519a3bca12c9aefca1612bfce4c13a86264d4", |
- "76e67dadbcdf1e10e1b74ddc608abd2f98dfb16fbce75277b5232a127f2087ef", |
- "ddb89be403809e325750d3d263cd78929c2942b7942a34b77e122c9594a74c8c", |
- "5dc9da79a70659a9ad559cb701ded9a2ab9d823aad2f4960cfe370eff4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 0xdf, 0x1e, 0x10, 0xe1, 0xb7, 0x4d, |
+ 0xdc, 0x60, 0x8a, 0xbd, 0x2f, 0x98, 0xdf, 0xb1, 0x6f, 0xbc, 0xe7, |
+ 0x52, 0x77, 0xb5, 0x23, 0x2a, 0x12, 0x7f, 0x20, 0x87, 0xef}, |
+ {0xdd, 0xb8, 0x9b, 0xe4, 0x03, 0x80, 0x9e, 0x32, 0x57, 0x50, 0xd3, |
+ 0xd2, 0x63, 0xcd, 0x78, 0x92, 0x9c, 0x29, 0x42, 0xb7, 0x94, 0x2a, |
+ 0x34, 0xb7, 0x7e, 0x12, 0x2c, 0x95, 0x94, 0xa7, 0x4c, 0x8c}, |
+ {0x5d, 0xc9, 0xda, 0x79, 0xa7, 0x06, 0x59, 0xa9, 0xad, 0x55, 0x9c, |
+ 0xb7, 0x01, 0xde, 0xd9, 0xa2, 0xab, 0x9d, 0x82, 0x3a, 0xad, 0x2f, |
+ 0x49, 0x60, 0xcf, 0xe3, 0x70, 0xef, 0xf4, 0x60, 0x43, 0x28}}; |
// A collection of consistency proofs between various sub-trees of the tree |
// defined by |kSHA256Roots|. |
@@ -77,38 +98,40 @@ const ProofTestVector kSHA256Proofs[4] = { |
{1, |
8, |
3, |
- {"96a296d224f285c67bee93c30f8a309157f0daa35dc5b87e410b78630a09cfc7", |
- "5f083f0a1a33ca076a95279832580db3e0ef4584bdff1f54c8a360f50de3031e", |
- "6b47aaf29ee3c2af9af889bc1fb9254dabd31177f16232dd6aab035ca39bf6e4"}}, |
+ {{0x96, 0xa2, 0x96, 0xd2, 0x24, 0xf2, 0x85, 0xc6, 0x7b, 0xee, 0x93, |
+ 0xc3, 0x0f, 0x8a, 0x30, 0x91, 0x57, 0xf0, 0xda, 0xa3, 0x5d, 0xc5, |
+ 0xb8, 0x7e, 0x41, 0x0b, 0x78, 0x63, 0x0a, 0x09, 0xcf, 0xc7}, |
+ {0x5f, 0x08, 0x3f, 0x0a, 0x1a, 0x33, 0xca, 0x07, 0x6a, 0x95, 0x27, |
+ 0x98, 0x32, 0x58, 0x0d, 0xb3, 0xe0, 0xef, 0x45, 0x84, 0xbd, 0xff, |
+ 0x1f, 0x54, 0xc8, 0xa3, 0x60, 0xf5, 0x0d, 0xe3, 0x03, 0x1e}, |
+ {0x6b, 0x47, 0xaa, 0xf2, 0x9e, 0xe3, 0xc2, 0xaf, 0x9a, 0xf8, 0x89, |
+ 0xbc, 0x1f, 0xb9, 0x25, 0x4d, 0xab, 0xd3, 0x11, 0x77, 0xf1, 0x62, |
+ 0x32, 0xdd, 0x6a, 0xab, 0x03, 0x5c, 0xa3, 0x9b, 0xf6, 0xe4}}}, |
// Consistency proof between tree of size 6 and tree of size 8, with 3 |
// nodes in the proof. |
{6, |
8, |
3, |
- {"0ebc5d3437fbe2db158b9f126a1d118e308181031d0a949f8dededebc558ef6a", |
- "ca854ea128ed050b41b35ffc1b87b8eb2bde461e9e3b5596ece6b9d5975a0ae0", |
- "d37ee418976dd95753c1c73862b9398fa2a2cf9b4ff0fdfe8b30cd95209614b7"}}, |
+ {{0x0e, 0xbc, 0x5d, 0x34, 0x37, 0xfb, 0xe2, 0xdb, 0x15, 0x8b, 0x9f, |
+ 0x12, 0x6a, 0x1d, 0x11, 0x8e, 0x30, 0x81, 0x81, 0x03, 0x1d, 0x0a, |
+ 0x94, 0x9f, 0x8d, 0xed, 0xed, 0xeb, 0xc5, 0x58, 0xef, 0x6a}, |
+ {0xca, 0x85, 0x4e, 0xa1, 0x28, 0xed, 0x05, 0x0b, 0x41, 0xb3, 0x5f, |
+ 0xfc, 0x1b, 0x87, 0xb8, 0xeb, 0x2b, 0xde, 0x46, 0x1e, 0x9e, 0x3b, |
+ 0x55, 0x96, 0xec, 0xe6, 0xb9, 0xd5, 0x97, 0x5a, 0x0a, 0xe0}, |
+ {0xd3, 0x7e, 0xe4, 0x18, 0x97, 0x6d, 0xd9, 0x57, 0x53, 0xc1, 0xc7, |
+ 0x38, 0x62, 0xb9, 0x39, 0x8f, 0xa2, 0xa2, 0xcf, 0x9b, 0x4f, 0xf0, |
+ 0xfd, 0xfe, 0x8b, 0x30, 0xcd, 0x95, 0x20, 0x96, 0x14, 0xb7}}}, |
// Consistency proof between tree of size 2 and tree of size 5, with 2 |
// nodes in the proof. |
{2, |
5, |
2, |
- {"5f083f0a1a33ca076a95279832580db3e0ef4584bdff1f54c8a360f50de3031e", |
- "bc1a0643b12e4d2d7c77918f44e0f4f79a838b6cf9ec5b5c283e1f4d88599e6b", ""}}}; |
- |
-// Decodes a hexadecimal string into the binary data it represents. |
-std::string HexToBytes(const std::string& hex_data) { |
- std::vector<uint8_t> output; |
- std::string result; |
- if (base::HexStringToBytes(hex_data, &output)) |
- result.assign(output.begin(), output.end()); |
- return result; |
-} |
- |
-std::string GetEmptyTreeHash() { |
- return std::string(std::begin(kSHA256EmptyTreeHash), |
- std::end(kSHA256EmptyTreeHash)); |
-} |
+ {{0x5f, 0x08, 0x3f, 0x0a, 0x1a, 0x33, 0xca, 0x07, 0x6a, 0x95, 0x27, |
+ 0x98, 0x32, 0x58, 0x0d, 0xb3, 0xe0, 0xef, 0x45, 0x84, 0xbd, 0xff, |
+ 0x1f, 0x54, 0xc8, 0xa3, 0x60, 0xf5, 0x0d, 0xe3, 0x03, 0x1e}, |
+ {0xbc, 0x1a, 0x06, 0x43, 0xb1, 0x2e, 0x4d, 0x2d, 0x7c, 0x77, 0x91, |
+ 0x8f, 0x44, 0xe0, 0xf4, 0xf7, 0x9a, 0x83, 0x8b, 0x6c, 0xf9, 0xec, |
+ 0x5b, 0x5c, 0x28, 0x3e, 0x1f, 0x4d, 0x88, 0x59, 0x9e, 0x6b}}}}; |
// Creates a ct::MerkleConsistencyProof and returns the result of |
// calling log->VerifyConsistencyProof with that proof and snapshots. |
@@ -193,7 +216,7 @@ class CTLogVerifierTest : public ::testing::Test { |
// Modify a single element in the proof. |
for (size_t j = 0; j < proof.size(); ++j) { |
wrong_proof = proof; |
- wrong_proof[j] = GetEmptyTreeHash(); |
+ wrong_proof[j] = HashAsStr(kSHA256EmptyTreeHash); |
EXPECT_FALSE(VerifyConsistencyProof(log_, snapshot1, root1, snapshot2, |
root2, wrong_proof)); |
} |
@@ -315,7 +338,8 @@ TEST_F(CTLogVerifierTest, ExcessDataInPublicKey) { |
TEST_F(CTLogVerifierTest, VerifiesConsistencyProofEdgeCases_EmptyProof) { |
std::vector<std::string> empty_proof; |
- std::string root1(GetEmptyTreeHash()), root2(GetEmptyTreeHash()); |
+ std::string root1(HashAsStr(kSHA256EmptyTreeHash)), |
+ root2(HashAsStr(kSHA256EmptyTreeHash)); |
// Snapshots that are always consistent, because they are either |
// from an empty tree to a non-empty one or for trees of the same |
@@ -335,7 +359,7 @@ TEST_F(CTLogVerifierTest, VerifiesConsistencyProofEdgeCases_EmptyProof) { |
TEST_F(CTLogVerifierTest, VerifiesConsistencyProofEdgeCases_MismatchingRoots) { |
std::vector<std::string> empty_proof; |
std::string root2; |
- const std::string empty_tree_hash(GetEmptyTreeHash()); |
+ const std::string empty_tree_hash(HashAsStr(kSHA256EmptyTreeHash)); |
// Roots don't match. |
EXPECT_FALSE( |
@@ -346,7 +370,7 @@ TEST_F(CTLogVerifierTest, VerifiesConsistencyProofEdgeCases_MismatchingRoots) { |
TEST_F(CTLogVerifierTest, |
VerifiesConsistencyProofEdgeCases_MatchingRootsNonEmptyProof) { |
- const std::string empty_tree_hash(GetEmptyTreeHash()); |
+ const std::string empty_tree_hash(HashAsStr(kSHA256EmptyTreeHash)); |
std::vector<std::string> proof; |
proof.push_back(empty_tree_hash); |
@@ -365,21 +389,18 @@ TEST_F(CTLogVerifierTest, |
TEST_F(CTLogVerifierTest, VerifiesValidConsistencyProofs) { |
std::vector<std::string> proof; |
- std::string root1, root2; |
// Known good proofs. |
for (size_t i = 0; i < arraysize(kSHA256Proofs); ++i) { |
proof.clear(); |
for (size_t j = 0; j < kSHA256Proofs[i].proof_length; ++j) { |
- const char* const v = kSHA256Proofs[i].proof[j]; |
- proof.push_back(HexToBytes(v)); |
+ proof.push_back(HashAsStr(kSHA256Proofs[i].proof[j])); |
} |
const uint64_t snapshot1 = kSHA256Proofs[i].snapshot1; |
const uint64_t snapshot2 = kSHA256Proofs[i].snapshot2; |
- const char* const old_root = kSHA256Roots[snapshot1 - 1]; |
- const char* const new_root = kSHA256Roots[snapshot2 - 1]; |
- VerifierConsistencyCheck(snapshot1, snapshot2, HexToBytes(old_root), |
- HexToBytes(new_root), proof); |
+ const std::string old_root = HashAsStr(kSHA256Roots[snapshot1 - 1]); |
+ const std::string new_root = HashAsStr(kSHA256Roots[snapshot2 - 1]); |
+ VerifierConsistencyCheck(snapshot1, snapshot2, old_root, new_root, proof); |
} |
} |
@@ -411,7 +432,7 @@ class TreeHasher { |
// specified in |inputs|. |
std::string ReferenceMerkleTreeHash(std::string* inputs, uint64_t input_size) { |
if (!input_size) |
- return GetEmptyTreeHash(); |
+ return HashAsStr(kSHA256EmptyTreeHash); |
if (input_size == 1) |
return TreeHasher::HashLeaf(inputs[0]); |