📄 lib_3des.c
字号:
/****************************************************************************
* *
* cryptlib Triple DES Encryption Routines *
* Copyright Peter Gutmann 1992-1996 *
* *
****************************************************************************/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "crypt.h"
#include "cryptctx.h"
#ifdef INC_ALL
#include "des.h"
#else
#include "crypt/des.h"
#endif /* Compiler-specific includes */
/* The DES block size */
#define DES_BLOCKSIZE 8
#ifdef INC_ALL
#include "testdes.h"
#else
#include "crypt/testdes.h"
#endif /* Compiler-specific includes */
/* A structure to hold the keyscheduled DES keys */
typedef struct {
Key_schedule desKey1; /* The first DES key */
Key_schedule desKey2; /* The second DES key */
Key_schedule desKey3; /* The third DES key */
} DES3_KEY;
/* The size of the keyscheduled DES and 3DES keys */
#define DES_KEYSIZE sizeof( Key_schedule )
#define DES3_KEYSIZE sizeof( DES3_KEY )
/****************************************************************************
* *
* 3DES Self-test Routines *
* *
****************************************************************************/
/* Test the DES implementation against the test vectors given in NBS Special
Publication 800-20, 1999 (which are actually the same as 500-20, 1980,
since they require that K1 = K2 = K3, but we do it anyway so we can claim
compliance) */
static int des3TestLoop( DES_TEST *testData, int iterations )
{
BYTE temp[ DES_BLOCKSIZE ];
BYTE key1[ DES_KEYSIZE ], key2[ DES_KEYSIZE ], key3[ DES_KEYSIZE ];
int i;
for( i = 0; i < iterations; i++ )
{
memcpy( temp, testData[ i ].plaintext, DES_BLOCKSIZE );
key_sched( ( C_Block * ) testData[ i ].key,
*( ( Key_schedule * ) key1 ) );
key_sched( ( C_Block * ) testData[ i ].key,
*( ( Key_schedule * ) key2 ) );
key_sched( ( C_Block * ) testData[ i ].key,
*( ( Key_schedule * ) key3 ) );
des_ecb3_encrypt( ( C_Block * ) temp, ( C_Block * ) temp,
*( ( Key_schedule * ) key1 ),
*( ( Key_schedule * ) key2 ),
*( ( Key_schedule * ) key3 ), DES_ENCRYPT );
if( memcmp( testData[ i ].ciphertext, temp, DES_BLOCKSIZE ) )
return( CRYPT_ERROR );
}
return( CRYPT_OK );
}
int des3SelfTest( void )
{
int status = CRYPT_OK;
/* Since the self-test uses weak keys, we have to turn off the checking
for key parity errors and weak keys until it's completed */
des_check_key = FALSE;
/* Check the 3DES test vectors. Note that we don't perform the RS test,
since it's valid only for single DES */
if( ( des3TestLoop( testIP, sizeof( testIP ) / sizeof( DES_TEST ) ) != CRYPT_OK ) || \
( des3TestLoop( testVP, sizeof( testVP ) / sizeof( DES_TEST ) ) != CRYPT_OK ) || \
( des3TestLoop( testKP, sizeof( testKP ) / sizeof( DES_TEST ) ) != CRYPT_OK ) || \
( des3TestLoop( testDP, sizeof( testDP ) / sizeof( DES_TEST ) ) != CRYPT_OK ) || \
( des3TestLoop( testSB, sizeof( testSB ) / sizeof( DES_TEST ) ) != CRYPT_OK ) )
status = CRYPT_ERROR;
/* Reenable checking for key parity errors and weak keys */
des_check_key = TRUE;
return( status );
}
/****************************************************************************
* *
* Init/Shutdown Routines *
* *
****************************************************************************/
/* Perform init and shutdown actions on an encryption context */
int des3Init( CRYPT_INFO *cryptInfo )
{
int status;
/* Allocate memory for the keyscheduled keys */
if( ( status = krnlMemalloc( &cryptInfo->ctxConv.key, DES3_KEYSIZE ) ) != CRYPT_OK )
return( status );
cryptInfo->ctxConv.keyLength = DES3_KEYSIZE;
return( CRYPT_OK );
}
int des3End( CRYPT_INFO *cryptInfo )
{
/* Free any allocated memory */
krnlMemfree( &cryptInfo->ctxConv.key );
return( CRYPT_OK );
}
/****************************************************************************
* *
* 3DES En/Decryption Routines *
* *
****************************************************************************/
/* Encrypt/decrypt data in ECB mode */
int des3EncryptECB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
{
DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->ctxConv.key;
int blockCount = noBytes / DES_BLOCKSIZE;
while( blockCount-- )
{
/* Encrypt a block of data */
des_ecb3_encrypt( ( C_Block * ) buffer, ( C_Block * ) buffer,
des3Key->desKey1, des3Key->desKey2,
des3Key->desKey3, DES_ENCRYPT );
/* Move on to next block of data */
buffer += DES_BLOCKSIZE;
}
return( CRYPT_OK );
}
int des3DecryptECB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
{
DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->ctxConv.key;
int blockCount = noBytes / DES_BLOCKSIZE;
while( blockCount-- )
{
/* Decrypt a block of data */
des_ecb3_encrypt( ( C_Block * ) buffer, ( C_Block * ) buffer,
des3Key->desKey1, des3Key->desKey2,
des3Key->desKey3, DES_DECRYPT );
/* Move on to next block of data */
buffer += DES_BLOCKSIZE;
}
return( CRYPT_OK );
}
/* Encrypt/decrypt data in CBC mode */
int des3EncryptCBC( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
{
DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->ctxConv.key;
#if 0
int blockCount = noBytes / DES_BLOCKSIZE;
while( blockCount-- )
{
int i;
/* XOR the buffer contents with the IV */
for( i = 0; i < DES_BLOCKSIZE; i++ )
buffer[ i ] ^= cryptInfo->currentIV[ i ];
/* Encrypt a block of data */
des_ecb3_encrypt( ( C_Block * ) buffer, ( C_Block * ) buffer,
des3Key->desKey1, des3Key->desKey2,
des3Key->desKey3, DES_ENCRYPT );
/* Shift ciphertext into IV */
memcpy( cryptInfo->currentIV, buffer, DES_BLOCKSIZE );
/* Move on to next block of data */
buffer += DES_BLOCKSIZE;
}
#endif
/* Encrypt the buffer of data */
des_ede3_cbc_encrypt( ( C_Block * ) buffer, ( C_Block * ) buffer, noBytes,
des3Key->desKey1, des3Key->desKey2, des3Key->desKey3,
( C_Block * ) cryptInfo->ctxConv.currentIV,
DES_ENCRYPT );
return( CRYPT_OK );
}
int des3DecryptCBC( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
{
DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->ctxConv.key;
#if 0
BYTE temp[ DES_BLOCKSIZE ];
int blockCount = noBytes / DES_BLOCKSIZE;
while( blockCount-- )
{
int i;
/* Save the ciphertext */
memcpy( temp, buffer, DES_BLOCKSIZE );
/* Decrypt a block of data */
des_ecb3_encrypt( ( C_Block * ) buffer, ( C_Block * ) buffer,
des3Key->desKey1, des3Key->desKey2,
des3Key->desKey3, DES_DECRYPT );
/* XOR the buffer contents with the IV */
for( i = 0; i < DES_BLOCKSIZE; i++ )
buffer[ i ] ^= cryptInfo->currentIV[ i ];
/* Shift the ciphertext into the IV */
memcpy( cryptInfo->currentIV, temp, DES_BLOCKSIZE );
/* Move on to next block of data */
buffer += DES_BLOCKSIZE;
}
/* Clear the temporary buffer */
zeroise( temp, DES_BLOCKSIZE );
#endif
/* Encrypt the buffer of data */
des_ede3_cbc_encrypt( ( C_Block * ) buffer, ( C_Block * ) buffer, noBytes,
des3Key->desKey1, des3Key->desKey2, des3Key->desKey3,
( C_Block * ) cryptInfo->ctxConv.currentIV,
DES_DECRYPT );
return( CRYPT_OK );
}
/* Encrypt/decrypt data in CFB mode */
int des3EncryptCFB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
{
DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->ctxConv.key;
int i, ivCount = cryptInfo->ctxConv.ivCount;
/* If there's any encrypted material left in the IV, use it now */
if( ivCount )
{
int bytesToUse;
/* Find out how much material left in the encrypted IV we can use */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -