blob: ce1d506083e8df81495cf4972300dbc4105fce1d [file] [log] [blame]
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyev8722d872014-07-02 13:00:29 -07003 * Copyright (c) 2012-2014 University of California, Los Angeles
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -08004 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07005 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -08007 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07008 * ChronoSync is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation, either
10 * version 3 of the License, or (at your option) any later version.
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080011 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070012 * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080015 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070016 * You should have received a copy of the GNU General Public License along with
17 * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/>
20 * @author Chaoyi Bian <bcy@pku.edu.cn>
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080022 */
23
24#include "sync-digest.h"
25#include <string.h>
26
Yingdi Yu06a678a2014-08-01 17:07:08 -070027#include "boost-header.h"
28
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070029typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str;
30typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int;
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080031
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -080032// for printing, may be disabled in optimized build
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080033
34// #ifdef DIGEST_BASE64
35// #include <boost/archive/iterators/base64_from_binary.hpp>
36// #include <boost/archive/iterators/binary_from_base64.hpp>
37// #endif
38
Yingdi Yu06a678a2014-08-01 17:07:08 -070039
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -080040
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080041using namespace boost;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080042using namespace boost::archive::iterators;
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -080043
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
Zhenkai Zhucbdf3792012-04-09 14:10:30 -070045#define HASH_FUNCTION EVP_sha256
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080046#define HASH_FUNCTION_LEN 32
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080047
48
49// #ifndef DIGEST_BASE64
50
51template<class CharType>
52struct hex_from_4_bit
53{
54 typedef CharType result_type;
55 CharType operator () (CharType ch) const
56 {
57 const char *lookup_table = "0123456789abcdef";
58 // cout << "New character: " << (int) ch << " (" << (char) ch << ")" << "\n";
59 BOOST_ASSERT (ch < 16);
60 return lookup_table[static_cast<size_t>(ch)];
61 }
62};
63
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080064typedef transform_iterator<hex_from_4_bit<std::vector<uint8_t>::const_iterator::value_type>,
65 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 -080066
67
68template<class CharType>
69struct hex_to_4_bit
70{
71 typedef CharType result_type;
72 CharType operator () (CharType ch) const
73 {
74 const signed char lookup_table [] = {
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 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
78 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1,
79 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
80 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
81 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
82 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
83 };
84
85 // cout << "New character: " << hex << (int) ch << " (" << (char) ch << ")" << "\n";
86 signed char value = -1;
87 if ((unsigned)ch < 128)
88 value = lookup_table [(unsigned)ch];
89 if (value == -1)
Alexander Afanasyevc1030192012-03-08 22:21:28 -080090 BOOST_THROW_EXCEPTION (Sync::Error::DigestCalculationError () << errmsg_info_int ((int)ch));
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070091
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080092 return value;
93 }
94};
95
Yingdi Yu68bc51a2014-03-25 16:02:12 -070096typedef 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 -080097
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -080098namespace Sync {
99
100Digest::Digest ()
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800101{
102 m_context = EVP_MD_CTX_create ();
103
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800104 reset ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800105}
106
107Digest::~Digest ()
108{
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800109 EVP_MD_CTX_destroy (m_context);
110}
111
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800112bool
113Digest::empty () const
114{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800115 return m_buffer.empty ();
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800116}
117
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700118bool
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700119Digest::isZero () const
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700120{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800121 if (m_buffer.empty ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700122 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
123 << errmsg_info_str ("Digest has not been yet finalized"));
124
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800125 return (m_buffer.size () == 1 && m_buffer[0] == 0);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700126}
127
128
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800129void
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800130Digest::reset ()
131{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800132 m_buffer.clear ();
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800133
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800134 int ok = EVP_DigestInit_ex (m_context, HASH_FUNCTION (), 0);
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800135 if (!ok)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800136 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800137 << errmsg_info_str ("EVP_DigestInit_ex returned error")
138 << errmsg_info_int (ok));
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800139}
140
141
142void
143Digest::finalize ()
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800144{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800145 if (!m_buffer.empty ()) return;
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800146
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800147 m_buffer.resize (HASH_FUNCTION_LEN);
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700148
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800149 unsigned int tmp;
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800150 int ok = EVP_DigestFinal_ex (m_context,
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800151 &m_buffer[0], &tmp);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -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_DigestFinal_ex returned error")
155 << errmsg_info_int (ok));
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800156}
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700157
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800158std::size_t
Alexander Afanasyeva5858032012-03-09 15:55:10 -0800159Digest::getHash () const
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800160{
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700161 if (isZero ()) return 0;
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700162
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800163 if (sizeof (std::size_t) > m_buffer.size ())
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700164 {
165 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
166 << errmsg_info_str ("Hash is not zero and length is less than size_t")
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800167 << errmsg_info_int (m_buffer.size ()));
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700168 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700169
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800170 // just getting first sizeof(std::size_t) bytes
171 // not ideal, but should work pretty well
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800172 return *(reinterpret_cast<const std::size_t*> (&m_buffer[0]));
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800173}
174
175bool
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800176Digest::operator == (const Digest &digest) const
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800177{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800178 if (m_buffer.empty ())
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800179 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800180 << errmsg_info_str ("Digest1 is empty"));
181
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800182 if (digest.m_buffer.empty ())
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800183 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800184 << errmsg_info_str ("Digest2 is empty"));
185
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800186 return m_buffer == digest.m_buffer;
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800187}
188
189
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800190void
191Digest::update (const uint8_t *buffer, size_t size)
192{
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800193 // cout << "Update: " << (void*)buffer << " / size: " << size << "\n";
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700194
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800195 // cannot update Digest when it has been finalized
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800196 if (!m_buffer.empty ())
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800197 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800198 << errmsg_info_str ("Digest has been already finalized"));
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800199
200 bool ok = EVP_DigestUpdate (m_context, buffer, size);
201 if (!ok)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800202 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800203 << errmsg_info_str ("EVP_DigestUpdate returned error")
204 << errmsg_info_int (ok));
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800205}
206
207
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800208Digest &
209Digest::operator << (const Digest &src)
210{
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700211 if (src.m_buffer.empty ())
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800212 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800213 << errmsg_info_str ("Digest has not been yet finalized"));
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800214
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800215 update (&src.m_buffer[0], src.m_buffer.size ());
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800216
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800217 return *this;
218}
219
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800220std::ostream &
221operator << (std::ostream &os, const Digest &digest)
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800222{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800223 BOOST_ASSERT (!digest.m_buffer.empty ());
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700224
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700225 std::ostreambuf_iterator<char> out_it (os); // ostream iterator
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800226 // need to encode to base64
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800227 copy (string_from_binary (digest.m_buffer.begin ()),
228 string_from_binary (digest.m_buffer.end ()),
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800229 out_it);
230
231 return os;
232}
233
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800234std::istream &
235operator >> (std::istream &is, Digest &digest)
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800236{
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700237 std::string str;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800238 is >> str; // read string first
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800239
240 if (str.size () == 0)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800241 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800242 << errmsg_info_str ("Input is empty"));
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700243
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800244 // uint8_t padding = (3 - str.size () % 3) % 3;
245 // for (uint8_t i = 0; i < padding; i++) str.push_back ('=');
246
247 // only empty digest object can be used for reading
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800248 if (!digest.m_buffer.empty ())
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800249 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800250 << errmsg_info_str ("Digest has been already finalized"));
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800251
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800252 digest.m_buffer.clear ();
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700253
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800254 copy (string_to_binary (str.begin ()),
255 string_to_binary (str.end ()),
256 std::back_inserter (digest.m_buffer));
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800257
258 return is;
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800259}
260
261
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800262} // Sync