1 /*
2 ============
3 SHA-1 in C++
4 ============
5
6 100% Public Domain.
7
8 Original C Code
9 -- Steve Reid <steve@edmweb.com>
10 Small changes to fit into bglibs
11 -- Bruce Guenter <bruce@untroubled.org>
12 Translation to simpler C++ Code
13 -- Volker Grabsch <vog@notjusthosting.com>
14 */
15
16 #include "SHA1.h"
17 #include <sstream>
18 #include <iomanip>
19 #include <fstream>
20
21 /* Help macros */
22 #define SHA1_ROL(value, bits) (((value) << (bits)) | (((value) & 0xffffffff) >> (32 - (bits))))
23 #define SHA1_BLK(i) (block[i&15] = SHA1_ROL(block[(i+13)&15] ^ block[(i+8)&15] ^ block[(i+2)&15] ^ block[i&15],1))
24
25 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
26 #define SHA1_R0(v,w,x,y,z,i) z += ((w&(x^y))^y) + block[i] + 0x5a827999 + SHA1_ROL(v,5); w=SHA1_ROL(w,30);
27 #define SHA1_R1(v,w,x,y,z,i) z += ((w&(x^y))^y) + SHA1_BLK(i) + 0x5a827999 + SHA1_ROL(v,5); w=SHA1_ROL(w,30);
28 #define SHA1_R2(v,w,x,y,z,i) z += (w^x^y) + SHA1_BLK(i) + 0x6ed9eba1 + SHA1_ROL(v,5); w=SHA1_ROL(w,30);
29 #define SHA1_R3(v,w,x,y,z,i) z += (((w|x)&y)|(w&x)) + SHA1_BLK(i) + 0x8f1bbcdc + SHA1_ROL(v,5); w=SHA1_ROL(w,30);
30 #define SHA1_R4(v,w,x,y,z,i) z += (w^x^y) + SHA1_BLK(i) + 0xca62c1d6 + SHA1_ROL(v,5); w=SHA1_ROL(w,30);
31
SHA1()32 SHA1::SHA1()
33 {
34 reset();
35 }
36
37
update(const std::string & s)38 void SHA1::update(const std::string &s)
39 {
40 std::istringstream is(s);
41 update(is);
42 }
43
44
update(std::istream & is)45 void SHA1::update(std::istream &is)
46 {
47 std::string rest_of_buffer;
48 read(is, rest_of_buffer, BLOCK_BYTES - buffer.size());
49 buffer += rest_of_buffer;
50
51 while (is)
52 {
53 uint32 block[BLOCK_INTS];
54 buffer_to_block(buffer, block);
55 transform(block);
56 read(is, buffer, BLOCK_BYTES);
57 }
58 }
59
60
61 /*
62 * Add padding and return the message digest.
63 */
64
final()65 std::string SHA1::final()
66 {
67 /* Total number of hashed bits */
68 uint64 total_bits = (transforms*BLOCK_BYTES + buffer.size()) * 8;
69
70 /* Padding */
71 buffer += 0x80;
72 std::string::size_type orig_size = (unsigned int)buffer.size();
73 while (buffer.size() < BLOCK_BYTES)
74 {
75 buffer += (char)0x00;
76 }
77
78 uint32 block[BLOCK_INTS];
79 buffer_to_block(buffer, block);
80
81 if (orig_size > BLOCK_BYTES - 8)
82 {
83 transform(block);
84 for (unsigned int i = 0; i < BLOCK_INTS - 2; i++)
85 {
86 block[i] = 0;
87 }
88 }
89
90 /* Append total_bits, split this uint64 into two uint32 */
91 block[BLOCK_INTS - 1] = (SHA1::uint32)total_bits;
92 block[BLOCK_INTS - 2] = (total_bits >> 32);
93 transform(block);
94
95 // Modified from original to output a binary string
96 std::string result;
97 result.reserve(16);
98
99 for (unsigned int i = 0; i < DIGEST_INTS; i++)
100 {
101 uint32 v = digest[i];
102 result.append(1, (char)(v >> 24));
103 result.append(1, (char)(v >> 16));
104 result.append(1, (char)(v >> 8));
105 result.append(1, (char)(v));
106 }
107
108 /* Reset for next run */
109 reset();
110
111 return result;
112 }
113
reset()114 void SHA1::reset()
115 {
116 /* SHA1 initialization constants */
117 digest[0] = 0x67452301;
118 digest[1] = 0xefcdab89;
119 digest[2] = 0x98badcfe;
120 digest[3] = 0x10325476;
121 digest[4] = 0xc3d2e1f0;
122
123 /* Reset counters */
124 transforms = 0;
125 buffer = "";
126 }
127
128
129 /*
130 * Hash a single 512-bit block. This is the core of the algorithm.
131 */
132
transform(uint32 block[BLOCK_BYTES])133 void SHA1::transform(uint32 block[BLOCK_BYTES])
134 {
135 /* Copy digest[] to working vars */
136 uint32 a = digest[0];
137 uint32 b = digest[1];
138 uint32 c = digest[2];
139 uint32 d = digest[3];
140 uint32 e = digest[4];
141
142
143 /* 4 rounds of 20 operations each. Loop unrolled. */
144 SHA1_R0(a,b,c,d,e, 0);
145 SHA1_R0(e,a,b,c,d, 1);
146 SHA1_R0(d,e,a,b,c, 2);
147 SHA1_R0(c,d,e,a,b, 3);
148 SHA1_R0(b,c,d,e,a, 4);
149 SHA1_R0(a,b,c,d,e, 5);
150 SHA1_R0(e,a,b,c,d, 6);
151 SHA1_R0(d,e,a,b,c, 7);
152 SHA1_R0(c,d,e,a,b, 8);
153 SHA1_R0(b,c,d,e,a, 9);
154 SHA1_R0(a,b,c,d,e,10);
155 SHA1_R0(e,a,b,c,d,11);
156 SHA1_R0(d,e,a,b,c,12);
157 SHA1_R0(c,d,e,a,b,13);
158 SHA1_R0(b,c,d,e,a,14);
159 SHA1_R0(a,b,c,d,e,15);
160 SHA1_R1(e,a,b,c,d,16);
161 SHA1_R1(d,e,a,b,c,17);
162 SHA1_R1(c,d,e,a,b,18);
163 SHA1_R1(b,c,d,e,a,19);
164 SHA1_R2(a,b,c,d,e,20);
165 SHA1_R2(e,a,b,c,d,21);
166 SHA1_R2(d,e,a,b,c,22);
167 SHA1_R2(c,d,e,a,b,23);
168 SHA1_R2(b,c,d,e,a,24);
169 SHA1_R2(a,b,c,d,e,25);
170 SHA1_R2(e,a,b,c,d,26);
171 SHA1_R2(d,e,a,b,c,27);
172 SHA1_R2(c,d,e,a,b,28);
173 SHA1_R2(b,c,d,e,a,29);
174 SHA1_R2(a,b,c,d,e,30);
175 SHA1_R2(e,a,b,c,d,31);
176 SHA1_R2(d,e,a,b,c,32);
177 SHA1_R2(c,d,e,a,b,33);
178 SHA1_R2(b,c,d,e,a,34);
179 SHA1_R2(a,b,c,d,e,35);
180 SHA1_R2(e,a,b,c,d,36);
181 SHA1_R2(d,e,a,b,c,37);
182 SHA1_R2(c,d,e,a,b,38);
183 SHA1_R2(b,c,d,e,a,39);
184 SHA1_R3(a,b,c,d,e,40);
185 SHA1_R3(e,a,b,c,d,41);
186 SHA1_R3(d,e,a,b,c,42);
187 SHA1_R3(c,d,e,a,b,43);
188 SHA1_R3(b,c,d,e,a,44);
189 SHA1_R3(a,b,c,d,e,45);
190 SHA1_R3(e,a,b,c,d,46);
191 SHA1_R3(d,e,a,b,c,47);
192 SHA1_R3(c,d,e,a,b,48);
193 SHA1_R3(b,c,d,e,a,49);
194 SHA1_R3(a,b,c,d,e,50);
195 SHA1_R3(e,a,b,c,d,51);
196 SHA1_R3(d,e,a,b,c,52);
197 SHA1_R3(c,d,e,a,b,53);
198 SHA1_R3(b,c,d,e,a,54);
199 SHA1_R3(a,b,c,d,e,55);
200 SHA1_R3(e,a,b,c,d,56);
201 SHA1_R3(d,e,a,b,c,57);
202 SHA1_R3(c,d,e,a,b,58);
203 SHA1_R3(b,c,d,e,a,59);
204 SHA1_R4(a,b,c,d,e,60);
205 SHA1_R4(e,a,b,c,d,61);
206 SHA1_R4(d,e,a,b,c,62);
207 SHA1_R4(c,d,e,a,b,63);
208 SHA1_R4(b,c,d,e,a,64);
209 SHA1_R4(a,b,c,d,e,65);
210 SHA1_R4(e,a,b,c,d,66);
211 SHA1_R4(d,e,a,b,c,67);
212 SHA1_R4(c,d,e,a,b,68);
213 SHA1_R4(b,c,d,e,a,69);
214 SHA1_R4(a,b,c,d,e,70);
215 SHA1_R4(e,a,b,c,d,71);
216 SHA1_R4(d,e,a,b,c,72);
217 SHA1_R4(c,d,e,a,b,73);
218 SHA1_R4(b,c,d,e,a,74);
219 SHA1_R4(a,b,c,d,e,75);
220 SHA1_R4(e,a,b,c,d,76);
221 SHA1_R4(d,e,a,b,c,77);
222 SHA1_R4(c,d,e,a,b,78);
223 SHA1_R4(b,c,d,e,a,79);
224
225 /* Add the working vars back into digest[] */
226 digest[0] += a;
227 digest[1] += b;
228 digest[2] += c;
229 digest[3] += d;
230 digest[4] += e;
231
232 /* Count the number of transformations */
233 transforms++;
234 }
235
236
buffer_to_block(const std::string & buffer,uint32 block[BLOCK_BYTES])237 void SHA1::buffer_to_block(const std::string &buffer, uint32 block[BLOCK_BYTES])
238 {
239 /* Convert the std::string (byte buffer) to a uint32 array (MSB) */
240 for (unsigned int i = 0; i < BLOCK_INTS; i++)
241 {
242 block[i] = (buffer[4*i+3] & 0xff)
243 | (buffer[4*i+2] & 0xff)<<8
244 | (buffer[4*i+1] & 0xff)<<16
245 | (buffer[4*i+0] & 0xff)<<24;
246 }
247 }
248
249
read(std::istream & is,std::string & s,int max)250 void SHA1::read(std::istream &is, std::string &s, int max)
251 {
252 char* sbuf = new char[max];
253 is.read(sbuf, max);
254 s.assign(sbuf, is.gcount());
255 delete [] sbuf;
256 }
257
258
from_string(const std::string & string)259 std::string SHA1::from_string(const std::string &string)
260 {
261 SHA1 checksum;
262 checksum.update(string);
263 return checksum.final();
264 }
265