PolarSSL v1.3.1
test_suite_cipher.camellia.c
Go to the documentation of this file.
1 #include <polarssl/config.h>
2 
3 #ifdef POLARSSL_CIPHER_C
4 
5 #include <polarssl/cipher.h>
6 
7 #if defined(POLARSSL_GCM_C)
8 #include <polarssl/gcm.h>
9 #endif
10 #endif /* POLARSSL_CIPHER_C */
11 
12 
13 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
14 #include "polarssl/memory.h"
15 #endif
16 
17 #ifdef _MSC_VER
18 #include <basetsd.h>
19 typedef UINT32 uint32_t;
20 #else
21 #include <inttypes.h>
22 #endif
23 
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <string.h>
27 
28 /*
29  * 32-bit integer manipulation macros (big endian)
30  */
31 #ifndef GET_UINT32_BE
32 #define GET_UINT32_BE(n,b,i) \
33 { \
34  (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
35  | ( (uint32_t) (b)[(i) + 1] << 16 ) \
36  | ( (uint32_t) (b)[(i) + 2] << 8 ) \
37  | ( (uint32_t) (b)[(i) + 3] ); \
38 }
39 #endif
40 
41 #ifndef PUT_UINT32_BE
42 #define PUT_UINT32_BE(n,b,i) \
43 { \
44  (b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
45  (b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
46  (b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
47  (b)[(i) + 3] = (unsigned char) ( (n) ); \
48 }
49 #endif
50 
51 static int unhexify(unsigned char *obuf, const char *ibuf)
52 {
53  unsigned char c, c2;
54  int len = strlen(ibuf) / 2;
55  assert(!(strlen(ibuf) %1)); // must be even number of bytes
56 
57  while (*ibuf != 0)
58  {
59  c = *ibuf++;
60  if( c >= '0' && c <= '9' )
61  c -= '0';
62  else if( c >= 'a' && c <= 'f' )
63  c -= 'a' - 10;
64  else if( c >= 'A' && c <= 'F' )
65  c -= 'A' - 10;
66  else
67  assert( 0 );
68 
69  c2 = *ibuf++;
70  if( c2 >= '0' && c2 <= '9' )
71  c2 -= '0';
72  else if( c2 >= 'a' && c2 <= 'f' )
73  c2 -= 'a' - 10;
74  else if( c2 >= 'A' && c2 <= 'F' )
75  c2 -= 'A' - 10;
76  else
77  assert( 0 );
78 
79  *obuf++ = ( c << 4 ) | c2;
80  }
81 
82  return len;
83 }
84 
85 static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
86 {
87  unsigned char l, h;
88 
89  while (len != 0)
90  {
91  h = (*ibuf) / 16;
92  l = (*ibuf) % 16;
93 
94  if( h < 10 )
95  *obuf++ = '0' + h;
96  else
97  *obuf++ = 'a' + h - 10;
98 
99  if( l < 10 )
100  *obuf++ = '0' + l;
101  else
102  *obuf++ = 'a' + l - 10;
103 
104  ++ibuf;
105  len--;
106  }
107 }
108 
118 static int rnd_std_rand( void *rng_state, unsigned char *output, size_t len )
119 {
120  size_t i;
121 
122  if( rng_state != NULL )
123  rng_state = NULL;
124 
125  for( i = 0; i < len; ++i )
126  output[i] = rand();
127 
128  return( 0 );
129 }
130 
136 static int rnd_zero_rand( void *rng_state, unsigned char *output, size_t len )
137 {
138  if( rng_state != NULL )
139  rng_state = NULL;
140 
141  memset( output, 0, len );
142 
143  return( 0 );
144 }
145 
146 typedef struct
147 {
148  unsigned char *buf;
149  size_t length;
150 } rnd_buf_info;
151 
163 static int rnd_buffer_rand( void *rng_state, unsigned char *output, size_t len )
164 {
165  rnd_buf_info *info = (rnd_buf_info *) rng_state;
166  size_t use_len;
167 
168  if( rng_state == NULL )
169  return( rnd_std_rand( NULL, output, len ) );
170 
171  use_len = len;
172  if( len > info->length )
173  use_len = info->length;
174 
175  if( use_len )
176  {
177  memcpy( output, info->buf, use_len );
178  info->buf += use_len;
179  info->length -= use_len;
180  }
181 
182  if( len - use_len > 0 )
183  return( rnd_std_rand( NULL, output + use_len, len - use_len ) );
184 
185  return( 0 );
186 }
187 
195 typedef struct
196 {
197  uint32_t key[16];
198  uint32_t v0, v1;
200 
209 static int rnd_pseudo_rand( void *rng_state, unsigned char *output, size_t len )
210 {
211  rnd_pseudo_info *info = (rnd_pseudo_info *) rng_state;
212  uint32_t i, *k, sum, delta=0x9E3779B9;
213  unsigned char result[4];
214 
215  if( rng_state == NULL )
216  return( rnd_std_rand( NULL, output, len ) );
217 
218  k = info->key;
219 
220  while( len > 0 )
221  {
222  size_t use_len = ( len > 4 ) ? 4 : len;
223  sum = 0;
224 
225  for( i = 0; i < 32; i++ )
226  {
227  info->v0 += (((info->v1 << 4) ^ (info->v1 >> 5)) + info->v1) ^ (sum + k[sum & 3]);
228  sum += delta;
229  info->v1 += (((info->v0 << 4) ^ (info->v0 >> 5)) + info->v0) ^ (sum + k[(sum>>11) & 3]);
230  }
231 
232  PUT_UINT32_BE( info->v0, result, 0 );
233  memcpy( output, result, use_len );
234  len -= use_len;
235  }
236 
237  return( 0 );
238 }
239 
249 static int not_rnd( void *in, unsigned char *out, size_t len )
250 {
251  unsigned char *obuf;
252  const char *ibuf = in;
253  unsigned char c, c2;
254  assert( len == strlen(ibuf) / 2 );
255  assert(!(strlen(ibuf) %1)); // must be even number of bytes
256 
257  obuf = out + (len - 1); // sic
258  while (*ibuf != 0)
259  {
260  c = *ibuf++;
261  if( c >= '0' && c <= '9' )
262  c -= '0';
263  else if( c >= 'a' && c <= 'f' )
264  c -= 'a' - 10;
265  else if( c >= 'A' && c <= 'F' )
266  c -= 'A' - 10;
267  else
268  assert( 0 );
269 
270  c2 = *ibuf++;
271  if( c2 >= '0' && c2 <= '9' )
272  c2 -= '0';
273  else if( c2 >= 'a' && c2 <= 'f' )
274  c2 -= 'a' - 10;
275  else if( c2 >= 'A' && c2 <= 'F' )
276  c2 -= 'A' - 10;
277  else
278  assert( 0 );
279 
280  *obuf-- = ( c << 4 ) | c2; // sic
281  }
282 
283  return( 0 );
284 }
285 
286 
287 #include <stdio.h>
288 #include <string.h>
289 
290 static int test_errors = 0;
291 
292 #ifdef POLARSSL_CIPHER_C
293 
294 #define TEST_SUITE_ACTIVE
295 
296 static int test_assert( int correct, char *test )
297 {
298  if( correct )
299  return( 0 );
300 
301  test_errors++;
302  if( test_errors == 1 )
303  printf( "FAILED\n" );
304  printf( " %s\n", test );
305 
306  return( 1 );
307 }
308 
309 #define TEST_ASSERT( TEST ) \
310  do { test_assert( (TEST) ? 1 : 0, #TEST ); \
311  if( test_errors) return; \
312  } while (0)
313 
314 int verify_string( char **str )
315 {
316  if( (*str)[0] != '"' ||
317  (*str)[strlen( *str ) - 1] != '"' )
318  {
319  printf( "Expected string (with \"\") for parameter and got: %s\n", *str );
320  return( -1 );
321  }
322 
323  (*str)++;
324  (*str)[strlen( *str ) - 1] = '\0';
325 
326  return( 0 );
327 }
328 
329 int verify_int( char *str, int *value )
330 {
331  size_t i;
332  int minus = 0;
333  int digits = 1;
334  int hex = 0;
335 
336  for( i = 0; i < strlen( str ); i++ )
337  {
338  if( i == 0 && str[i] == '-' )
339  {
340  minus = 1;
341  continue;
342  }
343 
344  if( ( ( minus && i == 2 ) || ( !minus && i == 1 ) ) &&
345  str[i - 1] == '0' && str[i] == 'x' )
346  {
347  hex = 1;
348  continue;
349  }
350 
351  if( str[i] < '0' || str[i] > '9' )
352  {
353  digits = 0;
354  break;
355  }
356  }
357 
358  if( digits )
359  {
360  if( hex )
361  *value = strtol( str, NULL, 16 );
362  else
363  *value = strtol( str, NULL, 10 );
364 
365  return( 0 );
366  }
367 
368  if( strcmp( str, "POLARSSL_PADDING_ZEROS_AND_LEN" ) == 0 )
369  {
370  *value = ( POLARSSL_PADDING_ZEROS_AND_LEN );
371  return( 0 );
372  }
373  if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_192_CBC" ) == 0 )
374  {
376  return( 0 );
377  }
378  if( strcmp( str, "-1" ) == 0 )
379  {
380  *value = ( -1 );
381  return( 0 );
382  }
383  if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_128_CTR" ) == 0 )
384  {
386  return( 0 );
387  }
388  if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_128_CBC" ) == 0 )
389  {
391  return( 0 );
392  }
393  if( strcmp( str, "POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED" ) == 0 )
394  {
396  return( 0 );
397  }
398  if( strcmp( str, "POLARSSL_PADDING_ONE_AND_ZEROS" ) == 0 )
399  {
400  *value = ( POLARSSL_PADDING_ONE_AND_ZEROS );
401  return( 0 );
402  }
403  if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_256_CBC" ) == 0 )
404  {
406  return( 0 );
407  }
408  if( strcmp( str, "POLARSSL_PADDING_ZEROS" ) == 0 )
409  {
410  *value = ( POLARSSL_PADDING_ZEROS );
411  return( 0 );
412  }
413  if( strcmp( str, "POLARSSL_PADDING_NONE" ) == 0 )
414  {
415  *value = ( POLARSSL_PADDING_NONE );
416  return( 0 );
417  }
418  if( strcmp( str, "POLARSSL_CIPHER_CAMELLIA_128_CFB128" ) == 0 )
419  {
421  return( 0 );
422  }
423 
424 
425  printf( "Expected integer for parameter and got: %s\n", str );
426  return( -1 );
427 }
428 
429 void test_suite_enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
430  int length_val, int pad_mode )
431 {
432  size_t length = length_val, outlen, total_len, i;
433  unsigned char key[32];
434  unsigned char iv[16];
435  unsigned char ad[13];
436  unsigned char tag[16];
437  unsigned char inbuf[64];
438  unsigned char encbuf[64];
439  unsigned char decbuf[64];
440 
441  const cipher_info_t *cipher_info;
442  cipher_context_t ctx_dec;
443  cipher_context_t ctx_enc;
444 
445  /*
446  * Prepare contexts
447  */
448  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
449  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
450 
451  memset( key, 0x2a, sizeof( key ) );
452 
453  /* Check and get info structures */
454  cipher_info = cipher_info_from_type( cipher_id );
455  TEST_ASSERT( NULL != cipher_info );
456  TEST_ASSERT( cipher_info_from_string( cipher_string ) == cipher_info );
457 
458  /* Initialise enc and dec contexts */
459  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
460  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
461 
462  TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
463  TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
464 
465 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
466  if( -1 != pad_mode )
467  {
468  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_dec, pad_mode ) );
469  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx_enc, pad_mode ) );
470  }
471 #else
472  (void) pad_mode;
473 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
474 
475  /*
476  * Do a few encode/decode cycles
477  */
478  for( i = 0; i < 3; i++ )
479  {
480  memset( iv , 0x00 + i, sizeof( iv ) );
481  memset( ad, 0x10 + i, sizeof( ad ) );
482  memset( inbuf, 0x20 + i, sizeof( inbuf ) );
483 
484  memset( encbuf, 0, sizeof( encbuf ) );
485  memset( decbuf, 0, sizeof( decbuf ) );
486  memset( tag, 0, sizeof( tag ) );
487 
488  TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
489  TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
490 
491  TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
492  TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
493 
494 #if defined(POLARSSL_CIPHER_MODE_AEAD)
495  TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
496  TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
497 #endif /* POLARSSL_CIPHER_MODE_AEAD */
498 
499  /* encode length number of bytes from inbuf */
500  TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
501  total_len = outlen;
502 
503  TEST_ASSERT( total_len == length ||
504  ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
505  total_len < length &&
506  total_len + cipher_get_block_size( &ctx_enc ) > length ) );
507 
508  TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
509  total_len += outlen;
510 
511 #if defined(POLARSSL_CIPHER_MODE_AEAD)
512  TEST_ASSERT( 0 == cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
513 #endif /* POLARSSL_CIPHER_MODE_AEAD */
514 
515  TEST_ASSERT( total_len == length ||
516  ( total_len % cipher_get_block_size( &ctx_enc ) == 0 &&
517  total_len > length &&
518  total_len <= length + cipher_get_block_size( &ctx_enc ) ) );
519 
520  /* decode the previously encoded string */
521  TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
522  total_len = outlen;
523 
524  TEST_ASSERT( total_len == length ||
525  ( total_len % cipher_get_block_size( &ctx_dec ) == 0 &&
526  total_len < length &&
527  total_len + cipher_get_block_size( &ctx_dec ) >= length ) );
528 
529  TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
530  total_len += outlen;
531 
532 #if defined(POLARSSL_CIPHER_MODE_AEAD)
533  TEST_ASSERT( 0 == cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
534 #endif /* POLARSSL_CIPHER_MODE_AEAD */
535 
536  /* check result */
537  TEST_ASSERT( total_len == length );
538  TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
539  }
540 
541  /*
542  * Done
543  */
544  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
545  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
546 }
547 
548 void test_suite_enc_fail( int cipher_id, int pad_mode, int key_len,
549  int length_val, int ret )
550 {
551  size_t length = length_val;
552  unsigned char key[32];
553  unsigned char iv[16];
554 
555  const cipher_info_t *cipher_info;
556  cipher_context_t ctx;
557 
558  unsigned char inbuf[64];
559  unsigned char encbuf[64];
560 
561  size_t outlen = 0;
562 
563  memset( key, 0, 32 );
564  memset( iv , 0, 16 );
565 
566  memset( &ctx, 0, sizeof( ctx ) );
567 
568  memset( inbuf, 5, 64 );
569  memset( encbuf, 0, 64 );
570 
571  /* Check and get info structures */
572  cipher_info = cipher_info_from_type( cipher_id );
573  TEST_ASSERT( NULL != cipher_info );
574 
575  /* Initialise context */
576  TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
577  TEST_ASSERT( 0 == cipher_setkey( &ctx, key, key_len, POLARSSL_ENCRYPT ) );
578 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
579  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
580 #else
581  (void) pad_mode;
582 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
583  TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, 16 ) );
584  TEST_ASSERT( 0 == cipher_reset( &ctx ) );
585 #if defined(POLARSSL_CIPHER_MODE_AEAD)
586  TEST_ASSERT( 0 == cipher_update_ad( &ctx, NULL, 0 ) );
587 #endif /* POLARSSL_CIPHER_MODE_AEAD */
588 
589  /* encode length number of bytes from inbuf */
590  TEST_ASSERT( 0 == cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
591  TEST_ASSERT( ret == cipher_finish( &ctx, encbuf + outlen, &outlen ) );
592 
593  /* done */
594  TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
595 }
596 
597 void test_suite_dec_empty_buf()
598 {
599  unsigned char key[32];
600  unsigned char iv[16];
601 
602  cipher_context_t ctx_dec;
603  const cipher_info_t *cipher_info;
604 
605  unsigned char encbuf[64];
606  unsigned char decbuf[64];
607 
608  size_t outlen = 0;
609 
610  memset( key, 0, 32 );
611  memset( iv , 0, 16 );
612 
613  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
614 
615  memset( encbuf, 0, 64 );
616  memset( decbuf, 0, 64 );
617 
618  /* Initialise context */
620  TEST_ASSERT( NULL != cipher_info);
621 
622  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
623 
624  TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) );
625 
626  TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
627 
628  TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
629 
630 #if defined(POLARSSL_CIPHER_MODE_AEAD)
631  TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
632 #endif /* POLARSSL_CIPHER_MODE_AEAD */
633 
634  /* decode 0-byte string */
635  TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
636  TEST_ASSERT( 0 == outlen );
638  &ctx_dec, decbuf + outlen, &outlen ) );
639  TEST_ASSERT( 0 == outlen );
640 
641  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
642 }
643 
644 void test_suite_enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
645  int second_length_val )
646 {
647  size_t first_length = first_length_val;
648  size_t second_length = second_length_val;
649  size_t length = first_length + second_length;
650  unsigned char key[32];
651  unsigned char iv[16];
652 
653  cipher_context_t ctx_dec;
654  cipher_context_t ctx_enc;
655  const cipher_info_t *cipher_info;
656 
657  unsigned char inbuf[64];
658  unsigned char encbuf[64];
659  unsigned char decbuf[64];
660 
661  size_t outlen = 0;
662  size_t totaloutlen = 0;
663 
664  memset( key, 0, 32 );
665  memset( iv , 0, 16 );
666 
667  memset( &ctx_dec, 0, sizeof( ctx_dec ) );
668  memset( &ctx_enc, 0, sizeof( ctx_enc ) );
669 
670  memset( inbuf, 5, 64 );
671  memset( encbuf, 0, 64 );
672  memset( decbuf, 0, 64 );
673 
674  /* Initialise enc and dec contexts */
675  cipher_info = cipher_info_from_type( cipher_id );
676  TEST_ASSERT( NULL != cipher_info);
677 
678  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) );
679  TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) );
680 
681  TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, key_len, POLARSSL_DECRYPT ) );
682  TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, key_len, POLARSSL_ENCRYPT ) );
683 
684  TEST_ASSERT( 0 == cipher_set_iv( &ctx_dec, iv, 16 ) );
685  TEST_ASSERT( 0 == cipher_set_iv( &ctx_enc, iv, 16 ) );
686 
687  TEST_ASSERT( 0 == cipher_reset( &ctx_dec ) );
688  TEST_ASSERT( 0 == cipher_reset( &ctx_enc ) );
689 
690 #if defined(POLARSSL_CIPHER_MODE_AEAD)
691  TEST_ASSERT( 0 == cipher_update_ad( &ctx_dec, NULL, 0 ) );
692  TEST_ASSERT( 0 == cipher_update_ad( &ctx_enc, NULL, 0 ) );
693 #endif /* POLARSSL_CIPHER_MODE_AEAD */
694 
695  /* encode length number of bytes from inbuf */
696  TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
697  totaloutlen = outlen;
698  TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
699  totaloutlen += outlen;
700  TEST_ASSERT( totaloutlen == length ||
701  ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
702  totaloutlen < length &&
703  totaloutlen + cipher_get_block_size( &ctx_enc ) > length ) );
704 
705  TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
706  totaloutlen += outlen;
707  TEST_ASSERT( totaloutlen == length ||
708  ( totaloutlen % cipher_get_block_size( &ctx_enc ) == 0 &&
709  totaloutlen > length &&
710  totaloutlen <= length + cipher_get_block_size( &ctx_enc ) ) );
711 
712  /* decode the previously encoded string */
713  TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, totaloutlen, decbuf, &outlen ) );
714  totaloutlen = outlen;
715 
716  TEST_ASSERT( totaloutlen == length ||
717  ( totaloutlen % cipher_get_block_size( &ctx_dec ) == 0 &&
718  totaloutlen < length &&
719  totaloutlen + cipher_get_block_size( &ctx_dec ) >= length ) );
720 
721  TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
722  totaloutlen += outlen;
723 
724  TEST_ASSERT( totaloutlen == length );
725 
726  TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
727 
728  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) );
729  TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) );
730 }
731 
732 void test_suite_decrypt_test_vec( int cipher_id, int pad_mode,
733  char *hex_key, char *hex_iv,
734  char *hex_cipher, char *hex_clear,
735  char *hex_ad, char *hex_tag,
736  int finish_result, int tag_result )
737 {
738  unsigned char key[50];
739  unsigned char iv[50];
740  unsigned char cipher[200];
741  unsigned char clear[200];
742  unsigned char ad[200];
743  unsigned char tag[20];
744  size_t key_len, iv_len, cipher_len, clear_len;
745 #if defined(POLARSSL_CIPHER_MODE_AEAD)
746  size_t ad_len, tag_len;
747 #endif
748  cipher_context_t ctx;
749  unsigned char output[200];
750  size_t outlen, total_len;
751 
752  memset( key, 0x00, sizeof( key ) );
753  memset( iv, 0x00, sizeof( iv ) );
754  memset( cipher, 0x00, sizeof( cipher ) );
755  memset( clear, 0x00, sizeof( clear ) );
756  memset( ad, 0x00, sizeof( ad ) );
757  memset( tag, 0x00, sizeof( tag ) );
758  memset( output, 0x00, sizeof( output ) );
759 
760  key_len = unhexify( key, hex_key );
761  iv_len = unhexify( iv, hex_iv );
762  cipher_len = unhexify( cipher, hex_cipher );
763  clear_len = unhexify( clear, hex_clear );
764 #if defined(POLARSSL_CIPHER_MODE_AEAD)
765  ad_len = unhexify( ad, hex_ad );
766  tag_len = unhexify( tag, hex_tag );
767 #else
768  ((void) hex_ad);
769  ((void) hex_tag);
770 #endif
771 
772  /* Prepare context */
773  TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
774  cipher_info_from_type( cipher_id ) ) );
775  TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, POLARSSL_DECRYPT ) );
776 #if defined(POLARSSL_CIPHER_MODE_WITH_PADDING)
777  if( pad_mode != -1 )
778  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
779 #else
780  (void) pad_mode;
781 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
782  TEST_ASSERT( 0 == cipher_set_iv( &ctx, iv, iv_len ) );
783  TEST_ASSERT( 0 == cipher_reset( &ctx ) );
784 #if defined(POLARSSL_CIPHER_MODE_AEAD)
785  TEST_ASSERT( 0 == cipher_update_ad( &ctx, ad, ad_len ) );
786 #endif /* POLARSSL_CIPHER_MODE_AEAD */
787 
788  /* decode buffer and check tag */
789  total_len = 0;
790  TEST_ASSERT( 0 == cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
791  total_len += outlen;
792  TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
793  &outlen ) );
794  total_len += outlen;
795 #if defined(POLARSSL_CIPHER_MODE_AEAD)
796  TEST_ASSERT( tag_result == cipher_check_tag( &ctx, tag, tag_len ) );
797 #endif /* POLARSSL_CIPHER_MODE_AEAD */
798 
799  /* check plaintext only if everything went fine */
800  if( 0 == finish_result && 0 == tag_result )
801  {
802  TEST_ASSERT( total_len == clear_len );
803  TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
804  }
805 
806  cipher_free_ctx( &ctx );
807 }
808 
809 void test_suite_test_vec_ecb( int cipher_id, int operation, char *hex_key,
810  char *hex_input, char *hex_result,
811  int finish_result )
812 {
813  unsigned char key[50];
814  unsigned char input[16];
815  unsigned char result[16];
816  size_t key_len;
817  cipher_context_t ctx;
818  unsigned char output[32];
819  size_t outlen;
820 
821  memset( key, 0x00, sizeof( key ) );
822  memset( input, 0x00, sizeof( input ) );
823  memset( result, 0x00, sizeof( result ) );
824  memset( output, 0x00, sizeof( output ) );
825 
826  /* Prepare context */
827  TEST_ASSERT( 0 == cipher_init_ctx( &ctx,
828  cipher_info_from_type( cipher_id ) ) );
829 
830  key_len = unhexify( key, hex_key );
831  TEST_ASSERT( unhexify( input, hex_input ) ==
832  (int) cipher_get_block_size( &ctx ) );
833  TEST_ASSERT( unhexify( result, hex_result ) ==
834  (int) cipher_get_block_size( &ctx ) );
835 
836  TEST_ASSERT( 0 == cipher_setkey( &ctx, key, 8 * key_len, operation ) );
837 
838  TEST_ASSERT( 0 == cipher_update( &ctx, input,
839  cipher_get_block_size( &ctx ),
840  output, &outlen ) );
841  TEST_ASSERT( outlen == cipher_get_block_size( &ctx ) );
842  TEST_ASSERT( finish_result == cipher_finish( &ctx, output + outlen,
843  &outlen ) );
844  TEST_ASSERT( 0 == outlen );
845 
846  /* check plaintext only if everything went fine */
847  if( 0 == finish_result )
848  TEST_ASSERT( 0 == memcmp( output, result,
849  cipher_get_block_size( &ctx ) ) );
850 
851  cipher_free_ctx( &ctx );
852 }
853 
854 #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
855 void test_suite_set_padding( int cipher_id, int pad_mode, int ret )
856 {
857  const cipher_info_t *cipher_info;
858  cipher_context_t ctx;
859 
860  cipher_info = cipher_info_from_type( cipher_id );
861  TEST_ASSERT( NULL != cipher_info );
862  TEST_ASSERT( 0 == cipher_init_ctx( &ctx, cipher_info ) );
863 
864  TEST_ASSERT( ret == cipher_set_padding_mode( &ctx, pad_mode ) );
865 
866  TEST_ASSERT( 0 == cipher_free_ctx( &ctx ) );
867 }
868 #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
869 
870 #ifdef POLARSSL_CIPHER_MODE_CBC
871 void test_suite_check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
872 {
873  cipher_info_t cipher_info;
874  cipher_context_t ctx;
875  unsigned char input[16];
876  size_t ilen, dlen;
877 
878  /* build a fake context just for getting access to get_padding */
879  memset( &ctx, 0, sizeof( ctx ) );
880  cipher_info.mode = POLARSSL_MODE_CBC;
881  ctx.cipher_info = &cipher_info;
882 
883  TEST_ASSERT( 0 == cipher_set_padding_mode( &ctx, pad_mode ) );
884 
885  ilen = unhexify( input, input_str );
886 
887  TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
888  if( 0 == ret )
889  TEST_ASSERT( dlen == (size_t) dlen_check );
890 }
891 #endif /* POLARSSL_CIPHER_MODE_CBC */
892 
893 #ifdef POLARSSL_SELF_TEST
894 void test_suite_cipher_selftest()
895 {
896  TEST_ASSERT( cipher_self_test( 0 ) == 0 );
897 }
898 #endif /* POLARSSL_SELF_TEST */
899 
900 
901 #endif /* POLARSSL_CIPHER_C */
902 
903 
904 int dep_check( char *str )
905 {
906  if( str == NULL )
907  return( 1 );
908 
909  if( strcmp( str, "POLARSSL_CIPHER_MODE_CTR" ) == 0 )
910  {
911 #if defined(POLARSSL_CIPHER_MODE_CTR)
912  return( 0 );
913 #else
914  return( 1 );
915 #endif
916  }
917  if( strcmp( str, "POLARSSL_CIPHER_MODE_CFB" ) == 0 )
918  {
919 #if defined(POLARSSL_CIPHER_MODE_CFB)
920  return( 0 );
921 #else
922  return( 1 );
923 #endif
924  }
925  if( strcmp( str, "POLARSSL_CIPHER_PADDING_PKCS7" ) == 0 )
926  {
927 #if defined(POLARSSL_CIPHER_PADDING_PKCS7)
928  return( 0 );
929 #else
930  return( 1 );
931 #endif
932  }
933  if( strcmp( str, "POLARSSL_CIPHER_MODE_CBC" ) == 0 )
934  {
935 #if defined(POLARSSL_CIPHER_MODE_CBC)
936  return( 0 );
937 #else
938  return( 1 );
939 #endif
940  }
941  if( strcmp( str, "POLARSSL_CAMELLIA_C" ) == 0 )
942  {
943 #if defined(POLARSSL_CAMELLIA_C)
944  return( 0 );
945 #else
946  return( 1 );
947 #endif
948  }
949 
950 
951  return( 1 );
952 }
953 
954 int dispatch_test(int cnt, char *params[50])
955 {
956  int ret;
957  ((void) cnt);
958  ((void) params);
959 
960 #if defined(TEST_SUITE_ACTIVE)
961  if( strcmp( params[0], "enc_dec_buf" ) == 0 )
962  {
963 
964  int param1;
965  char *param2 = params[2];
966  int param3;
967  int param4;
968  int param5;
969 
970  if( cnt != 6 )
971  {
972  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
973  return( 2 );
974  }
975 
976  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
977  if( verify_string( &param2 ) != 0 ) return( 2 );
978  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
979  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
980  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
981 
982  test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
983  return ( 0 );
984 
985  return ( 3 );
986  }
987  else
988  if( strcmp( params[0], "enc_fail" ) == 0 )
989  {
990 
991  int param1;
992  int param2;
993  int param3;
994  int param4;
995  int param5;
996 
997  if( cnt != 6 )
998  {
999  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
1000  return( 2 );
1001  }
1002 
1003  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1004  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1005  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1006  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1007  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
1008 
1009  test_suite_enc_fail( param1, param2, param3, param4, param5 );
1010  return ( 0 );
1011 
1012  return ( 3 );
1013  }
1014  else
1015  if( strcmp( params[0], "dec_empty_buf" ) == 0 )
1016  {
1017 
1018 
1019  if( cnt != 1 )
1020  {
1021  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1022  return( 2 );
1023  }
1024 
1025 
1026  test_suite_dec_empty_buf( );
1027  return ( 0 );
1028 
1029  return ( 3 );
1030  }
1031  else
1032  if( strcmp( params[0], "enc_dec_buf_multipart" ) == 0 )
1033  {
1034 
1035  int param1;
1036  int param2;
1037  int param3;
1038  int param4;
1039 
1040  if( cnt != 5 )
1041  {
1042  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1043  return( 2 );
1044  }
1045 
1046  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1047  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1048  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1049  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1050 
1051  test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
1052  return ( 0 );
1053 
1054  return ( 3 );
1055  }
1056  else
1057  if( strcmp( params[0], "decrypt_test_vec" ) == 0 )
1058  {
1059 
1060  int param1;
1061  int param2;
1062  char *param3 = params[3];
1063  char *param4 = params[4];
1064  char *param5 = params[5];
1065  char *param6 = params[6];
1066  char *param7 = params[7];
1067  char *param8 = params[8];
1068  int param9;
1069  int param10;
1070 
1071  if( cnt != 11 )
1072  {
1073  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1074  return( 2 );
1075  }
1076 
1077  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1078  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1079  if( verify_string( &param3 ) != 0 ) return( 2 );
1080  if( verify_string( &param4 ) != 0 ) return( 2 );
1081  if( verify_string( &param5 ) != 0 ) return( 2 );
1082  if( verify_string( &param6 ) != 0 ) return( 2 );
1083  if( verify_string( &param7 ) != 0 ) return( 2 );
1084  if( verify_string( &param8 ) != 0 ) return( 2 );
1085  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1086  if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
1087 
1088  test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1089  return ( 0 );
1090 
1091  return ( 3 );
1092  }
1093  else
1094  if( strcmp( params[0], "test_vec_ecb" ) == 0 )
1095  {
1096 
1097  int param1;
1098  int param2;
1099  char *param3 = params[3];
1100  char *param4 = params[4];
1101  char *param5 = params[5];
1102  int param6;
1103 
1104  if( cnt != 7 )
1105  {
1106  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1107  return( 2 );
1108  }
1109 
1110  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1111  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1112  if( verify_string( &param3 ) != 0 ) return( 2 );
1113  if( verify_string( &param4 ) != 0 ) return( 2 );
1114  if( verify_string( &param5 ) != 0 ) return( 2 );
1115  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1116 
1117  test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1118  return ( 0 );
1119 
1120  return ( 3 );
1121  }
1122  else
1123  if( strcmp( params[0], "set_padding" ) == 0 )
1124  {
1125  #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1126 
1127  int param1;
1128  int param2;
1129  int param3;
1130 
1131  if( cnt != 4 )
1132  {
1133  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1134  return( 2 );
1135  }
1136 
1137  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1138  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1139  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1140 
1141  test_suite_set_padding( param1, param2, param3 );
1142  return ( 0 );
1143  #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
1144 
1145  return ( 3 );
1146  }
1147  else
1148  if( strcmp( params[0], "check_padding" ) == 0 )
1149  {
1150  #ifdef POLARSSL_CIPHER_MODE_CBC
1151 
1152  int param1;
1153  char *param2 = params[2];
1154  int param3;
1155  int param4;
1156 
1157  if( cnt != 5 )
1158  {
1159  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1160  return( 2 );
1161  }
1162 
1163  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1164  if( verify_string( &param2 ) != 0 ) return( 2 );
1165  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1166  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1167 
1168  test_suite_check_padding( param1, param2, param3, param4 );
1169  return ( 0 );
1170  #endif /* POLARSSL_CIPHER_MODE_CBC */
1171 
1172  return ( 3 );
1173  }
1174  else
1175  if( strcmp( params[0], "cipher_selftest" ) == 0 )
1176  {
1177  #ifdef POLARSSL_SELF_TEST
1178 
1179 
1180  if( cnt != 1 )
1181  {
1182  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1183  return( 2 );
1184  }
1185 
1186 
1187  test_suite_cipher_selftest( );
1188  return ( 0 );
1189  #endif /* POLARSSL_SELF_TEST */
1190 
1191  return ( 3 );
1192  }
1193  else
1194 
1195  {
1196  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
1197  fflush( stdout );
1198  return( 1 );
1199  }
1200 #else
1201  return( 3 );
1202 #endif
1203  return( ret );
1204 }
1205 
1206 int get_line( FILE *f, char *buf, size_t len )
1207 {
1208  char *ret;
1209 
1210  ret = fgets( buf, len, f );
1211  if( ret == NULL )
1212  return( -1 );
1213 
1214  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
1215  buf[strlen(buf) - 1] = '\0';
1216  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
1217  buf[strlen(buf) - 1] = '\0';
1218 
1219  return( 0 );
1220 }
1221 
1222 int parse_arguments( char *buf, size_t len, char *params[50] )
1223 {
1224  int cnt = 0, i;
1225  char *cur = buf;
1226  char *p = buf, *q;
1227 
1228  params[cnt++] = cur;
1229 
1230  while( *p != '\0' && p < buf + len )
1231  {
1232  if( *p == '\\' )
1233  {
1234  *p++;
1235  *p++;
1236  continue;
1237  }
1238  if( *p == ':' )
1239  {
1240  if( p + 1 < buf + len )
1241  {
1242  cur = p + 1;
1243  params[cnt++] = cur;
1244  }
1245  *p = '\0';
1246  }
1247 
1248  *p++;
1249  }
1250 
1251  // Replace newlines, question marks and colons in strings
1252  for( i = 0; i < cnt; i++ )
1253  {
1254  p = params[i];
1255  q = params[i];
1256 
1257  while( *p != '\0' )
1258  {
1259  if( *p == '\\' && *(p + 1) == 'n' )
1260  {
1261  p += 2;
1262  *(q++) = '\n';
1263  }
1264  else if( *p == '\\' && *(p + 1) == ':' )
1265  {
1266  p += 2;
1267  *(q++) = ':';
1268  }
1269  else if( *p == '\\' && *(p + 1) == '?' )
1270  {
1271  p += 2;
1272  *(q++) = '?';
1273  }
1274  else
1275  *(q++) = *(p++);
1276  }
1277  *q = '\0';
1278  }
1279 
1280  return( cnt );
1281 }
1282 
1283 int main()
1284 {
1285  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1286  const char *filename = "/home/iurt/rpmbuild/BUILD/polarssl-1.3.1/tests/suites/test_suite_cipher.camellia.data";
1287  FILE *file;
1288  char buf[5000];
1289  char *params[50];
1290 
1291 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1292  unsigned char alloc_buf[1000000];
1293  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1294 #endif
1295 
1296  file = fopen( filename, "r" );
1297  if( file == NULL )
1298  {
1299  fprintf( stderr, "Failed to open\n" );
1300  return( 1 );
1301  }
1302 
1303  while( !feof( file ) )
1304  {
1305  int skip = 0;
1306 
1307  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1308  break;
1309  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1310  fprintf( stdout, " " );
1311  for( i = strlen( buf ) + 1; i < 67; i++ )
1312  fprintf( stdout, "." );
1313  fprintf( stdout, " " );
1314  fflush( stdout );
1315 
1316  total_tests++;
1317 
1318  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1319  break;
1320  cnt = parse_arguments( buf, strlen(buf), params );
1321 
1322  if( strcmp( params[0], "depends_on" ) == 0 )
1323  {
1324  for( i = 1; i < cnt; i++ )
1325  if( dep_check( params[i] ) != 0 )
1326  skip = 1;
1327 
1328  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1329  break;
1330  cnt = parse_arguments( buf, strlen(buf), params );
1331  }
1332 
1333  if( skip == 0 )
1334  {
1335  test_errors = 0;
1336  ret = dispatch_test( cnt, params );
1337  }
1338 
1339  if( skip == 1 || ret == 3 )
1340  {
1341  total_skipped++;
1342  fprintf( stdout, "----\n" );
1343  fflush( stdout );
1344  }
1345  else if( ret == 0 && test_errors == 0 )
1346  {
1347  fprintf( stdout, "PASS\n" );
1348  fflush( stdout );
1349  }
1350  else if( ret == 2 )
1351  {
1352  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1353  fclose(file);
1354  exit( 2 );
1355  }
1356  else
1357  total_errors++;
1358 
1359  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1360  break;
1361  if( strlen(buf) != 0 )
1362  {
1363  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1364  return( 1 );
1365  }
1366  }
1367  fclose(file);
1368 
1369  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1370  if( total_errors == 0 )
1371  fprintf( stdout, "PASSED" );
1372  else
1373  fprintf( stdout, "FAILED" );
1374 
1375  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1376  total_tests - total_errors, total_tests, total_skipped );
1377 
1378 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1379 #if defined(POLARSSL_MEMORY_DEBUG)
1380  memory_buffer_alloc_status();
1381 #endif
1382  memory_buffer_alloc_free();
1383 #endif
1384 
1385  return( total_errors != 0 );
1386 }
1387 
1388 
int cipher_finish(cipher_context_t *ctx, unsigned char *output, size_t *olen)
Generic cipher finalisation function.
Memory allocation layer.
static int not_rnd(void *in, unsigned char *out, size_t len)
This function returns a buffer given as a hex string.
Generic cipher context.
Definition: cipher.h:233
Info structure for the pseudo random function.
int cipher_write_tag(cipher_context_t *ctx, unsigned char *tag, size_t tag_len)
Write tag for AEAD ciphers.
Cipher information.
Definition: cipher.h:201
zero padding (not reversible!)
Definition: cipher.h:133
const cipher_info_t * cipher_info_from_type(const cipher_type_t cipher_type)
Returns the cipher information structure associated with the given cipher type.
static unsigned int cipher_get_block_size(const cipher_context_t *ctx)
Returns the block size of the given cipher.
Definition: cipher.h:342
const cipher_info_t * cipher_info_from_string(const char *cipher_name)
Returns the cipher information structure associated with the given cipher name.
static int rnd_pseudo_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a pseudo random function.
int(* get_padding)(unsigned char *input, size_t ilen, size_t *data_len)
Definition: cipher.h:245
Configuration options (set of defines)
ISO/IEC 7816-4 padding.
Definition: cipher.h:131
static int test_assert(int correct, char *test)
int main(int argc, char *argv[])
int dep_check(char *str)
const cipher_info_t * cipher_info
Information about the associated cipher.
Definition: cipher.h:235
#define TEST_ASSERT(TEST)
int cipher_free_ctx(cipher_context_t *ctx)
Free the cipher-specific context of ctx.
int cipher_update_ad(cipher_context_t *ctx, const unsigned char *ad, size_t ad_len)
Add additional data (for AEAD ciphers).
int cipher_set_iv(cipher_context_t *ctx, const unsigned char *iv, size_t iv_len)
Set the initialization vector (IV) or nonce.
int cipher_update(cipher_context_t *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
Generic cipher update function.
static int unhexify(unsigned char *obuf, const char *ibuf)
static int test_errors
#define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
Decryption of block requires a full block.
Definition: cipher.h:57
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
Generic cipher wrapper.
int parse_arguments(char *buf, size_t len, char *params[50])
#define PUT_UINT32_BE(n, b, i)
int cipher_reset(cipher_context_t *ctx)
Finish preparation of the given context.
int cipher_set_padding_mode(cipher_context_t *ctx, cipher_padding_t mode)
Set padding mode, for cipher modes that use padding.
cipher_mode_t mode
Cipher mode (e.g.
Definition: cipher.h:206
int cipher_init_ctx(cipher_context_t *ctx, const cipher_info_t *cipher_info)
Initialises and fills the cipher context structure with the appropriate values.
int cipher_setkey(cipher_context_t *ctx, const unsigned char *key, int key_length, const operation_t operation)
Set the key to use with the given context.
int verify_string(char **str)
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
static void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
int dispatch_test(int cnt, char *params[50])
static int rnd_buffer_rand(void *rng_state, unsigned char *output, size_t len)
This function returns random based on a buffer it receives.
never pad (full blocks only)
Definition: cipher.h:134
Galois/Counter mode for 128-bit block ciphers.
unsigned char * buf
ANSI X.923 padding.
Definition: cipher.h:132
int verify_int(char *str, int *value)
int cipher_self_test(int verbose)
Checkup routine.
int cipher_check_tag(cipher_context_t *ctx, const unsigned char *tag, size_t tag_len)
Check tag for AEAD ciphers.
int get_line(FILE *f, char *buf, size_t len)