| 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 "net/cert/ct_log_verifier.h" | 5 #include "net/cert/ct_log_verifier.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 }; | 48 }; |
| 49 | 49 |
| 50 // All test data replicated from | 50 // All test data replicated from |
| 51 // https://github.com/google/certificate-transparency/blob/c41b090ecc14ddd6b3531
dc7e5ce36b21e253fdd/cpp/merkletree/merkle_tree_test.cc | 51 // https://github.com/google/certificate-transparency/blob/c41b090ecc14ddd6b3531
dc7e5ce36b21e253fdd/cpp/merkletree/merkle_tree_test.cc |
| 52 // A hash of the empty string. | 52 // A hash of the empty string. |
| 53 const uint8_t kSHA256EmptyTreeHash[32] = { | 53 const uint8_t kSHA256EmptyTreeHash[32] = { |
| 54 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, | 54 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, |
| 55 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, | 55 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, |
| 56 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55}; | 56 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55}; |
| 57 | 57 |
| 58 // Node hashes for a sample tree of size 8 (each element in this array is | 58 // Root hashes from building the sample tree of size 8 leaf-by-leaf. |
| 59 // a node hash, not leaf data; order represents order of the nodes in the tree). | 59 // The first entry is the root at size 0, the last is the root at size 8. |
| 60 const char* const kSHA256Roots[8] = { | 60 const char* const kSHA256Roots[8] = { |
| 61 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", | 61 "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d", |
| 62 "fac54203e7cc696cf0dfcb42c92a1d9dbaf70ad9e621f4bd8d98662f00e3c125", | 62 "fac54203e7cc696cf0dfcb42c92a1d9dbaf70ad9e621f4bd8d98662f00e3c125", |
| 63 "aeb6bcfe274b70a14fb067a5e5578264db0fa9b51af5e0ba159158f329e06e77", | 63 "aeb6bcfe274b70a14fb067a5e5578264db0fa9b51af5e0ba159158f329e06e77", |
| 64 "d37ee418976dd95753c1c73862b9398fa2a2cf9b4ff0fdfe8b30cd95209614b7", | 64 "d37ee418976dd95753c1c73862b9398fa2a2cf9b4ff0fdfe8b30cd95209614b7", |
| 65 "4e3bbb1f7b478dcfe71fb631631519a3bca12c9aefca1612bfce4c13a86264d4", | 65 "4e3bbb1f7b478dcfe71fb631631519a3bca12c9aefca1612bfce4c13a86264d4", |
| 66 "76e67dadbcdf1e10e1b74ddc608abd2f98dfb16fbce75277b5232a127f2087ef", | 66 "76e67dadbcdf1e10e1b74ddc608abd2f98dfb16fbce75277b5232a127f2087ef", |
| 67 "ddb89be403809e325750d3d263cd78929c2942b7942a34b77e122c9594a74c8c", | 67 "ddb89be403809e325750d3d263cd78929c2942b7942a34b77e122c9594a74c8c", |
| 68 "5dc9da79a70659a9ad559cb701ded9a2ab9d823aad2f4960cfe370eff4604328"}; | 68 "5dc9da79a70659a9ad559cb701ded9a2ab9d823aad2f4960cfe370eff4604328"}; |
| 69 | 69 |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 EXPECT_FALSE(VerifyConsistencyProof(log_, 1, empty_tree_hash, 1, | 362 EXPECT_FALSE(VerifyConsistencyProof(log_, 1, empty_tree_hash, 1, |
| 363 empty_tree_hash, proof)); | 363 empty_tree_hash, proof)); |
| 364 } | 364 } |
| 365 | 365 |
| 366 TEST_F(CTLogVerifierTest, VerifiesValidConsistencyProofs) { | 366 TEST_F(CTLogVerifierTest, VerifiesValidConsistencyProofs) { |
| 367 std::vector<std::string> proof; | 367 std::vector<std::string> proof; |
| 368 std::string root1, root2; | 368 std::string root1, root2; |
| 369 | 369 |
| 370 // Known good proofs. | 370 // Known good proofs. |
| 371 for (size_t i = 0; i < arraysize(kSHA256Proofs); ++i) { | 371 for (size_t i = 0; i < arraysize(kSHA256Proofs); ++i) { |
| 372 SCOPED_TRACE(i); |
| 372 proof.clear(); | 373 proof.clear(); |
| 373 for (size_t j = 0; j < kSHA256Proofs[i].proof_length; ++j) { | 374 for (size_t j = 0; j < kSHA256Proofs[i].proof_length; ++j) { |
| 374 const char* const v = kSHA256Proofs[i].proof[j]; | 375 const char* const v = kSHA256Proofs[i].proof[j]; |
| 375 proof.push_back(HexToBytes(v)); | 376 proof.push_back(HexToBytes(v)); |
| 376 } | 377 } |
| 377 const uint64_t snapshot1 = kSHA256Proofs[i].snapshot1; | 378 const uint64_t snapshot1 = kSHA256Proofs[i].snapshot1; |
| 378 const uint64_t snapshot2 = kSHA256Proofs[i].snapshot2; | 379 const uint64_t snapshot2 = kSHA256Proofs[i].snapshot2; |
| 379 const char* const old_root = kSHA256Roots[snapshot1 - 1]; | 380 const char* const old_root = kSHA256Roots[snapshot1 - 1]; |
| 380 const char* const new_root = kSHA256Roots[snapshot2 - 1]; | 381 const char* const new_root = kSHA256Roots[snapshot2 - 1]; |
| 381 VerifierConsistencyCheck(snapshot1, snapshot2, HexToBytes(old_root), | 382 VerifierConsistencyCheck(snapshot1, snapshot2, HexToBytes(old_root), |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 } | 469 } |
| 469 return proof; | 470 return proof; |
| 470 } | 471 } |
| 471 | 472 |
| 472 class CTLogVerifierTestUsingReferenceGenerator | 473 class CTLogVerifierTestUsingReferenceGenerator |
| 473 : public CTLogVerifierTest, | 474 : public CTLogVerifierTest, |
| 474 public ::testing::WithParamInterface<uint64_t> {}; | 475 public ::testing::WithParamInterface<uint64_t> {}; |
| 475 | 476 |
| 476 const uint64_t kReferenceTreeSize = 256; | 477 const uint64_t kReferenceTreeSize = 256; |
| 477 | 478 |
| 479 // Tests that every possible valid consistency proof for a tree of a given size |
| 480 // verifies correctly. Also checks that invalid variations of each proof fail to |
| 481 // verify (see VerifierConsistencyCheck). |
| 478 TEST_P(CTLogVerifierTestUsingReferenceGenerator, | 482 TEST_P(CTLogVerifierTestUsingReferenceGenerator, |
| 479 VerifiesValidConsistencyProof) { | 483 VerifiesValidConsistencyProof) { |
| 480 std::vector<std::string> data; | 484 std::vector<std::string> data; |
| 481 for (uint64_t i = 0; i < kReferenceTreeSize; ++i) | 485 for (uint64_t i = 0; i < kReferenceTreeSize; ++i) |
| 482 data.push_back(std::string(1, static_cast<char>(i))); | 486 data.push_back(std::string(1, static_cast<char>(i))); |
| 483 | 487 |
| 484 const uint64_t tree_size = GetParam(); | 488 const uint64_t tree_size = GetParam(); |
| 485 const std::string tree_root = ReferenceMerkleTreeHash(data.data(), tree_size); | 489 const std::string tree_root = ReferenceMerkleTreeHash(data.data(), tree_size); |
| 486 | 490 |
| 487 for (uint64_t snapshot = 1; snapshot <= tree_size; ++snapshot) { | 491 for (uint64_t snapshot = 1; snapshot <= tree_size; ++snapshot) { |
| 488 SCOPED_TRACE(snapshot); | 492 SCOPED_TRACE(snapshot); |
| 489 const std::string snapshot_root = | 493 const std::string snapshot_root = |
| 490 ReferenceMerkleTreeHash(data.data(), snapshot); | 494 ReferenceMerkleTreeHash(data.data(), snapshot); |
| 491 const std::vector<std::string> proof = | 495 const std::vector<std::string> proof = |
| 492 ReferenceSnapshotConsistency(data.data(), tree_size, snapshot, true); | 496 ReferenceSnapshotConsistency(data.data(), tree_size, snapshot, true); |
| 493 VerifierConsistencyCheck(snapshot, tree_size, snapshot_root, tree_root, | 497 VerifierConsistencyCheck(snapshot, tree_size, snapshot_root, tree_root, |
| 494 proof); | 498 proof); |
| 495 } | 499 } |
| 496 } | 500 } |
| 497 | 501 |
| 498 // Test verification of consistency proofs between all tree sizes from 1 to 128. | 502 // Test verification of consistency proofs between all tree sizes from 1 to 128. |
| 499 INSTANTIATE_TEST_CASE_P(RangeOfTreeSizesAndSnapshots, | 503 INSTANTIATE_TEST_CASE_P(RangeOfTreeSizesAndSnapshots, |
| 500 CTLogVerifierTestUsingReferenceGenerator, | 504 CTLogVerifierTestUsingReferenceGenerator, |
| 501 testing::Range(UINT64_C(1), | 505 testing::Range(UINT64_C(1), |
| 502 (kReferenceTreeSize / 2) + 1)); | 506 (kReferenceTreeSize / 2) + 1)); |
| 503 | 507 |
| 504 } // namespace | 508 } // namespace |
| 505 | 509 |
| 506 } // namespace net | 510 } // namespace net |
| OLD | NEW |