blob: 516255fbcde523cd691224c8b7a0ce07aa515f03 [file] [log] [blame]
Yingdi Yude222c72014-08-15 16:06:52 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento94368312017-07-08 22:25:03 -04002/*
Alexander Afanasyev574aa862017-01-10 19:53:28 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yude222c72014-08-15 16:06:52 -07004 *
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 Afanasyev8828ca62015-07-02 13:40:09 -070023#include "string-helper.hpp"
Davide Pesavento94368312017-07-08 22:25:03 -040024#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 Afanasyev574aa862017-01-10 19:53:28 -080029
Alexander Afanasyev6800e1f2014-09-30 13:20:12 -070030#include <sstream>
Yingdi Yude222c72014-08-15 16:06:52 -070031
32namespace ndn {
33namespace util {
34
35template<typename Hash>
36Digest<Hash>::Digest()
37{
38 reset();
39}
40
41template<typename Hash>
Alexander Afanasyevd27334f2015-07-01 21:44:36 -070042Digest<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
54template<typename Hash>
Yingdi Yude222c72014-08-15 16:06:52 -070055void
56Digest<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
64template<typename Hash>
65void
66Digest<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
77template<typename Hash>
78ConstBufferPtr
79Digest<Hash>::computeDigest()
80{
81 finalize();
82 return m_buffer;
83}
84
85template<typename Hash>
86bool
87Digest<Hash>::operator==(Digest<Hash>& digest)
88{
Alexander Afanasyev574aa862017-01-10 19:53:28 -080089 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 Yude222c72014-08-15 16:06:52 -070098}
99
100template<typename Hash>
101Digest<Hash>&
102Digest<Hash>::operator<<(Digest<Hash>& src)
103{
104 ConstBufferPtr buffer = src.computeDigest();
105 update(buffer->get(), buffer->size());
106
107 return *this;
108}
109
110template<typename Hash>
111Digest<Hash>&
112Digest<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
119template<typename Hash>
120Digest<Hash>&
121Digest<Hash>::operator<<(const Block& block)
122{
123 update(block.wire(), block.size());
124
125 return *this;
126}
127
128template<typename Hash>
129Digest<Hash>&
130Digest<Hash>::operator<<(uint64_t value)
131{
132 update(reinterpret_cast<const uint8_t*>(&value), sizeof(uint64_t));
133
134 return *this;
135}
136
137template<typename Hash>
138void
139Digest<Hash>::update(const uint8_t* buffer, size_t size)
140{
141 // cannot update Digest when it has been finalized
142 if (m_isFinalized)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700143 BOOST_THROW_EXCEPTION(Error("Digest has been already finalized"));
Yingdi Yude222c72014-08-15 16:06:52 -0700144
145 m_hash.Update(buffer, size);
146
147 m_isInProcess = true;
148}
149
150template<typename Hash>
151ConstBufferPtr
152Digest<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 Yu9ad2d722014-08-30 16:13:57 -0700162template<typename Hash>
163std::string
164Digest<Hash>::toString()
165{
166 std::ostringstream os;
167 os << *this;
168
169 return os.str();
170}
171
172template<typename Hash>
173std::ostream&
174operator<<(std::ostream& os, Digest<Hash>& digest)
175{
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700176 ConstBufferPtr buffer = digest.computeDigest();
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700177 printHex(os, buffer->buf(), buffer->size());
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700178
179 return os;
180}
181
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700182
Davide Pesavento94368312017-07-08 22:25:03 -0400183////////////////////////////////////////
184
185
186Sha256::Sha256()
187{
188 reset();
189}
190
191Sha256::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
201void
202Sha256::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
214ConstBufferPtr
215Sha256::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
226bool
227Sha256::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
240Sha256&
241Sha256::operator<<(Sha256& src)
242{
243 auto buf = src.computeDigest();
244 update(buf->get(), buf->size());
245 return *this;
246}
247
248Sha256&
249Sha256::operator<<(const std::string& str)
250{
251 update(reinterpret_cast<const uint8_t*>(str.data()), str.size());
252 return *this;
253}
254
255Sha256&
256Sha256::operator<<(const Block& block)
257{
258 update(block.wire(), block.size());
259 return *this;
260}
261
262Sha256&
263Sha256::operator<<(uint64_t value)
264{
265 update(reinterpret_cast<const uint8_t*>(&value), sizeof(uint64_t));
266 return *this;
267}
268
269void
270Sha256::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
280std::string
281Sha256::toString()
282{
283 auto buf = computeDigest();
284 return toHex(*buf);
285}
286
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700287std::ostream&
Davide Pesavento94368312017-07-08 22:25:03 -0400288operator<<(std::ostream& os, Sha256& digest)
289{
290 auto buf = digest.computeDigest();
291 printHex(os, *buf);
292 return os;
293}
Yingdi Yude222c72014-08-15 16:06:52 -0700294
295} // namespace util
296} // namespace ndn