blob: 6f41d3dc21ebc9c736897bc1c644d7c0d855d161 [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>
20 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#include "sync-digest.h"
24#include <string.h>
25
26#include <boost/assert.hpp>
27#include <boost/throw_exception.hpp>
28typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
29typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
30
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
38#include <boost/archive/iterators/transform_width.hpp>
39#include <boost/iterator/transform_iterator.hpp>
40#include <boost/archive/iterators/dataflow_exception.hpp>
41
42using namespace boost;
43using namespace boost::archive::iterators;
44using namespace std;
45
46// Other options: VP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha256, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160
47#define HASH_FUNCTION EVP_sha256
48#define HASH_FUNCTION_LEN 32
49
50
51// #ifndef DIGEST_BASE64
52
53template<class CharType>
54struct hex_from_4_bit
55{
56 typedef CharType result_type;
57 CharType operator () (CharType ch) const
58 {
59 const char *lookup_table = "0123456789abcdef";
60 // cout << "New character: " << (int) ch << " (" << (char) ch << ")" << "\n";
61 BOOST_ASSERT (ch < 16);
62 return lookup_table[static_cast<size_t>(ch)];
63 }
64};
65
66typedef transform_iterator<hex_from_4_bit<std::vector<uint8_t>::const_iterator::value_type>,
67 transform_width<std::vector<uint8_t>::const_iterator, 4, 8, std::vector<uint8_t>::const_iterator::value_type> > string_from_binary;
68
69
70template<class CharType>
71struct hex_to_4_bit
72{
73 typedef CharType result_type;
74 CharType operator () (CharType ch) const
75 {
76 const signed char lookup_table [] = {
77 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
78 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
79 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
80 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-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 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
84 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
85 };
86
87 // cout << "New character: " << hex << (int) ch << " (" << (char) ch << ")" << "\n";
88 signed char value = -1;
89 if ((unsigned)ch < 128)
90 value = lookup_table [(unsigned)ch];
91 if (value == -1)
92 BOOST_THROW_EXCEPTION (Sync::Error::DigestCalculationError () << errmsg_info_int ((int)ch));
93
94 return value;
95 }
96};
97
98typedef transform_width<transform_iterator<hex_to_4_bit<string::const_iterator::value_type>, string::const_iterator>, 8, 4> string_to_binary;
99
100namespace Sync {
101
102Digest::Digest ()
103{
104 m_context = EVP_MD_CTX_create ();
105
106 reset ();
107}
108
109Digest::~Digest ()
110{
111 EVP_MD_CTX_destroy (m_context);
112}
113
114bool
115Digest::empty () const
116{
117 return m_buffer.empty ();
118}
119
120bool
121Digest::isZero () const
122{
123 if (m_buffer.empty ())
124 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
125 << errmsg_info_str ("Digest has not been yet finalized"));
126
127 return (m_buffer.size () == 1 && m_buffer[0] == 0);
128}
129
130
131void
132Digest::reset ()
133{
134 m_buffer.clear ();
135
136 int ok = EVP_DigestInit_ex (m_context, HASH_FUNCTION (), 0);
137 if (!ok)
138 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
139 << errmsg_info_str ("EVP_DigestInit_ex returned error")
140 << errmsg_info_int (ok));
141}
142
143
144void
145Digest::finalize ()
146{
147 if (!m_buffer.empty ()) return;
148
149 m_buffer.resize (HASH_FUNCTION_LEN);
150
151 unsigned int tmp;
152 int ok = EVP_DigestFinal_ex (m_context,
153 &m_buffer[0], &tmp);
154 if (!ok)
155 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
156 << errmsg_info_str ("EVP_DigestFinal_ex returned error")
157 << errmsg_info_int (ok));
158}
159
160std::size_t
161Digest::getHash () const
162{
163 if (isZero ()) return 0;
164
165 if (sizeof (std::size_t) > m_buffer.size ())
166 {
167 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
168 << errmsg_info_str ("Hash is not zero and length is less than size_t")
169 << errmsg_info_int (m_buffer.size ()));
170 }
171
172 // just getting first sizeof(std::size_t) bytes
173 // not ideal, but should work pretty well
174 return *(reinterpret_cast<const std::size_t*> (&m_buffer[0]));
175}
176
177bool
178Digest::operator == (const Digest &digest) const
179{
180 if (m_buffer.empty ())
181 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
182 << errmsg_info_str ("Digest1 is empty"));
183
184 if (digest.m_buffer.empty ())
185 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
186 << errmsg_info_str ("Digest2 is empty"));
187
188 return m_buffer == digest.m_buffer;
189}
190
191
192void
193Digest::update (const uint8_t *buffer, size_t size)
194{
195 // cout << "Update: " << (void*)buffer << " / size: " << size << "\n";
196
197 // cannot update Digest when it has been finalized
198 if (!m_buffer.empty ())
199 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
200 << errmsg_info_str ("Digest has been already finalized"));
201
202 bool ok = EVP_DigestUpdate (m_context, buffer, size);
203 if (!ok)
204 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
205 << errmsg_info_str ("EVP_DigestUpdate returned error")
206 << errmsg_info_int (ok));
207}
208
209
210Digest &
211Digest::operator << (const Digest &src)
212{
213 if (src.m_buffer.empty ())
214 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
215 << errmsg_info_str ("Digest has not been yet finalized"));
216
217 update (&src.m_buffer[0], src.m_buffer.size ());
218
219 return *this;
220}
221
222std::ostream &
223operator << (std::ostream &os, const Digest &digest)
224{
225 BOOST_ASSERT (!digest.m_buffer.empty ());
226
227 ostreambuf_iterator<char> out_it (os); // ostream iterator
228 // need to encode to base64
229 copy (string_from_binary (digest.m_buffer.begin ()),
230 string_from_binary (digest.m_buffer.end ()),
231 out_it);
232
233 return os;
234}
235
236std::istream &
237operator >> (std::istream &is, Digest &digest)
238{
239 string str;
240 is >> str; // read string first
241
242 if (str.size () == 0)
243 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
244 << errmsg_info_str ("Input is empty"));
245
246 // uint8_t padding = (3 - str.size () % 3) % 3;
247 // for (uint8_t i = 0; i < padding; i++) str.push_back ('=');
248
249 // only empty digest object can be used for reading
250 if (!digest.m_buffer.empty ())
251 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
252 << errmsg_info_str ("Digest has been already finalized"));
253
254 digest.m_buffer.clear ();
255
256 copy (string_to_binary (str.begin ()),
257 string_to_binary (str.end ()),
258 std::back_inserter (digest.m_buffer));
259
260 return is;
261}
262
263
264} // Sync
265