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 | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 27 | #include <boost/exception/errinfo_at_line.hpp> |
| 28 | |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 29 | // for printing, may be disabled in optimized build |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 30 | |
| 31 | // #ifdef DIGEST_BASE64 |
| 32 | // #include <boost/archive/iterators/base64_from_binary.hpp> |
| 33 | // #include <boost/archive/iterators/binary_from_base64.hpp> |
| 34 | // #endif |
| 35 | |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 36 | #include <boost/archive/iterators/transform_width.hpp> |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 37 | #include <boost/iterator/transform_iterator.hpp> |
| 38 | #include <boost/archive/iterators/dataflow_exception.hpp> |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 39 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 40 | using namespace boost; |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 41 | using namespace boost::archive::iterators; |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 42 | using namespace std; |
| 43 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 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_sha1 |
| 46 | |
| 47 | |
| 48 | // #ifndef DIGEST_BASE64 |
| 49 | |
| 50 | template<class CharType> |
| 51 | struct hex_from_4_bit |
| 52 | { |
| 53 | typedef CharType result_type; |
| 54 | CharType operator () (CharType ch) const |
| 55 | { |
| 56 | const char *lookup_table = "0123456789abcdef"; |
| 57 | // cout << "New character: " << (int) ch << " (" << (char) ch << ")" << "\n"; |
| 58 | BOOST_ASSERT (ch < 16); |
| 59 | return lookup_table[static_cast<size_t>(ch)]; |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | typedef transform_iterator<hex_from_4_bit<string::const_iterator::value_type>, |
| 64 | transform_width<string::const_iterator, 4, 8, string::const_iterator::value_type> > string_from_binary; |
| 65 | |
| 66 | |
| 67 | template<class CharType> |
| 68 | struct hex_to_4_bit |
| 69 | { |
| 70 | typedef CharType result_type; |
| 71 | CharType operator () (CharType ch) const |
| 72 | { |
| 73 | const signed char lookup_table [] = { |
| 74 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 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 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1,-1,-1,-1,-1, |
| 78 | -1,10,11,12,13,14,15,-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 | -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 | }; |
| 83 | |
| 84 | // cout << "New character: " << hex << (int) ch << " (" << (char) ch << ")" << "\n"; |
| 85 | signed char value = -1; |
| 86 | if ((unsigned)ch < 128) |
| 87 | value = lookup_table [(unsigned)ch]; |
| 88 | if (value == -1) |
| 89 | throw Sync::DigestCalculationError () << errinfo_at_line (__LINE__); |
| 90 | |
| 91 | return value; |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | typedef transform_width<transform_iterator<hex_to_4_bit<string::const_iterator::value_type>, string::const_iterator>, 8, 4> string_to_binary; |
| 96 | |
| 97 | // #else |
| 98 | |
| 99 | // typedef base64_from_binary<transform_width<string::const_iterator, 6, 8> > string_from_binary; |
| 100 | // typedef binary_from_base64<transform_width<string::const_iterator, 8, 6> > string_to_binary; |
| 101 | |
| 102 | // #endif |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 104 | namespace Sync { |
| 105 | |
| 106 | Digest::Digest () |
| 107 | : m_buffer (0) |
| 108 | , m_hashLength (0) |
| 109 | { |
| 110 | m_context = EVP_MD_CTX_create (); |
| 111 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 112 | reset (); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | Digest::~Digest () |
| 116 | { |
| 117 | if (m_buffer != 0) |
| 118 | delete [] m_buffer; |
| 119 | |
| 120 | EVP_MD_CTX_destroy (m_context); |
| 121 | } |
| 122 | |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 123 | bool |
| 124 | Digest::empty () const |
| 125 | { |
| 126 | return m_buffer == 0; |
| 127 | } |
| 128 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 129 | void |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 130 | Digest::reset () |
| 131 | { |
| 132 | if (m_buffer != 0) |
| 133 | { |
| 134 | delete [] m_buffer; |
| 135 | m_buffer = 0; |
| 136 | } |
| 137 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 138 | int ok = EVP_DigestInit_ex (m_context, HASH_FUNCTION (), 0); |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 139 | if (!ok) |
| 140 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | void |
| 145 | Digest::finalize () |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 146 | { |
| 147 | if (m_buffer != 0) return; |
| 148 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 149 | m_buffer = new uint8_t [EVP_MAX_MD_SIZE]; |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 150 | |
| 151 | int ok = EVP_DigestFinal_ex (m_context, |
| 152 | m_buffer, &m_hashLength); |
| 153 | if (!ok) |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 154 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | std::size_t |
| 158 | Digest::getHash () |
| 159 | { |
| 160 | if (m_buffer == 0) |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 161 | finalize (); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 162 | |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 163 | if (sizeof (std::size_t) > m_hashLength) |
| 164 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 165 | |
| 166 | // just getting first sizeof(std::size_t) bytes |
| 167 | // not ideal, but should work pretty well |
| 168 | return reinterpret_cast<std::size_t> (m_buffer); |
| 169 | } |
| 170 | |
| 171 | bool |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 172 | Digest::operator == (const Digest &digest) const |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 173 | { |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 174 | if (m_buffer == 0 || digest.m_buffer == 0) |
| 175 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 176 | |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 177 | BOOST_ASSERT (m_hashLength == digest.m_hashLength); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 178 | |
| 179 | return memcmp (m_buffer, digest.m_buffer, m_hashLength) == 0; |
| 180 | } |
| 181 | |
| 182 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 183 | void |
| 184 | Digest::update (const uint8_t *buffer, size_t size) |
| 185 | { |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 186 | // cout << "Update: " << (void*)buffer << " / size: " << size << "\n"; |
| 187 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 188 | // cannot update Digest when it has been finalized |
| 189 | if (m_buffer != 0) |
| 190 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 191 | |
| 192 | bool ok = EVP_DigestUpdate (m_context, buffer, size); |
| 193 | if (!ok) |
| 194 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 195 | } |
| 196 | |
| 197 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 198 | Digest & |
| 199 | Digest::operator << (const Digest &src) |
| 200 | { |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 201 | if (src.m_buffer == 0) |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 202 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 203 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 204 | update (src.m_buffer, src.m_hashLength); |
| 205 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 206 | return *this; |
| 207 | } |
| 208 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 209 | std::ostream & |
| 210 | operator << (std::ostream &os, const Digest &digest) |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 211 | { |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 212 | BOOST_ASSERT (digest.m_hashLength != 0); |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 213 | |
| 214 | ostreambuf_iterator<char> out_it (os); // ostream iterator |
| 215 | // need to encode to base64 |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 216 | copy (string_from_binary (reinterpret_cast<const char*> (digest.m_buffer)), |
| 217 | 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] | 218 | out_it); |
| 219 | |
| 220 | return os; |
| 221 | } |
| 222 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 223 | std::istream & |
| 224 | operator >> (std::istream &is, Digest &digest) |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 225 | { |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 226 | string str; |
| 227 | is >> str; // read string first |
Alexander Afanasyev | b71beab | 2012-03-05 21:13:49 -0800 | [diff] [blame] | 228 | |
| 229 | if (str.size () == 0) |
| 230 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 231 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 232 | // uint8_t padding = (3 - str.size () % 3) % 3; |
| 233 | // for (uint8_t i = 0; i < padding; i++) str.push_back ('='); |
| 234 | |
| 235 | // only empty digest object can be used for reading |
| 236 | if (digest.m_buffer != 0) |
| 237 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 238 | |
| 239 | digest.m_buffer = new uint8_t [EVP_MAX_MD_SIZE]; |
| 240 | uint8_t *end = copy (string_to_binary (str.begin ()), |
| 241 | string_to_binary (str.end ()), |
| 242 | digest.m_buffer); |
| 243 | |
| 244 | digest.m_hashLength = end - digest.m_buffer; |
| 245 | |
| 246 | return is; |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 250 | } // Sync |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 251 | |