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

  1. /****************************************************************************
  2. *                                                                            *
  3. *                            cryptlib MD4 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 "md4.h"
  16. #else
  17.   #include "hash/md4.h"
  18. #endif /* Compiler-specific includes */
  19.  
  20. /****************************************************************************
  21. *                                                                            *
  22. *                                MD4 Self-test Routines                        *
  23. *                                                                            *
  24. ****************************************************************************/
  25.  
  26. /* Test the MD4 output against the test vectors given in RFC 1320 */
  27.  
  28. void md4HashBuffer( 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[ MD4_DIGESTSIZE ];    /* Digest of data */
  35.     } digestValues[] = {
  36.     { "", 0,
  37.       { 0x31, 0xD6, 0xCF, 0xE0, 0xD1, 0x6A, 0xE9, 0x31,
  38.         0xB7, 0x3C, 0x59, 0xD7, 0xE0, 0xC0, 0x89, 0xC0 } },
  39.     { "a", 1,
  40.       { 0xBD, 0xE5, 0x2C, 0xB3, 0x1D, 0xE3, 0x3E, 0x46,
  41.         0x24, 0x5E, 0x05, 0xFB, 0xDB, 0xD6, 0xFB, 0x24 } },
  42.     { "abc", 3,
  43.       { 0xA4, 0x48, 0x01, 0x7A, 0xAF, 0x21, 0xD8, 0x52,
  44.         0x5F, 0xC1, 0x0A, 0xE8, 0x7A, 0xA6, 0x72, 0x9D } },
  45.     { "message digest", 14,
  46.       { 0xD9, 0x13, 0x0A, 0x81, 0x64, 0x54, 0x9F, 0xE8,
  47.         0x18, 0x87, 0x48, 0x06, 0xE1, 0xC7, 0x01, 0x4B } },
  48.     { "abcdefghijklmnopqrstuvwxyz", 26,
  49.       { 0xD7, 0x9E, 0x1C, 0x30, 0x8A, 0xA5, 0xBB, 0xCD,
  50.         0xEE, 0xA8, 0xED, 0x63, 0xDF, 0x41, 0x2D, 0xA9 } },
  51.     { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62,
  52.       { 0x04, 0x3F, 0x85, 0x82, 0xF2, 0x41, 0xDB, 0x35,
  53.         0x1C, 0xE6, 0x27, 0xE1, 0x53, 0xE7, 0xF0, 0xE4 } },
  54.     { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", 80,
  55.       { 0xE3, 0x3B, 0x4D, 0xDC, 0x9C, 0x38, 0xF2, 0x19,
  56.         0x9C, 0x3E, 0x7B, 0x16, 0x4F, 0xCC, 0x05, 0x36 } },
  57.     { NULL, 0, { 0 } }
  58.     };
  59.  
  60. int md4SelfTest( void )
  61.     {
  62.     BYTE digest[ MD4_DIGESTSIZE ];
  63.     int i;
  64.  
  65.     /* Test MD4 against the test vectors given in RFC 1320 */
  66.     for( i = 0; digestValues[ i ].data != NULL; i++ )
  67.         {
  68.         md4HashBuffer( NULL, digest, ( BYTE * ) digestValues[ i ].data,
  69.                        digestValues[ i ].length, HASH_ALL );
  70.         if( memcmp( digest, digestValues[ i ].digest, MD4_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 md4Init( CRYPT_INFO *cryptInfo )
  86.     {
  87.     int status;
  88.  
  89.     /* Allocate memory for the MD4 context within the encryption context */
  90.     if( cryptInfo->privateData != NULL )
  91.         return( CRYPT_INITED );
  92.     if( ( status = secureMalloc( &cryptInfo->privateData, sizeof( MD4_INFO ) ) ) != CRYPT_OK )
  93.         return( status );
  94.     md4Initial( ( MD4_INFO * ) cryptInfo->privateData );
  95.  
  96.     return( CRYPT_OK );
  97.     }
  98.  
  99. int md4End( CRYPT_INFO *cryptInfo )
  100.     {
  101.     /* Free any allocated memory */
  102.     secureFree( &cryptInfo->privateData );
  103.  
  104.     return( CRYPT_OK );
  105.     }
  106.  
  107. /****************************************************************************
  108. *                                                                            *
  109. *                                MD4 Hash Routines                            *
  110. *                                                                            *
  111. ****************************************************************************/
  112.  
  113. /* Hash data using MD4 */
  114.  
  115. int md4Hash( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  116.     {
  117.     MD4_INFO *md4Info = ( MD4_INFO * ) cryptInfo->privateData;
  118.  
  119.     /* If we've already called md4Final(), we can't continue */
  120.     if( md4Info->done )
  121.         return( CRYPT_COMPLETE );
  122.  
  123.     if( !noBytes )
  124.         md4Final( md4Info );
  125.     else
  126.         md4Update( md4Info, buffer, noBytes );
  127.  
  128.     return( CRYPT_OK );
  129.     }
  130.  
  131. /* Retrieve the hash value */
  132.  
  133. int md4GetData( CRYPT_INFO *cryptInfo, BYTE *buffer )
  134.     {
  135.     MD4_INFO *md4Info = ( MD4_INFO * ) cryptInfo->privateData;
  136.     int i;
  137.  
  138.     /* Extract the digest into the memory buffer */
  139.     for( i = 0; i < MD4_DIGESTSIZE / 4; i++ )
  140.         {
  141.         mputLLong( buffer, md4Info->digest[ i ] );
  142.         }
  143.  
  144.     return( ( md4Info->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 md4HashBuffer( void *hashInfo, BYTE *outBuffer, BYTE *inBuffer,
  151.                     int length, const HASH_STATE hashState )
  152.     {
  153.     MD4_INFO *md4Info = ( MD4_INFO * ) hashInfo, md4InfoBuffer;
  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( md4Info == NULL )
  159.         md4Info = &md4InfoBuffer;
  160.  
  161.     if( hashState == HASH_ALL )
  162.         {
  163.         md4Initial( md4Info );
  164.         md4Update( md4Info, inBuffer, length );
  165.         md4Final( md4Info );
  166.         for( i = 0; i < MD4_DIGESTSIZE / 4; i++ )
  167.             {
  168.             mputLLong( outBuffer, md4Info->digest[ i ] );
  169.             }
  170.         }
  171.     else
  172.         switch( hashState )
  173.             {
  174.             case HASH_START:
  175.                 md4Initial( md4Info );
  176.                 /* Drop through */
  177.  
  178.             case HASH_CONTINUE:
  179.                 md4Update( md4Info, inBuffer, length );
  180.                 break;
  181.  
  182.             case HASH_END:
  183.                 md4Update( md4Info, inBuffer, length );
  184.                 md4Final( md4Info );
  185.                 for( i = 0; i < MD4_DIGESTSIZE / 4; i++ )
  186.                     {
  187.                     mputLLong( outBuffer, md4Info->digest[ i ] );
  188.                     }
  189.             }
  190.  
  191.     /* Clean up */
  192.     zeroise( &md4InfoBuffer, sizeof( MD4_INFO ) );
  193.     }
  194.