blob: 81d3e4e1608e34e806665d119740a1f9f134d864 [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
Alexander Afanasyevd94542d2012-03-05 08:41:46 -080027#include <boost/assert.hpp>
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -080028#include <boost/throw_exception.hpp>
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
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -080039#include <boost/archive/iterators/transform_width.hpp>
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080040#include <boost/iterator/transform_iterator.hpp>
41#include <boost/archive/iterators/dataflow_exception.hpp>
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -080042
Alexander Afanasyevdf718f52012-03-02 00:23:04 -080043using namespace boost;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080044using namespace boost::archive::iterators;
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -080045
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
Zhenkai Zhucbdf3792012-04-09 14:10:30 -070047#define HASH_FUNCTION EVP_sha256
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080048#define HASH_FUNCTION_LEN 32
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080049
50
51// #ifndef DIGEST_BASE64
52
53template<class CharType>
54struct hex_from_4_bit
55{
56 typedef CharType result_type;
57 CharType operator () (CharType ch) const
58 {
59 const char *lookup_table = "0123456789abcdef";
60 // cout << "New character: " << (int) ch << " (" << (char) ch << ")" << "\n";
61 BOOST_ASSERT (ch < 16);
62 return lookup_table[static_cast<size_t>(ch)];
63 }
64};
65
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080066typedef transform_iterator<hex_from_4_bit<std::vector<uint8_t>::const_iterator::value_type>,
67 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 -080068
69
70template<class CharType>
71struct hex_to_4_bit
72{
73 typedef CharType result_type;
74 CharType operator () (CharType ch) const
75 {
76 const signed char lookup_table [] = {
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 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
80 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-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 -1,10,11,12,13,14,15,-1,-1,-1,-1,-1,-1,-1,-1,-1,
84 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
85 };
86
87 // cout << "New character: " << hex << (int) ch << " (" << (char) ch << ")" << "\n";
88 signed char value = -1;
89 if ((unsigned)ch < 128)
90 value = lookup_table [(unsigned)ch];
91 if (value == -1)
Alexander Afanasyevc1030192012-03-08 22:21:28 -080092 BOOST_THROW_EXCEPTION (Sync::Error::DigestCalculationError () << errmsg_info_int ((int)ch));
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070093
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080094 return value;
95 }
96};
97
Yingdi Yu68bc51a2014-03-25 16:02:12 -070098typedef 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 -080099
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800100namespace Sync {
101
102Digest::Digest ()
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800103{
104 m_context = EVP_MD_CTX_create ();
105
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800106 reset ();
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800107}
108
109Digest::~Digest ()
110{
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800111 EVP_MD_CTX_destroy (m_context);
112}
113
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800114bool
115Digest::empty () const
116{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800117 return m_buffer.empty ();
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800118}
119
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700120bool
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700121Digest::isZero () const
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700122{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800123 if (m_buffer.empty ())
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700124 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
125 << errmsg_info_str ("Digest has not been yet finalized"));
126
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800127 return (m_buffer.size () == 1 && m_buffer[0] == 0);
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700128}
129
130
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800131void
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800132Digest::reset ()
133{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800134 m_buffer.clear ();
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800135
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800136 int ok = EVP_DigestInit_ex (m_context, HASH_FUNCTION (), 0);
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800137 if (!ok)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800138 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800139 << errmsg_info_str ("EVP_DigestInit_ex returned error")
140 << errmsg_info_int (ok));
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800141}
142
143
144void
145Digest::finalize ()
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800146{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800147 if (!m_buffer.empty ()) return;
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800148
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800149 m_buffer.resize (HASH_FUNCTION_LEN);
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700150
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800151 unsigned int tmp;
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800152 int ok = EVP_DigestFinal_ex (m_context,
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800153 &m_buffer[0], &tmp);
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800154 if (!ok)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800155 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800156 << errmsg_info_str ("EVP_DigestFinal_ex returned error")
157 << errmsg_info_int (ok));
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800158}
Alexander Afanasyevdd0eba72012-03-13 13:57:10 -0700159
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800160std::size_t
Alexander Afanasyeva5858032012-03-09 15:55:10 -0800161Digest::getHash () const
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800162{
Alexander Afanasyev763855a2012-03-13 14:17:37 -0700163 if (isZero ()) return 0;
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700164
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800165 if (sizeof (std::size_t) > m_buffer.size ())
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700166 {
167 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
168 << errmsg_info_str ("Hash is not zero and length is less than size_t")
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800169 << errmsg_info_int (m_buffer.size ()));
Alexander Afanasyevf07ab352012-03-13 12:57:46 -0700170 }
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700171
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800172 // just getting first sizeof(std::size_t) bytes
173 // not ideal, but should work pretty well
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800174 return *(reinterpret_cast<const std::size_t*> (&m_buffer[0]));
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800175}
176
177bool
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800178Digest::operator == (const Digest &digest) const
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800179{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800180 if (m_buffer.empty ())
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800181 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800182 << errmsg_info_str ("Digest1 is empty"));
183
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800184 if (digest.m_buffer.empty ())
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800185 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800186 << errmsg_info_str ("Digest2 is empty"));
187
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800188 return m_buffer == digest.m_buffer;
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800189}
190
191
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800192void
193Digest::update (const uint8_t *buffer, size_t size)
194{
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800195 // cout << "Update: " << (void*)buffer << " / size: " << size << "\n";
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700196
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800197 // cannot update Digest when it has been finalized
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800198 if (!m_buffer.empty ())
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800199 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800200 << errmsg_info_str ("Digest has been already finalized"));
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800201
202 bool ok = EVP_DigestUpdate (m_context, buffer, size);
203 if (!ok)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800204 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800205 << errmsg_info_str ("EVP_DigestUpdate returned error")
206 << errmsg_info_int (ok));
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800207}
208
209
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800210Digest &
211Digest::operator << (const Digest &src)
212{
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700213 if (src.m_buffer.empty ())
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800214 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800215 << errmsg_info_str ("Digest has not been yet finalized"));
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800216
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800217 update (&src.m_buffer[0], src.m_buffer.size ());
Alexander Afanasyeve00ffbe2012-03-05 00:01:36 -0800218
Alexander Afanasyevdf718f52012-03-02 00:23:04 -0800219 return *this;
220}
221
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800222std::ostream &
223operator << (std::ostream &os, const Digest &digest)
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800224{
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800225 BOOST_ASSERT (!digest.m_buffer.empty ());
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700226
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700227 std::ostreambuf_iterator<char> out_it (os); // ostream iterator
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800228 // need to encode to base64
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800229 copy (string_from_binary (digest.m_buffer.begin ()),
230 string_from_binary (digest.m_buffer.end ()),
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800231 out_it);
232
233 return os;
234}
235
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800236std::istream &
237operator >> (std::istream &is, Digest &digest)
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800238{
Yingdi Yu68bc51a2014-03-25 16:02:12 -0700239 std::string str;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800240 is >> str; // read string first
Alexander Afanasyevb71beab2012-03-05 21:13:49 -0800241
242 if (str.size () == 0)
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800243 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800244 << errmsg_info_str ("Input is empty"));
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700245
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800246 // uint8_t padding = (3 - str.size () % 3) % 3;
247 // for (uint8_t i = 0; i < padding; i++) str.push_back ('=');
248
249 // only empty digest object can be used for reading
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800250 if (!digest.m_buffer.empty ())
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800251 BOOST_THROW_EXCEPTION (Error::DigestCalculationError ()
Alexander Afanasyev87c9b5d2012-03-07 17:23:21 -0800252 << errmsg_info_str ("Digest has been already finalized"));
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800253
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800254 digest.m_buffer.clear ();
Yingdi Yu7c64e5c2014-04-30 14:06:37 -0700255
Alexander Afanasyevd95c2312013-11-07 13:45:34 -0800256 copy (string_to_binary (str.begin ()),
257 string_to_binary (str.end ()),
258 std::back_inserter (digest.m_buffer));
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -0800259
260 return is;
Alexander Afanasyevb080dbf2012-03-05 10:25:22 -0800261}
262
263
Alexander Afanasyev8f25cbb2012-03-01 23:53:40 -0800264} // Sync