]> mj.ucw.cz Git - libucw.git/blob - ucw/sha1.c
Doc. system: Add type sections to definition list
[libucw.git] / ucw / sha1.c
1 /*
2  *      SHA-1 Hash Function (FIPS 180-1, RFC 3174)
3  *
4  *      Based on the code from libgcrypt-1.2.3, which is
5  *      Copyright (C) 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
6  *
7  *      Adaptation for libucw:
8  *      (c) 2008 Martin Mares <mj@ucw.cz>
9  *
10  *      This software may be freely distributed and used according to the terms
11  *      of the GNU Lesser General Public License.
12  */
13
14 #include "ucw/lib.h"
15 #include "ucw/sha1.h"
16 #include "ucw/unaligned.h"
17
18 #include <string.h>
19
20 void
21 sha1_init(sha1_context *hd)
22 {
23   hd->h0 = 0x67452301;
24   hd->h1 = 0xefcdab89;
25   hd->h2 = 0x98badcfe;
26   hd->h3 = 0x10325476;
27   hd->h4 = 0xc3d2e1f0;
28   hd->nblocks = 0;
29   hd->count = 0;
30 }
31
32 /*
33  * Transform the message X which consists of 16 32-bit-words
34  */
35 static void
36 transform(sha1_context *hd, const byte *data)
37 {
38   u32 a,b,c,d,e,tm;
39   u32 x[16];
40
41   /* Get values from the chaining vars. */
42   a = hd->h0;
43   b = hd->h1;
44   c = hd->h2;
45   d = hd->h3;
46   e = hd->h4;
47
48 #ifdef CPU_BIG_ENDIAN
49   memcpy( x, data, 64 );
50 #else
51   {
52     for (int i=0; i<16; i++)
53       x[i] = get_u32_be(data+4*i);
54   }
55 #endif
56
57
58 #define K1  0x5A827999L
59 #define K2  0x6ED9EBA1L
60 #define K3  0x8F1BBCDCL
61 #define K4  0xCA62C1D6L
62 #define F1(x,y,z)   ( z ^ ( x & ( y ^ z ) ) )
63 #define F2(x,y,z)   ( x ^ y ^ z )
64 #define F3(x,y,z)   ( ( x & y ) | ( z & ( x | y ) ) )
65 #define F4(x,y,z)   ( x ^ y ^ z )
66
67
68 #define M(i) ( tm =   x[i&0x0f] ^ x[(i-14)&0x0f] \
69                     ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f] \
70                , (x[i&0x0f] = ROL(tm, 1)) )
71
72 #define R(a,b,c,d,e,f,k,m)  do { e += ROL( a, 5 )     \
73                                       + f( b, c, d )  \
74                                       + k             \
75                                       + m;            \
76                                  b = ROL( b, 30 );    \
77                                } while(0)
78   R( a, b, c, d, e, F1, K1, x[ 0] );
79   R( e, a, b, c, d, F1, K1, x[ 1] );
80   R( d, e, a, b, c, F1, K1, x[ 2] );
81   R( c, d, e, a, b, F1, K1, x[ 3] );
82   R( b, c, d, e, a, F1, K1, x[ 4] );
83   R( a, b, c, d, e, F1, K1, x[ 5] );
84   R( e, a, b, c, d, F1, K1, x[ 6] );
85   R( d, e, a, b, c, F1, K1, x[ 7] );
86   R( c, d, e, a, b, F1, K1, x[ 8] );
87   R( b, c, d, e, a, F1, K1, x[ 9] );
88   R( a, b, c, d, e, F1, K1, x[10] );
89   R( e, a, b, c, d, F1, K1, x[11] );
90   R( d, e, a, b, c, F1, K1, x[12] );
91   R( c, d, e, a, b, F1, K1, x[13] );
92   R( b, c, d, e, a, F1, K1, x[14] );
93   R( a, b, c, d, e, F1, K1, x[15] );
94   R( e, a, b, c, d, F1, K1, M(16) );
95   R( d, e, a, b, c, F1, K1, M(17) );
96   R( c, d, e, a, b, F1, K1, M(18) );
97   R( b, c, d, e, a, F1, K1, M(19) );
98   R( a, b, c, d, e, F2, K2, M(20) );
99   R( e, a, b, c, d, F2, K2, M(21) );
100   R( d, e, a, b, c, F2, K2, M(22) );
101   R( c, d, e, a, b, F2, K2, M(23) );
102   R( b, c, d, e, a, F2, K2, M(24) );
103   R( a, b, c, d, e, F2, K2, M(25) );
104   R( e, a, b, c, d, F2, K2, M(26) );
105   R( d, e, a, b, c, F2, K2, M(27) );
106   R( c, d, e, a, b, F2, K2, M(28) );
107   R( b, c, d, e, a, F2, K2, M(29) );
108   R( a, b, c, d, e, F2, K2, M(30) );
109   R( e, a, b, c, d, F2, K2, M(31) );
110   R( d, e, a, b, c, F2, K2, M(32) );
111   R( c, d, e, a, b, F2, K2, M(33) );
112   R( b, c, d, e, a, F2, K2, M(34) );
113   R( a, b, c, d, e, F2, K2, M(35) );
114   R( e, a, b, c, d, F2, K2, M(36) );
115   R( d, e, a, b, c, F2, K2, M(37) );
116   R( c, d, e, a, b, F2, K2, M(38) );
117   R( b, c, d, e, a, F2, K2, M(39) );
118   R( a, b, c, d, e, F3, K3, M(40) );
119   R( e, a, b, c, d, F3, K3, M(41) );
120   R( d, e, a, b, c, F3, K3, M(42) );
121   R( c, d, e, a, b, F3, K3, M(43) );
122   R( b, c, d, e, a, F3, K3, M(44) );
123   R( a, b, c, d, e, F3, K3, M(45) );
124   R( e, a, b, c, d, F3, K3, M(46) );
125   R( d, e, a, b, c, F3, K3, M(47) );
126   R( c, d, e, a, b, F3, K3, M(48) );
127   R( b, c, d, e, a, F3, K3, M(49) );
128   R( a, b, c, d, e, F3, K3, M(50) );
129   R( e, a, b, c, d, F3, K3, M(51) );
130   R( d, e, a, b, c, F3, K3, M(52) );
131   R( c, d, e, a, b, F3, K3, M(53) );
132   R( b, c, d, e, a, F3, K3, M(54) );
133   R( a, b, c, d, e, F3, K3, M(55) );
134   R( e, a, b, c, d, F3, K3, M(56) );
135   R( d, e, a, b, c, F3, K3, M(57) );
136   R( c, d, e, a, b, F3, K3, M(58) );
137   R( b, c, d, e, a, F3, K3, M(59) );
138   R( a, b, c, d, e, F4, K4, M(60) );
139   R( e, a, b, c, d, F4, K4, M(61) );
140   R( d, e, a, b, c, F4, K4, M(62) );
141   R( c, d, e, a, b, F4, K4, M(63) );
142   R( b, c, d, e, a, F4, K4, M(64) );
143   R( a, b, c, d, e, F4, K4, M(65) );
144   R( e, a, b, c, d, F4, K4, M(66) );
145   R( d, e, a, b, c, F4, K4, M(67) );
146   R( c, d, e, a, b, F4, K4, M(68) );
147   R( b, c, d, e, a, F4, K4, M(69) );
148   R( a, b, c, d, e, F4, K4, M(70) );
149   R( e, a, b, c, d, F4, K4, M(71) );
150   R( d, e, a, b, c, F4, K4, M(72) );
151   R( c, d, e, a, b, F4, K4, M(73) );
152   R( b, c, d, e, a, F4, K4, M(74) );
153   R( a, b, c, d, e, F4, K4, M(75) );
154   R( e, a, b, c, d, F4, K4, M(76) );
155   R( d, e, a, b, c, F4, K4, M(77) );
156   R( c, d, e, a, b, F4, K4, M(78) );
157   R( b, c, d, e, a, F4, K4, M(79) );
158
159   /* Update chaining vars. */
160   hd->h0 += a;
161   hd->h1 += b;
162   hd->h2 += c;
163   hd->h3 += d;
164   hd->h4 += e;
165 }
166
167
168 /*
169  * Update the message digest with the contents
170  * of INBUF with length INLEN.
171  */
172 void
173 sha1_update(sha1_context *hd, const byte *inbuf, uns inlen)
174 {
175   if( hd->count == 64 )  /* flush the buffer */
176     {
177       transform( hd, hd->buf );
178       hd->count = 0;
179       hd->nblocks++;
180     }
181   if( !inbuf )
182     return;
183
184   if( hd->count )
185     {
186       for( ; inlen && hd->count < 64; inlen-- )
187         hd->buf[hd->count++] = *inbuf++;
188       sha1_update( hd, NULL, 0 );
189       if( !inlen )
190         return;
191     }
192
193   while( inlen >= 64 ) 
194     {
195       transform( hd, inbuf );
196       hd->count = 0;
197       hd->nblocks++;
198       inlen -= 64;
199       inbuf += 64;
200     }
201   for( ; inlen && hd->count < 64; inlen-- )
202     hd->buf[hd->count++] = *inbuf++;
203 }
204
205
206 /*
207  * The routine final terminates the computation and
208  * returns the digest.
209  * The handle is prepared for a new cycle, but adding bytes to the
210  * handle will the destroy the returned buffer.
211  * Returns: 20 bytes representing the digest.
212  */
213
214 byte *
215 sha1_final(sha1_context *hd)
216 {
217   u32 t, msb, lsb;
218   byte *p;
219
220   sha1_update(hd, NULL, 0); /* flush */;
221
222   t = hd->nblocks;
223   /* multiply by 64 to make a byte count */
224   lsb = t << 6;
225   msb = t >> 26;
226   /* add the count */
227   t = lsb;
228   if( (lsb += hd->count) < t )
229     msb++;
230   /* multiply by 8 to make a bit count */
231   t = lsb;
232   lsb <<= 3;
233   msb <<= 3;
234   msb |= t >> 29;
235
236   if( hd->count < 56 )  /* enough room */
237     {
238       hd->buf[hd->count++] = 0x80; /* pad */
239       while( hd->count < 56 )
240         hd->buf[hd->count++] = 0;  /* pad */
241     }
242   else  /* need one extra block */
243     {
244       hd->buf[hd->count++] = 0x80; /* pad character */
245       while( hd->count < 64 )
246         hd->buf[hd->count++] = 0;
247       sha1_update(hd, NULL, 0);  /* flush */;
248       memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
249     }
250   /* append the 64 bit count */
251   hd->buf[56] = msb >> 24;
252   hd->buf[57] = msb >> 16;
253   hd->buf[58] = msb >>  8;
254   hd->buf[59] = msb        ;
255   hd->buf[60] = lsb >> 24;
256   hd->buf[61] = lsb >> 16;
257   hd->buf[62] = lsb >>  8;
258   hd->buf[63] = lsb        ;
259   transform( hd, hd->buf );
260
261   p = hd->buf;
262 #define X(a) do { put_u32_be(p, hd->h##a); p += 4; } while(0)
263   X(0);
264   X(1);
265   X(2);
266   X(3);
267   X(4);
268 #undef X
269
270   return hd->buf;
271 }
272
273 /*
274  * Shortcut function which puts the hash value of the supplied buffer
275  * into outbuf which must have a size of 20 bytes.
276  */
277 void
278 sha1_hash_buffer(byte *outbuf, const byte *buffer, uns length)
279 {
280   sha1_context hd;
281
282   sha1_init(&hd);
283   sha1_update(&hd, buffer, length);
284   memcpy(outbuf, sha1_final(&hd), SHA1_SIZE);
285 }
286
287 #ifdef TEST
288
289 #include <stdio.h>
290 #include <unistd.h>
291 #include "ucw/string.h"
292
293 int main(void)
294 {
295   sha1_context hd;
296   byte buf[3];
297   int cnt;
298
299   sha1_init(&hd);
300   while ((cnt = read(0, buf, sizeof(buf))) > 0)
301     sha1_update(&hd, buf, cnt);
302
303   char text[SHA1_HEX_SIZE];
304   mem_to_hex(text, sha1_final(&hd), SHA1_SIZE, 0);
305   puts(text);
306
307   return 0;
308 }
309
310 #endif