blob: 621b52b8b438b357689ba102efb88735462b685e [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
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800123bool
124Digest::empty () const
125{
126 return m_buffer == 0;
127}
128
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800129void
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800130Digest::reset ()
131{
132 if (m_buffer != 0)
133 {
134 delete [] m_buffer;
135 m_buffer = 0;
136 }
137
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800138 int ok = EVP_DigestInit_ex (m_context, HASH_FUNCTION (), 0);
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800139 if (!ok)
140 throw DigestCalculationError () << errinfo_at_line (__LINE__);
141}
142
143
144void
145Digest::finalize ()
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800146{
147 if (m_buffer != 0) return;
148
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800149 m_buffer = new uint8_t [EVP_MAX_MD_SIZE];
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800150
151 int ok = EVP_DigestFinal_ex (m_context,
152 m_buffer, &m_hashLength);
153 if (!ok)
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800154 throw DigestCalculationError () << errinfo_at_line (__LINE__);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800155}
156
157std::size_t
158Digest::getHash ()
159{
160 if (m_buffer == 0)
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800161 finalize ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800162
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800163 if (sizeof (std::size_t) > m_hashLength)
164 throw DigestCalculationError () << errinfo_at_line (__LINE__);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800165
166 // just getting first sizeof(std::size_t) bytes
167 // not ideal, but should work pretty well
168 return reinterpret_cast<std::size_t> (m_buffer);
169}
170
171bool
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800172Digest::operator == (const Digest &digest) const
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800173{
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800174 if (m_buffer == 0 || digest.m_buffer == 0)
175 throw DigestCalculationError () << errinfo_at_line (__LINE__);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800176
Alexander Afanasyevd94542d2012-03-05 08:41:46 -0800177 BOOST_ASSERT (m_hashLength == digest.m_hashLength);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800178
179 return memcmp (m_buffer, digest.m_buffer, m_hashLength) == 0;
180}
181
182
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800183void
184Digest::update (const uint8_t *buffer, size_t size)
185{
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800186 // cout << "Update: " << (void*)buffer << " / size: " << size << "\n";
187
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800188 // cannot update Digest when it has been finalized
189 if (m_buffer != 0)
190 throw DigestCalculationError () << errinfo_at_line (__LINE__);
191
192 bool ok = EVP_DigestUpdate (m_context, buffer, size);
193 if (!ok)
194 throw DigestCalculationError () << errinfo_at_line (__LINE__);
195}
196
197
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800198Digest &
199Digest::operator << (const Digest &src)
200{
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800201 if (src.m_buffer == 0)
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800202 throw DigestCalculationError () << errinfo_at_line (__LINE__);
203
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800204 update (src.m_buffer, src.m_hashLength);
205
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800206 return *this;
207}
208
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800209std::ostream &
210operator << (std::ostream &os, const Digest &digest)
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800211{
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800212 BOOST_ASSERT (digest.m_hashLength != 0);
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800213
214 ostreambuf_iterator<char> out_it (os); // ostream iterator
215 // need to encode to base64
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800216 copy (string_from_binary (reinterpret_cast<const char*> (digest.m_buffer)),
217 string_from_binary (reinterpret_cast<const char*> (digest.m_buffer+digest.m_hashLength)),
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800218 out_it);
219
220 return os;
221}
222
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800223std::istream &
224operator >> (std::istream &is, Digest &digest)
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800225{
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800226 string str;
227 is >> str; // read string first
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800228
229 if (str.size () == 0)
230 throw DigestCalculationError () << errinfo_at_line (__LINE__);
231
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800232 // uint8_t padding = (3 - str.size () % 3) % 3;
233 // for (uint8_t i = 0; i < padding; i++) str.push_back ('=');
234
235 // only empty digest object can be used for reading
236 if (digest.m_buffer != 0)
237 throw DigestCalculationError () << errinfo_at_line (__LINE__);
238
239 digest.m_buffer = new uint8_t [EVP_MAX_MD_SIZE];
240 uint8_t *end = copy (string_to_binary (str.begin ()),
241 string_to_binary (str.end ()),
242 digest.m_buffer);
243
244 digest.m_hashLength = end - digest.m_buffer;
245
246 return is;
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800247}
248
249
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800250} // Sync
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800251