📄 ctx_3des.c
字号:
/****************************************************************************
* *
* cryptlib Triple DES Encryption Routines *
* Copyright Peter Gutmann 1994-2005 *
* *
****************************************************************************/
#if defined( INC_ALL )
#include "crypt.h"
#include "context.h"
#include "des.h"
#else
#include "crypt.h"
#include "context/context.h"
#include "crypt/des.h"
#endif /* Compiler-specific includes */
/* The DES block size */
#define DES_BLOCKSIZE 8
#if defined( 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 testLoop( const DES_TEST *testDES, int iterations )
{
const CAPABILITY_INFO *capabilityInfo = get3DESCapability();
BYTE keyData[ DES3_KEYSIZE + 8 ];
int i, status;
for( i = 0; i < iterations; i++ )
{
BYTE desKeyData[ ( DES_BLOCKSIZE * 3 ) + 8 ];
memcpy( desKeyData, testDES[ i ].key, DES_BLOCKSIZE );
memcpy( desKeyData + DES_BLOCKSIZE, testDES[ i ].key,
DES_BLOCKSIZE );
memcpy( desKeyData + ( DES_BLOCKSIZE * 2 ), testDES[ i ].key,
DES_BLOCKSIZE );
/* The self-test uses weak keys, which means they'll be rejected by
the key-load function if it checks for these. For the OpenSSL
DES implementation we can kludge around this by temporarily
clearing the global des_check_key value, but for other
implementations some alternative workaround will be necessary */
des_check_key = FALSE;
status = testCipher( capabilityInfo, keyData, desKeyData,
DES_BLOCKSIZE * 3, testDES[ i ].plaintext,
testDES[ i ].ciphertext );
des_check_key = TRUE;
if( cryptStatusError( status ) )
return( status );
}
return( CRYPT_OK );
}
static int selfTest( void )
{
/* Check the 3DES test vectors. Note that we don't perform the RS test,
since it's valid only for single DES */
if( ( testLoop( testIP, sizeof( testIP ) / sizeof( DES_TEST ) ) != CRYPT_OK ) || \
( testLoop( testVP, sizeof( testVP ) / sizeof( DES_TEST ) ) != CRYPT_OK ) || \
( testLoop( testKP, sizeof( testKP ) / sizeof( DES_TEST ) ) != CRYPT_OK ) || \
( testLoop( testDP, sizeof( testDP ) / sizeof( DES_TEST ) ) != CRYPT_OK ) || \
( testLoop( testSB, sizeof( testSB ) / sizeof( DES_TEST ) ) != CRYPT_OK ) )
return( CRYPT_ERROR_FAILED );
return( CRYPT_OK );
}
/****************************************************************************
* *
* Control Routines *
* *
****************************************************************************/
/* Return context subtype-specific information */
static int getInfo( const CAPABILITY_INFO_TYPE type, const void *ptrParam,
const int intParam, int *result )
{
if( type == CAPABILITY_INFO_STATESIZE )
{
*result = DES3_KEYSIZE;
return( CRYPT_OK );
}
return( getDefaultInfo( type, ptrParam, intParam, result ) );
}
/****************************************************************************
* *
* 3DES En/Decryption Routines *
* *
****************************************************************************/
/* Encrypt/decrypt data in ECB mode */
static int encryptECB( CONTEXT_INFO *contextInfoPtr, BYTE *buffer,
int noBytes )
{
CONV_INFO *convInfo = contextInfoPtr->ctxConv;
DES3_KEY *des3Key = ( DES3_KEY * ) convInfo->key;
int blockCount = noBytes / DES_BLOCKSIZE;
while( blockCount-- > 0 )
{
/* 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 );
}
static int decryptECB( CONTEXT_INFO *contextInfoPtr, BYTE *buffer,
int noBytes )
{
CONV_INFO *convInfo = contextInfoPtr->ctxConv;
DES3_KEY *des3Key = ( DES3_KEY * ) convInfo->key;
int blockCount = noBytes / DES_BLOCKSIZE;
while( blockCount-- > 0 )
{
/* 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 */
static int encryptCBC( CONTEXT_INFO *contextInfoPtr, BYTE *buffer,
int noBytes )
{
CONV_INFO *convInfo = contextInfoPtr->ctxConv;
DES3_KEY *des3Key = ( DES3_KEY * ) convInfo->key;
des_ede3_cbc_encrypt( buffer, buffer, noBytes,
des3Key->desKey1, des3Key->desKey2, des3Key->desKey3,
( C_Block * ) convInfo->currentIV, DES_ENCRYPT );
return( CRYPT_OK );
}
static int decryptCBC( CONTEXT_INFO *contextInfoPtr, BYTE *buffer,
int noBytes )
{
CONV_INFO *convInfo = contextInfoPtr->ctxConv;
DES3_KEY *des3Key = ( DES3_KEY * ) convInfo->key;
des_ede3_cbc_encrypt( buffer, buffer, noBytes,
des3Key->desKey1, des3Key->desKey2, des3Key->desKey3,
( C_Block * ) convInfo->currentIV, DES_DECRYPT );
return( CRYPT_OK );
}
/* Encrypt/decrypt data in CFB mode */
static int encryptCFB( CONTEXT_INFO *contextInfoPtr, BYTE *buffer,
int noBytes )
{
CONV_INFO *convInfo = contextInfoPtr->ctxConv;
DES3_KEY *des3Key = ( DES3_KEY * ) convInfo->key;
int i, ivCount = convInfo->ivCount;
/* If there's any encrypted material left in the IV, use it now */
if( ivCount > 0 )
{
int bytesToUse;
/* Find out how much material left in the encrypted IV we can use */
bytesToUse = DES_BLOCKSIZE - ivCount;
if( noBytes < bytesToUse )
bytesToUse = noBytes;
/* Encrypt the data */
for( i = 0; i < bytesToUse; i++ )
buffer[ i ] ^= convInfo->currentIV[ i + ivCount ];
memcpy( convInfo->currentIV + ivCount, buffer, bytesToUse );
/* Adjust the byte count and buffer position */
noBytes -= bytesToUse;
buffer += bytesToUse;
ivCount += bytesToUse;
}
while( noBytes > 0 )
{
ivCount = ( noBytes > DES_BLOCKSIZE ) ? DES_BLOCKSIZE : noBytes;
/* Encrypt the IV */
des_ecb3_encrypt( ( C_Block * ) convInfo->currentIV,
( C_Block * ) convInfo->currentIV,
des3Key->desKey1, des3Key->desKey2,
des3Key->desKey3, DES_ENCRYPT );
/* XOR the buffer contents with the encrypted IV */
for( i = 0; i < ivCount; i++ )
buffer[ i ] ^= convInfo->currentIV[ i ];
/* Shift the ciphertext into the IV */
memcpy( convInfo->currentIV, buffer, ivCount );
/* Move on to next block of data */
noBytes -= ivCount;
buffer += ivCount;
}
/* Remember how much of the IV is still available for use */
convInfo->ivCount = ( ivCount % DES_BLOCKSIZE );
return( CRYPT_OK );
}
/* Decrypt data in CFB mode. Note that the transformation can be made
faster (but less clear) with temp = buffer, buffer ^= iv, iv = temp
all in one loop */
static int decryptCFB( CONTEXT_INFO *contextInfoPtr, BYTE *buffer,
int noBytes )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -