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

  1. /****************************************************************************
  2. *                                                                            *
  3. *                        cryptlib RC4 Encryption 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 "rc4.h"
  16. #else
  17.   #include "rc/rc4.h"
  18. #endif /* Compiler-specific includes */
  19.  
  20. /* The size of the expanded IDEA keys */
  21.  
  22. #define RC4_EXPANDED_KEYSIZE    sizeof( RC4KEY )
  23.  
  24. /****************************************************************************
  25. *                                                                            *
  26. *                                RC4 Self-test Routines                        *
  27. *                                                                            *
  28. ****************************************************************************/
  29.  
  30. #include "testrc4.h"
  31.  
  32. /* Test the RC4 code against the test vectors from the BSAFE2 implementation */
  33.  
  34. static int rc4Test( BYTE *key, int keySize,
  35.                     BYTE *plaintext, BYTE *ciphertext, int length )
  36.     {
  37.     BYTE temp[ 512 ];
  38.     RC4KEY rc4key;
  39.  
  40.     memcpy( temp, plaintext, length );
  41.     rc4ExpandKey( &rc4key, key, keySize );
  42.     rc4Crypt( &rc4key, temp, length );
  43.     if( memcmp( ciphertext, temp, length ) )
  44.         return( CRYPT_ERROR );
  45.  
  46.     return( CRYPT_OK );
  47.     }
  48.  
  49.  
  50. int rc4SelfTest( void )
  51.     {
  52.     /* The testing gets somewhat messy here because of the variable-length
  53.        arrays, which isn't normally a problem with the fixed-length keys
  54.        and data used in the block ciphers */
  55.     if( rc4Test( testRC4key1, sizeof( testRC4key1 ), testRC4plaintext1,
  56.                  testRC4ciphertext1, sizeof( testRC4plaintext1 ) ) != CRYPT_OK ||
  57.         rc4Test( testRC4key2, sizeof( testRC4key2 ), testRC4plaintext2,
  58.                  testRC4ciphertext2, sizeof( testRC4plaintext2 ) ) != CRYPT_OK ||
  59.         rc4Test( testRC4key3, sizeof( testRC4key3 ), testRC4plaintext3,
  60.                  testRC4ciphertext3, sizeof( testRC4plaintext3 ) ) != CRYPT_OK ||
  61.         rc4Test( testRC4key4, sizeof( testRC4key4 ), testRC4plaintext4,
  62.                  testRC4ciphertext4, sizeof( testRC4plaintext4 ) ) != CRYPT_OK ||
  63.         rc4Test( testRC4key5, sizeof( testRC4key5 ), testRC4plaintext5,
  64.                  testRC4ciphertext5, sizeof( testRC4plaintext5 ) ) != CRYPT_OK )
  65.         return( CRYPT_ERROR );
  66.  
  67.     return( CRYPT_OK );
  68.     }
  69.  
  70. /****************************************************************************
  71. *                                                                            *
  72. *                            Init/Shutdown Routines                            *
  73. *                                                                            *
  74. ****************************************************************************/
  75.  
  76. /* Perform auxiliary init and shutdown actions on an encryption context */
  77.  
  78. int rc4InitEx( CRYPT_INFO *cryptInfo, void *cryptInfoEx )
  79.     {
  80.     int status;
  81.  
  82.     UNUSED( cryptInfoEx );
  83.  
  84.     /* Allocate memory for the expanded key */
  85.     if( cryptInfo->key != NULL || cryptInfo->privateData != NULL )
  86.         return( CRYPT_INITED );
  87.     if( ( status = secureMalloc( &cryptInfo->key, RC4_EXPANDED_KEYSIZE ) ) != CRYPT_OK )
  88.         return( status );
  89.     cryptInfo->keyLength = RC4_EXPANDED_KEYSIZE;
  90.  
  91.     return( CRYPT_OK );
  92.     }
  93.  
  94. int rc4Init( CRYPT_INFO *cryptInfo )
  95.     {
  96.     /* Just pass the call through to the extended setup routine */
  97.     return( rc4InitEx( cryptInfo, NULL ) );
  98.     }
  99.  
  100. int rc4End( CRYPT_INFO *cryptInfo )
  101.     {
  102.     /* Free any allocated memory */
  103.     secureFree( &cryptInfo->key );
  104.  
  105.     return( CRYPT_OK );
  106.     }
  107.  
  108. /****************************************************************************
  109. *                                                                            *
  110. *                            RC4 En/Decryption Routines                        *
  111. *                                                                            *
  112. ****************************************************************************/
  113.  
  114. /* Encrypt/decrypt data.  Since RC4 is a stream cipher, encryption and
  115.    decryption are one and the same */
  116.  
  117. int rc4Encrypt( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  118.     {
  119.     rc4Crypt( ( RC4KEY * ) cryptInfo->key, buffer, noBytes );
  120.  
  121.     return( CRYPT_OK );
  122.     }
  123.  
  124. int rc4Decrypt( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  125.     {
  126.     rc4Crypt( ( RC4KEY * ) cryptInfo->key, buffer, noBytes );
  127.  
  128.     return( CRYPT_OK );
  129.     }
  130.  
  131. /****************************************************************************
  132. *                                                                            *
  133. *                            RC4 Key Management Routines                        *
  134. *                                                                            *
  135. ****************************************************************************/
  136.  
  137. /* Create an expanded RC4 key */
  138.  
  139. int rc4InitKey( CRYPT_INFO *cryptInfo )
  140.     {
  141.     rc4ExpandKey( ( RC4KEY * ) cryptInfo->key, cryptInfo->userKey,
  142.                   cryptInfo->userKeyLength );
  143.  
  144.     return( CRYPT_OK );
  145.     }
  146.