Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / mbedtls / repo / tests / suites / test_suite_cipher.function
1 /* BEGIN_HEADER */
2 #include "mbedtls/cipher.h"
3
4 #if defined(MBEDTLS_GCM_C)
5 #include "mbedtls/gcm.h"
6 #endif
7 /* END_HEADER */
8
9 /* BEGIN_DEPENDENCIES
10  * depends_on:MBEDTLS_CIPHER_C
11  * END_DEPENDENCIES
12  */
13
14 /* BEGIN_CASE */
15 void mbedtls_cipher_list(  )
16 {
17     const int *cipher_type;
18
19     for( cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++ )
20         TEST_ASSERT( mbedtls_cipher_info_from_type( *cipher_type ) != NULL );
21 }
22 /* END_CASE */
23
24 /* BEGIN_CASE */
25 void cipher_invalid_param_unconditional( )
26 {
27     mbedtls_cipher_context_t valid_ctx;
28     mbedtls_cipher_context_t invalid_ctx;
29     mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
30     mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
31     unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
32     int valid_size = sizeof(valid_buffer);
33     int valid_bitlen = valid_size * 8;
34     const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
35         *( mbedtls_cipher_list() ) );
36     size_t size_t_var;
37
38     (void)valid_mode; /* In some configurations this is unused */
39
40     mbedtls_cipher_init( &valid_ctx );
41     mbedtls_cipher_setup( &valid_ctx, valid_info );
42     mbedtls_cipher_init( &invalid_ctx );
43
44     /* mbedtls_cipher_setup() */
45     TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, NULL ) ==
46                  MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
47
48     /* mbedtls_cipher_get_block_size() */
49     TEST_ASSERT( mbedtls_cipher_get_block_size( &invalid_ctx ) == 0 );
50
51     /* mbedtls_cipher_get_cipher_mode() */
52     TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &invalid_ctx ) ==
53                  MBEDTLS_MODE_NONE );
54
55     /* mbedtls_cipher_get_iv_size() */
56     TEST_ASSERT( mbedtls_cipher_get_iv_size( &invalid_ctx ) == 0 );
57
58     /* mbedtls_cipher_get_type() */
59     TEST_ASSERT(
60         mbedtls_cipher_get_type( &invalid_ctx ) ==
61         MBEDTLS_CIPHER_NONE);
62
63     /* mbedtls_cipher_get_name() */
64     TEST_ASSERT( mbedtls_cipher_get_name( &invalid_ctx ) == 0 );
65
66     /* mbedtls_cipher_get_key_bitlen() */
67     TEST_ASSERT( mbedtls_cipher_get_key_bitlen( &invalid_ctx ) ==
68                  MBEDTLS_KEY_LENGTH_NONE );
69
70     /* mbedtls_cipher_get_operation() */
71     TEST_ASSERT( mbedtls_cipher_get_operation( &invalid_ctx ) ==
72                  MBEDTLS_OPERATION_NONE );
73
74     /* mbedtls_cipher_setkey() */
75     TEST_ASSERT(
76         mbedtls_cipher_setkey( &invalid_ctx,
77                                valid_buffer,
78                                valid_bitlen,
79                                valid_operation ) ==
80         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
81
82     /* mbedtls_cipher_set_iv() */
83     TEST_ASSERT(
84         mbedtls_cipher_set_iv( &invalid_ctx,
85                                valid_buffer,
86                                valid_size ) ==
87         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
88
89     /* mbedtls_cipher_reset() */
90     TEST_ASSERT( mbedtls_cipher_reset( &invalid_ctx ) ==
91                  MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
92
93 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
94     /* mbedtls_cipher_update_ad() */
95     TEST_ASSERT(
96         mbedtls_cipher_update_ad( &invalid_ctx,
97                                   valid_buffer,
98                                   valid_size ) ==
99         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
100 #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
101
102 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
103     /* mbedtls_cipher_set_padding_mode() */
104     TEST_ASSERT( mbedtls_cipher_set_padding_mode( &invalid_ctx, valid_mode ) ==
105                  MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
106 #endif
107
108     /* mbedtls_cipher_update() */
109     TEST_ASSERT(
110         mbedtls_cipher_update( &invalid_ctx,
111                                valid_buffer,
112                                valid_size,
113                                valid_buffer,
114                                &size_t_var ) ==
115         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
116
117     /* mbedtls_cipher_finish() */
118     TEST_ASSERT(
119         mbedtls_cipher_finish( &invalid_ctx,
120                                valid_buffer,
121                                &size_t_var ) ==
122         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
123
124 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
125     /* mbedtls_cipher_write_tag() */
126     TEST_ASSERT(
127         mbedtls_cipher_write_tag( &invalid_ctx,
128                                   valid_buffer,
129                                   valid_size ) ==
130         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
131
132     /* mbedtls_cipher_check_tag() */
133     TEST_ASSERT(
134         mbedtls_cipher_check_tag( &invalid_ctx,
135                                   valid_buffer,
136                                   valid_size ) ==
137         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
138 #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
139
140 exit:
141     mbedtls_cipher_free( &invalid_ctx );
142     mbedtls_cipher_free( &valid_ctx );
143 }
144 /* END_CASE */
145
146 /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
147 void cipher_invalid_param_conditional( )
148 {
149     mbedtls_cipher_context_t valid_ctx;
150
151     mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
152     mbedtls_operation_t invalid_operation = 100;
153     mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
154     unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
155     int valid_size = sizeof(valid_buffer);
156     int valid_bitlen = valid_size * 8;
157     const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
158         *( mbedtls_cipher_list() ) );
159
160     size_t size_t_var;
161
162     (void)valid_mode; /* In some configurations this is unused */
163
164     /* mbedtls_cipher_init() */
165     TEST_VALID_PARAM( mbedtls_cipher_init( &valid_ctx ) );
166     TEST_INVALID_PARAM( mbedtls_cipher_init( NULL ) );
167
168     /* mbedtls_cipher_setup() */
169     TEST_VALID_PARAM( mbedtls_cipher_setup( &valid_ctx, valid_info ) );
170     TEST_INVALID_PARAM_RET(
171         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
172         mbedtls_cipher_setup( NULL, valid_info ) );
173
174     /* mbedtls_cipher_get_block_size() */
175     TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_block_size( NULL ) );
176
177     /* mbedtls_cipher_get_cipher_mode() */
178     TEST_INVALID_PARAM_RET(
179         MBEDTLS_MODE_NONE,
180         mbedtls_cipher_get_cipher_mode( NULL ) );
181
182     /* mbedtls_cipher_get_iv_size() */
183     TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_iv_size( NULL ) );
184
185     /* mbedtls_cipher_get_type() */
186     TEST_INVALID_PARAM_RET(
187         MBEDTLS_CIPHER_NONE,
188         mbedtls_cipher_get_type( NULL ) );
189
190     /* mbedtls_cipher_get_name() */
191     TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_name( NULL ) );
192
193     /* mbedtls_cipher_get_key_bitlen() */
194     TEST_INVALID_PARAM_RET(
195         MBEDTLS_KEY_LENGTH_NONE,
196         mbedtls_cipher_get_key_bitlen( NULL ) );
197
198     /* mbedtls_cipher_get_operation() */
199     TEST_INVALID_PARAM_RET(
200         MBEDTLS_OPERATION_NONE,
201         mbedtls_cipher_get_operation( NULL ) );
202
203     /* mbedtls_cipher_setkey() */
204     TEST_INVALID_PARAM_RET(
205         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
206         mbedtls_cipher_setkey( NULL,
207                                valid_buffer,
208                                valid_bitlen,
209                                valid_operation ) );
210     TEST_INVALID_PARAM_RET(
211         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
212         mbedtls_cipher_setkey( &valid_ctx,
213                                NULL,
214                                valid_bitlen,
215                                valid_operation ) );
216     TEST_INVALID_PARAM_RET(
217         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
218         mbedtls_cipher_setkey( &valid_ctx,
219                                valid_buffer,
220                                valid_bitlen,
221                                invalid_operation ) );
222
223     /* mbedtls_cipher_set_iv() */
224     TEST_INVALID_PARAM_RET(
225         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
226         mbedtls_cipher_set_iv( NULL,
227                                valid_buffer,
228                                valid_size ) );
229     TEST_INVALID_PARAM_RET(
230         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
231         mbedtls_cipher_set_iv( &valid_ctx,
232                                NULL,
233                                valid_size ) );
234
235     /* mbedtls_cipher_reset() */
236     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
237                             mbedtls_cipher_reset( NULL ) );
238
239 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
240     /* mbedtls_cipher_update_ad() */
241     TEST_INVALID_PARAM_RET(
242         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
243         mbedtls_cipher_update_ad( NULL,
244                                   valid_buffer,
245                                   valid_size ) );
246     TEST_INVALID_PARAM_RET(
247         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
248         mbedtls_cipher_update_ad( &valid_ctx,
249                                   NULL,
250                                   valid_size ) );
251 #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
252
253 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
254     /* mbedtls_cipher_set_padding_mode() */
255     TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
256                             mbedtls_cipher_set_padding_mode( NULL, valid_mode ) );
257 #endif
258
259     /* mbedtls_cipher_update() */
260     TEST_INVALID_PARAM_RET(
261         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
262         mbedtls_cipher_update( NULL,
263                                valid_buffer,
264                                valid_size,
265                                valid_buffer,
266                                &size_t_var ) );
267     TEST_INVALID_PARAM_RET(
268         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
269         mbedtls_cipher_update( &valid_ctx,
270                                NULL, valid_size,
271                                valid_buffer,
272                                &size_t_var ) );
273     TEST_INVALID_PARAM_RET(
274         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
275         mbedtls_cipher_update( &valid_ctx,
276                                valid_buffer, valid_size,
277                                NULL,
278                                &size_t_var ) );
279     TEST_INVALID_PARAM_RET(
280         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
281         mbedtls_cipher_update( &valid_ctx,
282                                valid_buffer, valid_size,
283                                valid_buffer,
284                                NULL ) );
285
286     /* mbedtls_cipher_finish() */
287     TEST_INVALID_PARAM_RET(
288         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
289         mbedtls_cipher_finish( NULL,
290                                valid_buffer,
291                                &size_t_var ) );
292     TEST_INVALID_PARAM_RET(
293         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
294         mbedtls_cipher_finish( &valid_ctx,
295                                NULL,
296                                &size_t_var ) );
297     TEST_INVALID_PARAM_RET(
298         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
299         mbedtls_cipher_finish( &valid_ctx,
300                                valid_buffer,
301                                NULL ) );
302
303 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
304     /* mbedtls_cipher_write_tag() */
305     TEST_INVALID_PARAM_RET(
306         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
307         mbedtls_cipher_write_tag( NULL,
308                                   valid_buffer,
309                                   valid_size ) );
310     TEST_INVALID_PARAM_RET(
311         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
312         mbedtls_cipher_write_tag( &valid_ctx,
313                                   NULL,
314                                   valid_size ) );
315
316     /* mbedtls_cipher_check_tag() */
317     TEST_INVALID_PARAM_RET(
318         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
319         mbedtls_cipher_check_tag( NULL,
320                                   valid_buffer,
321                                   valid_size ) );
322     TEST_INVALID_PARAM_RET(
323         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
324         mbedtls_cipher_check_tag( &valid_ctx,
325                                   NULL,
326                                   valid_size ) );
327 #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
328
329     /* mbedtls_cipher_crypt() */
330     TEST_INVALID_PARAM_RET(
331         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
332         mbedtls_cipher_crypt( NULL,
333                               valid_buffer, valid_size,
334                               valid_buffer, valid_size,
335                               valid_buffer, &size_t_var ) );
336     TEST_INVALID_PARAM_RET(
337         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
338         mbedtls_cipher_crypt( &valid_ctx,
339                               NULL, valid_size,
340                               valid_buffer, valid_size,
341                               valid_buffer, &size_t_var ) );
342     TEST_INVALID_PARAM_RET(
343         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
344         mbedtls_cipher_crypt( &valid_ctx,
345                               valid_buffer, valid_size,
346                               NULL, valid_size,
347                               valid_buffer, &size_t_var ) );
348     TEST_INVALID_PARAM_RET(
349         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
350         mbedtls_cipher_crypt( &valid_ctx,
351                               valid_buffer, valid_size,
352                               valid_buffer, valid_size,
353                               NULL, &size_t_var ) );
354     TEST_INVALID_PARAM_RET(
355         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
356         mbedtls_cipher_crypt( &valid_ctx,
357                               valid_buffer, valid_size,
358                               valid_buffer, valid_size,
359                               valid_buffer, NULL ) );
360
361 #if defined(MBEDTLS_CIPHER_MODE_AEAD)
362     /* mbedtls_cipher_auth_encrypt() */
363     TEST_INVALID_PARAM_RET(
364         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
365         mbedtls_cipher_auth_encrypt( NULL,
366                                      valid_buffer, valid_size,
367                                      valid_buffer, valid_size,
368                                      valid_buffer, valid_size,
369                                      valid_buffer, &size_t_var,
370                                      valid_buffer, valid_size ) );
371     TEST_INVALID_PARAM_RET(
372         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
373         mbedtls_cipher_auth_encrypt( &valid_ctx,
374                                      NULL, valid_size,
375                                      valid_buffer, valid_size,
376                                      valid_buffer, valid_size,
377                                      valid_buffer, &size_t_var,
378                                      valid_buffer, valid_size ) );
379     TEST_INVALID_PARAM_RET(
380         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
381         mbedtls_cipher_auth_encrypt( &valid_ctx,
382                                      valid_buffer, valid_size,
383                                      NULL, valid_size,
384                                      valid_buffer, valid_size,
385                                      valid_buffer, &size_t_var,
386                                      valid_buffer, valid_size ) );
387     TEST_INVALID_PARAM_RET(
388         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
389         mbedtls_cipher_auth_encrypt( &valid_ctx,
390                                      valid_buffer, valid_size,
391                                      valid_buffer, valid_size,
392                                      NULL, valid_size,
393                                      valid_buffer, &size_t_var,
394                                      valid_buffer, valid_size ) );
395     TEST_INVALID_PARAM_RET(
396         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
397         mbedtls_cipher_auth_encrypt( &valid_ctx,
398                                      valid_buffer, valid_size,
399                                      valid_buffer, valid_size,
400                                      valid_buffer, valid_size,
401                                      NULL, &size_t_var,
402                                      valid_buffer, valid_size ) );
403     TEST_INVALID_PARAM_RET(
404         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
405         mbedtls_cipher_auth_encrypt( &valid_ctx,
406                                      valid_buffer, valid_size,
407                                      valid_buffer, valid_size,
408                                      valid_buffer, valid_size,
409                                      valid_buffer, NULL,
410                                      valid_buffer, valid_size ) );
411     TEST_INVALID_PARAM_RET(
412         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
413         mbedtls_cipher_auth_encrypt( &valid_ctx,
414                                      valid_buffer, valid_size,
415                                      valid_buffer, valid_size,
416                                      valid_buffer, valid_size,
417                                      valid_buffer, &size_t_var,
418                                      NULL, valid_size ) );
419
420     /* mbedtls_cipher_auth_decrypt() */
421     TEST_INVALID_PARAM_RET(
422         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
423         mbedtls_cipher_auth_decrypt( NULL,
424                                      valid_buffer, valid_size,
425                                      valid_buffer, valid_size,
426                                      valid_buffer, valid_size,
427                                      valid_buffer, &size_t_var,
428                                      valid_buffer, valid_size ) );
429     TEST_INVALID_PARAM_RET(
430         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
431         mbedtls_cipher_auth_decrypt( &valid_ctx,
432                                      NULL, valid_size,
433                                      valid_buffer, valid_size,
434                                      valid_buffer, valid_size,
435                                      valid_buffer, &size_t_var,
436                                      valid_buffer, valid_size ) );
437     TEST_INVALID_PARAM_RET(
438         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
439         mbedtls_cipher_auth_decrypt( &valid_ctx,
440                                      valid_buffer, valid_size,
441                                      NULL, valid_size,
442                                      valid_buffer, valid_size,
443                                      valid_buffer, &size_t_var,
444                                      valid_buffer, valid_size ) );
445     TEST_INVALID_PARAM_RET(
446         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
447         mbedtls_cipher_auth_decrypt( &valid_ctx,
448                                      valid_buffer, valid_size,
449                                      valid_buffer, valid_size,
450                                      NULL, valid_size,
451                                      valid_buffer, &size_t_var,
452                                      valid_buffer, valid_size ) );
453     TEST_INVALID_PARAM_RET(
454         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
455         mbedtls_cipher_auth_decrypt( &valid_ctx,
456                                      valid_buffer, valid_size,
457                                      valid_buffer, valid_size,
458                                      valid_buffer, valid_size,
459                                      NULL, &size_t_var,
460                                      valid_buffer, valid_size ) );
461     TEST_INVALID_PARAM_RET(
462         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
463         mbedtls_cipher_auth_decrypt( &valid_ctx,
464                                      valid_buffer, valid_size,
465                                      valid_buffer, valid_size,
466                                      valid_buffer, valid_size,
467                                      valid_buffer, NULL,
468                                      valid_buffer, valid_size ) );
469     TEST_INVALID_PARAM_RET(
470         MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
471         mbedtls_cipher_auth_decrypt( &valid_ctx,
472                                      valid_buffer, valid_size,
473                                      valid_buffer, valid_size,
474                                      valid_buffer, valid_size,
475                                      valid_buffer, &size_t_var,
476                                      NULL, valid_size ) );
477 #endif /* defined(MBEDTLS_CIPHER_MODE_AEAD) */
478
479     /* mbedtls_cipher_free() */
480     TEST_VALID_PARAM( mbedtls_cipher_free( NULL ) );
481 exit:
482     TEST_VALID_PARAM( mbedtls_cipher_free( &valid_ctx ) );
483 }
484 /* END_CASE */
485
486 /* BEGIN_CASE depends_on:MBEDTLS_AES_C */
487 void cipher_special_behaviours(  )
488 {
489     const mbedtls_cipher_info_t *cipher_info;
490     mbedtls_cipher_context_t ctx;
491     unsigned char input[32];
492     unsigned char output[32];
493 #if defined (MBEDTLS_CIPHER_MODE_CBC)
494     unsigned char iv[32];
495 #endif
496     size_t olen = 0;
497
498     mbedtls_cipher_init( &ctx );
499     memset( input, 0, sizeof( input ) );
500     memset( output, 0, sizeof( output ) );
501 #if defined(MBEDTLS_CIPHER_MODE_CBC)
502     memset( iv, 0, sizeof( iv ) );
503
504     /* Check and get info structures */
505     cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
506     TEST_ASSERT( NULL != cipher_info );
507
508     TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
509
510     /* IV too big */
511     TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
512                  == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
513
514     /* IV too small */
515     TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
516                  == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
517
518     mbedtls_cipher_free( &ctx );
519     mbedtls_cipher_init( &ctx );
520 #endif /* MBEDTLS_CIPHER_MODE_CBC */
521     cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
522     TEST_ASSERT( NULL != cipher_info );
523
524     TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
525
526     /* Update ECB with partial block */
527     TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
528                  == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
529
530 exit:
531     mbedtls_cipher_free( &ctx );
532 }
533 /* END_CASE */
534
535 /* BEGIN_CASE */
536 void enc_dec_buf( int cipher_id, char * cipher_string, int key_len,
537                   int length_val, int pad_mode )
538 {
539     size_t length = length_val, outlen, total_len, i, block_size;
540     unsigned char key[64];
541     unsigned char iv[16];
542     unsigned char ad[13];
543     unsigned char tag[16];
544     unsigned char inbuf[64];
545     unsigned char encbuf[64];
546     unsigned char decbuf[64];
547
548     const mbedtls_cipher_info_t *cipher_info;
549     mbedtls_cipher_context_t ctx_dec;
550     mbedtls_cipher_context_t ctx_enc;
551
552     /*
553      * Prepare contexts
554      */
555     mbedtls_cipher_init( &ctx_dec );
556     mbedtls_cipher_init( &ctx_enc );
557
558     memset( key, 0x2a, sizeof( key ) );
559
560     /* Check and get info structures */
561     cipher_info = mbedtls_cipher_info_from_type( cipher_id );
562     TEST_ASSERT( NULL != cipher_info );
563     TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
564
565     /* Initialise enc and dec contexts */
566     TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
567     TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
568
569     TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
570     TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
571
572 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
573     if( -1 != pad_mode )
574     {
575         TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
576         TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
577     }
578 #else
579     (void) pad_mode;
580 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
581
582     /*
583      * Do a few encode/decode cycles
584      */
585     for( i = 0; i < 3; i++ )
586     {
587     memset( iv , 0x00 + i, sizeof( iv ) );
588     memset( ad, 0x10 + i, sizeof( ad ) );
589     memset( inbuf, 0x20 + i, sizeof( inbuf ) );
590
591     memset( encbuf, 0, sizeof( encbuf ) );
592     memset( decbuf, 0, sizeof( decbuf ) );
593     memset( tag, 0, sizeof( tag ) );
594
595     TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
596     TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
597
598     TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
599     TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
600
601 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
602     TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
603     TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
604 #endif
605
606     block_size = mbedtls_cipher_get_block_size( &ctx_enc );
607     TEST_ASSERT( block_size != 0 );
608
609     /* encode length number of bytes from inbuf */
610     TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
611     total_len = outlen;
612
613     TEST_ASSERT( total_len == length ||
614                  ( total_len % block_size == 0 &&
615                    total_len < length &&
616                    total_len + block_size > length ) );
617
618     TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
619     total_len += outlen;
620
621 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
622     TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
623 #endif
624
625     TEST_ASSERT( total_len == length ||
626                  ( total_len % block_size == 0 &&
627                    total_len > length &&
628                    total_len <= length + block_size ) );
629
630     /* decode the previously encoded string */
631     TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
632     total_len = outlen;
633
634     TEST_ASSERT( total_len == length ||
635                  ( total_len % block_size == 0 &&
636                    total_len < length &&
637                    total_len + block_size >= length ) );
638
639     TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
640     total_len += outlen;
641
642 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
643     TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
644 #endif
645
646     /* check result */
647     TEST_ASSERT( total_len == length );
648     TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
649     }
650
651     /*
652      * Done
653      */
654 exit:
655     mbedtls_cipher_free( &ctx_dec );
656     mbedtls_cipher_free( &ctx_enc );
657 }
658 /* END_CASE */
659
660 /* BEGIN_CASE */
661 void enc_fail( int cipher_id, int pad_mode, int key_len, int length_val,
662                int ret )
663 {
664     size_t length = length_val;
665     unsigned char key[32];
666     unsigned char iv[16];
667
668     const mbedtls_cipher_info_t *cipher_info;
669     mbedtls_cipher_context_t ctx;
670
671     unsigned char inbuf[64];
672     unsigned char encbuf[64];
673
674     size_t outlen = 0;
675
676     memset( key, 0, 32 );
677     memset( iv , 0, 16 );
678
679     mbedtls_cipher_init( &ctx );
680
681     memset( inbuf, 5, 64 );
682     memset( encbuf, 0, 64 );
683
684     /* Check and get info structures */
685     cipher_info = mbedtls_cipher_info_from_type( cipher_id );
686     TEST_ASSERT( NULL != cipher_info );
687
688     /* Initialise context */
689     TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
690     TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
691 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
692     TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
693 #else
694     (void) pad_mode;
695 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
696     TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
697     TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
698 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
699     TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
700 #endif
701
702     /* encode length number of bytes from inbuf */
703     TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
704     TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
705
706     /* done */
707 exit:
708     mbedtls_cipher_free( &ctx );
709 }
710 /* END_CASE */
711
712 /* BEGIN_CASE */
713 void dec_empty_buf( int cipher )
714 {
715     unsigned char key[32];
716     unsigned char iv[16];
717
718     mbedtls_cipher_context_t ctx_dec;
719     const mbedtls_cipher_info_t *cipher_info;
720
721     unsigned char encbuf[64];
722     unsigned char decbuf[64];
723
724     size_t outlen = 0;
725
726     int expected_ret;
727
728     memset( key, 0, 32 );
729     memset( iv , 0, 16 );
730
731     mbedtls_cipher_init( &ctx_dec );
732
733     memset( encbuf, 0, 64 );
734     memset( decbuf, 0, 64 );
735
736     /* Initialise context */
737     cipher_info = mbedtls_cipher_info_from_type( cipher );
738     TEST_ASSERT( NULL != cipher_info);
739     TEST_ASSERT( sizeof(key) * 8 >= cipher_info->key_bitlen );
740
741     TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
742
743     TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec,
744                                              key, cipher_info->key_bitlen,
745                                              MBEDTLS_DECRYPT ) );
746
747     TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
748
749     TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
750
751 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
752     TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
753 #endif
754
755     /* decode 0-byte string */
756     TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
757     TEST_ASSERT( 0 == outlen );
758
759     if ( cipher_info->mode == MBEDTLS_MODE_CBC ||
760          cipher_info->mode == MBEDTLS_MODE_ECB )
761     {
762         /* CBC and ECB ciphers need a full block of input. */
763         expected_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
764     }
765     else
766     {
767         /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
768          * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
769          * decrypting an empty buffer. */
770         expected_ret = 0;
771     }
772
773     TEST_ASSERT( expected_ret == mbedtls_cipher_finish(
774                                     &ctx_dec, decbuf + outlen, &outlen ) );
775     TEST_ASSERT( 0 == outlen );
776
777 exit:
778     mbedtls_cipher_free( &ctx_dec );
779 }
780 /* END_CASE */
781
782 /* BEGIN_CASE */
783 void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
784                             int second_length_val, int pad_mode,
785                             int first_encrypt_output_len, int second_encrypt_output_len,
786                             int first_decrypt_output_len, int second_decrypt_output_len )
787 {
788     size_t first_length = first_length_val;
789     size_t second_length = second_length_val;
790     size_t length = first_length + second_length;
791     size_t block_size;
792     unsigned char key[32];
793     unsigned char iv[16];
794
795     mbedtls_cipher_context_t ctx_dec;
796     mbedtls_cipher_context_t ctx_enc;
797     const mbedtls_cipher_info_t *cipher_info;
798
799     unsigned char inbuf[64];
800     unsigned char encbuf[64];
801     unsigned char decbuf[64];
802
803     size_t outlen = 0;
804     size_t totaloutlen = 0;
805
806     memset( key, 0, 32 );
807     memset( iv , 0, 16 );
808
809     mbedtls_cipher_init( &ctx_dec );
810     mbedtls_cipher_init( &ctx_enc );
811
812     memset( inbuf, 5, 64 );
813     memset( encbuf, 0, 64 );
814     memset( decbuf, 0, 64 );
815
816     /* Initialise enc and dec contexts */
817     cipher_info = mbedtls_cipher_info_from_type( cipher_id );
818     TEST_ASSERT( NULL != cipher_info);
819
820     TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
821     TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
822
823     TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
824     TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
825
826 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
827     if( -1 != pad_mode )
828     {
829         TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
830         TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
831     }
832 #else
833     (void) pad_mode;
834 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
835
836     TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
837     TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, 16 ) );
838
839     TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
840     TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
841
842 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
843     TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
844     TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
845 #endif
846
847     block_size = mbedtls_cipher_get_block_size( &ctx_enc );
848     TEST_ASSERT( block_size != 0 );
849
850     /* encode length number of bytes from inbuf */
851     TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
852     TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
853     totaloutlen = outlen;
854     TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
855     TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
856     totaloutlen += outlen;
857     TEST_ASSERT( totaloutlen == length ||
858                  ( totaloutlen % block_size == 0 &&
859                    totaloutlen < length &&
860                    totaloutlen + block_size > length ) );
861
862     TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
863     totaloutlen += outlen;
864     TEST_ASSERT( totaloutlen == length ||
865                  ( totaloutlen % block_size == 0 &&
866                    totaloutlen > length &&
867                    totaloutlen <= length + block_size ) );
868
869     /* decode the previously encoded string */
870     second_length = totaloutlen - first_length;
871     TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
872     TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
873     totaloutlen = outlen;
874     TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
875     TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
876     totaloutlen += outlen;
877
878     TEST_ASSERT( totaloutlen == length ||
879                  ( totaloutlen % block_size == 0 &&
880                    totaloutlen < length &&
881                    totaloutlen + block_size >= length ) );
882
883     TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
884     totaloutlen += outlen;
885
886     TEST_ASSERT( totaloutlen == length );
887
888     TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
889
890 exit:
891     mbedtls_cipher_free( &ctx_dec );
892     mbedtls_cipher_free( &ctx_enc );
893 }
894 /* END_CASE */
895
896 /* BEGIN_CASE */
897 void decrypt_test_vec( int cipher_id, int pad_mode, data_t * key,
898                        data_t * iv, data_t * cipher,
899                        data_t * clear, data_t * ad, data_t * tag,
900                        int finish_result, int tag_result )
901 {
902     unsigned char output[265];
903     mbedtls_cipher_context_t ctx;
904     size_t outlen, total_len;
905
906     mbedtls_cipher_init( &ctx );
907
908     memset( output, 0x00, sizeof( output ) );
909
910 #if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
911     ((void) ad);
912     ((void) tag);
913 #endif
914
915     /* Prepare context */
916     TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
917                                        mbedtls_cipher_info_from_type( cipher_id ) ) );
918     TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
919 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
920     if( pad_mode != -1 )
921         TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
922 #else
923     (void) pad_mode;
924 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
925     TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv->x, iv->len ) );
926     TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
927 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
928     TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad->x, ad->len ) );
929 #endif
930
931     /* decode buffer and check tag->x */
932     total_len = 0;
933     TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher->x, cipher->len, output, &outlen ) );
934     total_len += outlen;
935     TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
936                                                  &outlen ) );
937     total_len += outlen;
938 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
939     TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag->x, tag->len ) );
940 #endif
941
942     /* check plaintext only if everything went fine */
943     if( 0 == finish_result && 0 == tag_result )
944     {
945         TEST_ASSERT( total_len == clear->len );
946         TEST_ASSERT( 0 == memcmp( output, clear->x, clear->len ) );
947     }
948
949 exit:
950     mbedtls_cipher_free( &ctx );
951 }
952 /* END_CASE */
953
954 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */
955 void auth_crypt_tv( int cipher_id, data_t * key, data_t * iv,
956                     data_t * ad, data_t * cipher, data_t * tag,
957                     char * result, data_t * clear, int use_psa )
958 {
959     /* Takes an AEAD ciphertext + tag and performs a pair
960      * of AEAD decryption and AEAD encryption. It checks that
961      * this results in the expected plaintext, and that
962      * decryption and encryption are inverse to one another. */
963
964     int ret;
965     unsigned char output[300];        /* Temporary buffer for results of
966                                        * encryption and decryption. */
967     unsigned char *output_tag = NULL; /* Temporary buffer for tag in the
968                                        * encryption step. */
969
970     mbedtls_cipher_context_t ctx;
971     size_t outlen;
972
973     unsigned char *tmp_tag    = NULL;
974     unsigned char *tmp_cipher = NULL;
975
976     mbedtls_cipher_init( &ctx );
977     memset( output, 0xFF, sizeof( output ) );
978
979     /* Prepare context */
980 #if !defined(MBEDTLS_USE_PSA_CRYPTO)
981     (void) use_psa;
982 #else
983     if( use_psa == 1 )
984     {
985         TEST_ASSERT( psa_crypto_init() == 0 );
986
987         /* PSA requires that the tag immediately follows the ciphertext. */
988         tmp_cipher = mbedtls_calloc( 1, cipher->len + tag->len );
989         TEST_ASSERT( tmp_cipher != NULL );
990         tmp_tag = tmp_cipher + cipher->len;
991
992         memcpy( tmp_cipher, cipher->x, cipher->len );
993         memcpy( tmp_tag, tag->x, tag->len );
994
995         TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( &ctx,
996                               mbedtls_cipher_info_from_type( cipher_id ),
997                               tag->len ) );
998     }
999     else
1000 #endif
1001     {
1002         tmp_tag = tag->x;
1003         tmp_cipher = cipher->x;
1004         TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
1005                               mbedtls_cipher_info_from_type( cipher_id ) ) );
1006     }
1007
1008     TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len,
1009                                              MBEDTLS_DECRYPT ) );
1010
1011     /* decode buffer and check tag->x */
1012
1013     /* Sanity check that we don't use overly long inputs. */
1014     TEST_ASSERT( sizeof( output ) >= cipher->len );
1015
1016     ret = mbedtls_cipher_auth_decrypt( &ctx, iv->x, iv->len, ad->x, ad->len,
1017                                tmp_cipher, cipher->len, output, &outlen,
1018                                tmp_tag, tag->len );
1019
1020     /* make sure the message is rejected if it should be */
1021     if( strcmp( result, "FAIL" ) == 0 )
1022     {
1023         TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
1024         goto exit;
1025     }
1026
1027     /* otherwise, make sure it was decrypted properly */
1028     TEST_ASSERT( ret == 0 );
1029
1030     TEST_ASSERT( outlen == clear->len );
1031     TEST_ASSERT( memcmp( output, clear->x, clear->len ) == 0 );
1032
1033     /* then encrypt the clear->x and make sure we get the same ciphertext and tag->x */
1034     mbedtls_cipher_free( &ctx );
1035 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1036     if( use_psa == 1 )
1037     {
1038         TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( &ctx,
1039                               mbedtls_cipher_info_from_type( cipher_id ),
1040                               tag->len ) );
1041     }
1042     else
1043 #endif
1044     {
1045         TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
1046                               mbedtls_cipher_info_from_type( cipher_id ) ) );
1047     }
1048     TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len,
1049                                              MBEDTLS_ENCRYPT ) );
1050
1051     memset( output, 0xFF, sizeof( output ) );
1052     outlen = 0;
1053
1054     /* Sanity check that we don't use overly long inputs. */
1055     TEST_ASSERT( sizeof( output ) >= clear->len + tag->len );
1056
1057     output_tag = output + clear->len;
1058     ret = mbedtls_cipher_auth_encrypt( &ctx, iv->x, iv->len, ad->x, ad->len,
1059                                clear->x, clear->len, output, &outlen,
1060                                output_tag, tag->len );
1061     TEST_ASSERT( ret == 0 );
1062
1063     TEST_ASSERT( outlen == cipher->len );
1064     TEST_ASSERT( memcmp( output, cipher->x, cipher->len ) == 0 );
1065     TEST_ASSERT( memcmp( output_tag, tag->x, tag->len ) == 0 );
1066
1067 exit:
1068
1069 #if defined(MBEDTLS_USE_PSA_CRYPTO)
1070     if( use_psa == 1 )
1071     {
1072         mbedtls_free( tmp_cipher );
1073     }
1074 #endif /* MBEDTLS_USE_PSA_CRYPTO */
1075
1076     mbedtls_cipher_free( &ctx );
1077 }
1078 /* END_CASE */
1079
1080 /* BEGIN_CASE */
1081 void test_vec_ecb( int cipher_id, int operation, data_t * key,
1082                    data_t * input, data_t * result, int finish_result
1083                    )
1084 {
1085     mbedtls_cipher_context_t ctx;
1086     unsigned char output[32];
1087     size_t outlen;
1088
1089     mbedtls_cipher_init( &ctx );
1090
1091     memset( output, 0x00, sizeof( output ) );
1092
1093     /* Prepare context */
1094     TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
1095                                        mbedtls_cipher_info_from_type( cipher_id ) ) );
1096
1097
1098     TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
1099
1100     TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input->x,
1101                                      mbedtls_cipher_get_block_size( &ctx ),
1102                                      output, &outlen ) );
1103     TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
1104     TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
1105                                                  &outlen ) );
1106     TEST_ASSERT( 0 == outlen );
1107
1108     /* check plaintext only if everything went fine */
1109     if( 0 == finish_result )
1110         TEST_ASSERT( 0 == memcmp( output, result->x,
1111                                   mbedtls_cipher_get_block_size( &ctx ) ) );
1112
1113 exit:
1114     mbedtls_cipher_free( &ctx );
1115 }
1116 /* END_CASE */
1117
1118 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
1119 void test_vec_crypt( int cipher_id, int operation, char *hex_key,
1120                      char *hex_iv, char *hex_input, char *hex_result,
1121                      int finish_result, int use_psa )
1122 {
1123     unsigned char key[50];
1124     unsigned char input[16];
1125     unsigned char result[16];
1126     unsigned char iv[16];
1127     size_t key_len, iv_len, inputlen, resultlen;
1128     mbedtls_cipher_context_t ctx;
1129     unsigned char output[32];
1130     size_t outlen;
1131
1132     mbedtls_cipher_init( &ctx );
1133
1134     memset( key, 0x00, sizeof( key ) );
1135     memset( input, 0x00, sizeof( input ) );
1136     memset( result, 0x00, sizeof( result ) );
1137     memset( output, 0x00, sizeof( output ) );
1138     memset( iv, 0x00, sizeof( iv ) );
1139
1140     /* Prepare context */
1141 #if !defined(MBEDTLS_USE_PSA_CRYPTO)
1142     (void) use_psa;
1143 #else
1144     if( use_psa == 1 )
1145     {
1146         TEST_ASSERT( psa_crypto_init() == 0 );
1147         TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( &ctx,
1148                               mbedtls_cipher_info_from_type( cipher_id ), 0 ) );
1149     }
1150     else
1151 #endif /* MBEDTLS_USE_PSA_CRYPTO */
1152     TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
1153                               mbedtls_cipher_info_from_type( cipher_id ) ) );
1154
1155     key_len = unhexify( key, hex_key );
1156     inputlen =  unhexify( input, hex_input );
1157     resultlen = unhexify( result, hex_result );
1158
1159     TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, operation ) );
1160     if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode )
1161         TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) );
1162
1163     iv_len = unhexify( iv, hex_iv );
1164
1165     TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv_len ? iv : NULL,
1166                                                         iv_len, input, inputlen,
1167                                                         output, &outlen ) );
1168     TEST_ASSERT( resultlen == outlen );
1169     /* check plaintext only if everything went fine */
1170     if( 0 == finish_result )
1171         TEST_ASSERT( 0 == memcmp( output, result, outlen ) );
1172
1173 exit:
1174     mbedtls_cipher_free( &ctx );
1175 }
1176 /* END_CASE */
1177
1178 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
1179 void set_padding( int cipher_id, int pad_mode, int ret )
1180 {
1181     const mbedtls_cipher_info_t *cipher_info;
1182     mbedtls_cipher_context_t ctx;
1183
1184     mbedtls_cipher_init( &ctx );
1185
1186     cipher_info = mbedtls_cipher_info_from_type( cipher_id );
1187     TEST_ASSERT( NULL != cipher_info );
1188     TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
1189
1190     TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
1191
1192 exit:
1193     mbedtls_cipher_free( &ctx );
1194 }
1195 /* END_CASE */
1196
1197 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
1198 void check_padding( int pad_mode, data_t * input, int ret, int dlen_check
1199                     )
1200 {
1201     mbedtls_cipher_info_t cipher_info;
1202     mbedtls_cipher_context_t ctx;
1203     size_t dlen;
1204
1205     /* build a fake context just for getting access to get_padding */
1206     mbedtls_cipher_init( &ctx );
1207     cipher_info.mode = MBEDTLS_MODE_CBC;
1208     ctx.cipher_info = &cipher_info;
1209
1210     TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
1211
1212
1213     TEST_ASSERT( ret == ctx.get_padding( input->x, input->len, &dlen ) );
1214     if( 0 == ret )
1215         TEST_ASSERT( dlen == (size_t) dlen_check );
1216 }
1217 /* END_CASE */