• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  FIPS-180-1 compliant SHA-1 implementation
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0
6  *
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
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19 /*
20  *  The SHA-1 standard was published by NIST in 1993.
21  *
22  *  http://www.itl.nist.gov/fipspubs/fip180-1.htm
23  */
24 
25 #include "common.h"
26 
27 #if defined(MBEDTLS_SHA1_C)
28 
29 #include "mbedtls/sha1.h"
30 #include "mbedtls/platform_util.h"
31 #include "mbedtls/error.h"
32 
33 #include <string.h>
34 
35 #if defined(MBEDTLS_SELF_TEST)
36 #if defined(MBEDTLS_PLATFORM_C)
37 #include "mbedtls/platform.h"
38 #else
39 #include <stdio.h>
40 #define mbedtls_printf printf
41 #endif /* MBEDTLS_PLATFORM_C */
42 #endif /* MBEDTLS_SELF_TEST */
43 
44 #define SHA1_VALIDATE_RET(cond)                             \
45     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA )
46 
47 #define SHA1_VALIDATE(cond)  MBEDTLS_INTERNAL_VALIDATE( cond )
48 
49 #if !defined(MBEDTLS_SHA1_ALT)
50 
mbedtls_sha1_init(mbedtls_sha1_context * ctx)51 void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
52 {
53     SHA1_VALIDATE( ctx != NULL );
54 
55     memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
56 }
57 
mbedtls_sha1_free(mbedtls_sha1_context * ctx)58 void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
59 {
60     if( ctx == NULL )
61         return;
62 
63     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
64 }
65 
mbedtls_sha1_clone(mbedtls_sha1_context * dst,const mbedtls_sha1_context * src)66 void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
67                          const mbedtls_sha1_context *src )
68 {
69     SHA1_VALIDATE( dst != NULL );
70     SHA1_VALIDATE( src != NULL );
71 
72     *dst = *src;
73 }
74 
75 /*
76  * SHA-1 context setup
77  */
mbedtls_sha1_starts(mbedtls_sha1_context * ctx)78 int mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
79 {
80     SHA1_VALIDATE_RET( ctx != NULL );
81 
82     ctx->total[0] = 0;
83     ctx->total[1] = 0;
84 
85     ctx->state[0] = 0x67452301;
86     ctx->state[1] = 0xEFCDAB89;
87     ctx->state[2] = 0x98BADCFE;
88     ctx->state[3] = 0x10325476;
89     ctx->state[4] = 0xC3D2E1F0;
90 
91     return( 0 );
92 }
93 
94 #if !defined(MBEDTLS_SHA1_PROCESS_ALT)
mbedtls_internal_sha1_process(mbedtls_sha1_context * ctx,const unsigned char data[64])95 int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
96                                    const unsigned char data[64] )
97 {
98     struct
99     {
100         uint32_t temp, W[16], A, B, C, D, E;
101     } local;
102 
103     SHA1_VALIDATE_RET( ctx != NULL );
104     SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
105 
106     local.W[ 0] = MBEDTLS_GET_UINT32_BE( data,  0 );
107     local.W[ 1] = MBEDTLS_GET_UINT32_BE( data,  4 );
108     local.W[ 2] = MBEDTLS_GET_UINT32_BE( data,  8 );
109     local.W[ 3] = MBEDTLS_GET_UINT32_BE( data, 12 );
110     local.W[ 4] = MBEDTLS_GET_UINT32_BE( data, 16 );
111     local.W[ 5] = MBEDTLS_GET_UINT32_BE( data, 20 );
112     local.W[ 6] = MBEDTLS_GET_UINT32_BE( data, 24 );
113     local.W[ 7] = MBEDTLS_GET_UINT32_BE( data, 28 );
114     local.W[ 8] = MBEDTLS_GET_UINT32_BE( data, 32 );
115     local.W[ 9] = MBEDTLS_GET_UINT32_BE( data, 36 );
116     local.W[10] = MBEDTLS_GET_UINT32_BE( data, 40 );
117     local.W[11] = MBEDTLS_GET_UINT32_BE( data, 44 );
118     local.W[12] = MBEDTLS_GET_UINT32_BE( data, 48 );
119     local.W[13] = MBEDTLS_GET_UINT32_BE( data, 52 );
120     local.W[14] = MBEDTLS_GET_UINT32_BE( data, 56 );
121     local.W[15] = MBEDTLS_GET_UINT32_BE( data, 60 );
122 
123 #define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
124 
125 #define R(t)                                                    \
126     (                                                           \
127         local.temp = local.W[( (t) -  3 ) & 0x0F] ^             \
128                      local.W[( (t) -  8 ) & 0x0F] ^             \
129                      local.W[( (t) - 14 ) & 0x0F] ^             \
130                      local.W[  (t)        & 0x0F],              \
131         ( local.W[(t) & 0x0F] = S(local.temp,1) )               \
132     )
133 
134 #define P(a,b,c,d,e,x)                                          \
135     do                                                          \
136     {                                                           \
137         (e) += S((a),5) + F((b),(c),(d)) + K + (x);             \
138         (b) = S((b),30);                                        \
139     } while( 0 )
140 
141     local.A = ctx->state[0];
142     local.B = ctx->state[1];
143     local.C = ctx->state[2];
144     local.D = ctx->state[3];
145     local.E = ctx->state[4];
146 
147 #define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
148 #define K 0x5A827999
149 
150     P( local.A, local.B, local.C, local.D, local.E, local.W[0]  );
151     P( local.E, local.A, local.B, local.C, local.D, local.W[1]  );
152     P( local.D, local.E, local.A, local.B, local.C, local.W[2]  );
153     P( local.C, local.D, local.E, local.A, local.B, local.W[3]  );
154     P( local.B, local.C, local.D, local.E, local.A, local.W[4]  );
155     P( local.A, local.B, local.C, local.D, local.E, local.W[5]  );
156     P( local.E, local.A, local.B, local.C, local.D, local.W[6]  );
157     P( local.D, local.E, local.A, local.B, local.C, local.W[7]  );
158     P( local.C, local.D, local.E, local.A, local.B, local.W[8]  );
159     P( local.B, local.C, local.D, local.E, local.A, local.W[9]  );
160     P( local.A, local.B, local.C, local.D, local.E, local.W[10] );
161     P( local.E, local.A, local.B, local.C, local.D, local.W[11] );
162     P( local.D, local.E, local.A, local.B, local.C, local.W[12] );
163     P( local.C, local.D, local.E, local.A, local.B, local.W[13] );
164     P( local.B, local.C, local.D, local.E, local.A, local.W[14] );
165     P( local.A, local.B, local.C, local.D, local.E, local.W[15] );
166     P( local.E, local.A, local.B, local.C, local.D, R(16) );
167     P( local.D, local.E, local.A, local.B, local.C, R(17) );
168     P( local.C, local.D, local.E, local.A, local.B, R(18) );
169     P( local.B, local.C, local.D, local.E, local.A, R(19) );
170 
171 #undef K
172 #undef F
173 
174 #define F(x,y,z) ((x) ^ (y) ^ (z))
175 #define K 0x6ED9EBA1
176 
177     P( local.A, local.B, local.C, local.D, local.E, R(20) );
178     P( local.E, local.A, local.B, local.C, local.D, R(21) );
179     P( local.D, local.E, local.A, local.B, local.C, R(22) );
180     P( local.C, local.D, local.E, local.A, local.B, R(23) );
181     P( local.B, local.C, local.D, local.E, local.A, R(24) );
182     P( local.A, local.B, local.C, local.D, local.E, R(25) );
183     P( local.E, local.A, local.B, local.C, local.D, R(26) );
184     P( local.D, local.E, local.A, local.B, local.C, R(27) );
185     P( local.C, local.D, local.E, local.A, local.B, R(28) );
186     P( local.B, local.C, local.D, local.E, local.A, R(29) );
187     P( local.A, local.B, local.C, local.D, local.E, R(30) );
188     P( local.E, local.A, local.B, local.C, local.D, R(31) );
189     P( local.D, local.E, local.A, local.B, local.C, R(32) );
190     P( local.C, local.D, local.E, local.A, local.B, R(33) );
191     P( local.B, local.C, local.D, local.E, local.A, R(34) );
192     P( local.A, local.B, local.C, local.D, local.E, R(35) );
193     P( local.E, local.A, local.B, local.C, local.D, R(36) );
194     P( local.D, local.E, local.A, local.B, local.C, R(37) );
195     P( local.C, local.D, local.E, local.A, local.B, R(38) );
196     P( local.B, local.C, local.D, local.E, local.A, R(39) );
197 
198 #undef K
199 #undef F
200 
201 #define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
202 #define K 0x8F1BBCDC
203 
204     P( local.A, local.B, local.C, local.D, local.E, R(40) );
205     P( local.E, local.A, local.B, local.C, local.D, R(41) );
206     P( local.D, local.E, local.A, local.B, local.C, R(42) );
207     P( local.C, local.D, local.E, local.A, local.B, R(43) );
208     P( local.B, local.C, local.D, local.E, local.A, R(44) );
209     P( local.A, local.B, local.C, local.D, local.E, R(45) );
210     P( local.E, local.A, local.B, local.C, local.D, R(46) );
211     P( local.D, local.E, local.A, local.B, local.C, R(47) );
212     P( local.C, local.D, local.E, local.A, local.B, R(48) );
213     P( local.B, local.C, local.D, local.E, local.A, R(49) );
214     P( local.A, local.B, local.C, local.D, local.E, R(50) );
215     P( local.E, local.A, local.B, local.C, local.D, R(51) );
216     P( local.D, local.E, local.A, local.B, local.C, R(52) );
217     P( local.C, local.D, local.E, local.A, local.B, R(53) );
218     P( local.B, local.C, local.D, local.E, local.A, R(54) );
219     P( local.A, local.B, local.C, local.D, local.E, R(55) );
220     P( local.E, local.A, local.B, local.C, local.D, R(56) );
221     P( local.D, local.E, local.A, local.B, local.C, R(57) );
222     P( local.C, local.D, local.E, local.A, local.B, R(58) );
223     P( local.B, local.C, local.D, local.E, local.A, R(59) );
224 
225 #undef K
226 #undef F
227 
228 #define F(x,y,z) ((x) ^ (y) ^ (z))
229 #define K 0xCA62C1D6
230 
231     P( local.A, local.B, local.C, local.D, local.E, R(60) );
232     P( local.E, local.A, local.B, local.C, local.D, R(61) );
233     P( local.D, local.E, local.A, local.B, local.C, R(62) );
234     P( local.C, local.D, local.E, local.A, local.B, R(63) );
235     P( local.B, local.C, local.D, local.E, local.A, R(64) );
236     P( local.A, local.B, local.C, local.D, local.E, R(65) );
237     P( local.E, local.A, local.B, local.C, local.D, R(66) );
238     P( local.D, local.E, local.A, local.B, local.C, R(67) );
239     P( local.C, local.D, local.E, local.A, local.B, R(68) );
240     P( local.B, local.C, local.D, local.E, local.A, R(69) );
241     P( local.A, local.B, local.C, local.D, local.E, R(70) );
242     P( local.E, local.A, local.B, local.C, local.D, R(71) );
243     P( local.D, local.E, local.A, local.B, local.C, R(72) );
244     P( local.C, local.D, local.E, local.A, local.B, R(73) );
245     P( local.B, local.C, local.D, local.E, local.A, R(74) );
246     P( local.A, local.B, local.C, local.D, local.E, R(75) );
247     P( local.E, local.A, local.B, local.C, local.D, R(76) );
248     P( local.D, local.E, local.A, local.B, local.C, R(77) );
249     P( local.C, local.D, local.E, local.A, local.B, R(78) );
250     P( local.B, local.C, local.D, local.E, local.A, R(79) );
251 
252 #undef K
253 #undef F
254 
255     ctx->state[0] += local.A;
256     ctx->state[1] += local.B;
257     ctx->state[2] += local.C;
258     ctx->state[3] += local.D;
259     ctx->state[4] += local.E;
260 
261     /* Zeroise buffers and variables to clear sensitive data from memory. */
262     mbedtls_platform_zeroize( &local, sizeof( local ) );
263 
264     return( 0 );
265 }
266 
267 #endif /* !MBEDTLS_SHA1_PROCESS_ALT */
268 
269 /*
270  * SHA-1 process buffer
271  */
mbedtls_sha1_update(mbedtls_sha1_context * ctx,const unsigned char * input,size_t ilen)272 int mbedtls_sha1_update( mbedtls_sha1_context *ctx,
273                              const unsigned char *input,
274                              size_t ilen )
275 {
276     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
277     size_t fill;
278     uint32_t left;
279 
280     SHA1_VALIDATE_RET( ctx != NULL );
281     SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
282 
283     if( ilen == 0 )
284         return( 0 );
285 
286     left = ctx->total[0] & 0x3F;
287     fill = 64 - left;
288 
289     ctx->total[0] += (uint32_t) ilen;
290     ctx->total[0] &= 0xFFFFFFFF;
291 
292     if( ctx->total[0] < (uint32_t) ilen )
293         ctx->total[1]++;
294 
295     if( left && ilen >= fill )
296     {
297         memcpy( (void *) (ctx->buffer + left), input, fill );
298 
299         if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
300             return( ret );
301 
302         input += fill;
303         ilen  -= fill;
304         left = 0;
305     }
306 
307     while( ilen >= 64 )
308     {
309         if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
310             return( ret );
311 
312         input += 64;
313         ilen  -= 64;
314     }
315 
316     if( ilen > 0 )
317         memcpy( (void *) (ctx->buffer + left), input, ilen );
318 
319     return( 0 );
320 }
321 
322 /*
323  * SHA-1 final digest
324  */
mbedtls_sha1_finish(mbedtls_sha1_context * ctx,unsigned char output[20])325 int mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
326                              unsigned char output[20] )
327 {
328     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
329     uint32_t used;
330     uint32_t high, low;
331 
332     SHA1_VALIDATE_RET( ctx != NULL );
333     SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
334 
335     /*
336      * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
337      */
338     used = ctx->total[0] & 0x3F;
339 
340     ctx->buffer[used++] = 0x80;
341 
342     if( used <= 56 )
343     {
344         /* Enough room for padding + length in current block */
345         memset( ctx->buffer + used, 0, 56 - used );
346     }
347     else
348     {
349         /* We'll need an extra block */
350         memset( ctx->buffer + used, 0, 64 - used );
351 
352         if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
353             return( ret );
354 
355         memset( ctx->buffer, 0, 56 );
356     }
357 
358     /*
359      * Add message length
360      */
361     high = ( ctx->total[0] >> 29 )
362          | ( ctx->total[1] <<  3 );
363     low  = ( ctx->total[0] <<  3 );
364 
365     MBEDTLS_PUT_UINT32_BE( high, ctx->buffer, 56 );
366     MBEDTLS_PUT_UINT32_BE( low,  ctx->buffer, 60 );
367 
368     if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
369         return( ret );
370 
371     /*
372      * Output final state
373      */
374     MBEDTLS_PUT_UINT32_BE( ctx->state[0], output,  0 );
375     MBEDTLS_PUT_UINT32_BE( ctx->state[1], output,  4 );
376     MBEDTLS_PUT_UINT32_BE( ctx->state[2], output,  8 );
377     MBEDTLS_PUT_UINT32_BE( ctx->state[3], output, 12 );
378     MBEDTLS_PUT_UINT32_BE( ctx->state[4], output, 16 );
379 
380     return( 0 );
381 }
382 
383 #endif /* !MBEDTLS_SHA1_ALT */
384 
385 /*
386  * output = SHA-1( input buffer )
387  */
mbedtls_sha1(const unsigned char * input,size_t ilen,unsigned char output[20])388 int mbedtls_sha1( const unsigned char *input,
389                       size_t ilen,
390                       unsigned char output[20] )
391 {
392     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
393     mbedtls_sha1_context ctx;
394 
395     SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
396     SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
397 
398     mbedtls_sha1_init( &ctx );
399 
400     if( ( ret = mbedtls_sha1_starts( &ctx ) ) != 0 )
401         goto exit;
402 
403     if( ( ret = mbedtls_sha1_update( &ctx, input, ilen ) ) != 0 )
404         goto exit;
405 
406     if( ( ret = mbedtls_sha1_finish( &ctx, output ) ) != 0 )
407         goto exit;
408 
409 exit:
410     mbedtls_sha1_free( &ctx );
411 
412     return( ret );
413 }
414 
415 #if defined(MBEDTLS_SELF_TEST)
416 /*
417  * FIPS-180-1 test vectors
418  */
419 static const unsigned char sha1_test_buf[3][57] =
420 {
421     { "abc" },
422     { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
423     { "" }
424 };
425 
426 static const size_t sha1_test_buflen[3] =
427 {
428     3, 56, 1000
429 };
430 
431 static const unsigned char sha1_test_sum[3][20] =
432 {
433     { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
434       0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
435     { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
436       0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
437     { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
438       0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
439 };
440 
441 /*
442  * Checkup routine
443  */
mbedtls_sha1_self_test(int verbose)444 int mbedtls_sha1_self_test( int verbose )
445 {
446     int i, j, buflen, ret = 0;
447     unsigned char buf[1024];
448     unsigned char sha1sum[20];
449     mbedtls_sha1_context ctx;
450 
451     mbedtls_sha1_init( &ctx );
452 
453     /*
454      * SHA-1
455      */
456     for( i = 0; i < 3; i++ )
457     {
458         if( verbose != 0 )
459             mbedtls_printf( "  SHA-1 test #%d: ", i + 1 );
460 
461         if( ( ret = mbedtls_sha1_starts( &ctx ) ) != 0 )
462             goto fail;
463 
464         if( i == 2 )
465         {
466             memset( buf, 'a', buflen = 1000 );
467 
468             for( j = 0; j < 1000; j++ )
469             {
470                 ret = mbedtls_sha1_update( &ctx, buf, buflen );
471                 if( ret != 0 )
472                     goto fail;
473             }
474         }
475         else
476         {
477             ret = mbedtls_sha1_update( &ctx, sha1_test_buf[i],
478                                            sha1_test_buflen[i] );
479             if( ret != 0 )
480                 goto fail;
481         }
482 
483         if( ( ret = mbedtls_sha1_finish( &ctx, sha1sum ) ) != 0 )
484             goto fail;
485 
486         if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
487         {
488             ret = 1;
489             goto fail;
490         }
491 
492         if( verbose != 0 )
493             mbedtls_printf( "passed\n" );
494     }
495 
496     if( verbose != 0 )
497         mbedtls_printf( "\n" );
498 
499     goto exit;
500 
501 fail:
502     if( verbose != 0 )
503         mbedtls_printf( "failed\n" );
504 
505 exit:
506     mbedtls_sha1_free( &ctx );
507 
508     return( ret );
509 }
510 
511 #endif /* MBEDTLS_SELF_TEST */
512 
513 #endif /* MBEDTLS_SHA1_C */
514