37 #define AES_KEYSIZE 32
38 #define AES_BLOCKSIZE 16
39 #define AES_EXPANDED_KEYSIZE sizeof( AES_CTX )
43 #define AES_GCM_CTX gcm_ctx
44 #define AES_GCM_EXPANDED_KEYSIZE sizeof( AES_GCM_CTX )
49 #define AES_EKEY aes_encrypt_ctx
50 #define AES_DKEY aes_decrypt_ctx
51 #define AES_2KEY AES_CTX
58 #if defined( TABLES_8K ) || defined( TABLES_64K )
59 #error AES-GCM is configured to use unusually large GF-mult tables, is this deliberate?
76 #define BYTE_SIZE( x ) ( UNIT_SIZE * ( ( sizeof( x ) + UNIT_SIZE - 1 ) / UNIT_SIZE ) )
81 #define KS_SIZE ( BYTE_SIZE( AES_EKEY ) + BYTE_SIZE( AES_DKEY ) + UNIT_SIZE )
85 #define KS_BASE(x) ( ( unsigned char * )( ( ( AES_CTX * ) x )->ksch ) )
89 #define EKEY( x ) ( ( AES_EKEY * ) ALIGN_CEIL( KS_BASE( x ), UNIT_SIZE ) )
90 #define DKEY( x ) ( ( AES_DKEY * ) ALIGN_CEIL( KS_BASE( x ) + BYTE_SIZE( AES_EKEY ), UNIT_SIZE ) )
94 typedef unsigned long _unit;
96 _unit ksch[ ( KS_SIZE +
sizeof( _unit ) - 1 ) /
sizeof( _unit ) ];
99 #define ENC_KEY( x ) EKEY( ( x )->key )
100 #define DEC_KEY( x ) DKEY( ( x )->key )
101 #define GCM_KEY( x ) ( x )->key
109 #ifndef CONFIG_NO_SELFTEST
117 const BYTE key[ AES_KEYSIZE + 8 ];
122 static const AES_TEST
FAR_BSS testAES[] = {
124 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
125 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F },
126 { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
127 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF },
128 { 0x69, 0xC4, 0xE0, 0xD8, 0x6A, 0x7B, 0x04, 0x30,
129 0xD8, 0xCD, 0xB7, 0x80, 0x70, 0xB4, 0xC5, 0x5A } },
131 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
132 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
133 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 },
134 { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
135 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF },
136 { 0xDD, 0xA9, 0x7C, 0xA4, 0x86, 0x4C, 0xDF, 0xE0,
137 0x6E, 0xAF, 0x70, 0xA0, 0xEC, 0x0D, 0x71, 0x91 } },
139 { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
140 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
141 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
142 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F },
143 { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
144 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF },
145 { 0x8E, 0xA2, 0xB7, 0xCA, 0x51, 0x67, 0x45, 0xBF,
146 0xEA, 0xFC, 0x49, 0x90, 0x4B, 0x49, 0x60, 0x89 } }
153 static void printVector(
const char *description,
const BYTE *
data,
158 printf(
"%s = ", description );
159 for( i = 0; i <
length; i++ )
160 printf(
"%02x", data[ i ] );
167 const BYTE *newKey1,
const BYTE *newKey2 )
175 memcpy( keyData, newKey2, keySize );
179 memcpy( keyData, newKey1 + 8, keySize );
180 memcpy( keyData + 8, newKey2, AES_BLOCKSIZE );
183 memcpy( keyData, newKey1, AES_BLOCKSIZE );
184 memcpy( keyData + 16, newKey2, AES_BLOCKSIZE );
188 key[ i ] ^= keyData[ i ];
189 return( capabilityInfo->initKeyFunction( contextInfo, key,
195 const BYTE *initialKey,
const int keySize,
196 const BYTE *initialIV,
const BYTE *initialPT )
198 BYTE key[ AES_KEYSIZE + 8 ], iv[ AES_KEYSIZE + 8 ];
199 BYTE temp[ AES_BLOCKSIZE + 8 ];
202 memcpy( key, initialKey, keySize );
204 memcpy( iv, initialIV, AES_BLOCKSIZE );
205 memcpy( temp, initialPT, AES_BLOCKSIZE );
206 for( i = 0; i < 100; i++ )
208 BYTE prevTemp[ AES_BLOCKSIZE + 8 ];
211 status = capabilityInfo->initKeyFunction( contextInfo, key,
215 printVector(
"Key", key, keySize );
217 printVector(
"IV", iv, AES_BLOCKSIZE );
218 printVector(
"Plaintext", temp, AES_BLOCKSIZE );
220 memcpy( contextInfo->ctxConv->currentIV, iv, AES_BLOCKSIZE );
221 for( j = 0; j < 1000; j++ )
224 if( iv != NULL && j == 0 )
226 status = capabilityInfo->encryptCBCFunction( contextInfo, temp,
228 memcpy( prevTemp, temp, AES_BLOCKSIZE );
229 memcpy( temp, iv, AES_BLOCKSIZE );
233 status = capabilityInfo->encryptFunction( contextInfo, temp,
237 BYTE tmpTemp[ AES_BLOCKSIZE + 8 ];
239 memcpy( tmpTemp, temp, AES_BLOCKSIZE );
240 memcpy( temp, prevTemp, AES_BLOCKSIZE );
241 memcpy( prevTemp, tmpTemp, AES_BLOCKSIZE );
247 printVector(
"Ciphertext", temp, AES_BLOCKSIZE );
249 status = updateKey( key, keySize, contextInfo, capabilityInfo,
259 static int selfTest(
void )
264 { 0x8D, 0x2E, 0x60, 0x36, 0x5F, 0x17, 0xC7, 0xDF, 0x10, 0x40, 0xD7, 0x50, 0x1B, 0x4A, 0x7B, 0x5A };
266 { 0x59, 0xB5, 0x08, 0x8E, 0x6D, 0xAD, 0xC3, 0xAD, 0x5F, 0x27, 0xA4, 0x60, 0x87, 0x2D, 0x59, 0x29 };
269 { 0x9D, 0xC2, 0xC8, 0x4A, 0x37, 0x85, 0x0C, 0x11, 0x69, 0x98, 0x18, 0x60, 0x5F, 0x47, 0x95, 0x8C };
271 { 0x25, 0x69, 0x53, 0xB2, 0xFE, 0xAB, 0x2A, 0x04, 0xAE, 0x01, 0x80, 0xD8, 0x33, 0x5B, 0xBE, 0xD6 };
273 { 0x2E, 0x58, 0x66, 0x92, 0xE6, 0x47, 0xF5, 0x02, 0x8E, 0xC6, 0xFA, 0x47, 0xA5, 0x5A, 0x2A, 0xAB };
276 { 0xB1, 0x1E, 0x4E, 0xCA, 0xE2, 0xE7, 0x1E, 0x14, 0x14, 0x5D, 0xD7, 0xDB, 0x26, 0x35, 0x65, 0x2F };
278 { 0xAD, 0xD3, 0x2B, 0xF8, 0x20, 0x4C, 0x33, 0x33, 0x9C, 0x54, 0xCD, 0x58, 0x58, 0xEE, 0x0D, 0x13 };
280 { 0x73, 0x20, 0x49, 0xE8, 0x9D, 0x74, 0xFC, 0xE7, 0xC5, 0xA4, 0x96, 0x64, 0x04, 0x86, 0x8F, 0xA6 };
283 { 0x71, 0x15, 0x11, 0x93, 0x1A, 0x15, 0x62, 0xEA, 0x73, 0x29, 0x0A, 0x8B, 0x0A, 0x37, 0xA3, 0xB4 };
285 { 0x9D, 0xCE, 0x23, 0xFD, 0x2D, 0xF5, 0x36, 0x0F, 0x79, 0x9C, 0xF1, 0x79, 0x84, 0xE4, 0x7C, 0x8D };
287 { 0xF0, 0x66, 0xBE, 0x4B, 0xD6, 0x71, 0xEB, 0xC1, 0xC4, 0xCF, 0x3C, 0x00, 0x8E, 0xF2, 0xCF, 0x18 };
290 BYTE keyData[ AES_EXPANDED_KEYSIZE + 8 ];
299 for( i = 0; i <
sizeof( testAES ) /
sizeof( AES_TEST ); i++ )
301 status = testCipher( capabilityInfo, keyData, testAES[ i ].key,
302 testAES[ i ].keySize, testAES[ i ].
plaintext,
309 staticInitContext( &contextInfo,
CONTEXT_CONV, capabilityInfo,
310 &contextData,
sizeof(
CONV_INFO ), keyData );
311 status = mct( &contextInfo, capabilityInfo, mctECBKey, 16,
313 staticDestroyContext( &contextInfo );
318 staticInitContext( &contextInfo,
CONTEXT_CONV, capabilityInfo,
319 &contextData,
sizeof(
CONV_INFO ), keyData );
320 status = mct( &contextInfo, capabilityInfo, mctCBCKey, 16,
321 mctCBCIV, mctCBCPT );
322 staticDestroyContext( &contextInfo );
330 #define selfTest NULL
347 assert( contextInfoPtr == NULL || \
349 assert( ( length == 0 &&
isWritePtr( data,
sizeof(
int ) ) ) || \
350 ( length > 0 &&
isWritePtr( data, length ) ) );
352 static_assert( AES_EXPANDED_KEYSIZE >= KS_SIZE,
"AES context storage" );
357 contextInfoPtr == NULL ) || \
359 contextInfoPtr != NULL ) );
368 *valuePtr =
max( AES_EXPANDED_KEYSIZE, AES_GCM_EXPANDED_KEYSIZE );
370 *valuePtr = AES_EXPANDED_KEYSIZE;
378 int *valuePtr = (
int * ) data;
381 *valuePtr = UNIT_SIZE;
388 CONV_INFO *convInfo = contextInfoPtr->ctxConv;
399 return( getDefaultInfo( type, contextInfoPtr, data, length ) );
418 CONV_INFO *convInfo = contextInfoPtr->ctxConv;
428 CONV_INFO *convInfo = contextInfoPtr->ctxConv;
438 CONV_INFO *convInfo = contextInfoPtr->ctxConv;
440 return( (
aes_cbc_encrypt( buffer, buffer, noBytes, convInfo->currentIV,
448 CONV_INFO *convInfo = contextInfoPtr->ctxConv;
450 return( (
aes_cbc_decrypt( buffer, buffer, noBytes, convInfo->currentIV,
458 CONV_INFO *convInfo = contextInfoPtr->ctxConv;
460 return( (
aes_cfb_encrypt( buffer, buffer, noBytes, convInfo->currentIV,
468 CONV_INFO *convInfo = contextInfoPtr->ctxConv;
470 return( (
aes_cfb_decrypt( buffer, buffer, noBytes, convInfo->currentIV,
478 CONV_INFO *convInfo = contextInfoPtr->ctxConv;
480 return( (
aes_ofb_encrypt( buffer, buffer, noBytes, convInfo->currentIV,
488 CONV_INFO *convInfo = contextInfoPtr->ctxConv;
490 return( (
aes_ofb_decrypt( buffer, buffer, noBytes, convInfo->currentIV,
500 CONV_INFO *convInfo = contextInfoPtr->ctxConv;
510 CONV_INFO *convInfo = contextInfoPtr->ctxConv;
533 CONV_INFO *convInfo = contextInfoPtr->ctxConv;
562 return( initGenericParams( contextInfoPtr, paramType, data,
568 static int initKey(
CONTEXT_INFO *contextInfoPtr,
const void *key,
571 CONV_INFO *convInfo = contextInfoPtr->ctxConv;
574 if( convInfo->userKey != key )
575 memcpy( convInfo->userKey, key, keyLength );
608 selfTest, getInfo, NULL,
initParams, initKey, NULL,
609 encryptECB, decryptECB, encryptCBC, decryptCBC,
610 encryptCFB, decryptCFB, encryptOFB, decryptOFB,
612 encryptGCM, decryptGCM
626 return( &capabilityInfo );