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

  1. /****************************************************************************
  2. *                                                                            *
  3. *                            cryptlib MD2 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 "md2.h"
  16. #else
  17.   #include "hash/md2.h"
  18. #endif /* Compiler-specific includes */
  19.  
  20. /****************************************************************************
  21. *                                                                            *
  22. *                                MD2 Self-test Routines                        *
  23. *                                                                            *
  24. ****************************************************************************/
  25.  
  26. /* Test the MD2 output against the test vectors given in RFC 1319 */
  27.  
  28. void md2HashBuffer( 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[ MD2_DIGESTSIZE ];    /* Digest of data */
  35.     } digestValues[] = {
  36.     { "", 0,
  37.       { 0x83, 0x50, 0xE5, 0xA3, 0xE2, 0x4C, 0x15, 0x3D,
  38.         0xF2, 0x27, 0x5C, 0x9F, 0x80, 0x69, 0x27, 0x73 } },
  39.     { "a", 1,
  40.       { 0x32, 0xEC, 0x01, 0xEC, 0x4A, 0x6D, 0xAC, 0x72,
  41.         0xC0, 0xAB, 0x96, 0xFB, 0x34, 0xC0, 0xB5, 0xD1 } },
  42.     { "abc", 3,
  43.       { 0xDA, 0x85, 0x3B, 0x0D, 0x3F, 0x88, 0xD9, 0x9B,
  44.         0x30, 0x28, 0x3A, 0x69, 0xE6, 0xDE, 0xD6, 0xBB } },
  45.     { "message digest", 14,
  46.       { 0xAB, 0x4F, 0x49, 0x6B, 0xFB, 0x2A, 0x53, 0x0B,
  47.         0x21, 0x9F, 0xF3, 0x30, 0x31, 0xFE, 0x06, 0xB0 } },
  48.     { "abcdefghijklmnopqrstuvwxyz", 26,
  49.       { 0x4E, 0x8D, 0xDF, 0xF3, 0x65, 0x02, 0x92, 0xAB,
  50.         0x5A, 0x41, 0x08, 0xC3, 0xAA, 0x47, 0x94, 0x0B } },
  51.     { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62,
  52.       { 0xDA, 0x33, 0xDE, 0xF2, 0xA4, 0x2D, 0xF1, 0x39,
  53.         0x75, 0x35, 0x28, 0x46, 0xC3, 0x03, 0x38, 0xCD } },
  54.     { "12345678901234567890123456789012345678901234567890123456789012345678901234567890", 80,
  55.       { 0xD5, 0x97, 0x6F, 0x79, 0xD8, 0x3D, 0x3A, 0x0D,
  56.         0xC9, 0x80, 0x6C, 0x3C, 0x66, 0xF3, 0xEF, 0xD8 } },
  57.     { NULL, 0, { 0 } }
  58.     };
  59.  
  60. int md2SelfTest( void )
  61.     {
  62.     BYTE digest[ MD2_DIGESTSIZE ];
  63.     int i;
  64.  
  65.     /* Test MD2 against the test vectors given in RFC 1319 */
  66.     for( i = 0; digestValues[ i ].data != NULL; i++ )
  67.         {
  68.         md2HashBuffer( NULL, digest, ( BYTE * ) digestValues[ i ].data,
  69.                        digestValues[ i ].length, HASH_ALL );
  70.         if( memcmp( digest, digestValues[ i ].digest, MD2_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 md2Init( CRYPT_INFO *cryptInfo )
  86.     {
  87.     int status;
  88.  
  89.     /* Allocate memory for the MD2 context within the encryption context */
  90.     if( cryptInfo->privateData != NULL )
  91.         return( CRYPT_INITED );
  92.     if( ( status = secureMalloc( &cryptInfo->privateData, sizeof( MD2_INFO ) ) ) != CRYPT_OK )
  93.         return( status );
  94.     md2Initial( ( MD2_INFO * ) cryptInfo->privateData );
  95.  
  96.     return( CRYPT_OK );
  97.     }
  98.  
  99. int md2End( CRYPT_INFO *cryptInfo )
  100.     {
  101.     /* Free any allocated memory */
  102.     secureFree( &cryptInfo->privateData );
  103.  
  104.     return( CRYPT_OK );
  105.     }
  106.  
  107. /****************************************************************************
  108. *                                                                            *
  109. *                                MD2 Hash Routines                            *
  110. *                                                                            *
  111. ****************************************************************************/
  112.  
  113. /* Hash data using MD2 */
  114.  
  115. int md2Hash( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  116.     {
  117.     MD2_INFO *md2Info = ( MD2_INFO * ) cryptInfo->privateData;
  118.  
  119.     /* If we've already called md2Final(), we can't continue */
  120.     if( md2Info->done )
  121.         return( CRYPT_COMPLETE );
  122.  
  123.     if( !noBytes )
  124.         md2Final( md2Info );
  125.     else
  126.         md2Update( md2Info, buffer, noBytes );
  127.  
  128.     return( CRYPT_OK );
  129.     }
  130.  
  131. /* Retrieve the hash value */
  132.  
  133. int md2GetData( CRYPT_INFO *cryptInfo, BYTE *buffer )
  134.     {
  135.     MD2_INFO *md2Info = ( MD2_INFO * ) cryptInfo->privateData;
  136.  
  137.     /* Extract the digest into the memory buffer */
  138.     memcpy( buffer, md2Info->state, MD2_DIGESTSIZE );
  139.  
  140.     return( ( md2Info->done ) ? CRYPT_OK : CRYPT_INCOMPLETE );
  141.     }
  142.  
  143. /* Internal API: Hash a single block of memory without the overhead of
  144.    creating an encryption context */
  145.  
  146. void md2HashBuffer( void *hashInfo, BYTE *outBuffer, BYTE *inBuffer,
  147.                     int length, const HASH_STATE hashState )
  148.     {
  149.     MD2_INFO *md2Info = ( MD2_INFO * ) hashInfo, md2InfoBuffer;
  150.  
  151.     /* If the user has left it up to us to allocate the hash context buffer,
  152.        use the internal buffer */
  153.     if( md2Info == NULL )
  154.         md2Info = &md2InfoBuffer;
  155.  
  156.     if( hashState == HASH_ALL )
  157.         {
  158.         md2Initial( md2Info );
  159.         md2Update( md2Info, inBuffer, length );
  160.         md2Final( md2Info );
  161.         memcpy( outBuffer, md2Info->state, MD2_DIGESTSIZE );
  162.         }
  163.     else
  164.         switch( hashState )
  165.             {
  166.             case HASH_START:
  167.                 md2Initial( md2Info );
  168.                 /* Drop through */
  169.  
  170.             case HASH_CONTINUE:
  171.                 md2Update( md2Info, inBuffer, length );
  172.                 break;
  173.  
  174.             case HASH_END:
  175.                 md2Update( md2Info, inBuffer, length );
  176.                 md2Final( md2Info );
  177.                 memcpy( outBuffer, md2Info->state, MD2_DIGESTSIZE );
  178.             }
  179.  
  180.     /* Clean up */
  181.     zeroise( &md2InfoBuffer, sizeof( MD2_INFO ) );
  182.     }
  183.