2 * FIPS-180-1 compliant SHA-1 implementation
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 * The SHA-1 standard was published by NIST in 1993.
24 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
27 #if !defined(MBEDTLS_CONFIG_FILE)
28 #include "mbedtls/config.h"
30 #include MBEDTLS_CONFIG_FILE
33 #if defined(MBEDTLS_SHA1_C)
35 #include "mbedtls/sha1.h"
39 #if defined(MBEDTLS_SELF_TEST)
40 #if defined(MBEDTLS_PLATFORM_C)
41 #include "mbedtls/platform.h"
44 #define mbedtls_printf printf
45 #endif /* MBEDTLS_PLATFORM_C */
46 #endif /* MBEDTLS_SELF_TEST */
48 #if !defined(MBEDTLS_SHA1_ALT)
50 /* Implementation that should never be optimized out by the compiler */
51 static void mbedtls_zeroize( void *v, size_t n ) {
52 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
56 * 32-bit integer manipulation macros (big endian)
59 #define GET_UINT32_BE(n,b,i) \
61 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
62 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
63 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
64 | ( (uint32_t) (b)[(i) + 3] ); \
69 #define PUT_UINT32_BE(n,b,i) \
71 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
72 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
73 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
74 (b)[(i) + 3] = (unsigned char) ( (n) ); \
78 void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
80 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
83 void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
88 mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
91 void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
92 const mbedtls_sha1_context *src )
100 int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
105 ctx->state[0] = 0x67452301;
106 ctx->state[1] = 0xEFCDAB89;
107 ctx->state[2] = 0x98BADCFE;
108 ctx->state[3] = 0x10325476;
109 ctx->state[4] = 0xC3D2E1F0;
114 void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
116 mbedtls_sha1_starts_ret( ctx );
119 #if !defined(MBEDTLS_SHA1_PROCESS_ALT)
120 int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
121 const unsigned char data[64] )
123 uint32_t temp, W[16], A, B, C, D, E;
125 GET_UINT32_BE( W[ 0], data, 0 );
126 GET_UINT32_BE( W[ 1], data, 4 );
127 GET_UINT32_BE( W[ 2], data, 8 );
128 GET_UINT32_BE( W[ 3], data, 12 );
129 GET_UINT32_BE( W[ 4], data, 16 );
130 GET_UINT32_BE( W[ 5], data, 20 );
131 GET_UINT32_BE( W[ 6], data, 24 );
132 GET_UINT32_BE( W[ 7], data, 28 );
133 GET_UINT32_BE( W[ 8], data, 32 );
134 GET_UINT32_BE( W[ 9], data, 36 );
135 GET_UINT32_BE( W[10], data, 40 );
136 GET_UINT32_BE( W[11], data, 44 );
137 GET_UINT32_BE( W[12], data, 48 );
138 GET_UINT32_BE( W[13], data, 52 );
139 GET_UINT32_BE( W[14], data, 56 );
140 GET_UINT32_BE( W[15], data, 60 );
142 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
146 temp = W[( t - 3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \
147 W[( t - 14 ) & 0x0F] ^ W[ t & 0x0F], \
148 ( W[t & 0x0F] = S(temp,1) ) \
151 #define P(a,b,c,d,e,x) \
153 e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
162 #define F(x,y,z) (z ^ (x & (y ^ z)))
165 P( A, B, C, D, E, W[0] );
166 P( E, A, B, C, D, W[1] );
167 P( D, E, A, B, C, W[2] );
168 P( C, D, E, A, B, W[3] );
169 P( B, C, D, E, A, W[4] );
170 P( A, B, C, D, E, W[5] );
171 P( E, A, B, C, D, W[6] );
172 P( D, E, A, B, C, W[7] );
173 P( C, D, E, A, B, W[8] );
174 P( B, C, D, E, A, W[9] );
175 P( A, B, C, D, E, W[10] );
176 P( E, A, B, C, D, W[11] );
177 P( D, E, A, B, C, W[12] );
178 P( C, D, E, A, B, W[13] );
179 P( B, C, D, E, A, W[14] );
180 P( A, B, C, D, E, W[15] );
181 P( E, A, B, C, D, R(16) );
182 P( D, E, A, B, C, R(17) );
183 P( C, D, E, A, B, R(18) );
184 P( B, C, D, E, A, R(19) );
189 #define F(x,y,z) (x ^ y ^ z)
192 P( A, B, C, D, E, R(20) );
193 P( E, A, B, C, D, R(21) );
194 P( D, E, A, B, C, R(22) );
195 P( C, D, E, A, B, R(23) );
196 P( B, C, D, E, A, R(24) );
197 P( A, B, C, D, E, R(25) );
198 P( E, A, B, C, D, R(26) );
199 P( D, E, A, B, C, R(27) );
200 P( C, D, E, A, B, R(28) );
201 P( B, C, D, E, A, R(29) );
202 P( A, B, C, D, E, R(30) );
203 P( E, A, B, C, D, R(31) );
204 P( D, E, A, B, C, R(32) );
205 P( C, D, E, A, B, R(33) );
206 P( B, C, D, E, A, R(34) );
207 P( A, B, C, D, E, R(35) );
208 P( E, A, B, C, D, R(36) );
209 P( D, E, A, B, C, R(37) );
210 P( C, D, E, A, B, R(38) );
211 P( B, C, D, E, A, R(39) );
216 #define F(x,y,z) ((x & y) | (z & (x | y)))
219 P( A, B, C, D, E, R(40) );
220 P( E, A, B, C, D, R(41) );
221 P( D, E, A, B, C, R(42) );
222 P( C, D, E, A, B, R(43) );
223 P( B, C, D, E, A, R(44) );
224 P( A, B, C, D, E, R(45) );
225 P( E, A, B, C, D, R(46) );
226 P( D, E, A, B, C, R(47) );
227 P( C, D, E, A, B, R(48) );
228 P( B, C, D, E, A, R(49) );
229 P( A, B, C, D, E, R(50) );
230 P( E, A, B, C, D, R(51) );
231 P( D, E, A, B, C, R(52) );
232 P( C, D, E, A, B, R(53) );
233 P( B, C, D, E, A, R(54) );
234 P( A, B, C, D, E, R(55) );
235 P( E, A, B, C, D, R(56) );
236 P( D, E, A, B, C, R(57) );
237 P( C, D, E, A, B, R(58) );
238 P( B, C, D, E, A, R(59) );
243 #define F(x,y,z) (x ^ y ^ z)
246 P( A, B, C, D, E, R(60) );
247 P( E, A, B, C, D, R(61) );
248 P( D, E, A, B, C, R(62) );
249 P( C, D, E, A, B, R(63) );
250 P( B, C, D, E, A, R(64) );
251 P( A, B, C, D, E, R(65) );
252 P( E, A, B, C, D, R(66) );
253 P( D, E, A, B, C, R(67) );
254 P( C, D, E, A, B, R(68) );
255 P( B, C, D, E, A, R(69) );
256 P( A, B, C, D, E, R(70) );
257 P( E, A, B, C, D, R(71) );
258 P( D, E, A, B, C, R(72) );
259 P( C, D, E, A, B, R(73) );
260 P( B, C, D, E, A, R(74) );
261 P( A, B, C, D, E, R(75) );
262 P( E, A, B, C, D, R(76) );
263 P( D, E, A, B, C, R(77) );
264 P( C, D, E, A, B, R(78) );
265 P( B, C, D, E, A, R(79) );
279 void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
280 const unsigned char data[64] )
282 mbedtls_internal_sha1_process( ctx, data );
284 #endif /* !MBEDTLS_SHA1_PROCESS_ALT */
287 * SHA-1 process buffer
289 int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
290 const unsigned char *input,
300 left = ctx->total[0] & 0x3F;
303 ctx->total[0] += (uint32_t) ilen;
304 ctx->total[0] &= 0xFFFFFFFF;
306 if( ctx->total[0] < (uint32_t) ilen )
309 if( left && ilen >= fill )
311 memcpy( (void *) (ctx->buffer + left), input, fill );
313 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
323 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
331 memcpy( (void *) (ctx->buffer + left), input, ilen );
336 void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
337 const unsigned char *input,
340 mbedtls_sha1_update_ret( ctx, input, ilen );
346 int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
347 unsigned char output[20] )
354 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
356 used = ctx->total[0] & 0x3F;
358 ctx->buffer[used++] = 0x80;
362 /* Enough room for padding + length in current block */
363 memset( ctx->buffer + used, 0, 56 - used );
367 /* We'll need an extra block */
368 memset( ctx->buffer + used, 0, 64 - used );
370 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
373 memset( ctx->buffer, 0, 56 );
379 high = ( ctx->total[0] >> 29 )
380 | ( ctx->total[1] << 3 );
381 low = ( ctx->total[0] << 3 );
383 PUT_UINT32_BE( high, ctx->buffer, 56 );
384 PUT_UINT32_BE( low, ctx->buffer, 60 );
386 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
392 PUT_UINT32_BE( ctx->state[0], output, 0 );
393 PUT_UINT32_BE( ctx->state[1], output, 4 );
394 PUT_UINT32_BE( ctx->state[2], output, 8 );
395 PUT_UINT32_BE( ctx->state[3], output, 12 );
396 PUT_UINT32_BE( ctx->state[4], output, 16 );
401 void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
402 unsigned char output[20] )
404 mbedtls_sha1_finish_ret( ctx, output );
407 #endif /* !MBEDTLS_SHA1_ALT */
410 * output = SHA-1( input buffer )
412 int mbedtls_sha1_ret( const unsigned char *input,
414 unsigned char output[20] )
417 mbedtls_sha1_context ctx;
419 mbedtls_sha1_init( &ctx );
421 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
424 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
427 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
431 mbedtls_sha1_free( &ctx );
436 void mbedtls_sha1( const unsigned char *input,
438 unsigned char output[20] )
440 mbedtls_sha1_ret( input, ilen, output );
443 #if defined(MBEDTLS_SELF_TEST)
445 * FIPS-180-1 test vectors
447 static const unsigned char sha1_test_buf[3][57] =
450 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
454 static const size_t sha1_test_buflen[3] =
459 static const unsigned char sha1_test_sum[3][20] =
461 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
462 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
463 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
464 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
465 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
466 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
472 int mbedtls_sha1_self_test( int verbose )
474 int i, j, buflen, ret = 0;
475 unsigned char buf[1024];
476 unsigned char sha1sum[20];
477 mbedtls_sha1_context ctx;
479 mbedtls_sha1_init( &ctx );
484 for( i = 0; i < 3; i++ )
487 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
489 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
494 memset( buf, 'a', buflen = 1000 );
496 for( j = 0; j < 1000; j++ )
498 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
505 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
506 sha1_test_buflen[i] );
511 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
514 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
521 mbedtls_printf( "passed\n" );
525 mbedtls_printf( "\n" );
531 mbedtls_printf( "failed\n" );
534 mbedtls_sha1_free( &ctx );
539 #endif /* MBEDTLS_SELF_TEST */
541 #endif /* MBEDTLS_SHA1_C */