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"
36 #include "mbedtls/platform_util.h"
40 #if defined(MBEDTLS_SELF_TEST)
41 #if defined(MBEDTLS_PLATFORM_C)
42 #include "mbedtls/platform.h"
45 #define mbedtls_printf printf
46 #endif /* MBEDTLS_PLATFORM_C */
47 #endif /* MBEDTLS_SELF_TEST */
49 #define SHA1_VALIDATE_RET(cond) \
50 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA )
52 #define SHA1_VALIDATE(cond) MBEDTLS_INTERNAL_VALIDATE( cond )
54 #if !defined(MBEDTLS_SHA1_ALT)
57 * 32-bit integer manipulation macros (big endian)
60 #define GET_UINT32_BE(n,b,i) \
62 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
63 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
64 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
65 | ( (uint32_t) (b)[(i) + 3] ); \
70 #define PUT_UINT32_BE(n,b,i) \
72 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
73 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
74 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
75 (b)[(i) + 3] = (unsigned char) ( (n) ); \
79 void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
81 SHA1_VALIDATE( ctx != NULL );
83 memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
86 void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
91 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
94 void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
95 const mbedtls_sha1_context *src )
97 SHA1_VALIDATE( dst != NULL );
98 SHA1_VALIDATE( src != NULL );
104 * SHA-1 context setup
106 int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
108 SHA1_VALIDATE_RET( ctx != NULL );
113 ctx->state[0] = 0x67452301;
114 ctx->state[1] = 0xEFCDAB89;
115 ctx->state[2] = 0x98BADCFE;
116 ctx->state[3] = 0x10325476;
117 ctx->state[4] = 0xC3D2E1F0;
122 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
123 void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
125 mbedtls_sha1_starts_ret( ctx );
129 #if !defined(MBEDTLS_SHA1_PROCESS_ALT)
130 int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
131 const unsigned char data[64] )
133 uint32_t temp, W[16], A, B, C, D, E;
135 SHA1_VALIDATE_RET( ctx != NULL );
136 SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
138 GET_UINT32_BE( W[ 0], data, 0 );
139 GET_UINT32_BE( W[ 1], data, 4 );
140 GET_UINT32_BE( W[ 2], data, 8 );
141 GET_UINT32_BE( W[ 3], data, 12 );
142 GET_UINT32_BE( W[ 4], data, 16 );
143 GET_UINT32_BE( W[ 5], data, 20 );
144 GET_UINT32_BE( W[ 6], data, 24 );
145 GET_UINT32_BE( W[ 7], data, 28 );
146 GET_UINT32_BE( W[ 8], data, 32 );
147 GET_UINT32_BE( W[ 9], data, 36 );
148 GET_UINT32_BE( W[10], data, 40 );
149 GET_UINT32_BE( W[11], data, 44 );
150 GET_UINT32_BE( W[12], data, 48 );
151 GET_UINT32_BE( W[13], data, 52 );
152 GET_UINT32_BE( W[14], data, 56 );
153 GET_UINT32_BE( W[15], data, 60 );
155 #define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
159 temp = W[( (t) - 3 ) & 0x0F] ^ W[( (t) - 8 ) & 0x0F] ^ \
160 W[( (t) - 14 ) & 0x0F] ^ W[ (t) & 0x0F], \
161 ( W[(t) & 0x0F] = S(temp,1) ) \
164 #define P(a,b,c,d,e,x) \
167 (e) += S((a),5) + F((b),(c),(d)) + K + (x); \
177 #define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
180 P( A, B, C, D, E, W[0] );
181 P( E, A, B, C, D, W[1] );
182 P( D, E, A, B, C, W[2] );
183 P( C, D, E, A, B, W[3] );
184 P( B, C, D, E, A, W[4] );
185 P( A, B, C, D, E, W[5] );
186 P( E, A, B, C, D, W[6] );
187 P( D, E, A, B, C, W[7] );
188 P( C, D, E, A, B, W[8] );
189 P( B, C, D, E, A, W[9] );
190 P( A, B, C, D, E, W[10] );
191 P( E, A, B, C, D, W[11] );
192 P( D, E, A, B, C, W[12] );
193 P( C, D, E, A, B, W[13] );
194 P( B, C, D, E, A, W[14] );
195 P( A, B, C, D, E, W[15] );
196 P( E, A, B, C, D, R(16) );
197 P( D, E, A, B, C, R(17) );
198 P( C, D, E, A, B, R(18) );
199 P( B, C, D, E, A, R(19) );
204 #define F(x,y,z) ((x) ^ (y) ^ (z))
207 P( A, B, C, D, E, R(20) );
208 P( E, A, B, C, D, R(21) );
209 P( D, E, A, B, C, R(22) );
210 P( C, D, E, A, B, R(23) );
211 P( B, C, D, E, A, R(24) );
212 P( A, B, C, D, E, R(25) );
213 P( E, A, B, C, D, R(26) );
214 P( D, E, A, B, C, R(27) );
215 P( C, D, E, A, B, R(28) );
216 P( B, C, D, E, A, R(29) );
217 P( A, B, C, D, E, R(30) );
218 P( E, A, B, C, D, R(31) );
219 P( D, E, A, B, C, R(32) );
220 P( C, D, E, A, B, R(33) );
221 P( B, C, D, E, A, R(34) );
222 P( A, B, C, D, E, R(35) );
223 P( E, A, B, C, D, R(36) );
224 P( D, E, A, B, C, R(37) );
225 P( C, D, E, A, B, R(38) );
226 P( B, C, D, E, A, R(39) );
231 #define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
234 P( A, B, C, D, E, R(40) );
235 P( E, A, B, C, D, R(41) );
236 P( D, E, A, B, C, R(42) );
237 P( C, D, E, A, B, R(43) );
238 P( B, C, D, E, A, R(44) );
239 P( A, B, C, D, E, R(45) );
240 P( E, A, B, C, D, R(46) );
241 P( D, E, A, B, C, R(47) );
242 P( C, D, E, A, B, R(48) );
243 P( B, C, D, E, A, R(49) );
244 P( A, B, C, D, E, R(50) );
245 P( E, A, B, C, D, R(51) );
246 P( D, E, A, B, C, R(52) );
247 P( C, D, E, A, B, R(53) );
248 P( B, C, D, E, A, R(54) );
249 P( A, B, C, D, E, R(55) );
250 P( E, A, B, C, D, R(56) );
251 P( D, E, A, B, C, R(57) );
252 P( C, D, E, A, B, R(58) );
253 P( B, C, D, E, A, R(59) );
258 #define F(x,y,z) ((x) ^ (y) ^ (z))
261 P( A, B, C, D, E, R(60) );
262 P( E, A, B, C, D, R(61) );
263 P( D, E, A, B, C, R(62) );
264 P( C, D, E, A, B, R(63) );
265 P( B, C, D, E, A, R(64) );
266 P( A, B, C, D, E, R(65) );
267 P( E, A, B, C, D, R(66) );
268 P( D, E, A, B, C, R(67) );
269 P( C, D, E, A, B, R(68) );
270 P( B, C, D, E, A, R(69) );
271 P( A, B, C, D, E, R(70) );
272 P( E, A, B, C, D, R(71) );
273 P( D, E, A, B, C, R(72) );
274 P( C, D, E, A, B, R(73) );
275 P( B, C, D, E, A, R(74) );
276 P( A, B, C, D, E, R(75) );
277 P( E, A, B, C, D, R(76) );
278 P( D, E, A, B, C, R(77) );
279 P( C, D, E, A, B, R(78) );
280 P( B, C, D, E, A, R(79) );
294 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
295 void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
296 const unsigned char data[64] )
298 mbedtls_internal_sha1_process( ctx, data );
301 #endif /* !MBEDTLS_SHA1_PROCESS_ALT */
304 * SHA-1 process buffer
306 int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
307 const unsigned char *input,
314 SHA1_VALIDATE_RET( ctx != NULL );
315 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
320 left = ctx->total[0] & 0x3F;
323 ctx->total[0] += (uint32_t) ilen;
324 ctx->total[0] &= 0xFFFFFFFF;
326 if( ctx->total[0] < (uint32_t) ilen )
329 if( left && ilen >= fill )
331 memcpy( (void *) (ctx->buffer + left), input, fill );
333 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
343 if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
351 memcpy( (void *) (ctx->buffer + left), input, ilen );
356 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
357 void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
358 const unsigned char *input,
361 mbedtls_sha1_update_ret( ctx, input, ilen );
368 int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
369 unsigned char output[20] )
375 SHA1_VALIDATE_RET( ctx != NULL );
376 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
379 * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
381 used = ctx->total[0] & 0x3F;
383 ctx->buffer[used++] = 0x80;
387 /* Enough room for padding + length in current block */
388 memset( ctx->buffer + used, 0, 56 - used );
392 /* We'll need an extra block */
393 memset( ctx->buffer + used, 0, 64 - used );
395 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
398 memset( ctx->buffer, 0, 56 );
404 high = ( ctx->total[0] >> 29 )
405 | ( ctx->total[1] << 3 );
406 low = ( ctx->total[0] << 3 );
408 PUT_UINT32_BE( high, ctx->buffer, 56 );
409 PUT_UINT32_BE( low, ctx->buffer, 60 );
411 if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
417 PUT_UINT32_BE( ctx->state[0], output, 0 );
418 PUT_UINT32_BE( ctx->state[1], output, 4 );
419 PUT_UINT32_BE( ctx->state[2], output, 8 );
420 PUT_UINT32_BE( ctx->state[3], output, 12 );
421 PUT_UINT32_BE( ctx->state[4], output, 16 );
426 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
427 void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
428 unsigned char output[20] )
430 mbedtls_sha1_finish_ret( ctx, output );
434 #endif /* !MBEDTLS_SHA1_ALT */
437 * output = SHA-1( input buffer )
439 int mbedtls_sha1_ret( const unsigned char *input,
441 unsigned char output[20] )
444 mbedtls_sha1_context ctx;
446 SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
447 SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
449 mbedtls_sha1_init( &ctx );
451 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
454 if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
457 if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
461 mbedtls_sha1_free( &ctx );
466 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
467 void mbedtls_sha1( const unsigned char *input,
469 unsigned char output[20] )
471 mbedtls_sha1_ret( input, ilen, output );
475 #if defined(MBEDTLS_SELF_TEST)
477 * FIPS-180-1 test vectors
479 static const unsigned char sha1_test_buf[3][57] =
482 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
486 static const size_t sha1_test_buflen[3] =
491 static const unsigned char sha1_test_sum[3][20] =
493 { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
494 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
495 { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
496 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
497 { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
498 0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
504 int mbedtls_sha1_self_test( int verbose )
506 int i, j, buflen, ret = 0;
507 unsigned char buf[1024];
508 unsigned char sha1sum[20];
509 mbedtls_sha1_context ctx;
511 mbedtls_sha1_init( &ctx );
516 for( i = 0; i < 3; i++ )
519 mbedtls_printf( " SHA-1 test #%d: ", i + 1 );
521 if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
526 memset( buf, 'a', buflen = 1000 );
528 for( j = 0; j < 1000; j++ )
530 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
537 ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
538 sha1_test_buflen[i] );
543 if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
546 if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
553 mbedtls_printf( "passed\n" );
557 mbedtls_printf( "\n" );
563 mbedtls_printf( "failed\n" );
566 mbedtls_sha1_free( &ctx );
571 #endif /* MBEDTLS_SELF_TEST */
573 #endif /* MBEDTLS_SHA1_C */