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

  1. /****************************************************************************
  2. *                                                                            *
  3. *                      cryptlib Blowfish 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 "blowfish.h"
  16. #else
  17.   #include "blowfish/blowfish.h"
  18. #endif /* Compiler-specific includes */
  19.  
  20. /* The size of the expanded Blowfish keys */
  21.  
  22. #define BLOWFISH_EXPANDED_KEYSIZE        sizeof( BLOWFISH_KEY )
  23.  
  24. /****************************************************************************
  25. *                                                                            *
  26. *                            Blowfish Self-test Routines                        *
  27. *                                                                            *
  28. ****************************************************************************/
  29.  
  30. /* Test the Blowfish code against Bruce Schneiers test vectors (1 & 2) and
  31.    Mike Morgans test vector (3) */
  32.  
  33. int blowfishSelfTest( void )
  34.     {
  35.     BYTE *plain1 = ( BYTE * ) "BLOWFISH";
  36.     BYTE *key1 = ( BYTE * ) "abcdefghijklmnopqrstuvwxyz";
  37.     BYTE cipher1[] = { 0x32, 0x4E, 0xD0, 0xFE, 0xF4, 0x13, 0xA2, 0x03 };
  38.     BYTE plain2[] = { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 };
  39.     BYTE *key2 = ( BYTE * ) "Who is John Galt?";
  40.     BYTE cipher2[] = { 0xCC, 0x91, 0x73, 0x2B, 0x80, 0x22, 0xF6, 0x84 };
  41.     BYTE plain3[] = { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 };
  42.     BYTE key3[] = { 0x41, 0x79, 0x6E, 0xA0, 0x52, 0x61, 0x6E, 0xE4 };
  43.     BYTE cipher3[] = { 0xE1, 0x13, 0xF4, 0x10, 0x2C, 0xFC, 0xCE, 0x43 };
  44.     BYTE *plain4 = ( BYTE * ) "BLOWFISH";
  45.     BYTE *key4 = ( BYTE * ) "Blowfish-SK test key";
  46.     BYTE cipher4[] = { 0xB4, 0xA0, 0xDC, 0x81, 0xE1, 0x3E, 0xAF, 0x4F };
  47.  
  48.     BLOWFISH_KEY bfKey;
  49.     BYTE buffer[ 8 ];
  50.  
  51.     /* Test the Blowfish implementation */
  52.     memcpy( buffer, plain1, 8 );
  53.     if( blowfishKeyInit( &bfKey, key1, strlen( ( char * ) key1 ) ) != CRYPT_OK )
  54.         return( CRYPT_ERROR );
  55.     blowfishEncrypt( &bfKey, buffer );
  56.     if( memcmp( buffer, cipher1, 8 ) )
  57.         return( CRYPT_ERROR );
  58.     blowfishDecrypt( &bfKey, buffer );
  59.     if( memcmp( buffer, plain1, 8 ) )
  60.         return( CRYPT_ERROR );
  61.     memcpy( buffer, plain2, 8 );
  62.     if( blowfishKeyInit( &bfKey, key2, strlen( ( char * ) key2 ) ) != CRYPT_OK )
  63.         return( CRYPT_ERROR );
  64.     blowfishEncrypt( &bfKey, buffer );
  65.     if( memcmp( buffer, cipher2, 8 ) )
  66.         return( CRYPT_ERROR );
  67.     blowfishDecrypt( &bfKey, buffer );
  68.     if( memcmp( buffer, plain2, 8 ) )
  69.         return( CRYPT_ERROR );
  70.     memcpy( buffer, plain3, 8 );
  71.     if( blowfishKeyInit( &bfKey, key3, 8 ) != CRYPT_OK )
  72.         return( CRYPT_ERROR );
  73.     blowfishEncrypt( &bfKey, buffer );
  74.     if( memcmp( buffer, cipher3, 8 ) )
  75.         return( CRYPT_ERROR );
  76.     blowfishDecrypt( &bfKey, buffer );
  77.     if( memcmp( buffer, plain3, 8 ) )
  78.         return( CRYPT_ERROR );
  79.  
  80.     /* Test the Blowfish-SK implementation */
  81.     memcpy( buffer, plain4, 8 );
  82.     if( blowfishKeyInitSK( &bfKey, key4, strlen( ( char * ) key4 ), 10 ) != CRYPT_OK )
  83.         return( CRYPT_ERROR );
  84.     blowfishEncrypt( &bfKey, buffer );
  85.     if( memcmp( buffer, cipher4, 8 ) )
  86.         return( CRYPT_ERROR );
  87.     blowfishDecrypt( &bfKey, buffer );
  88.     if( memcmp( buffer, plain4, 8 ) )
  89.         return( CRYPT_ERROR );
  90.  
  91.     return( CRYPT_OK );
  92.     }
  93.  
  94. /****************************************************************************
  95. *                                                                            *
  96. *                            Init/Shutdown Routines                            *
  97. *                                                                            *
  98. ****************************************************************************/
  99.  
  100. /* Perform auxiliary init and shutdown actions on an encryption context */
  101.  
  102. int blowfishInitEx( CRYPT_INFO *cryptInfo, void *cryptInfoEx )
  103.     {
  104.     CRYPT_INFO_BLOWFISH *cryptInfoExPtr = ( CRYPT_INFO_BLOWFISH * ) cryptInfoEx;
  105.     int status;
  106.  
  107.     /* Allocate memory for the key and the algorithm-specific data within
  108.        the crypt context and set up any pointers we need.  We don't process
  109.        cryptInfoExPtr->rounds at this point since we set things up when we
  110.        perform the blowfishInitKey() function, as the number of rounds is key-
  111.        dependant */
  112.     if( cryptInfo->key != NULL || cryptInfo->privateData != NULL )
  113.         return( CRYPT_INITED );
  114.     if( ( status = secureMalloc( &cryptInfo->key, BLOWFISH_EXPANDED_KEYSIZE ) ) != CRYPT_OK )
  115.         return( status );
  116.     if( cryptInfoExPtr->keySetupIterations == CRYPT_USE_DEFAULT && \
  117.         cryptInfoExPtr->useBlowfishSK == CRYPT_USE_DEFAULT )
  118.         cryptInfo->privateUseDefaults = TRUE;
  119.     if( ( status = secureMalloc( &cryptInfo->privateData, sizeof( CRYPT_INFO_BLOWFISH ) ) ) != CRYPT_OK )
  120.         {
  121.         secureFree( &cryptInfo->key );
  122.         return( status );
  123.         }
  124.     if( cryptInfoExPtr->useBlowfishSK == CRYPT_USE_DEFAULT )
  125.         setBlowfishInfo( cryptInfo, FALSE,
  126.                          cryptInfoExPtr->keySetupIterations );
  127.     else
  128.         setBlowfishInfo( cryptInfo, ( BOOLEAN ) cryptInfoExPtr->useBlowfishSK,
  129.                          cryptInfoExPtr->keySetupIterations );
  130.     cryptInfo->keyLength = BLOWFISH_EXPANDED_KEYSIZE;
  131.  
  132.     return( CRYPT_OK );
  133.     }
  134.  
  135. int blowfishInit( CRYPT_INFO *cryptInfo )
  136.     {
  137.     CRYPT_INFO_BLOWFISH cryptInfoEx;
  138.  
  139.     /* Use the default number of rounds and the non-enhanced Blowfish */
  140.     memset( &cryptInfoEx, 0, sizeof( CRYPT_INFO_BLOWFISH ) );
  141.     cryptInfoEx.keySetupIterations = CRYPT_USE_DEFAULT;
  142.     cryptInfoEx.useBlowfishSK = CRYPT_USE_DEFAULT;
  143.  
  144.     /* Pass through to the extended setup routine */
  145.     return( blowfishInitEx( cryptInfo, &cryptInfoEx ) );
  146.     }
  147.  
  148. int blowfishEnd( CRYPT_INFO *cryptInfo )
  149.     {
  150.     /* Free any allocated memory */
  151.     secureFree( &cryptInfo->key );
  152.     secureFree( &cryptInfo->privateData );
  153.  
  154.     return( CRYPT_OK );
  155.     }
  156.  
  157. /****************************************************************************
  158. *                                                                            *
  159. *                            Blowfish En/Decryption Routines                    *
  160. *                                                                            *
  161. ****************************************************************************/
  162.  
  163. /* Encrypt/decrypt data in ECB mode */
  164.  
  165. int blowfishEncryptECB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  166.     {
  167.     BLOWFISH_KEY *blowfishKey = ( BLOWFISH_KEY * ) cryptInfo->key;
  168.     int blockCount = noBytes / BLOWFISH_BLOCKSIZE;
  169.  
  170.     /* Make sure the data length is a multiple of the block size */
  171.     if( noBytes % BLOWFISH_BLOCKSIZE )
  172.         return( CRYPT_BADPARM3 );
  173.  
  174.     while( blockCount-- )
  175.         {
  176.         /* Encrypt a block of data */
  177.         blowfishEncrypt( blowfishKey, buffer );
  178.  
  179.         /* Move on to next block of data */
  180.         buffer += BLOWFISH_BLOCKSIZE;
  181.         }
  182.  
  183.     return( CRYPT_OK );
  184.     }
  185.  
  186. int blowfishDecryptECB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  187.     {
  188.     BLOWFISH_KEY *blowfishKey = ( BLOWFISH_KEY * ) cryptInfo->key;
  189.     int blockCount = noBytes / BLOWFISH_BLOCKSIZE;
  190.  
  191.     /* Make sure the data length is a multiple of the block size */
  192.     if( noBytes % BLOWFISH_BLOCKSIZE )
  193.         return( CRYPT_BADPARM3 );
  194.  
  195.     while( blockCount-- )
  196.         {
  197.         /* Decrypt a block of data */
  198.         blowfishDecrypt( blowfishKey, buffer );
  199.  
  200.         /* Move on to next block of data */
  201.         buffer += BLOWFISH_BLOCKSIZE;
  202.         }
  203.  
  204.     return( CRYPT_OK );
  205.     }
  206.  
  207. /* Encrypt/decrypt data in CBC mode */
  208.  
  209. int blowfishEncryptCBC( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  210.     {
  211.     BLOWFISH_KEY *blowfishKey = ( BLOWFISH_KEY * ) cryptInfo->key;
  212.     int blockCount = noBytes / BLOWFISH_BLOCKSIZE;
  213.  
  214.     /* Make sure the data length is a multiple of the block size */
  215.     if( noBytes % BLOWFISH_BLOCKSIZE )
  216.         return( CRYPT_BADPARM3 );
  217.  
  218.     while( blockCount-- )
  219.         {
  220.         int i;
  221.  
  222.         /* XOR the buffer contents with the IV */
  223.         for( i = 0; i < BLOWFISH_BLOCKSIZE; i++ )
  224.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  225.  
  226.         /* Encrypt a block of data */
  227.         blowfishEncrypt( blowfishKey, buffer );
  228.  
  229.         /* Shift ciphertext into IV */
  230.         memcpy( cryptInfo->currentIV, buffer, BLOWFISH_BLOCKSIZE );
  231.  
  232.         /* Move on to next block of data */
  233.         buffer += BLOWFISH_BLOCKSIZE;
  234.         }
  235.  
  236.     return( CRYPT_OK );
  237.     }
  238.  
  239. int blowfishDecryptCBC( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  240.     {
  241.     BLOWFISH_KEY *blowfishKey = ( BLOWFISH_KEY * ) cryptInfo->key;
  242.     BYTE temp[ BLOWFISH_BLOCKSIZE ];
  243.     int blockCount = noBytes / BLOWFISH_BLOCKSIZE;
  244.  
  245.     /* Make sure the data length is a multiple of the block size */
  246.     if( noBytes % BLOWFISH_BLOCKSIZE )
  247.         return( CRYPT_BADPARM3 );
  248.  
  249.     while( blockCount-- )
  250.         {
  251.         int i;
  252.  
  253.         /* Save the ciphertext */
  254.         memcpy( temp, buffer, BLOWFISH_BLOCKSIZE );
  255.  
  256.         /* Decrypt a block of data */
  257.         blowfishDecrypt( blowfishKey, buffer );
  258.  
  259.         /* XOR the buffer contents with the IV */
  260.         for( i = 0; i < BLOWFISH_BLOCKSIZE; i++ )
  261.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  262.  
  263.         /* Shift the ciphertext into the IV */
  264.         memcpy( cryptInfo->currentIV, temp, BLOWFISH_BLOCKSIZE );
  265.  
  266.         /* Move on to next block of data */
  267.         buffer += BLOWFISH_BLOCKSIZE;
  268.         }
  269.  
  270.     /* Clear the temporary buffer */
  271.     zeroise( temp, BLOWFISH_BLOCKSIZE );
  272.  
  273.     return( CRYPT_OK );
  274.     }
  275.  
  276. /* Encrypt/decrypt data in CFB mode */
  277.  
  278. int blowfishEncryptCFB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  279.     {
  280.     BLOWFISH_KEY *blowfishKey = ( BLOWFISH_KEY * ) cryptInfo->key;
  281.     int i, ivCount = cryptInfo->ivCount;
  282.  
  283.     /* If there's any encrypted material left in the IV, use it now */
  284.     if( ivCount )
  285.         {
  286.         int bytesToUse;
  287.  
  288.         /* Find out how much material left in the encrypted IV we can use */
  289.         bytesToUse = BLOWFISH_BLOCKSIZE - ivCount;
  290.         if( noBytes < bytesToUse )
  291.             bytesToUse = noBytes;
  292.  
  293.         /* Encrypt the data */
  294.         for( i = 0; i < bytesToUse; i++ )
  295.             buffer[ i ] ^= cryptInfo->currentIV[ i + ivCount ];
  296.         memcpy( cryptInfo->currentIV + ivCount, buffer, bytesToUse );
  297.  
  298.         /* Adjust the byte count and buffer position */
  299.         noBytes -= bytesToUse;
  300.         buffer += bytesToUse;
  301.         ivCount += bytesToUse;
  302.         }
  303.  
  304.     while( noBytes )
  305.         {
  306.         ivCount = ( noBytes > BLOWFISH_BLOCKSIZE ) ? BLOWFISH_BLOCKSIZE : noBytes;
  307.  
  308.         /* Encrypt the IV */
  309.         blowfishEncrypt( blowfishKey, cryptInfo->currentIV );
  310.  
  311.         /* XOR the buffer contents with the encrypted IV */
  312.         for( i = 0; i < ivCount; i++ )
  313.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  314.  
  315.         /* Shift the ciphertext into the IV */
  316.         memcpy( cryptInfo->currentIV, buffer, ivCount );
  317.  
  318.         /* Move on to next block of data */
  319.         noBytes -= ivCount;
  320.         buffer += ivCount;
  321.         }
  322.  
  323.     /* Remember how much of the IV is still available for use */
  324.     cryptInfo->ivCount = ( ivCount % BLOWFISH_BLOCKSIZE );
  325.  
  326.     return( CRYPT_OK );
  327.     }
  328.  
  329. /* Decrypt data in CFB mode.  Note that the transformation can be made
  330.    faster (but less clear) with temp = buffer, buffer ^= iv, iv = temp
  331.    all in one loop */
  332.  
  333. int blowfishDecryptCFB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  334.     {
  335.     BLOWFISH_KEY *blowfishKey = ( BLOWFISH_KEY * ) cryptInfo->key;
  336.     BYTE temp[ BLOWFISH_BLOCKSIZE ];
  337.     int i, ivCount = cryptInfo->ivCount;
  338.  
  339.     /* If there's any encrypted material left in the IV, use it now */
  340.     if( ivCount )
  341.         {
  342.         int bytesToUse;
  343.  
  344.         /* Find out how much material left in the encrypted IV we can use */
  345.         bytesToUse = BLOWFISH_BLOCKSIZE - ivCount;
  346.         if( noBytes < bytesToUse )
  347.             bytesToUse = noBytes;
  348.  
  349.         /* Decrypt the data */
  350.         memcpy( temp, buffer, bytesToUse );
  351.         for( i = 0; i < bytesToUse; i++ )
  352.             buffer[ i ] ^= cryptInfo->currentIV[ i + ivCount ];
  353.         memcpy( cryptInfo->currentIV + ivCount, temp, bytesToUse );
  354.  
  355.         /* Adjust the byte count and buffer position */
  356.         noBytes -= bytesToUse;
  357.         buffer += bytesToUse;
  358.         ivCount += bytesToUse;
  359.         }
  360.  
  361.     while( noBytes )
  362.         {
  363.         ivCount = ( noBytes > BLOWFISH_BLOCKSIZE ) ? BLOWFISH_BLOCKSIZE : noBytes;
  364.  
  365.         /* Encrypt the IV */
  366.         blowfishEncrypt( blowfishKey, cryptInfo->currentIV );
  367.  
  368.         /* Save the ciphertext */
  369.         memcpy( temp, buffer, ivCount );
  370.  
  371.         /* XOR the buffer contents with the encrypted IV */
  372.         for( i = 0; i < ivCount; i++ )
  373.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  374.  
  375.         /* Shift the ciphertext into the IV */
  376.         memcpy( cryptInfo->currentIV, temp, ivCount );
  377.  
  378.         /* Move on to next block of data */
  379.         noBytes -= ivCount;
  380.         buffer += ivCount;
  381.         }
  382.  
  383.     /* Remember how much of the IV is still available for use */
  384.     cryptInfo->ivCount = ( ivCount % BLOWFISH_BLOCKSIZE );
  385.  
  386.     /* Clear the temporary buffer */
  387.     zeroise( temp, BLOWFISH_BLOCKSIZE );
  388.  
  389.     return( CRYPT_OK );
  390.     }
  391.  
  392. /* Encrypt/decrypt data in OFB mode */
  393.  
  394. int blowfishEncryptOFB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  395.     {
  396.     BLOWFISH_KEY *blowfishKey = ( BLOWFISH_KEY * ) cryptInfo->key;
  397.     int i, ivCount = cryptInfo->ivCount;
  398.  
  399.     /* If there's any encrypted material left in the IV, use it now */
  400.     if( ivCount )
  401.         {
  402.         int bytesToUse;
  403.  
  404.         /* Find out how much material left in the encrypted IV we can use */
  405.         bytesToUse = BLOWFISH_BLOCKSIZE - ivCount;
  406.         if( noBytes < bytesToUse )
  407.             bytesToUse = noBytes;
  408.  
  409.         /* Encrypt the data */
  410.         for( i = 0; i < bytesToUse; i++ )
  411.             buffer[ i ] ^= cryptInfo->currentIV[ i + ivCount ];
  412.  
  413.         /* Adjust the byte count and buffer position */
  414.         noBytes -= bytesToUse;
  415.         buffer += bytesToUse;
  416.         ivCount += bytesToUse;
  417.         }
  418.  
  419.     while( noBytes )
  420.         {
  421.         ivCount = ( noBytes > BLOWFISH_BLOCKSIZE ) ? BLOWFISH_BLOCKSIZE : noBytes;
  422.  
  423.         /* Encrypt the IV */
  424.         blowfishEncrypt( blowfishKey, cryptInfo->currentIV );
  425.  
  426.         /* XOR the buffer contents with the encrypted IV */
  427.         for( i = 0; i < ivCount; i++ )
  428.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  429.  
  430.         /* Move on to next block of data */
  431.         noBytes -= ivCount;
  432.         buffer += ivCount;
  433.         }
  434.  
  435.     /* Remember how much of the IV is still available for use */
  436.     cryptInfo->ivCount = ( ivCount % BLOWFISH_BLOCKSIZE );
  437.  
  438.     return( CRYPT_OK );
  439.     }
  440.  
  441. /* Decrypt data in OFB mode */
  442.  
  443. int blowfishDecryptOFB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  444.     {
  445.     BLOWFISH_KEY *blowfishKey = ( BLOWFISH_KEY * ) cryptInfo->key;
  446.     int i, ivCount = cryptInfo->ivCount;
  447.  
  448.     /* If there's any encrypted material left in the IV, use it now */
  449.     if( ivCount )
  450.         {
  451.         int bytesToUse;
  452.  
  453.         /* Find out how much material left in the encrypted IV we can use */
  454.         bytesToUse = BLOWFISH_BLOCKSIZE - ivCount;
  455.         if( noBytes < bytesToUse )
  456.             bytesToUse = noBytes;
  457.  
  458.         /* Decrypt the data */
  459.         for( i = 0; i < bytesToUse; i++ )
  460.             buffer[ i ] ^= cryptInfo->currentIV[ i + ivCount ];
  461.  
  462.         /* Adjust the byte count and buffer position */
  463.         noBytes -= bytesToUse;
  464.         buffer += bytesToUse;
  465.         ivCount += bytesToUse;
  466.         }
  467.  
  468.     while( noBytes )
  469.         {
  470.         ivCount = ( noBytes > BLOWFISH_BLOCKSIZE ) ? BLOWFISH_BLOCKSIZE : noBytes;
  471.  
  472.         /* Encrypt the IV */
  473.         blowfishEncrypt( blowfishKey, cryptInfo->currentIV );
  474.  
  475.         /* XOR the buffer contents with the encrypted IV */
  476.         for( i = 0; i < ivCount; i++ )
  477.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  478.  
  479.         /* Move on to next block of data */
  480.         noBytes -= ivCount;
  481.         buffer += ivCount;
  482.         }
  483.  
  484.     /* Remember how much of the IV is still available for use */
  485.     cryptInfo->ivCount = ( ivCount % BLOWFISH_BLOCKSIZE );
  486.  
  487.     return( CRYPT_OK );
  488.     }
  489.  
  490. /* Encrypt/decrypt data in PCBC mode.  We have to carry along the previous
  491.    block's plaintext as well as the IV so we store it after the IV in the
  492.    buffer allocated for IV storage.  Initially the plaintextChain value will
  493.    be null as the IV buffer is zeroed on init, so we don't need to worry
  494.    about the special case of the first ciphertext block */
  495.  
  496. int blowfishEncryptPCBC( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  497.     {
  498.     BLOWFISH_KEY *blowfishKey = ( BLOWFISH_KEY * ) cryptInfo->key;
  499.     BYTE *plaintextChain = cryptInfo->currentIV + BLOWFISH_BLOCKSIZE;
  500.     BYTE temp[ BLOWFISH_BLOCKSIZE ];
  501.     int blockCount = noBytes / BLOWFISH_BLOCKSIZE;
  502.  
  503.     /* Make sure the data length is a multiple of the block size */
  504.     if( noBytes % BLOWFISH_BLOCKSIZE )
  505.         return( CRYPT_BADPARM3 );
  506.  
  507.     while( blockCount-- )
  508.         {
  509.         int i;
  510.  
  511.         /* Remember the previous block's plaintext and copy the current
  512.            plaintext for chaining in the next iteration */
  513.         memcpy( temp, plaintextChain, BLOWFISH_BLOCKSIZE );
  514.         memcpy( plaintextChain, buffer, BLOWFISH_BLOCKSIZE );
  515.  
  516.         /* XOR the buffer contents with the IV and previous plaintext */
  517.         for( i = 0; i < BLOWFISH_BLOCKSIZE; i++ )
  518.             buffer[ i ] ^= cryptInfo->currentIV[ i ] ^ temp[ i ];
  519.  
  520.         /* Encrypt a block of data */
  521.         blowfishEncrypt( blowfishKey, buffer );
  522.  
  523.         /* Shift ciphertext into IV */
  524.         memcpy( cryptInfo->currentIV, buffer, BLOWFISH_BLOCKSIZE );
  525.  
  526.         /* Move on to next block of data */
  527.         buffer += BLOWFISH_BLOCKSIZE;
  528.         }
  529.  
  530.     /* Clear the temporary buffer */
  531.     zeroise( temp, BLOWFISH_BLOCKSIZE );
  532.  
  533.     return( CRYPT_OK );
  534.     }
  535.  
  536. int blowfishDecryptPCBC( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  537.     {
  538.     BLOWFISH_KEY *blowfishKey = ( BLOWFISH_KEY * ) cryptInfo->key;
  539.     BYTE *plaintextChain = cryptInfo->currentIV + BLOWFISH_BLOCKSIZE;
  540.     BYTE temp[ BLOWFISH_BLOCKSIZE ];
  541.     int blockCount = noBytes / BLOWFISH_BLOCKSIZE;
  542.  
  543.     /* Make sure the data length is a multiple of the block size */
  544.     if( noBytes % BLOWFISH_BLOCKSIZE )
  545.         return( CRYPT_BADPARM3 );
  546.  
  547.     while( blockCount-- )
  548.         {
  549.         int i;
  550.  
  551.         /* Save the ciphertext */
  552.         memcpy( temp, buffer, BLOWFISH_BLOCKSIZE );
  553.  
  554.         /* Decrypt a block of data */
  555.         blowfishDecrypt( blowfishKey, buffer );
  556.  
  557.         /* XOR the buffer contents with the IV and previous plaintext */
  558.         for( i = 0; i < BLOWFISH_BLOCKSIZE; i++ )
  559.             buffer[ i ] ^= cryptInfo->currentIV[ i ] ^ plaintextChain[ i ];
  560.  
  561.         /* Shift the ciphertext into the IV and copy the current plaintext
  562.            for chaining in the next iteration */
  563.         memcpy( cryptInfo->currentIV, temp, BLOWFISH_BLOCKSIZE );
  564.         memcpy( plaintextChain, buffer, BLOWFISH_BLOCKSIZE );
  565.  
  566.         /* Move on to next block of data */
  567.         buffer += BLOWFISH_BLOCKSIZE;
  568.         }
  569.  
  570.     /* Clear the temporary buffer */
  571.     zeroise( temp, BLOWFISH_BLOCKSIZE );
  572.  
  573.     return( CRYPT_OK );
  574.     }
  575.  
  576.  
  577. /****************************************************************************
  578. *                                                                            *
  579. *                            Blowfish Key Management Routines                *
  580. *                                                                            *
  581. ****************************************************************************/
  582.  
  583. /* Get/set algorithm-specific parameters */
  584.  
  585. int getBlowfishInfo( const CRYPT_INFO *cryptInfo, BOOLEAN *useBlowfishSK )
  586.     {
  587.     CRYPT_INFO_BLOWFISH *cryptInfoEx = ( CRYPT_INFO_BLOWFISH * ) cryptInfo->privateData;
  588.  
  589.     *useBlowfishSK = cryptInfoEx->useBlowfishSK;
  590.     return( cryptInfoEx->keySetupIterations );
  591.     }
  592.  
  593. void setBlowfishInfo( CRYPT_INFO *cryptInfo, const BOOLEAN useBlowfishSK,
  594.                       const int keySetupIterations )
  595.     {
  596.     CRYPT_INFO_BLOWFISH *cryptInfoEx = ( CRYPT_INFO_BLOWFISH * ) cryptInfo->privateData;
  597.  
  598.     cryptInfoEx->useBlowfishSK = useBlowfishSK;
  599.     cryptInfoEx->keySetupIterations = keySetupIterations;
  600.     }
  601.  
  602. int blowfishGetKeysize( CRYPT_INFO *cryptInfo )
  603.     {
  604.     /* This is tricky, since we dynamically adjust the key type to 448 or
  605.        CRYPT_MAX_KEYSIZE bits depending on how much keying data we've been
  606.        passed by the user, but we can't tell in advance how much this will
  607.        be.  We get around this by taking advantage of the fact that when the
  608.        library queries the key size for an encryption context with no key
  609.        loaded, it always wants to know the maximum amount of data it can use
  610.        for a key, so we just return the maximum value */
  611.     if( cryptInfo->userKeyLength == 0 )
  612.         return( CRYPT_MAX_KEYSIZE );
  613.  
  614.     /* If the key has already been set up, just return the size of the key
  615.        we're using */
  616.     return( cryptInfo->userKeyLength );
  617.     }
  618.  
  619. /* Key schedule a Blowfish key */
  620.  
  621. int blowfishInitKey( CRYPT_INFO *cryptInfo )
  622.     {
  623.     CRYPT_INFO_BLOWFISH *cryptInfoEx = ( CRYPT_INFO_BLOWFISH * ) cryptInfo->privateData;
  624.     BLOWFISH_KEY *blowfishKey = ( BLOWFISH_KEY * ) cryptInfo->key;
  625.     int keySetupIterations = BLOWFISH_KEYSETUP_ITERATIONS;
  626.  
  627.     /* If it's a long key, make sure we're using Blowfish-SK */
  628.     if( cryptInfo->userKeyLength > bitsToBytes( 448 ) && \
  629.         !cryptInfoEx->useBlowfishSK )
  630.         return( CRYPT_BADPARM3 );
  631.  
  632.     /* If the number of rounds has been preset, use this value */
  633.     if( cryptInfoEx->keySetupIterations != CRYPT_USE_DEFAULT )
  634.         keySetupIterations = cryptInfoEx->keySetupIterations;
  635.  
  636.     /* Set up the key for Blowfish or Blowfish-SK */
  637.     if( cryptInfoEx->useBlowfishSK )
  638.         return( blowfishKeyInitSK( blowfishKey, cryptInfo->userKey,
  639.                                    cryptInfo->userKeyLength,
  640.                                    keySetupIterations ) );
  641.     else
  642.         return( blowfishKeyInit( blowfishKey, cryptInfo->userKey,
  643.                                  cryptInfo->userKeyLength ) );
  644.     }
  645.