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