blob: 68c53cef4db5ac5c2df5230b01eda7abc48db2e2 [file] [log] [blame]
Yingdi Yude222c72014-08-15 16:06:52 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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"
Alexander Afanasyev574aa862017-01-10 19:53:28 -080024#include "security/detail/openssl.hpp"
25
Alexander Afanasyev6800e1f2014-09-30 13:20:12 -070026#include <sstream>
Yingdi Yude222c72014-08-15 16:06:52 -070027
28namespace ndn {
29namespace util {
30
31template<typename Hash>
32Digest<Hash>::Digest()
33{
34 reset();
35}
36
37template<typename Hash>
Alexander Afanasyevd27334f2015-07-01 21:44:36 -070038Digest<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
50template<typename Hash>
Yingdi Yude222c72014-08-15 16:06:52 -070051void
52Digest<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
60template<typename Hash>
61void
62Digest<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
73template<typename Hash>
74ConstBufferPtr
75Digest<Hash>::computeDigest()
76{
77 finalize();
78 return m_buffer;
79}
80
81template<typename Hash>
82bool
83Digest<Hash>::operator==(Digest<Hash>& digest)
84{
Alexander Afanasyev574aa862017-01-10 19:53:28 -080085 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 Yude222c72014-08-15 16:06:52 -070094}
95
96template<typename Hash>
97Digest<Hash>&
98Digest<Hash>::operator<<(Digest<Hash>& src)
99{
100 ConstBufferPtr buffer = src.computeDigest();
101 update(buffer->get(), buffer->size());
102
103 return *this;
104}
105
106template<typename Hash>
107Digest<Hash>&
108Digest<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
115template<typename Hash>
116Digest<Hash>&
117Digest<Hash>::operator<<(const Block& block)
118{
119 update(block.wire(), block.size());
120
121 return *this;
122}
123
124template<typename Hash>
125Digest<Hash>&
126Digest<Hash>::operator<<(uint64_t value)
127{
128 update(reinterpret_cast<const uint8_t*>(&value), sizeof(uint64_t));
129
130 return *this;
131}
132
133template<typename Hash>
134void
135Digest<Hash>::update(const uint8_t* buffer, size_t size)
136{
137 // cannot update Digest when it has been finalized
138 if (m_isFinalized)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700139 BOOST_THROW_EXCEPTION(Error("Digest has been already finalized"));
Yingdi Yude222c72014-08-15 16:06:52 -0700140
141 m_hash.Update(buffer, size);
142
143 m_isInProcess = true;
144}
145
146template<typename Hash>
147ConstBufferPtr
148Digest<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 Yu9ad2d722014-08-30 16:13:57 -0700158template<typename Hash>
159std::string
160Digest<Hash>::toString()
161{
162 std::ostringstream os;
163 os << *this;
164
165 return os.str();
166}
167
168template<typename Hash>
169std::ostream&
170operator<<(std::ostream& os, Digest<Hash>& digest)
171{
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700172 ConstBufferPtr buffer = digest.computeDigest();
Alexander Afanasyev8828ca62015-07-02 13:40:09 -0700173 printHex(os, buffer->buf(), buffer->size());
Yingdi Yu9ad2d722014-08-30 16:13:57 -0700174
175 return os;
176}
177
178template
179class Digest<CryptoPP::SHA256>;
180
181template
182std::ostream&
183operator<<(std::ostream& os, Digest<CryptoPP::SHA256>& digest);
184
Yingdi Yude222c72014-08-15 16:06:52 -0700185
186} // namespace util
187} // namespace ndn