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 | |
| 123 | void |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 124 | Digest::reset () |
| 125 | { |
| 126 | if (m_buffer != 0) |
| 127 | { |
| 128 | delete [] m_buffer; |
| 129 | m_buffer = 0; |
| 130 | } |
| 131 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame^] | 132 | int ok = EVP_DigestInit_ex (m_context, HASH_FUNCTION (), 0); |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 133 | if (!ok) |
| 134 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | void |
| 139 | Digest::finalize () |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 140 | { |
| 141 | if (m_buffer != 0) return; |
| 142 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame^] | 143 | m_buffer = new uint8_t [EVP_MAX_MD_SIZE]; |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 144 | |
| 145 | int ok = EVP_DigestFinal_ex (m_context, |
| 146 | m_buffer, &m_hashLength); |
| 147 | if (!ok) |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 148 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | std::size_t |
| 152 | Digest::getHash () |
| 153 | { |
| 154 | if (m_buffer == 0) |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 155 | finalize (); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 156 | |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 157 | BOOST_ASSERT (sizeof (std::size_t) <= m_hashLength); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 158 | |
| 159 | // just getting first sizeof(std::size_t) bytes |
| 160 | // not ideal, but should work pretty well |
| 161 | return reinterpret_cast<std::size_t> (m_buffer); |
| 162 | } |
| 163 | |
| 164 | bool |
| 165 | Digest::operator == (Digest &digest) |
| 166 | { |
| 167 | if (m_buffer == 0) |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 168 | finalize (); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 169 | |
| 170 | if (digest.m_buffer == 0) |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 171 | digest.finalize (); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 172 | |
Alexander Afanasyev | d94542d | 2012-03-05 08:41:46 -0800 | [diff] [blame] | 173 | BOOST_ASSERT (m_hashLength == digest.m_hashLength); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 174 | |
| 175 | return memcmp (m_buffer, digest.m_buffer, m_hashLength) == 0; |
| 176 | } |
| 177 | |
| 178 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 179 | void |
| 180 | Digest::update (const uint8_t *buffer, size_t size) |
| 181 | { |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame^] | 182 | // cout << "Update: " << (void*)buffer << " / size: " << size << "\n"; |
| 183 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 184 | // cannot update Digest when it has been finalized |
| 185 | if (m_buffer != 0) |
| 186 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 187 | |
| 188 | bool ok = EVP_DigestUpdate (m_context, buffer, size); |
| 189 | if (!ok) |
| 190 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 191 | } |
| 192 | |
| 193 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 194 | Digest & |
| 195 | Digest::operator << (const Digest &src) |
| 196 | { |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 197 | if (src.m_buffer == 0) |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 198 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 199 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame] | 200 | update (src.m_buffer, src.m_hashLength); |
| 201 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 202 | return *this; |
| 203 | } |
| 204 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame^] | 205 | std::ostream & |
| 206 | operator << (std::ostream &os, const Digest &digest) |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 207 | { |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame^] | 208 | BOOST_ASSERT (digest.m_hashLength != 0); |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 209 | |
| 210 | ostreambuf_iterator<char> out_it (os); // ostream iterator |
| 211 | // need to encode to base64 |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame^] | 212 | copy (string_from_binary (reinterpret_cast<const char*> (digest.m_buffer)), |
| 213 | 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] | 214 | out_it); |
| 215 | |
| 216 | return os; |
| 217 | } |
| 218 | |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame^] | 219 | std::istream & |
| 220 | operator >> (std::istream &is, Digest &digest) |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 221 | { |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame^] | 222 | string str; |
| 223 | is >> str; // read string first |
| 224 | // uint8_t padding = (3 - str.size () % 3) % 3; |
| 225 | // for (uint8_t i = 0; i < padding; i++) str.push_back ('='); |
| 226 | |
| 227 | // only empty digest object can be used for reading |
| 228 | if (digest.m_buffer != 0) |
| 229 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 230 | |
| 231 | digest.m_buffer = new uint8_t [EVP_MAX_MD_SIZE]; |
| 232 | uint8_t *end = copy (string_to_binary (str.begin ()), |
| 233 | string_to_binary (str.end ()), |
| 234 | digest.m_buffer); |
| 235 | |
| 236 | digest.m_hashLength = end - digest.m_buffer; |
| 237 | |
| 238 | return is; |
Alexander Afanasyev | b080dbf | 2012-03-05 10:25:22 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 242 | } // Sync |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 243 | |