74 |
74 |
75 %} |
75 %} |
76 ! ! |
76 ! ! |
77 |
77 |
78 !SHA1Stream primitiveFunctions! |
78 !SHA1Stream primitiveFunctions! |
79 void SHA1Init(); |
79 |
80 void SHA1Update(); |
80 %{ |
81 void SHA1Final(); |
81 |
82 |
82 /* |
83 #endif /* USE_ANSI_C */ |
83 * SHA-1 in C |
|
84 * By Steve Reid <steve@edmweb.com> |
|
85 * 100% Public Domain |
|
86 */ |
|
87 |
|
88 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) |
|
89 |
|
90 /* blk0() and blk() perform the initial expand. */ |
|
91 /* I got the idea of expanding during the round function from SSLeay */ |
|
92 #ifdef LITTLE_ENDIAN |
|
93 # define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ |
|
94 |(rol(block->l[i],8)&0x00FF00FF)) |
|
95 #else |
|
96 # define blk0(i) block->l[i] |
|
97 #endif |
|
98 |
|
99 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ |
|
100 ^block->l[(i+2)&15]^block->l[i&15],1)) |
|
101 |
|
102 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ |
|
103 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); |
|
104 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); |
|
105 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); |
|
106 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); |
|
107 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); |
|
108 |
|
109 /* |
|
110 * Hash a single 512-bit block. This is the core of the algorithm. |
|
111 */ |
|
112 static void |
|
113 #if USE_ANSI_C |
|
114 SHA1Transform (unsigned int32 state[5], unsigned char buffer[64]) |
|
115 #else |
|
116 SHA1Transform (state, buffer) |
|
117 unsigned int32 state[5]; |
|
118 unsigned char buffer[64]; |
|
119 #endif |
|
120 { |
|
121 unsigned int32 a, b, c, d, e; |
|
122 typedef union { |
|
123 unsigned char c[64]; |
|
124 unsigned int32 /* long */ l[16]; |
|
125 } CHAR64LONG16; |
|
126 CHAR64LONG16* block; |
|
127 #ifdef SHA1HANDSOFF |
|
128 static unsigned char workspace[64]; |
|
129 block = (CHAR64LONG16*)workspace; |
|
130 memcpy(block, buffer, 64); |
|
131 #else |
|
132 block = (CHAR64LONG16*)buffer; |
|
133 #endif |
|
134 /* Copy context->state[] to working vars */ |
|
135 a = state[0]; |
|
136 b = state[1]; |
|
137 c = state[2]; |
|
138 d = state[3]; |
|
139 e = state[4]; |
|
140 /* 4 rounds of 20 operations each. Loop unrolled. */ |
|
141 R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3); |
|
142 R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7); |
|
143 R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11); |
|
144 R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15); |
|
145 R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19); |
|
146 R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23); |
|
147 R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27); |
|
148 R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31); |
|
149 R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35); |
|
150 R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39); |
|
151 R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43); |
|
152 R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47); |
|
153 R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51); |
|
154 R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55); |
|
155 R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59); |
|
156 R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63); |
|
157 R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67); |
|
158 R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71); |
|
159 R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75); |
|
160 R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79); |
|
161 /* Add the working vars back into context.state[] */ |
|
162 state[0] += a; |
|
163 state[1] += b; |
|
164 state[2] += c; |
|
165 state[3] += d; |
|
166 state[4] += e; |
|
167 /* Wipe variables */ |
|
168 a = b = c = d = e = 0; |
|
169 } |
|
170 |
|
171 /* |
|
172 * SHA1Init - Initialize new context |
|
173 */ |
|
174 STATIC_INLINE void |
|
175 #if USE_ANSI_C |
|
176 SHA1Init(SHA1_CTX* context) |
|
177 #else |
|
178 SHA1Init(context) |
|
179 SHA1_CTX* context; |
|
180 #endif |
|
181 { |
|
182 /* SHA1 initialization constants */ |
|
183 context->state[0] = 0x67452301; |
|
184 context->state[1] = 0xEFCDAB89; |
|
185 context->state[2] = 0x98BADCFE; |
|
186 context->state[3] = 0x10325476; |
|
187 context->state[4] = 0xC3D2E1F0; |
|
188 context->count[0] = context->count[1] = 0; |
|
189 } |
|
190 |
|
191 |
|
192 /* |
|
193 * Run your data through this. |
|
194 */ |
|
195 void |
|
196 #if USE_ANSI_C |
|
197 SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int len) |
|
198 #else |
|
199 SHA1Update(context, data, len) |
|
200 SHA1_CTX* context; |
|
201 unsigned char* data; |
|
202 unsigned int len; |
|
203 #endif |
|
204 { |
|
205 unsigned int i, j; |
|
206 |
|
207 j = (context->count[0] >> 3) & 63; |
|
208 if ((context->count[0] += len << 3) < (len << 3)) context->count[1]++; |
|
209 context->count[1] += (len >> 29); |
|
210 if ((j + len) > 63) { |
|
211 memcpy(&context->buffer[j], data, (i = 64-j)); |
|
212 SHA1Transform(context->state, context->buffer); |
|
213 for ( ; i + 63 < len; i += 64) { |
|
214 SHA1Transform(context->state, &data[i]); |
|
215 } |
|
216 j = 0; |
|
217 } |
|
218 else i = 0; |
|
219 memcpy(&context->buffer[j], &data[i], len - i); |
|
220 } |
|
221 |
|
222 |
|
223 /* |
|
224 * Add padding and return the message digest. |
|
225 */ |
|
226 void |
|
227 #if USE_ANSI_C |
|
228 SHA1Final(unsigned char digest[20], SHA1_CTX* context) |
|
229 #else |
|
230 SHA1Final(digest, context) |
|
231 unsigned char digest[20]; |
|
232 SHA1_CTX* context; |
|
233 #endif |
|
234 { |
|
235 unsigned int32 i, j; |
|
236 unsigned char finalcount[8]; |
|
237 |
|
238 for (i = 0; i < 8; i++) { |
|
239 finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] |
|
240 >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */ |
|
241 } |
|
242 SHA1Update(context, (unsigned char *)"\200", 1); |
|
243 while ((context->count[0] & 504) != 448) { |
|
244 SHA1Update(context, (unsigned char *)"\0", 1); |
|
245 } |
|
246 SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ |
|
247 for (i = 0; i < 20; i++) { |
|
248 digest[i] = (unsigned char) |
|
249 ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255); |
|
250 } |
|
251 /* Wipe variables */ |
|
252 i = j = 0; |
|
253 memset(context->buffer, 0, 64); |
|
254 memset(context->state, 0, 20); |
|
255 memset(context->count, 0, 8); |
|
256 memset(&finalcount, 0, 8); |
|
257 #ifdef SHA1HANDSOFF /* make SHA1Transform overwrite it's own static vars */ |
|
258 SHA1Transform(context->state, context->buffer); |
|
259 #endif |
|
260 } |
|
261 |
|
262 #if 0 |
|
263 /*************************************************************/ |
|
264 |
|
265 /* |
|
266 * SHA1 test program |
|
267 */ |
|
268 |
|
269 int main(int argc, char** argv) |
|
270 { |
|
271 int i, j; |
|
272 SHA1_CTX context; |
|
273 unsigned char digest[20], buffer[16384]; |
|
274 FILE* file; |
|
275 |
|
276 if (argc > 2) { |
|
277 puts("Public domain SHA-1 implementation - by Steve Reid <steve@edmweb.com>"); |
|
278 puts("Produces the SHA-1 hash of a file, or stdin if no file is specified."); |
|
279 exit(0); |
|
280 } |
|
281 if (argc < 2) { |
|
282 file = stdin; |
|
283 } |
|
284 else { |
|
285 if (!(file = fopen(argv[1], "rb"))) { |
|
286 fputs("Unable to open file.", stderr); |
|
287 exit(-1); |
|
288 } |
|
289 } |
|
290 SHA1Init(&context); |
|
291 while (!feof(file)) { /* note: what if ferror(file) */ |
|
292 i = fread(buffer, 1, 16384, file); |
|
293 SHA1Update(&context, buffer, i); |
|
294 } |
|
295 SHA1Final(digest, &context); |
|
296 fclose(file); |
|
297 for (i = 0; i < 5; i++) { |
|
298 for (j = 0; j < 4; j++) { |
|
299 printf("%02X", digest[i*4+j]); |
|
300 } |
|
301 putchar(' '); |
|
302 } |
|
303 putchar('\n'); |
|
304 exit(0); |
|
305 } |
|
306 #endif |
84 |
307 |
85 %} |
308 %} |
86 ! ! |
309 ! ! |
87 |
310 |
88 !SHA1Stream class methodsFor:'documentation'! |
311 !SHA1Stream class methodsFor:'documentation'! |
105 " |
328 " |
106 Generate a SHA-1 hash value as defined in |
329 Generate a SHA-1 hash value as defined in |
107 NIST, FIPS PUB 180-1: Secure Hash Standard, April 1995. |
330 NIST, FIPS PUB 180-1: Secure Hash Standard, April 1995. |
108 |
331 |
109 Notice (2005): |
332 Notice (2005): |
110 Be aware that SHA-1 is considered broken and may not be appropriate in some applications. |
333 Be aware that SHA-1 is considered broken and may not be appropriate in some applications. |
111 Especially it should no longer be used for security stuff. |
334 Especially it should no longer be used for security stuff. |
112 |
335 |
113 performance: roughly |
336 performance: roughly |
114 200 Mb/s on a 2012 MAC Powerbook (2.6Ghz I7) |
337 200 Mb/s on a 2012 MAC Powerbook (2.6Ghz I7) |
115 150 Mb/s on a 2007 MAC Powerbook (2.6Ghz Duo) |
338 150 Mb/s on a 2007 MAC Powerbook (2.6Ghz Duo) |
116 120 Mb/s on a 2.5Ghz 64X2 Athlon 4800+ (64bit) |
339 120 Mb/s on a 2.5Ghz 64X2 Athlon 4800+ (64bit) |
117 47400 Kb/s on a 2Ghz Duo (old measure) |
340 47400 Kb/s on a 2Ghz Duo (old measure) |
118 9580 Kb/s on a 400Mhz PIII |
341 9580 Kb/s on a 400Mhz PIII |
119 3970 Kb/s on a 300Mhz Sparc |
342 3970 Kb/s on a 300Mhz Sparc |
120 |
343 |
121 [author:] |
344 [author:] |
122 Stefan Vogel |
345 Stefan Vogel |
123 |
346 |
124 [see also:] |
347 [see also:] |
125 MD5Stream |
348 MD5Stream |
126 SHA256Stream SHA512Stream (in libcrypt) |
349 SHA256Stream SHA512Stream (in libcrypt) |
127 |
350 |
128 [class variables:] |
351 [class variables:] |
129 HashSize size of returned hash value |
352 HashSize size of returned hash value |
130 ContextSize (implementation) size of hash context |
353 ContextSize (implementation) size of hash context |
131 |
354 |
132 [instance variables:] |
355 [instance variables:] |
133 hashContext (implementation) |
356 hashContext (implementation) |
134 internal buffer for computation of the hash value |
357 internal buffer for computation of the hash value |
135 " |
358 " |
136 ! |
359 ! |
137 |
360 |
138 examples |
361 examples |
139 " |
362 " |
140 Test Vectors (from FIPS PUB 180-1); results are: |
363 Test Vectors (from FIPS PUB 180-1); results are: |
141 |
364 |
142 [exBegin] |
365 [exBegin] |
143 |hashStream| |
366 |hashStream| |
144 |
367 |
145 hashStream := SHA1Stream new. |
368 hashStream := SHA1Stream new. |
146 hashStream nextPut:'abc'. |
369 hashStream nextPut:'abc'. |
147 hashStream hashValue printOn:Transcript base:16. Transcript cr. |
370 hashStream hashValue printOn:Transcript base:16. Transcript cr. |
148 hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'. |
371 hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'. |
149 hashStream hashValue printOn:Transcript base:16. Transcript cr. |
372 hashStream hashValue printOn:Transcript base:16. Transcript cr. |
150 [exEnd] |
373 [exEnd] |
151 |
374 |
152 [exBegin] |
375 [exBegin] |
153 |hashValue| |
376 |hashValue| |
154 |
377 |
155 hashValue := SHA1Stream hashValueOf:'hello world'. |
378 hashValue := SHA1Stream hashValueOf:'hello world'. |
156 self assert:(hashValue hexPrintString = '2AAE6C35C94FCFB415DBE95F408B9CE91EE846ED') |
379 self assert:(hashValue hexPrintString = '2AAE6C35C94FCFB415DBE95F408B9CE91EE846ED') |
157 [exEnd] |
380 [exEnd] |
158 |
381 |
159 [exBegin] |
382 [exBegin] |
160 |hashValue| |
383 |hashValue| |
161 |
384 |
162 hashValue := SHA1Stream hashValueOf:'abc'. |
385 hashValue := SHA1Stream hashValueOf:'abc'. |
163 hashValue printOn:Transcript base:16. Transcript cr. |
386 hashValue printOn:Transcript base:16. Transcript cr. |
164 [exEnd] |
387 [exEnd] |
165 |
388 |
166 [exBegin] |
389 [exBegin] |
167 |hashStream| |
390 |hashStream| |
168 |
391 |
169 hashStream := SHA1Stream new. |
392 hashStream := SHA1Stream new. |
170 hashStream nextPut:'abc' asByteArray. |
393 hashStream nextPut:'abc' asByteArray. |
171 hashStream hashValue printOn:Transcript base:16. Transcript cr. |
394 hashStream hashValue printOn:Transcript base:16. Transcript cr. |
172 hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' asByteArray. |
395 hashStream nextPut:'dbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq' asByteArray. |
173 hashStream hashValue printOn:Transcript base:16. Transcript cr. |
396 hashStream hashValue printOn:Transcript base:16. Transcript cr. |
174 |
397 |
175 [exEnd] |
398 [exEnd] |
176 |
399 |
177 [exBegin] |
400 [exBegin] |
178 |hashStream| |
401 |hashStream| |
179 |
402 |
180 hashStream := SHA1Stream new. |
403 hashStream := SHA1Stream new. |
181 1000000 timesRepeat:[ hashStream nextPut:$a ]. |
404 1000000 timesRepeat:[ hashStream nextPut:$a ]. |
182 hashStream hashValue printOn:Transcript base:16. Transcript cr. |
405 hashStream hashValue printOn:Transcript base:16. Transcript cr. |
183 [exEnd] |
406 [exEnd] |
184 |
407 |
185 [exBegin] |
408 [exBegin] |
186 |hashStream| |
409 |hashStream| |
187 |
410 |
188 hashStream := SHA1Stream new. |
411 hashStream := SHA1Stream new. |
189 hashStream nextPut:'a'. |
412 hashStream nextPut:'a'. |
190 hashStream hashValue printOn:Transcript base:16. Transcript cr. |
413 hashStream hashValue printOn:Transcript base:16. Transcript cr. |
191 [exEnd] |
414 [exEnd] |
192 |
415 |
193 [exBegin] |
416 [exBegin] |
194 |hashStream| |
417 |hashStream| |
195 |
418 |
196 hashStream := SHA1Stream new. |
419 hashStream := SHA1Stream new. |
197 hashStream nextPut:$a. |
420 hashStream nextPut:$a. |
198 hashStream hashValue printOn:Transcript base:16. Transcript cr. |
421 hashStream hashValue printOn:Transcript base:16. Transcript cr. |
199 [exEnd] |
422 [exEnd] |
200 |
423 |
201 timing throughput: |
424 timing throughput: |
202 [exBegin] |
425 [exBegin] |
203 |hashStream n t| |
426 |hashStream n t| |
204 |
427 |
205 hashStream := SHA1Stream new. |
428 hashStream := SHA1Stream new. |
206 n := 1000000. |
429 n := 1000000. |
207 t := Time millisecondsToRun:[ |
430 t := Time millisecondsToRun:[ |
208 n timesRepeat:[ |
431 n timesRepeat:[ |
209 hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
432 hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
210 ]. |
433 ]. |
211 ]. |
434 ]. |
212 t := (t / 1000) asFloat. |
435 t := (t / 1000) asFloat. |
213 Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'. |
436 Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'. |
214 Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
437 Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
215 [exEnd] |
438 [exEnd] |
216 " |
439 " |
217 ! |
440 ! |
218 |
441 |
219 performance |
442 performance |
220 " |
443 " |
221 CPU cc algo mb/sec |
444 CPU cc algo mb/sec |
222 |
445 |
223 MAC (2010 macbook; 2.7Ghz Duo) clang -O2 slow 128.5 |
446 MAC (2010 macbook; 2.7Ghz Duo) clang -O2 slow 128.5 |
224 132 |
447 132 |
225 MAC (2012 macbook; 2.6Ghz I7) clang -O2 203.5 |
448 MAC (2012 macbook; 2.6Ghz I7) clang -O2 203.5 |
226 |
449 |
227 |
450 |
228 |
451 |
229 |
452 |
230 timing throughput: |
453 timing throughput: |
231 [exBegin] |
454 [exBegin] |
232 |hashStream n t| |
455 |hashStream n t| |
233 |
456 |
234 hashStream := SHA1Stream new. |
457 hashStream := SHA1Stream new. |
235 n := 1000000. |
458 n := 1000000. |
236 t := Time millisecondsToRun:[ |
459 t := Time millisecondsToRun:[ |
237 n timesRepeat:[ |
460 n timesRepeat:[ |
238 hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
461 hashStream nextPutAll:'12345678901234567890123456789012345678901234567890'. |
239 ]. |
462 ]. |
240 ]. |
463 ]. |
241 t := (t / 1000) asFloat. |
464 t := (t / 1000) asFloat. |
242 Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'. |
465 Transcript show:t; show:' seconds for '; show:(50*n/1024) asFloat; showCR:' Kb'. |
243 Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
466 Transcript show:(n*50/1024 / t); showCR:' Kb/s' |
244 [exEnd] |
467 [exEnd] |
245 " |
468 " |
246 ! ! |
469 ! ! |
247 |
470 |
248 !SHA1Stream class methodsFor:'initialization'! |
471 !SHA1Stream class methodsFor:'initialization'! |
249 |
472 |