PolarSSL v1.3.2
test_suite_cipher.blowfish.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_ERR_CIPHER_FULL_BLOCK_EXPECTED" ) == 0 )
365  {
367  return( 0 );
368  }
369  if( strcmp( str, "POLARSSL_CIPHER_BLOWFISH_CFB64" ) == 0 )
370  {
371  *value = ( POLARSSL_CIPHER_BLOWFISH_CFB64 );
372  return( 0 );
373  }
374  if( strcmp( str, "-1" ) == 0 )
375  {
376  *value = ( -1 );
377  return( 0 );
378  }
379  if( strcmp( str, "POLARSSL_PADDING_NONE" ) == 0 )
380  {
381  *value = ( POLARSSL_PADDING_NONE );
382  return( 0 );
383  }
384  if( strcmp( str, "POLARSSL_CIPHER_BLOWFISH_CBC" ) == 0 )
385  {
386  *value = ( POLARSSL_CIPHER_BLOWFISH_CBC );
387  return( 0 );
388  }
389  if( strcmp( str, "POLARSSL_PADDING_ZEROS_AND_LEN" ) == 0 )
390  {
391  *value = ( POLARSSL_PADDING_ZEROS_AND_LEN );
392  return( 0 );
393  }
394  if( strcmp( str, "POLARSSL_PADDING_ZEROS" ) == 0 )
395  {
396  *value = ( POLARSSL_PADDING_ZEROS );
397  return( 0 );
398  }
399  if( strcmp( str, "POLARSSL_PADDING_ONE_AND_ZEROS" ) == 0 )
400  {
401  *value = ( POLARSSL_PADDING_ONE_AND_ZEROS );
402  return( 0 );
403  }
404  if( strcmp( str, "POLARSSL_CIPHER_BLOWFISH_CTR" ) == 0 )
405  {
406  *value = ( POLARSSL_CIPHER_BLOWFISH_CTR );
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_CFB" ) == 0 )
896  {
897 #if defined(POLARSSL_CIPHER_MODE_CFB)
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_CIPHER_MODE_CTR" ) == 0 )
912  {
913 #if defined(POLARSSL_CIPHER_MODE_CTR)
914  return( 0 );
915 #else
916  return( 1 );
917 #endif
918  }
919  if( strcmp( str, "POLARSSL_CIPHER_MODE_CBC" ) == 0 )
920  {
921 #if defined(POLARSSL_CIPHER_MODE_CBC)
922  return( 0 );
923 #else
924  return( 1 );
925 #endif
926  }
927  if( strcmp( str, "POLARSSL_BLOWFISH_C" ) == 0 )
928  {
929 #if defined(POLARSSL_BLOWFISH_C)
930  return( 0 );
931 #else
932  return( 1 );
933 #endif
934  }
935 
936 
937  return( 1 );
938 }
939 
940 int dispatch_test(int cnt, char *params[50])
941 {
942  int ret;
943  ((void) cnt);
944  ((void) params);
945 
946 #if defined(TEST_SUITE_ACTIVE)
947  if( strcmp( params[0], "enc_dec_buf" ) == 0 )
948  {
949 
950  int param1;
951  char *param2 = params[2];
952  int param3;
953  int param4;
954  int param5;
955 
956  if( cnt != 6 )
957  {
958  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
959  return( 2 );
960  }
961 
962  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
963  if( verify_string( &param2 ) != 0 ) return( 2 );
964  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
965  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
966  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
967 
968  test_suite_enc_dec_buf( param1, param2, param3, param4, param5 );
969  return ( 0 );
970 
971  return ( 3 );
972  }
973  else
974  if( strcmp( params[0], "enc_fail" ) == 0 )
975  {
976 
977  int param1;
978  int param2;
979  int param3;
980  int param4;
981  int param5;
982 
983  if( cnt != 6 )
984  {
985  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 6 );
986  return( 2 );
987  }
988 
989  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
990  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
991  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
992  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
993  if( verify_int( params[5], &param5 ) != 0 ) return( 2 );
994 
995  test_suite_enc_fail( param1, param2, param3, param4, param5 );
996  return ( 0 );
997 
998  return ( 3 );
999  }
1000  else
1001  if( strcmp( params[0], "dec_empty_buf" ) == 0 )
1002  {
1003 
1004 
1005  if( cnt != 1 )
1006  {
1007  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1008  return( 2 );
1009  }
1010 
1011 
1012  test_suite_dec_empty_buf( );
1013  return ( 0 );
1014 
1015  return ( 3 );
1016  }
1017  else
1018  if( strcmp( params[0], "enc_dec_buf_multipart" ) == 0 )
1019  {
1020 
1021  int param1;
1022  int param2;
1023  int param3;
1024  int param4;
1025 
1026  if( cnt != 5 )
1027  {
1028  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1029  return( 2 );
1030  }
1031 
1032  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1033  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1034  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1035  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1036 
1037  test_suite_enc_dec_buf_multipart( param1, param2, param3, param4 );
1038  return ( 0 );
1039 
1040  return ( 3 );
1041  }
1042  else
1043  if( strcmp( params[0], "decrypt_test_vec" ) == 0 )
1044  {
1045 
1046  int param1;
1047  int param2;
1048  char *param3 = params[3];
1049  char *param4 = params[4];
1050  char *param5 = params[5];
1051  char *param6 = params[6];
1052  char *param7 = params[7];
1053  char *param8 = params[8];
1054  int param9;
1055  int param10;
1056 
1057  if( cnt != 11 )
1058  {
1059  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 11 );
1060  return( 2 );
1061  }
1062 
1063  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1064  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1065  if( verify_string( &param3 ) != 0 ) return( 2 );
1066  if( verify_string( &param4 ) != 0 ) return( 2 );
1067  if( verify_string( &param5 ) != 0 ) return( 2 );
1068  if( verify_string( &param6 ) != 0 ) return( 2 );
1069  if( verify_string( &param7 ) != 0 ) return( 2 );
1070  if( verify_string( &param8 ) != 0 ) return( 2 );
1071  if( verify_int( params[9], &param9 ) != 0 ) return( 2 );
1072  if( verify_int( params[10], &param10 ) != 0 ) return( 2 );
1073 
1074  test_suite_decrypt_test_vec( param1, param2, param3, param4, param5, param6, param7, param8, param9, param10 );
1075  return ( 0 );
1076 
1077  return ( 3 );
1078  }
1079  else
1080  if( strcmp( params[0], "test_vec_ecb" ) == 0 )
1081  {
1082 
1083  int param1;
1084  int param2;
1085  char *param3 = params[3];
1086  char *param4 = params[4];
1087  char *param5 = params[5];
1088  int param6;
1089 
1090  if( cnt != 7 )
1091  {
1092  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 7 );
1093  return( 2 );
1094  }
1095 
1096  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1097  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1098  if( verify_string( &param3 ) != 0 ) return( 2 );
1099  if( verify_string( &param4 ) != 0 ) return( 2 );
1100  if( verify_string( &param5 ) != 0 ) return( 2 );
1101  if( verify_int( params[6], &param6 ) != 0 ) return( 2 );
1102 
1103  test_suite_test_vec_ecb( param1, param2, param3, param4, param5, param6 );
1104  return ( 0 );
1105 
1106  return ( 3 );
1107  }
1108  else
1109  if( strcmp( params[0], "set_padding" ) == 0 )
1110  {
1111  #ifdef POLARSSL_CIPHER_MODE_WITH_PADDING
1112 
1113  int param1;
1114  int param2;
1115  int param3;
1116 
1117  if( cnt != 4 )
1118  {
1119  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 4 );
1120  return( 2 );
1121  }
1122 
1123  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1124  if( verify_int( params[2], &param2 ) != 0 ) return( 2 );
1125  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1126 
1127  test_suite_set_padding( param1, param2, param3 );
1128  return ( 0 );
1129  #endif /* POLARSSL_CIPHER_MODE_WITH_PADDING */
1130 
1131  return ( 3 );
1132  }
1133  else
1134  if( strcmp( params[0], "check_padding" ) == 0 )
1135  {
1136  #ifdef POLARSSL_CIPHER_MODE_CBC
1137 
1138  int param1;
1139  char *param2 = params[2];
1140  int param3;
1141  int param4;
1142 
1143  if( cnt != 5 )
1144  {
1145  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 5 );
1146  return( 2 );
1147  }
1148 
1149  if( verify_int( params[1], &param1 ) != 0 ) return( 2 );
1150  if( verify_string( &param2 ) != 0 ) return( 2 );
1151  if( verify_int( params[3], &param3 ) != 0 ) return( 2 );
1152  if( verify_int( params[4], &param4 ) != 0 ) return( 2 );
1153 
1154  test_suite_check_padding( param1, param2, param3, param4 );
1155  return ( 0 );
1156  #endif /* POLARSSL_CIPHER_MODE_CBC */
1157 
1158  return ( 3 );
1159  }
1160  else
1161  if( strcmp( params[0], "cipher_selftest" ) == 0 )
1162  {
1163  #ifdef POLARSSL_SELF_TEST
1164 
1165 
1166  if( cnt != 1 )
1167  {
1168  fprintf( stderr, "\nIncorrect argument count (%d != %d)\n", cnt, 1 );
1169  return( 2 );
1170  }
1171 
1172 
1173  test_suite_cipher_selftest( );
1174  return ( 0 );
1175  #endif /* POLARSSL_SELF_TEST */
1176 
1177  return ( 3 );
1178  }
1179  else
1180 
1181  {
1182  fprintf( stdout, "FAILED\nSkipping unknown test function '%s'\n", params[0] );
1183  fflush( stdout );
1184  return( 1 );
1185  }
1186 #else
1187  return( 3 );
1188 #endif
1189  return( ret );
1190 }
1191 
1192 int get_line( FILE *f, char *buf, size_t len )
1193 {
1194  char *ret;
1195 
1196  ret = fgets( buf, len, f );
1197  if( ret == NULL )
1198  return( -1 );
1199 
1200  if( strlen( buf ) && buf[strlen(buf) - 1] == '\n' )
1201  buf[strlen(buf) - 1] = '\0';
1202  if( strlen( buf ) && buf[strlen(buf) - 1] == '\r' )
1203  buf[strlen(buf) - 1] = '\0';
1204 
1205  return( 0 );
1206 }
1207 
1208 int parse_arguments( char *buf, size_t len, char *params[50] )
1209 {
1210  int cnt = 0, i;
1211  char *cur = buf;
1212  char *p = buf, *q;
1213 
1214  params[cnt++] = cur;
1215 
1216  while( *p != '\0' && p < buf + len )
1217  {
1218  if( *p == '\\' )
1219  {
1220  *p++;
1221  *p++;
1222  continue;
1223  }
1224  if( *p == ':' )
1225  {
1226  if( p + 1 < buf + len )
1227  {
1228  cur = p + 1;
1229  params[cnt++] = cur;
1230  }
1231  *p = '\0';
1232  }
1233 
1234  *p++;
1235  }
1236 
1237  // Replace newlines, question marks and colons in strings
1238  for( i = 0; i < cnt; i++ )
1239  {
1240  p = params[i];
1241  q = params[i];
1242 
1243  while( *p != '\0' )
1244  {
1245  if( *p == '\\' && *(p + 1) == 'n' )
1246  {
1247  p += 2;
1248  *(q++) = '\n';
1249  }
1250  else if( *p == '\\' && *(p + 1) == ':' )
1251  {
1252  p += 2;
1253  *(q++) = ':';
1254  }
1255  else if( *p == '\\' && *(p + 1) == '?' )
1256  {
1257  p += 2;
1258  *(q++) = '?';
1259  }
1260  else
1261  *(q++) = *(p++);
1262  }
1263  *q = '\0';
1264  }
1265 
1266  return( cnt );
1267 }
1268 
1269 int main()
1270 {
1271  int ret, i, cnt, total_errors = 0, total_tests = 0, total_skipped = 0;
1272  const char *filename = "/tmp/B.6b9404fc-5e27-486e-9bbd-77463d7343ee/BUILD/polarssl-1.3.2/tests/suites/test_suite_cipher.blowfish.data";
1273  FILE *file;
1274  char buf[5000];
1275  char *params[50];
1276 
1277 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1278  unsigned char alloc_buf[1000000];
1279  memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
1280 #endif
1281 
1282  file = fopen( filename, "r" );
1283  if( file == NULL )
1284  {
1285  fprintf( stderr, "Failed to open\n" );
1286  return( 1 );
1287  }
1288 
1289  while( !feof( file ) )
1290  {
1291  int skip = 0;
1292 
1293  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1294  break;
1295  fprintf( stdout, "%s%.66s", test_errors ? "\n" : "", buf );
1296  fprintf( stdout, " " );
1297  for( i = strlen( buf ) + 1; i < 67; i++ )
1298  fprintf( stdout, "." );
1299  fprintf( stdout, " " );
1300  fflush( stdout );
1301 
1302  total_tests++;
1303 
1304  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1305  break;
1306  cnt = parse_arguments( buf, strlen(buf), params );
1307 
1308  if( strcmp( params[0], "depends_on" ) == 0 )
1309  {
1310  for( i = 1; i < cnt; i++ )
1311  if( dep_check( params[i] ) != 0 )
1312  skip = 1;
1313 
1314  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1315  break;
1316  cnt = parse_arguments( buf, strlen(buf), params );
1317  }
1318 
1319  if( skip == 0 )
1320  {
1321  test_errors = 0;
1322  ret = dispatch_test( cnt, params );
1323  }
1324 
1325  if( skip == 1 || ret == 3 )
1326  {
1327  total_skipped++;
1328  fprintf( stdout, "----\n" );
1329  fflush( stdout );
1330  }
1331  else if( ret == 0 && test_errors == 0 )
1332  {
1333  fprintf( stdout, "PASS\n" );
1334  fflush( stdout );
1335  }
1336  else if( ret == 2 )
1337  {
1338  fprintf( stderr, "FAILED: FATAL PARSE ERROR\n" );
1339  fclose(file);
1340  exit( 2 );
1341  }
1342  else
1343  total_errors++;
1344 
1345  if( ( ret = get_line( file, buf, sizeof(buf) ) ) != 0 )
1346  break;
1347  if( strlen(buf) != 0 )
1348  {
1349  fprintf( stderr, "Should be empty %d\n", (int) strlen(buf) );
1350  return( 1 );
1351  }
1352  }
1353  fclose(file);
1354 
1355  fprintf( stdout, "\n----------------------------------------------------------------------------\n\n");
1356  if( total_errors == 0 )
1357  fprintf( stdout, "PASSED" );
1358  else
1359  fprintf( stdout, "FAILED" );
1360 
1361  fprintf( stdout, " (%d / %d tests (%d skipped))\n",
1362  total_tests - total_errors, total_tests, total_skipped );
1363 
1364 #if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
1365 #if defined(POLARSSL_MEMORY_DEBUG)
1366  memory_buffer_alloc_status();
1367 #endif
1368  memory_buffer_alloc_free();
1369 #endif
1370 
1371  return( total_errors != 0 );
1372 }
1373 
1374 
static int rnd_std_rand(void *rng_state, unsigned char *output, size_t len)
This function just returns data from rand().
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
static int test_errors
uint32_t t_uint
Definition: bignum.h:149
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.
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(* 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 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)
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 void hexify(unsigned char *obuf, const unsigned char *ibuf, int len)
#define POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED
Decryption of block requires a full block.
Definition: cipher.h:57
static int rnd_zero_rand(void *rng_state, unsigned char *output, size_t len)
This function only returns zeros.
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.
static int unhexify(unsigned char *obuf, const char *ibuf)
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 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.
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
#define PUT_UINT32_BE(n, b, i)
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
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)