Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 1 | /* -*- 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 Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
| 22 | |
| 23 | #include "sync-digest.h" |
| 24 | #include <string.h> |
| 25 | |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 26 | #include <boost/assert.hpp> |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 27 | #include <boost/throw_exception.hpp> |
| 28 | typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; |
| 29 | typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int; |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 31 | // for printing, may be disabled in optimized build |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 32 | |
| 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 Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 38 | #include <boost/archive/iterators/transform_width.hpp> |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 39 | #include <boost/iterator/transform_iterator.hpp> |
| 40 | #include <boost/archive/iterators/dataflow_exception.hpp> |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 42 | using namespace boost; |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 43 | using namespace boost::archive::iterators; |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 44 | using namespace std; |
| 45 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 46 | // Other options: VP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha256, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160 |
Zhenkai Zhu | cbdf379 | 2012-04-09 14:10:30 -0700 | [diff] [blame] | 47 | #define HASH_FUNCTION EVP_sha256 |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 48 | |
| 49 | |
| 50 | // #ifndef DIGEST_BASE64 |
| 51 | |
| 52 | template<class CharType> |
| 53 | struct 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 | |
| 65 | typedef transform_iterator<hex_from_4_bit<string::const_iterator::value_type>, |
| 66 | transform_width<string::const_iterator, 4, 8, string::const_iterator::value_type> > string_from_binary; |
| 67 | |
| 68 | |
| 69 | template<class CharType> |
| 70 | struct 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 Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 91 | BOOST_THROW_EXCEPTION (Sync::Error::DigestCalculationError () << errmsg_info_int ((int)ch)); |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 92 | |
| 93 | return value; |
| 94 | } |
| 95 | }; |
| 96 | |
| 97 | typedef transform_width<transform_iterator<hex_to_4_bit<string::const_iterator::value_type>, string::const_iterator>, 8, 4> string_to_binary; |
| 98 | |
| 99 | // #else |
| 100 | |
| 101 | // typedef base64_from_binary<transform_width<string::const_iterator, 6, 8> > string_from_binary; |
| 102 | // typedef binary_from_base64<transform_width<string::const_iterator, 8, 6> > string_to_binary; |
| 103 | |
| 104 | // #endif |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 105 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 106 | namespace Sync { |
| 107 | |
| 108 | Digest::Digest () |
| 109 | : m_buffer (0) |
| 110 | , m_hashLength (0) |
| 111 | { |
| 112 | m_context = EVP_MD_CTX_create (); |
| 113 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 114 | reset (); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | Digest::~Digest () |
| 118 | { |
| 119 | if (m_buffer != 0) |
| 120 | delete [] m_buffer; |
| 121 | |
| 122 | EVP_MD_CTX_destroy (m_context); |
| 123 | } |
| 124 | |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 125 | bool |
| 126 | Digest::empty () const |
| 127 | { |
| 128 | return m_buffer == 0; |
| 129 | } |
| 130 | |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 131 | bool |
Alexander Afanasyev | 763855a | 2012-03-13 14:17:37 -0700 | [diff] [blame] | 132 | Digest::isZero () const |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 133 | { |
| 134 | if (m_buffer == 0) |
| 135 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
| 136 | << errmsg_info_str ("Digest has not been yet finalized")); |
| 137 | |
Alexander Afanasyev | 763855a | 2012-03-13 14:17:37 -0700 | [diff] [blame] | 138 | return (m_hashLength == 1 && m_buffer[0] == 0); |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 142 | void |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 143 | Digest::reset () |
| 144 | { |
| 145 | if (m_buffer != 0) |
| 146 | { |
| 147 | delete [] m_buffer; |
| 148 | m_buffer = 0; |
| 149 | } |
| 150 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 151 | int ok = EVP_DigestInit_ex (m_context, HASH_FUNCTION (), 0); |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 152 | if (!ok) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 153 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 154 | << errmsg_info_str ("EVP_DigestInit_ex returned error") |
| 155 | << errmsg_info_int (ok)); |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
| 159 | void |
| 160 | Digest::finalize () |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 161 | { |
| 162 | if (m_buffer != 0) return; |
| 163 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 164 | m_buffer = new uint8_t [EVP_MAX_MD_SIZE]; |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 165 | |
| 166 | int ok = EVP_DigestFinal_ex (m_context, |
| 167 | m_buffer, &m_hashLength); |
| 168 | if (!ok) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 169 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 170 | << errmsg_info_str ("EVP_DigestFinal_ex returned error") |
| 171 | << errmsg_info_int (ok)); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 172 | } |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame] | 173 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 174 | std::size_t |
Alexander Afanasyev | a585803 | 2012-03-09 15:55:10 -0800 | [diff] [blame] | 175 | Digest::getHash () const |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 176 | { |
Alexander Afanasyev | 763855a | 2012-03-13 14:17:37 -0700 | [diff] [blame] | 177 | if (isZero ()) return 0; |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 178 | |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 179 | if (sizeof (std::size_t) > m_hashLength) |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 180 | { |
| 181 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
| 182 | << errmsg_info_str ("Hash is not zero and length is less than size_t") |
| 183 | << errmsg_info_int (m_hashLength)); |
| 184 | } |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 185 | |
| 186 | // just getting first sizeof(std::size_t) bytes |
| 187 | // not ideal, but should work pretty well |
Alexander Afanasyev | a20ca44 | 2012-03-14 16:31:14 -0700 | [diff] [blame] | 188 | return *(reinterpret_cast<std::size_t*> (m_buffer)); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | bool |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 192 | Digest::operator == (const Digest &digest) const |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 193 | { |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 194 | if (m_buffer == 0) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 195 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 196 | << errmsg_info_str ("Digest1 is empty")); |
| 197 | |
| 198 | if (digest.m_buffer == 0) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 199 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 200 | << errmsg_info_str ("Digest2 is empty")); |
| 201 | |
| 202 | if (m_hashLength != digest.m_hashLength) |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 203 | return false; |
| 204 | |
| 205 | // Allow different hash size |
| 206 | // BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
| 207 | // << errmsg_info_str ("Digest lengths are not the same") |
| 208 | // << errmsg_info_int (m_hashLength) |
| 209 | // << errmsg_info_int (digest.m_hashLength)); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 210 | |
| 211 | return memcmp (m_buffer, digest.m_buffer, m_hashLength) == 0; |
| 212 | } |
| 213 | |
| 214 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 215 | void |
| 216 | Digest::update (const uint8_t *buffer, size_t size) |
| 217 | { |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 218 | // cout << "Update: " << (void*)buffer << " / size: " << size << "\n"; |
| 219 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 220 | // cannot update Digest when it has been finalized |
| 221 | if (m_buffer != 0) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 222 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 223 | << errmsg_info_str ("Digest has been already finalized")); |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 224 | |
| 225 | bool ok = EVP_DigestUpdate (m_context, buffer, size); |
| 226 | if (!ok) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 227 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 228 | << errmsg_info_str ("EVP_DigestUpdate returned error") |
| 229 | << errmsg_info_int (ok)); |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 233 | Digest & |
| 234 | Digest::operator << (const Digest &src) |
| 235 | { |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 236 | if (src.m_buffer == 0) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 237 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 238 | << errmsg_info_str ("Digest has not been yet finalized")); |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 239 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 240 | update (src.m_buffer, src.m_hashLength); |
| 241 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 242 | return *this; |
| 243 | } |
| 244 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 245 | std::ostream & |
| 246 | operator << (std::ostream &os, const Digest &digest) |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 247 | { |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 248 | BOOST_ASSERT (digest.m_hashLength != 0); |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 249 | |
| 250 | ostreambuf_iterator<char> out_it (os); // ostream iterator |
| 251 | // need to encode to base64 |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 252 | copy (string_from_binary (reinterpret_cast<const char*> (digest.m_buffer)), |
| 253 | string_from_binary (reinterpret_cast<const char*> (digest.m_buffer+digest.m_hashLength)), |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 254 | out_it); |
| 255 | |
| 256 | return os; |
| 257 | } |
| 258 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 259 | std::istream & |
| 260 | operator >> (std::istream &is, Digest &digest) |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 261 | { |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 262 | string str; |
| 263 | is >> str; // read string first |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 264 | |
| 265 | if (str.size () == 0) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 266 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 267 | << errmsg_info_str ("Input is empty")); |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 268 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 269 | // uint8_t padding = (3 - str.size () % 3) % 3; |
| 270 | // for (uint8_t i = 0; i < padding; i++) str.push_back ('='); |
| 271 | |
| 272 | // only empty digest object can be used for reading |
| 273 | if (digest.m_buffer != 0) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 274 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 275 | << errmsg_info_str ("Digest has been already finalized")); |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 276 | |
| 277 | digest.m_buffer = new uint8_t [EVP_MAX_MD_SIZE]; |
| 278 | uint8_t *end = copy (string_to_binary (str.begin ()), |
| 279 | string_to_binary (str.end ()), |
| 280 | digest.m_buffer); |
| 281 | |
| 282 | digest.m_hashLength = end - digest.m_buffer; |
| 283 | |
| 284 | return is; |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 288 | } // Sync |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 289 | |