8 typedef UINT32 uint32_t;
17 #define GET_UINT32_BE(n,b,i) \
19 (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
20 | ( (uint32_t) (b)[(i) + 1] << 16 ) \
21 | ( (uint32_t) (b)[(i) + 2] << 8 ) \
22 | ( (uint32_t) (b)[(i) + 3] ); \
27 #define PUT_UINT32_BE(n,b,i) \
29 (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
30 (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
31 (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
32 (b)[(i) + 3] = (unsigned char) ( (n) ); \
36 int unhexify(
unsigned char *obuf,
const char *ibuf)
39 int len = strlen(ibuf) / 2;
40 assert(!(strlen(ibuf) %1));
45 if( c >=
'0' && c <=
'9' )
47 else if( c >=
'a' && c <=
'f' )
49 else if( c >=
'A' && c <=
'F' )
55 if( c2 >=
'0' && c2 <=
'9' )
57 else if( c2 >=
'a' && c2 <=
'f' )
59 else if( c2 >=
'A' && c2 <=
'F' )
64 *obuf++ = ( c << 4 ) | c2;
70 void hexify(
unsigned char *obuf,
const unsigned char *ibuf,
int len)
82 *obuf++ =
'a' + h - 10;
87 *obuf++ =
'a' + l - 10;
103 static int rnd_std_rand(
void *rng_state,
unsigned char *output,
size_t len )
107 if( rng_state != NULL )
110 for( i = 0; i < len; ++i )
121 static int rnd_zero_rand(
void *rng_state,
unsigned char *output,
size_t len )
123 if( rng_state != NULL )
126 memset( output, 0, len );
153 if( rng_state == NULL )
162 memcpy( output, info->
buf, use_len );
163 info->
buf += use_len;
167 if( len - use_len > 0 )
168 return(
rnd_std_rand( NULL, output + use_len, len - use_len ) );
197 uint32_t i, *k, sum, delta=0x9E3779B9;
198 unsigned char result[4];
200 if( rng_state == NULL )
207 size_t use_len = ( len > 4 ) ? 4 : len;
210 for( i = 0; i < 32; i++ )
212 info->
v0 += (((info->
v1 << 4) ^ (info->
v1 >> 5)) + info->
v1) ^ (sum + k[sum & 3]);
214 info->
v1 += (((info->
v0 << 4) ^ (info->
v0 >> 5)) + info->
v0) ^ (sum + k[(sum>>11) & 3]);
218 memcpy( output, result, use_len );
228 #ifdef POLARSSL_AES_C
231 FCT_SUITE_BGN(test_suite_aes)
234 FCT_TEST_BGN(aes_ecb_encrypt_invalid_keylength)
236 unsigned char key_str[100];
237 unsigned char src_str[100];
238 unsigned char dst_str[100];
239 unsigned char output[100];
243 memset(key_str, 0x00, 100);
244 memset(src_str, 0x00, 100);
245 memset(dst_str, 0x00, 100);
246 memset(output, 0x00, 100);
248 key_len =
unhexify( key_str,
"000000000000000000000000000000" );
249 unhexify( src_str,
"f34481ec3cc627bacd5dc3fb08f273e6" );
255 hexify( dst_str, output, 16 );
257 fct_chk( strcmp( (
char *) dst_str,
"0336763e966d92595a567cc9ce537f5e" ) == 0 );
263 FCT_TEST_BGN(aes_ecb_decrypt_invalid_keylength)
265 unsigned char key_str[100];
266 unsigned char src_str[100];
267 unsigned char dst_str[100];
268 unsigned char output[100];
272 memset(key_str, 0x00, 100);
273 memset(src_str, 0x00, 100);
274 memset(dst_str, 0x00, 100);
275 memset(output, 0x00, 100);
277 key_len =
unhexify( key_str,
"000000000000000000000000000000" );
278 unhexify( src_str,
"f34481ec3cc627bacd5dc3fb08f273e6" );
284 hexify( dst_str, output, 16 );
286 fct_chk( strcmp( (
char *) dst_str,
"0336763e966d92595a567cc9ce537f5e" ) == 0 );
292 FCT_TEST_BGN(aes_256_cbc_encrypt_invalid_input_length)
294 unsigned char key_str[100];
295 unsigned char iv_str[100];
296 unsigned char src_str[100];
297 unsigned char dst_str[100];
298 unsigned char output[100];
300 int key_len, data_len;
302 memset(key_str, 0x00, 100);
303 memset(iv_str, 0x00, 100);
304 memset(src_str, 0x00, 100);
305 memset(dst_str, 0x00, 100);
306 memset(output, 0x00, 100);
308 key_len =
unhexify( key_str,
"0000000000000000000000000000000000000000000000000000000000000000" );
309 unhexify( iv_str,
"00000000000000000000000000000000" );
310 data_len =
unhexify( src_str,
"ffffffffffffffe000000000000000" );
316 hexify( dst_str, output, data_len );
318 fct_chk( strcmp( (
char *) dst_str,
"" ) == 0 );
324 FCT_TEST_BGN(aes_256_cbc_decrypt_invalid_input_length)
326 unsigned char key_str[100];
327 unsigned char iv_str[100];
328 unsigned char src_str[100];
329 unsigned char dst_str[100];
330 unsigned char output[100];
332 int key_len, data_len;
334 memset(key_str, 0x00, 100);
335 memset(iv_str, 0x00, 100);
336 memset(src_str, 0x00, 100);
337 memset(dst_str, 0x00, 100);
338 memset(output, 0x00, 100);
340 key_len =
unhexify( key_str,
"0000000000000000000000000000000000000000000000000000000000000000" );
341 unhexify( iv_str,
"00000000000000000000000000000000" );
342 data_len =
unhexify( src_str,
"623a52fcea5d443e48d9181ab32c74" );
348 hexify( dst_str, output, data_len );
350 fct_chk( strcmp( (
char *) dst_str,
"" ) == 0 );
355 #ifdef POLARSSL_SELF_TEST
357 FCT_TEST_BGN(aes_selftest)