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> |
| 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 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 |
| 47 | #define HASH_FUNCTION EVP_sha1 |
| 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 |
| 132 | Digest::zero () const |
| 133 | { |
| 134 | if (m_buffer == 0) |
| 135 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
| 136 | << errmsg_info_str ("Digest has not been yet finalized")); |
| 137 | |
| 138 | if (m_hashLength == 1 && m_buffer[0] == 0) |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 143 | void |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 144 | Digest::reset () |
| 145 | { |
| 146 | if (m_buffer != 0) |
| 147 | { |
| 148 | delete [] m_buffer; |
| 149 | m_buffer = 0; |
| 150 | } |
| 151 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 152 | int ok = EVP_DigestInit_ex (m_context, HASH_FUNCTION (), 0); |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 153 | if (!ok) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 154 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 155 | << errmsg_info_str ("EVP_DigestInit_ex returned error") |
| 156 | << errmsg_info_int (ok)); |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
| 160 | void |
| 161 | Digest::finalize () |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 162 | { |
| 163 | if (m_buffer != 0) return; |
| 164 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 165 | m_buffer = new uint8_t [EVP_MAX_MD_SIZE]; |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 166 | |
| 167 | int ok = EVP_DigestFinal_ex (m_context, |
| 168 | m_buffer, &m_hashLength); |
| 169 | if (!ok) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 170 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 171 | << errmsg_info_str ("EVP_DigestFinal_ex returned error") |
| 172 | << errmsg_info_int (ok)); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 173 | } |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame^] | 174 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 175 | std::size_t |
Alexander Afanasyev | a585803 | 2012-03-09 15:55:10 -0800 | [diff] [blame] | 176 | Digest::getHash () const |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 177 | { |
Alexander Afanasyev | dd0eba7 | 2012-03-13 13:57:10 -0700 | [diff] [blame^] | 178 | if (zero ()) return 0; |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 179 | |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 180 | if (sizeof (std::size_t) > m_hashLength) |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 181 | { |
| 182 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
| 183 | << errmsg_info_str ("Hash is not zero and length is less than size_t") |
| 184 | << errmsg_info_int (m_hashLength)); |
| 185 | } |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 186 | |
| 187 | // just getting first sizeof(std::size_t) bytes |
| 188 | // not ideal, but should work pretty well |
| 189 | return reinterpret_cast<std::size_t> (m_buffer); |
| 190 | } |
| 191 | |
| 192 | bool |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 193 | Digest::operator == (const Digest &digest) const |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 194 | { |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 195 | if (m_buffer == 0) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 196 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 197 | << errmsg_info_str ("Digest1 is empty")); |
| 198 | |
| 199 | if (digest.m_buffer == 0) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 200 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 201 | << errmsg_info_str ("Digest2 is empty")); |
| 202 | |
| 203 | if (m_hashLength != digest.m_hashLength) |
Alexander Afanasyev | f07ab35 | 2012-03-13 12:57:46 -0700 | [diff] [blame] | 204 | return false; |
| 205 | |
| 206 | // Allow different hash size |
| 207 | // BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
| 208 | // << errmsg_info_str ("Digest lengths are not the same") |
| 209 | // << errmsg_info_int (m_hashLength) |
| 210 | // << errmsg_info_int (digest.m_hashLength)); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 211 | |
| 212 | return memcmp (m_buffer, digest.m_buffer, m_hashLength) == 0; |
| 213 | } |
| 214 | |
| 215 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 216 | void |
| 217 | Digest::update (const uint8_t *buffer, size_t size) |
| 218 | { |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 219 | // cout << "Update: " << (void*)buffer << " / size: " << size << "\n"; |
| 220 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 221 | // cannot update Digest when it has been finalized |
| 222 | if (m_buffer != 0) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 223 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 224 | << errmsg_info_str ("Digest has been already finalized")); |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 225 | |
| 226 | bool ok = EVP_DigestUpdate (m_context, buffer, size); |
| 227 | if (!ok) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 228 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 229 | << errmsg_info_str ("EVP_DigestUpdate returned error") |
| 230 | << errmsg_info_int (ok)); |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 234 | Digest & |
| 235 | Digest::operator << (const Digest &src) |
| 236 | { |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 237 | if (src.m_buffer == 0) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 238 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 239 | << errmsg_info_str ("Digest has not been yet finalized")); |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 240 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 241 | update (src.m_buffer, src.m_hashLength); |
| 242 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 243 | return *this; |
| 244 | } |
| 245 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 246 | std::ostream & |
| 247 | operator << (std::ostream &os, const Digest &digest) |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 248 | { |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 249 | BOOST_ASSERT (digest.m_hashLength != 0); |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 250 | |
| 251 | ostreambuf_iterator<char> out_it (os); // ostream iterator |
| 252 | // need to encode to base64 |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 253 | copy (string_from_binary (reinterpret_cast<const char*> (digest.m_buffer)), |
| 254 | 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] | 255 | out_it); |
| 256 | |
| 257 | return os; |
| 258 | } |
| 259 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 260 | std::istream & |
| 261 | operator >> (std::istream &is, Digest &digest) |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 262 | { |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 263 | string str; |
| 264 | is >> str; // read string first |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 265 | |
| 266 | if (str.size () == 0) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 267 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 268 | << errmsg_info_str ("Input is empty")); |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 269 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 270 | // uint8_t padding = (3 - str.size () % 3) % 3; |
| 271 | // for (uint8_t i = 0; i < padding; i++) str.push_back ('='); |
| 272 | |
| 273 | // only empty digest object can be used for reading |
| 274 | if (digest.m_buffer != 0) |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 275 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
Alexander Afanasyev | 87c9b5d | 2012-03-07 17:23:21 -0800 | [diff] [blame] | 276 | << errmsg_info_str ("Digest has been already finalized")); |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 277 | |
| 278 | digest.m_buffer = new uint8_t [EVP_MAX_MD_SIZE]; |
| 279 | uint8_t *end = copy (string_to_binary (str.begin ()), |
| 280 | string_to_binary (str.end ()), |
| 281 | digest.m_buffer); |
| 282 | |
| 283 | digest.m_hashLength = end - digest.m_buffer; |
| 284 | |
| 285 | return is; |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 289 | } // Sync |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 290 | |