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