ff8036bd7d105acf89567f68126a7371fc22ecb8
[metze/wireshark/wip.git] / wsutil / sha1.c
1 /*
2  *  FIPS-180-1 compliant SHA-1 implementation
3  *
4  *  Copyright (C) 2001-2003  Christophe Devine
5  *  Copyright (C) 2012       Chris Elston, Katalix Systems Ltd <celston@katalix.com>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  *  Changed to use guint instead of uint 2004 by Anders Broman
22  *  Original code found at http://www.cr0.net:8040/code/crypto/sha1/
23  *  References: http://www.ietf.org/rfc/rfc3174.txt?number=3174
24  *
25  *  2012-08-21 - C Elston - Split sha1_hmac function to allow incremental usage.
26  */
27
28 #include <string.h>
29 #include <glib.h>
30
31 #include "sha1.h"
32 #include "file_util.h"
33
34 #define GET_UINT32(n,b,i)                       \
35 {                                               \
36     (n) = ( (guint32) (b)[(i)    ] << 24 )       \
37         | ( (guint32) (b)[(i) + 1] << 16 )       \
38         | ( (guint32) (b)[(i) + 2] <<  8 )       \
39         | ( (guint32) (b)[(i) + 3]       );      \
40 }
41
42 #define PUT_UINT32(n,b,i)                       \
43 {                                               \
44     (b)[(i)    ] = (guint8) ( (n) >> 24 );       \
45     (b)[(i) + 1] = (guint8) ( (n) >> 16 );       \
46     (b)[(i) + 2] = (guint8) ( (n) >>  8 );       \
47     (b)[(i) + 3] = (guint8) ( (n)       );       \
48 }
49
50 void sha1_starts( sha1_context *ctx )
51 {
52     ctx->total[0] = 0;
53     ctx->total[1] = 0;
54
55     ctx->state[0] = 0x67452301;
56     ctx->state[1] = 0xEFCDAB89;
57     ctx->state[2] = 0x98BADCFE;
58     ctx->state[3] = 0x10325476;
59     ctx->state[4] = 0xC3D2E1F0;
60 }
61
62 static void sha1_process( sha1_context *ctx, const guint8 data[64] )
63 {
64     guint32 temp, W[16], A, B, C, D, E;
65
66     GET_UINT32( W[0],  data,  0 );
67     GET_UINT32( W[1],  data,  4 );
68     GET_UINT32( W[2],  data,  8 );
69     GET_UINT32( W[3],  data, 12 );
70     GET_UINT32( W[4],  data, 16 );
71     GET_UINT32( W[5],  data, 20 );
72     GET_UINT32( W[6],  data, 24 );
73     GET_UINT32( W[7],  data, 28 );
74     GET_UINT32( W[8],  data, 32 );
75     GET_UINT32( W[9],  data, 36 );
76     GET_UINT32( W[10], data, 40 );
77     GET_UINT32( W[11], data, 44 );
78     GET_UINT32( W[12], data, 48 );
79     GET_UINT32( W[13], data, 52 );
80     GET_UINT32( W[14], data, 56 );
81     GET_UINT32( W[15], data, 60 );
82
83 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
84
85 #define R(t)                                            \
86 (                                                       \
87     temp = W[(t -  3) & 0x0F] ^ W[(t - 8) & 0x0F] ^     \
88            W[(t - 14) & 0x0F] ^ W[ t      & 0x0F],      \
89     ( W[t & 0x0F] = S(temp,1) )                         \
90 )
91
92 #define P(a,b,c,d,e,x)                                  \
93 {                                                       \
94     e += S(a,5) + F(b,c,d) + K + x; b = S(b,30);        \
95 }
96
97     A = ctx->state[0];
98     B = ctx->state[1];
99     C = ctx->state[2];
100     D = ctx->state[3];
101     E = ctx->state[4];
102
103 #define F(x,y,z) (z ^ (x & (y ^ z)))
104 #define K 0x5A827999
105
106     P( A, B, C, D, E, W[0]  );
107     P( E, A, B, C, D, W[1]  );
108     P( D, E, A, B, C, W[2]  );
109     P( C, D, E, A, B, W[3]  );
110     P( B, C, D, E, A, W[4]  );
111     P( A, B, C, D, E, W[5]  );
112     P( E, A, B, C, D, W[6]  );
113     P( D, E, A, B, C, W[7]  );
114     P( C, D, E, A, B, W[8]  );
115     P( B, C, D, E, A, W[9]  );
116     P( A, B, C, D, E, W[10] );
117     P( E, A, B, C, D, W[11] );
118     P( D, E, A, B, C, W[12] );
119     P( C, D, E, A, B, W[13] );
120     P( B, C, D, E, A, W[14] );
121     P( A, B, C, D, E, W[15] );
122     P( E, A, B, C, D, R(16) );
123     P( D, E, A, B, C, R(17) );
124     P( C, D, E, A, B, R(18) );
125     P( B, C, D, E, A, R(19) );
126
127 #undef K
128 #undef F
129
130 #define F(x,y,z) (x ^ y ^ z)
131 #define K 0x6ED9EBA1
132
133     P( A, B, C, D, E, R(20) );
134     P( E, A, B, C, D, R(21) );
135     P( D, E, A, B, C, R(22) );
136     P( C, D, E, A, B, R(23) );
137     P( B, C, D, E, A, R(24) );
138     P( A, B, C, D, E, R(25) );
139     P( E, A, B, C, D, R(26) );
140     P( D, E, A, B, C, R(27) );
141     P( C, D, E, A, B, R(28) );
142     P( B, C, D, E, A, R(29) );
143     P( A, B, C, D, E, R(30) );
144     P( E, A, B, C, D, R(31) );
145     P( D, E, A, B, C, R(32) );
146     P( C, D, E, A, B, R(33) );
147     P( B, C, D, E, A, R(34) );
148     P( A, B, C, D, E, R(35) );
149     P( E, A, B, C, D, R(36) );
150     P( D, E, A, B, C, R(37) );
151     P( C, D, E, A, B, R(38) );
152     P( B, C, D, E, A, R(39) );
153
154 #undef K
155 #undef F
156
157 #define F(x,y,z) ((x & y) | (z & (x | y)))
158 #define K 0x8F1BBCDC
159
160     P( A, B, C, D, E, R(40) );
161     P( E, A, B, C, D, R(41) );
162     P( D, E, A, B, C, R(42) );
163     P( C, D, E, A, B, R(43) );
164     P( B, C, D, E, A, R(44) );
165     P( A, B, C, D, E, R(45) );
166     P( E, A, B, C, D, R(46) );
167     P( D, E, A, B, C, R(47) );
168     P( C, D, E, A, B, R(48) );
169     P( B, C, D, E, A, R(49) );
170     P( A, B, C, D, E, R(50) );
171     P( E, A, B, C, D, R(51) );
172     P( D, E, A, B, C, R(52) );
173     P( C, D, E, A, B, R(53) );
174     P( B, C, D, E, A, R(54) );
175     P( A, B, C, D, E, R(55) );
176     P( E, A, B, C, D, R(56) );
177     P( D, E, A, B, C, R(57) );
178     P( C, D, E, A, B, R(58) );
179     P( B, C, D, E, A, R(59) );
180
181 #undef K
182 #undef F
183
184 #define F(x,y,z) (x ^ y ^ z)
185 #define K 0xCA62C1D6
186
187     P( A, B, C, D, E, R(60) );
188     P( E, A, B, C, D, R(61) );
189     P( D, E, A, B, C, R(62) );
190     P( C, D, E, A, B, R(63) );
191     P( B, C, D, E, A, R(64) );
192     P( A, B, C, D, E, R(65) );
193     P( E, A, B, C, D, R(66) );
194     P( D, E, A, B, C, R(67) );
195     P( C, D, E, A, B, R(68) );
196     P( B, C, D, E, A, R(69) );
197     P( A, B, C, D, E, R(70) );
198     P( E, A, B, C, D, R(71) );
199     P( D, E, A, B, C, R(72) );
200     P( C, D, E, A, B, R(73) );
201     P( B, C, D, E, A, R(74) );
202     P( A, B, C, D, E, R(75) );
203     P( E, A, B, C, D, R(76) );
204     P( D, E, A, B, C, R(77) );
205     P( C, D, E, A, B, R(78) );
206     P( B, C, D, E, A, R(79) );
207
208 #undef K
209 #undef F
210
211     ctx->state[0] += A;
212     ctx->state[1] += B;
213     ctx->state[2] += C;
214     ctx->state[3] += D;
215     ctx->state[4] += E;
216 }
217
218 void sha1_update( sha1_context *ctx, const guint8 *input, guint32 length )
219 {
220     guint32 left, fill;
221
222     if( ! length ) return;
223
224     left = ctx->total[0] & 0x3F;
225     fill = 64 - left;
226
227     ctx->total[0] += length;
228     ctx->total[0] &= 0xFFFFFFFF;
229
230     if( ctx->total[0] < length )
231         ctx->total[1]++;
232
233     if( left && length >= fill )
234     {
235         memcpy( (void *) (ctx->buffer + left),
236                 (const void *) input, fill );
237         sha1_process( ctx, ctx->buffer );
238         length -= fill;
239         input  += fill;
240         left = 0;
241     }
242
243     while( length >= 64 )
244     {
245         sha1_process( ctx, input );
246         length -= 64;
247         input  += 64;
248     }
249
250     if( length )
251     {
252         memcpy( (void *) (ctx->buffer + left),
253                 (const void *) input, length );
254     }
255 }
256
257 static guint8 sha1_padding[64] =
258 {
259     0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
260        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
261        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
262        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
263 };
264
265 void sha1_finish( sha1_context *ctx, guint8 digest[20] )
266 {
267     guint32 last, padn;
268     guint32 high, low;
269     guint8 msglen[8];
270
271     high = ( ctx->total[0] >> 29 )
272          | ( ctx->total[1] <<  3 );
273     low  = ( ctx->total[0] <<  3 );
274
275     PUT_UINT32( high, msglen, 0 );
276     PUT_UINT32( low,  msglen, 4 );
277
278     last = ctx->total[0] & 0x3F;
279     padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
280
281     sha1_update( ctx, sha1_padding, padn );
282     sha1_update( ctx, msglen, 8 );
283
284     PUT_UINT32( ctx->state[0], digest,  0 );
285     PUT_UINT32( ctx->state[1], digest,  4 );
286     PUT_UINT32( ctx->state[2], digest,  8 );
287     PUT_UINT32( ctx->state[3], digest, 12 );
288     PUT_UINT32( ctx->state[4], digest, 16 );
289 }
290
291 void sha1_hmac_starts( sha1_hmac_context *hctx, const guint8 *key, guint32 keylen )
292 {
293     guint32 i;
294     guint8 k_ipad[64];
295
296     memset( k_ipad, 0x36, 64 );
297     memset( hctx->k_opad, 0x5C, 64 );
298
299     for( i = 0; i < keylen; i++ )
300     {
301         if( i >= 64 ) break;
302
303         k_ipad[i] ^= key[i];
304         hctx->k_opad[i] ^= key[i];
305     }
306
307     sha1_starts( &hctx->ctx );
308     sha1_update( &hctx->ctx, k_ipad, 64 );
309 }
310
311 void sha1_hmac_update( sha1_hmac_context *hctx, const guint8 *buf, guint32 buflen )
312 {
313     sha1_update( &hctx->ctx, buf, buflen );
314 }
315
316 void sha1_hmac_finish( sha1_hmac_context *hctx, guint8 digest[20] )
317 {
318     guint8 tmpbuf[20];
319
320     sha1_finish( &hctx->ctx, tmpbuf );
321
322     sha1_starts( &hctx->ctx );
323     sha1_update( &hctx->ctx, hctx->k_opad, 64 );
324     sha1_update( &hctx->ctx, tmpbuf, 20 );
325     sha1_finish( &hctx->ctx, digest );
326 }
327
328 void sha1_hmac( const guint8 *key, guint32 keylen, const guint8 *buf, guint32 buflen,
329                 guint8 digest[20] )
330 {
331     sha1_hmac_context hctx;
332
333     sha1_hmac_starts( &hctx, key, keylen );
334     sha1_hmac_update( &hctx, buf, buflen );
335     sha1_hmac_finish( &hctx, digest );
336 }
337
338 #ifdef TEST
339
340 #include <stdlib.h>
341 #include <stdio.h>
342 #include <glib.h>
343 #include <errno.h>
344
345 /*
346  * those are the standard FIPS-180-1 test vectors
347  */
348
349 static const char *msg[] =
350 {
351     "abc",
352     "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
353     NULL
354 };
355
356 static const char *val[] =
357 {
358     "a9993e364706816aba3e25717850c26c9cd0d89d",
359     "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
360     "34aa973cd4c4daa4f61eeb2bdbad27316534016f"
361 };
362
363 int main( int argc, char *argv[] )
364 {
365     FILE *f;
366     int i, j;
367     char output[41];
368     sha1_context ctx;
369     unsigned char buf[1000];
370     unsigned char sha1sum[20];
371
372     if( argc < 2 )
373     {
374         printf( "\n SHA-1 Validation Tests:\n\n" );
375
376         for( i = 0; i < 3; i++ )
377         {
378             printf( " Test %d ", i + 1 );
379
380             sha1_starts( &ctx );
381
382             if( i < 2 )
383             {
384                 sha1_update( &ctx, (guint8 *) msg[i],
385                              strlen( msg[i] ) );
386             }
387             else
388             {
389                 memset( buf, 'a', 1000 );
390
391                 for( j = 0; j < 1000; j++ )
392                 {
393                     sha1_update( &ctx, (guint8 *) buf, 1000 );
394                 }
395             }
396
397             sha1_finish( &ctx, sha1sum );
398
399             for( j = 0; j < 20; j++ )
400             {
401                 g_snprintf( output + j * 2, 41-j*2, "%02x", sha1sum[j] );
402             }
403
404             if( memcmp( output, val[i], 40 ) )
405             {
406                 printf( "failed!\n" );
407                 return( 1 );
408             }
409
410             printf( "passed.\n" );
411         }
412
413         printf( "\n" );
414     }
415     else
416     {
417         if( ! ( f = ws_fopen( argv[1], "rb" ) ) )
418         {
419             printf("fopen: %s", g_strerror(errno));
420             return( 1 );
421         }
422
423         sha1_starts( &ctx );
424
425         while( ( i = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
426         {
427             sha1_update( &ctx, buf, i );
428         }
429
430         sha1_finish( &ctx, sha1sum );
431
432         for( j = 0; j < 20; j++ )
433         {
434             printf( "%02x", sha1sum[j] );
435         }
436
437         printf( "  %s\n", argv[1] );
438     }
439
440     return( 0 );
441 }
442
443 #endif
444
445
446 /*
447  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
448  *
449  * Local variables:
450  * c-basic-offset: 4
451  * tab-width: 8
452  * indent-tabs-mode: nil
453  * End:
454  *
455  * vi: set shiftwidth=4 tabstop=8 expandtab:
456  * :indentSize=4:tabSize=8:noTabs=true:
457  */