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 | |
| 26 | #include "ns3/assert.h" |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 27 | #include <boost/exception/errinfo_at_line.hpp> |
| 28 | |
| 29 | using namespace boost; |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 31 | namespace Sync { |
| 32 | |
| 33 | Digest::Digest () |
| 34 | : m_buffer (0) |
| 35 | , m_hashLength (0) |
| 36 | { |
| 37 | m_context = EVP_MD_CTX_create (); |
| 38 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame^] | 39 | reset (); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | Digest::~Digest () |
| 43 | { |
| 44 | if (m_buffer != 0) |
| 45 | delete [] m_buffer; |
| 46 | |
| 47 | EVP_MD_CTX_destroy (m_context); |
| 48 | } |
| 49 | |
| 50 | void |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame^] | 51 | Digest::reset () |
| 52 | { |
| 53 | if (m_buffer != 0) |
| 54 | { |
| 55 | delete [] m_buffer; |
| 56 | m_buffer = 0; |
| 57 | } |
| 58 | |
| 59 | int ok = EVP_DigestInit_ex (m_context, EVP_sha1 (), 0); |
| 60 | if (!ok) |
| 61 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | void |
| 66 | Digest::finalize () |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 67 | { |
| 68 | if (m_buffer != 0) return; |
| 69 | |
| 70 | m_buffer = new uint8_t [HASH_SIZE]; |
| 71 | |
| 72 | int ok = EVP_DigestFinal_ex (m_context, |
| 73 | m_buffer, &m_hashLength); |
| 74 | if (!ok) |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 75 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | std::size_t |
| 79 | Digest::getHash () |
| 80 | { |
| 81 | if (m_buffer == 0) |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame^] | 82 | finalize (); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 83 | |
| 84 | NS_ASSERT (sizeof (std::size_t) <= m_hashLength); |
| 85 | |
| 86 | // just getting first sizeof(std::size_t) bytes |
| 87 | // not ideal, but should work pretty well |
| 88 | return reinterpret_cast<std::size_t> (m_buffer); |
| 89 | } |
| 90 | |
| 91 | bool |
| 92 | Digest::operator == (Digest &digest) |
| 93 | { |
| 94 | if (m_buffer == 0) |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame^] | 95 | finalize (); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 96 | |
| 97 | if (digest.m_buffer == 0) |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame^] | 98 | digest.finalize (); |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 99 | |
| 100 | NS_ASSERT (m_hashLength == digest.m_hashLength); |
| 101 | |
| 102 | return memcmp (m_buffer, digest.m_buffer, m_hashLength) == 0; |
| 103 | } |
| 104 | |
| 105 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame^] | 106 | void |
| 107 | Digest::update (const uint8_t *buffer, size_t size) |
| 108 | { |
| 109 | // cannot update Digest when it has been finalized |
| 110 | if (m_buffer != 0) |
| 111 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 112 | |
| 113 | bool ok = EVP_DigestUpdate (m_context, buffer, size); |
| 114 | if (!ok) |
| 115 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 116 | } |
| 117 | |
| 118 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 119 | Digest & |
| 120 | Digest::operator << (const Digest &src) |
| 121 | { |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame^] | 122 | if (src.m_buffer == 0) |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 123 | throw DigestCalculationError () << errinfo_at_line (__LINE__); |
| 124 | |
Alexander Afanasyev | e00ffbe | 2012-03-05 00:01:36 -0800 | [diff] [blame^] | 125 | update (src.m_buffer, src.m_hashLength); |
| 126 | |
Alexander Afanasyev | df718f5 | 2012-03-02 00:23:04 -0800 | [diff] [blame] | 127 | return *this; |
| 128 | } |
| 129 | |
| 130 | |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 131 | } // Sync |
Alexander Afanasyev | 8f25cbb | 2012-03-01 23:53:40 -0800 | [diff] [blame] | 132 | |