📄 pgptestcfb.c
字号:
/*____________________________________________________________________________
Copyright (C) 1997 Pretty Good Privacy, Inc.
All rights reserved.
$Id: pgpTestCFB.c,v 1.2 2002/11/23 02:19:31 jeffc Exp $
____________________________________________________________________________*/
#include "pgpConfig.h"
#include <stdlib.h>
#include <string.h>
#include "pgpCFB.h"
#include "pgpSymmetricCipher.h"
#include "pgpErrors.h"
#include "pgpTest.h"
/*____________________________________________________________________________
Test a cipher feedback algorithm.
____________________________________________________________________________*/
static void
TestCFBAlg(
PGPContextRef context,
PGPCipherAlgorithm algorithm,
PGPUInt16 interleave,
PGPUInt32 testSize )
{
PGPError err = kPGPError_NoErr;
PGPSymmetricCipherContextRef symmetricRef = NULL;
err = PGPNewSymmetricCipherContext( context, algorithm, &symmetricRef );
if ( IsntPGPError( err ) )
{
PGPCFBContextRef ref;
PGPByte key[ 32 ];
PGPByte * iv = NULL;
PGPSize blockSize;
PGPGetSymmetricCipherSizes( symmetricRef, NULL, &blockSize );
/*
* this is *NOT* a good choice for an iv, but for test
* purposes it suffices.
*/
iv = (PGPByte *)malloc( blockSize * interleave );
if ( IsNull( iv ) )
{
err = kPGPError_OutOfMemory;
}
if ( IsntPGPError( err ) )
{
err = PGPNewCFBContext( symmetricRef, interleave, &ref );
symmetricRef = NULL; /* no longer belongs to us */
}
if ( IsntPGPError( err ) )
{
PGPCFBContextRef tempRef = NULL;
/* test a simple copy */
err = PGPCopyCFBContext( ref, &tempRef );
if ( IsntPGPError( err ) )
{
PGPFreeCFBContext( tempRef );
}
err = PGPInitCFB( ref, key, iv );
pgpTestAssert( IsntPGPError( err ) );
if ( IsntPGPError( err ) )
{
PGPByte * testBuffer = NULL;
testBuffer = (PGPByte *)malloc( testSize * 3 );
pgpTestAssert( IsntNull( testBuffer ) );
if ( IsntNull( testBuffer ) )
{
PGPByte * clearText = NULL;
PGPByte * cipherText = NULL;
PGPByte * decryptedText = NULL;
clearText = testBuffer;
cipherText = clearText + testSize;
decryptedText = cipherText + testSize;
err = PGPCFBEncrypt( ref, clearText,
testSize, cipherText );
if ( IsntPGPError( err ) )
{
/* reinitialize for decryption */
err = PGPInitCFB( ref, key, iv );
pgpTestAssert( IsntPGPError( err ) );
err = PGPCFBDecrypt( ref, cipherText,
testSize, decryptedText );
}
pgpTestAssert( IsntPGPError( err ) );
pgpTestAssert( memcmp( clearText,
decryptedText, testSize ) == 0 );
free( testBuffer );
}
}
pgpTestAssert( IsntPGPError( err ) );
err = PGPFreeCFBContext( ref );
}
if ( IsntNull( iv ) )
{
free( iv );
}
}
pgpTestAssert( IsntPGPError( err ) );
}
/*____________________________________________________________________________
Test all cipher feedback algorithms
____________________________________________________________________________*/
void
TestCFB( PGPContextRef context )
{
PGPError err = kPGPError_NoErr;
PGPUInt32 testIndex;
(void)err;
#define kNumTests 1
for( testIndex = 0; testIndex < kNumTests; ++testIndex )
{
PGPUInt16 interleave;
#define kCFBTestSize ( 32 * 1024UL + 1 )
#define kNumInterleave 9
for ( interleave = 1; interleave <= kNumInterleave; ++interleave )
{
TestCFBAlg( context,
kPGPCipherAlgorithm_IDEA, interleave, kCFBTestSize);
TestCFBAlg( context,
kPGPCipherAlgorithm_3DES, interleave, kCFBTestSize);
TestCFBAlg( context,
kPGPCipherAlgorithm_CAST5, interleave, kCFBTestSize);
}
}
#undef kNumTests
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -