peizhen guo | 0401e3e | 2014-08-12 13:24:30 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California |
| 4 | * |
| 5 | * This file is part of NSL (NDN Signature Logger). |
| 6 | * See AUTHORS.md for complete list of NSL authors and contributors. |
| 7 | * |
| 8 | * NSL is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NSL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NSL, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Peizhen Guo <patrick.guopz@gmail.com> |
| 20 | */ |
| 21 | #include "auditor.hpp" |
| 22 | |
| 23 | namespace nsl { |
| 24 | |
| 25 | ndn::ConstBufferPtr |
| 26 | Auditor::computeHash(ndn::ConstBufferPtr hash_l, ndn::ConstBufferPtr hash_r) |
| 27 | { |
| 28 | ndn::Buffer tmp_buf = *hash_l; |
| 29 | for (int i = 0; i < hash_r->size(); i++) |
| 30 | { |
| 31 | tmp_buf.push_back((*hash_r)[i]); |
| 32 | } |
| 33 | ndn::ConstBufferPtr digest = ndn::crypto::sha256(tmp_buf.buf(), tmp_buf.size()); |
| 34 | return digest; |
| 35 | } |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | ndn::ConstBufferPtr |
| 42 | Auditor::computeHashOneSide(ndn::ConstBufferPtr hash_l) |
| 43 | { |
| 44 | ndn::ConstBufferPtr digest = ndn::crypto::sha256(hash_l->buf(), hash_l->size()); |
| 45 | return digest; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | bool |
| 54 | Auditor::verifyConsistency(uint64_t version1, uint64_t version2, ndn::ConstBufferPtr hash1, |
| 55 | ndn::ConstBufferPtr hash2, std::vector<ConstNodePtr> proof) |
| 56 | { |
| 57 | // find version2's level |
| 58 | uint64_t levelVer2 = 1; |
| 59 | uint64_t ver2 = version2; |
| 60 | while(ver2 >= 1) |
| 61 | { |
| 62 | ver2 = ver2 / 2; |
| 63 | levelVer2 += 1; |
| 64 | } |
| 65 | |
| 66 | // compare version2's hash |
| 67 | ndn::ConstBufferPtr hash_l; |
| 68 | ndn::ConstBufferPtr hash_r; |
| 69 | ndn::ConstBufferPtr tmp_hash; |
| 70 | Index tmp_idx = proof[0]->getIndex(); |
| 71 | int isRight = tmp_idx.number % int(pow(2, tmp_idx.level + 1)); |
| 72 | if (isRight != 0) |
| 73 | hash_r = proof[0]->getHash(); |
| 74 | else |
| 75 | hash_l = proof[0]->getHash(); |
| 76 | uint64_t i_ = 1; |
| 77 | for (; tmp_idx.level < levelVer2 - 1; ) |
| 78 | { |
| 79 | if (isRight != 0) |
| 80 | { |
| 81 | hash_l = proof[i_]->getHash(); |
| 82 | tmp_hash = computeHash(hash_l, hash_r); |
| 83 | i_++; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | tmp_hash = computeHashOneSide(hash_l); |
| 88 | } |
| 89 | tmp_idx.level += 1; |
| 90 | tmp_idx.number -= tmp_idx.number % int(pow(2, tmp_idx.level)); |
| 91 | isRight = tmp_idx.number % int(pow(2, tmp_idx.level + 1)); |
| 92 | if (isRight != 0) |
| 93 | { |
| 94 | hash_r = tmp_hash; |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | hash_l = tmp_hash; |
| 99 | } |
| 100 | } |
| 101 | bool hash2_consis = true; |
| 102 | if (isRight != 0) |
| 103 | { |
| 104 | for (int i = 0; i < hash_r->size() ; i++) |
| 105 | { |
| 106 | if ((*hash2)[i] != (*hash_r)[i]) |
| 107 | { |
| 108 | hash2_consis = false; |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | for (int i = 0; i < hash_l->size() ; i++) |
| 116 | { |
| 117 | if ((*hash2)[i] != (*hash_l)[i]) |
| 118 | { |
| 119 | hash2_consis = false; |
| 120 | break; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | // compare hash1 |
| 129 | tmp_idx = proof[i_]->getIndex(); |
| 130 | isRight = tmp_idx.number % int(pow(2, tmp_idx.level + 1)); |
| 131 | if (isRight != 0) |
| 132 | hash_r = proof[i_]->getHash(); |
| 133 | else |
| 134 | hash_l = proof[i_]->getHash(); |
| 135 | i_++; |
| 136 | for (; i_ < proof.size(); ) |
| 137 | { |
| 138 | if (isRight != 0) |
| 139 | { |
| 140 | hash_l = proof[i_]->getHash(); |
| 141 | tmp_hash = computeHash(hash_l, hash_r); |
| 142 | i_++; |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | tmp_hash = computeHashOneSide(hash_l); |
| 147 | } |
| 148 | tmp_idx.level += 1; |
| 149 | tmp_idx.number -= tmp_idx.number % int(pow(2, tmp_idx.level)); |
| 150 | isRight = tmp_idx.number % int(pow(2, tmp_idx.level + 1)); |
| 151 | if (isRight != 0) |
| 152 | { |
| 153 | hash_r = tmp_hash; |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | hash_l = tmp_hash; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | bool hash1_consis = true; |
| 162 | if (isRight != 0) |
| 163 | { |
| 164 | for (int i = 0; i < hash_r->size() ; i++) |
| 165 | { |
| 166 | if ((*hash1)[i] != (*hash_r)[i]) |
| 167 | { |
| 168 | hash1_consis = false; |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | for (int i = 0; i < hash_l->size() ; i++) |
| 176 | { |
| 177 | if ((*hash1)[i] != (*hash_l)[i]) |
| 178 | { |
| 179 | hash1_consis = false; |
| 180 | break; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return hash1_consis && hash2_consis; |
| 186 | |
| 187 | } |
| 188 | |
| 189 | |
| 190 | } // namespace nsl |