blob: d3dde6cf084637bf9d12f69c03385f09c71c513d [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>
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080021 */
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>
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070028typedef 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 -080044
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080045// 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 -070046#define HASH_FUNCTION EVP_sha256
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080047#define HASH_FUNCTION_LEN 32
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
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080065typedef transform_iterator<hex_from_4_bit<std::vector<uint8_t>::const_iterator::value_type>,
66 transform_width<std::vector<uint8_t>::const_iterator, 4, 8, std::vector<uint8_t>::const_iterator::value_type> > string_from_binary;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080067
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));
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070092
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080093 return value;
94 }
95};
96
Yingdi Yu68bc51a2014-03-25 16:02:12 -070097typedef transform_width<transform_iterator<hex_to_4_bit<std::string::const_iterator::value_type>, std::string::const_iterator>, 8, 4> string_to_binary;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080098
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080099namespace Sync {
100
101Digest::Digest ()
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800102{
103 m_context = EVP_MD_CTX_create ();
104
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800105 reset ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800106}
107
108Digest::~Digest ()
109{
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800110 EVP_MD_CTX_destroy (m_context);
111}
112
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800113bool
114Digest::empty () const
115{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800116 return m_buffer.empty ();
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800117}
118
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700119bool
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700120Digest::isZero () const
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700121{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800122 if (m_buffer.empty ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700123 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
124 << errmsg_info_str ("Digest has not been yet finalized"));
125
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800126 return (m_buffer.size () == 1 && m_buffer[0] == 0);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700127}
128
129
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800130void
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800131Digest::reset ()
132{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800133 m_buffer.clear ();
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800134
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800135 int ok = EVP_DigestInit_ex (m_context, HASH_FUNCTION (), 0);
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800136 if (!ok)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800137 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800138 << errmsg_info_str ("EVP_DigestInit_ex returned error")
139 << errmsg_info_int (ok));
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800140}
141
142
143void
144Digest::finalize ()
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800145{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800146 if (!m_buffer.empty ()) return;
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800147
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800148 m_buffer.resize (HASH_FUNCTION_LEN);
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700149
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800150 unsigned int tmp;
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800151 int ok = EVP_DigestFinal_ex (m_context,
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800152 &m_buffer[0], &tmp);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800153 if (!ok)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800154 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800155 << errmsg_info_str ("EVP_DigestFinal_ex returned error")
156 << errmsg_info_int (ok));
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800157}
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700158
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800159std::size_t
Alexander Afanasyeva5858032012-03-09 15:55:10 -0800160Digest::getHash () const
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800161{
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700162 if (isZero ()) return 0;
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700163
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800164 if (sizeof (std::size_t) > m_buffer.size ())
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700165 {
166 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
167 << errmsg_info_str ("Hash is not zero and length is less than size_t")
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800168 << errmsg_info_int (m_buffer.size ()));
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700169 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700170
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800171 // just getting first sizeof(std::size_t) bytes
172 // not ideal, but should work pretty well
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800173 return *(reinterpret_cast<const std::size_t*> (&m_buffer[0]));
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800174}
175
176bool
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800177Digest::operator == (const Digest &digest) const
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800178{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800179 if (m_buffer.empty ())
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800180 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800181 << errmsg_info_str ("Digest1 is empty"));
182
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800183 if (digest.m_buffer.empty ())
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800184 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800185 << errmsg_info_str ("Digest2 is empty"));
186
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800187 return m_buffer == digest.m_buffer;
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800188}
189
190
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800191void
192Digest::update (const uint8_t *buffer, size_t size)
193{
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800194 // cout << "Update: " << (void*)buffer << " / size: " << size << "\n";
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700195
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800196 // cannot update Digest when it has been finalized
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800197 if (!m_buffer.empty ())
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800198 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800199 << errmsg_info_str ("Digest has been already finalized"));
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800200
201 bool ok = EVP_DigestUpdate (m_context, buffer, size);
202 if (!ok)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800203 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800204 << errmsg_info_str ("EVP_DigestUpdate returned error")
205 << errmsg_info_int (ok));
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800206}
207
208
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800209Digest &
210Digest::operator << (const Digest &src)
211{
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700212 if (src.m_buffer.empty ())
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800213 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800214 << errmsg_info_str ("Digest has not been yet finalized"));
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800215
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800216 update (&src.m_buffer[0], src.m_buffer.size ());
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800217
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800218 return *this;
219}
220
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800221std::ostream &
222operator << (std::ostream &os, const Digest &digest)
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800223{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800224 BOOST_ASSERT (!digest.m_buffer.empty ());
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700225
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700226 std::ostreambuf_iterator<char> out_it (os); // ostream iterator
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800227 // need to encode to base64
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800228 copy (string_from_binary (digest.m_buffer.begin ()),
229 string_from_binary (digest.m_buffer.end ()),
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800230 out_it);
231
232 return os;
233}
234
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800235std::istream &
236operator >> (std::istream &is, Digest &digest)
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800237{
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700238 std::string str;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800239 is >> str; // read string first
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800240
241 if (str.size () == 0)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800242 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800243 << errmsg_info_str ("Input is empty"));
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700244
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800245 // uint8_t padding = (3 - str.size () % 3) % 3;
246 // for (uint8_t i = 0; i < padding; i++) str.push_back ('=');
247
248 // only empty digest object can be used for reading
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800249 if (!digest.m_buffer.empty ())
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800250 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800251 << errmsg_info_str ("Digest has been already finalized"));
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800252
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800253 digest.m_buffer.clear ();
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700254
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800255 copy (string_to_binary (str.begin ()),
256 string_to_binary (str.end ()),
257 std::back_inserter (digest.m_buffer));
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800258
259 return is;
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800260}
261
262
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800263} // Sync