1 /********************* */
4 ** Top contributors (to current version):
5 ** Dejan Jovanovic, Tim King
6 ** This file is part of the CVC4 project.
7 ** Copyright (c) 2009-2019 by the authors listed in the file AUTHORS
8 ** in the top-level source directory) and their institutional affiliations.
9 ** All rights reserved. See the file COPYING in the top-level source
10 ** directory for licensing information.\endverbatim
12 ** \brief [[ Add one-line brief description here ]]
14 ** [[ Add lengthier description here ]]
15 ** \todo document this file
18 // boost/uuid/sha1.hpp header file ----------------------------------------------//
20 // Copyright 2007 Andy Tompkins.
21 // Distributed under the Boost Software License, Version 1.0. (See
22 // accompanying file LICENSE_1_0.txt or copy at
23 // http://www.boost.org/LICENSE_1_0.txt)
26 // 29 May 2007 - Initial Revision
27 // 25 Feb 2008 - moved to namespace boost::uuids::detail
29 // This is a byte oriented implementation
30 // Note: this implementation does not handle message longer than
33 #ifndef __CVC4__EXAMPLES__HASHSMT__SHA1_H
34 #define __CVC4__EXAMPLES__HASHSMT__SHA1_H
40 #ifdef BOOST_NO_STDC_NAMESPACE
48 static_assert(sizeof(unsigned char)*8 == 8,
49 "Unexpected size for unsigned char");
50 static_assert(sizeof(unsigned int)*8 == 32,
51 "Unexpected size for unsigned int");
53 inline cvc4_uint32 left_rotate(cvc4_uint32 x, std::size_t n)
55 return (x<<n) ^ (x>> (32-n));
61 typedef cvc4_uint32(&digest_type)[5];
63 sha1(unsigned rounds = 80);
67 void process_byte(cvc4_uchar8 byte);
68 void process_block(void const* bytes_begin, void const* bytes_end);
69 void process_bytes(void const* buffer, std::size_t byte_count);
71 void get_digest(digest_type digest);
79 cvc4_uchar8 block_[64];
81 std::size_t block_byte_index_;
82 std::size_t byte_count_;
87 inline sha1::sha1(unsigned rounds)
93 inline void sha1::reset()
101 block_byte_index_ = 0;
105 inline void sha1::process_byte(cvc4_uchar8 byte)
107 block_[block_byte_index_++] = byte;
109 if (block_byte_index_ == 64) {
110 block_byte_index_ = 0;
115 inline void sha1::process_block(void const* bytes_begin, void const* bytes_end)
117 cvc4_uchar8 const* begin = static_cast<cvc4_uchar8 const*>(bytes_begin);
118 cvc4_uchar8 const* end = static_cast<cvc4_uchar8 const*>(bytes_end);
119 for(; begin != end; ++begin) {
120 process_byte(*begin);
124 inline void sha1::process_bytes(void const* buffer, std::size_t byte_count)
126 cvc4_uchar8 const* b = static_cast<cvc4_uchar8 const*>(buffer);
127 process_block(b, b+byte_count);
130 inline void sha1::process_block()
133 for (std::size_t i=0; i<16; ++i) {
134 w[i] = (block_[i*4 + 0] << 24);
135 w[i] |= (block_[i*4 + 1] << 16);
136 w[i] |= (block_[i*4 + 2] << 8);
137 w[i] |= (block_[i*4 + 3]);
139 for (std::size_t i=16; i<80; ++i) {
140 w[i] = left_rotate((w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16]), 1);
143 cvc4_uint32 a = h_[0];
144 cvc4_uint32 b = h_[1];
145 cvc4_uint32 c = h_[2];
146 cvc4_uint32 d = h_[3];
147 cvc4_uint32 e = h_[4];
149 for (std::size_t i=0; i<rounds_; ++i) {
154 f = (b & c) | (~b & d);
160 f = (b & c) | (b & d) | (c & d);
167 cvc4_uint32 temp = left_rotate(a, 5) + f + e + k + w[i];
170 c = left_rotate(b, 30);
182 inline void sha1::get_digest(digest_type digest)
184 std::size_t bit_count = byte_count_*8;
186 // append the bit '1' to the message
189 // append k bits '0', where k is the minimum number >= 0
190 // such that the resulting message length is congruent to 56 (mod 64)
191 // check if there is enough space for padding and bit_count
192 if (block_byte_index_ > 56) {
194 while (block_byte_index_ != 0) {
199 while (block_byte_index_ < 56) {
203 while (block_byte_index_ < 56) {
208 // append length of message (before pre-processing)
209 // as a 64-bit big-endian integer
214 process_byte( static_cast<unsigned char>((bit_count>>24) & 0xFF));
215 process_byte( static_cast<unsigned char>((bit_count>>16) & 0xFF));
216 process_byte( static_cast<unsigned char>((bit_count>>8 ) & 0xFF));
217 process_byte( static_cast<unsigned char>((bit_count) & 0xFF));
227 } // namespace hashsmt
229 #endif /* __CVC4__EXAMPLES__HASHSMT__SHA1_H */