incrementaltp: respect physics overrides
[waspsaliva.git] / src / util / sha1.cpp
blobd61b262afef97d2fc04685c54d567d5a02d409d8
1 /* sha1.cpp
3 Copyright (c) 2005 Michael D. Leonhard
5 http://tamale.net/
7 Permission is hereby granted, free of charge, to any person obtaining a copy of
8 this software and associated documentation files (the "Software"), to deal in
9 the Software without restriction, including without limitation the rights to
10 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11 of the Software, and to permit persons to whom the Software is furnished to do
12 so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
27 #include <cstdio>
28 #include <cstring>
29 #include <cstdlib>
30 #include <cassert>
32 #include "sha1.h"
34 // print out memory in hexadecimal
35 void SHA1::hexPrinter( unsigned char* c, int l )
37 assert( c );
38 assert( l > 0 );
39 while( l > 0 )
41 printf( " %02x", *c );
42 l--;
43 c++;
47 // circular left bit rotation. MSB wraps around to LSB
48 Uint32 SHA1::lrot( Uint32 x, int bits )
50 return (x<<bits) | (x>>(32 - bits));
53 // Save a 32-bit unsigned integer to memory, in big-endian order
54 void SHA1::storeBigEndianUint32( unsigned char* byte, Uint32 num )
56 assert( byte );
57 byte[0] = (unsigned char)(num>>24);
58 byte[1] = (unsigned char)(num>>16);
59 byte[2] = (unsigned char)(num>>8);
60 byte[3] = (unsigned char)num;
64 // Constructor *******************************************************
65 SHA1::SHA1()
67 // make sure that the data type is the right size
68 assert( sizeof( Uint32 ) * 5 == 20 );
71 // Destructor ********************************************************
72 SHA1::~SHA1()
74 // erase data
75 H0 = H1 = H2 = H3 = H4 = 0;
76 for( int c = 0; c < 64; c++ ) bytes[c] = 0;
77 unprocessedBytes = size = 0;
80 // process ***********************************************************
81 void SHA1::process()
83 assert( unprocessedBytes == 64 );
84 //printf( "process: " ); hexPrinter( bytes, 64 ); printf( "\n" );
85 int t;
86 Uint32 a, b, c, d, e, K, f, W[80];
87 // starting values
88 a = H0;
89 b = H1;
90 c = H2;
91 d = H3;
92 e = H4;
93 // copy and expand the message block
94 for( t = 0; t < 16; t++ ) W[t] = (bytes[t*4] << 24)
95 +(bytes[t*4 + 1] << 16)
96 +(bytes[t*4 + 2] << 8)
97 + bytes[t*4 + 3];
98 for(; t< 80; t++ ) W[t] = lrot( W[t-3]^W[t-8]^W[t-14]^W[t-16], 1 );
100 /* main loop */
101 Uint32 temp;
102 for( t = 0; t < 80; t++ )
104 if( t < 20 ) {
105 K = 0x5a827999;
106 f = (b & c) | ((b ^ 0xFFFFFFFF) & d);//TODO: try using ~
107 } else if( t < 40 ) {
108 K = 0x6ed9eba1;
109 f = b ^ c ^ d;
110 } else if( t < 60 ) {
111 K = 0x8f1bbcdc;
112 f = (b & c) | (b & d) | (c & d);
113 } else {
114 K = 0xca62c1d6;
115 f = b ^ c ^ d;
117 temp = lrot(a,5) + f + e + W[t] + K;
118 e = d;
119 d = c;
120 c = lrot(b,30);
121 b = a;
122 a = temp;
123 //printf( "t=%d %08x %08x %08x %08x %08x\n",t,a,b,c,d,e );
125 /* add variables */
126 H0 += a;
127 H1 += b;
128 H2 += c;
129 H3 += d;
130 H4 += e;
131 //printf( "Current: %08x %08x %08x %08x %08x\n",H0,H1,H2,H3,H4 );
132 /* all bytes have been processed */
133 unprocessedBytes = 0;
136 // addBytes **********************************************************
137 void SHA1::addBytes( const char* data, int num )
139 assert( data );
140 assert( num >= 0 );
141 // add these bytes to the running total
142 size += num;
143 // repeat until all data is processed
144 while( num > 0 )
146 // number of bytes required to complete block
147 int needed = 64 - unprocessedBytes;
148 assert( needed > 0 );
149 // number of bytes to copy (use smaller of two)
150 int toCopy = (num < needed) ? num : needed;
151 // Copy the bytes
152 memcpy( bytes + unprocessedBytes, data, toCopy );
153 // Bytes have been copied
154 num -= toCopy;
155 data += toCopy;
156 unprocessedBytes += toCopy;
158 // there is a full block
159 if( unprocessedBytes == 64 ) process();
163 // digest ************************************************************
164 unsigned char* SHA1::getDigest()
166 // save the message size
167 Uint32 totalBitsL = size << 3;
168 Uint32 totalBitsH = size >> 29;
169 // add 0x80 to the message
170 addBytes( "\x80", 1 );
172 unsigned char footer[64] = {
173 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
174 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
175 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
176 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
177 // block has no room for 8-byte filesize, so finish it
178 if( unprocessedBytes > 56 )
179 addBytes( (char*)footer, 64 - unprocessedBytes);
180 assert( unprocessedBytes <= 56 );
181 // how many zeros do we need
182 int neededZeros = 56 - unprocessedBytes;
183 // store file size (in bits) in big-endian format
184 storeBigEndianUint32( footer + neededZeros , totalBitsH );
185 storeBigEndianUint32( footer + neededZeros + 4, totalBitsL );
186 // finish the final block
187 addBytes( (char*)footer, neededZeros + 8 );
188 // allocate memory for the digest bytes
189 unsigned char* digest = (unsigned char*)malloc( 20 );
190 // copy the digest bytes
191 storeBigEndianUint32( digest, H0 );
192 storeBigEndianUint32( digest + 4, H1 );
193 storeBigEndianUint32( digest + 8, H2 );
194 storeBigEndianUint32( digest + 12, H3 );
195 storeBigEndianUint32( digest + 16, H4 );
196 // return the digest
197 return digest;