blob: 94ea88f791d964b957324eae18396d15e6b9f3b8 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +02003
k-stachowiakd8727232019-07-29 17:46:29 +02004#if defined(MBEDTLS_AES_C)
5#include "mbedtls/aes.h"
6#endif
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +020010#endif
Gilles Peskine5386f6b2019-08-01 12:47:40 +020011
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +010012#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
13#define MBEDTLS_CIPHER_AUTH_CRYPT
14#endif
15
Gilles Peskine0be02bd2021-07-19 16:32:54 +020016/* Check the internal consistency of a cipher info structure, and
17 * check it against mbedtls_cipher_info_from_xxx(). */
18static int check_cipher_info( mbedtls_cipher_type_t type,
19 const mbedtls_cipher_info_t *info )
20{
21 size_t key_bitlen;
22
23 TEST_ASSERT( info != NULL );
24 TEST_EQUAL( type, mbedtls_cipher_info_get_type( info ) );
25 TEST_EQUAL( type, info->type );
26 TEST_ASSERT( mbedtls_cipher_info_from_type( type ) == info );
27
28 TEST_EQUAL( info->mode, mbedtls_cipher_info_get_mode( info ) );
29
30 /* Insist that get_name() return the string from the structure and
31 * not a copy. A copy would have an unknown storage duration. */
32 TEST_ASSERT( mbedtls_cipher_info_get_name( info ) == info->name );
33 TEST_ASSERT( mbedtls_cipher_info_from_string( info->name ) == info );
34
35 key_bitlen = mbedtls_cipher_info_get_key_bitlen( info );
36 TEST_ASSERT( key_bitlen % 8 == 0 );
37 /* All current and plausible supported ciphers use a 64-bit, 128-bit
38 * or 256-bit key, except XTS which uses a double AES key. */
39 TEST_ASSERT( key_bitlen >= 64 );
40 TEST_ASSERT( key_bitlen <= 512 );
41
42 return( 1 );
43
44exit:
45 return( 0 );
46}
47
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010048#if defined(MBEDTLS_CIPHER_AUTH_CRYPT)
49/* Helper for resetting key/direction
50 *
51 * The documentation doesn't explicitly say whether calling
52 * mbedtls_cipher_setkey() twice is allowed or not. This currently works with
53 * the default software implementation, but only by accident. It isn't
54 * guaranteed to work with new ciphers or with alternative implementations of
55 * individual ciphers, and it doesn't work with the PSA wrappers. So don't do
56 * it, and instead start with a fresh context.
57 */
Gilles Peskine8a3d2342020-12-03 21:06:15 +010058static int cipher_reset_key( mbedtls_cipher_context_t *ctx, int cipher_id,
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010059 int use_psa, size_t tag_len, const data_t *key, int direction )
60{
61 mbedtls_cipher_free( ctx );
62 mbedtls_cipher_init( ctx );
63
64#if !defined(MBEDTLS_USE_PSA_CRYPTO)
65 (void) use_psa;
66 (void) tag_len;
67#else
68 if( use_psa == 1 )
69 {
70 TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( ctx,
71 mbedtls_cipher_info_from_type( cipher_id ),
72 tag_len ) );
73 }
74 else
75#endif /* MBEDTLS_USE_PSA_CRYPTO */
76 {
77 TEST_ASSERT( 0 == mbedtls_cipher_setup( ctx,
78 mbedtls_cipher_info_from_type( cipher_id ) ) );
79 }
80
81 TEST_ASSERT( 0 == mbedtls_cipher_setkey( ctx, key->x, 8 * key->len,
82 direction ) );
Gilles Peskine8a3d2342020-12-03 21:06:15 +010083 return( 1 );
84
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010085exit:
Gilles Peskine8a3d2342020-12-03 21:06:15 +010086 return( 0 );
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010087}
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +010088
89/*
90 * Check if a buffer is all-0 bytes:
91 * return 1 if it is,
92 * 0 if it isn't.
93 */
94int buffer_is_all_zero( const uint8_t *buf, size_t size )
95{
96 for( size_t i = 0; i < size; i++ )
97 if( buf[i] != 0 )
98 return 0;
99 return 1;
100}
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100101#endif /* MBEDTLS_CIPHER_AUTH_CRYPT */
102
Paul Bakker33b43f12013-08-20 11:48:36 +0200103/* END_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000104
Paul Bakker33b43f12013-08-20 11:48:36 +0200105/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 * depends_on:MBEDTLS_CIPHER_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200107 * END_DEPENDENCIES
108 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000109
Paul Bakker33b43f12013-08-20 11:48:36 +0200110/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100111void mbedtls_cipher_list( )
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100112{
113 const int *cipher_type;
114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 for( cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++ )
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200116 {
117 const mbedtls_cipher_info_t *info =
118 mbedtls_cipher_info_from_type( *cipher_type );
119 mbedtls_test_set_step( *cipher_type );
120 if( ! check_cipher_info( *cipher_type, info ) )
121 goto exit;
122 }
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100123}
124/* END_CASE */
125
126/* BEGIN_CASE */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500127void cipher_invalid_param_unconditional( )
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200128{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500129 mbedtls_cipher_context_t valid_ctx;
130 mbedtls_cipher_context_t invalid_ctx;
131 mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
132 mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
133 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
134 int valid_size = sizeof(valid_buffer);
135 int valid_bitlen = valid_size * 8;
136 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
137 *( mbedtls_cipher_list() ) );
138 size_t size_t_var;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200139
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500140 (void)valid_mode; /* In some configurations this is unused */
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200141
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500142 mbedtls_cipher_init( &valid_ctx );
143 mbedtls_cipher_setup( &valid_ctx, valid_info );
144 mbedtls_cipher_init( &invalid_ctx );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200145
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500146 /* mbedtls_cipher_setup() */
147 TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, NULL ) ==
148 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200149
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500150 /* mbedtls_cipher_get_block_size() */
151 TEST_ASSERT( mbedtls_cipher_get_block_size( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200152
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500153 /* mbedtls_cipher_get_cipher_mode() */
154 TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &invalid_ctx ) ==
155 MBEDTLS_MODE_NONE );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200156
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500157 /* mbedtls_cipher_get_iv_size() */
158 TEST_ASSERT( mbedtls_cipher_get_iv_size( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200159
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500160 /* mbedtls_cipher_get_type() */
161 TEST_ASSERT(
162 mbedtls_cipher_get_type( &invalid_ctx ) ==
163 MBEDTLS_CIPHER_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200164
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500165 /* mbedtls_cipher_get_name() */
166 TEST_ASSERT( mbedtls_cipher_get_name( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200167
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500168 /* mbedtls_cipher_get_key_bitlen() */
169 TEST_ASSERT( mbedtls_cipher_get_key_bitlen( &invalid_ctx ) ==
170 MBEDTLS_KEY_LENGTH_NONE );
171
172 /* mbedtls_cipher_get_operation() */
173 TEST_ASSERT( mbedtls_cipher_get_operation( &invalid_ctx ) ==
174 MBEDTLS_OPERATION_NONE );
175
176 /* mbedtls_cipher_setkey() */
177 TEST_ASSERT(
178 mbedtls_cipher_setkey( &invalid_ctx,
179 valid_buffer,
180 valid_bitlen,
181 valid_operation ) ==
182 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
183
184 /* mbedtls_cipher_set_iv() */
185 TEST_ASSERT(
186 mbedtls_cipher_set_iv( &invalid_ctx,
187 valid_buffer,
188 valid_size ) ==
189 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
190
191 /* mbedtls_cipher_reset() */
192 TEST_ASSERT( mbedtls_cipher_reset( &invalid_ctx ) ==
193 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200194
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200195#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500196 /* mbedtls_cipher_update_ad() */
197 TEST_ASSERT(
198 mbedtls_cipher_update_ad( &invalid_ctx,
199 valid_buffer,
200 valid_size ) ==
201 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
202#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
203
204#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
205 /* mbedtls_cipher_set_padding_mode() */
206 TEST_ASSERT( mbedtls_cipher_set_padding_mode( &invalid_ctx, valid_mode ) ==
207 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200208#endif
209
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500210 /* mbedtls_cipher_update() */
211 TEST_ASSERT(
212 mbedtls_cipher_update( &invalid_ctx,
213 valid_buffer,
214 valid_size,
215 valid_buffer,
216 &size_t_var ) ==
217 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200218
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500219 /* mbedtls_cipher_finish() */
220 TEST_ASSERT(
221 mbedtls_cipher_finish( &invalid_ctx,
222 valid_buffer,
223 &size_t_var ) ==
224 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200225
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200226#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500227 /* mbedtls_cipher_write_tag() */
228 TEST_ASSERT(
229 mbedtls_cipher_write_tag( &invalid_ctx,
230 valid_buffer,
231 valid_size ) ==
232 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200233
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500234 /* mbedtls_cipher_check_tag() */
235 TEST_ASSERT(
236 mbedtls_cipher_check_tag( &invalid_ctx,
237 valid_buffer,
238 valid_size ) ==
239 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
240#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
241
242exit:
243 mbedtls_cipher_free( &invalid_ctx );
244 mbedtls_cipher_free( &valid_ctx );
245}
246/* END_CASE */
247
TRodziewicz062f3532021-05-25 15:15:57 +0200248/* BEGIN_CASE depends_on:NOT_DEFINED */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500249void cipher_invalid_param_conditional( )
250{
251 mbedtls_cipher_context_t valid_ctx;
252
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500253 mbedtls_operation_t invalid_operation = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500254 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
255 int valid_size = sizeof(valid_buffer);
256 int valid_bitlen = valid_size * 8;
257 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
258 *( mbedtls_cipher_list() ) );
259
Ronald Cron875b5fb2021-05-21 08:50:00 +0200260 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500261 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
262 mbedtls_cipher_setkey( &valid_ctx,
263 valid_buffer,
264 valid_bitlen,
265 invalid_operation ) );
266
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500267exit:
TRodziewicz70199552021-05-27 13:52:59 +0200268 ;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200269}
270/* END_CASE */
271
Paul Bakker6a9c7252016-07-14 13:46:10 +0100272/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100273void cipher_special_behaviours( )
Paul Bakker6a9c7252016-07-14 13:46:10 +0100274{
275 const mbedtls_cipher_info_t *cipher_info;
276 mbedtls_cipher_context_t ctx;
277 unsigned char input[32];
278 unsigned char output[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300279#if defined (MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100280 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300281#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100282 size_t olen = 0;
283
284 mbedtls_cipher_init( &ctx );
285 memset( input, 0, sizeof( input ) );
286 memset( output, 0, sizeof( output ) );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300287#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100288 memset( iv, 0, sizeof( iv ) );
289
290 /* Check and get info structures */
Ron Eldor7b012442017-09-25 17:03:12 +0300291 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
Paul Bakker6a9c7252016-07-14 13:46:10 +0100292 TEST_ASSERT( NULL != cipher_info );
293
294 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
295
296 /* IV too big */
297 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
298 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
299
300 /* IV too small */
301 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
302 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
303
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300304 mbedtls_cipher_free( &ctx );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300305 mbedtls_cipher_init( &ctx );
Ron Eldor6f90ed82017-09-26 12:08:54 +0300306#endif /* MBEDTLS_CIPHER_MODE_CBC */
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300307 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
Ron Eldor7b012442017-09-25 17:03:12 +0300308 TEST_ASSERT( NULL != cipher_info );
309
310 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
311
Paul Bakker6a9c7252016-07-14 13:46:10 +0100312 /* Update ECB with partial block */
313 TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
314 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
315
316exit:
317 mbedtls_cipher_free( &ctx );
318}
319/* END_CASE */
320
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200321/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100322void enc_dec_buf( int cipher_id, char * cipher_string, int key_len,
Paul Bakker33b43f12013-08-20 11:48:36 +0200323 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200324{
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200325 size_t length = length_val, outlen, total_len, i, block_size;
Jaeden Amerod906b812018-06-08 11:03:16 +0100326 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000327 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200328 unsigned char ad[13];
329 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200330 unsigned char inbuf[64];
331 unsigned char encbuf[64];
332 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 const mbedtls_cipher_info_t *cipher_info;
335 mbedtls_cipher_context_t ctx_dec;
336 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000337
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200338 /*
339 * Prepare contexts
340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 mbedtls_cipher_init( &ctx_dec );
342 mbedtls_cipher_init( &ctx_enc );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200343
344 memset( key, 0x2a, sizeof( key ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000345
346 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000348 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200350 TEST_ASSERT( strcmp( mbedtls_cipher_info_get_name( cipher_info ),
351 cipher_string ) == 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000352
353 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200354 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
355 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
358 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200361 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
364 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200365 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200366#else
367 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200369
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200370 /*
371 * Do a few encode/decode cycles
372 */
373 for( i = 0; i < 3; i++ )
374 {
375 memset( iv , 0x00 + i, sizeof( iv ) );
376 memset( ad, 0x10 + i, sizeof( ad ) );
377 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
378
379 memset( encbuf, 0, sizeof( encbuf ) );
380 memset( decbuf, 0, sizeof( decbuf ) );
381 memset( tag, 0, sizeof( tag ) );
382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
384 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
387 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200388
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200389#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
391 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200392#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000393
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200394 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
395 TEST_ASSERT( block_size != 0 );
396
Paul Bakker8123e9d2011-01-06 15:37:30 +0000397 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200399 total_len = outlen;
400
401 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200402 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200403 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200404 total_len + block_size > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200407 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000408
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200409#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200411#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200412
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200413 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200414 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200415 total_len > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200416 total_len <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000417
418 /* decode the previously encoded string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200420 total_len = outlen;
421
422 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200423 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200424 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200425 total_len + block_size >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200428 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000429
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200430#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200432#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200433
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200434 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200435 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000436 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200437 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000438
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200439 /*
440 * Done
441 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200442exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_cipher_free( &ctx_dec );
444 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200445}
Paul Bakker33b43f12013-08-20 11:48:36 +0200446/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000447
Paul Bakker33b43f12013-08-20 11:48:36 +0200448/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100449void enc_fail( int cipher_id, int pad_mode, int key_len, int length_val,
450 int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200451{
Paul Bakker33b43f12013-08-20 11:48:36 +0200452 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200453 unsigned char key[32];
454 unsigned char iv[16];
455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 const mbedtls_cipher_info_t *cipher_info;
457 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200458
459 unsigned char inbuf[64];
460 unsigned char encbuf[64];
461
462 size_t outlen = 0;
463
464 memset( key, 0, 32 );
465 memset( iv , 0, 16 );
466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200468
469 memset( inbuf, 5, 64 );
470 memset( encbuf, 0, 64 );
471
472 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200474 TEST_ASSERT( NULL != cipher_info );
475
476 /* Initialise context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200477 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
479#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
480 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200481#else
482 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
484 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
485 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200486#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200488#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200489
490 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
492 TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200493
494 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200495exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200497}
Paul Bakker33b43f12013-08-20 11:48:36 +0200498/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200499
Paul Bakker33b43f12013-08-20 11:48:36 +0200500/* BEGIN_CASE */
k-stachowiakd8727232019-07-29 17:46:29 +0200501void dec_empty_buf( int cipher,
502 int expected_update_ret,
503 int expected_finish_ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200504{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000505 unsigned char key[32];
506 unsigned char iv[16];
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_cipher_context_t ctx_dec;
509 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000510
511 unsigned char encbuf[64];
512 unsigned char decbuf[64];
513
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000514 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000515
516 memset( key, 0, 32 );
517 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_cipher_init( &ctx_dec );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200520
Paul Bakker8123e9d2011-01-06 15:37:30 +0000521 memset( encbuf, 0, 64 );
522 memset( decbuf, 0, 64 );
523
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200524 /* Initialise context */
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100525 cipher_info = mbedtls_cipher_info_from_type( cipher );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000526 TEST_ASSERT( NULL != cipher_info);
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100527 TEST_ASSERT( sizeof(key) * 8 >= cipher_info->key_bitlen );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200528
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200529 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000530
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100531 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec,
532 key, cipher_info->key_bitlen,
533 MBEDTLS_DECRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200538
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200539#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200541#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000542
543 /* decode 0-byte string */
k-stachowiakd8727232019-07-29 17:46:29 +0200544 TEST_ASSERT( expected_update_ret ==
545 mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000546 TEST_ASSERT( 0 == outlen );
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100547
k-stachowiakd8727232019-07-29 17:46:29 +0200548 if ( expected_finish_ret == 0 &&
549 ( cipher_info->mode == MBEDTLS_MODE_CBC ||
550 cipher_info->mode == MBEDTLS_MODE_ECB ) )
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100551 {
552 /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
553 * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
k-stachowiakd8727232019-07-29 17:46:29 +0200554 * decrypting an empty buffer.
555 * On the other hand, CBC and ECB ciphers need a full block of input.
556 */
557 expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100558 }
559
k-stachowiakd8727232019-07-29 17:46:29 +0200560 TEST_ASSERT( expected_finish_ret == mbedtls_cipher_finish(
561 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000562 TEST_ASSERT( 0 == outlen );
563
Paul Bakkerbd51b262014-07-10 15:26:12 +0200564exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_cipher_free( &ctx_dec );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200566}
Paul Bakker33b43f12013-08-20 11:48:36 +0200567/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000568
Paul Bakker33b43f12013-08-20 11:48:36 +0200569/* BEGIN_CASE */
570void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700571 int second_length_val, int pad_mode,
572 int first_encrypt_output_len, int second_encrypt_output_len,
573 int first_decrypt_output_len, int second_decrypt_output_len )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200574{
Paul Bakker33b43f12013-08-20 11:48:36 +0200575 size_t first_length = first_length_val;
576 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000577 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200578 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000579 unsigned char key[32];
580 unsigned char iv[16];
581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_cipher_context_t ctx_dec;
583 mbedtls_cipher_context_t ctx_enc;
584 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000585
586 unsigned char inbuf[64];
587 unsigned char encbuf[64];
588 unsigned char decbuf[64];
589
Paul Bakker23986e52011-04-24 08:57:21 +0000590 size_t outlen = 0;
591 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000592
593 memset( key, 0, 32 );
594 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_cipher_init( &ctx_dec );
597 mbedtls_cipher_init( &ctx_enc );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200598
Paul Bakker8123e9d2011-01-06 15:37:30 +0000599 memset( inbuf, 5, 64 );
600 memset( encbuf, 0, 64 );
601 memset( decbuf, 0, 64 );
602
603 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000605 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200606
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200607 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
608 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
611 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000612
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700613#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
614 if( -1 != pad_mode )
615 {
616 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
617 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
618 }
619#else
620 (void) pad_mode;
621#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
624 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
627 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200628
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200629#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
631 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200632#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000633
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200634 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
635 TEST_ASSERT( block_size != 0 );
636
Paul Bakker8123e9d2011-01-06 15:37:30 +0000637 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700639 TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000640 totaloutlen = outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700642 TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000643 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200644 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200645 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200646 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200647 totaloutlen + block_size > length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000650 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200651 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200652 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200653 totaloutlen > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200654 totaloutlen <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000655
656 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700657 second_length = totaloutlen - first_length;
658 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
659 TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200660 totaloutlen = outlen;
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700661 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
662 TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
663 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200664
665 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200666 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200667 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200668 totaloutlen + block_size >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200669
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700670 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200671 totaloutlen += outlen;
672
673 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000674
675 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
676
Paul Bakkerbd51b262014-07-10 15:26:12 +0200677exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 mbedtls_cipher_free( &ctx_dec );
679 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200680}
Paul Bakker33b43f12013-08-20 11:48:36 +0200681/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000682
Paul Bakker33b43f12013-08-20 11:48:36 +0200683/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100684void decrypt_test_vec( int cipher_id, int pad_mode, data_t * key,
685 data_t * iv, data_t * cipher,
686 data_t * clear, data_t * ad, data_t * tag,
Azim Khand30ca132017-06-09 04:32:58 +0100687 int finish_result, int tag_result )
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200688{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200689 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200691 size_t outlen, total_len;
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200694
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200695 memset( output, 0x00, sizeof( output ) );
696
Azim Khanf1aaec92017-05-30 14:23:15 +0100697#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100698 ((void) ad);
699 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200700#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200701
702 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200703 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_cipher_info_from_type( cipher_id ) ) );
Azim Khand30ca132017-06-09 04:32:58 +0100705 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200707 if( pad_mode != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200709#else
710 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Azim Khand30ca132017-06-09 04:32:58 +0100712 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv->x, iv->len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200714#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100715 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad->x, ad->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200716#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200717
Azim Khand30ca132017-06-09 04:32:58 +0100718 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200719 total_len = 0;
Azim Khand30ca132017-06-09 04:32:58 +0100720 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher->x, cipher->len, output, &outlen ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200721 total_len += outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200723 &outlen ) );
724 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200725#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100726 TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag->x, tag->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200727#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200728
729 /* check plaintext only if everything went fine */
730 if( 0 == finish_result && 0 == tag_result )
731 {
Azim Khand30ca132017-06-09 04:32:58 +0100732 TEST_ASSERT( total_len == clear->len );
733 TEST_ASSERT( 0 == memcmp( output, clear->x, clear->len ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200734 }
735
Paul Bakkerbd51b262014-07-10 15:26:12 +0200736exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200738}
739/* END_CASE */
740
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100741/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_AUTH_CRYPT */
Azim Khan5fcca462018-06-29 11:05:32 +0100742void auth_crypt_tv( int cipher_id, data_t * key, data_t * iv,
743 data_t * ad, data_t * cipher, data_t * tag,
Hanno Beckera13272d2018-11-12 16:27:30 +0000744 char * result, data_t * clear, int use_psa )
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200745{
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100746 /*
747 * Take an AEAD ciphertext + tag and perform a pair
748 * of AEAD decryption and AEAD encryption. Check that
Hanno Beckera13272d2018-11-12 16:27:30 +0000749 * this results in the expected plaintext, and that
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100750 * decryption and encryption are inverse to one another.
751 */
Hanno Beckera13272d2018-11-12 16:27:30 +0000752
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200753 int ret;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100754 int using_nist_kw, using_nist_kw_padding;
Hanno Beckera13272d2018-11-12 16:27:30 +0000755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200757 size_t outlen;
758
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100759 unsigned char *cipher_plus_tag = NULL;
760 size_t cipher_plus_tag_len;
761 unsigned char *decrypt_buf = NULL;
762 size_t decrypt_buf_len = 0;
763 unsigned char *encrypt_buf = NULL;
764 size_t encrypt_buf_len = 0;
765
Gilles Peskine70edd682020-12-03 20:27:27 +0100766 /* Null pointers are documented as valid for inputs of length 0.
767 * The test framework passes non-null pointers, so set them to NULL.
768 * key, cipher and tag can't be empty. */
769 if( iv->len == 0 )
770 iv->x = NULL;
771 if( ad->len == 0 )
772 ad->x = NULL;
773 if( clear->len == 0 )
774 clear->x = NULL;
775
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +0100776 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200777
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100778 /* Initialize PSA Crypto */
779#if defined(MBEDTLS_USE_PSA_CRYPTO)
780 if( use_psa == 1 )
781 PSA_ASSERT( psa_crypto_init( ) );
Hanno Beckera13272d2018-11-12 16:27:30 +0000782#else
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100783 (void) use_psa;
784#endif
785
786 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100787 * Are we using NIST_KW? with padding?
788 */
789 using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
790 cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
791 cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
792 using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
793 cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
794 cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
795 using_nist_kw_padding;
796
797 /*
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100798 * Prepare context for decryption
799 */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100800 if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
801 MBEDTLS_DECRYPT ) )
802 goto exit;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200803
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100804 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100805 * prepare buffer for decryption
806 * (we need the tag appended to the ciphertext)
807 */
808 cipher_plus_tag_len = cipher->len + tag->len;
809 ASSERT_ALLOC( cipher_plus_tag, cipher_plus_tag_len );
810 memcpy( cipher_plus_tag, cipher->x, cipher->len );
811 memcpy( cipher_plus_tag + cipher->len, tag->x, tag->len );
812
813 /*
814 * Compute length of output buffer according to the documentation
815 */
816 if( using_nist_kw )
817 decrypt_buf_len = cipher_plus_tag_len - 8;
818 else
819 decrypt_buf_len = cipher_plus_tag_len - tag->len;
820
821
822 /*
823 * Try decrypting to a buffer that's 1B too small
824 */
825 if( decrypt_buf_len != 0 )
826 {
827 ASSERT_ALLOC( decrypt_buf, decrypt_buf_len - 1 );
828
829 outlen = 0;
830 ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
831 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
832 decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len );
833 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
834
835 mbedtls_free( decrypt_buf );
836 decrypt_buf = NULL;
837 }
838
839 /*
840 * Authenticate and decrypt, and check result
841 */
842 ASSERT_ALLOC( decrypt_buf, decrypt_buf_len );
843
844 outlen = 0;
845 ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
846 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
847 decrypt_buf, decrypt_buf_len, &outlen, tag->len );
848
849 if( strcmp( result, "FAIL" ) == 0 )
850 {
851 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100852 TEST_ASSERT( buffer_is_all_zero( decrypt_buf, decrypt_buf_len ) );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100853 }
854 else
855 {
856 TEST_ASSERT( ret == 0 );
Gilles Peskinea2971ea2020-12-03 20:36:02 +0100857 ASSERT_COMPARE( decrypt_buf, outlen, clear->x, clear->len );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100858 }
859
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100860 mbedtls_free( decrypt_buf );
861 decrypt_buf = NULL;
862
863 /*
864 * Encrypt back if test data was authentic
865 */
866 if( strcmp( result, "FAIL" ) != 0 )
867 {
868 /* prepare context for encryption */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100869 if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
870 MBEDTLS_ENCRYPT ) )
871 goto exit;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100872
873 /*
874 * Compute size of output buffer according to documentation
875 */
876 if( using_nist_kw )
877 {
878 encrypt_buf_len = clear->len + 8;
879 if( using_nist_kw_padding && encrypt_buf_len % 8 != 0 )
880 encrypt_buf_len += 8 - encrypt_buf_len % 8;
881 }
882 else
883 {
884 encrypt_buf_len = clear->len + tag->len;
885 }
886
887 /*
888 * Try encrypting with an output buffer that's 1B too small
889 */
890 ASSERT_ALLOC( encrypt_buf, encrypt_buf_len - 1 );
891
892 outlen = 0;
893 ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
894 ad->x, ad->len, clear->x, clear->len,
895 encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len );
896 TEST_ASSERT( ret != 0 );
897
898 mbedtls_free( encrypt_buf );
899 encrypt_buf = NULL;
900
901 /*
902 * Encrypt and check the result
903 */
904 ASSERT_ALLOC( encrypt_buf, encrypt_buf_len );
905
906 outlen = 0;
907 ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
908 ad->x, ad->len, clear->x, clear->len,
909 encrypt_buf, encrypt_buf_len, &outlen, tag->len );
910 TEST_ASSERT( ret == 0 );
911
912 TEST_ASSERT( outlen == cipher->len + tag->len );
913 TEST_ASSERT( memcmp( encrypt_buf, cipher->x, cipher->len ) == 0 );
914 TEST_ASSERT( memcmp( encrypt_buf + cipher->len,
915 tag->x, tag->len ) == 0 );
916
917 mbedtls_free( encrypt_buf );
918 encrypt_buf = NULL;
919 }
920
Paul Bakkerbd51b262014-07-10 15:26:12 +0200921exit:
Hanno Beckera13272d2018-11-12 16:27:30 +0000922
Gilles Peskine5386f6b2019-08-01 12:47:40 +0200923 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100924 mbedtls_free( decrypt_buf );
925 mbedtls_free( encrypt_buf );
926 mbedtls_free( cipher_plus_tag );
Gilles Peskine5386f6b2019-08-01 12:47:40 +0200927
Hanno Beckera13272d2018-11-12 16:27:30 +0000928#if defined(MBEDTLS_USE_PSA_CRYPTO)
929 if( use_psa == 1 )
Gilles Peskine5386f6b2019-08-01 12:47:40 +0200930 PSA_DONE( );
Hanno Beckera13272d2018-11-12 16:27:30 +0000931#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200932}
933/* END_CASE */
934
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200935/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100936void test_vec_ecb( int cipher_id, int operation, data_t * key,
937 data_t * input, data_t * result, int finish_result
Azim Khand30ca132017-06-09 04:32:58 +0100938 )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200939{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200941 unsigned char output[32];
942 size_t outlen;
943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200945
Paul Bakker5e0efa72013-09-08 23:04:04 +0200946 memset( output, 0x00, sizeof( output ) );
947
948 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200949 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 mbedtls_cipher_info_from_type( cipher_id ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200951
Paul Bakker5e0efa72013-09-08 23:04:04 +0200952
Azim Khand30ca132017-06-09 04:32:58 +0100953 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200954
Azim Khand30ca132017-06-09 04:32:58 +0100955 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 mbedtls_cipher_get_block_size( &ctx ),
Paul Bakker5e0efa72013-09-08 23:04:04 +0200957 output, &outlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
959 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200960 &outlen ) );
961 TEST_ASSERT( 0 == outlen );
962
963 /* check plaintext only if everything went fine */
964 if( 0 == finish_result )
Azim Khand30ca132017-06-09 04:32:58 +0100965 TEST_ASSERT( 0 == memcmp( output, result->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_cipher_get_block_size( &ctx ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200967
Paul Bakkerbd51b262014-07-10 15:26:12 +0200968exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_cipher_free( &ctx );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200970}
971/* END_CASE */
972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Ronald Cron9ed40732020-06-25 09:03:34 +0200974void test_vec_crypt( int cipher_id, int operation, data_t *key,
975 data_t *iv, data_t *input, data_t *result,
Hanno Beckere43164e2018-11-12 12:46:35 +0000976 int finish_result, int use_psa )
Ron Eldor7b012442017-09-25 17:03:12 +0300977{
Ron Eldor7b012442017-09-25 17:03:12 +0300978 mbedtls_cipher_context_t ctx;
979 unsigned char output[32];
980 size_t outlen;
981
982 mbedtls_cipher_init( &ctx );
983
Ron Eldor7b012442017-09-25 17:03:12 +0300984 memset( output, 0x00, sizeof( output ) );
Ron Eldor7b012442017-09-25 17:03:12 +0300985
986 /* Prepare context */
Hanno Beckere43164e2018-11-12 12:46:35 +0000987#if !defined(MBEDTLS_USE_PSA_CRYPTO)
988 (void) use_psa;
989#else
990 if( use_psa == 1 )
991 {
Gilles Peskine5386f6b2019-08-01 12:47:40 +0200992 PSA_ASSERT( psa_crypto_init( ) );
Hanno Beckere43164e2018-11-12 12:46:35 +0000993 TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( &ctx,
Hanno Beckera13272d2018-11-12 16:27:30 +0000994 mbedtls_cipher_info_from_type( cipher_id ), 0 ) );
Hanno Beckere43164e2018-11-12 12:46:35 +0000995 }
996 else
997#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ron Eldor7b012442017-09-25 17:03:12 +0300998 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Hanno Beckera13272d2018-11-12 16:27:30 +0000999 mbedtls_cipher_info_from_type( cipher_id ) ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001000
Ronald Cron9ed40732020-06-25 09:03:34 +02001001 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001002 if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode )
1003 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) );
1004
Ronald Cron9ed40732020-06-25 09:03:34 +02001005 TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv->len ? iv->x : NULL,
1006 iv->len, input->x, input->len,
Ron Eldor7b012442017-09-25 17:03:12 +03001007 output, &outlen ) );
Ronald Cron9ed40732020-06-25 09:03:34 +02001008 TEST_ASSERT( result->len == outlen );
Ron Eldor7b012442017-09-25 17:03:12 +03001009 /* check plaintext only if everything went fine */
1010 if( 0 == finish_result )
Ronald Cron9ed40732020-06-25 09:03:34 +02001011 TEST_ASSERT( 0 == memcmp( output, result->x, outlen ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001012
1013exit:
1014 mbedtls_cipher_free( &ctx );
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001015#if defined(MBEDTLS_USE_PSA_CRYPTO)
1016 PSA_DONE( );
1017#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ron Eldor7b012442017-09-25 17:03:12 +03001018}
1019/* END_CASE */
1020
1021/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +02001022void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001023{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 const mbedtls_cipher_info_t *cipher_info;
1025 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001030 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001031 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001034
Paul Bakkerbd51b262014-07-10 15:26:12 +02001035exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001037}
Paul Bakker33b43f12013-08-20 11:48:36 +02001038/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +01001041void check_padding( int pad_mode, data_t * input, int ret, int dlen_check
Azim Khand30ca132017-06-09 04:32:58 +01001042 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001043{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 mbedtls_cipher_info_t cipher_info;
1045 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +01001046 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001047
1048 /* build a fake context just for getting access to get_padding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 mbedtls_cipher_init( &ctx );
1050 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001051 ctx.cipher_info = &cipher_info;
1052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001054
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001055
Azim Khand30ca132017-06-09 04:32:58 +01001056 TEST_ASSERT( ret == ctx.get_padding( input->x, input->len, &dlen ) );
Paul Bakker33b43f12013-08-20 11:48:36 +02001057 if( 0 == ret )
1058 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001059}
Paul Bakker33b43f12013-08-20 11:48:36 +02001060/* END_CASE */