Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 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" |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 24 | #include "security/detail/openssl.hpp" |
| 25 | |
Alexander Afanasyev | 6800e1f | 2014-09-30 13:20:12 -0700 | [diff] [blame] | 26 | #include <sstream> |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace util { |
| 30 | |
| 31 | template<typename Hash> |
| 32 | Digest<Hash>::Digest() |
| 33 | { |
| 34 | reset(); |
| 35 | } |
| 36 | |
| 37 | template<typename Hash> |
Alexander Afanasyev | d27334f | 2015-07-01 21:44:36 -0700 | [diff] [blame] | 38 | Digest<Hash>::Digest(std::istream& is) |
| 39 | : m_isInProcess(false) |
| 40 | , m_isFinalized(true) |
| 41 | { |
| 42 | using namespace CryptoPP; |
| 43 | |
| 44 | m_buffer = make_shared<Buffer>(m_hash.DigestSize()); |
| 45 | FileSource(is, true, |
| 46 | new HashFilter(m_hash, |
| 47 | new ArraySink(m_buffer->get(), m_buffer->size()))); |
| 48 | } |
| 49 | |
| 50 | template<typename Hash> |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 51 | void |
| 52 | Digest<Hash>::reset() |
| 53 | { |
| 54 | m_hash.Restart(); |
| 55 | m_buffer = make_shared<Buffer>(m_hash.DigestSize()); |
| 56 | m_isInProcess = false; |
| 57 | m_isFinalized = false; |
| 58 | } |
| 59 | |
| 60 | template<typename Hash> |
| 61 | void |
| 62 | Digest<Hash>::finalize() |
| 63 | { |
| 64 | // return immediately if Digest is finalized already. |
| 65 | if (m_isFinalized) |
| 66 | return; |
| 67 | |
| 68 | m_hash.Final(m_buffer->get()); |
| 69 | |
| 70 | m_isFinalized = true; |
| 71 | } |
| 72 | |
| 73 | template<typename Hash> |
| 74 | ConstBufferPtr |
| 75 | Digest<Hash>::computeDigest() |
| 76 | { |
| 77 | finalize(); |
| 78 | return m_buffer; |
| 79 | } |
| 80 | |
| 81 | template<typename Hash> |
| 82 | bool |
| 83 | Digest<Hash>::operator==(Digest<Hash>& digest) |
| 84 | { |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 85 | const Buffer& lhs = *computeDigest(); |
| 86 | const Buffer& rhs = *digest.computeDigest(); |
| 87 | |
| 88 | if (lhs.size() != rhs.size()) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | // constant-time buffer comparison to mitigate timing attacks |
| 93 | return CRYPTO_memcmp(lhs.buf(), rhs.buf(), lhs.size()) == 0; |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | template<typename Hash> |
| 97 | Digest<Hash>& |
| 98 | Digest<Hash>::operator<<(Digest<Hash>& src) |
| 99 | { |
| 100 | ConstBufferPtr buffer = src.computeDigest(); |
| 101 | update(buffer->get(), buffer->size()); |
| 102 | |
| 103 | return *this; |
| 104 | } |
| 105 | |
| 106 | template<typename Hash> |
| 107 | Digest<Hash>& |
| 108 | Digest<Hash>::operator<<(const std::string& str) |
| 109 | { |
| 110 | update(reinterpret_cast<const uint8_t*>(str.c_str()), str.size()); |
| 111 | |
| 112 | return *this; |
| 113 | } |
| 114 | |
| 115 | template<typename Hash> |
| 116 | Digest<Hash>& |
| 117 | Digest<Hash>::operator<<(const Block& block) |
| 118 | { |
| 119 | update(block.wire(), block.size()); |
| 120 | |
| 121 | return *this; |
| 122 | } |
| 123 | |
| 124 | template<typename Hash> |
| 125 | Digest<Hash>& |
| 126 | Digest<Hash>::operator<<(uint64_t value) |
| 127 | { |
| 128 | update(reinterpret_cast<const uint8_t*>(&value), sizeof(uint64_t)); |
| 129 | |
| 130 | return *this; |
| 131 | } |
| 132 | |
| 133 | template<typename Hash> |
| 134 | void |
| 135 | Digest<Hash>::update(const uint8_t* buffer, size_t size) |
| 136 | { |
| 137 | // cannot update Digest when it has been finalized |
| 138 | if (m_isFinalized) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 139 | BOOST_THROW_EXCEPTION(Error("Digest has been already finalized")); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 140 | |
| 141 | m_hash.Update(buffer, size); |
| 142 | |
| 143 | m_isInProcess = true; |
| 144 | } |
| 145 | |
| 146 | template<typename Hash> |
| 147 | ConstBufferPtr |
| 148 | Digest<Hash>::computeDigest(const uint8_t* buffer, size_t size) |
| 149 | { |
| 150 | Hash hash; |
| 151 | BufferPtr result = make_shared<Buffer>(hash.DigestSize()); |
| 152 | hash.Update(buffer, size); |
| 153 | hash.Final(result->get()); |
| 154 | |
| 155 | return result; |
| 156 | } |
| 157 | |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 158 | template<typename Hash> |
| 159 | std::string |
| 160 | Digest<Hash>::toString() |
| 161 | { |
| 162 | std::ostringstream os; |
| 163 | os << *this; |
| 164 | |
| 165 | return os.str(); |
| 166 | } |
| 167 | |
| 168 | template<typename Hash> |
| 169 | std::ostream& |
| 170 | operator<<(std::ostream& os, Digest<Hash>& digest) |
| 171 | { |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 172 | ConstBufferPtr buffer = digest.computeDigest(); |
Alexander Afanasyev | 8828ca6 | 2015-07-02 13:40:09 -0700 | [diff] [blame] | 173 | printHex(os, buffer->buf(), buffer->size()); |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 174 | |
| 175 | return os; |
| 176 | } |
| 177 | |
| 178 | template |
| 179 | class Digest<CryptoPP::SHA256>; |
| 180 | |
| 181 | template |
| 182 | std::ostream& |
| 183 | operator<<(std::ostream& os, Digest<CryptoPP::SHA256>& digest); |
| 184 | |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 185 | |
| 186 | } // namespace util |
| 187 | } // namespace ndn |