1 /*
2  *  FIPS-180-1 compliant SHA-1 implementation
3  *
4  *  Copyright The Mbed TLS Contributors
5  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6  *
7  *  This file is provided under the Apache License 2.0, or the
8  *  GNU General Public License v2.0 or later.
9  *
10  *  **********
11  *  Apache License 2.0:
12  *
13  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
14  *  not use this file except in compliance with the License.
15  *  You may obtain a copy of the License at
16  *
17  *  http://www.apache.org/licenses/LICENSE-2.0
18  *
19  *  Unless required by applicable law or agreed to in writing, software
20  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the License for the specific language governing permissions and
23  *  limitations under the License.
24  *
25  *  **********
26  *
27  *  **********
28  *  GNU General Public License v2.0 or later:
29  *
30  *  This program is free software; you can redistribute it and/or modify
31  *  it under the terms of the GNU General Public License as published by
32  *  the Free Software Foundation; either version 2 of the License, or
33  *  (at your option) any later version.
34  *
35  *  This program is distributed in the hope that it will be useful,
36  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
37  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38  *  GNU General Public License for more details.
39  *
40  *  You should have received a copy of the GNU General Public License along
41  *  with this program; if not, write to the Free Software Foundation, Inc.,
42  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43  *
44  *  **********
45  */
46 /*
47  *  The SHA-1 standard was published by NIST in 1993.
48  *
49  *  http://www.itl.nist.gov/fipspubs/fip180-1.htm
50  */
51 
52 #if !defined(MBEDTLS_CONFIG_FILE)
53 #include "mbedtls/config.h"
54 #else
55 #include MBEDTLS_CONFIG_FILE
56 #endif
57 
58 #if defined(MBEDTLS_SHA1_C)
59 
60 #include "mbedtls/sha1.h"
61 #include "mbedtls/platform_util.h"
62 
63 #include <string.h>
64 
65 #if defined(MBEDTLS_SELF_TEST)
66 #if defined(MBEDTLS_PLATFORM_C)
67 #include "mbedtls/platform.h"
68 #else
69 #include <stdio.h>
70 #define mbedtls_printf printf
71 #endif /* MBEDTLS_PLATFORM_C */
72 #endif /* MBEDTLS_SELF_TEST */
73 
74 #define SHA1_VALIDATE_RET(cond)                             \
75     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_SHA1_BAD_INPUT_DATA )
76 
77 #define SHA1_VALIDATE(cond)  MBEDTLS_INTERNAL_VALIDATE( cond )
78 
79 #if !defined(MBEDTLS_SHA1_ALT)
80 
81 /*
82  * 32-bit integer manipulation macros (big endian)
83  */
84 #ifndef GET_UINT32_BE
85 #define GET_UINT32_BE(n,b,i)                            \
86 {                                                       \
87     (n) = ( (uint32_t) (b)[(i)    ] << 24 )             \
88         | ( (uint32_t) (b)[(i) + 1] << 16 )             \
89         | ( (uint32_t) (b)[(i) + 2] <<  8 )             \
90         | ( (uint32_t) (b)[(i) + 3]       );            \
91 }
92 #endif
93 
94 #ifndef PUT_UINT32_BE
95 #define PUT_UINT32_BE(n,b,i)                            \
96 {                                                       \
97     (b)[(i)    ] = (unsigned char) ( (n) >> 24 );       \
98     (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );       \
99     (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );       \
100     (b)[(i) + 3] = (unsigned char) ( (n)       );       \
101 }
102 #endif
103 
mbedtls_sha1_init(mbedtls_sha1_context * ctx)104 void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
105 {
106     SHA1_VALIDATE( ctx != NULL );
107 
108     memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
109 }
110 
mbedtls_sha1_free(mbedtls_sha1_context * ctx)111 void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
112 {
113     if( ctx == NULL )
114         return;
115 
116     mbedtls_platform_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
117 }
118 
mbedtls_sha1_clone(mbedtls_sha1_context * dst,const mbedtls_sha1_context * src)119 void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
120                          const mbedtls_sha1_context *src )
121 {
122     SHA1_VALIDATE( dst != NULL );
123     SHA1_VALIDATE( src != NULL );
124 
125     *dst = *src;
126 }
127 
128 /*
129  * SHA-1 context setup
130  */
mbedtls_sha1_starts_ret(mbedtls_sha1_context * ctx)131 int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
132 {
133     SHA1_VALIDATE_RET( ctx != NULL );
134 
135     ctx->total[0] = 0;
136     ctx->total[1] = 0;
137 
138     ctx->state[0] = 0x67452301;
139     ctx->state[1] = 0xEFCDAB89;
140     ctx->state[2] = 0x98BADCFE;
141     ctx->state[3] = 0x10325476;
142     ctx->state[4] = 0xC3D2E1F0;
143 
144     return( 0 );
145 }
146 
147 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
mbedtls_sha1_starts(mbedtls_sha1_context * ctx)148 void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
149 {
150     mbedtls_sha1_starts_ret( ctx );
151 }
152 #endif
153 
154 #if !defined(MBEDTLS_SHA1_PROCESS_ALT)
mbedtls_internal_sha1_process(mbedtls_sha1_context * ctx,const unsigned char data[64])155 int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
156                                    const unsigned char data[64] )
157 {
158     uint32_t temp, W[16], A, B, C, D, E;
159 
160     SHA1_VALIDATE_RET( ctx != NULL );
161     SHA1_VALIDATE_RET( (const unsigned char *)data != NULL );
162 
163     GET_UINT32_BE( W[ 0], data,  0 );
164     GET_UINT32_BE( W[ 1], data,  4 );
165     GET_UINT32_BE( W[ 2], data,  8 );
166     GET_UINT32_BE( W[ 3], data, 12 );
167     GET_UINT32_BE( W[ 4], data, 16 );
168     GET_UINT32_BE( W[ 5], data, 20 );
169     GET_UINT32_BE( W[ 6], data, 24 );
170     GET_UINT32_BE( W[ 7], data, 28 );
171     GET_UINT32_BE( W[ 8], data, 32 );
172     GET_UINT32_BE( W[ 9], data, 36 );
173     GET_UINT32_BE( W[10], data, 40 );
174     GET_UINT32_BE( W[11], data, 44 );
175     GET_UINT32_BE( W[12], data, 48 );
176     GET_UINT32_BE( W[13], data, 52 );
177     GET_UINT32_BE( W[14], data, 56 );
178     GET_UINT32_BE( W[15], data, 60 );
179 
180 #define S(x,n) (((x) << (n)) | (((x) & 0xFFFFFFFF) >> (32 - (n))))
181 
182 #define R(t)                                                    \
183     (                                                           \
184         temp = W[( (t) -  3 ) & 0x0F] ^ W[( (t) - 8 ) & 0x0F] ^ \
185                W[( (t) - 14 ) & 0x0F] ^ W[  (t)       & 0x0F],  \
186         ( W[(t) & 0x0F] = S(temp,1) )                           \
187     )
188 
189 #define P(a,b,c,d,e,x)                                          \
190     do                                                          \
191     {                                                           \
192         (e) += S((a),5) + F((b),(c),(d)) + K + (x);             \
193         (b) = S((b),30);                                        \
194     } while( 0 )
195 
196     A = ctx->state[0];
197     B = ctx->state[1];
198     C = ctx->state[2];
199     D = ctx->state[3];
200     E = ctx->state[4];
201 
202 #define F(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
203 #define K 0x5A827999
204 
205     P( A, B, C, D, E, W[0]  );
206     P( E, A, B, C, D, W[1]  );
207     P( D, E, A, B, C, W[2]  );
208     P( C, D, E, A, B, W[3]  );
209     P( B, C, D, E, A, W[4]  );
210     P( A, B, C, D, E, W[5]  );
211     P( E, A, B, C, D, W[6]  );
212     P( D, E, A, B, C, W[7]  );
213     P( C, D, E, A, B, W[8]  );
214     P( B, C, D, E, A, W[9]  );
215     P( A, B, C, D, E, W[10] );
216     P( E, A, B, C, D, W[11] );
217     P( D, E, A, B, C, W[12] );
218     P( C, D, E, A, B, W[13] );
219     P( B, C, D, E, A, W[14] );
220     P( A, B, C, D, E, W[15] );
221     P( E, A, B, C, D, R(16) );
222     P( D, E, A, B, C, R(17) );
223     P( C, D, E, A, B, R(18) );
224     P( B, C, D, E, A, R(19) );
225 
226 #undef K
227 #undef F
228 
229 #define F(x,y,z) ((x) ^ (y) ^ (z))
230 #define K 0x6ED9EBA1
231 
232     P( A, B, C, D, E, R(20) );
233     P( E, A, B, C, D, R(21) );
234     P( D, E, A, B, C, R(22) );
235     P( C, D, E, A, B, R(23) );
236     P( B, C, D, E, A, R(24) );
237     P( A, B, C, D, E, R(25) );
238     P( E, A, B, C, D, R(26) );
239     P( D, E, A, B, C, R(27) );
240     P( C, D, E, A, B, R(28) );
241     P( B, C, D, E, A, R(29) );
242     P( A, B, C, D, E, R(30) );
243     P( E, A, B, C, D, R(31) );
244     P( D, E, A, B, C, R(32) );
245     P( C, D, E, A, B, R(33) );
246     P( B, C, D, E, A, R(34) );
247     P( A, B, C, D, E, R(35) );
248     P( E, A, B, C, D, R(36) );
249     P( D, E, A, B, C, R(37) );
250     P( C, D, E, A, B, R(38) );
251     P( B, C, D, E, A, R(39) );
252 
253 #undef K
254 #undef F
255 
256 #define F(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
257 #define K 0x8F1BBCDC
258 
259     P( A, B, C, D, E, R(40) );
260     P( E, A, B, C, D, R(41) );
261     P( D, E, A, B, C, R(42) );
262     P( C, D, E, A, B, R(43) );
263     P( B, C, D, E, A, R(44) );
264     P( A, B, C, D, E, R(45) );
265     P( E, A, B, C, D, R(46) );
266     P( D, E, A, B, C, R(47) );
267     P( C, D, E, A, B, R(48) );
268     P( B, C, D, E, A, R(49) );
269     P( A, B, C, D, E, R(50) );
270     P( E, A, B, C, D, R(51) );
271     P( D, E, A, B, C, R(52) );
272     P( C, D, E, A, B, R(53) );
273     P( B, C, D, E, A, R(54) );
274     P( A, B, C, D, E, R(55) );
275     P( E, A, B, C, D, R(56) );
276     P( D, E, A, B, C, R(57) );
277     P( C, D, E, A, B, R(58) );
278     P( B, C, D, E, A, R(59) );
279 
280 #undef K
281 #undef F
282 
283 #define F(x,y,z) ((x) ^ (y) ^ (z))
284 #define K 0xCA62C1D6
285 
286     P( A, B, C, D, E, R(60) );
287     P( E, A, B, C, D, R(61) );
288     P( D, E, A, B, C, R(62) );
289     P( C, D, E, A, B, R(63) );
290     P( B, C, D, E, A, R(64) );
291     P( A, B, C, D, E, R(65) );
292     P( E, A, B, C, D, R(66) );
293     P( D, E, A, B, C, R(67) );
294     P( C, D, E, A, B, R(68) );
295     P( B, C, D, E, A, R(69) );
296     P( A, B, C, D, E, R(70) );
297     P( E, A, B, C, D, R(71) );
298     P( D, E, A, B, C, R(72) );
299     P( C, D, E, A, B, R(73) );
300     P( B, C, D, E, A, R(74) );
301     P( A, B, C, D, E, R(75) );
302     P( E, A, B, C, D, R(76) );
303     P( D, E, A, B, C, R(77) );
304     P( C, D, E, A, B, R(78) );
305     P( B, C, D, E, A, R(79) );
306 
307 #undef K
308 #undef F
309 
310     ctx->state[0] += A;
311     ctx->state[1] += B;
312     ctx->state[2] += C;
313     ctx->state[3] += D;
314     ctx->state[4] += E;
315 
316     return( 0 );
317 }
318 
319 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
mbedtls_sha1_process(mbedtls_sha1_context * ctx,const unsigned char data[64])320 void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
321                            const unsigned char data[64] )
322 {
323     mbedtls_internal_sha1_process( ctx, data );
324 }
325 #endif
326 #endif /* !MBEDTLS_SHA1_PROCESS_ALT */
327 
328 /*
329  * SHA-1 process buffer
330  */
mbedtls_sha1_update_ret(mbedtls_sha1_context * ctx,const unsigned char * input,size_t ilen)331 int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
332                              const unsigned char *input,
333                              size_t ilen )
334 {
335     int ret;
336     size_t fill;
337     uint32_t left;
338 
339     SHA1_VALIDATE_RET( ctx != NULL );
340     SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
341 
342     if( ilen == 0 )
343         return( 0 );
344 
345     left = ctx->total[0] & 0x3F;
346     fill = 64 - left;
347 
348     ctx->total[0] += (uint32_t) ilen;
349     ctx->total[0] &= 0xFFFFFFFF;
350 
351     if( ctx->total[0] < (uint32_t) ilen )
352         ctx->total[1]++;
353 
354     if( left && ilen >= fill )
355     {
356         memcpy( (void *) (ctx->buffer + left), input, fill );
357 
358         if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
359             return( ret );
360 
361         input += fill;
362         ilen  -= fill;
363         left = 0;
364     }
365 
366     while( ilen >= 64 )
367     {
368         if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
369             return( ret );
370 
371         input += 64;
372         ilen  -= 64;
373     }
374 
375     if( ilen > 0 )
376         memcpy( (void *) (ctx->buffer + left), input, ilen );
377 
378     return( 0 );
379 }
380 
381 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
mbedtls_sha1_update(mbedtls_sha1_context * ctx,const unsigned char * input,size_t ilen)382 void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
383                           const unsigned char *input,
384                           size_t ilen )
385 {
386     mbedtls_sha1_update_ret( ctx, input, ilen );
387 }
388 #endif
389 
390 /*
391  * SHA-1 final digest
392  */
mbedtls_sha1_finish_ret(mbedtls_sha1_context * ctx,unsigned char output[20])393 int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
394                              unsigned char output[20] )
395 {
396     int ret;
397     uint32_t used;
398     uint32_t high, low;
399 
400     SHA1_VALIDATE_RET( ctx != NULL );
401     SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
402 
403     /*
404      * Add padding: 0x80 then 0x00 until 8 bytes remain for the length
405      */
406     used = ctx->total[0] & 0x3F;
407 
408     ctx->buffer[used++] = 0x80;
409 
410     if( used <= 56 )
411     {
412         /* Enough room for padding + length in current block */
413         memset( ctx->buffer + used, 0, 56 - used );
414     }
415     else
416     {
417         /* We'll need an extra block */
418         memset( ctx->buffer + used, 0, 64 - used );
419 
420         if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
421             return( ret );
422 
423         memset( ctx->buffer, 0, 56 );
424     }
425 
426     /*
427      * Add message length
428      */
429     high = ( ctx->total[0] >> 29 )
430          | ( ctx->total[1] <<  3 );
431     low  = ( ctx->total[0] <<  3 );
432 
433     PUT_UINT32_BE( high, ctx->buffer, 56 );
434     PUT_UINT32_BE( low,  ctx->buffer, 60 );
435 
436     if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer ) ) != 0 )
437         return( ret );
438 
439     /*
440      * Output final state
441      */
442     PUT_UINT32_BE( ctx->state[0], output,  0 );
443     PUT_UINT32_BE( ctx->state[1], output,  4 );
444     PUT_UINT32_BE( ctx->state[2], output,  8 );
445     PUT_UINT32_BE( ctx->state[3], output, 12 );
446     PUT_UINT32_BE( ctx->state[4], output, 16 );
447 
448     return( 0 );
449 }
450 
451 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
mbedtls_sha1_finish(mbedtls_sha1_context * ctx,unsigned char output[20])452 void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
453                           unsigned char output[20] )
454 {
455     mbedtls_sha1_finish_ret( ctx, output );
456 }
457 #endif
458 
459 #endif /* !MBEDTLS_SHA1_ALT */
460 
461 /*
462  * output = SHA-1( input buffer )
463  */
mbedtls_sha1_ret(const unsigned char * input,size_t ilen,unsigned char output[20])464 int mbedtls_sha1_ret( const unsigned char *input,
465                       size_t ilen,
466                       unsigned char output[20] )
467 {
468     int ret;
469     mbedtls_sha1_context ctx;
470 
471     SHA1_VALIDATE_RET( ilen == 0 || input != NULL );
472     SHA1_VALIDATE_RET( (unsigned char *)output != NULL );
473 
474     mbedtls_sha1_init( &ctx );
475 
476     if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
477         goto exit;
478 
479     if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
480         goto exit;
481 
482     if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
483         goto exit;
484 
485 exit:
486     mbedtls_sha1_free( &ctx );
487 
488     return( ret );
489 }
490 
491 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
mbedtls_sha1(const unsigned char * input,size_t ilen,unsigned char output[20])492 void mbedtls_sha1( const unsigned char *input,
493                    size_t ilen,
494                    unsigned char output[20] )
495 {
496     mbedtls_sha1_ret( input, ilen, output );
497 }
498 #endif
499 
500 #if defined(MBEDTLS_SELF_TEST)
501 /*
502  * FIPS-180-1 test vectors
503  */
504 static const unsigned char sha1_test_buf[3][57] =
505 {
506     { "abc" },
507     { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
508     { "" }
509 };
510 
511 static const size_t sha1_test_buflen[3] =
512 {
513     3, 56, 1000
514 };
515 
516 static const unsigned char sha1_test_sum[3][20] =
517 {
518     { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
519       0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
520     { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
521       0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
522     { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
523       0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
524 };
525 
526 /*
527  * Checkup routine
528  */
mbedtls_sha1_self_test(int verbose)529 int mbedtls_sha1_self_test( int verbose )
530 {
531     int i, j, buflen, ret = 0;
532     unsigned char buf[1024];
533     unsigned char sha1sum[20];
534     mbedtls_sha1_context ctx;
535 
536     mbedtls_sha1_init( &ctx );
537 
538     /*
539      * SHA-1
540      */
541     for( i = 0; i < 3; i++ )
542     {
543         if( verbose != 0 )
544             mbedtls_printf( "  SHA-1 test #%d: ", i + 1 );
545 
546         if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
547             goto fail;
548 
549         if( i == 2 )
550         {
551             memset( buf, 'a', buflen = 1000 );
552 
553             for( j = 0; j < 1000; j++ )
554             {
555                 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
556                 if( ret != 0 )
557                     goto fail;
558             }
559         }
560         else
561         {
562             ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
563                                            sha1_test_buflen[i] );
564             if( ret != 0 )
565                 goto fail;
566         }
567 
568         if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
569             goto fail;
570 
571         if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
572         {
573             ret = 1;
574             goto fail;
575         }
576 
577         if( verbose != 0 )
578             mbedtls_printf( "passed\n" );
579     }
580 
581     if( verbose != 0 )
582         mbedtls_printf( "\n" );
583 
584     goto exit;
585 
586 fail:
587     if( verbose != 0 )
588         mbedtls_printf( "failed\n" );
589 
590 exit:
591     mbedtls_sha1_free( &ctx );
592 
593     return( ret );
594 }
595 
596 #endif /* MBEDTLS_SELF_TEST */
597 
598 #endif /* MBEDTLS_SHA1_C */
599