home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / crypl200.zip / HASH / SHACORE.C < prev   
Text File  |  1996-09-29  |  13KB  |  356 lines

  1. /****************************************************************************
  2. *                                                                            *
  3. *                            SHA Core Transformation                         *
  4. *                        Copyright Peter Gutmann 1992-1996                    *
  5. *                                                                            *
  6. ****************************************************************************/
  7.  
  8. /* The SHA f()-functions.  The f1 and f3 functions can be optimized to
  9.    save one boolean operation each - thanks to Rich Schroeppel
  10.    <rcs@cs.arizona.edu> for discovering this.  f3 was further optimized by
  11.    Colin Plumb <colin@nyx10.cs.du.edu> to produce code which uses one instead
  12.    of two temporary registers and can be scheduled in any order by the
  13.    compiler once it's part of the basic SHA sub-round */
  14.  
  15. /*#define f1(x,y,z)    ( ( x & y ) | ( ~x & z ) )            // Rounds  0-19 */
  16. #define f1(x,y,z)    ( z ^ ( x & ( y ^ z ) ) )            /* Rounds  0-19 */
  17. #define f2(x,y,z)    ( x ^ y ^ z )                        /* Rounds 20-39 */
  18. /*#define f3(x,y,z)    ( ( x & y ) | ( x & z ) | ( y & z ) )    // Rounds 40-59 */
  19. /*#define f3(x,y,z)    ( ( x & y ) | ( z & ( x | y ) ) )    // Rounds 40-59 */
  20. #define f3(x,y,z)    ( x & y ) + ( z & ( x ^ y ) )        /* Rounds 40-59 */
  21. #define f4(x,y,z)    ( x ^ y ^ z )                        /* Rounds 60-79 */
  22.  
  23. /* The SHA Mysterious Constants */
  24.  
  25. #define K1    0x5A827999UL                                /* Rounds  0-19 */
  26. #define K2    0x6ED9EBA1UL                                /* Rounds 20-39 */
  27. #define K3    0x8F1BBCDCUL                                /* Rounds 40-59 */
  28. #define K4    0xCA62C1D6UL                                /* Rounds 60-79 */
  29.  
  30. /* SHA initial values */
  31.  
  32. #define h0init    0x67452301UL
  33. #define h1init    0xEFCDAB89UL
  34. #define h2init    0x98BADCFEUL
  35. #define h3init    0x10325476UL
  36. #define h4init    0xC3D2E1F0UL
  37.  
  38. /* Note that it may be necessary to add parentheses to these macros if they
  39.    are to be called with expressions as arguments */
  40.  
  41. /* 32-bit rotate left - kludged with shifts unless we can tell the compiler
  42.    how to do it (some compilers like gcc get it right anyway) */
  43.  
  44. #ifdef __xxWIN32xx__
  45.   #define ROTL( n, X )    __asm rol X, n
  46. #else
  47.   #define ROTL( n, X )    ( ( ( X ) << n ) | ( ( X ) >> ( 32 - n ) ) )
  48. #endif /* __WIN32__ */
  49.  
  50. /* The initial expanding function.  The hash function is defined over an
  51.    80-word expanded input array W, where the first 16 are copies of the input
  52.    data, and the remaining 64 are defined by
  53.  
  54.         W[ i ] = W[ i - 16 ] ^ W[ i - 14 ] ^ W[ i - 8 ] ^ W[ i - 3 ]
  55.  
  56.    This implementation generates these values on the fly in a circular
  57.    buffer - thanks to Colin Plumb <colin@nyx10.cs.du.edu> for this
  58.    optimization.
  59.  
  60.    The updated SHA changes the expanding function by adding a rotate of 1
  61.    bit.  Thanks to Jim Gillogly, jim@rand.org, and an anonymous contributor
  62.    for this information */
  63.  
  64. #ifdef USE_SHA1
  65.   #define expand(W,i) ( W[ i & 15 ] = MASK32( ROTL( 1, ( W[ i & 15 ] ^ W[ i - 14 & 15 ] ^ \
  66.                                                          W[ i - 8 & 15 ] ^ W[ i - 3 & 15 ] ) ) ) )
  67. #else
  68.   #define expand(W,i) ( W[ i & 15 ] ^= W[ i - 14 & 15 ] ^ W[ i - 8 & 15 ] ^ W[ i - 3 & 15 ] )
  69. #endif /* USE_SHA1 */
  70.  
  71. /* The prototype SHA sub-round.  The fundamental sub-round is:
  72.  
  73.         a' = e + ROTL( 5, a ) + f( b, c, d ) + k + data;
  74.         b' = a;
  75.         c' = ROTL( 30, b );
  76.         d' = c;
  77.         e' = d;
  78.  
  79.    but this is implemented by unrolling the loop 5 times and renaming the
  80.    variables ( e, a, b, c, d ) = ( a', b', c', d', e' ) each iteration.
  81.    This code is then replicated 20 times for each of the 4 functions, using
  82.    the next 20 values from the W[] array each time */
  83.  
  84. #define subRound(a, b, c, d, e, f, k, data) \
  85.     e = MASK32( e + ROTL( 5, a ) + f( b, c, d ) + k + data ); \
  86.     b = MASK32( ROTL( 30, b ) )
  87.  
  88. #ifndef ASM_SHA
  89.  
  90. /* Perform the SHA transformation.  Note that this code, like MD5, seems to
  91.    break some optimizing compilers due to the complexity of the expressions
  92.    and the size of the basic block.  It may be necessary to split it into
  93.    sections, e.g. based on the four subrounds */
  94.  
  95. void SHATransform( LONG *digest, LONG *data )
  96.     {
  97.     LONG A, B, C, D, E;        /* Local vars */
  98.     LONG eData[ 16 ];        /* Expanded data */
  99.     int i;
  100.  
  101.     /* Set up first buffer and local data buffer */
  102.     A = digest[ 0 ];
  103.     B = digest[ 1 ];
  104.     C = digest[ 2 ];
  105.     D = digest[ 3 ];
  106.     E = digest[ 4 ];
  107.     for( i = 0; i < 16; i++ )
  108.         eData[ i ] = data[ i ];
  109.  
  110.     /* Heavy mangling, in 4 sub-rounds of 20 interations each. */
  111.     subRound( A, B, C, D, E, f1, K1, eData[  0 ] );
  112.     subRound( E, A, B, C, D, f1, K1, eData[  1 ] );
  113.     subRound( D, E, A, B, C, f1, K1, eData[  2 ] );
  114.     subRound( C, D, E, A, B, f1, K1, eData[  3 ] );
  115.     subRound( B, C, D, E, A, f1, K1, eData[  4 ] );
  116.     subRound( A, B, C, D, E, f1, K1, eData[  5 ] );
  117.     subRound( E, A, B, C, D, f1, K1, eData[  6 ] );
  118.     subRound( D, E, A, B, C, f1, K1, eData[  7 ] );
  119.     subRound( C, D, E, A, B, f1, K1, eData[  8 ] );
  120.     subRound( B, C, D, E, A, f1, K1, eData[  9 ] );
  121.     subRound( A, B, C, D, E, f1, K1, eData[ 10 ] );
  122.     subRound( E, A, B, C, D, f1, K1, eData[ 11 ] );
  123.     subRound( D, E, A, B, C, f1, K1, eData[ 12 ] );
  124.     subRound( C, D, E, A, B, f1, K1, eData[ 13 ] );
  125.     subRound( B, C, D, E, A, f1, K1, eData[ 14 ] );
  126.     subRound( A, B, C, D, E, f1, K1, eData[ 15 ] );
  127.     subRound( E, A, B, C, D, f1, K1, expand( eData, 16 ) );
  128.     subRound( D, E, A, B, C, f1, K1, expand( eData, 17 ) );
  129.     subRound( C, D, E, A, B, f1, K1, expand( eData, 18 ) );
  130.     subRound( B, C, D, E, A, f1, K1, expand( eData, 19 ) );
  131.  
  132.     subRound( A, B, C, D, E, f2, K2, expand( eData, 20 ) );
  133.     subRound( E, A, B, C, D, f2, K2, expand( eData, 21 ) );
  134.     subRound( D, E, A, B, C, f2, K2, expand( eData, 22 ) );
  135.     subRound( C, D, E, A, B, f2, K2, expand( eData, 23 ) );
  136.     subRound( B, C, D, E, A, f2, K2, expand( eData, 24 ) );
  137.     subRound( A, B, C, D, E, f2, K2, expand( eData, 25 ) );
  138.     subRound( E, A, B, C, D, f2, K2, expand( eData, 26 ) );
  139.     subRound( D, E, A, B, C, f2, K2, expand( eData, 27 ) );
  140.     subRound( C, D, E, A, B, f2, K2, expand( eData, 28 ) );
  141.     subRound( B, C, D, E, A, f2, K2, expand( eData, 29 ) );
  142.     subRound( A, B, C, D, E, f2, K2, expand( eData, 30 ) );
  143.     subRound( E, A, B, C, D, f2, K2, expand( eData, 31 ) );
  144.     subRound( D, E, A, B, C, f2, K2, expand( eData, 32 ) );
  145.     subRound( C, D, E, A, B, f2, K2, expand( eData, 33 ) );
  146.     subRound( B, C, D, E, A, f2, K2, expand( eData, 34 ) );
  147.     subRound( A, B, C, D, E, f2, K2, expand( eData, 35 ) );
  148.     subRound( E, A, B, C, D, f2, K2, expand( eData, 36 ) );
  149.     subRound( D, E, A, B, C, f2, K2, expand( eData, 37 ) );
  150.     subRound( C, D, E, A, B, f2, K2, expand( eData, 38 ) );
  151.     subRound( B, C, D, E, A, f2, K2, expand( eData, 39 ) );
  152.  
  153.     subRound( A, B, C, D, E, f3, K3, expand( eData, 40 ) );
  154.     subRound( E, A, B, C, D, f3, K3, expand( eData, 41 ) );
  155.     subRound( D, E, A, B, C, f3, K3, expand( eData, 42 ) );
  156.     subRound( C, D, E, A, B, f3, K3, expand( eData, 43 ) );
  157.     subRound( B, C, D, E, A, f3, K3, expand( eData, 44 ) );
  158.     subRound( A, B, C, D, E, f3, K3, expand( eData, 45 ) );
  159.     subRound( E, A, B, C, D, f3, K3, expand( eData, 46 ) );
  160.     subRound( D, E, A, B, C, f3, K3, expand( eData, 47 ) );
  161.     subRound( C, D, E, A, B, f3, K3, expand( eData, 48 ) );
  162.     subRound( B, C, D, E, A, f3, K3, expand( eData, 49 ) );
  163.     subRound( A, B, C, D, E, f3, K3, expand( eData, 50 ) );
  164.     subRound( E, A, B, C, D, f3, K3, expand( eData, 51 ) );
  165.     subRound( D, E, A, B, C, f3, K3, expand( eData, 52 ) );
  166.     subRound( C, D, E, A, B, f3, K3, expand( eData, 53 ) );
  167.     subRound( B, C, D, E, A, f3, K3, expand( eData, 54 ) );
  168.     subRound( A, B, C, D, E, f3, K3, expand( eData, 55 ) );
  169.     subRound( E, A, B, C, D, f3, K3, expand( eData, 56 ) );
  170.     subRound( D, E, A, B, C, f3, K3, expand( eData, 57 ) );
  171.     subRound( C, D, E, A, B, f3, K3, expand( eData, 58 ) );
  172.     subRound( B, C, D, E, A, f3, K3, expand( eData, 59 ) );
  173.  
  174.     subRound( A, B, C, D, E, f4, K4, expand( eData, 60 ) );
  175.     subRound( E, A, B, C, D, f4, K4, expand( eData, 61 ) );
  176.     subRound( D, E, A, B, C, f4, K4, expand( eData, 62 ) );
  177.     subRound( C, D, E, A, B, f4, K4, expand( eData, 63 ) );
  178.     subRound( B, C, D, E, A, f4, K4, expand( eData, 64 ) );
  179.     subRound( A, B, C, D, E, f4, K4, expand( eData, 65 ) );
  180.     subRound( E, A, B, C, D, f4, K4, expand( eData, 66 ) );
  181.     subRound( D, E, A, B, C, f4, K4, expand( eData, 67 ) );
  182.     subRound( C, D, E, A, B, f4, K4, expand( eData, 68 ) );
  183.     subRound( B, C, D, E, A, f4, K4, expand( eData, 69 ) );
  184.     subRound( A, B, C, D, E, f4, K4, expand( eData, 70 ) );
  185.     subRound( E, A, B, C, D, f4, K4, expand( eData, 71 ) );
  186.     subRound( D, E, A, B, C, f4, K4, expand( eData, 72 ) );
  187.     subRound( C, D, E, A, B, f4, K4, expand( eData, 73 ) );
  188.     subRound( B, C, D, E, A, f4, K4, expand( eData, 74 ) );
  189.     subRound( A, B, C, D, E, f4, K4, expand( eData, 75 ) );
  190.     subRound( E, A, B, C, D, f4, K4, expand( eData, 76 ) );
  191.     subRound( D, E, A, B, C, f4, K4, expand( eData, 77 ) );
  192.     subRound( C, D, E, A, B, f4, K4, expand( eData, 78 ) );
  193.     subRound( B, C, D, E, A, f4, K4, expand( eData, 79 ) );
  194.  
  195.     /* Build message digest */
  196.     digest[ 0 ] = MASK32( digest[ 0 ] + A );
  197.     digest[ 1 ] = MASK32( digest[ 1 ] + B );
  198.     digest[ 2 ] = MASK32( digest[ 2 ] + C );
  199.     digest[ 3 ] = MASK32( digest[ 3 ] + D );
  200.     digest[ 4 ] = MASK32( digest[ 4 ] + E );
  201.     }
  202. #else
  203.   void SHATransform( LONG *digest, LONG *data );
  204. #endif /* !ASM_SHA */
  205.  
  206. /****************************************************************************
  207. *                                                                            *
  208. *                            SHA Support Routines                            *
  209. *                                                                            *
  210. ****************************************************************************/
  211.  
  212. /* Initialize the SHA values */
  213.  
  214. void shaInitial( SHA_INFO *shaInfo )
  215.     {
  216.     /* Clear all fields */
  217.     memset( shaInfo, 0, sizeof( SHA_INFO ) );
  218.  
  219.     /* Set the h-vars to their initial values */
  220.     shaInfo->digest[ 0 ] = h0init;
  221.     shaInfo->digest[ 1 ] = h1init;
  222.     shaInfo->digest[ 2 ] = h2init;
  223.     shaInfo->digest[ 3 ] = h3init;
  224.     shaInfo->digest[ 4 ] = h4init;
  225.  
  226.     /* Initialise bit count */
  227.     shaInfo->countLo = shaInfo->countHi = 0;
  228.     }
  229.  
  230. /* Update SHA for a block of data */
  231.  
  232. void shaUpdate( SHA_INFO *shaInfo, BYTE *buffer, int count )
  233.     {
  234.     LONG tmp;
  235.     int dataCount;
  236.  
  237.     /* Update bitcount */
  238.     tmp = shaInfo->countLo;
  239.     if ( ( shaInfo->countLo = tmp + ( ( LONG ) count << 3 ) ) < tmp )
  240.         shaInfo->countHi++;                /* Carry from low to high */
  241.     shaInfo->countHi += count >> 29;
  242.  
  243.     /* Get count of bytes already in data */
  244.     dataCount = ( int ) ( tmp >> 3 ) & 0x3F;
  245.  
  246.     /* Handle any leading odd-sized chunks */
  247.     if( dataCount )
  248.         {
  249. #ifdef _BIG_WORDS
  250.         BYTE *p = shaInfo->dataBuffer + dataCount;
  251. #else
  252.         BYTE *p = ( BYTE * ) shaInfo->data + dataCount;
  253. #endif /* _BIG_WORDS */
  254.  
  255.         dataCount = SHA_DATASIZE - dataCount;
  256.         if( count < dataCount )
  257.             {
  258.             memcpy( p, buffer, count );
  259.             return;
  260.             }
  261.         memcpy( p, buffer, dataCount );
  262. #ifdef _BIG_WORDS
  263.         copyToBLong( shaInfo->data, shaInfo->dataBuffer, SHA_DATASIZE );
  264. #else
  265.         bigToLittleLong( shaInfo->data, SHA_DATASIZE );
  266. #endif /* _BIG_WORDS */
  267.         SHATransform( shaInfo->digest, shaInfo->data );
  268.         buffer += dataCount;
  269.         count -= dataCount;
  270.         }
  271.  
  272.     /* Process data in SHA_DATASIZE chunks */
  273.     while( count >= SHA_DATASIZE )
  274.         {
  275. #ifdef _BIG_WORDS
  276.         memcpy( shaInfo->dataBuffer, buffer, SHA_DATASIZE );
  277.         copyToBLong( shaInfo->data, shaInfo->dataBuffer, SHA_DATASIZE );
  278. #else
  279.         memcpy( shaInfo->data, buffer, SHA_DATASIZE );
  280.         bigToLittleLong( shaInfo->data, SHA_DATASIZE );
  281. #endif /* _BIG_WORDS */
  282.         SHATransform( shaInfo->digest, shaInfo->data );
  283.         buffer += SHA_DATASIZE;
  284.         count -= SHA_DATASIZE;
  285.         }
  286.  
  287.     /* Handle any remaining bytes of data. */
  288. #ifdef _BIG_WORDS
  289.     memcpy( shaInfo->dataBuffer, buffer, count );
  290. #else
  291.     memcpy( shaInfo->data, buffer, count );
  292. #endif /* _BIG_WORDS */
  293.     }
  294.  
  295. /* Final wrapup - pad to SHA_DATASIZE-byte boundary with the bit pattern
  296.    1 0* (64-bit count of bits processed, MSB-first) */
  297.  
  298. void shaFinal( SHA_INFO *shaInfo )
  299.     {
  300.     int count;
  301.     BYTE *dataPtr;
  302.  
  303.     /* Compute number of bytes mod 64 */
  304.     count = ( int ) shaInfo->countLo;
  305.     count = ( count >> 3 ) & 0x3F;
  306.  
  307.     /* Set the first char of padding to 0x80.  This is safe since there is
  308.        always at least one byte free */
  309. #ifdef _BIG_WORDS
  310.     dataPtr = shaInfo->dataBuffer + count;
  311. #else
  312.     dataPtr = ( BYTE * ) shaInfo->data + count;
  313. #endif /* _BIG_WORDS */
  314.     *dataPtr++ = 0x80;
  315.  
  316.     /* Bytes of padding needed to make 64 bytes */
  317.     count = SHA_DATASIZE - 1 - count;
  318.  
  319.     /* Pad out to 56 mod 64 */
  320.     if( count < 8 )
  321.         {
  322.         /* Two lots of padding:  Pad the first block to 64 bytes */
  323.         memset( dataPtr, 0, count );
  324. #ifdef _BIG_WORDS
  325.         copyToBLong( shaInfo->data, shaInfo->dataBuffer, SHA_DATASIZE );
  326. #else
  327.         bigToLittleLong( shaInfo->data, SHA_DATASIZE );
  328. #endif /* _BIG_WORDS */
  329.         SHATransform( shaInfo->digest, shaInfo->data );
  330.  
  331.         /* Now fill the next block with 56 bytes */
  332. #ifdef _BIG_WORDS
  333.         memset( shaInfo->dataBuffer, 0, SHA_DATASIZE - 8 );
  334. #else
  335.         memset( shaInfo->data, 0, SHA_DATASIZE - 8 );
  336. #endif /* _BIG_WORDS */
  337.         }
  338.     else
  339.         /* Pad block to 56 bytes */
  340.         memset( dataPtr, 0, count - 8 );
  341. #ifdef _BIG_WORDS
  342.     copyToBLong( shaInfo->data, shaInfo->dataBuffer, SHA_DATASIZE );
  343. #endif /* _BIG_WORDS */
  344.  
  345.     /* Append length in bits and transform */
  346.     shaInfo->data[ 14 ] = shaInfo->countHi;
  347.     shaInfo->data[ 15 ] = shaInfo->countLo;
  348.  
  349. #ifndef _BIG_WORDS
  350.     bigToLittleLong( shaInfo->data, SHA_DATASIZE - 8 );
  351. #endif /* _BIG_WORDS */
  352.     SHATransform( shaInfo->digest, shaInfo->data );
  353.  
  354.     shaInfo->done = TRUE;
  355.     }
  356.