📄 ctx_aes.c
字号:
#endif
static int selfTest( void )
{
#if 0
/* ECB */
static const BYTE FAR_BSS mctECBKey[] = \
{ 0x8D, 0x2E, 0x60, 0x36, 0x5F, 0x17, 0xC7, 0xDF, 0x10, 0x40, 0xD7, 0x50, 0x1B, 0x4A, 0x7B, 0x5A };
static const BYTE FAR_BSS mctECBPT[] = \
{ 0x59, 0xB5, 0x08, 0x8E, 0x6D, 0xAD, 0xC3, 0xAD, 0x5F, 0x27, 0xA4, 0x60, 0x87, 0x2D, 0x59, 0x29 };
/* CBC */
static const BYTE FAR_BSS mctCBCKey[] = \
{ 0x9D, 0xC2, 0xC8, 0x4A, 0x37, 0x85, 0x0C, 0x11, 0x69, 0x98, 0x18, 0x60, 0x5F, 0x47, 0x95, 0x8C };
static const BYTE FAR_BSS mctCBCIV[] = \
{ 0x25, 0x69, 0x53, 0xB2, 0xFE, 0xAB, 0x2A, 0x04, 0xAE, 0x01, 0x80, 0xD8, 0x33, 0x5B, 0xBE, 0xD6 };
static const BYTE FAR_BSS mctCBCPT[] = \
{ 0x2E, 0x58, 0x66, 0x92, 0xE6, 0x47, 0xF5, 0x02, 0x8E, 0xC6, 0xFA, 0x47, 0xA5, 0x5A, 0x2A, 0xAB };
/* OFB */
static const BYTE FAR_BSS mctOFBKey[] = \
{ 0xB1, 0x1E, 0x4E, 0xCA, 0xE2, 0xE7, 0x1E, 0x14, 0x14, 0x5D, 0xD7, 0xDB, 0x26, 0x35, 0x65, 0x2F };
static const BYTE FAR_BSS mctOFBIV[] = \
{ 0xAD, 0xD3, 0x2B, 0xF8, 0x20, 0x4C, 0x33, 0x33, 0x9C, 0x54, 0xCD, 0x58, 0x58, 0xEE, 0x0D, 0x13 };
static const BYTE FAR_BSS mctOFBPT[] = \
{ 0x73, 0x20, 0x49, 0xE8, 0x9D, 0x74, 0xFC, 0xE7, 0xC5, 0xA4, 0x96, 0x64, 0x04, 0x86, 0x8F, 0xA6 };
/* CFB-128 */
static const BYTE FAR_BSS mctCFBKey[] = \
{ 0x71, 0x15, 0x11, 0x93, 0x1A, 0x15, 0x62, 0xEA, 0x73, 0x29, 0x0A, 0x8B, 0x0A, 0x37, 0xA3, 0xB4 };
static const BYTE FAR_BSS mctCFBIV[] = \
{ 0x9D, 0xCE, 0x23, 0xFD, 0x2D, 0xF5, 0x36, 0x0F, 0x79, 0x9C, 0xF1, 0x79, 0x84, 0xE4, 0x7C, 0x8D };
static const BYTE FAR_BSS mctCFBPT[] = \
{ 0xF0, 0x66, 0xBE, 0x4B, 0xD6, 0x71, 0xEB, 0xC1, 0xC4, 0xCF, 0x3C, 0x00, 0x8E, 0xF2, 0xCF, 0x18 };
#endif /* 0 */
const CAPABILITY_INFO *capabilityInfo = getAESCapability();
BYTE keyData[ AES_EXPANDED_KEYSIZE + 8 ];
int i, status;
/* The AES code requires 16-byte alignment for data structures, before
we try anything else we make sure that the compiler voodoo required
to handle this has worked */
if( aes_test_alignment_detection( 16 ) != EXIT_SUCCESS )
return( CRYPT_ERROR_FAILED );
for( i = 0; i < sizeof( testAES ) / sizeof( AES_TEST ); i++ )
{
status = testCipher( capabilityInfo, keyData, testAES[ i ].key,
testAES[ i ].keySize, testAES[ i ].plaintext,
testAES[ i ].ciphertext );
if( cryptStatusError( status ) )
return( status );
}
#if 0 /* OK */
staticInitContext( &contextInfo, CONTEXT_CONV, capabilityInfo,
&contextData, sizeof( CONV_INFO ), keyData );
status = mct( &contextInfo, capabilityInfo, mctECBKey, 16,
NULL, mctECBPT );
staticDestroyContext( &contextInfo );
if( cryptStatusError( status ) )
return( CRYPT_ERROR_FAILED );
#endif
#if 0 /* OK */
staticInitContext( &contextInfo, CONTEXT_CONV, capabilityInfo,
&contextData, sizeof( CONV_INFO ), keyData );
status = mct( &contextInfo, capabilityInfo, mctCBCKey, 16,
mctCBCIV, mctCBCPT );
staticDestroyContext( &contextInfo );
if( cryptStatusError( status ) )
return( CRYPT_ERROR_FAILED );
#endif
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 = AES_EXPANDED_KEYSIZE;
return( CRYPT_OK );
}
return( getDefaultInfo( type, ptrParam, intParam, result ) );
}
/****************************************************************************
* *
* AES En/Decryption Routines *
* *
****************************************************************************/
/* Encrypt/decrypt data in ECB/CBC/CFB modes. These are just basic wrappers
for the AES code, which either calls down to the C/asm AES routines or
uses hardware assist to perform the operation directly */
static int encryptECB( CONTEXT_INFO *contextInfoPtr, BYTE *buffer,
int noBytes )
{
CONV_INFO *convInfo = contextInfoPtr->ctxConv;
return( ( aes_ecb_encrypt( buffer, buffer, noBytes,
ENC_KEY( convInfo ) ) == EXIT_SUCCESS ) ? \
CRYPT_OK : CRYPT_ERROR_FAILED );
}
static int decryptECB( CONTEXT_INFO *contextInfoPtr, BYTE *buffer,
int noBytes )
{
CONV_INFO *convInfo = contextInfoPtr->ctxConv;
return( ( aes_ecb_decrypt( buffer, buffer, noBytes,
DEC_KEY( convInfo ) ) == EXIT_SUCCESS ) ? \
CRYPT_OK : CRYPT_ERROR_FAILED );
}
static int encryptCBC( CONTEXT_INFO *contextInfoPtr, BYTE *buffer,
int noBytes )
{
CONV_INFO *convInfo = contextInfoPtr->ctxConv;
return( ( aes_cbc_encrypt( buffer, buffer, noBytes, convInfo->currentIV,
ENC_KEY( convInfo ) ) == EXIT_SUCCESS ) ? \
CRYPT_OK : CRYPT_ERROR_FAILED );
}
static int decryptCBC( CONTEXT_INFO *contextInfoPtr, BYTE *buffer,
int noBytes )
{
CONV_INFO *convInfo = contextInfoPtr->ctxConv;
return( ( aes_cbc_decrypt( buffer, buffer, noBytes, convInfo->currentIV,
DEC_KEY( convInfo ) ) == EXIT_SUCCESS ) ? \
CRYPT_OK : CRYPT_ERROR_FAILED );
}
static int encryptCFB( CONTEXT_INFO *contextInfoPtr, BYTE *buffer,
int noBytes )
{
CONV_INFO *convInfo = contextInfoPtr->ctxConv;
return( ( aes_cfb_encrypt( buffer, buffer, noBytes, convInfo->currentIV,
ENC_KEY( convInfo ) ) == EXIT_SUCCESS ) ? \
CRYPT_OK : CRYPT_ERROR_FAILED );
}
static int decryptCFB( CONTEXT_INFO *contextInfoPtr, BYTE *buffer,
int noBytes )
{
CONV_INFO *convInfo = contextInfoPtr->ctxConv;
return( ( aes_cfb_decrypt( buffer, buffer, noBytes, convInfo->currentIV,
ENC_KEY( convInfo ) ) == EXIT_SUCCESS ) ? \
CRYPT_OK : CRYPT_ERROR_FAILED );
}
static int encryptOFB( CONTEXT_INFO *contextInfoPtr, BYTE *buffer,
int noBytes )
{
CONV_INFO *convInfo = contextInfoPtr->ctxConv;
return( ( aes_ofb_encrypt( buffer, buffer, noBytes, convInfo->currentIV,
ENC_KEY( convInfo ) ) == EXIT_SUCCESS ) ? \
CRYPT_OK : CRYPT_ERROR_FAILED );
}
static int decryptOFB( CONTEXT_INFO *contextInfoPtr, BYTE *buffer,
int noBytes )
{
CONV_INFO *convInfo = contextInfoPtr->ctxConv;
return( ( aes_ofb_decrypt( buffer, buffer, noBytes, convInfo->currentIV,
ENC_KEY( convInfo ) ) == EXIT_SUCCESS ) ? \
CRYPT_OK : CRYPT_ERROR_FAILED );
}
/****************************************************************************
* *
* AES Key Management Routines *
* *
****************************************************************************/
/* Key schedule an AES key */
static int initKey( CONTEXT_INFO *contextInfoPtr, const void *key,
const int keyLength )
{
CONV_INFO *convInfo = contextInfoPtr->ctxConv;
/* Copy the key to internal storage */
if( convInfo->userKey != key )
memcpy( convInfo->userKey, key, keyLength );
convInfo->userKeyLength = keyLength;
/* Call the AES key schedule code */
if( aes_encrypt_key( convInfo->userKey, keyLength,
ENC_KEY( convInfo ) ) != EXIT_SUCCESS || \
aes_decrypt_key( convInfo->userKey, keyLength,
DEC_KEY( convInfo ) ) != EXIT_SUCCESS )
return( CRYPT_ERROR_FAILED );
return( CRYPT_OK );
}
/****************************************************************************
* *
* Capability Access Routines *
* *
****************************************************************************/
static const CAPABILITY_INFO FAR_BSS capabilityInfo = {
CRYPT_ALGO_AES, bitsToBytes( 128 ), "AES", 3,
bitsToBytes( 128 ), bitsToBytes( 128 ), bitsToBytes( 256 ),
selfTest, getInfo, NULL, initKeyParams, initKey, NULL,
encryptECB, decryptECB, encryptCBC, decryptCBC,
encryptCFB, decryptCFB, encryptOFB, decryptOFB
};
const CAPABILITY_INFO *getAESCapability( void )
{
/* If we're not using compiler-generated tables, we have to manually
initialise the tables before we can use AES (this is only required
for old/broken compilers that aren't tough enough for the
preprocessor-based table calculations) */
#ifndef FIXED_TABLES
gen_tabs();
#endif /* FIXED_TABLES */
return( &capabilityInfo );
}
#endif /* USE_AES */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -