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 | #ifndef NDN_UTIL_DIGEST_HPP |
| 23 | #define NDN_UTIL_DIGEST_HPP |
| 24 | |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 25 | #include "concepts.hpp" |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame^] | 26 | #include "crypto.hpp" |
| 27 | #include "../encoding/block.hpp" |
| 28 | #include "../encoding/buffer-stream.hpp" |
| 29 | #include "../security/transform/step-source.hpp" |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 30 | |
| 31 | namespace ndn { |
| 32 | namespace util { |
| 33 | |
| 34 | /** |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 35 | * @brief provides a stateful digest calculation |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 36 | * |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 37 | * SHA256 example: |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 38 | * |
| 39 | * Digest<CryptoPP::SHA256> digest; |
| 40 | * digest.update(buf1, size1); |
| 41 | * digest.update(buf2, size2); |
| 42 | * ... |
| 43 | * ConstBufferPtr result = digest.computeDigest(); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 44 | */ |
| 45 | template<typename Hash> |
| 46 | class Digest |
| 47 | { |
| 48 | public: |
| 49 | BOOST_CONCEPT_ASSERT((Hashable<Hash>)); |
| 50 | |
| 51 | typedef Hash HashFunction; |
| 52 | |
| 53 | class Error : public std::runtime_error |
| 54 | { |
| 55 | public: |
| 56 | explicit |
| 57 | Error(const std::string& what) |
| 58 | : std::runtime_error(what) |
| 59 | { |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | Digest(); |
| 64 | |
| 65 | /** |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 66 | * @brief Calculate digest of the input stream @p is |
Alexander Afanasyev | d27334f | 2015-07-01 21:44:36 -0700 | [diff] [blame] | 67 | * @param is input stream |
| 68 | */ |
| 69 | explicit |
| 70 | Digest(std::istream& is); |
| 71 | |
| 72 | /** |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 73 | * @brief Discard the current state and start a new digest calculation. |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 74 | */ |
| 75 | void |
| 76 | reset(); |
| 77 | |
| 78 | /** |
| 79 | * @brief Check if digest is empty. |
| 80 | * |
| 81 | * An empty digest means nothing has been taken into calculation. |
| 82 | */ |
| 83 | bool |
| 84 | empty() const |
| 85 | { |
| 86 | return !m_isInProcess; |
| 87 | } |
| 88 | |
| 89 | /** |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 90 | * @brief Finalize and return the digest based on all previously supplied inputs. |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 91 | */ |
| 92 | ConstBufferPtr |
| 93 | computeDigest(); |
| 94 | |
| 95 | /** |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 96 | * @brief Check if the supplied digest equals to this digest |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 97 | * |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 98 | * @note This method will invoke computeDigest(), finalizing the digest. |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 99 | */ |
| 100 | bool |
| 101 | operator==(Digest<Hash>& digest); |
| 102 | |
| 103 | /** |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 104 | * @brief Check if the supplied digest is not equal to this digest |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 105 | * |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 106 | * @note This method will invoke computeDigest(), finalizing the digest. |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 107 | */ |
| 108 | bool |
| 109 | operator!=(Digest<Hash>& digest) |
| 110 | { |
| 111 | return !(*this == digest); |
| 112 | } |
| 113 | |
| 114 | /** |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 115 | * @brief Add existing digest to the digest calculation |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 116 | * @param src digest to combine with |
| 117 | * |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 118 | * The result of this combination is `digest(digest(...))` |
| 119 | * |
| 120 | * @note This method will invoke computeDigest(), finalizing the digest. |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 121 | */ |
| 122 | Digest<Hash>& |
| 123 | operator<<(Digest<Hash>& src); |
| 124 | |
| 125 | /** |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 126 | * @brief Add string to the digest calculation |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 127 | * @param str string to put into digest |
| 128 | */ |
| 129 | Digest<Hash>& |
| 130 | operator<<(const std::string& str); |
| 131 | |
| 132 | /** |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 133 | * @brief Add block to the digest calculation |
| 134 | * @param block data block to put into digest |
| 135 | * @throw Error the digest has been finalized. |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 136 | */ |
| 137 | Digest<Hash>& |
| 138 | operator<<(const Block& block); |
| 139 | |
| 140 | /** |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 141 | * @brief Add uint64_t value to the digest calculation |
| 142 | * @param value the integer value to put into digest |
| 143 | * @throw Error the digest has been finalized. |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 144 | */ |
| 145 | Digest<Hash>& |
| 146 | operator<<(uint64_t value); |
| 147 | |
| 148 | /** |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 149 | * @brief Add a buffer to the digest calculation |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 150 | * |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 151 | * Update the state of the digest if it has not been finalized and mark the digest as |
| 152 | * InProcess. |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 153 | * |
| 154 | * @param buffer the input buffer |
| 155 | * @param size the size of the input buffer. |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 156 | * @throw Error the digest has been finalized. |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 157 | */ |
| 158 | void |
| 159 | update(const uint8_t* buffer, size_t size); |
| 160 | |
| 161 | /** |
| 162 | * @brief Compute one-time digest |
| 163 | * @param buffer the input buffer |
| 164 | * @param size the size of the input buffer. |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 165 | * @return digest computed according to the `Hash` algorithm |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 166 | */ |
| 167 | static ConstBufferPtr |
| 168 | computeDigest(const uint8_t* buffer, size_t size); |
| 169 | |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 170 | /** |
| 171 | * @brief Convert digest to std::string |
| 172 | * |
Alexander Afanasyev | 574aa86 | 2017-01-10 19:53:28 -0800 | [diff] [blame] | 173 | * @note This method will invoke computeDigest(), finalizing the digest. |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 174 | */ |
| 175 | std::string |
| 176 | toString(); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 177 | |
| 178 | private: |
| 179 | /** |
| 180 | * @brief Finalize digest. |
| 181 | * |
| 182 | * All subsequent calls to "operator<<" will throw an exception |
| 183 | */ |
| 184 | void |
| 185 | finalize(); |
| 186 | |
| 187 | private: |
| 188 | Hash m_hash; |
| 189 | BufferPtr m_buffer; |
| 190 | bool m_isInProcess; |
| 191 | bool m_isFinalized; |
| 192 | }; |
| 193 | |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 194 | template<typename Hash> |
| 195 | std::ostream& |
| 196 | operator<<(std::ostream& os, Digest<Hash>& digest); |
| 197 | |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame^] | 198 | |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 199 | /** |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame^] | 200 | * @brief Provides stateful SHA-256 digest calculation. |
| 201 | * |
| 202 | * Example: |
| 203 | * @code |
| 204 | * Sha256 digest; |
| 205 | * digest.update(buf1, size1); |
| 206 | * digest.update(buf2, size2); |
| 207 | * ... |
| 208 | * ConstBufferPtr result = digest.computeDigest(); |
| 209 | * @endcode |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 210 | */ |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame^] | 211 | class Sha256 |
| 212 | { |
| 213 | public: |
| 214 | class Error : public std::runtime_error |
| 215 | { |
| 216 | public: |
| 217 | explicit |
| 218 | Error(const std::string& what) |
| 219 | : std::runtime_error(what) |
| 220 | { |
| 221 | } |
| 222 | }; |
| 223 | |
| 224 | /** |
| 225 | * @brief Create an empty SHA-256 digest. |
| 226 | */ |
| 227 | Sha256(); |
| 228 | |
| 229 | /** |
| 230 | * @brief Calculate SHA-256 digest of the input stream @p is. |
| 231 | */ |
| 232 | explicit |
| 233 | Sha256(std::istream& is); |
| 234 | |
| 235 | /** |
| 236 | * @brief Check if digest is empty. |
| 237 | * |
| 238 | * An empty digest means nothing has been taken into calculation. |
| 239 | */ |
| 240 | bool |
| 241 | empty() const |
| 242 | { |
| 243 | return m_isEmpty; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * @brief Discard the current state and start a new digest calculation. |
| 248 | */ |
| 249 | void |
| 250 | reset(); |
| 251 | |
| 252 | /** |
| 253 | * @brief Finalize and return the digest based on all previously supplied inputs. |
| 254 | */ |
| 255 | ConstBufferPtr |
| 256 | computeDigest(); |
| 257 | |
| 258 | /** |
| 259 | * @brief Check if the supplied digest is equal to this digest. |
| 260 | * @note This method invokes computeDigest() on both operands, finalizing the digest. |
| 261 | */ |
| 262 | bool |
| 263 | operator==(Sha256& digest); |
| 264 | |
| 265 | /** |
| 266 | * @brief Check if the supplied digest is not equal to this digest. |
| 267 | * @note This method invokes computeDigest() on both operands, finalizing the digest. |
| 268 | */ |
| 269 | bool |
| 270 | operator!=(Sha256& digest) |
| 271 | { |
| 272 | return !(*this == digest); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @brief Add existing digest to the digest calculation. |
| 277 | * @param src digest to combine with |
| 278 | * |
| 279 | * The result of this combination is `sha256(sha256(...))` |
| 280 | * |
| 281 | * @note This method invokes computeDigest() on @p src, finalizing the digest. |
| 282 | * @throw Error the digest has already been finalized |
| 283 | */ |
| 284 | Sha256& |
| 285 | operator<<(Sha256& src); |
| 286 | |
| 287 | /** |
| 288 | * @brief Add a string to the digest calculation. |
| 289 | * @throw Error the digest has already been finalized |
| 290 | */ |
| 291 | Sha256& |
| 292 | operator<<(const std::string& str); |
| 293 | |
| 294 | /** |
| 295 | * @brief Add a block to the digest calculation. |
| 296 | * @throw Error the digest has already been finalized |
| 297 | */ |
| 298 | Sha256& |
| 299 | operator<<(const Block& block); |
| 300 | |
| 301 | /** |
| 302 | * @brief Add a uint64_t value to the digest calculation. |
| 303 | * @throw Error the digest has already been finalized |
| 304 | */ |
| 305 | Sha256& |
| 306 | operator<<(uint64_t value); |
| 307 | |
| 308 | /** |
| 309 | * @brief Add a raw buffer to the digest calculation. |
| 310 | * @param buffer the input buffer |
| 311 | * @param size the size of the input buffer |
| 312 | * @throw Error the digest has already been finalized |
| 313 | */ |
| 314 | void |
| 315 | update(const uint8_t* buffer, size_t size); |
| 316 | |
| 317 | /** |
| 318 | * @brief Convert digest to std::string. |
| 319 | * @note This method invokes computeDigest(), finalizing the digest. |
| 320 | */ |
| 321 | std::string |
| 322 | toString(); |
| 323 | |
| 324 | /** |
| 325 | * @brief Compute a one-time SHA-256 digest. |
| 326 | * @param buffer the input buffer |
| 327 | * @param size the size of the input buffer |
| 328 | * @return SHA-256 digest of the input buffer |
| 329 | */ |
| 330 | static ConstBufferPtr |
| 331 | computeDigest(const uint8_t* buffer, size_t size) |
| 332 | { |
| 333 | return crypto::computeSha256Digest(buffer, size); |
| 334 | } |
| 335 | |
| 336 | private: |
| 337 | unique_ptr<security::transform::StepSource> m_input; |
| 338 | unique_ptr<OBufferStream> m_output; |
| 339 | bool m_isEmpty; |
| 340 | bool m_isFinalized; |
| 341 | }; |
| 342 | |
| 343 | std::ostream& |
| 344 | operator<<(std::ostream& os, Sha256& digest); |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 345 | |
| 346 | } // namespace util |
| 347 | } // namespace ndn |
| 348 | |
| 349 | #endif // NDN_UTIL_DIGEST_HPP |