Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

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