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

  1. /****************************************************************************
  2. *                                                                            *
  3. *                    cryptlib Triple DES 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 "des.h"
  16. #else
  17.   #include "libdes/des.h"
  18. #endif /* Compiler-specific includes */
  19.  
  20. /* The DES block size */
  21.  
  22. #define DES_BLOCKSIZE    8
  23.  
  24. /* A structure to hold the keyscheduled DES keys */
  25.  
  26. typedef struct {
  27.     Key_schedule desKey1;            /* The first DES key */
  28.     Key_schedule desKey2;            /* The second DES key */
  29.     Key_schedule desKey3;            /* The third DES key */
  30.     BOOLEAN isThreeKey;                /* Whether two or three-key triple DES */
  31.     } DES3_KEY;
  32.  
  33. /* The size of the keyscheduled DES and 3DES keys */
  34.  
  35. #define DES_KEYSIZE        sizeof( Key_schedule )
  36. #define DES3_KEYSIZE    sizeof( DES3_KEY )
  37.  
  38. /****************************************************************************
  39. *                                                                            *
  40. *                                3DES Self-test Routines                        *
  41. *                                                                            *
  42. ****************************************************************************/
  43.  
  44. /* Are there any 3DES test vectors?  Presumably X9F1 will eventually
  45.    publish some... */
  46.  
  47. int des3SelfTest( void )
  48.     {
  49.     return( CRYPT_OK );
  50.     }
  51.  
  52. /****************************************************************************
  53. *                                                                            *
  54. *                            Init/Shutdown Routines                            *
  55. *                                                                            *
  56. ****************************************************************************/
  57.  
  58. /* Perform auxiliary init and shutdown actions on an encryption context */
  59.  
  60. int des3InitEx( CRYPT_INFO *cryptInfo, void *cryptInfoEx )
  61.     {
  62.     CRYPT_INFO_3DES *cryptInfoExPtr = ( CRYPT_INFO_3DES * ) cryptInfoEx;
  63.     int status;
  64.  
  65.     /* Allocate memory for the keyscheduled keys */
  66.     if( cryptInfo->key != NULL || cryptInfo->privateData != NULL )
  67.         return( CRYPT_INITED );
  68.     if( ( status = secureMalloc( &cryptInfo->key, DES3_KEYSIZE ) ) != CRYPT_OK )
  69.         return( status );
  70.     memset( cryptInfo->key, 0, DES3_KEYSIZE );
  71.     if( cryptInfoExPtr->isThreeKey == CRYPT_USE_DEFAULT )
  72.         {
  73.         cryptInfo->privateUseDefaults = TRUE;
  74.         set3DESinfo( cryptInfo, FALSE );
  75.         }
  76.     else
  77.         set3DESinfo( cryptInfo, ( BOOLEAN ) cryptInfoExPtr->isThreeKey );
  78.     cryptInfo->keyLength = DES3_KEYSIZE;
  79.  
  80.     return( CRYPT_OK );
  81.     }
  82.  
  83. int des3Init( CRYPT_INFO *cryptInfo )
  84.     {
  85.     CRYPT_INFO_3DES cryptInfoEx;
  86.  
  87.     /* Use standard 2-key EDE triple DES */
  88.     memset( &cryptInfoEx, 0, sizeof( CRYPT_INFO_3DES ) );
  89.     cryptInfoEx.isThreeKey = CRYPT_USE_DEFAULT;
  90.  
  91.     /* Pass through to the extended setup routine */
  92.     return( des3InitEx( cryptInfo, &cryptInfoEx ) );
  93.     }
  94.  
  95. int des3End( CRYPT_INFO *cryptInfo )
  96.     {
  97.     /* Free any allocated memory */
  98.     secureFree( &cryptInfo->key );
  99.  
  100.     return( CRYPT_OK );
  101.     }
  102.  
  103. /****************************************************************************
  104. *                                                                            *
  105. *                            3DES En/Decryption Routines                        *
  106. *                                                                            *
  107. ****************************************************************************/
  108.  
  109. /* Encrypt/decrypt data in ECB mode */
  110.  
  111. int des3EncryptECB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  112.     {
  113.     DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->key;
  114.     int blockCount = noBytes / DES_BLOCKSIZE;
  115.  
  116.     /* Make sure the data length is a multiple of the block size */
  117.     if( noBytes % DES_BLOCKSIZE )
  118.         return( CRYPT_BADPARM3 );
  119.  
  120.     while( blockCount-- )
  121.         {
  122.         /* Encrypt a block of data */
  123.         des_ecb3_encrypt( ( C_Block * ) buffer, ( C_Block * ) buffer,
  124.                           des3Key->desKey1, des3Key->desKey2,
  125.                           des3Key->desKey3, DES_ENCRYPT );
  126.  
  127.         /* Move on to next block of data */
  128.         buffer += DES_BLOCKSIZE;
  129.         }
  130.  
  131.     return( CRYPT_OK );
  132.     }
  133.  
  134. int des3DecryptECB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  135.     {
  136.     DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->key;
  137.     int blockCount = noBytes / DES_BLOCKSIZE;
  138.  
  139.     /* Make sure the data length is a multiple of the block size */
  140.     if( noBytes % DES_BLOCKSIZE )
  141.         return( CRYPT_BADPARM3 );
  142.  
  143.     while( blockCount-- )
  144.         {
  145.         /* Decrypt a block of data */
  146.         des_ecb3_encrypt( ( C_Block * ) buffer, ( C_Block * ) buffer,
  147.                           des3Key->desKey1, des3Key->desKey2,
  148.                           des3Key->desKey3, DES_DECRYPT );
  149.  
  150.         /* Move on to next block of data */
  151.         buffer += DES_BLOCKSIZE;
  152.         }
  153.  
  154.     return( CRYPT_OK );
  155.     }
  156.  
  157. /* Encrypt/decrypt data in CBC mode */
  158.  
  159. int des3EncryptCBC( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  160.     {
  161.     DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->key;
  162.     int blockCount = noBytes / DES_BLOCKSIZE;
  163.  
  164.     /* Make sure the data length is a multiple of the block size */
  165.     if( noBytes % DES_BLOCKSIZE )
  166.         return( CRYPT_BADPARM3 );
  167.  
  168.     while( blockCount-- )
  169.         {
  170.         int i;
  171.  
  172.         /* XOR the buffer contents with the IV */
  173.         for( i = 0; i < DES_BLOCKSIZE; i++ )
  174.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  175.  
  176.         /* Encrypt a block of data */
  177.         des_ecb3_encrypt( ( C_Block * ) buffer, ( C_Block * ) buffer,
  178.                           des3Key->desKey1, des3Key->desKey2,
  179.                           des3Key->desKey3, DES_ENCRYPT );
  180.  
  181.         /* Shift ciphertext into IV */
  182.         memcpy( cryptInfo->currentIV, buffer, DES_BLOCKSIZE );
  183.  
  184.         /* Move on to next block of data */
  185.         buffer += DES_BLOCKSIZE;
  186.         }
  187.  
  188.     return( CRYPT_OK );
  189.     }
  190.  
  191. int des3DecryptCBC( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  192.     {
  193.     DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->key;
  194.     BYTE temp[ DES_BLOCKSIZE ];
  195.     int blockCount = noBytes / DES_BLOCKSIZE;
  196.  
  197.     /* Make sure the data length is a multiple of the block size */
  198.     if( noBytes % DES_BLOCKSIZE )
  199.         return( CRYPT_BADPARM3 );
  200.  
  201.     while( blockCount-- )
  202.         {
  203.         int i;
  204.  
  205.         /* Save the ciphertext */
  206.         memcpy( temp, buffer, DES_BLOCKSIZE );
  207.  
  208.         /* Decrypt a block of data */
  209.         des_ecb3_encrypt( ( C_Block * ) buffer, ( C_Block * ) buffer,
  210.                           des3Key->desKey1, des3Key->desKey2,
  211.                           des3Key->desKey3, DES_DECRYPT );
  212.  
  213.         /* XOR the buffer contents with the IV */
  214.         for( i = 0; i < DES_BLOCKSIZE; i++ )
  215.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  216.  
  217.         /* Shift the ciphertext into the IV */
  218.         memcpy( cryptInfo->currentIV, temp, DES_BLOCKSIZE );
  219.  
  220.         /* Move on to next block of data */
  221.         buffer += DES_BLOCKSIZE;
  222.         }
  223.  
  224.     /* Clear the temporary buffer */
  225.     zeroise( temp, DES_BLOCKSIZE );
  226.  
  227.     return( CRYPT_OK );
  228.     }
  229.  
  230. /* Encrypt/decrypt data in CFB mode */
  231.  
  232. int des3EncryptCFB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  233.     {
  234.     DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->key;
  235.     int i, ivCount = cryptInfo->ivCount;
  236.  
  237.     /* If there's any encrypted material left in the IV, use it now */
  238.     if( ivCount )
  239.         {
  240.         int bytesToUse;
  241.  
  242.         /* Find out how much material left in the encrypted IV we can use */
  243.         bytesToUse = DES_BLOCKSIZE - ivCount;
  244.         if( noBytes < bytesToUse )
  245.             bytesToUse = noBytes;
  246.  
  247.         /* Encrypt the data */
  248.         for( i = 0; i < bytesToUse; i++ )
  249.             buffer[ i ] ^= cryptInfo->currentIV[ i + ivCount ];
  250.         memcpy( cryptInfo->currentIV + ivCount, buffer, bytesToUse );
  251.  
  252.         /* Adjust the byte count and buffer position */
  253.         noBytes -= bytesToUse;
  254.         buffer += bytesToUse;
  255.         ivCount += bytesToUse;
  256.         }
  257.  
  258.     while( noBytes )
  259.         {
  260.         ivCount = ( noBytes > DES_BLOCKSIZE ) ? DES_BLOCKSIZE : noBytes;
  261.  
  262.         /* Encrypt the IV */
  263.         des_ecb3_encrypt( ( C_Block * ) cryptInfo->currentIV,
  264.                           ( C_Block * ) cryptInfo->currentIV,
  265.                           des3Key->desKey1, des3Key->desKey2,
  266.                           des3Key->desKey3, DES_ENCRYPT );
  267.  
  268.         /* XOR the buffer contents with the encrypted IV */
  269.         for( i = 0; i < ivCount; i++ )
  270.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  271.  
  272.         /* Shift the ciphertext into the IV */
  273.         memcpy( cryptInfo->currentIV, buffer, ivCount );
  274.  
  275.         /* Move on to next block of data */
  276.         noBytes -= ivCount;
  277.         buffer += ivCount;
  278.         }
  279.  
  280.     /* Remember how much of the IV is still available for use */
  281.     cryptInfo->ivCount = ( ivCount % DES_BLOCKSIZE );
  282.  
  283.     return( CRYPT_OK );
  284.     }
  285.  
  286. /* Decrypt data in CFB mode.  Note that the transformation can be made
  287.    faster (but less clear) with temp = buffer, buffer ^= iv, iv = temp
  288.    all in one loop */
  289.  
  290. int des3DecryptCFB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  291.     {
  292.     DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->key;
  293.     BYTE temp[ DES_BLOCKSIZE ];
  294.     int i, ivCount = cryptInfo->ivCount;
  295.  
  296.     /* If there's any encrypted material left in the IV, use it now */
  297.     if( ivCount )
  298.         {
  299.         int bytesToUse;
  300.  
  301.         /* Find out how much material left in the encrypted IV we can use */
  302.         bytesToUse = DES_BLOCKSIZE - ivCount;
  303.         if( noBytes < bytesToUse )
  304.             bytesToUse = noBytes;
  305.  
  306.         /* Decrypt the data */
  307.         memcpy( temp, buffer, bytesToUse );
  308.         for( i = 0; i < bytesToUse; i++ )
  309.             buffer[ i ] ^= cryptInfo->currentIV[ i + ivCount ];
  310.         memcpy( cryptInfo->currentIV + ivCount, temp, bytesToUse );
  311.  
  312.         /* Adjust the byte count and buffer position */
  313.         noBytes -= bytesToUse;
  314.         buffer += bytesToUse;
  315.         ivCount += bytesToUse;
  316.         }
  317.  
  318.     while( noBytes )
  319.         {
  320.         ivCount = ( noBytes > DES_BLOCKSIZE ) ? DES_BLOCKSIZE : noBytes;
  321.  
  322.         /* Encrypt the IV */
  323.         des_ecb3_encrypt( ( C_Block * ) cryptInfo->currentIV,
  324.                           ( C_Block * ) cryptInfo->currentIV,
  325.                           des3Key->desKey1, des3Key->desKey2,
  326.                           des3Key->desKey3, DES_ENCRYPT );
  327.  
  328.         /* Save the ciphertext */
  329.         memcpy( temp, buffer, ivCount );
  330.  
  331.         /* XOR the buffer contents with the encrypted IV */
  332.         for( i = 0; i < ivCount; i++ )
  333.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  334.  
  335.         /* Shift the ciphertext into the IV */
  336.         memcpy( cryptInfo->currentIV, temp, ivCount );
  337.  
  338.         /* Move on to next block of data */
  339.         noBytes -= ivCount;
  340.         buffer += ivCount;
  341.         }
  342.  
  343.     /* Remember how much of the IV is still available for use */
  344.     cryptInfo->ivCount = ( ivCount % DES_BLOCKSIZE );
  345.  
  346.     /* Clear the temporary buffer */
  347.     zeroise( temp, DES_BLOCKSIZE );
  348.  
  349.     return( CRYPT_OK );
  350.     }
  351.  
  352. /* Encrypt/decrypt data in OFB mode */
  353.  
  354. int des3EncryptOFB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  355.     {
  356.     DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->key;
  357.     int i, ivCount = cryptInfo->ivCount;
  358.  
  359.     /* If there's any encrypted material left in the IV, use it now */
  360.     if( ivCount )
  361.         {
  362.         int bytesToUse;
  363.  
  364.         /* Find out how much material left in the encrypted IV we can use */
  365.         bytesToUse = DES_BLOCKSIZE - ivCount;
  366.         if( noBytes < bytesToUse )
  367.             bytesToUse = noBytes;
  368.  
  369.         /* Encrypt the data */
  370.         for( i = 0; i < bytesToUse; i++ )
  371.             buffer[ i ] ^= cryptInfo->currentIV[ i + ivCount ];
  372.  
  373.         /* Adjust the byte count and buffer position */
  374.         noBytes -= bytesToUse;
  375.         buffer += bytesToUse;
  376.         ivCount += bytesToUse;
  377.         }
  378.  
  379.     while( noBytes )
  380.         {
  381.         ivCount = ( noBytes > DES_BLOCKSIZE ) ? DES_BLOCKSIZE : noBytes;
  382.  
  383.         /* Encrypt the IV */
  384.         des_ecb3_encrypt( ( C_Block * ) cryptInfo->currentIV,
  385.                           ( C_Block * ) cryptInfo->currentIV,
  386.                           des3Key->desKey1, des3Key->desKey2,
  387.                           des3Key->desKey3, DES_ENCRYPT );
  388.  
  389.         /* XOR the buffer contents with the encrypted IV */
  390.         for( i = 0; i < ivCount; i++ )
  391.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  392.  
  393.         /* Move on to next block of data */
  394.         noBytes -= ivCount;
  395.         buffer += ivCount;
  396.         }
  397.  
  398.     /* Remember how much of the IV is still available for use */
  399.     cryptInfo->ivCount = ( ivCount % DES_BLOCKSIZE );
  400.  
  401.     return( CRYPT_OK );
  402.     }
  403.  
  404. /* Decrypt data in OFB mode */
  405.  
  406. int des3DecryptOFB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  407.     {
  408.     DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->key;
  409.     int i, ivCount = cryptInfo->ivCount;
  410.  
  411.     /* If there's any encrypted material left in the IV, use it now */
  412.     if( ivCount )
  413.         {
  414.         int bytesToUse;
  415.  
  416.         /* Find out how much material left in the encrypted IV we can use */
  417.         bytesToUse = DES_BLOCKSIZE - ivCount;
  418.         if( noBytes < bytesToUse )
  419.             bytesToUse = noBytes;
  420.  
  421.         /* Decrypt the data */
  422.         for( i = 0; i < bytesToUse; i++ )
  423.             buffer[ i ] ^= cryptInfo->currentIV[ i + ivCount ];
  424.  
  425.         /* Adjust the byte count and buffer position */
  426.         noBytes -= bytesToUse;
  427.         buffer += bytesToUse;
  428.         ivCount += bytesToUse;
  429.         }
  430.  
  431.     while( noBytes )
  432.         {
  433.         ivCount = ( noBytes > DES_BLOCKSIZE ) ? DES_BLOCKSIZE : noBytes;
  434.  
  435.         /* Encrypt the IV */
  436.         des_ecb3_encrypt( ( C_Block * ) cryptInfo->currentIV,
  437.                           ( C_Block * ) cryptInfo->currentIV,
  438.                           des3Key->desKey1, des3Key->desKey2,
  439.                           des3Key->desKey3, DES_ENCRYPT );
  440.  
  441.         /* XOR the buffer contents with the encrypted IV */
  442.         for( i = 0; i < ivCount; i++ )
  443.             buffer[ i ] ^= cryptInfo->currentIV[ i ];
  444.  
  445.         /* Move on to next block of data */
  446.         noBytes -= ivCount;
  447.         buffer += ivCount;
  448.         }
  449.  
  450.     /* Remember how much of the IV is still available for use */
  451.     cryptInfo->ivCount = ( ivCount % DES_BLOCKSIZE );
  452.  
  453.     return( CRYPT_OK );
  454.     }
  455.  
  456. /* Encrypt/decrypt data in PCBC mode.  We have to carry along the previous
  457.    block's plaintext as well as the IV so we store it after the IV in the
  458.    buffer allocated for IV storage.  Initially the plaintextChain value will
  459.    be null as the IV buffer is zeroed on init, so we don't need to worry
  460.    about the special case of the first ciphertext block */
  461.  
  462. int des3EncryptPCBC( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  463.     {
  464.     DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->key;
  465.     BYTE *plaintextChain = cryptInfo->currentIV + DES_BLOCKSIZE;
  466.     BYTE temp[ DES_BLOCKSIZE ];
  467.     int blockCount = noBytes / DES_BLOCKSIZE;
  468.  
  469.     /* Make sure the data length is a multiple of the block size */
  470.     if( noBytes % DES_BLOCKSIZE )
  471.         return( CRYPT_BADPARM3 );
  472.  
  473.     while( blockCount-- )
  474.         {
  475.         int i;
  476.  
  477.         /* Remember the previous block's plaintext and copy the current
  478.            plaintext for chaining in the next iteration */
  479.         memcpy( temp, plaintextChain, DES_BLOCKSIZE );
  480.         memcpy( plaintextChain, buffer, DES_BLOCKSIZE );
  481.  
  482.         /* XOR the buffer contents with the IV and previous plaintext */
  483.         for( i = 0; i < DES_BLOCKSIZE; i++ )
  484.             buffer[ i ] ^= cryptInfo->currentIV[ i ] ^ temp[ i ];
  485.  
  486.         /* Encrypt a block of data */
  487.         des_ecb3_encrypt( ( C_Block * ) buffer, ( C_Block * ) buffer,
  488.                           des3Key->desKey1, des3Key->desKey2,
  489.                           des3Key->desKey3, DES_ENCRYPT );
  490.  
  491.         /* Shift ciphertext into IV */
  492.         memcpy( cryptInfo->currentIV, buffer, DES_BLOCKSIZE );
  493.  
  494.         /* Move on to next block of data */
  495.         buffer += DES_BLOCKSIZE;
  496.         }
  497.  
  498.     /* Clear the temporary buffer */
  499.     zeroise( temp, DES_BLOCKSIZE );
  500.  
  501.     return( CRYPT_OK );
  502.     }
  503.  
  504. int des3DecryptPCBC( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
  505.     {
  506.     DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->key;
  507.     BYTE *plaintextChain = cryptInfo->currentIV + DES_BLOCKSIZE;
  508.     BYTE temp[ DES_BLOCKSIZE ];
  509.     int blockCount = noBytes / DES_BLOCKSIZE;
  510.  
  511.     /* Make sure the data length is a multiple of the block size */
  512.     if( noBytes % DES_BLOCKSIZE )
  513.         return( CRYPT_BADPARM3 );
  514.  
  515.     while( blockCount-- )
  516.         {
  517.         int i;
  518.  
  519.         /* Save the ciphertext */
  520.         memcpy( temp, buffer, DES_BLOCKSIZE );
  521.  
  522.         /* Decrypt a block of data */
  523.         des_ecb3_encrypt( ( C_Block * ) buffer, ( C_Block * ) buffer,
  524.                           des3Key->desKey1, des3Key->desKey2,
  525.                           des3Key->desKey3, DES_DECRYPT );
  526.  
  527.         /* XOR the buffer contents with the IV and previous plaintext */
  528.         for( i = 0; i < DES_BLOCKSIZE; i++ )
  529.             buffer[ i ] ^= cryptInfo->currentIV[ i ] ^ plaintextChain[ i ];
  530.  
  531.         /* Shift the ciphertext into the IV and copy the current plaintext
  532.            for chaining in the next iteration */
  533.         memcpy( cryptInfo->currentIV, temp, DES_BLOCKSIZE );
  534.         memcpy( plaintextChain, buffer, DES_BLOCKSIZE );
  535.  
  536.         /* Move on to next block of data */
  537.         buffer += DES_BLOCKSIZE;
  538.         }
  539.  
  540.     /* Clear the temporary buffer */
  541.     zeroise( temp, DES_BLOCKSIZE );
  542.  
  543.     return( CRYPT_OK );
  544.     }
  545.  
  546. /****************************************************************************
  547. *                                                                            *
  548. *                            3DES Key Management Routines                        *
  549. *                                                                            *
  550. ****************************************************************************/
  551.  
  552. /* Get/set algorithm-specific parameters */
  553.  
  554. BOOLEAN get3DESinfo( const CRYPT_INFO *cryptInfo )
  555.     {
  556.     return( ( ( DES3_KEY * ) cryptInfo->key )->isThreeKey );
  557.     }
  558.  
  559. void set3DESinfo( CRYPT_INFO *cryptInfo, const BOOLEAN isThreeKey )
  560.     {
  561.     ( ( DES3_KEY * ) cryptInfo->key )->isThreeKey = isThreeKey;
  562.     }
  563.  
  564. int des3GetKeysize( CRYPT_INFO *cryptInfo )
  565.     {
  566.     return( ( ( ( DES3_KEY * ) cryptInfo->key )->isThreeKey ) ? \
  567.             bitsToBytes( 64 * 3 ) : bitsToBytes( 64 * 2 ) );
  568.     }
  569.  
  570. /* Key schedule two/three DES keys */
  571.  
  572. int des3InitKey( CRYPT_INFO *cryptInfo )
  573.     {
  574.     DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->key;
  575.  
  576.     /* If it's a long key, make sure we're using three-key triple DES */
  577.     if( cryptInfo->userKeyLength > bitsToBytes( 64 * 2 ) && \
  578.         !des3Key->isThreeKey )
  579.         return( CRYPT_BADPARM3 );
  580.  
  581.     /* Call the libdes key schedule code.  Returns with -1 if the key parity
  582.        is wrong (which never occurs since we force the correct parity) or -2
  583.        if a weak key is used */
  584.     des_set_odd_parity( ( C_Block * ) cryptInfo->userKey );
  585.     if( key_sched( ( des_cblock * ) cryptInfo->userKey, des3Key->desKey1 ) )
  586.         return( CRYPT_BADPARM );
  587.     des_set_odd_parity( ( C_Block * ) ( cryptInfo->userKey + bitsToBytes( 64 ) ) );
  588.     if( key_sched( ( des_cblock * ) ( cryptInfo->userKey + bitsToBytes( 64 ) ),
  589.                    des3Key->desKey2 ) )
  590.         return( CRYPT_BADPARM );
  591.     if( des3Key->isThreeKey )
  592.         {
  593.         des_set_odd_parity( ( C_Block * ) ( cryptInfo->userKey + bitsToBytes( 128 ) ) );
  594.         if( key_sched( ( des_cblock * ) ( cryptInfo->userKey + bitsToBytes( 128 ) ),
  595.                        des3Key->desKey3 ) )
  596.             return( CRYPT_BADPARM );
  597.         }
  598.     else
  599.         if( key_sched( ( des_cblock * ) cryptInfo->userKey, des3Key->desKey3 ) )
  600.             return( CRYPT_BADPARM );
  601.  
  602.     return( CRYPT_OK );
  603.     }
  604.