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" |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace util { |
| 31 | |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 32 | Sha256::Sha256() |
| 33 | { |
| 34 | reset(); |
| 35 | } |
| 36 | |
| 37 | Sha256::Sha256(std::istream& is) |
| 38 | : m_output(make_unique<OBufferStream>()) |
| 39 | , m_isEmpty(false) |
| 40 | , m_isFinalized(true) |
| 41 | { |
| 42 | namespace tr = security::transform; |
| 43 | |
| 44 | tr::streamSource(is) >> tr::digestFilter(DigestAlgorithm::SHA256) >> tr::streamSink(*m_output); |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | Sha256::reset() |
| 49 | { |
| 50 | namespace tr = security::transform; |
| 51 | |
| 52 | m_input = make_unique<tr::StepSource>(); |
| 53 | m_output = make_unique<OBufferStream>(); |
| 54 | m_isEmpty = true; |
| 55 | m_isFinalized = false; |
| 56 | |
| 57 | *m_input >> tr::digestFilter(DigestAlgorithm::SHA256) >> tr::streamSink(*m_output); |
| 58 | } |
| 59 | |
| 60 | ConstBufferPtr |
| 61 | Sha256::computeDigest() |
| 62 | { |
| 63 | if (!m_isFinalized) { |
| 64 | BOOST_ASSERT(m_input != nullptr); |
| 65 | m_input->end(); |
| 66 | m_isFinalized = true; |
| 67 | } |
| 68 | |
| 69 | return m_output->buf(); |
| 70 | } |
| 71 | |
| 72 | bool |
| 73 | Sha256::operator==(Sha256& digest) |
| 74 | { |
| 75 | const Buffer& lhs = *computeDigest(); |
| 76 | const Buffer& rhs = *digest.computeDigest(); |
| 77 | |
| 78 | if (lhs.size() != rhs.size()) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | // constant-time buffer comparison to mitigate timing attacks |
| 83 | return CRYPTO_memcmp(lhs.get(), rhs.get(), lhs.size()) == 0; |
| 84 | } |
| 85 | |
| 86 | Sha256& |
| 87 | Sha256::operator<<(Sha256& src) |
| 88 | { |
| 89 | auto buf = src.computeDigest(); |
| 90 | update(buf->get(), buf->size()); |
| 91 | return *this; |
| 92 | } |
| 93 | |
| 94 | Sha256& |
| 95 | Sha256::operator<<(const std::string& str) |
| 96 | { |
| 97 | update(reinterpret_cast<const uint8_t*>(str.data()), str.size()); |
| 98 | return *this; |
| 99 | } |
| 100 | |
| 101 | Sha256& |
| 102 | Sha256::operator<<(const Block& block) |
| 103 | { |
| 104 | update(block.wire(), block.size()); |
| 105 | return *this; |
| 106 | } |
| 107 | |
| 108 | Sha256& |
| 109 | Sha256::operator<<(uint64_t value) |
| 110 | { |
| 111 | update(reinterpret_cast<const uint8_t*>(&value), sizeof(uint64_t)); |
| 112 | return *this; |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | Sha256::update(const uint8_t* buffer, size_t size) |
| 117 | { |
| 118 | if (m_isFinalized) |
| 119 | BOOST_THROW_EXCEPTION(Error("Digest has been already finalized")); |
| 120 | |
| 121 | BOOST_ASSERT(m_input != nullptr); |
| 122 | m_input->write(buffer, size); |
| 123 | m_isEmpty = false; |
| 124 | } |
| 125 | |
| 126 | std::string |
| 127 | Sha256::toString() |
| 128 | { |
| 129 | auto buf = computeDigest(); |
| 130 | return toHex(*buf); |
| 131 | } |
| 132 | |
Yingdi Yu | 9ad2d72 | 2014-08-30 16:13:57 -0700 | [diff] [blame] | 133 | std::ostream& |
Davide Pesavento | 9436831 | 2017-07-08 22:25:03 -0400 | [diff] [blame] | 134 | operator<<(std::ostream& os, Sha256& digest) |
| 135 | { |
| 136 | auto buf = digest.computeDigest(); |
| 137 | printHex(os, *buf); |
| 138 | return os; |
| 139 | } |
Yingdi Yu | de222c7 | 2014-08-15 16:06:52 -0700 | [diff] [blame] | 140 | |
| 141 | } // namespace util |
| 142 | } // namespace ndn |