blob: 07b52486e05e2e787ee973af9d65582505b396eb [file] [log] [blame]
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -08001/* -*- 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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#include "sync-digest.h"
24#include <string.h>
25
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080026#include <boost/assert.hpp>
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -080027#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;
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080030
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -080031// for printing, may be disabled in optimized build
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080032
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
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -080038#include <boost/archive/iterators/transform_width.hpp>
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080039#include <boost/iterator/transform_iterator.hpp>
40#include <boost/archive/iterators/dataflow_exception.hpp>
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -080041
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080042using namespace boost;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080043using namespace boost::archive::iterators;
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -080044using namespace std;
45
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080046// Other options: VP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha256, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160
Zhenkai Zhucbdf3792012-04-09 14:10:30 -070047#define HASH_FUNCTION EVP_sha256
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080048
49
50// #ifndef DIGEST_BASE64
51
52template<class CharType>
53struct hex_from_4_bit
54{
55 typedef CharType result_type;
56 CharType operator () (CharType ch) const
57 {
58 const char *lookup_table = "0123456789abcdef";
59 // cout << "New character: " << (int) ch << " (" << (char) ch << ")" << "\n";
60 BOOST_ASSERT (ch < 16);
61 return lookup_table[static_cast<size_t>(ch)];
62 }
63};
64
65typedef transform_iterator<hex_from_4_bit<string::const_iterator::value_type>,
66 transform_width<string::const_iterator, 4, 8, string::const_iterator::value_type> > string_from_binary;
67
68
69template<class CharType>
70struct hex_to_4_bit
71{
72 typedef CharType result_type;
73 CharType operator () (CharType ch) const
74 {
75 const signed char lookup_table [] = {
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 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
79 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
80 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
81 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
82 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
83 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
84 };
85
86 // cout << "New character: " << hex << (int) ch << " (" << (char) ch << ")" << "\n";
87 signed char value = -1;
88 if ((unsigned)ch < 128)
89 value = lookup_table [(unsigned)ch];
90 if (value == -1)
Alexander Afanasyevc1030192012-03-08 22:21:28 -080091 BOOST_THROW_EXCEPTION (Sync::Error::DigestCalculationError () << errmsg_info_int ((int)ch));
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080092
93 return value;
94 }
95};
96
97typedef transform_width<transform_iterator<hex_to_4_bit<string::const_iterator::value_type>, string::const_iterator>, 8, 4> string_to_binary;
98
99// #else
100
101// typedef base64_from_binary<transform_width<string::const_iterator, 6, 8> > string_from_binary;
102// typedef binary_from_base64<transform_width<string::const_iterator, 8, 6> > string_to_binary;
103
104// #endif
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800105
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800106namespace Sync {
107
108Digest::Digest ()
109 : m_buffer (0)
110 , m_hashLength (0)
111{
112 m_context = EVP_MD_CTX_create ();
113
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800114 reset ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800115}
116
117Digest::~Digest ()
118{
119 if (m_buffer != 0)
120 delete [] m_buffer;
121
122 EVP_MD_CTX_destroy (m_context);
123}
124
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800125bool
126Digest::empty () const
127{
128 return m_buffer == 0;
129}
130
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700131bool
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700132Digest::isZero () const
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700133{
134 if (m_buffer == 0)
135 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
136 << errmsg_info_str ("Digest has not been yet finalized"));
137
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700138 return (m_hashLength == 1 && m_buffer[0] == 0);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700139}
140
141
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800142void
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800143Digest::reset ()
144{
145 if (m_buffer != 0)
146 {
147 delete [] m_buffer;
148 m_buffer = 0;
149 }
150
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800151 int ok = EVP_DigestInit_ex (m_context, HASH_FUNCTION (), 0);
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800152 if (!ok)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800153 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800154 << errmsg_info_str ("EVP_DigestInit_ex returned error")
155 << errmsg_info_int (ok));
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800156}
157
158
159void
160Digest::finalize ()
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800161{
162 if (m_buffer != 0) return;
163
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800164 m_buffer = new uint8_t [EVP_MAX_MD_SIZE];
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800165
166 int ok = EVP_DigestFinal_ex (m_context,
167 m_buffer, &m_hashLength);
168 if (!ok)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800169 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800170 << errmsg_info_str ("EVP_DigestFinal_ex returned error")
171 << errmsg_info_int (ok));
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800172}
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700173
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800174std::size_t
Alexander Afanasyeva5858032012-03-09 15:55:10 -0800175Digest::getHash () const
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800176{
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700177 if (isZero ()) return 0;
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700178
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800179 if (sizeof (std::size_t) > m_hashLength)
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700180 {
181 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
182 << errmsg_info_str ("Hash is not zero and length is less than size_t")
183 << errmsg_info_int (m_hashLength));
184 }
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800185
186 // just getting first sizeof(std::size_t) bytes
187 // not ideal, but should work pretty well
Alexander Afanasyeva20ca442012-03-14 16:31:14 -0700188 return *(reinterpret_cast<std::size_t*> (m_buffer));
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800189}
190
191bool
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800192Digest::operator == (const Digest &digest) const
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800193{
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800194 if (m_buffer == 0)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800195 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800196 << errmsg_info_str ("Digest1 is empty"));
197
198 if (digest.m_buffer == 0)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800199 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800200 << errmsg_info_str ("Digest2 is empty"));
201
202 if (m_hashLength != digest.m_hashLength)
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700203 return false;
204
205 // Allow different hash size
206 // BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
207 // << errmsg_info_str ("Digest lengths are not the same")
208 // << errmsg_info_int (m_hashLength)
209 // << errmsg_info_int (digest.m_hashLength));
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800210
211 return memcmp (m_buffer, digest.m_buffer, m_hashLength) == 0;
212}
213
214
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800215void
216Digest::update (const uint8_t *buffer, size_t size)
217{
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800218 // cout << "Update: " << (void*)buffer << " / size: " << size << "\n";
219
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800220 // cannot update Digest when it has been finalized
221 if (m_buffer != 0)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800222 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800223 << errmsg_info_str ("Digest has been already finalized"));
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800224
225 bool ok = EVP_DigestUpdate (m_context, buffer, size);
226 if (!ok)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800227 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800228 << errmsg_info_str ("EVP_DigestUpdate returned error")
229 << errmsg_info_int (ok));
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800230}
231
232
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800233Digest &
234Digest::operator << (const Digest &src)
235{
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800236 if (src.m_buffer == 0)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800237 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800238 << errmsg_info_str ("Digest has not been yet finalized"));
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800239
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800240 update (src.m_buffer, src.m_hashLength);
241
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800242 return *this;
243}
244
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800245std::ostream &
246operator << (std::ostream &os, const Digest &digest)
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800247{
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800248 BOOST_ASSERT (digest.m_hashLength != 0);
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800249
250 ostreambuf_iterator<char> out_it (os); // ostream iterator
251 // need to encode to base64
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800252 copy (string_from_binary (reinterpret_cast<const char*> (digest.m_buffer)),
253 string_from_binary (reinterpret_cast<const char*> (digest.m_buffer+digest.m_hashLength)),
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800254 out_it);
255
256 return os;
257}
258
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800259std::istream &
260operator >> (std::istream &is, Digest &digest)
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800261{
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800262 string str;
263 is >> str; // read string first
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800264
265 if (str.size () == 0)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800266 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800267 << errmsg_info_str ("Input is empty"));
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800268
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800269 // uint8_t padding = (3 - str.size () % 3) % 3;
270 // for (uint8_t i = 0; i < padding; i++) str.push_back ('=');
271
272 // only empty digest object can be used for reading
273 if (digest.m_buffer != 0)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800274 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800275 << errmsg_info_str ("Digest has been already finalized"));
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800276
277 digest.m_buffer = new uint8_t [EVP_MAX_MD_SIZE];
278 uint8_t *end = copy (string_to_binary (str.begin ()),
279 string_to_binary (str.end ()),
280 digest.m_buffer);
281
282 digest.m_hashLength = end - digest.m_buffer;
283
284 return is;
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800285}
286
287
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800288} // Sync
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800289