1 /*-
2  * Copyright (c) 2001-2003 Allan Saddi <allan@saddi.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY ALLAN SADDI AND HIS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL ALLAN SADDI OR HIS CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $Id: sha1.c 680 2003-07-25 21:57:38Z asaddi $
27  */
28 
29 /*
30  * Define WORDS_BIGENDIAN if compiling on a big-endian architecture.
31  *
32  * Define SHA1_TEST to test the implementation using the NIST's
33  * sample messages. The output should be:
34  *
35  *   a9993e36 4706816a ba3e2571 7850c26c 9cd0d89d
36  *   84983e44 1c3bd26e baae4aa1 f95129e5 e54670f1
37  *   34aa973c d4c4daa4 f61eeb2b dbad2731 6534016f
38  */
39 
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif /* HAVE_CONFIG_H */
43 
44 #if HAVE_INTTYPES_H
45 # include <inttypes.h>
46 #else
47 # if HAVE_STDINT_H
48 #  include <stdint.h>
49 # endif
50 #endif
51 
52 #ifdef _MSC_VER
53 #define inline __inline
54 #endif
55 
56 #include <string.h>
57 
58 #include "sha1.h"
59 
60 #ifdef __clang__
61 #pragma clang diagnostic push
62 #pragma clang diagnostic ignored "-Wunused-const-variable"
63 #endif
64 
65 #if 0
66 static const char rcsid[] =
67 	"$Id: sha1.c 680 2003-07-25 21:57:38Z asaddi $";
68 #endif /* !lint */
69 
70 #ifdef __clang__
71 #pragma clang diagnostic pop
72 #endif
73 
74 #define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
75 #define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n))))
76 
77 #define F_0_19(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
78 #define F_20_39(x, y, z) ((x) ^ (y) ^ (z))
79 #define F_40_59(x, y, z) (((x) & ((y) | (z))) | ((y) & (z)))
80 #define F_60_79(x, y, z) ((x) ^ (y) ^ (z))
81 
82 #define DO_ROUND(F, K) { \
83   temp = ROTL(a, 5) + F(b, c, d) + e + *(W++) + K; \
84   e = d; \
85   d = c; \
86   c = ROTL(b, 30); \
87   b = a; \
88   a = temp; \
89 }
90 
91 #define K_0_19 0x5a827999L
92 #define K_20_39 0x6ed9eba1L
93 #define K_40_59 0x8f1bbcdcL
94 #define K_60_79 0xca62c1d6L
95 
96 #ifndef RUNTIME_ENDIAN
97 
98 #ifdef WORDS_BIGENDIAN
99 
100 #define BYTESWAP(x) (x)
101 #define BYTESWAP64(x) (x)
102 
103 #else /* WORDS_BIGENDIAN */
104 
105 #define BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | \
106 		     (ROTL((x), 8) & 0x00ff00ffL))
107 #define BYTESWAP64(x) _byteswap64(x)
108 
_byteswap64(uint64_t x)109 static inline uint64_t _byteswap64(uint64_t x)
110 {
111   uint32_t a = (uint32_t)(x >> 32);
112   uint32_t b = (uint32_t) x;
113   return ((uint64_t) BYTESWAP(b) << 32) | (uint64_t) BYTESWAP(a);
114 }
115 
116 #endif /* WORDS_BIGENDIAN */
117 
118 #else /* !RUNTIME_ENDIAN */
119 
120 #define BYTESWAP(x) _byteswap(sc->littleEndian, x)
121 #define BYTESWAP64(x) _byteswap64(sc->littleEndian, x)
122 
123 #define _BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | \
124 		      (ROTL((x), 8) & 0x00ff00ffL))
125 #define _BYTESWAP64(x) __byteswap64(x)
126 
__byteswap64(uint64_t x)127 static inline uint64_t __byteswap64(uint64_t x)
128 {
129   uint32_t a = x >> 32;
130   uint32_t b = (uint32_t) x;
131   return ((uint64_t) _BYTESWAP(b) << 32) | (uint64_t) _BYTESWAP(a);
132 }
133 
_byteswap(int littleEndian,uint32_t x)134 static inline uint32_t _byteswap(int littleEndian, uint32_t x)
135 {
136   if (!littleEndian)
137     return x;
138   else
139     return _BYTESWAP(x);
140 }
141 
_byteswap64(int littleEndian,uint64_t x)142 static inline uint64_t _byteswap64(int littleEndian, uint64_t x)
143 {
144   if (!littleEndian)
145     return x;
146   else
147     return _BYTESWAP64(x);
148 }
149 
setEndian(int * littleEndianp)150 static inline void setEndian(int *littleEndianp)
151 {
152   union {
153     uint32_t w;
154     uint8_t b[4];
155   } endian;
156 
157   endian.w = 1L;
158   *littleEndianp = endian.b[0] != 0;
159 }
160 
161 #endif /* !RUNTIME_ENDIAN */
162 
163 static const uint8_t padding[64] = {
164   0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
165   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
166   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
167   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
168   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
169   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
170   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
171   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
172 };
173 
174 void
SHA1Init(SHA1Context * sc)175 SHA1Init (SHA1Context *sc)
176 {
177 #ifdef RUNTIME_ENDIAN
178   setEndian (&sc->littleEndian);
179 #endif /* RUNTIME_ENDIAN */
180 
181   sc->totalLength = 0LL;
182   sc->hash[0] = 0x67452301L;
183   sc->hash[1] = 0xefcdab89L;
184   sc->hash[2] = 0x98badcfeL;
185   sc->hash[3] = 0x10325476L;
186   sc->hash[4] = 0xc3d2e1f0L;
187   sc->bufferLength = 0L;
188 }
189 
190 static void
burnStack(int size)191 burnStack (int size)
192 {
193   char buf[128];
194 
195   memset (buf, 0, sizeof (buf));
196   size -= sizeof (buf);
197   if (size > 0)
198     burnStack (size);
199 }
200 
201 static void
SHA1Guts(SHA1Context * sc,const uint32_t * cbuf)202 SHA1Guts (SHA1Context *sc, const uint32_t *cbuf)
203 {
204   uint32_t buf[80];
205   uint32_t *W, *W3, *W8, *W14, *W16;
206   uint32_t a, b, c, d, e, temp;
207   int i;
208 
209   W = buf;
210 
211   for (i = 15; i >= 0; i--) {
212     *(W++) = BYTESWAP(*cbuf);
213     cbuf++;
214   }
215 
216   W16 = &buf[0];
217   W14 = &buf[2];
218   W8 = &buf[8];
219   W3 = &buf[13];
220 
221   for (i = 63; i >= 0; i--) {
222     *W = *(W3++) ^ *(W8++) ^ *(W14++) ^ *(W16++);
223     *W = ROTL(*W, 1);
224     W++;
225   }
226 
227   a = sc->hash[0];
228   b = sc->hash[1];
229   c = sc->hash[2];
230   d = sc->hash[3];
231   e = sc->hash[4];
232 
233   W = buf;
234 
235 #ifndef SHA1_UNROLL
236 #define SHA1_UNROLL 20
237 #endif /* !SHA1_UNROLL */
238 
239 #if SHA1_UNROLL == 1
240   for (i = 19; i >= 0; i--)
241     DO_ROUND(F_0_19, K_0_19);
242 
243   for (i = 19; i >= 0; i--)
244     DO_ROUND(F_20_39, K_20_39);
245 
246   for (i = 19; i >= 0; i--)
247     DO_ROUND(F_40_59, K_40_59);
248 
249   for (i = 19; i >= 0; i--)
250     DO_ROUND(F_60_79, K_60_79);
251 #elif SHA1_UNROLL == 2
252   for (i = 9; i >= 0; i--) {
253     DO_ROUND(F_0_19, K_0_19);
254     DO_ROUND(F_0_19, K_0_19);
255   }
256 
257   for (i = 9; i >= 0; i--) {
258     DO_ROUND(F_20_39, K_20_39);
259     DO_ROUND(F_20_39, K_20_39);
260   }
261 
262   for (i = 9; i >= 0; i--) {
263     DO_ROUND(F_40_59, K_40_59);
264     DO_ROUND(F_40_59, K_40_59);
265   }
266 
267   for (i = 9; i >= 0; i--) {
268     DO_ROUND(F_60_79, K_60_79);
269     DO_ROUND(F_60_79, K_60_79);
270   }
271 #elif SHA1_UNROLL == 4
272   for (i = 4; i >= 0; i--) {
273     DO_ROUND(F_0_19, K_0_19);
274     DO_ROUND(F_0_19, K_0_19);
275     DO_ROUND(F_0_19, K_0_19);
276     DO_ROUND(F_0_19, K_0_19);
277   }
278 
279   for (i = 4; i >= 0; i--) {
280     DO_ROUND(F_20_39, K_20_39);
281     DO_ROUND(F_20_39, K_20_39);
282     DO_ROUND(F_20_39, K_20_39);
283     DO_ROUND(F_20_39, K_20_39);
284   }
285 
286   for (i = 4; i >= 0; i--) {
287     DO_ROUND(F_40_59, K_40_59);
288     DO_ROUND(F_40_59, K_40_59);
289     DO_ROUND(F_40_59, K_40_59);
290     DO_ROUND(F_40_59, K_40_59);
291   }
292 
293   for (i = 4; i >= 0; i--) {
294     DO_ROUND(F_60_79, K_60_79);
295     DO_ROUND(F_60_79, K_60_79);
296     DO_ROUND(F_60_79, K_60_79);
297     DO_ROUND(F_60_79, K_60_79);
298   }
299 #elif SHA1_UNROLL == 5
300   for (i = 3; i >= 0; i--) {
301     DO_ROUND(F_0_19, K_0_19);
302     DO_ROUND(F_0_19, K_0_19);
303     DO_ROUND(F_0_19, K_0_19);
304     DO_ROUND(F_0_19, K_0_19);
305     DO_ROUND(F_0_19, K_0_19);
306   }
307 
308   for (i = 3; i >= 0; i--) {
309     DO_ROUND(F_20_39, K_20_39);
310     DO_ROUND(F_20_39, K_20_39);
311     DO_ROUND(F_20_39, K_20_39);
312     DO_ROUND(F_20_39, K_20_39);
313     DO_ROUND(F_20_39, K_20_39);
314   }
315 
316   for (i = 3; i >= 0; i--) {
317     DO_ROUND(F_40_59, K_40_59);
318     DO_ROUND(F_40_59, K_40_59);
319     DO_ROUND(F_40_59, K_40_59);
320     DO_ROUND(F_40_59, K_40_59);
321     DO_ROUND(F_40_59, K_40_59);
322   }
323 
324   for (i = 3; i >= 0; i--) {
325     DO_ROUND(F_60_79, K_60_79);
326     DO_ROUND(F_60_79, K_60_79);
327     DO_ROUND(F_60_79, K_60_79);
328     DO_ROUND(F_60_79, K_60_79);
329     DO_ROUND(F_60_79, K_60_79);
330   }
331 #elif SHA1_UNROLL == 10
332   for (i = 1; i >= 0; i--) {
333     DO_ROUND(F_0_19, K_0_19);
334     DO_ROUND(F_0_19, K_0_19);
335     DO_ROUND(F_0_19, K_0_19);
336     DO_ROUND(F_0_19, K_0_19);
337     DO_ROUND(F_0_19, K_0_19);
338     DO_ROUND(F_0_19, K_0_19);
339     DO_ROUND(F_0_19, K_0_19);
340     DO_ROUND(F_0_19, K_0_19);
341     DO_ROUND(F_0_19, K_0_19);
342     DO_ROUND(F_0_19, K_0_19);
343   }
344 
345   for (i = 1; i >= 0; i--) {
346     DO_ROUND(F_20_39, K_20_39);
347     DO_ROUND(F_20_39, K_20_39);
348     DO_ROUND(F_20_39, K_20_39);
349     DO_ROUND(F_20_39, K_20_39);
350     DO_ROUND(F_20_39, K_20_39);
351     DO_ROUND(F_20_39, K_20_39);
352     DO_ROUND(F_20_39, K_20_39);
353     DO_ROUND(F_20_39, K_20_39);
354     DO_ROUND(F_20_39, K_20_39);
355     DO_ROUND(F_20_39, K_20_39);
356   }
357 
358   for (i = 1; i >= 0; i--) {
359     DO_ROUND(F_40_59, K_40_59);
360     DO_ROUND(F_40_59, K_40_59);
361     DO_ROUND(F_40_59, K_40_59);
362     DO_ROUND(F_40_59, K_40_59);
363     DO_ROUND(F_40_59, K_40_59);
364     DO_ROUND(F_40_59, K_40_59);
365     DO_ROUND(F_40_59, K_40_59);
366     DO_ROUND(F_40_59, K_40_59);
367     DO_ROUND(F_40_59, K_40_59);
368     DO_ROUND(F_40_59, K_40_59);
369   }
370 
371   for (i = 1; i >= 0; i--) {
372     DO_ROUND(F_60_79, K_60_79);
373     DO_ROUND(F_60_79, K_60_79);
374     DO_ROUND(F_60_79, K_60_79);
375     DO_ROUND(F_60_79, K_60_79);
376     DO_ROUND(F_60_79, K_60_79);
377     DO_ROUND(F_60_79, K_60_79);
378     DO_ROUND(F_60_79, K_60_79);
379     DO_ROUND(F_60_79, K_60_79);
380     DO_ROUND(F_60_79, K_60_79);
381     DO_ROUND(F_60_79, K_60_79);
382   }
383 #elif SHA1_UNROLL == 20
384   DO_ROUND(F_0_19, K_0_19);
385   DO_ROUND(F_0_19, K_0_19);
386   DO_ROUND(F_0_19, K_0_19);
387   DO_ROUND(F_0_19, K_0_19);
388   DO_ROUND(F_0_19, K_0_19);
389   DO_ROUND(F_0_19, K_0_19);
390   DO_ROUND(F_0_19, K_0_19);
391   DO_ROUND(F_0_19, K_0_19);
392   DO_ROUND(F_0_19, K_0_19);
393   DO_ROUND(F_0_19, K_0_19);
394   DO_ROUND(F_0_19, K_0_19);
395   DO_ROUND(F_0_19, K_0_19);
396   DO_ROUND(F_0_19, K_0_19);
397   DO_ROUND(F_0_19, K_0_19);
398   DO_ROUND(F_0_19, K_0_19);
399   DO_ROUND(F_0_19, K_0_19);
400   DO_ROUND(F_0_19, K_0_19);
401   DO_ROUND(F_0_19, K_0_19);
402   DO_ROUND(F_0_19, K_0_19);
403   DO_ROUND(F_0_19, K_0_19);
404 
405   DO_ROUND(F_20_39, K_20_39);
406   DO_ROUND(F_20_39, K_20_39);
407   DO_ROUND(F_20_39, K_20_39);
408   DO_ROUND(F_20_39, K_20_39);
409   DO_ROUND(F_20_39, K_20_39);
410   DO_ROUND(F_20_39, K_20_39);
411   DO_ROUND(F_20_39, K_20_39);
412   DO_ROUND(F_20_39, K_20_39);
413   DO_ROUND(F_20_39, K_20_39);
414   DO_ROUND(F_20_39, K_20_39);
415   DO_ROUND(F_20_39, K_20_39);
416   DO_ROUND(F_20_39, K_20_39);
417   DO_ROUND(F_20_39, K_20_39);
418   DO_ROUND(F_20_39, K_20_39);
419   DO_ROUND(F_20_39, K_20_39);
420   DO_ROUND(F_20_39, K_20_39);
421   DO_ROUND(F_20_39, K_20_39);
422   DO_ROUND(F_20_39, K_20_39);
423   DO_ROUND(F_20_39, K_20_39);
424   DO_ROUND(F_20_39, K_20_39);
425 
426   DO_ROUND(F_40_59, K_40_59);
427   DO_ROUND(F_40_59, K_40_59);
428   DO_ROUND(F_40_59, K_40_59);
429   DO_ROUND(F_40_59, K_40_59);
430   DO_ROUND(F_40_59, K_40_59);
431   DO_ROUND(F_40_59, K_40_59);
432   DO_ROUND(F_40_59, K_40_59);
433   DO_ROUND(F_40_59, K_40_59);
434   DO_ROUND(F_40_59, K_40_59);
435   DO_ROUND(F_40_59, K_40_59);
436   DO_ROUND(F_40_59, K_40_59);
437   DO_ROUND(F_40_59, K_40_59);
438   DO_ROUND(F_40_59, K_40_59);
439   DO_ROUND(F_40_59, K_40_59);
440   DO_ROUND(F_40_59, K_40_59);
441   DO_ROUND(F_40_59, K_40_59);
442   DO_ROUND(F_40_59, K_40_59);
443   DO_ROUND(F_40_59, K_40_59);
444   DO_ROUND(F_40_59, K_40_59);
445   DO_ROUND(F_40_59, K_40_59);
446 
447   DO_ROUND(F_60_79, K_60_79);
448   DO_ROUND(F_60_79, K_60_79);
449   DO_ROUND(F_60_79, K_60_79);
450   DO_ROUND(F_60_79, K_60_79);
451   DO_ROUND(F_60_79, K_60_79);
452   DO_ROUND(F_60_79, K_60_79);
453   DO_ROUND(F_60_79, K_60_79);
454   DO_ROUND(F_60_79, K_60_79);
455   DO_ROUND(F_60_79, K_60_79);
456   DO_ROUND(F_60_79, K_60_79);
457   DO_ROUND(F_60_79, K_60_79);
458   DO_ROUND(F_60_79, K_60_79);
459   DO_ROUND(F_60_79, K_60_79);
460   DO_ROUND(F_60_79, K_60_79);
461   DO_ROUND(F_60_79, K_60_79);
462   DO_ROUND(F_60_79, K_60_79);
463   DO_ROUND(F_60_79, K_60_79);
464   DO_ROUND(F_60_79, K_60_79);
465   DO_ROUND(F_60_79, K_60_79);
466   DO_ROUND(F_60_79, K_60_79);
467 #else /* SHA1_UNROLL */
468 #error SHA1_UNROLL must be 1, 2, 4, 5, 10 or 20!
469 #endif
470 
471   sc->hash[0] += a;
472   sc->hash[1] += b;
473   sc->hash[2] += c;
474   sc->hash[3] += d;
475   sc->hash[4] += e;
476 }
477 
478 void
SHA1Update(SHA1Context * sc,const void * vdata,uint32_t len)479 SHA1Update (SHA1Context *sc, const void *vdata, uint32_t len)
480 {
481   const uint8_t *data = vdata;
482   uint32_t bufferBytesLeft;
483   uint32_t bytesToCopy;
484   int needBurn = 0;
485 
486 #ifdef SHA1_FAST_COPY
487   if (sc->bufferLength) {
488     bufferBytesLeft = 64L - sc->bufferLength;
489 
490     bytesToCopy = bufferBytesLeft;
491     if (bytesToCopy > len)
492       bytesToCopy = len;
493 
494     memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy);
495 
496     sc->totalLength += bytesToCopy * 8L;
497 
498     sc->bufferLength += bytesToCopy;
499     data += bytesToCopy;
500     len -= bytesToCopy;
501 
502     if (sc->bufferLength == 64L) {
503       SHA1Guts (sc, sc->buffer.words);
504       needBurn = 1;
505       sc->bufferLength = 0L;
506     }
507   }
508 
509   while (len > 63) {
510     sc->totalLength += 512L;
511 
512     SHA1Guts (sc, data);
513     needBurn = 1;
514 
515     data += 64L;
516     len -= 64L;
517   }
518 
519   if (len) {
520     memcpy (&sc->buffer.bytes[sc->bufferLength], data, len);
521 
522     sc->totalLength += len * 8L;
523 
524     sc->bufferLength += len;
525   }
526 #else /* SHA1_FAST_COPY */
527   while (len) {
528     bufferBytesLeft = 64L - sc->bufferLength;
529 
530     bytesToCopy = bufferBytesLeft;
531     if (bytesToCopy > len)
532       bytesToCopy = len;
533 
534     memcpy (&sc->buffer.bytes[sc->bufferLength], data, bytesToCopy);
535 
536     sc->totalLength += bytesToCopy * 8L;
537 
538     sc->bufferLength += bytesToCopy;
539     data += bytesToCopy;
540     len -= bytesToCopy;
541 
542     if (sc->bufferLength == 64L) {
543       SHA1Guts (sc, sc->buffer.words);
544       needBurn = 1;
545       sc->bufferLength = 0L;
546     }
547   }
548 #endif /* SHA1_FAST_COPY */
549 
550   if (needBurn)
551     burnStack (sizeof (uint32_t[86]) + sizeof (uint32_t *[5]) + sizeof (int));
552 }
553 
554 void
SHA1Final(SHA1Context * sc,uint8_t hash[SHA1_HASH_SIZE])555 SHA1Final (SHA1Context *sc, uint8_t hash[SHA1_HASH_SIZE])
556 {
557   uint32_t bytesToPad;
558   uint64_t lengthPad;
559   int i;
560 
561   bytesToPad = 120L - sc->bufferLength;
562   if (bytesToPad > 64L)
563     bytesToPad -= 64L;
564 
565   lengthPad = BYTESWAP64(sc->totalLength);
566 
567   SHA1Update (sc, padding, bytesToPad);
568   SHA1Update (sc, &lengthPad, 8L);
569 
570   if (hash) {
571     for (i = 0; i < SHA1_HASH_WORDS; i++) {
572 #ifdef SHA1_FAST_COPY
573       *((uint32_t *) hash) = BYTESWAP(sc->hash[i]);
574 #else /* SHA1_FAST_COPY */
575       hash[0] = (uint8_t) (sc->hash[i] >> 24);
576       hash[1] = (uint8_t) (sc->hash[i] >> 16);
577       hash[2] = (uint8_t) (sc->hash[i] >> 8);
578       hash[3] = (uint8_t) sc->hash[i];
579 #endif /* SHA1_FAST_COPY */
580       hash += 4;
581     }
582   }
583 }
584 
585 #ifdef SHA1_TEST
586 
587 #include <stdio.h>
588 #include <stdlib.h>
589 #include <string.h>
590 
591 int
main(int argc,char * argv[])592 main (int argc, char *argv[])
593 {
594   SHA1Context foo;
595   uint8_t hash[SHA1_HASH_SIZE];
596   char buf[1000];
597   int i;
598 
599   SHA1Init (&foo);
600   SHA1Update (&foo, "abc", 3);
601   SHA1Final (&foo, hash);
602 
603   for (i = 0; i < SHA1_HASH_SIZE;) {
604     printf ("%02x", hash[i++]);
605     if (!(i % 4))
606       printf (" ");
607   }
608   printf ("\n");
609 
610   SHA1Init (&foo);
611   SHA1Update (&foo,
612 		"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
613 		56);
614   SHA1Final (&foo, hash);
615 
616   for (i = 0; i < SHA1_HASH_SIZE;) {
617     printf ("%02x", hash[i++]);
618     if (!(i % 4))
619       printf (" ");
620   }
621   printf ("\n");
622 
623   SHA1Init (&foo);
624   memset (buf, 'a', sizeof (buf));
625   for (i = 0; i < 1000; i++)
626     SHA1Update (&foo, buf, sizeof (buf));
627   SHA1Final (&foo, hash);
628 
629   for (i = 0; i < SHA1_HASH_SIZE;) {
630     printf ("%02x", hash[i++]);
631     if (!(i % 4))
632       printf (" ");
633   }
634   printf ("\n");
635 
636   exit (0);
637 }
638 
639 #endif /* SHA1_TEST */
640