blob: 5ff941847124b87be86dbc178a56af16665b9ec5 [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 Afanasyevdf718f52012-03-02 00:23:04 -080027#include <boost/exception/errinfo_at_line.hpp>
28
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -080029// for printing, may be disabled in optimized build
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080030
31// #ifdef DIGEST_BASE64
32// #include <boost/archive/iterators/base64_from_binary.hpp>
33// #include <boost/archive/iterators/binary_from_base64.hpp>
34// #endif
35
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -080036#include <boost/archive/iterators/transform_width.hpp>
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080037#include <boost/iterator/transform_iterator.hpp>
38#include <boost/archive/iterators/dataflow_exception.hpp>
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -080039
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080040using namespace boost;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080041using namespace boost::archive::iterators;
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -080042using namespace std;
43
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080044// 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_sha1
46
47
48// #ifndef DIGEST_BASE64
49
50template<class CharType>
51struct hex_from_4_bit
52{
53 typedef CharType result_type;
54 CharType operator () (CharType ch) const
55 {
56 const char *lookup_table = "0123456789abcdef";
57 // cout << "New character: " << (int) ch << " (" << (char) ch << ")" << "\n";
58 BOOST_ASSERT (ch < 16);
59 return lookup_table[static_cast<size_t>(ch)];
60 }
61};
62
63typedef transform_iterator<hex_from_4_bit<string::const_iterator::value_type>,
64 transform_width<string::const_iterator, 4, 8, string::const_iterator::value_type> > string_from_binary;
65
66
67template<class CharType>
68struct hex_to_4_bit
69{
70 typedef CharType result_type;
71 CharType operator () (CharType ch) const
72 {
73 const signed char lookup_table [] = {
74 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
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 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
78 -1,10,11,12,13,14,15,-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 -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 };
83
84 // cout << "New character: " << hex << (int) ch << " (" << (char) ch << ")" << "\n";
85 signed char value = -1;
86 if ((unsigned)ch < 128)
87 value = lookup_table [(unsigned)ch];
88 if (value == -1)
89 throw Sync::DigestCalculationError () << errinfo_at_line (__LINE__);
90
91 return value;
92 }
93};
94
95typedef transform_width<transform_iterator<hex_to_4_bit<string::const_iterator::value_type>, string::const_iterator>, 8, 4> string_to_binary;
96
97// #else
98
99// typedef base64_from_binary<transform_width<string::const_iterator, 6, 8> > string_from_binary;
100// typedef binary_from_base64<transform_width<string::const_iterator, 8, 6> > string_to_binary;
101
102// #endif
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800103
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800104namespace Sync {
105
106Digest::Digest ()
107 : m_buffer (0)
108 , m_hashLength (0)
109{
110 m_context = EVP_MD_CTX_create ();
111
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800112 reset ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800113}
114
115Digest::~Digest ()
116{
117 if (m_buffer != 0)
118 delete [] m_buffer;
119
120 EVP_MD_CTX_destroy (m_context);
121}
122
123void
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800124Digest::reset ()
125{
126 if (m_buffer != 0)
127 {
128 delete [] m_buffer;
129 m_buffer = 0;
130 }
131
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800132 int ok = EVP_DigestInit_ex (m_context, HASH_FUNCTION (), 0);
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800133 if (!ok)
134 throw DigestCalculationError () << errinfo_at_line (__LINE__);
135}
136
137
138void
139Digest::finalize ()
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800140{
141 if (m_buffer != 0) return;
142
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800143 m_buffer = new uint8_t [EVP_MAX_MD_SIZE];
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800144
145 int ok = EVP_DigestFinal_ex (m_context,
146 m_buffer, &m_hashLength);
147 if (!ok)
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800148 throw DigestCalculationError () << errinfo_at_line (__LINE__);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800149}
150
151std::size_t
152Digest::getHash ()
153{
154 if (m_buffer == 0)
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800155 finalize ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800156
Alexander Afanasyevd94542d2012-03-05 08:41:46 -0800157 BOOST_ASSERT (sizeof (std::size_t) <= m_hashLength);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800158
159 // just getting first sizeof(std::size_t) bytes
160 // not ideal, but should work pretty well
161 return reinterpret_cast<std::size_t> (m_buffer);
162}
163
164bool
165Digest::operator == (Digest &digest)
166{
167 if (m_buffer == 0)
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800168 finalize ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800169
170 if (digest.m_buffer == 0)
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800171 digest.finalize ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800172
Alexander Afanasyevd94542d2012-03-05 08:41:46 -0800173 BOOST_ASSERT (m_hashLength == digest.m_hashLength);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800174
175 return memcmp (m_buffer, digest.m_buffer, m_hashLength) == 0;
176}
177
178
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800179void
180Digest::update (const uint8_t *buffer, size_t size)
181{
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800182 // cout << "Update: " << (void*)buffer << " / size: " << size << "\n";
183
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800184 // cannot update Digest when it has been finalized
185 if (m_buffer != 0)
186 throw DigestCalculationError () << errinfo_at_line (__LINE__);
187
188 bool ok = EVP_DigestUpdate (m_context, buffer, size);
189 if (!ok)
190 throw DigestCalculationError () << errinfo_at_line (__LINE__);
191}
192
193
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800194Digest &
195Digest::operator << (const Digest &src)
196{
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800197 if (src.m_buffer == 0)
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800198 throw DigestCalculationError () << errinfo_at_line (__LINE__);
199
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800200 update (src.m_buffer, src.m_hashLength);
201
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800202 return *this;
203}
204
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800205std::ostream &
206operator << (std::ostream &os, const Digest &digest)
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800207{
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800208 BOOST_ASSERT (digest.m_hashLength != 0);
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800209
210 ostreambuf_iterator<char> out_it (os); // ostream iterator
211 // need to encode to base64
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800212 copy (string_from_binary (reinterpret_cast<const char*> (digest.m_buffer)),
213 string_from_binary (reinterpret_cast<const char*> (digest.m_buffer+digest.m_hashLength)),
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800214 out_it);
215
216 return os;
217}
218
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800219std::istream &
220operator >> (std::istream &is, Digest &digest)
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800221{
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800222 string str;
223 is >> str; // read string first
224 // uint8_t padding = (3 - str.size () % 3) % 3;
225 // for (uint8_t i = 0; i < padding; i++) str.push_back ('=');
226
227 // only empty digest object can be used for reading
228 if (digest.m_buffer != 0)
229 throw DigestCalculationError () << errinfo_at_line (__LINE__);
230
231 digest.m_buffer = new uint8_t [EVP_MAX_MD_SIZE];
232 uint8_t *end = copy (string_to_binary (str.begin ()),
233 string_to_binary (str.end ()),
234 digest.m_buffer);
235
236 digest.m_hashLength = end - digest.m_buffer;
237
238 return is;
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800239}
240
241
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800242} // Sync
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800243