2 * FIPS-180-1 compliant SHA-1 implementation
4 * Copyright (C) 2006-2010, Brainspark B.V.
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * The SHA-1 standard was published by NIST in 1993.
28 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
31 /* $Cambridge: exim/src/src/pdkim/sha1.c,v 1.3 2009/12/07 13:05:07 tom Exp $ */
39 * 32-bit integer manipulation macros (big endian)
42 #define GET_ULONG_BE(n,b,i) \
44 (n) = ( (unsigned long) (b)[(i) ] << 24 ) \
45 | ( (unsigned long) (b)[(i) + 1] << 16 ) \
46 | ( (unsigned long) (b)[(i) + 2] << 8 ) \
47 | ( (unsigned long) (b)[(i) + 3] ); \
52 #define PUT_ULONG_BE(n,b,i) \
54 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
55 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
56 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
57 (b)[(i) + 3] = (unsigned char) ( (n) ); \
64 void sha1_starts( sha1_context *ctx )
69 ctx->state[0] = 0x67452301;
70 ctx->state[1] = 0xEFCDAB89;
71 ctx->state[2] = 0x98BADCFE;
72 ctx->state[3] = 0x10325476;
73 ctx->state[4] = 0xC3D2E1F0;
76 static void sha1_process( sha1_context *ctx, const unsigned char data[64] )
78 unsigned long temp, W[16], A, B, C, D, E;
80 GET_ULONG_BE( W[ 0], data, 0 );
81 GET_ULONG_BE( W[ 1], data, 4 );
82 GET_ULONG_BE( W[ 2], data, 8 );
83 GET_ULONG_BE( W[ 3], data, 12 );
84 GET_ULONG_BE( W[ 4], data, 16 );
85 GET_ULONG_BE( W[ 5], data, 20 );
86 GET_ULONG_BE( W[ 6], data, 24 );
87 GET_ULONG_BE( W[ 7], data, 28 );
88 GET_ULONG_BE( W[ 8], data, 32 );
89 GET_ULONG_BE( W[ 9], data, 36 );
90 GET_ULONG_BE( W[10], data, 40 );
91 GET_ULONG_BE( W[11], data, 44 );
92 GET_ULONG_BE( W[12], data, 48 );
93 GET_ULONG_BE( W[13], data, 52 );
94 GET_ULONG_BE( W[14], data, 56 );
95 GET_ULONG_BE( W[15], data, 60 );
97 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
101 temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ \
102 W[(t - 14) & 0x0F] ^ W[ t & 0x0F], \
103 ( W[t & 0x0F] = S(temp,1) ) \
106 #define P(a,b,c,d,e,x) \
108 e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
117 #define F(x,y,z) (z ^ (x & (y ^ z)))
120 P( A, B, C, D, E, W[0] );
121 P( E, A, B, C, D, W[1] );
122 P( D, E, A, B, C, W[2] );
123 P( C, D, E, A, B, W[3] );
124 P( B, C, D, E, A, W[4] );
125 P( A, B, C, D, E, W[5] );
126 P( E, A, B, C, D, W[6] );
127 P( D, E, A, B, C, W[7] );
128 P( C, D, E, A, B, W[8] );
129 P( B, C, D, E, A, W[9] );
130 P( A, B, C, D, E, W[10] );
131 P( E, A, B, C, D, W[11] );
132 P( D, E, A, B, C, W[12] );
133 P( C, D, E, A, B, W[13] );
134 P( B, C, D, E, A, W[14] );
135 P( A, B, C, D, E, W[15] );
136 P( E, A, B, C, D, R(16) );
137 P( D, E, A, B, C, R(17) );
138 P( C, D, E, A, B, R(18) );
139 P( B, C, D, E, A, R(19) );
144 #define F(x,y,z) (x ^ y ^ z)
147 P( A, B, C, D, E, R(20) );
148 P( E, A, B, C, D, R(21) );
149 P( D, E, A, B, C, R(22) );
150 P( C, D, E, A, B, R(23) );
151 P( B, C, D, E, A, R(24) );
152 P( A, B, C, D, E, R(25) );
153 P( E, A, B, C, D, R(26) );
154 P( D, E, A, B, C, R(27) );
155 P( C, D, E, A, B, R(28) );
156 P( B, C, D, E, A, R(29) );
157 P( A, B, C, D, E, R(30) );
158 P( E, A, B, C, D, R(31) );
159 P( D, E, A, B, C, R(32) );
160 P( C, D, E, A, B, R(33) );
161 P( B, C, D, E, A, R(34) );
162 P( A, B, C, D, E, R(35) );
163 P( E, A, B, C, D, R(36) );
164 P( D, E, A, B, C, R(37) );
165 P( C, D, E, A, B, R(38) );
166 P( B, C, D, E, A, R(39) );
171 #define F(x,y,z) ((x & y) | (z & (x | y)))
174 P( A, B, C, D, E, R(40) );
175 P( E, A, B, C, D, R(41) );
176 P( D, E, A, B, C, R(42) );
177 P( C, D, E, A, B, R(43) );
178 P( B, C, D, E, A, R(44) );
179 P( A, B, C, D, E, R(45) );
180 P( E, A, B, C, D, R(46) );
181 P( D, E, A, B, C, R(47) );
182 P( C, D, E, A, B, R(48) );
183 P( B, C, D, E, A, R(49) );
184 P( A, B, C, D, E, R(50) );
185 P( E, A, B, C, D, R(51) );
186 P( D, E, A, B, C, R(52) );
187 P( C, D, E, A, B, R(53) );
188 P( B, C, D, E, A, R(54) );
189 P( A, B, C, D, E, R(55) );
190 P( E, A, B, C, D, R(56) );
191 P( D, E, A, B, C, R(57) );
192 P( C, D, E, A, B, R(58) );
193 P( B, C, D, E, A, R(59) );
198 #define F(x,y,z) (x ^ y ^ z)
201 P( A, B, C, D, E, R(60) );
202 P( E, A, B, C, D, R(61) );
203 P( D, E, A, B, C, R(62) );
204 P( C, D, E, A, B, R(63) );
205 P( B, C, D, E, A, R(64) );
206 P( A, B, C, D, E, R(65) );
207 P( E, A, B, C, D, R(66) );
208 P( D, E, A, B, C, R(67) );
209 P( C, D, E, A, B, R(68) );
210 P( B, C, D, E, A, R(69) );
211 P( A, B, C, D, E, R(70) );
212 P( E, A, B, C, D, R(71) );
213 P( D, E, A, B, C, R(72) );
214 P( C, D, E, A, B, R(73) );
215 P( B, C, D, E, A, R(74) );
216 P( A, B, C, D, E, R(75) );
217 P( E, A, B, C, D, R(76) );
218 P( D, E, A, B, C, R(77) );
219 P( C, D, E, A, B, R(78) );
220 P( B, C, D, E, A, R(79) );
233 * SHA-1 process buffer
235 void sha1_update( sha1_context *ctx, const unsigned char *input, int ilen )
243 left = ctx->total[0] & 0x3F;
246 ctx->total[0] += ilen;
247 ctx->total[0] &= 0xFFFFFFFF;
249 if( ctx->total[0] < (unsigned long) ilen )
252 if( left && ilen >= fill )
254 memcpy( (void *) (ctx->buffer + left),
255 (void *) input, fill );
256 sha1_process( ctx, ctx->buffer );
264 sha1_process( ctx, input );
271 memcpy( (void *) (ctx->buffer + left),
272 (void *) input, ilen );
276 static const unsigned char sha1_padding[64] =
278 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
279 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
280 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
287 void sha1_finish( sha1_context *ctx, unsigned char output[20] )
289 unsigned long last, padn;
290 unsigned long high, low;
291 unsigned char msglen[8];
293 high = ( ctx->total[0] >> 29 )
294 | ( ctx->total[1] << 3 );
295 low = ( ctx->total[0] << 3 );
297 PUT_ULONG_BE( high, msglen, 0 );
298 PUT_ULONG_BE( low, msglen, 4 );
300 last = ctx->total[0] & 0x3F;
301 padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
303 sha1_update( ctx, (unsigned char *) sha1_padding, padn );
304 sha1_update( ctx, msglen, 8 );
306 PUT_ULONG_BE( ctx->state[0], output, 0 );
307 PUT_ULONG_BE( ctx->state[1], output, 4 );
308 PUT_ULONG_BE( ctx->state[2], output, 8 );
309 PUT_ULONG_BE( ctx->state[3], output, 12 );
310 PUT_ULONG_BE( ctx->state[4], output, 16 );
314 * output = SHA-1( input buffer )
316 void sha1( const unsigned char *input, int ilen, unsigned char output[20] )
321 sha1_update( &ctx, input, ilen );
322 sha1_finish( &ctx, output );
324 memset( &ctx, 0, sizeof( sha1_context ) );
328 * output = SHA-1( file contents )
330 int sha1_file( const char *path, unsigned char output[20] )
335 unsigned char buf[1024];
337 if( ( f = fopen( path, "rb" ) ) == NULL )
342 while( ( n = fread( buf, 1, sizeof( buf ), f ) ) > 0 )
343 sha1_update( &ctx, buf, (int) n );
345 sha1_finish( &ctx, output );
347 memset( &ctx, 0, sizeof( sha1_context ) );
349 if( ferror( f ) != 0 )
360 * SHA-1 HMAC context setup
362 void sha1_hmac_starts( sha1_context *ctx, const unsigned char *key, int keylen )
365 unsigned char sum[20];
369 sha1( key, keylen, sum );
374 memset( ctx->ipad, 0x36, 64 );
375 memset( ctx->opad, 0x5C, 64 );
377 for( i = 0; i < keylen; i++ )
379 ctx->ipad[i] = (unsigned char)( ctx->ipad[i] ^ key[i] );
380 ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
384 sha1_update( ctx, ctx->ipad, 64 );
386 memset( sum, 0, sizeof( sum ) );
390 * SHA-1 HMAC process buffer
392 void sha1_hmac_update( sha1_context *ctx, const unsigned char *input, int ilen )
394 sha1_update( ctx, input, ilen );
398 * SHA-1 HMAC final digest
400 void sha1_hmac_finish( sha1_context *ctx, unsigned char output[20] )
402 unsigned char tmpbuf[20];
404 sha1_finish( ctx, tmpbuf );
406 sha1_update( ctx, ctx->opad, 64 );
407 sha1_update( ctx, tmpbuf, 20 );
408 sha1_finish( ctx, output );
410 memset( tmpbuf, 0, sizeof( tmpbuf ) );
414 * SHA1 HMAC context reset
416 void sha1_hmac_reset( sha1_context *ctx )
419 sha1_update( ctx, ctx->ipad, 64 );
423 * output = HMAC-SHA-1( hmac key, input buffer )
425 void sha1_hmac( const unsigned char *key, int keylen,
426 const unsigned char *input, int ilen,
427 unsigned char output[20] )
431 sha1_hmac_starts( &ctx, key, keylen );
432 sha1_hmac_update( &ctx, input, ilen );
433 sha1_hmac_finish( &ctx, output );
435 memset( &ctx, 0, sizeof( sha1_context ) );