📄 lib_3des.c
字号:
bytesToUse = DES_BLOCKSIZE - ivCount;
if( noBytes < bytesToUse )
bytesToUse = noBytes;
/* Encrypt the data */
for( i = 0; i < bytesToUse; i++ )
buffer[ i ] ^= cryptInfo->ctxConv.currentIV[ i + ivCount ];
memcpy( cryptInfo->ctxConv.currentIV + ivCount, buffer, bytesToUse );
/* Adjust the byte count and buffer position */
noBytes -= bytesToUse;
buffer += bytesToUse;
ivCount += bytesToUse;
}
while( noBytes )
{
ivCount = ( noBytes > DES_BLOCKSIZE ) ? DES_BLOCKSIZE : noBytes;
/* Encrypt the IV */
des_ecb3_encrypt( ( C_Block * ) cryptInfo->ctxConv.currentIV,
( C_Block * ) cryptInfo->ctxConv.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 ] ^= cryptInfo->ctxConv.currentIV[ i ];
/* Shift the ciphertext into the IV */
memcpy( cryptInfo->ctxConv.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 */
cryptInfo->ctxConv.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 */
int des3DecryptCFB( CRYPT_INFO *cryptInfo, BYTE *buffer, int noBytes )
{
DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->ctxConv.key;
BYTE temp[ DES_BLOCKSIZE ];
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 */
bytesToUse = DES_BLOCKSIZE - ivCount;
if( noBytes < bytesToUse )
bytesToUse = noBytes;
/* Decrypt the data */
memcpy( temp, buffer, bytesToUse );
for( i = 0; i < bytesToUse; i++ )
buffer[ i ] ^= cryptInfo->ctxConv.currentIV[ i + ivCount ];
memcpy( cryptInfo->ctxConv.currentIV + ivCount, temp, bytesToUse );
/* Adjust the byte count and buffer position */
noBytes -= bytesToUse;
buffer += bytesToUse;
ivCount += bytesToUse;
}
while( noBytes )
{
ivCount = ( noBytes > DES_BLOCKSIZE ) ? DES_BLOCKSIZE : noBytes;
/* Encrypt the IV */
des_ecb3_encrypt( ( C_Block * ) cryptInfo->ctxConv.currentIV,
( C_Block * ) cryptInfo->ctxConv.currentIV,
des3Key->desKey1, des3Key->desKey2,
des3Key->desKey3, DES_ENCRYPT );
/* Save the ciphertext */
memcpy( temp, buffer, ivCount );
/* XOR the buffer contents with the encrypted IV */
for( i = 0; i < ivCount; i++ )
buffer[ i ] ^= cryptInfo->ctxConv.currentIV[ i ];
/* Shift the ciphertext into the IV */
memcpy( cryptInfo->ctxConv.currentIV, temp, ivCount );
/* Move on to next block of data */
noBytes -= ivCount;
buffer += ivCount;
}
/* Remember how much of the IV is still available for use */
cryptInfo->ctxConv.ivCount = ( ivCount % DES_BLOCKSIZE );
/* Clear the temporary buffer */
zeroise( temp, DES_BLOCKSIZE );
return( CRYPT_OK );
}
/* Encrypt/decrypt data in OFB mode */
int des3EncryptOFB( 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 */
bytesToUse = DES_BLOCKSIZE - ivCount;
if( noBytes < bytesToUse )
bytesToUse = noBytes;
/* Encrypt the data */
for( i = 0; i < bytesToUse; i++ )
buffer[ i ] ^= cryptInfo->ctxConv.currentIV[ i + ivCount ];
/* Adjust the byte count and buffer position */
noBytes -= bytesToUse;
buffer += bytesToUse;
ivCount += bytesToUse;
}
while( noBytes )
{
ivCount = ( noBytes > DES_BLOCKSIZE ) ? DES_BLOCKSIZE : noBytes;
/* Encrypt the IV */
des_ecb3_encrypt( ( C_Block * ) cryptInfo->ctxConv.currentIV,
( C_Block * ) cryptInfo->ctxConv.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 ] ^= cryptInfo->ctxConv.currentIV[ i ];
/* Move on to next block of data */
noBytes -= ivCount;
buffer += ivCount;
}
/* Remember how much of the IV is still available for use */
cryptInfo->ctxConv.ivCount = ( ivCount % DES_BLOCKSIZE );
return( CRYPT_OK );
}
/* Decrypt data in OFB mode */
int des3DecryptOFB( 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 */
bytesToUse = DES_BLOCKSIZE - ivCount;
if( noBytes < bytesToUse )
bytesToUse = noBytes;
/* Decrypt the data */
for( i = 0; i < bytesToUse; i++ )
buffer[ i ] ^= cryptInfo->ctxConv.currentIV[ i + ivCount ];
/* Adjust the byte count and buffer position */
noBytes -= bytesToUse;
buffer += bytesToUse;
ivCount += bytesToUse;
}
while( noBytes )
{
ivCount = ( noBytes > DES_BLOCKSIZE ) ? DES_BLOCKSIZE : noBytes;
/* Encrypt the IV */
des_ecb3_encrypt( ( C_Block * ) cryptInfo->ctxConv.currentIV,
( C_Block * ) cryptInfo->ctxConv.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 ] ^= cryptInfo->ctxConv.currentIV[ i ];
/* Move on to next block of data */
noBytes -= ivCount;
buffer += ivCount;
}
/* Remember how much of the IV is still available for use */
cryptInfo->ctxConv.ivCount = ( ivCount % DES_BLOCKSIZE );
return( CRYPT_OK );
}
/****************************************************************************
* *
* 3DES Key Management Routines *
* *
****************************************************************************/
/* Key schedule two/three DES keys */
int des3InitKey( CRYPT_INFO *cryptInfo, const void *key, const int keyLength )
{
DES3_KEY *des3Key = ( DES3_KEY * ) cryptInfo->ctxConv.key;
BOOLEAN useEDE = FALSE;
/* Copy the key to internal storage */
if( cryptInfo->ctxConv.userKey != key )
memcpy( cryptInfo->ctxConv.userKey, key, keyLength );
cryptInfo->ctxConv.userKeyLength = keyLength;
/* Check the key size. This gets a bit complicated because although we
follow X9.52 and default to three-key triple DES, we'll often be
passed a 128 (112)-bit key which was common in older designs. If this
happens we take the 112-bit key and repeat the first 56 bits to create
a 168-bit key. X9.52 says that if the caller wants EDE behaviour they
have to set it up themselves using a full 168-bit key, but this will
cause problems for people using the high-level functions which don't
allow this level of control, so if we're passed a 112-bit key we just
expand it out to 168 bits to get two-key EDE */
if( keyLength <= bitsToBytes( 64 * 2 ) )
useEDE = TRUE; /* Only 112 bits of key, force EDE mode */
/* Call the libdes key schedule code. Returns with -1 if the key parity
is wrong (which never occurs since we force the correct parity) or -2
if a weak key is used */
des_set_odd_parity( ( C_Block * ) cryptInfo->ctxConv.userKey );
if( key_sched( ( des_cblock * ) cryptInfo->ctxConv.userKey,
des3Key->desKey1 ) )
return( CRYPT_ARGERROR_STR1 );
des_set_odd_parity( ( C_Block * ) ( ( BYTE * ) cryptInfo->ctxConv.userKey + bitsToBytes( 64 ) ) );
if( key_sched( ( des_cblock * ) ( ( BYTE * ) cryptInfo->ctxConv.userKey + bitsToBytes( 64 ) ),
des3Key->desKey2 ) )
return( CRYPT_ARGERROR_STR1 );
if( useEDE )
/* Rather than performing another key schedule, we just copy the first
scheduled key into the third one */
memcpy( des3Key->desKey3, des3Key->desKey1, DES_KEYSIZE );
else
{
des_set_odd_parity( ( C_Block * ) ( ( BYTE * ) cryptInfo->ctxConv.userKey + bitsToBytes( 128 ) ) );
if( key_sched( ( des_cblock * ) ( ( BYTE * ) cryptInfo->ctxConv.userKey + bitsToBytes( 128 ) ),
des3Key->desKey3 ) )
return( CRYPT_ARGERROR_STR1 );
}
return( CRYPT_OK );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -