blob: 9b1c5368f7f26dd7dffe01c132f0546a8965d603 [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>
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
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
47#define HASH_FUNCTION EVP_sha1
48
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 Afanasyev8f25cbb2012-03-01 23:53:40 -0800131void
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800132Digest::reset ()
133{
134 if (m_buffer != 0)
135 {
136 delete [] m_buffer;
137 m_buffer = 0;
138 }
139
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800140 int ok = EVP_DigestInit_ex (m_context, HASH_FUNCTION (), 0);
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800141 if (!ok)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800142 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800143 << errmsg_info_str ("EVP_DigestInit_ex returned error")
144 << errmsg_info_int (ok));
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800145}
146
147
148void
149Digest::finalize ()
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800150{
151 if (m_buffer != 0) return;
152
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800153 m_buffer = new uint8_t [EVP_MAX_MD_SIZE];
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800154
155 int ok = EVP_DigestFinal_ex (m_context,
156 m_buffer, &m_hashLength);
157 if (!ok)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800158 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800159 << errmsg_info_str ("EVP_DigestFinal_ex returned error")
160 << errmsg_info_int (ok));
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800161}
162
163std::size_t
164Digest::getHash ()
165{
166 if (m_buffer == 0)
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800167 finalize ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800168
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800169 if (sizeof (std::size_t) > m_hashLength)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800170 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800171 << errmsg_info_str ("Hash length is less than size_t")
172 << errmsg_info_int (m_hashLength));
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800173
174 // just getting first sizeof(std::size_t) bytes
175 // not ideal, but should work pretty well
176 return reinterpret_cast<std::size_t> (m_buffer);
177}
178
179bool
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800180Digest::operator == (const Digest &digest) const
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800181{
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800182 if (m_buffer == 0)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800183 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800184 << errmsg_info_str ("Digest1 is empty"));
185
186 if (digest.m_buffer == 0)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800187 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800188 << errmsg_info_str ("Digest2 is empty"));
189
190 if (m_hashLength != digest.m_hashLength)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800191 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800192 << errmsg_info_str ("Digest lengths are not the same")
193 << errmsg_info_int (m_hashLength)
194 << errmsg_info_int (digest.m_hashLength));
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800195
196 return memcmp (m_buffer, digest.m_buffer, m_hashLength) == 0;
197}
198
199
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800200void
201Digest::update (const uint8_t *buffer, size_t size)
202{
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800203 // cout << "Update: " << (void*)buffer << " / size: " << size << "\n";
204
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800205 // cannot update Digest when it has been finalized
206 if (m_buffer != 0)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800207 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800208 << errmsg_info_str ("Digest has been already finalized"));
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800209
210 bool ok = EVP_DigestUpdate (m_context, buffer, size);
211 if (!ok)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800212 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800213 << errmsg_info_str ("EVP_DigestUpdate returned error")
214 << errmsg_info_int (ok));
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800215}
216
217
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800218Digest &
219Digest::operator << (const Digest &src)
220{
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800221 if (src.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 not been yet finalized"));
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800224
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800225 update (src.m_buffer, src.m_hashLength);
226
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800227 return *this;
228}
229
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800230std::ostream &
231operator << (std::ostream &os, const Digest &digest)
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800232{
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800233 BOOST_ASSERT (digest.m_hashLength != 0);
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800234
235 ostreambuf_iterator<char> out_it (os); // ostream iterator
236 // need to encode to base64
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800237 copy (string_from_binary (reinterpret_cast<const char*> (digest.m_buffer)),
238 string_from_binary (reinterpret_cast<const char*> (digest.m_buffer+digest.m_hashLength)),
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800239 out_it);
240
241 return os;
242}
243
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800244std::istream &
245operator >> (std::istream &is, Digest &digest)
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800246{
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800247 string str;
248 is >> str; // read string first
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800249
250 if (str.size () == 0)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800251 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800252 << errmsg_info_str ("Input is empty"));
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800253
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800254 // uint8_t padding = (3 - str.size () % 3) % 3;
255 // for (uint8_t i = 0; i < padding; i++) str.push_back ('=');
256
257 // only empty digest object can be used for reading
258 if (digest.m_buffer != 0)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800259 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800260 << errmsg_info_str ("Digest has been already finalized"));
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800261
262 digest.m_buffer = new uint8_t [EVP_MAX_MD_SIZE];
263 uint8_t *end = copy (string_to_binary (str.begin ()),
264 string_to_binary (str.end ()),
265 digest.m_buffer);
266
267 digest.m_hashLength = end - digest.m_buffer;
268
269 return is;
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800270}
271
272
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800273} // Sync
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800274