home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / crypl200.zip / LIB_MD5.C < prev    next >
Text File  |  1996-09-29  |  6KB  |  194 lines

  1. /****************************************************************************
  2. *                                                                            *
  3. *                            cryptlib MD5 Hash Routines                        *
  4. *                        Copyright Peter Gutmann 1992-1996                    *
  5. *                                                                            *
  6. ****************************************************************************/
  7.  
  8. #include <ctype.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include "crypt.h"
  14. #ifdef INC_ALL
  15.   #include "md5.h"
  16. #else
  17.   #include "hash/md5.h"
  18. #endif /* Compiler-specific includes */
  19.  
  20. /****************************************************************************
  21. *                                                                            *
  22. *                                MD5 Self-test Routines                        *
  23. *                                                                            *
  24. ****************************************************************************/
  25.  
  26. /* Test the MD5 output against the test vectors given in RFC 1321 */
  27.  
  28. void md5HashBuffer( void *hashInfo, BYTE *outBuffer, BYTE *inBuffer,
  29.                     int length, const HASH_STATE hashState );
  30.  
  31. static struct {
  32.     char *data;                        /* Data to hash */
  33.     int length;                        /* Length of data */
  34.     BYTE digest[ MD5_DIGESTSIZE ];    /* Digest of data */
  35.     } digestValues[] = {
  36.     { "", 0,
  37.       { 0xD4, 0x1D, 0x8C, 0xD9, 0x8F, 0x00, 0xB2, 0x04,
  38.         0xE9, 0x80, 0x09, 0x98, 0xEC, 0xF8, 0x42, 0x7E } },
  39.     { "a", 1,
  40.       { 0x0C, 0xC1, 0x75, 0xB9, 0xC0, 0xF1, 0xB6, 0xA8,
  41.         0x31, 0xC3, 0x99, 0xE2, 0x69, 0x77, 0x26, 0x61 } },
  42.     { "abc", 3,
  43.       { 0x90, 0x01, 0x50, 0x98, 0x3C, 0xD2, 0x4F, 0xB0,
  44.         0xD6, 0x96, 0x3F, 0x7D, 0x28, 0xE1, 0x7F, 0x72 } },
  45.     { "message digest", 14,
  46.       { 0xF9, 0x6B, 0x69, 0x7D, 0x7C, 0xB7, 0x93, 0x8D,
  47.         0x52, 0x5A, 0x2F, 0x31, 0xAA, 0xF1, 0x61, 0xD0 } },
  48.     { "abcdefghijklmnopqrstuvwxyz", 26,
  49.       { 0xC3, 0xFC, 0xD3, 0xD7, 0x61, 0x92, 0xE4, 0x00,
  50.         0x7D, 0xFB, 0x49, 0x6C, 0xCA, 0x67, 0xE1, 0x3B } },
  51.     { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62,
  52.       { 0xD1, 0x74, 0xAB, 0x98, 0xD2, 0x77, 0xD9, 0xF5,
  53.         0xA5, 0x61, 0x1C, 0x2C, 0x9F, 0x41, 0x9D, 0x9F } },
  54.     { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", 80,
  55.       { 0x57, 0xED, 0xF4, 0xA2, 0x2B, 0xE3, 0xC9, 0x55,
  56.         0xAC, 0x49, 0xDA, 0x2E, 0x21, 0x07, 0xB6, 0x7A } },
  57.     { NULL, 0, { 0 } }
  58.     };
  59.  
  60. int md5SelfTest( void )
  61.     {
  62.     BYTE digest[ MD5_DIGESTSIZE ];
  63.     int i;
  64.  
  65.     /* Test MD5 against the test vectors given in RFC 1320 */
  66.     for( i = 0; digestValues[ i ].data != NULL; i++ )
  67.         {
  68.         md5HashBuffer( NULL, digest, ( BYTE * ) digestValues[ i ].data,
  69.                        digestValues[ i ].length, HASH_ALL );
  70.         if( memcmp( digest, digestValues[ i ].digest, MD5_DIGESTSIZE ) )
  71.             return( CRYPT_SELFTEST );
  72.         }
  73.  
  74.     return( CRYPT_OK );
  75.     }
  76.  
  77. /****************************************************************************
  78. *                                                                            *
  79. *                            Init/Shutdown Routines                            *
  80. *                                                                            *
  81. ****************************************************************************/
  82.  
  83. /* Perform auxiliary init and shutdown actions on an encryption context */
  84.  
  85. int md5Init( CRYPT_INFO *cryptInfo )
  86.     {
  87.     int status;
  88.  
  89.     /* Allocate memory for the MD5 context within the encryption context */
  90.     if( cryptInfo->privateData != NULL )
  91.         return( CRYPT_INITED );
  92.     if( ( status = secureMalloc( &cryptInfo->privateData, sizeof( MD5_INFO ) ) ) != CRYPT_OK )
  93.         return( status );
  94.     md5Initial( ( MD5_INFO * ) cryptInfo->privateData );
  95.  
  96.     return( CRYPT_OK );
  97.     }
  98.  
  99. int md5End( CRYPT_INFO *cryptInfo )
  100.     {
  101.     /* Free any allocated memory */
  102.     secureFree( &cryptInfo->privateData );
  103.  
  104.     return( CRYPT_OK );
  105.     }
  106.  
  107. /****************************************************************************
  108. *                                                                            *
  109. *                                MD5 Hash Routines                            *
  110. *                                                                            *
  111. ****************************************************************************/
  112.  
  113. /* Hash data using MD5 */
  114.  
  115. int md5Hash( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  116.     {
  117.     MD5_INFO *md5Info = ( MD5_INFO * ) cryptInfo->privateData;
  118.  
  119.     /* If we've already called md5Final(), we can't continue */
  120.     if( md5Info->done )
  121.         return( CRYPT_COMPLETE );
  122.  
  123.     if( !noBytes )
  124.         md5Final( md5Info );
  125.     else
  126.         md5Update( md5Info, buffer, noBytes );
  127.  
  128.     return( CRYPT_OK );
  129.     }
  130.  
  131. /* Retrieve the hash value */
  132.  
  133. int md5GetData( CRYPT_INFO *cryptInfo, BYTE *buffer )
  134.     {
  135.     MD5_INFO *md5Info = ( MD5_INFO * ) cryptInfo->privateData;
  136.     int i;
  137.  
  138.     /* Extract the digest into the memory buffer */
  139.     for( i = 0; i < MD5_DIGESTSIZE / 4; i++ )
  140.         {
  141.         mputLLong( buffer, md5Info->digest[ i ] );
  142.         }
  143.  
  144.     return( ( md5Info->done ) ? CRYPT_OK : CRYPT_INCOMPLETE );
  145.     }
  146.  
  147. /* Internal API: Hash a single block of memory without the overhead of
  148.    creating an encryption context */
  149.  
  150. void md5HashBuffer( void *hashInfo, BYTE *outBuffer, BYTE *inBuffer,
  151.                     int length, const HASH_STATE hashState )
  152.     {
  153.     MD5_INFO *md5Info = ( MD5_INFO * ) hashInfo, md5InfoBuffer;
  154.     int i;
  155.  
  156.     /* If the user has left it up to us to allocate the hash context buffer,
  157.        use the internal buffer */
  158.     if( md5Info == NULL )
  159.         md5Info = &md5InfoBuffer;
  160.  
  161.     if( hashState == HASH_ALL )
  162.         {
  163.         md5Initial( md5Info );
  164.         md5Update( md5Info, inBuffer, length );
  165.         md5Final( md5Info );
  166.         for( i = 0; i < MD5_DIGESTSIZE / 4; i++ )
  167.             {
  168.             mputLLong( outBuffer, md5Info->digest[ i ] );
  169.             }
  170.         }
  171.     else
  172.         switch( hashState )
  173.             {
  174.             case HASH_START:
  175.                 md5Initial( md5Info );
  176.                 /* Drop through */
  177.  
  178.             case HASH_CONTINUE:
  179.                 md5Update( md5Info, inBuffer, length );
  180.                 break;
  181.  
  182.             case HASH_END:
  183.                 md5Update( md5Info, inBuffer, length );
  184.                 md5Final( md5Info );
  185.                 for( i = 0; i < MD5_DIGESTSIZE / 4; i++ )
  186.                     {
  187.                     mputLLong( outBuffer, md5Info->digest[ i ] );
  188.                     }
  189.             }
  190.  
  191.     /* Clean up */
  192.     zeroise( &md5InfoBuffer, sizeof( MD5_INFO ) );
  193.     }
  194.