blob: c6dc060d66d45286da427b9b4341044543372530 [file] [log] [blame]
akmhoque66e66182014-02-21 17:56:03 -06001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Chaoyi Bian <bcy@pku.edu.cn>
Vince Lehman0a7da612014-10-29 14:39:29 -050020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
akmhoque66e66182014-02-21 17:56:03 -060021 */
22
23#include "sync-digest.h"
24#include <string.h>
25
26#include <boost/assert.hpp>
27#include <boost/throw_exception.hpp>
Yingdi Yu40cd1c32014-04-17 15:02:17 -070028typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
29typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
akmhoque66e66182014-02-21 17:56:03 -060030
31// for printing, may be disabled in optimized build
32
33// #ifdef DIGEST_BASE64
34// #include <boost/archive/iterators/base64_from_binary.hpp>
35// #include <boost/archive/iterators/binary_from_base64.hpp>
36// #endif
37
Yingdi Yu40cd1c32014-04-17 15:02:17 -070038#include "boost-archive.h"
akmhoque66e66182014-02-21 17:56:03 -060039
40using namespace boost;
41using namespace boost::archive::iterators;
42using namespace std;
43
44// Other options: VP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha256, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160
45#define HASH_FUNCTION EVP_sha256
46#define HASH_FUNCTION_LEN 32
47
48
49// #ifndef DIGEST_BASE64
50
51template<class CharType>
52struct hex_from_4_bit
53{
54 typedef CharType result_type;
55 CharType operator () (CharType ch) const
56 {
57 const char *lookup_table = "0123456789abcdef";
58 // cout << "New character: " << (int) ch << " (" << (char) ch << ")" << "\n";
59 BOOST_ASSERT (ch < 16);
60 return lookup_table[static_cast<size_t>(ch)];
61 }
62};
63
64typedef transform_iterator<hex_from_4_bit<std::vector<uint8_t>::const_iterator::value_type>,
65 transform_width<std::vector<uint8_t>::const_iterator, 4, 8, std::vector<uint8_t>::const_iterator::value_type> > string_from_binary;
66
67
68template<class CharType>
69struct hex_to_4_bit
70{
71 typedef CharType result_type;
72 CharType operator () (CharType ch) const
73 {
74 const signed char lookup_table [] = {
75 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
76 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
77 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
78 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
79 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
80 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
81 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
82 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
83 };
84
85 // cout << "New character: " << hex << (int) ch << " (" << (char) ch << ")" << "\n";
86 signed char value = -1;
87 if ((unsigned)ch < 128)
88 value = lookup_table [(unsigned)ch];
89 if (value == -1)
90 BOOST_THROW_EXCEPTION (Sync::Error::DigestCalculationError () << errmsg_info_int ((int)ch));
Yingdi Yu40cd1c32014-04-17 15:02:17 -070091
akmhoque66e66182014-02-21 17:56:03 -060092 return value;
93 }
94};
95
96typedef transform_width<transform_iterator<hex_to_4_bit<string::const_iterator::value_type>, string::const_iterator>, 8, 4> string_to_binary;
97
98namespace Sync {
99
100Digest::Digest ()
101{
102 m_context = EVP_MD_CTX_create ();
103
104 reset ();
105}
106
107Digest::~Digest ()
108{
109 EVP_MD_CTX_destroy (m_context);
110}
111
112bool
113Digest::empty () const
114{
115 return m_buffer.empty ();
116}
117
118bool
119Digest::isZero () const
120{
121 if (m_buffer.empty ())
122 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
123 << errmsg_info_str ("Digest has not been yet finalized"));
124
125 return (m_buffer.size () == 1 && m_buffer[0] == 0);
126}
127
128
129void
130Digest::reset ()
131{
132 m_buffer.clear ();
133
134 int ok = EVP_DigestInit_ex (m_context, HASH_FUNCTION (), 0);
135 if (!ok)
136 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
137 << errmsg_info_str ("EVP_DigestInit_ex returned error")
138 << errmsg_info_int (ok));
139}
140
141
142void
143Digest::finalize ()
144{
145 if (!m_buffer.empty ()) return;
146
147 m_buffer.resize (HASH_FUNCTION_LEN);
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700148
akmhoque66e66182014-02-21 17:56:03 -0600149 unsigned int tmp;
150 int ok = EVP_DigestFinal_ex (m_context,
151 &m_buffer[0], &tmp);
152 if (!ok)
153 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
154 << errmsg_info_str ("EVP_DigestFinal_ex returned error")
155 << errmsg_info_int (ok));
156}
157
158std::size_t
159Digest::getHash () const
160{
161 if (isZero ()) return 0;
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700162
akmhoque66e66182014-02-21 17:56:03 -0600163 if (sizeof (std::size_t) > m_buffer.size ())
164 {
165 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
166 << errmsg_info_str ("Hash is not zero and length is less than size_t")
167 << errmsg_info_int (m_buffer.size ()));
168 }
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700169
akmhoque66e66182014-02-21 17:56:03 -0600170 // just getting first sizeof(std::size_t) bytes
171 // not ideal, but should work pretty well
172 return *(reinterpret_cast<const std::size_t*> (&m_buffer[0]));
173}
174
175bool
176Digest::operator == (const Digest &digest) const
177{
178 if (m_buffer.empty ())
179 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
180 << errmsg_info_str ("Digest1 is empty"));
181
182 if (digest.m_buffer.empty ())
183 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
184 << errmsg_info_str ("Digest2 is empty"));
185
186 return m_buffer == digest.m_buffer;
187}
188
189
190void
191Digest::update (const uint8_t *buffer, size_t size)
192{
193 // cout << "Update: " << (void*)buffer << " / size: " << size << "\n";
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700194
akmhoque66e66182014-02-21 17:56:03 -0600195 // cannot update Digest when it has been finalized
196 if (!m_buffer.empty ())
197 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
198 << errmsg_info_str ("Digest has been already finalized"));
199
200 bool ok = EVP_DigestUpdate (m_context, buffer, size);
201 if (!ok)
202 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
203 << errmsg_info_str ("EVP_DigestUpdate returned error")
204 << errmsg_info_int (ok));
205}
206
207
208Digest &
209Digest::operator << (const Digest &src)
210{
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700211 if (src.m_buffer.empty ())
akmhoque66e66182014-02-21 17:56:03 -0600212 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
213 << errmsg_info_str ("Digest has not been yet finalized"));
214
215 update (&src.m_buffer[0], src.m_buffer.size ());
216
217 return *this;
218}
219
220std::ostream &
221operator << (std::ostream &os, const Digest &digest)
222{
223 BOOST_ASSERT (!digest.m_buffer.empty ());
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700224
akmhoque66e66182014-02-21 17:56:03 -0600225 ostreambuf_iterator<char> out_it (os); // ostream iterator
226 // need to encode to base64
227 copy (string_from_binary (digest.m_buffer.begin ()),
228 string_from_binary (digest.m_buffer.end ()),
229 out_it);
230
231 return os;
232}
233
234std::istream &
235operator >> (std::istream &is, Digest &digest)
236{
237 string str;
238 is >> str; // read string first
239
240 if (str.size () == 0)
241 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
242 << errmsg_info_str ("Input is empty"));
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700243
akmhoque66e66182014-02-21 17:56:03 -0600244 // uint8_t padding = (3 - str.size () % 3) % 3;
245 // for (uint8_t i = 0; i < padding; i++) str.push_back ('=');
246
247 // only empty digest object can be used for reading
248 if (!digest.m_buffer.empty ())
249 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
250 << errmsg_info_str ("Digest has been already finalized"));
251
252 digest.m_buffer.clear ();
Yingdi Yu40cd1c32014-04-17 15:02:17 -0700253
akmhoque66e66182014-02-21 17:56:03 -0600254 copy (string_to_binary (str.begin ()),
255 string_to_binary (str.end ()),
256 std::back_inserter (digest.m_buffer));
257
258 return is;
259}
260
261
262} // Sync