Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sha1.c Source File

sha1.c

00001 /*
00002  *  FIPS-180-1 compliant SHA-1 implementation
00003  *
00004  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
00005  *  SPDX-License-Identifier: Apache-2.0
00006  *
00007  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00008  *  not use this file except in compliance with the License.
00009  *  You may obtain a copy of the License at
00010  *
00011  *  http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  *  Unless required by applicable law or agreed to in writing, software
00014  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00015  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  *  See the License for the specific language governing permissions and
00017  *  limitations under the License.
00018  *
00019  *  This file is part of mbed TLS (https://tls.mbed.org)
00020  */
00021 /*
00022  *  The SHA-1 standard was published by NIST in 1993.
00023  *
00024  *  http://www.itl.nist.gov/fipspubs/fip180-1.htm
00025  */
00026 
00027 #if !defined(MBEDTLS_CONFIG_FILE)
00028 #include "mbedtls/config.h"
00029 #else
00030 #include MBEDTLS_CONFIG_FILE
00031 #endif
00032 
00033 #if defined(MBEDTLS_SHA1_C)
00034 
00035 #include "mbedtls/sha1.h"
00036 
00037 #include <string.h>
00038 
00039 #if defined(MBEDTLS_SELF_TEST)
00040 #if defined(MBEDTLS_PLATFORM_C)
00041 #include "mbedtls/platform.h"
00042 #else
00043 #include <stdio.h>
00044 #define mbedtls_printf printf
00045 #endif /* MBEDTLS_PLATFORM_C */
00046 #endif /* MBEDTLS_SELF_TEST */
00047 
00048 #if !defined(MBEDTLS_SHA1_ALT)
00049 
00050 /* Implementation that should never be optimized out by the compiler */
00051 static void mbedtls_zeroize( void *v, size_t n ) {
00052     volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
00053 }
00054 
00055 /*
00056  * 32-bit integer manipulation macros (big endian)
00057  */
00058 #ifndef GET_UINT32_BE
00059 #define GET_UINT32_BE(n,b,i)                            \
00060 {                                                       \
00061     (n) = ( (uint32_t) (b)[(i)    ] << 24 )             \
00062         | ( (uint32_t) (b)[(i) + 1] << 16 )             \
00063         | ( (uint32_t) (b)[(i) + 2] <<  8 )             \
00064         | ( (uint32_t) (b)[(i) + 3]       );            \
00065 }
00066 #endif
00067 
00068 #ifndef PUT_UINT32_BE
00069 #define PUT_UINT32_BE(n,b,i)                            \
00070 {                                                       \
00071     (b)[(i)    ] = (unsigned char) ( (n) >> 24 );       \
00072     (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );       \
00073     (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );       \
00074     (b)[(i) + 3] = (unsigned char) ( (n)       );       \
00075 }
00076 #endif
00077 
00078 void mbedtls_sha1_init( mbedtls_sha1_context *ctx )
00079 {
00080     memset( ctx, 0, sizeof( mbedtls_sha1_context ) );
00081 }
00082 
00083 void mbedtls_sha1_free( mbedtls_sha1_context *ctx )
00084 {
00085     if( ctx == NULL )
00086         return;
00087 
00088     mbedtls_zeroize( ctx, sizeof( mbedtls_sha1_context ) );
00089 }
00090 
00091 void mbedtls_sha1_clone( mbedtls_sha1_context *dst,
00092                          const mbedtls_sha1_context *src )
00093 {
00094     *dst = *src;
00095 }
00096 
00097 /*
00098  * SHA-1 context setup
00099  */
00100 int mbedtls_sha1_starts_ret( mbedtls_sha1_context *ctx )
00101 {
00102     ctx->total [0] = 0;
00103     ctx->total [1] = 0;
00104 
00105     ctx->state [0] = 0x67452301;
00106     ctx->state [1] = 0xEFCDAB89;
00107     ctx->state [2] = 0x98BADCFE;
00108     ctx->state [3] = 0x10325476;
00109     ctx->state [4] = 0xC3D2E1F0;
00110 
00111     return( 0 );
00112 }
00113 
00114 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
00115 void mbedtls_sha1_starts( mbedtls_sha1_context *ctx )
00116 {
00117     mbedtls_sha1_starts_ret( ctx );
00118 }
00119 #endif
00120 
00121 #if !defined(MBEDTLS_SHA1_PROCESS_ALT)
00122 int mbedtls_internal_sha1_process( mbedtls_sha1_context *ctx,
00123                                    const unsigned char data[64] )
00124 {
00125     uint32_t temp, W[16], A, B, C, D, E;
00126 
00127     GET_UINT32_BE( W[ 0], data,  0 );
00128     GET_UINT32_BE( W[ 1], data,  4 );
00129     GET_UINT32_BE( W[ 2], data,  8 );
00130     GET_UINT32_BE( W[ 3], data, 12 );
00131     GET_UINT32_BE( W[ 4], data, 16 );
00132     GET_UINT32_BE( W[ 5], data, 20 );
00133     GET_UINT32_BE( W[ 6], data, 24 );
00134     GET_UINT32_BE( W[ 7], data, 28 );
00135     GET_UINT32_BE( W[ 8], data, 32 );
00136     GET_UINT32_BE( W[ 9], data, 36 );
00137     GET_UINT32_BE( W[10], data, 40 );
00138     GET_UINT32_BE( W[11], data, 44 );
00139     GET_UINT32_BE( W[12], data, 48 );
00140     GET_UINT32_BE( W[13], data, 52 );
00141     GET_UINT32_BE( W[14], data, 56 );
00142     GET_UINT32_BE( W[15], data, 60 );
00143 
00144 #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
00145 
00146 #define R(t)                                            \
00147 (                                                       \
00148     temp = W[( t -  3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \
00149            W[( t - 14 ) & 0x0F] ^ W[  t       & 0x0F],  \
00150     ( W[t & 0x0F] = S(temp,1) )                         \
00151 )
00152 
00153 #define P(a,b,c,d,e,x)                                  \
00154 {                                                       \
00155     e += S(a,5) + F(b,c,d) + K + x; b = S(b,30);        \
00156 }
00157 
00158     A = ctx->state [0];
00159     B = ctx->state [1];
00160     C = ctx->state [2];
00161     D = ctx->state [3];
00162     E = ctx->state [4];
00163 
00164 #define F(x,y,z) (z ^ (x & (y ^ z)))
00165 #define K 0x5A827999
00166 
00167     P( A, B, C, D, E, W[0]  );
00168     P( E, A, B, C, D, W[1]  );
00169     P( D, E, A, B, C, W[2]  );
00170     P( C, D, E, A, B, W[3]  );
00171     P( B, C, D, E, A, W[4]  );
00172     P( A, B, C, D, E, W[5]  );
00173     P( E, A, B, C, D, W[6]  );
00174     P( D, E, A, B, C, W[7]  );
00175     P( C, D, E, A, B, W[8]  );
00176     P( B, C, D, E, A, W[9]  );
00177     P( A, B, C, D, E, W[10] );
00178     P( E, A, B, C, D, W[11] );
00179     P( D, E, A, B, C, W[12] );
00180     P( C, D, E, A, B, W[13] );
00181     P( B, C, D, E, A, W[14] );
00182     P( A, B, C, D, E, W[15] );
00183     P( E, A, B, C, D, R(16) );
00184     P( D, E, A, B, C, R(17) );
00185     P( C, D, E, A, B, R(18) );
00186     P( B, C, D, E, A, R(19) );
00187 
00188 #undef K
00189 #undef F
00190 
00191 #define F(x,y,z) (x ^ y ^ z)
00192 #define K 0x6ED9EBA1
00193 
00194     P( A, B, C, D, E, R(20) );
00195     P( E, A, B, C, D, R(21) );
00196     P( D, E, A, B, C, R(22) );
00197     P( C, D, E, A, B, R(23) );
00198     P( B, C, D, E, A, R(24) );
00199     P( A, B, C, D, E, R(25) );
00200     P( E, A, B, C, D, R(26) );
00201     P( D, E, A, B, C, R(27) );
00202     P( C, D, E, A, B, R(28) );
00203     P( B, C, D, E, A, R(29) );
00204     P( A, B, C, D, E, R(30) );
00205     P( E, A, B, C, D, R(31) );
00206     P( D, E, A, B, C, R(32) );
00207     P( C, D, E, A, B, R(33) );
00208     P( B, C, D, E, A, R(34) );
00209     P( A, B, C, D, E, R(35) );
00210     P( E, A, B, C, D, R(36) );
00211     P( D, E, A, B, C, R(37) );
00212     P( C, D, E, A, B, R(38) );
00213     P( B, C, D, E, A, R(39) );
00214 
00215 #undef K
00216 #undef F
00217 
00218 #define F(x,y,z) ((x & y) | (z & (x | y)))
00219 #define K 0x8F1BBCDC
00220 
00221     P( A, B, C, D, E, R(40) );
00222     P( E, A, B, C, D, R(41) );
00223     P( D, E, A, B, C, R(42) );
00224     P( C, D, E, A, B, R(43) );
00225     P( B, C, D, E, A, R(44) );
00226     P( A, B, C, D, E, R(45) );
00227     P( E, A, B, C, D, R(46) );
00228     P( D, E, A, B, C, R(47) );
00229     P( C, D, E, A, B, R(48) );
00230     P( B, C, D, E, A, R(49) );
00231     P( A, B, C, D, E, R(50) );
00232     P( E, A, B, C, D, R(51) );
00233     P( D, E, A, B, C, R(52) );
00234     P( C, D, E, A, B, R(53) );
00235     P( B, C, D, E, A, R(54) );
00236     P( A, B, C, D, E, R(55) );
00237     P( E, A, B, C, D, R(56) );
00238     P( D, E, A, B, C, R(57) );
00239     P( C, D, E, A, B, R(58) );
00240     P( B, C, D, E, A, R(59) );
00241 
00242 #undef K
00243 #undef F
00244 
00245 #define F(x,y,z) (x ^ y ^ z)
00246 #define K 0xCA62C1D6
00247 
00248     P( A, B, C, D, E, R(60) );
00249     P( E, A, B, C, D, R(61) );
00250     P( D, E, A, B, C, R(62) );
00251     P( C, D, E, A, B, R(63) );
00252     P( B, C, D, E, A, R(64) );
00253     P( A, B, C, D, E, R(65) );
00254     P( E, A, B, C, D, R(66) );
00255     P( D, E, A, B, C, R(67) );
00256     P( C, D, E, A, B, R(68) );
00257     P( B, C, D, E, A, R(69) );
00258     P( A, B, C, D, E, R(70) );
00259     P( E, A, B, C, D, R(71) );
00260     P( D, E, A, B, C, R(72) );
00261     P( C, D, E, A, B, R(73) );
00262     P( B, C, D, E, A, R(74) );
00263     P( A, B, C, D, E, R(75) );
00264     P( E, A, B, C, D, R(76) );
00265     P( D, E, A, B, C, R(77) );
00266     P( C, D, E, A, B, R(78) );
00267     P( B, C, D, E, A, R(79) );
00268 
00269 #undef K
00270 #undef F
00271 
00272     ctx->state [0] += A;
00273     ctx->state [1] += B;
00274     ctx->state [2] += C;
00275     ctx->state [3] += D;
00276     ctx->state [4] += E;
00277 
00278     return( 0 );
00279 }
00280 
00281 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
00282 void mbedtls_sha1_process( mbedtls_sha1_context *ctx,
00283                            const unsigned char data[64] )
00284 {
00285     mbedtls_internal_sha1_process( ctx, data );
00286 }
00287 #endif
00288 #endif /* !MBEDTLS_SHA1_PROCESS_ALT */
00289 
00290 /*
00291  * SHA-1 process buffer
00292  */
00293 int mbedtls_sha1_update_ret( mbedtls_sha1_context *ctx,
00294                              const unsigned char *input,
00295                              size_t ilen )
00296 {
00297     int ret;
00298     size_t fill;
00299     uint32_t left;
00300 
00301     if( ilen == 0 )
00302         return( 0 );
00303 
00304     left = ctx->total [0] & 0x3F;
00305     fill = 64 - left;
00306 
00307     ctx->total [0] += (uint32_t) ilen;
00308     ctx->total [0] &= 0xFFFFFFFF;
00309 
00310     if( ctx->total [0] < (uint32_t) ilen )
00311         ctx->total [1]++;
00312 
00313     if( left && ilen >= fill )
00314     {
00315         memcpy( (void *) (ctx->buffer  + left), input, fill );
00316 
00317         if( ( ret = mbedtls_internal_sha1_process( ctx, ctx->buffer  ) ) != 0 )
00318             return( ret );
00319 
00320         input += fill;
00321         ilen  -= fill;
00322         left = 0;
00323     }
00324 
00325     while( ilen >= 64 )
00326     {
00327         if( ( ret = mbedtls_internal_sha1_process( ctx, input ) ) != 0 )
00328             return( ret );
00329 
00330         input += 64;
00331         ilen  -= 64;
00332     }
00333 
00334     if( ilen > 0 )
00335         memcpy( (void *) (ctx->buffer  + left), input, ilen );
00336 
00337     return( 0 );
00338 }
00339 
00340 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
00341 void mbedtls_sha1_update( mbedtls_sha1_context *ctx,
00342                           const unsigned char *input,
00343                           size_t ilen )
00344 {
00345     mbedtls_sha1_update_ret( ctx, input, ilen );
00346 }
00347 #endif
00348 
00349 static const unsigned char sha1_padding[64] =
00350 {
00351  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00352     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00353     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
00354     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
00355 };
00356 
00357 /*
00358  * SHA-1 final digest
00359  */
00360 int mbedtls_sha1_finish_ret( mbedtls_sha1_context *ctx,
00361                              unsigned char output[20] )
00362 {
00363     int ret;
00364     uint32_t last, padn;
00365     uint32_t high, low;
00366     unsigned char msglen[8];
00367 
00368     high = ( ctx->total [0] >> 29 )
00369          | ( ctx->total [1] <<  3 );
00370     low  = ( ctx->total [0] <<  3 );
00371 
00372     PUT_UINT32_BE( high, msglen, 0 );
00373     PUT_UINT32_BE( low,  msglen, 4 );
00374 
00375     last = ctx->total [0] & 0x3F;
00376     padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
00377 
00378     if( ( ret = mbedtls_sha1_update_ret( ctx, sha1_padding, padn ) ) != 0 )
00379         return( ret );
00380     if( ( ret = mbedtls_sha1_update_ret( ctx, msglen, 8 ) ) != 0 )
00381         return( ret );
00382 
00383     PUT_UINT32_BE( ctx->state [0], output,  0 );
00384     PUT_UINT32_BE( ctx->state [1], output,  4 );
00385     PUT_UINT32_BE( ctx->state [2], output,  8 );
00386     PUT_UINT32_BE( ctx->state [3], output, 12 );
00387     PUT_UINT32_BE( ctx->state [4], output, 16 );
00388 
00389     return( 0 );
00390 }
00391 
00392 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
00393 void mbedtls_sha1_finish( mbedtls_sha1_context *ctx,
00394                           unsigned char output[20] )
00395 {
00396     mbedtls_sha1_finish_ret( ctx, output );
00397 }
00398 #endif
00399 
00400 #endif /* !MBEDTLS_SHA1_ALT */
00401 
00402 /*
00403  * output = SHA-1( input buffer )
00404  */
00405 int mbedtls_sha1_ret( const unsigned char *input,
00406                       size_t ilen,
00407                       unsigned char output[20] )
00408 {
00409     int ret;
00410     mbedtls_sha1_context ctx;
00411 
00412     mbedtls_sha1_init( &ctx );
00413 
00414     if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
00415         goto exit;
00416 
00417     if( ( ret = mbedtls_sha1_update_ret( &ctx, input, ilen ) ) != 0 )
00418         goto exit;
00419 
00420     if( ( ret = mbedtls_sha1_finish_ret( &ctx, output ) ) != 0 )
00421         goto exit;
00422 
00423 exit:
00424     mbedtls_sha1_free( &ctx );
00425 
00426     return( ret );
00427 }
00428 
00429 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
00430 void mbedtls_sha1( const unsigned char *input,
00431                    size_t ilen,
00432                    unsigned char output[20] )
00433 {
00434     mbedtls_sha1_ret( input, ilen, output );
00435 }
00436 #endif
00437 
00438 #if defined(MBEDTLS_SELF_TEST)
00439 /*
00440  * FIPS-180-1 test vectors
00441  */
00442 static const unsigned char sha1_test_buf[3][57] =
00443 {
00444     { "abc" },
00445     { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
00446     { "" }
00447 };
00448 
00449 static const size_t sha1_test_buflen[3] =
00450 {
00451     3, 56, 1000
00452 };
00453 
00454 static const unsigned char sha1_test_sum[3][20] =
00455 {
00456     { 0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A, 0xBA, 0x3E,
00457       0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C, 0x9C, 0xD0, 0xD8, 0x9D },
00458     { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E, 0xBA, 0xAE,
00459       0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5, 0xE5, 0x46, 0x70, 0xF1 },
00460     { 0x34, 0xAA, 0x97, 0x3C, 0xD4, 0xC4, 0xDA, 0xA4, 0xF6, 0x1E,
00461       0xEB, 0x2B, 0xDB, 0xAD, 0x27, 0x31, 0x65, 0x34, 0x01, 0x6F }
00462 };
00463 
00464 /*
00465  * Checkup routine
00466  */
00467 int mbedtls_sha1_self_test( int verbose )
00468 {
00469     int i, j, buflen, ret = 0;
00470     unsigned char buf[1024];
00471     unsigned char sha1sum[20];
00472     mbedtls_sha1_context ctx;
00473 
00474     mbedtls_sha1_init( &ctx );
00475 
00476     /*
00477      * SHA-1
00478      */
00479     for( i = 0; i < 3; i++ )
00480     {
00481         if( verbose != 0 )
00482             mbedtls_printf( "  SHA-1 test #%d: ", i + 1 );
00483 
00484         if( ( ret = mbedtls_sha1_starts_ret( &ctx ) ) != 0 )
00485             goto fail;
00486 
00487         if( i == 2 )
00488         {
00489             memset( buf, 'a', buflen = 1000 );
00490 
00491             for( j = 0; j < 1000; j++ )
00492             {
00493                 ret = mbedtls_sha1_update_ret( &ctx, buf, buflen );
00494                 if( ret != 0 )
00495                     goto fail;
00496             }
00497         }
00498         else
00499         {
00500             ret = mbedtls_sha1_update_ret( &ctx, sha1_test_buf[i],
00501                                            sha1_test_buflen[i] );
00502             if( ret != 0 )
00503                 goto fail;
00504         }
00505 
00506         if( ( ret = mbedtls_sha1_finish_ret( &ctx, sha1sum ) ) != 0 )
00507             goto fail;
00508 
00509         if( memcmp( sha1sum, sha1_test_sum[i], 20 ) != 0 )
00510         {
00511             ret = 1;
00512             goto fail;
00513         }
00514 
00515         if( verbose != 0 )
00516             mbedtls_printf( "passed\n" );
00517     }
00518 
00519     if( verbose != 0 )
00520         mbedtls_printf( "\n" );
00521 
00522     goto exit;
00523 
00524 fail:
00525     if( verbose != 0 )
00526         mbedtls_printf( "failed\n" );
00527 
00528 exit:
00529     mbedtls_sha1_free( &ctx );
00530 
00531     return( ret );
00532 }
00533 
00534 #endif /* MBEDTLS_SELF_TEST */
00535 
00536 #endif /* MBEDTLS_SHA1_C */