akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [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> |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include "sync-digest.h" |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include <boost/assert.hpp> |
| 27 | #include <boost/throw_exception.hpp> |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 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; |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 30 | |
| 31 | // for printing, may be disabled in optimized build |
| 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 | |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 38 | #include "boost-archive.h" |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 39 | |
| 40 | using namespace boost; |
| 41 | using namespace boost::archive::iterators; |
| 42 | using namespace std; |
| 43 | |
| 44 | // Other options: VP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha256, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160 |
| 45 | #define HASH_FUNCTION EVP_sha256 |
| 46 | #define HASH_FUNCTION_LEN 32 |
| 47 | |
| 48 | |
| 49 | // #ifndef DIGEST_BASE64 |
| 50 | |
| 51 | template<class CharType> |
| 52 | struct 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 | |
| 64 | typedef 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; |
| 66 | |
| 67 | |
| 68 | template<class CharType> |
| 69 | struct 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) |
| 90 | BOOST_THROW_EXCEPTION (Sync::Error::DigestCalculationError () << errmsg_info_int ((int)ch)); |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 91 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 92 | return value; |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | typedef transform_width<transform_iterator<hex_to_4_bit<string::const_iterator::value_type>, string::const_iterator>, 8, 4> string_to_binary; |
| 97 | |
| 98 | namespace Sync { |
| 99 | |
| 100 | Digest::Digest () |
| 101 | { |
| 102 | m_context = EVP_MD_CTX_create (); |
| 103 | |
| 104 | reset (); |
| 105 | } |
| 106 | |
| 107 | Digest::~Digest () |
| 108 | { |
| 109 | EVP_MD_CTX_destroy (m_context); |
| 110 | } |
| 111 | |
| 112 | bool |
| 113 | Digest::empty () const |
| 114 | { |
| 115 | return m_buffer.empty (); |
| 116 | } |
| 117 | |
| 118 | bool |
| 119 | Digest::isZero () const |
| 120 | { |
| 121 | if (m_buffer.empty ()) |
| 122 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
| 123 | << errmsg_info_str ("Digest has not been yet finalized")); |
| 124 | |
| 125 | return (m_buffer.size () == 1 && m_buffer[0] == 0); |
| 126 | } |
| 127 | |
| 128 | |
| 129 | void |
| 130 | Digest::reset () |
| 131 | { |
| 132 | m_buffer.clear (); |
| 133 | |
| 134 | int ok = EVP_DigestInit_ex (m_context, HASH_FUNCTION (), 0); |
| 135 | if (!ok) |
| 136 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
| 137 | << errmsg_info_str ("EVP_DigestInit_ex returned error") |
| 138 | << errmsg_info_int (ok)); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | void |
| 143 | Digest::finalize () |
| 144 | { |
| 145 | if (!m_buffer.empty ()) return; |
| 146 | |
| 147 | m_buffer.resize (HASH_FUNCTION_LEN); |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 148 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 149 | unsigned int tmp; |
| 150 | int ok = EVP_DigestFinal_ex (m_context, |
| 151 | &m_buffer[0], &tmp); |
| 152 | if (!ok) |
| 153 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
| 154 | << errmsg_info_str ("EVP_DigestFinal_ex returned error") |
| 155 | << errmsg_info_int (ok)); |
| 156 | } |
| 157 | |
| 158 | std::size_t |
| 159 | Digest::getHash () const |
| 160 | { |
| 161 | if (isZero ()) return 0; |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 162 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 163 | if (sizeof (std::size_t) > m_buffer.size ()) |
| 164 | { |
| 165 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
| 166 | << errmsg_info_str ("Hash is not zero and length is less than size_t") |
| 167 | << errmsg_info_int (m_buffer.size ())); |
| 168 | } |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 169 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 170 | // just getting first sizeof(std::size_t) bytes |
| 171 | // not ideal, but should work pretty well |
| 172 | return *(reinterpret_cast<const std::size_t*> (&m_buffer[0])); |
| 173 | } |
| 174 | |
| 175 | bool |
| 176 | Digest::operator == (const Digest &digest) const |
| 177 | { |
| 178 | if (m_buffer.empty ()) |
| 179 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
| 180 | << errmsg_info_str ("Digest1 is empty")); |
| 181 | |
| 182 | if (digest.m_buffer.empty ()) |
| 183 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
| 184 | << errmsg_info_str ("Digest2 is empty")); |
| 185 | |
| 186 | return m_buffer == digest.m_buffer; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | void |
| 191 | Digest::update (const uint8_t *buffer, size_t size) |
| 192 | { |
| 193 | // cout << "Update: " << (void*)buffer << " / size: " << size << "\n"; |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 194 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 195 | // cannot update Digest when it has been finalized |
| 196 | if (!m_buffer.empty ()) |
| 197 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
| 198 | << errmsg_info_str ("Digest has been already finalized")); |
| 199 | |
| 200 | bool ok = EVP_DigestUpdate (m_context, buffer, size); |
| 201 | if (!ok) |
| 202 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
| 203 | << errmsg_info_str ("EVP_DigestUpdate returned error") |
| 204 | << errmsg_info_int (ok)); |
| 205 | } |
| 206 | |
| 207 | |
| 208 | Digest & |
| 209 | Digest::operator << (const Digest &src) |
| 210 | { |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 211 | if (src.m_buffer.empty ()) |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 212 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
| 213 | << errmsg_info_str ("Digest has not been yet finalized")); |
| 214 | |
| 215 | update (&src.m_buffer[0], src.m_buffer.size ()); |
| 216 | |
| 217 | return *this; |
| 218 | } |
| 219 | |
| 220 | std::ostream & |
| 221 | operator << (std::ostream &os, const Digest &digest) |
| 222 | { |
| 223 | BOOST_ASSERT (!digest.m_buffer.empty ()); |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 224 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 225 | ostreambuf_iterator<char> out_it (os); // ostream iterator |
| 226 | // need to encode to base64 |
| 227 | copy (string_from_binary (digest.m_buffer.begin ()), |
| 228 | string_from_binary (digest.m_buffer.end ()), |
| 229 | out_it); |
| 230 | |
| 231 | return os; |
| 232 | } |
| 233 | |
| 234 | std::istream & |
| 235 | operator >> (std::istream &is, Digest &digest) |
| 236 | { |
| 237 | string str; |
| 238 | is >> str; // read string first |
| 239 | |
| 240 | if (str.size () == 0) |
| 241 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
| 242 | << errmsg_info_str ("Input is empty")); |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 243 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 244 | // 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 |
| 248 | if (!digest.m_buffer.empty ()) |
| 249 | BOOST_THROW_EXCEPTION (Error::DigestCalculationError () |
| 250 | << errmsg_info_str ("Digest has been already finalized")); |
| 251 | |
| 252 | digest.m_buffer.clear (); |
Yingdi Yu | 40cd1c3 | 2014-04-17 15:02:17 -0700 | [diff] [blame] | 253 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 254 | copy (string_to_binary (str.begin ()), |
| 255 | string_to_binary (str.end ()), |
| 256 | std::back_inserter (digest.m_buffer)); |
| 257 | |
| 258 | return is; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | } // Sync |