📄 aesimpl.c
字号:
static UInt32 RT2[256] = { RT };
#undef V
#define V(a,b,c,d) 0x##b##c##d##a
static UInt32 RT3[256] = { RT };
#undef V
#undef RT
/* round constants */
static UInt32 RCON[10] =
{
0x01000000, 0x02000000, 0x04000000, 0x08000000,
0x10000000, 0x20000000, 0x40000000, 0x80000000,
0x1B000000, 0x36000000
};
/* key schedule tables */
/* platform-independant 32-bit integer manipulation macros */
#define GET_UINT32(n,b,i) \
{ \
(n) = ( (UInt32) (b)[(i) ] << 24 ) \
| ( (UInt32) (b)[(i) + 1] << 16 ) \
| ( (UInt32) (b)[(i) + 2] << 8 ) \
| ( (UInt32) (b)[(i) + 3] ); \
}
#define PUT_UINT32(n,b,i) \
{ \
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
(b)[(i) + 3] = (unsigned char) ( (n) ); \
}
/* AES key scheduling routine
*/
void AESInit (
VoltLibCtx *libCtx,
VoltAESCtx *ctx,
unsigned int encryptFlag,
VtItem *keyData
)
{
unsigned int index, wordCount;
UInt32 *RK, *SK;
UInt32 KT0[256];
UInt32 KT1[256];
UInt32 KT2[256];
UInt32 KT3[256];
UInt32 dKeyTable[64];
switch (keyData->len)
{
case 24:
ctx->rounds = 12;
wordCount = 6;
break;
case 32:
ctx->rounds = 14;
wordCount = 8;
break;
case 16:
default:
ctx->rounds = 10;
wordCount = 4;
}
RK = ctx->keyTable;
/* Copy the key into the key table.
*/
for (index = 0; index < wordCount; ++index)
{
GET_UINT32 (RK[index], keyData->data, index * 4 );
}
/* Set up the encryption key table.
*/
switch (keyData->len)
{
case 16:
/* Build the next 4 words of the key table, then move the temp
* key table pointer up 4 words.
*/
for (index = 0; index < 10; ++index, RK += 4)
{
RK[4] = RK[0] ^ RCON[index] ^
( FSb[ (unsigned char) ( RK[3] >> 16 ) ] << 24 ) ^
( FSb[ (unsigned char) ( RK[3] >> 8 ) ] << 16 ) ^
( FSb[ (unsigned char) ( RK[3] ) ] << 8 ) ^
( FSb[ (unsigned char) ( RK[3] >> 24 ) ] );
RK[5] = RK[1] ^ RK[4];
RK[6] = RK[2] ^ RK[5];
RK[7] = RK[3] ^ RK[6];
}
break;
case 24:
/* Build the next 6 words of the key table, then move the temp
* key table pointer up 6 words.
*/
for (index = 0; index < 8; ++index, RK += 6 )
{
RK[6] = RK[0] ^ RCON[index] ^
( FSb[ (unsigned char) ( RK[5] >> 16 ) ] << 24 ) ^
( FSb[ (unsigned char) ( RK[5] >> 8 ) ] << 16 ) ^
( FSb[ (unsigned char) ( RK[5] ) ] << 8 ) ^
( FSb[ (unsigned char) ( RK[5] >> 24 ) ] );
RK[7] = RK[1] ^ RK[6];
RK[8] = RK[2] ^ RK[7];
RK[9] = RK[3] ^ RK[8];
RK[10] = RK[4] ^ RK[9];
RK[11] = RK[5] ^ RK[10];
}
break;
case 32:
/* Build the next 8 words of the key table, then move the temp
* key table pointer up 8 words.
*/
for (index = 0; index < 7; ++index, RK += 8 )
{
RK[8] = RK[0] ^ RCON[index] ^
( FSb[ (unsigned char) ( RK[7] >> 16 ) ] << 24 ) ^
( FSb[ (unsigned char) ( RK[7] >> 8 ) ] << 16 ) ^
( FSb[ (unsigned char) ( RK[7] ) ] << 8 ) ^
( FSb[ (unsigned char) ( RK[7] >> 24 ) ] );
RK[9] = RK[1] ^ RK[8];
RK[10] = RK[2] ^ RK[9];
RK[11] = RK[3] ^ RK[10];
RK[12] = RK[4] ^
( FSb[ (unsigned char) ( RK[11] >> 24 ) ] << 24 ) ^
( FSb[ (unsigned char) ( RK[11] >> 16 ) ] << 16 ) ^
( FSb[ (unsigned char) ( RK[11] >> 8 ) ] << 8 ) ^
( FSb[ (unsigned char) ( RK[11] ) ] );
RK[13] = RK[5] ^ RK[12];
RK[14] = RK[6] ^ RK[13];
RK[15] = RK[7] ^ RK[14];
}
break;
}
/* If this is encrypting, we're done.
*/
if (encryptFlag == VOLT_AES_ENCRYPT)
return;
/* Set up the decryption key table.
*/
for (index = 0; index < 256; ++index )
{
KT0[index] = RT0[ FSb[index] ];
KT1[index] = RT1[ FSb[index] ];
KT2[index] = RT2[ FSb[index] ];
KT3[index] = RT3[ FSb[index] ];
}
SK = dKeyTable;
*SK++ = *RK++;
*SK++ = *RK++;
*SK++ = *RK++;
*SK++ = *RK++;
for (index = 1; index < ctx->rounds; ++index )
{
RK -= 8;
*SK++ = KT0[ (unsigned char) ( *RK >> 24 ) ] ^
KT1[ (unsigned char) ( *RK >> 16 ) ] ^
KT2[ (unsigned char) ( *RK >> 8 ) ] ^
KT3[ (unsigned char) ( *RK ) ]; RK++;
*SK++ = KT0[ (unsigned char) ( *RK >> 24 ) ] ^
KT1[ (unsigned char) ( *RK >> 16 ) ] ^
KT2[ (unsigned char) ( *RK >> 8 ) ] ^
KT3[ (unsigned char) ( *RK ) ]; RK++;
*SK++ = KT0[ (unsigned char) ( *RK >> 24 ) ] ^
KT1[ (unsigned char) ( *RK >> 16 ) ] ^
KT2[ (unsigned char) ( *RK >> 8 ) ] ^
KT3[ (unsigned char) ( *RK ) ]; RK++;
*SK++ = KT0[ (unsigned char) ( *RK >> 24 ) ] ^
KT1[ (unsigned char) ( *RK >> 16 ) ] ^
KT2[ (unsigned char) ( *RK >> 8 ) ] ^
KT3[ (unsigned char) ( *RK ) ]; RK++;
}
RK -= 8;
*SK++ = *RK++;
*SK++ = *RK++;
*SK++ = *RK++;
*SK++ = *RK++;
/* Copy the decryption key table into the context key table.
*/
Z2Memcpy (ctx->keyTable, dKeyTable, 256);
Z2Memset (dKeyTable, 0, 256);
}
int AESEncryptInit (
VoltAlgorithmObject *algObj,
VoltKeyObject *keyObj
)
{
int status;
VtItem *keyData;
VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
VoltBlockCipherCtx *blockCtx =
(VoltBlockCipherCtx *)(cipherCtx->localCipherCtx);
VoltAESCtx *aesCtx = (VoltAESCtx *)(blockCtx->algCtx);
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Make sure the key matches the algorithm object.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_KEY_OBJ;
if ((keyObj->keyType & VOLT_KEY_TYPE_MASK_SYM_ALG) != VOLT_KEY_ALG_AES)
break;
/* We need data (status is still set to VT_ERROR_INVALID_KEY_OBJ if we
* can't get the data out).
*/
keyData = (VtItem *)(keyObj->keyData);
if ((keyObj->keyType & VOLT_KEY_TYPE_MASK_DATA) != VOLT_KEY_TYPE_DATA)
{
if (keyObj->GetKeyData == (VGetKeyData)0)
break;
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = keyObj->GetKeyData (
(VtKeyObject)keyObj, (Pointer *)&keyData);
if (status != 0)
break;
}
AESInit (
(VoltLibCtx *)(algObj->voltObject.libraryCtx), aesCtx,
VOLT_AES_ENCRYPT, keyData);
status = 0;
} while (0);
VOLT_LOG_ERROR_COMPARE (
status, algObj->voltObject.libraryCtx, status, errorType, fnctLine,
"AESEncryptInit", (char *)0)
return (status);
}
int AESEncryptUpdate (
VoltAlgorithmObject *algObj,
VtRandomObject random,
unsigned char *dataToEncrypt,
unsigned int dataToEncryptLen,
unsigned char *encryptedData
)
{
VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
VoltBlockCipherCtx *blockCtx =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -