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

  1. /****************************************************************************
  2. *                                                                            *
  3. *                          Blowfish Encryption Algorithm                     *
  4. *                        Copyright Peter Gutmann 1995-1996                    *
  5. *                                                                            *
  6. ****************************************************************************/
  7.  
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #if defined( INC_ALL )
  11.   #include "crypt.h"
  12.   #include "blowfish.h"
  13. #elif defined( INC_CHILD )
  14.   #include "../crypt.h"
  15.   #include "blowfish.h"
  16. #else
  17.   #include "crypt.h"
  18.   #include "blowfish/blowfish.h"
  19. #endif /* Compiler-specific includes */
  20.  
  21. /* LCRNG start value */
  22.  
  23. #define LCRNG_START                1
  24.  
  25. /* Test vectors for the LCRNG and Blowfish-SK itself */
  26.  
  27. #define LCRNG_INITIAL            23312U
  28. #define LCRNG_FINAL                23021U
  29.  
  30. #define BLOWFISH_PLAINTEXT        "\x00\x00\x00\x00\x00\x00\x00\x00"
  31. #define BLOWFISH_CIPHERTEXT        "\x4D\x38\xE6\x00\x47\x35\x24\xCF"
  32.  
  33. /* The LCRNG used for the key setup */
  34.  
  35. #define lcrng(number)    ( WORD ) ( ( ( number * 23311L ) + 1 ) % 65533U )
  36.  
  37. /* Macros to extract 8-bit values a, b, c, d from a 32-bit value.  The cast
  38.    is necessary because some compilers prefer ints as array indices */
  39.  
  40. #define exta(x)        ( ( int ) ( ( x >> 24 ) & 0xFF ) )
  41. #define extb(x)        ( ( int ) ( ( x >> 16 ) & 0xFF ) )
  42. #define extc(x)        ( ( int ) ( ( x >> 8 ) & 0xFF ) )
  43. #define extd(x)        ( ( int ) ( ( x ) & 0xFF ) )
  44.  
  45. /* The f-function */
  46.  
  47. #define f(data,S1,S2,S3,S4)        \
  48.     ( ( ( S1[ exta( data ) ] + S2[ extb( data ) ] ) ^ S3[ extc( data ) ] ) + \
  49.                                                       S4[ extd( data ) ] )
  50. /* The individual encrypt/decrypt rounds */
  51.  
  52. #define oddRoundE(count,P,S1,S2,S3,S4)    L ^= P[ count - 1 ]; \
  53.                                         R ^= f( L, S1, S2, S3, S4 )
  54. #define evenRoundE(count,P,S1,S2,S3,S4)    R ^= P[ count - 1 ]; \
  55.                                         L ^= f( R, S1, S2, S3, S4 )
  56. #define oddRoundD(count,P,S1,S2,S3,S4)    L ^= P[ count + 1 ]; \
  57.                                         R ^= f( L, S1, S2, S3, S4 )
  58. #define evenRoundD(count,P,S1,S2,S3,S4)    R ^= P[ count + 1 ]; \
  59.                                         L ^= f( R, S1, S2, S3, S4 )
  60.  
  61. void blowfishEncrypt( BLOWFISH_KEY *key, BYTE *data )
  62.     {
  63.     BYTE *dataPtr = data;
  64.     LONG *P = key->P, *S1 = key->S1, *S2 = key->S2, *S3 = key->S3, *S4 = key->S4;
  65.     LONG L, R;
  66.  
  67.     L = mgetBLong( dataPtr );
  68.     R = mgetBLong( dataPtr );
  69.  
  70.     /* Perform 16 rounds of encryption */
  71.     oddRoundE(   1, P, S1, S2, S3, S4 );
  72.     evenRoundE(  2, P, S1, S2, S3, S4 );
  73.     oddRoundE(   3, P, S1, S2, S3, S4 );
  74.     evenRoundE(  4, P, S1, S2, S3, S4 );
  75.     oddRoundE(   5, P, S1, S2, S3, S4 );
  76.     evenRoundE(  6, P, S1, S2, S3, S4 );
  77.     oddRoundE(   7, P, S1, S2, S3, S4 );
  78.     evenRoundE(  8, P, S1, S2, S3, S4 );
  79.     oddRoundE(   9, P, S1, S2, S3, S4 );
  80.     evenRoundE( 10, P, S1, S2, S3, S4 );
  81.     oddRoundE(  11, P, S1, S2, S3, S4 );
  82.     evenRoundE( 12, P, S1, S2, S3, S4 );
  83.     oddRoundE(  13, P, S1, S2, S3, S4 );
  84.     evenRoundE( 14, P, S1, S2, S3, S4 );
  85.     oddRoundE(  15, P, S1, S2, S3, S4 );
  86.     evenRoundE( 16, P, S1, S2, S3, S4 );
  87.  
  88.     /* Perform the final XOR's */
  89.     L ^= P[ 16 ];
  90.     R ^= P[ 17 ];
  91.  
  92.     dataPtr = data;
  93.     mputBLong( dataPtr, R );
  94.     mputBLong( dataPtr, L );
  95.     }
  96.  
  97. void blowfishDecrypt( BLOWFISH_KEY *key, BYTE *data )
  98.     {
  99.     BYTE *dataPtr = data;
  100.     LONG *P = key->P, *S1 = key->S1, *S2 = key->S2, *S3 = key->S3, *S4 = key->S4;
  101.     LONG L, R;
  102.  
  103.     R = mgetBLong( dataPtr );
  104.     L = mgetBLong( dataPtr );
  105.  
  106.     /* Perform 16 rounds of encryption */
  107.     evenRoundD( 16, P, S1, S2, S3, S4 );
  108.     oddRoundD(  15, P, S1, S2, S3, S4 );
  109.     evenRoundD( 14, P, S1, S2, S3, S4 );
  110.     oddRoundD(  13, P, S1, S2, S3, S4 );
  111.     evenRoundD( 12, P, S1, S2, S3, S4 );
  112.     oddRoundD(  11, P, S1, S2, S3, S4 );
  113.     evenRoundD( 10, P, S1, S2, S3, S4 );
  114.     oddRoundD(   9, P, S1, S2, S3, S4 );
  115.     evenRoundD(  8, P, S1, S2, S3, S4 );
  116.     oddRoundD(   7, P, S1, S2, S3, S4 );
  117.     evenRoundD(  6, P, S1, S2, S3, S4 );
  118.     oddRoundD(   5, P, S1, S2, S3, S4 );
  119.     evenRoundD(  4, P, S1, S2, S3, S4 );
  120.     oddRoundD(   3, P, S1, S2, S3, S4 );
  121.     evenRoundD(  2, P, S1, S2, S3, S4 );
  122.     oddRoundD(   1, P, S1, S2, S3, S4 );
  123.  
  124.     /* Perform the final XOR's */
  125.     R ^= P[ 1 ];
  126.     L ^= P[ 0 ];
  127.  
  128.     dataPtr = data;
  129.     mputBLong( dataPtr, L );
  130.     mputBLong( dataPtr, R );
  131.     }
  132.  
  133. /****************************************************************************
  134. *                                                                            *
  135. *                        Blowfish Key Management Routines                    *
  136. *                                                                            *
  137. ****************************************************************************/
  138.  
  139. /* Get the initial values of the P-array and S-boxes from an external file */
  140.  
  141. #if defined( INC_ALL ) || defined( INC_CHILD )
  142.   #include "bf_init.c"
  143. #else
  144.   #include "blowfish/bf_init.c"
  145. #endif /* _MSC_VER */
  146.  
  147. /* Various defines needed for the key setup */
  148.  
  149. #define BLOWFISH_NO_ROUNDS    16
  150.  
  151. /* Set up a Blowfish S-box */
  152.  
  153. static void initSBox( BLOWFISH_KEY *key, LONG *Sbox, BYTE *buffer )
  154.     {
  155.     int sBoxIndex;
  156.  
  157.     for( sBoxIndex = 0; sBoxIndex < 256; sBoxIndex += 2 )
  158.         {
  159.         BYTE *bufferPtr = buffer;
  160.  
  161.         blowfishEncrypt( key, buffer );
  162.         Sbox[ sBoxIndex ] = mgetBLong( bufferPtr );
  163.         Sbox[ sBoxIndex + 1 ] = mgetBLong( bufferPtr );
  164.         }
  165.     }
  166.  
  167. /* Set up a Blowfish key */
  168.  
  169. int blowfishKeyInit( BLOWFISH_KEY *key, BYTE *userKey, int userKeyLength )
  170.     {
  171.     BYTE buffer[ BLOWFISH_BLOCKSIZE ];
  172.     int keyIndex = 0, i;
  173.  
  174.     /* Set up the initial P-array and S-boxes based on the digits of pi */
  175.     memcpy( key->P, initialParray, sizeof( initialParray ) );
  176.     memcpy( key->S1, initialSbox1, sizeof( initialSbox1 ) );
  177.     memcpy( key->S2, initialSbox2, sizeof( initialSbox2 ) );
  178.     memcpy( key->S3, initialSbox3, sizeof( initialSbox3 ) );
  179.     memcpy( key->S4, initialSbox4, sizeof( initialSbox4 ) );
  180.  
  181.     /* XOR the user key bits into the P-array */
  182.     for( i = 0; i < BLOWFISH_NO_ROUNDS + 2; i++ )
  183.         {
  184.         LONG value = 0L;    /* Needed for > 32-bit processors */
  185.         int byteIndex;
  186.  
  187.         /* Get 32 bits of user key and XOR them into the P-array */
  188.         for( byteIndex = 0; byteIndex < 4; byteIndex++ )
  189.             {
  190.             value = ( value << 8 ) | userKey[ keyIndex++ ];
  191.             keyIndex %= userKeyLength;
  192.             }
  193.         key->P[ i ] = key->P[ i ] ^ value;
  194.         }
  195.  
  196.     /* Encrypt the all-zero string with the initial P-array to get the final
  197.        P-array */
  198.     memset( buffer, 0, BLOWFISH_BLOCKSIZE );
  199.     for( i = 0; i < BLOWFISH_NO_ROUNDS + 2; i += 2 )
  200.         {
  201.         BYTE *bufferPtr = buffer;
  202.  
  203.         blowfishEncrypt( key, buffer );
  204.         key->P[ i ] = mgetBLong( bufferPtr );
  205.         key->P[ i + 1 ] = mgetBLong( bufferPtr );
  206.         }
  207.  
  208.     /* Continue the process to fill the S-boxes */
  209.     initSBox( key, key->S1, buffer );
  210.     initSBox( key, key->S2, buffer );
  211.     initSBox( key, key->S3, buffer );
  212.     initSBox( key, key->S4, buffer );
  213.  
  214.     return( CRYPT_OK );
  215.     }
  216.  
  217. /****************************************************************************
  218. *                                                                            *
  219. *                        Blowfish-SK Key Management Routines                    *
  220. *                                                                            *
  221. ****************************************************************************/
  222.  
  223. /* Set the P-array and S-boxes from a buffer full of keying material */
  224.  
  225. static void setKeyData( BLOWFISH_KEY *key, BYTE *keyBufPtr )
  226.     {
  227.     LONG *P = key->P, *S1 = key->S1, *S2 = key->S2, *S3 = key->S3, *S4 = key->S4;
  228.     int i;
  229.  
  230.     /* Move the data from the keybuffer into the P-array and S-boxes,
  231.        converting it to the local endianness in the process.  We set the
  232.        four S-boxes one after the other rather than in parallel for
  233.        compatibility with code which treats them as a single large set of
  234.        S-boxes */
  235.     for( i = 0; i < BLOWFISH_PARRAY_SIZE; i++ )
  236.         { P[ i ] = mgetBLong( keyBufPtr ); }
  237.     for( i = 0; i < BLOWFISH_SBOX_SIZE; i++ )
  238.         { S1[ i ] = mgetBLong( keyBufPtr ); }
  239.     for( i = 0; i < BLOWFISH_SBOX_SIZE; i++ )
  240.         { S2[ i ] = mgetBLong( keyBufPtr ); }
  241.     for( i = 0; i < BLOWFISH_SBOX_SIZE; i++ )
  242.         { S3[ i ] = mgetBLong( keyBufPtr ); }
  243.     for( i = 0; i < BLOWFISH_SBOX_SIZE; i++ )
  244.         { S4[ i ] = mgetBLong( keyBufPtr ); }
  245.     }
  246.  
  247. /* Encrypt data in CFB mode */
  248.  
  249. static void encryptCFB( BLOWFISH_KEY *key, BYTE *iv, BYTE *buffer, int noBytes )
  250.     {
  251.     int i, ivCount;
  252.  
  253.     while( noBytes )
  254.         {
  255.         ivCount = ( noBytes > BLOWFISH_BLOCKSIZE ) ? BLOWFISH_BLOCKSIZE : noBytes;
  256.  
  257.         /* Encrypt the IV */
  258.         blowfishEncrypt( key, iv );
  259.  
  260.         /* XOR the buffer contents with the encrypted IV */
  261.         for( i = 0; i < ivCount; i++ )
  262.             buffer[ i ] ^= iv[ i ];
  263.  
  264.         /* Shift the ciphertext into the IV */
  265.         memcpy( iv, buffer, ivCount );
  266.  
  267.         /* Move on to next block of data */
  268.         noBytes -= ivCount;
  269.         buffer += ivCount;
  270.         }
  271.     }
  272.  
  273. /* Set up a Blowfish-SK key */
  274.  
  275. int blowfishKeyInitSK( BLOWFISH_KEY *key, BYTE *userKey, int userKeyLength, \
  276.                        int keySetupIterations )
  277.     {
  278.     BYTE *keyData, iv[ BLOWFISH_BLOCKSIZE ];
  279.     WORD lcrngNumber = LCRNG_START;
  280.     int count;
  281.  
  282.     /* Check that the LCRNG is implemented correctly */
  283.     if( lcrng( lcrngNumber ) != LCRNG_INITIAL )
  284.         return( CRYPT_SELFTEST );
  285.  
  286.     /* Some environments have real problems allocating the keyData array on
  287.        the stack.  Under Win16 the program will just exit quietly with no
  288.        indication of what went wrong.  To avoid this we need to allocate the
  289.        buffer dynamically.  Since the Blowfish-SK key setup is quite slow
  290.        anyway, the extra malloc() won't be noticed */
  291.     if( ( keyData = ( BYTE * ) malloc( BLOWFISH_KEYSIZE_BYTES ) ) == NULL )
  292.         return( CRYPT_NOMEM );
  293.  
  294.     /* Initialise the P-array and S-boxes */
  295.     for( count = 0; count < BLOWFISH_KEYSIZE_BYTES; count++ )
  296.         {
  297.         lcrngNumber = lcrng( lcrngNumber );
  298.         keyData[ count ] = ( BYTE ) lcrngNumber;
  299.         }
  300.     if( lcrng( lcrngNumber ) != LCRNG_FINAL )
  301.         {
  302.         free( keyData );
  303.         return( CRYPT_SELFTEST );
  304.         }
  305.     setKeyData( key, keyData );
  306.  
  307.     /* Make sure the encryption works OK */
  308.     memcpy( iv, BLOWFISH_PLAINTEXT, BLOWFISH_BLOCKSIZE );
  309.     blowfishEncrypt( key, iv );
  310.     if( memcmp( iv, BLOWFISH_CIPHERTEXT, BLOWFISH_BLOCKSIZE ) )
  311.         {
  312.         free( keyData );
  313.         return( CRYPT_SELFTEST );
  314.         }
  315.     blowfishDecrypt( key, iv );
  316.     if( memcmp( iv, BLOWFISH_PLAINTEXT, BLOWFISH_BLOCKSIZE ) )
  317.         {
  318.         free( keyData );
  319.         return( CRYPT_SELFTEST );
  320.         }
  321.  
  322.     /* Copy the user key (zero-padded) into the keybuffer */
  323.     memset( keyData, 0, BLOWFISH_KEYSIZE_BYTES );
  324.     keyData[ 0 ] = ( BYTE ) ( userKeyLength >> 8 );
  325.     keyData[ 1 ] = ( BYTE ) userKeyLength;
  326.     userKeyLength %= BLOWFISH_KEYSIZE_BYTES - sizeof( WORD );
  327.     memcpy( keyData + sizeof( WORD ), userKey, userKeyLength );
  328.  
  329.     /* Encrypt the keyData with the given IV and then set the key to the
  330.        encrypted keyData (initially the IV is still 0 from the self-test).
  331.        The act of encryption also sets the IV for the next iteration */
  332.     for( count = 0; count < keySetupIterations; count++ )
  333.         {
  334.         encryptCFB( key, iv, keyData, BLOWFISH_KEYSIZE_BYTES );
  335.         setKeyData( key, keyData );
  336.         }
  337.  
  338.     /* Perform one last copy in case they've specified zero iterations and
  339.        the loop was never executed */
  340.     setKeyData( key, keyData );
  341.  
  342.     /* Wipe the keyData and IV */
  343.     zeroise( keyData, BLOWFISH_KEYSIZE_BYTES );
  344.     zeroise( iv, BLOWFISH_BLOCKSIZE );
  345.     free( keyData );
  346.  
  347.     return( CRYPT_OK );
  348.     }
  349.  
  350. #ifdef TEST
  351.  
  352. /* Test routines */
  353.  
  354. #include <stdio.h>
  355.  
  356. #ifdef __TURBOC__        /* Only 4K stack under DOS - blechh */
  357. extern unsigned _stklen = 8192;
  358. #endif /* __TURBOC__ */
  359.  
  360. int main( void )
  361.     {
  362.     BYTE *plain1 = ( BYTE * ) "BLOWFISH";
  363.     BYTE *key1 = ( BYTE * ) "abcdefghijklmnopqrstuvwxyz";
  364.     BYTE *cipher1 = ( BYTE * ) "\x32\x4E\xD0\xFE\xF4\x13\xA2\x03";
  365.     BYTE *plain2 = ( BYTE * ) "\xFE\xDC\xBA\x98\x76\x54\x32\x10";
  366.     BYTE *key2 = ( BYTE * ) "Who is John Galt?";
  367.     BYTE *cipher2 = ( BYTE * ) "\xCC\x91\x73\x2B\x80\x22\xF6\x84";
  368.     BLOWFISH_KEY bfKey;
  369.     BYTE buffer[ 8 ];
  370.  
  371.     memcpy( buffer, plain1, 8 );
  372.     if( blowfishKeyInit( &bfKey, key1, strlen( ( char * ) key1 ) ) != CRYPT_OK )
  373.         puts( "Init failed" );
  374.     blowfishEncrypt( &bfKey, buffer );
  375.     if( memcmp( buffer, cipher1, 8 ) )
  376.         return( CRYPT_ERROR );
  377.     blowfishDecrypt( &bfKey, buffer );
  378.     if( memcmp( buffer, plain1, 8 ) )
  379.         return( CRYPT_ERROR );
  380.     memcpy( buffer, plain2, 8 );
  381.     if( blowfishKeyInit( &bfKey, key2, strlen( ( char * ) key2 ) ) != CRYPT_OK )
  382.         puts( "Init failed" );
  383.     blowfishEncrypt( &bfKey, buffer );
  384.     if( memcmp( buffer, cipher2, 8 ) )
  385.         return( CRYPT_ERROR );
  386.     blowfishDecrypt( &bfKey, buffer );
  387.     if( memcmp( buffer, plain2, 8 ) )
  388.         return( CRYPT_ERROR );
  389.  
  390.     return( CRYPT_OK );
  391.     }
  392. #endif /* TEST */
  393.