Guest User

sha1.c

a guest
May 30th, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.00 KB | None | 0 0
  1. /*
  2.  * FIPS 180-1 compliant SHA-1 implementation,
  3.  * this program is licensed under the GPL.
  4.  */
  5.  
  6. #include <string.h>
  7. #include "sha1.h"
  8.  
  9. #define GET_UINT32(n,b,i)                       \
  10. {                                               \
  11.     (n) = ( (uint32) (b)[(i)    ] << 24 )       \
  12.         | ( (uint32) (b)[(i) + 1] << 16 )       \
  13.         | ( (uint32) (b)[(i) + 2] <<  8 )       \
  14.         | ( (uint32) (b)[(i) + 3]       );      \
  15. }
  16.  
  17. #define PUT_UINT32(n,b,i)                       \
  18. {                                               \
  19.     (b)[(i)    ] = (uint8) ( (n) >> 24 );       \
  20.     (b)[(i) + 1] = (uint8) ( (n) >> 16 );       \
  21.     (b)[(i) + 2] = (uint8) ( (n) >>  8 );       \
  22.     (b)[(i) + 3] = (uint8) ( (n)       );       \
  23. }
  24.  
  25. void sha1_starts( struct sha1_context *ctx )
  26. {
  27.     ctx->total[0] = 0;
  28.     ctx->total[1] = 0;
  29.     ctx->state[0] = 0x67452301;
  30.     ctx->state[1] = 0xEFCDAB89;
  31.     ctx->state[2] = 0x98BADCFE;
  32.     ctx->state[3] = 0x10325476;
  33.     ctx->state[4] = 0xC3D2E1F0;
  34. }
  35.  
  36. void sha1_process( struct sha1_context *ctx, uint8 data[64] )
  37. {
  38.     uint32 temp, A, B, C, D, E, W[16];
  39.  
  40.     GET_UINT32( W[0],  data,  0 );
  41.     GET_UINT32( W[1],  data,  4 );
  42.     GET_UINT32( W[2],  data,  8 );
  43.     GET_UINT32( W[3],  data, 12 );
  44.     GET_UINT32( W[4],  data, 16 );
  45.     GET_UINT32( W[5],  data, 20 );
  46.     GET_UINT32( W[6],  data, 24 );
  47.     GET_UINT32( W[7],  data, 28 );
  48.     GET_UINT32( W[8],  data, 32 );
  49.     GET_UINT32( W[9],  data, 36 );
  50.     GET_UINT32( W[10], data, 40 );
  51.     GET_UINT32( W[11], data, 44 );
  52.     GET_UINT32( W[12], data, 48 );
  53.     GET_UINT32( W[13], data, 52 );
  54.     GET_UINT32( W[14], data, 56 );
  55.     GET_UINT32( W[15], data, 60 );
  56.  
  57. #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
  58.  
  59. #define R(t)                                            \
  60. (                                                       \
  61.     temp = W[(t -  3) & 0x0F] ^ W[(t - 8) & 0x0F] ^     \
  62.            W[(t - 14) & 0x0F] ^ W[ t      & 0x0F],      \
  63.     ( W[t & 0x0F] = S(temp,1) )                         \
  64. )
  65.  
  66. #define P(a,b,c,d,e,x)                                  \
  67. {                                                       \
  68.     e += S(a,5) + F(b,c,d) + K + x; b = S(b,30);        \
  69. }
  70.  
  71.     A = ctx->state[0];
  72.     B = ctx->state[1];
  73.     C = ctx->state[2];
  74.     D = ctx->state[3];
  75.     E = ctx->state[4];
  76.  
  77. #define F(x,y,z) (z ^ (x & (y ^ z)))
  78. #define K 0x5A827999
  79.  
  80.     P( A, B, C, D, E, W[0]  );
  81.     P( E, A, B, C, D, W[1]  );
  82.     P( D, E, A, B, C, W[2]  );
  83.     P( C, D, E, A, B, W[3]  );
  84.     P( B, C, D, E, A, W[4]  );
  85.     P( A, B, C, D, E, W[5]  );
  86.     P( E, A, B, C, D, W[6]  );
  87.     P( D, E, A, B, C, W[7]  );
  88.     P( C, D, E, A, B, W[8]  );
  89.     P( B, C, D, E, A, W[9]  );
  90.     P( A, B, C, D, E, W[10] );
  91.     P( E, A, B, C, D, W[11] );
  92.     P( D, E, A, B, C, W[12] );
  93.     P( C, D, E, A, B, W[13] );
  94.     P( B, C, D, E, A, W[14] );
  95.     P( A, B, C, D, E, W[15] );
  96.     P( E, A, B, C, D, R(16) );
  97.     P( D, E, A, B, C, R(17) );
  98.     P( C, D, E, A, B, R(18) );
  99.     P( B, C, D, E, A, R(19) );
  100.  
  101. #undef K
  102. #undef F
  103.  
  104. #define F(x,y,z) (x ^ y ^ z)
  105. #define K 0x6ED9EBA1
  106.  
  107.     P( A, B, C, D, E, R(20) );
  108.     P( E, A, B, C, D, R(21) );
  109.     P( D, E, A, B, C, R(22) );
  110.     P( C, D, E, A, B, R(23) );
  111.     P( B, C, D, E, A, R(24) );
  112.     P( A, B, C, D, E, R(25) );
  113.     P( E, A, B, C, D, R(26) );
  114.     P( D, E, A, B, C, R(27) );
  115.     P( C, D, E, A, B, R(28) );
  116.     P( B, C, D, E, A, R(29) );
  117.     P( A, B, C, D, E, R(30) );
  118.     P( E, A, B, C, D, R(31) );
  119.     P( D, E, A, B, C, R(32) );
  120.     P( C, D, E, A, B, R(33) );
  121.     P( B, C, D, E, A, R(34) );
  122.     P( A, B, C, D, E, R(35) );
  123.     P( E, A, B, C, D, R(36) );
  124.     P( D, E, A, B, C, R(37) );
  125.     P( C, D, E, A, B, R(38) );
  126.     P( B, C, D, E, A, R(39) );
  127.  
  128. #undef K
  129. #undef F
  130.  
  131. #define F(x,y,z) ((x & y) | (z & (x | y)))
  132. #define K 0x8F1BBCDC
  133.  
  134.     P( A, B, C, D, E, R(40) );
  135.     P( E, A, B, C, D, R(41) );
  136.     P( D, E, A, B, C, R(42) );
  137.     P( C, D, E, A, B, R(43) );
  138.     P( B, C, D, E, A, R(44) );
  139.     P( A, B, C, D, E, R(45) );
  140.     P( E, A, B, C, D, R(46) );
  141.     P( D, E, A, B, C, R(47) );
  142.     P( C, D, E, A, B, R(48) );
  143.     P( B, C, D, E, A, R(49) );
  144.     P( A, B, C, D, E, R(50) );
  145.     P( E, A, B, C, D, R(51) );
  146.     P( D, E, A, B, C, R(52) );
  147.     P( C, D, E, A, B, R(53) );
  148.     P( B, C, D, E, A, R(54) );
  149.     P( A, B, C, D, E, R(55) );
  150.     P( E, A, B, C, D, R(56) );
  151.     P( D, E, A, B, C, R(57) );
  152.     P( C, D, E, A, B, R(58) );
  153.     P( B, C, D, E, A, R(59) );
  154.  
  155. #undef K
  156. #undef F
  157.  
  158. #define F(x,y,z) (x ^ y ^ z)
  159. #define K 0xCA62C1D6
  160.  
  161.     P( A, B, C, D, E, R(60) );
  162.     P( E, A, B, C, D, R(61) );
  163.     P( D, E, A, B, C, R(62) );
  164.     P( C, D, E, A, B, R(63) );
  165.     P( B, C, D, E, A, R(64) );
  166.     P( A, B, C, D, E, R(65) );
  167.     P( E, A, B, C, D, R(66) );
  168.     P( D, E, A, B, C, R(67) );
  169.     P( C, D, E, A, B, R(68) );
  170.     P( B, C, D, E, A, R(69) );
  171.     P( A, B, C, D, E, R(70) );
  172.     P( E, A, B, C, D, R(71) );
  173.     P( D, E, A, B, C, R(72) );
  174.     P( C, D, E, A, B, R(73) );
  175.     P( B, C, D, E, A, R(74) );
  176.     P( A, B, C, D, E, R(75) );
  177.     P( E, A, B, C, D, R(76) );
  178.     P( D, E, A, B, C, R(77) );
  179.     P( C, D, E, A, B, R(78) );
  180.     P( B, C, D, E, A, R(79) );
  181.  
  182. #undef K
  183. #undef F
  184.  
  185.     ctx->state[0] += A;
  186.     ctx->state[1] += B;
  187.     ctx->state[2] += C;
  188.     ctx->state[3] += D;
  189.     ctx->state[4] += E;
  190. }
  191.  
  192. void sha1_update( struct sha1_context *ctx, uint8 *input, uint32 length )
  193. {
  194.     uint32 left, fill;
  195.  
  196.     if( ! length ) return;
  197.  
  198.     left = ( ctx->total[0] >> 3 ) & 0x3F;
  199.     fill = 64 - left;
  200.  
  201.     ctx->total[0] += length <<  3;
  202.     ctx->total[1] += length >> 29;
  203.  
  204.     ctx->total[0] &= 0xFFFFFFFF;
  205.     ctx->total[1] += ctx->total[0] < ( length << 3 );
  206.  
  207.     if( left && length >= fill )
  208.     {
  209.         memcpy( (void *) (ctx->buffer + left), (void *) input, fill );
  210.         sha1_process( ctx, ctx->buffer );
  211.         length -= fill;
  212.         input  += fill;
  213.         left = 0;
  214.     }
  215.  
  216.     while( length >= 64 )
  217.     {
  218.         sha1_process( ctx, input );
  219.         length -= 64;
  220.         input  += 64;
  221.     }
  222.  
  223.     if( length )
  224.     {
  225.         memcpy( (void *) (ctx->buffer + left), (void *) input, length );
  226.     }
  227. }
  228.  
  229. static uint8 sha1_padding[64] =
  230. {
  231.  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  232.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  233.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  234.     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  235. };
  236.  
  237. void sha1_finish( struct sha1_context *ctx, uint8 digest[20] )
  238. {
  239.     uint32 last, padn;
  240.     uint8 msglen[8];
  241.  
  242.     PUT_UINT32( ctx->total[1], msglen, 0 );
  243.     PUT_UINT32( ctx->total[0], msglen, 4 );
  244.  
  245.     last = ( ctx->total[0] >> 3 ) & 0x3F;
  246.     padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
  247.  
  248.     sha1_update( ctx, sha1_padding, padn );
  249.     sha1_update( ctx, msglen, 8 );
  250.  
  251.     PUT_UINT32( ctx->state[0], digest,  0 );
  252.     PUT_UINT32( ctx->state[1], digest,  4 );
  253.     PUT_UINT32( ctx->state[2], digest,  8 );
  254.     PUT_UINT32( ctx->state[3], digest, 12 );
  255.     PUT_UINT32( ctx->state[4], digest, 16 );
  256. }
  257.  
  258. #ifdef TEST
  259.  
  260. #include <stdlib.h>
  261. #include <stdio.h>
  262.  
  263. /*
  264.  * those are the standard FIPS 180-1 test vectors
  265.  */
  266.  
  267. static char *msg[] =
  268. {
  269.     "abc",
  270.     "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  271.     NULL
  272. };
  273.  
  274. static char *val[] =
  275. {
  276.     "a9993e364706816aba3e25717850c26c9cd0d89d",
  277.     "84983e441c3bd26ebaae4aa1f95129e5e54670f1",
  278.     "34aa973cd4c4daa4f61eeb2bdbad27316534016f"
  279. };
  280.  
  281. int main( int argc, char *argv[] )
  282. {
  283.     FILE *f;
  284.     int i, j;
  285.     char output[41];
  286.     struct sha1_context ctx;
  287.     unsigned char sha1sum[20], buffer[1000];
  288.  
  289.     if( argc < 2 )
  290.     {
  291.         for( i = 0; i < 3; i++ )
  292.         {
  293.             sha1_starts( &ctx );
  294.  
  295.             if( i < 2 )
  296.             {
  297.                 sha1_update( &ctx, (uint8 *) msg[i], strlen( msg[i] ) );
  298.             }
  299.             else
  300.             {
  301.                 memset( buffer, 'a', 1000 );
  302.  
  303.                 for( j = 0; j < 1000; j++ )
  304.                 {
  305.                     sha1_update( &ctx, (uint8 *) buffer, 1000 );
  306.                 }
  307.             }
  308.  
  309.             sha1_finish( &ctx, sha1sum );
  310.  
  311.             for( j = 0; j < 20; j++ )
  312.             {
  313.                 sprintf( output + j * 2, "%02x", sha1sum[j] );
  314.             }
  315.  
  316.             printf( "test %d ", i + 1 );
  317.  
  318.             if( ! memcmp( output, val[i], 40 ) )
  319.             {
  320.                 printf( "passed\n" );
  321.             }
  322.             else
  323.             {
  324.                 printf( "failed\n" );
  325.                 return( 1 );
  326.             }
  327.         }
  328.     }
  329.     else
  330.     {
  331.         if( ! ( f = fopen( argv[1], "rb" ) ) )
  332.         {
  333.             perror( "fopen" );
  334.             return( 1 );
  335.         }
  336.  
  337.         sha1_starts( &ctx );
  338.  
  339.         while( ( i = fread( buffer, 1, sizeof( buffer ), f ) ) > 0 )
  340.         {
  341.             sha1_update( &ctx, buffer, i );
  342.         }
  343.  
  344.         sha1_finish( &ctx, sha1sum );
  345.  
  346.         for( j = 0; j < 20; j++ )
  347.         {
  348.             printf( "%02x", sha1sum[j] );
  349.         }
  350.  
  351.         printf( "  %s\n", argv[1] );
  352.     }
  353.  
  354.     return( 0 );
  355. }
  356.  
  357. #endif
Add Comment
Please, Sign In to add comment