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

  1. /****************************************************************************
  2. *                                                                            *
  3. *                        cryptlib RC2 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 "rc2.h"
  16. #else
  17.   #include "rc/rc2.h"
  18. #endif /* Compiler-specific includes */
  19.  
  20. /****************************************************************************
  21. *                                                                            *
  22. *                                RC2 Self-test Routines                        *
  23. *                                                                            *
  24. ****************************************************************************/
  25.  
  26. /* RC2 test vectors from RC2 specification */
  27.  
  28. static struct RC2_TEST {
  29.     BYTE key[ 16 ];
  30.     BYTE plainText[ 8 ];
  31.     BYTE cipherText[ 8 ];
  32.     } testRC2[] = {
  33.     { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  34.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  35.       { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  36.       { 0x1C, 0x19, 0x8A, 0x83, 0x8D, 0xF0, 0x28, 0xB7 } },
  37.     { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  38.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 },
  39.       { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  40.       { 0x21, 0x82, 0x9C, 0x78, 0xA9, 0xF9, 0xC0, 0x74 } },
  41.     { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42.         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  43.       { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
  44.       { 0x13, 0xDB, 0x35, 0x17, 0xD3, 0x21, 0x86, 0x9E } },
  45.     { { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  46.         0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
  47.       { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
  48.       { 0x50, 0xDC, 0x01, 0x62, 0xBD, 0x75, 0x7F, 0x31 } }
  49.     };
  50.  
  51. /* Test the RC2 code against the RC2 test vectors */
  52.  
  53. int rc2SelfTest( void )
  54.     {
  55.     BYTE temp[ RC2_BLOCKSIZE ];
  56.     RC2_KEY key;
  57.     int i;
  58.  
  59.     for( i = 0; i < sizeof( testRC2 ) / sizeof( struct RC2_TEST ); i++ )
  60.         {
  61.         memcpy( temp, testRC2[ i ].plainText, RC2_BLOCKSIZE );
  62.             rc2keyInit( &key, testRC2[ i ].key, 16 );
  63.         rc2encrypt( &key, temp );
  64.         if( memcmp( testRC2[ i ].cipherText, temp, RC2_BLOCKSIZE ) )
  65.             return( CRYPT_ERROR );
  66.         }
  67.  
  68.     return( CRYPT_OK );
  69.     }
  70.  
  71. /****************************************************************************
  72. *                                                                            *
  73. *                            Init/Shutdown Routines                            *
  74. *                                                                            *
  75. ****************************************************************************/
  76.  
  77. /* Perform auxiliary init and shutdown actions on an encryption context */
  78.  
  79. int rc2InitEx( CRYPT_INFO *cryptInfo, void *cryptInfoEx )
  80.     {
  81.     int status;
  82.  
  83.     UNUSED( cryptInfoEx );
  84.  
  85.     /* Allocate memory for the key and the algorithm-specific data within
  86.        the crypt context and set up any pointers we need */
  87.     if( cryptInfo->key != NULL )
  88.         return( CRYPT_INITED );
  89.     if( ( status = secureMalloc( &cryptInfo->key, sizeof( RC2_KEY ) ) ) != CRYPT_OK )
  90.         return( status );
  91.     cryptInfo->keyLength = sizeof( RC2_KEY );
  92.  
  93.     return( CRYPT_OK );
  94.     }
  95.  
  96. int rc2Init( CRYPT_INFO *cryptInfo )
  97.     {
  98.     /* Just pass through to the extended setup routine */
  99.     return( rc2InitEx( cryptInfo, NULL ) );
  100.     }
  101.  
  102. int rc2End( CRYPT_INFO *cryptInfo )
  103.     {
  104.     /* Free any allocated memory */
  105.     secureFree( &cryptInfo->key );
  106.  
  107.     return( CRYPT_OK );
  108.     }
  109.  
  110. /****************************************************************************
  111. *                                                                            *
  112. *                            RC2 En/Decryption Routines                        *
  113. *                                                                            *
  114. ****************************************************************************/
  115.  
  116. /* Encrypt/decrypt data in ECB mode */
  117.  
  118. int rc2EncryptECB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  119.     {
  120.     RC2_KEY *rc2Key = ( RC2_KEY * ) cryptInfo->key;
  121.     int blockCount = noBytes / RC2_BLOCKSIZE;
  122.  
  123.     /* Make sure the data length is a multiple of the block size */
  124.     if( noBytes % RC2_BLOCKSIZE )
  125.         return( CRYPT_BADPARM3 );
  126.  
  127.     while( blockCount-- )
  128.         {
  129.         /* Encrypt a block of data */
  130.         rc2encrypt( rc2Key, buffer );
  131.  
  132.         /* Move on to next block of data */
  133.         buffer += RC2_BLOCKSIZE;
  134.         }
  135.  
  136.     return( CRYPT_OK );
  137.     }
  138.  
  139. int rc2DecryptECB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  140.     {
  141.     RC2_KEY *rc2Key = ( RC2_KEY * ) cryptInfo->key;
  142.     int blockCount = noBytes / RC2_BLOCKSIZE;
  143.  
  144.     /* Make sure the data length is a multiple of the block size */
  145.     if( noBytes % RC2_BLOCKSIZE )
  146.         return( CRYPT_BADPARM3 );
  147.  
  148.     while( blockCount-- )
  149.         {
  150.         /* Decrypt a block of data */
  151.         rc2decrypt( rc2Key, buffer );
  152.  
  153.         /* Move on to next block of data */
  154.         buffer += RC2_BLOCKSIZE;
  155.         }
  156.  
  157.     return( CRYPT_OK );
  158.     }
  159.  
  160. /* Encrypt/decrypt data in CBC mode */
  161.  
  162. int rc2EncryptCBC( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  163.     {
  164.     RC2_KEY *rc2Key = ( RC2_KEY * ) cryptInfo->key;
  165.     int blockCount = noBytes / RC2_BLOCKSIZE;
  166.  
  167.     /* Make sure the data length is a multiple of the block size */
  168.     if( noBytes % RC2_BLOCKSIZE )
  169.         return( CRYPT_BADPARM3 );
  170.  
  171.     while( blockCount-- )
  172.         {
  173.         int i;
  174.  
  175.         /* XOR the buffer contents with the IV */
  176.         for( i = 0; i < RC2_BLOCKSIZE; i++ )
  177.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  178.  
  179.         /* Encrypt a block of data */
  180.         rc2encrypt( rc2Key, buffer );
  181.  
  182.         /* Shift ciphertext into IV */
  183.         memcpy( cryptInfo->currentIV, buffer, RC2_BLOCKSIZE );
  184.  
  185.         /* Move on to next block of data */
  186.         buffer += RC2_BLOCKSIZE;
  187.         }
  188.  
  189.     return( CRYPT_OK );
  190.     }
  191.  
  192. int rc2DecryptCBC( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  193.     {
  194.     RC2_KEY *rc2Key = ( RC2_KEY * ) cryptInfo->key;
  195.     BYTE temp[ RC2_BLOCKSIZE ];
  196.     int blockCount = noBytes / RC2_BLOCKSIZE;
  197.  
  198.     /* Make sure the data length is a multiple of the block size */
  199.     if( noBytes % RC2_BLOCKSIZE )
  200.         return( CRYPT_BADPARM3 );
  201.  
  202.     while( blockCount-- )
  203.         {
  204.         int i;
  205.  
  206.         /* Save the ciphertext */
  207.         memcpy( temp, buffer, RC2_BLOCKSIZE );
  208.  
  209.         /* Decrypt a block of data */
  210.         rc2decrypt( rc2Key, buffer );
  211.  
  212.         /* XOR the buffer contents with the IV */
  213.         for( i = 0; i < RC2_BLOCKSIZE; i++ )
  214.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  215.  
  216.         /* Shift the ciphertext into the IV */
  217.         memcpy( cryptInfo->currentIV, temp, RC2_BLOCKSIZE );
  218.  
  219.         /* Move on to next block of data */
  220.         buffer += RC2_BLOCKSIZE;
  221.         }
  222.  
  223.     /* Clear the temporary buffer */
  224.     zeroise( temp, RC2_BLOCKSIZE );
  225.  
  226.     return( CRYPT_OK );
  227.     }
  228.  
  229. /* Encrypt/decrypt data in CFB mode */
  230.  
  231. int rc2EncryptCFB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  232.     {
  233.     RC2_KEY *rc2Key = ( RC2_KEY * ) cryptInfo->key;
  234.     int i, ivCount = cryptInfo->ivCount;
  235.  
  236.     /* If there's any encrypted material left in the IV, use it now */
  237.     if( ivCount )
  238.         {
  239.         int bytesToUse;
  240.  
  241.         /* Find out how much material left in the encrypted IV we can use */
  242.         bytesToUse = RC2_BLOCKSIZE - ivCount;
  243.         if( noBytes < bytesToUse )
  244.             bytesToUse = noBytes;
  245.  
  246.         /* Encrypt the data */
  247.         for( i = 0; i < bytesToUse; i++ )
  248.             buffer[ i ] ^= cryptInfo->currentIV[ i + ivCount ];
  249.         memcpy( cryptInfo->currentIV + ivCount, buffer, bytesToUse );
  250.  
  251.         /* Adjust the byte count and buffer position */
  252.         noBytes -= bytesToUse;
  253.         buffer += bytesToUse;
  254.         ivCount += bytesToUse;
  255.         }
  256.  
  257.     while( noBytes )
  258.         {
  259.         ivCount = ( noBytes > RC2_BLOCKSIZE ) ? RC2_BLOCKSIZE : noBytes;
  260.  
  261.         /* Encrypt the IV */
  262.         rc2encrypt( rc2Key, cryptInfo->currentIV );
  263.  
  264.         /* XOR the buffer contents with the encrypted IV */
  265.         for( i = 0; i < ivCount; i++ )
  266.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  267.  
  268.         /* Shift the ciphertext into the IV */
  269.         memcpy( cryptInfo->currentIV, buffer, ivCount );
  270.  
  271.         /* Move on to next block of data */
  272.         noBytes -= ivCount;
  273.         buffer += ivCount;
  274.         }
  275.  
  276.     /* Remember how much of the IV is still available for use */
  277.     cryptInfo->ivCount = ( ivCount % RC2_BLOCKSIZE );
  278.  
  279.     return( CRYPT_OK );
  280.     }
  281.  
  282. /* Decrypt data in CFB mode.  Note that the transformation can be made
  283.    faster (but less clear) with temp = buffer, buffer ^= iv, iv = temp
  284.    all in one loop */
  285.  
  286. int rc2DecryptCFB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  287.     {
  288.     RC2_KEY *rc2Key = ( RC2_KEY * ) cryptInfo->key;
  289.     BYTE temp[ RC2_BLOCKSIZE ];
  290.     int i, ivCount = cryptInfo->ivCount;
  291.  
  292.     /* If there's any encrypted material left in the IV, use it now */
  293.     if( ivCount )
  294.         {
  295.         int bytesToUse;
  296.  
  297.         /* Find out how much material left in the encrypted IV we can use */
  298.         bytesToUse = RC2_BLOCKSIZE - ivCount;
  299.         if( noBytes < bytesToUse )
  300.             bytesToUse = noBytes;
  301.  
  302.         /* Decrypt the data */
  303.         memcpy( temp, buffer, bytesToUse );
  304.         for( i = 0; i < bytesToUse; i++ )
  305.             buffer[ i ] ^= cryptInfo->currentIV[ i + ivCount ];
  306.         memcpy( cryptInfo->currentIV + ivCount, temp, bytesToUse );
  307.  
  308.         /* Adjust the byte count and buffer position */
  309.         noBytes -= bytesToUse;
  310.         buffer += bytesToUse;
  311.         ivCount += bytesToUse;
  312.         }
  313.  
  314.     while( noBytes )
  315.         {
  316.         ivCount = ( noBytes > RC2_BLOCKSIZE ) ? RC2_BLOCKSIZE : noBytes;
  317.  
  318.         /* Encrypt the IV */
  319.         rc2encrypt( rc2Key, cryptInfo->currentIV );
  320.  
  321.         /* Save the ciphertext */
  322.         memcpy( temp, buffer, ivCount );
  323.  
  324.         /* XOR the buffer contents with the encrypted IV */
  325.         for( i = 0; i < ivCount; i++ )
  326.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  327.  
  328.         /* Shift the ciphertext into the IV */
  329.         memcpy( cryptInfo->currentIV, temp, ivCount );
  330.  
  331.         /* Move on to next block of data */
  332.         noBytes -= ivCount;
  333.         buffer += ivCount;
  334.         }
  335.  
  336.     /* Remember how much of the IV is still available for use */
  337.     cryptInfo->ivCount = ( ivCount % RC2_BLOCKSIZE );
  338.  
  339.     /* Clear the temporary buffer */
  340.     zeroise( temp, RC2_BLOCKSIZE );
  341.  
  342.     return( CRYPT_OK );
  343.     }
  344.  
  345. /* Encrypt/decrypt data in OFB mode */
  346.  
  347. int rc2EncryptOFB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  348.     {
  349.     RC2_KEY *rc2Key = ( RC2_KEY * ) cryptInfo->key;
  350.     int i, ivCount = cryptInfo->ivCount;
  351.  
  352.     /* If there's any encrypted material left in the IV, use it now */
  353.     if( ivCount )
  354.         {
  355.         int bytesToUse;
  356.  
  357.         /* Find out how much material left in the encrypted IV we can use */
  358.         bytesToUse = RC2_BLOCKSIZE - ivCount;
  359.         if( noBytes < bytesToUse )
  360.             bytesToUse = noBytes;
  361.  
  362.         /* Encrypt the data */
  363.         for( i = 0; i < bytesToUse; i++ )
  364.             buffer[ i ] ^= cryptInfo->currentIV[ i + ivCount ];
  365.  
  366.         /* Adjust the byte count and buffer position */
  367.         noBytes -= bytesToUse;
  368.         buffer += bytesToUse;
  369.         ivCount += bytesToUse;
  370.         }
  371.  
  372.     while( noBytes )
  373.         {
  374.         ivCount = ( noBytes > RC2_BLOCKSIZE ) ? RC2_BLOCKSIZE : noBytes;
  375.  
  376.         /* Encrypt the IV */
  377.         rc2encrypt( rc2Key, cryptInfo->currentIV );
  378.  
  379.         /* XOR the buffer contents with the encrypted IV */
  380.         for( i = 0; i < ivCount; i++ )
  381.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  382.  
  383.         /* Move on to next block of data */
  384.         noBytes -= ivCount;
  385.         buffer += ivCount;
  386.         }
  387.  
  388.     /* Remember how much of the IV is still available for use */
  389.     cryptInfo->ivCount = ( ivCount % RC2_BLOCKSIZE );
  390.  
  391.     return( CRYPT_OK );
  392.     }
  393.  
  394. /* Decrypt data in OFB mode */
  395.  
  396. int rc2DecryptOFB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  397.     {
  398.     RC2_KEY *rc2Key = ( RC2_KEY * ) cryptInfo->key;
  399.     int i, ivCount = cryptInfo->ivCount;
  400.  
  401.     /* If there's any encrypted material left in the IV, use it now */
  402.     if( ivCount )
  403.         {
  404.         int bytesToUse;
  405.  
  406.         /* Find out how much material left in the encrypted IV we can use */
  407.         bytesToUse = RC2_BLOCKSIZE - ivCount;
  408.         if( noBytes < bytesToUse )
  409.             bytesToUse = noBytes;
  410.  
  411.         /* Decrypt the data */
  412.         for( i = 0; i < bytesToUse; i++ )
  413.             buffer[ i ] ^= cryptInfo->currentIV[ i + ivCount ];
  414.  
  415.         /* Adjust the byte count and buffer position */
  416.         noBytes -= bytesToUse;
  417.         buffer += bytesToUse;
  418.         ivCount += bytesToUse;
  419.         }
  420.  
  421.     while( noBytes )
  422.         {
  423.         ivCount = ( noBytes > RC2_BLOCKSIZE ) ? RC2_BLOCKSIZE : noBytes;
  424.  
  425.         /* Encrypt the IV */
  426.         rc2encrypt( rc2Key, cryptInfo->currentIV );
  427.  
  428.         /* XOR the buffer contents with the encrypted IV */
  429.         for( i = 0; i < ivCount; i++ )
  430.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  431.  
  432.         /* Move on to next block of data */
  433.         noBytes -= ivCount;
  434.         buffer += ivCount;
  435.         }
  436.  
  437.     /* Remember how much of the IV is still available for use */
  438.     cryptInfo->ivCount = ( ivCount % RC2_BLOCKSIZE );
  439.  
  440.     return( CRYPT_OK );
  441.     }
  442.  
  443. /* Encrypt/decrypt data in PCBC mode.  We have to carry along the previous
  444.    block's plaintext as well as the IV so we store it after the IV in the
  445.    buffer allocated for IV storage.  Initially the plaintextChain value will
  446.    be null as the IV buffer is zeroed on init, so we don't need to worry
  447.    about the special case of the first ciphertext block */
  448.  
  449. int rc2EncryptPCBC( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  450.     {
  451.     RC2_KEY *rc2Key = ( RC2_KEY * ) cryptInfo->key;
  452.     BYTE *plaintextChain = cryptInfo->currentIV + RC2_BLOCKSIZE;
  453.     BYTE temp[ RC2_BLOCKSIZE ];
  454.     int blockCount = noBytes / RC2_BLOCKSIZE;
  455.  
  456.     /* Make sure the data length is a multiple of the block size */
  457.     if( noBytes % RC2_BLOCKSIZE )
  458.         return( CRYPT_BADPARM3 );
  459.  
  460.     while( blockCount-- )
  461.         {
  462.         int i;
  463.  
  464.         /* Remember the previous block's plaintext and copy the current
  465.            plaintext for chaining in the next iteration */
  466.         memcpy( temp, plaintextChain, RC2_BLOCKSIZE );
  467.         memcpy( plaintextChain, buffer, RC2_BLOCKSIZE );
  468.  
  469.         /* XOR the buffer contents with the IV and previous plaintext */
  470.         for( i = 0; i < RC2_BLOCKSIZE; i++ )
  471.             buffer[ i ] ^= cryptInfo->currentIV[ i ] ^ temp[ i ];
  472.  
  473.         /* Encrypt a block of data */
  474.         rc2encrypt( rc2Key, buffer );
  475.  
  476.         /* Shift ciphertext into IV */
  477.         memcpy( cryptInfo->currentIV, buffer, RC2_BLOCKSIZE );
  478.  
  479.         /* Move on to next block of data */
  480.         buffer += RC2_BLOCKSIZE;
  481.         }
  482.  
  483.     /* Clear the temporary buffer */
  484.     zeroise( temp, RC2_BLOCKSIZE );
  485.  
  486.     return( CRYPT_OK );
  487.     }
  488.  
  489. int rc2DecryptPCBC( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  490.     {
  491.     RC2_KEY *rc2Key = ( RC2_KEY * ) cryptInfo->key;
  492.     BYTE *plaintextChain = cryptInfo->currentIV + RC2_BLOCKSIZE;
  493.     BYTE temp[ RC2_BLOCKSIZE ];
  494.     int blockCount = noBytes / RC2_BLOCKSIZE;
  495.  
  496.     /* Make sure the data length is a multiple of the block size */
  497.     if( noBytes % RC2_BLOCKSIZE )
  498.         return( CRYPT_BADPARM3 );
  499.  
  500.     while( blockCount-- )
  501.         {
  502.         int i;
  503.  
  504.         /* Save the ciphertext */
  505.         memcpy( temp, buffer, RC2_BLOCKSIZE );
  506.  
  507.         /* Decrypt a block of data */
  508.         rc2decrypt( rc2Key, buffer );
  509.  
  510.         /* XOR the buffer contents with the IV and previous plaintext */
  511.         for( i = 0; i < RC2_BLOCKSIZE; i++ )
  512.             buffer[ i ] ^= cryptInfo->currentIV[ i ] ^ plaintextChain[ i ];
  513.  
  514.         /* Shift the ciphertext into the IV and copy the current plaintext
  515.            for chaining in the next iteration */
  516.         memcpy( cryptInfo->currentIV, temp, RC2_BLOCKSIZE );
  517.         memcpy( plaintextChain, buffer, RC2_BLOCKSIZE );
  518.  
  519.         /* Move on to next block of data */
  520.         buffer += RC2_BLOCKSIZE;
  521.         }
  522.  
  523.     /* Clear the temporary buffer */
  524.     zeroise( temp, RC2_BLOCKSIZE );
  525.  
  526.     return( CRYPT_OK );
  527.     }
  528.  
  529.  
  530. /****************************************************************************
  531. *                                                                            *
  532. *                            RC2 Key Management Routines                        *
  533. *                                                                            *
  534. ****************************************************************************/
  535.  
  536. /* Key schedule an RC2 key */
  537.  
  538. int rc2InitKey( CRYPT_INFO *cryptInfo )
  539.     {
  540.     RC2_KEY *rc2Key = ( RC2_KEY * ) cryptInfo->key;
  541.  
  542.     rc2keyInit( rc2Key, cryptInfo->userKey, cryptInfo->userKeyLength );
  543.     return( CRYPT_OK );
  544.     }
  545.