PolarSSL v1.2.8
test_suite_aes.rest.c
Go to the documentation of this file.
1 #include "fct.h"
2 #include <polarssl/config.h>
3 
4 #include <polarssl/aes.h>
5 
6 #ifdef _MSC_VER
7 #include <basetsd.h>
8 typedef UINT32 uint32_t;
9 #else
10 #include <inttypes.h>
11 #endif
12 
13 /*
14  * 32-bit integer manipulation macros (big endian)
15  */
16 #ifndef GET_UINT32_BE
17 #define GET_UINT32_BE(n,b,i) \
18 { \
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] ); \
23 }
24 #endif
25 
26 #ifndef PUT_UINT32_BE
27 #define PUT_UINT32_BE(n,b,i) \
28 { \
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) ); \
33 }
34 #endif
35 
36 int unhexify(unsigned char *obuf, const char *ibuf)
37 {
38  unsigned char c, c2;
39  int len = strlen(ibuf) / 2;
40  assert(!(strlen(ibuf) %1)); // must be even number of bytes
41 
42  while (*ibuf != 0)
43  {
44  c = *ibuf++;
45  if( c >= '0' && c <= '9' )
46  c -= '0';
47  else if( c >= 'a' && c <= 'f' )
48  c -= 'a' - 10;
49  else if( c >= 'A' && c <= 'F' )
50  c -= 'A' - 10;
51  else
52  assert( 0 );
53 
54  c2 = *ibuf++;
55  if( c2 >= '0' && c2 <= '9' )
56  c2 -= '0';
57  else if( c2 >= 'a' && c2 <= 'f' )
58  c2 -= 'a' - 10;
59  else if( c2 >= 'A' && c2 <= 'F' )
60  c2 -= 'A' - 10;
61  else
62  assert( 0 );
63 
64  *obuf++ = ( c << 4 ) | c2;
65  }
66 
67  return len;
68 }
69 
70 void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
71 {
72  unsigned char l, h;
73 
74  while (len != 0)
75  {
76  h = (*ibuf) / 16;
77  l = (*ibuf) % 16;
78 
79  if( h < 10 )
80  *obuf++ = '0' + h;
81  else
82  *obuf++ = 'a' + h - 10;
83 
84  if( l < 10 )
85  *obuf++ = '0' + l;
86  else
87  *obuf++ = 'a' + l - 10;
88 
89  ++ibuf;
90  len--;
91  }
92 }
93 
103 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
104 {
105  size_t i;
106 
107  if( rng_state != NULL )
108  rng_state = NULL;
109 
110  for( i = 0; i < len; ++i )
111  output[i] = rand();
112 
113  return( 0 );
114 }
115 
121 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
122 {
123  if( rng_state != NULL )
124  rng_state = NULL;
125 
126  memset( output, 0, len );
127 
128  return( 0 );
129 }
130 
131 typedef struct
132 {
133  unsigned char *buf;
134  size_t length;
135 } rnd_buf_info;
136 
148 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
149 {
150  rnd_buf_info *info = (rnd_buf_info *) rng_state;
151  size_t use_len;
152 
153  if( rng_state == NULL )
154  return( rnd_std_rand( NULL, output, len ) );
155 
156  use_len = len;
157  if( len > info->length )
158  use_len = info->length;
159 
160  if( use_len )
161  {
162  memcpy( output, info->buf, use_len );
163  info->buf += use_len;
164  info->length -= use_len;
165  }
166 
167  if( len - use_len > 0 )
168  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
169 
170  return( 0 );
171 }
172 
180 typedef struct
181 {
182  uint32_t key[16];
183  uint32_t v0, v1;
185 
194 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
195 {
196  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
197  uint32_t i, *k, sum, delta=0x9E3779B9;
198  unsigned char result[4];
199 
200  if( rng_state == NULL )
201  return( rnd_std_rand( NULL, output, len ) );
202 
203  k = info->key;
204 
205  while( len > 0 )
206  {
207  size_t use_len = ( len > 4 ) ? 4 : len;
208  sum = 0;
209 
210  for( i = 0; i < 32; i++ )
211  {
212  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
213  sum += delta;
214  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
215  }
216 
217  PUT_UINT32_BE( info->v0, result, 0 );
218  memcpy( output, result, use_len );
219  len -= use_len;
220  }
221 
222  return( 0 );
223 }
224 
225 
227 {
228 #ifdef POLARSSL_AES_C
229 
230 
231  FCT_SUITE_BGN(test_suite_aes)
232  {
233 
234  FCT_TEST_BGN(aes_ecb_encrypt_invalid_keylength)
235  {
236  unsigned char key_str[100];
237  unsigned char src_str[100];
238  unsigned char dst_str[100];
239  unsigned char output[100];
240  aes_context ctx;
241  int key_len;
242 
243  memset(key_str, 0x00, 100);
244  memset(src_str, 0x00, 100);
245  memset(dst_str, 0x00, 100);
246  memset(output, 0x00, 100);
247 
248  key_len = unhexify( key_str, "000000000000000000000000000000" );
249  unhexify( src_str, "f34481ec3cc627bacd5dc3fb08f273e6" );
250 
251  fct_chk( aes_setkey_enc( &ctx, key_str, key_len * 8 ) == POLARSSL_ERR_AES_INVALID_KEY_LENGTH );
253  {
254  fct_chk( aes_crypt_ecb( &ctx, AES_ENCRYPT, src_str, output ) == 0 );
255  hexify( dst_str, output, 16 );
256 
257  fct_chk( strcmp( (char *) dst_str, "0336763e966d92595a567cc9ce537f5e" ) == 0 );
258  }
259  }
260  FCT_TEST_END();
261 
262 
263  FCT_TEST_BGN(aes_ecb_decrypt_invalid_keylength)
264  {
265  unsigned char key_str[100];
266  unsigned char src_str[100];
267  unsigned char dst_str[100];
268  unsigned char output[100];
269  aes_context ctx;
270  int key_len;
271 
272  memset(key_str, 0x00, 100);
273  memset(src_str, 0x00, 100);
274  memset(dst_str, 0x00, 100);
275  memset(output, 0x00, 100);
276 
277  key_len = unhexify( key_str, "000000000000000000000000000000" );
278  unhexify( src_str, "f34481ec3cc627bacd5dc3fb08f273e6" );
279 
280  fct_chk( aes_setkey_dec( &ctx, key_str, key_len * 8 ) == POLARSSL_ERR_AES_INVALID_KEY_LENGTH );
282  {
283  fct_chk( aes_crypt_ecb( &ctx, AES_DECRYPT, src_str, output ) == 0 );
284  hexify( dst_str, output, 16 );
285 
286  fct_chk( strcmp( (char *) dst_str, "0336763e966d92595a567cc9ce537f5e" ) == 0 );
287  }
288  }
289  FCT_TEST_END();
290 
291 
292  FCT_TEST_BGN(aes_256_cbc_encrypt_invalid_input_length)
293  {
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];
299  aes_context ctx;
300  int key_len, data_len;
301 
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);
307 
308  key_len = unhexify( key_str, "0000000000000000000000000000000000000000000000000000000000000000" );
309  unhexify( iv_str, "00000000000000000000000000000000" );
310  data_len = unhexify( src_str, "ffffffffffffffe000000000000000" );
311 
312  aes_setkey_enc( &ctx, key_str, key_len * 8 );
313  fct_chk( aes_crypt_cbc( &ctx, AES_ENCRYPT, data_len, iv_str, src_str, output ) == POLARSSL_ERR_AES_INVALID_INPUT_LENGTH );
315  {
316  hexify( dst_str, output, data_len );
317 
318  fct_chk( strcmp( (char *) dst_str, "" ) == 0 );
319  }
320  }
321  FCT_TEST_END();
322 
323 
324  FCT_TEST_BGN(aes_256_cbc_decrypt_invalid_input_length)
325  {
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];
331  aes_context ctx;
332  int key_len, data_len;
333 
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);
339 
340  key_len = unhexify( key_str, "0000000000000000000000000000000000000000000000000000000000000000" );
341  unhexify( iv_str, "00000000000000000000000000000000" );
342  data_len = unhexify( src_str, "623a52fcea5d443e48d9181ab32c74" );
343 
344  aes_setkey_dec( &ctx, key_str, key_len * 8 );
345  fct_chk( aes_crypt_cbc( &ctx, AES_DECRYPT, data_len, iv_str, src_str, output ) == POLARSSL_ERR_AES_INVALID_INPUT_LENGTH );
347  {
348  hexify( dst_str, output, data_len );
349 
350  fct_chk( strcmp( (char *) dst_str, "" ) == 0 );
351  }
352  }
353  FCT_TEST_END();
354 
355 #ifdef POLARSSL_SELF_TEST
356 
357  FCT_TEST_BGN(aes_selftest)
358  {
359  fct_chk( aes_self_test( 0 ) == 0 );
360  }
361  FCT_TEST_END();
362 #endif /* POLARSSL_SELF_TEST */
363 
364  }
365  FCT_SUITE_END();
366 
367 #endif /* POLARSSL_AES_C */
368 
369 }
370 FCT_END();
371