Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame^] | 2 | /* |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "digest.hpp" |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 23 | #include "string-helper.hpp" |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame^] | 24 | #include "../security/detail/openssl.hpp" |
| 25 | #include "../security/transform/digest-filter.hpp" |
| 26 | #include "../security/transform/stream-sink.hpp" |
| 27 | #include "../security/transform/stream-source.hpp" |
| 28 | #include "../security/v1/cryptopp.hpp" |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 6800e1f | 2014-09-30 13:20:12 -0700 | [diff] [blame] | 30 | #include <sstream> |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
| 33 | namespace util { |
| 34 | |
| 35 | template<typename Hash> |
| 36 | Digest<Hash>::Digest() |
| 37 | { |
| 38 | reset(); |
| 39 | } |
| 40 | |
| 41 | template<typename Hash> |
Alexander Afanasyev | d27334f | 2015-07-01 21:44:36 -0700 | [diff] [blame] | 42 | Digest<Hash>::Digest(std::istream& is) |
| 43 | : m_isInProcess(false) |
| 44 | , m_isFinalized(true) |
| 45 | { |
| 46 | using namespace CryptoPP; |
| 47 | |
| 48 | m_buffer = make_shared<Buffer>(m_hash.DigestSize()); |
| 49 | FileSource(is, true, |
| 50 | new HashFilter(m_hash, |
| 51 | new ArraySink(m_buffer->get(), m_buffer->size()))); |
| 52 | } |
| 53 | |
| 54 | template<typename Hash> |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 55 | void |
| 56 | Digest<Hash>::reset() |
| 57 | { |
| 58 | m_hash.Restart(); |
| 59 | m_buffer = make_shared<Buffer>(m_hash.DigestSize()); |
| 60 | m_isInProcess = false; |
| 61 | m_isFinalized = false; |
| 62 | } |
| 63 | |
| 64 | template<typename Hash> |
| 65 | void |
| 66 | Digest<Hash>::finalize() |
| 67 | { |
| 68 | // return immediately if Digest is finalized already. |
| 69 | if (m_isFinalized) |
| 70 | return; |
| 71 | |
| 72 | m_hash.Final(m_buffer->get()); |
| 73 | |
| 74 | m_isFinalized = true; |
| 75 | } |
| 76 | |
| 77 | template<typename Hash> |
| 78 | ConstBufferPtr |
| 79 | Digest<Hash>::computeDigest() |
| 80 | { |
| 81 | finalize(); |
| 82 | return m_buffer; |
| 83 | } |
| 84 | |
| 85 | template<typename Hash> |
| 86 | bool |
| 87 | Digest<Hash>::operator==(Digest<Hash>& digest) |
| 88 | { |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 89 | const Buffer& lhs = *computeDigest(); |
| 90 | const Buffer& rhs = *digest.computeDigest(); |
| 91 | |
| 92 | if (lhs.size() != rhs.size()) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | // constant-time buffer comparison to mitigate timing attacks |
| 97 | return CRYPTO_memcmp(lhs.buf(), rhs.buf(), lhs.size()) == 0; |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | template<typename Hash> |
| 101 | Digest<Hash>& |
| 102 | Digest<Hash>::operator<<(Digest<Hash>& src) |
| 103 | { |
| 104 | ConstBufferPtr buffer = src.computeDigest(); |
| 105 | update(buffer->get(), buffer->size()); |
| 106 | |
| 107 | return *this; |
| 108 | } |
| 109 | |
| 110 | template<typename Hash> |
| 111 | Digest<Hash>& |
| 112 | Digest<Hash>::operator<<(const std::string& str) |
| 113 | { |
| 114 | update(reinterpret_cast<const uint8_t*>(str.c_str()), str.size()); |
| 115 | |
| 116 | return *this; |
| 117 | } |
| 118 | |
| 119 | template<typename Hash> |
| 120 | Digest<Hash>& |
| 121 | Digest<Hash>::operator<<(const Block& block) |
| 122 | { |
| 123 | update(block.wire(), block.size()); |
| 124 | |
| 125 | return *this; |
| 126 | } |
| 127 | |
| 128 | template<typename Hash> |
| 129 | Digest<Hash>& |
| 130 | Digest<Hash>::operator<<(uint64_t value) |
| 131 | { |
| 132 | update(reinterpret_cast<const uint8_t*>(&value), sizeof(uint64_t)); |
| 133 | |
| 134 | return *this; |
| 135 | } |
| 136 | |
| 137 | template<typename Hash> |
| 138 | void |
| 139 | Digest<Hash>::update(const uint8_t* buffer, size_t size) |
| 140 | { |
| 141 | // cannot update Digest when it has been finalized |
| 142 | if (m_isFinalized) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 143 | BOOST_THROW_EXCEPTION(Error("Digest has been already finalized")); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 144 | |
| 145 | m_hash.Update(buffer, size); |
| 146 | |
| 147 | m_isInProcess = true; |
| 148 | } |
| 149 | |
| 150 | template<typename Hash> |
| 151 | ConstBufferPtr |
| 152 | Digest<Hash>::computeDigest(const uint8_t* buffer, size_t size) |
| 153 | { |
| 154 | Hash hash; |
| 155 | BufferPtr result = make_shared<Buffer>(hash.DigestSize()); |
| 156 | hash.Update(buffer, size); |
| 157 | hash.Final(result->get()); |
| 158 | |
| 159 | return result; |
| 160 | } |
| 161 | |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 162 | template<typename Hash> |
| 163 | std::string |
| 164 | Digest<Hash>::toString() |
| 165 | { |
| 166 | std::ostringstream os; |
| 167 | os << *this; |
| 168 | |
| 169 | return os.str(); |
| 170 | } |
| 171 | |
| 172 | template<typename Hash> |
| 173 | std::ostream& |
| 174 | operator<<(std::ostream& os, Digest<Hash>& digest) |
| 175 | { |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 176 | ConstBufferPtr buffer = digest.computeDigest(); |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 177 | printHex(os, buffer->buf(), buffer->size()); |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 178 | |
| 179 | return os; |
| 180 | } |
| 181 | |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 182 | |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame^] | 183 | //////////////////////////////////////// |
| 184 | |
| 185 | |
| 186 | Sha256::Sha256() |
| 187 | { |
| 188 | reset(); |
| 189 | } |
| 190 | |
| 191 | Sha256::Sha256(std::istream& is) |
| 192 | : m_output(make_unique<OBufferStream>()) |
| 193 | , m_isEmpty(false) |
| 194 | , m_isFinalized(true) |
| 195 | { |
| 196 | namespace tr = security::transform; |
| 197 | |
| 198 | tr::streamSource(is) >> tr::digestFilter(DigestAlgorithm::SHA256) >> tr::streamSink(*m_output); |
| 199 | } |
| 200 | |
| 201 | void |
| 202 | Sha256::reset() |
| 203 | { |
| 204 | namespace tr = security::transform; |
| 205 | |
| 206 | m_input = make_unique<tr::StepSource>(); |
| 207 | m_output = make_unique<OBufferStream>(); |
| 208 | m_isEmpty = true; |
| 209 | m_isFinalized = false; |
| 210 | |
| 211 | *m_input >> tr::digestFilter(DigestAlgorithm::SHA256) >> tr::streamSink(*m_output); |
| 212 | } |
| 213 | |
| 214 | ConstBufferPtr |
| 215 | Sha256::computeDigest() |
| 216 | { |
| 217 | if (!m_isFinalized) { |
| 218 | BOOST_ASSERT(m_input != nullptr); |
| 219 | m_input->end(); |
| 220 | m_isFinalized = true; |
| 221 | } |
| 222 | |
| 223 | return m_output->buf(); |
| 224 | } |
| 225 | |
| 226 | bool |
| 227 | Sha256::operator==(Sha256& digest) |
| 228 | { |
| 229 | const Buffer& lhs = *computeDigest(); |
| 230 | const Buffer& rhs = *digest.computeDigest(); |
| 231 | |
| 232 | if (lhs.size() != rhs.size()) { |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | // constant-time buffer comparison to mitigate timing attacks |
| 237 | return CRYPTO_memcmp(lhs.get(), rhs.get(), lhs.size()) == 0; |
| 238 | } |
| 239 | |
| 240 | Sha256& |
| 241 | Sha256::operator<<(Sha256& src) |
| 242 | { |
| 243 | auto buf = src.computeDigest(); |
| 244 | update(buf->get(), buf->size()); |
| 245 | return *this; |
| 246 | } |
| 247 | |
| 248 | Sha256& |
| 249 | Sha256::operator<<(const std::string& str) |
| 250 | { |
| 251 | update(reinterpret_cast<const uint8_t*>(str.data()), str.size()); |
| 252 | return *this; |
| 253 | } |
| 254 | |
| 255 | Sha256& |
| 256 | Sha256::operator<<(const Block& block) |
| 257 | { |
| 258 | update(block.wire(), block.size()); |
| 259 | return *this; |
| 260 | } |
| 261 | |
| 262 | Sha256& |
| 263 | Sha256::operator<<(uint64_t value) |
| 264 | { |
| 265 | update(reinterpret_cast<const uint8_t*>(&value), sizeof(uint64_t)); |
| 266 | return *this; |
| 267 | } |
| 268 | |
| 269 | void |
| 270 | Sha256::update(const uint8_t* buffer, size_t size) |
| 271 | { |
| 272 | if (m_isFinalized) |
| 273 | BOOST_THROW_EXCEPTION(Error("Digest has been already finalized")); |
| 274 | |
| 275 | BOOST_ASSERT(m_input != nullptr); |
| 276 | m_input->write(buffer, size); |
| 277 | m_isEmpty = false; |
| 278 | } |
| 279 | |
| 280 | std::string |
| 281 | Sha256::toString() |
| 282 | { |
| 283 | auto buf = computeDigest(); |
| 284 | return toHex(*buf); |
| 285 | } |
| 286 | |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 287 | std::ostream& |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame^] | 288 | operator<<(std::ostream& os, Sha256& digest) |
| 289 | { |
| 290 | auto buf = digest.computeDigest(); |
| 291 | printHex(os, *buf); |
| 292 | return os; |
| 293 | } |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 294 | |
| 295 | } // namespace util |
| 296 | } // namespace ndn |