blob: f1095b1f038e59ab9b2b73b0c66f41f6635f93ac [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
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010016#if defined(MBEDTLS_CIPHER_AUTH_CRYPT)
17/* Helper for resetting key/direction
18 *
19 * The documentation doesn't explicitly say whether calling
20 * mbedtls_cipher_setkey() twice is allowed or not. This currently works with
21 * the default software implementation, but only by accident. It isn't
22 * guaranteed to work with new ciphers or with alternative implementations of
23 * individual ciphers, and it doesn't work with the PSA wrappers. So don't do
24 * it, and instead start with a fresh context.
25 */
Gilles Peskine8a3d2342020-12-03 21:06:15 +010026static int cipher_reset_key( mbedtls_cipher_context_t *ctx, int cipher_id,
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010027 int use_psa, size_t tag_len, const data_t *key, int direction )
28{
29 mbedtls_cipher_free( ctx );
30 mbedtls_cipher_init( ctx );
31
32#if !defined(MBEDTLS_USE_PSA_CRYPTO)
33 (void) use_psa;
34 (void) tag_len;
35#else
36 if( use_psa == 1 )
37 {
38 TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( ctx,
39 mbedtls_cipher_info_from_type( cipher_id ),
40 tag_len ) );
41 }
42 else
43#endif /* MBEDTLS_USE_PSA_CRYPTO */
44 {
45 TEST_ASSERT( 0 == mbedtls_cipher_setup( ctx,
46 mbedtls_cipher_info_from_type( cipher_id ) ) );
47 }
48
49 TEST_ASSERT( 0 == mbedtls_cipher_setkey( ctx, key->x, 8 * key->len,
50 direction ) );
Gilles Peskine8a3d2342020-12-03 21:06:15 +010051 return( 1 );
52
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010053exit:
Gilles Peskine8a3d2342020-12-03 21:06:15 +010054 return( 0 );
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010055}
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +010056
57/*
58 * Check if a buffer is all-0 bytes:
59 * return 1 if it is,
60 * 0 if it isn't.
61 */
62int buffer_is_all_zero( const uint8_t *buf, size_t size )
63{
64 for( size_t i = 0; i < size; i++ )
65 if( buf[i] != 0 )
66 return 0;
67 return 1;
68}
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010069#endif /* MBEDTLS_CIPHER_AUTH_CRYPT */
70
Paul Bakker33b43f12013-08-20 11:48:36 +020071/* END_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +000072
Paul Bakker33b43f12013-08-20 11:48:36 +020073/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 * depends_on:MBEDTLS_CIPHER_C
Paul Bakker33b43f12013-08-20 11:48:36 +020075 * END_DEPENDENCIES
76 */
Paul Bakker5690efc2011-05-26 13:16:06 +000077
Paul Bakker33b43f12013-08-20 11:48:36 +020078/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010079void mbedtls_cipher_list( )
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +010080{
81 const int *cipher_type;
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 for( cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++ )
84 TEST_ASSERT( mbedtls_cipher_info_from_type( *cipher_type ) != NULL );
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +010085}
86/* END_CASE */
87
88/* BEGIN_CASE */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050089void cipher_invalid_param_unconditional( )
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020090{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050091 mbedtls_cipher_context_t valid_ctx;
92 mbedtls_cipher_context_t invalid_ctx;
93 mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
94 mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
95 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
96 int valid_size = sizeof(valid_buffer);
97 int valid_bitlen = valid_size * 8;
98 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
99 *( mbedtls_cipher_list() ) );
100 size_t size_t_var;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200101
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500102 (void)valid_mode; /* In some configurations this is unused */
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200103
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500104 mbedtls_cipher_init( &valid_ctx );
105 mbedtls_cipher_setup( &valid_ctx, valid_info );
106 mbedtls_cipher_init( &invalid_ctx );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200107
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500108 /* mbedtls_cipher_setup() */
109 TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, NULL ) ==
110 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200111
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500112 /* mbedtls_cipher_get_block_size() */
113 TEST_ASSERT( mbedtls_cipher_get_block_size( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200114
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500115 /* mbedtls_cipher_get_cipher_mode() */
116 TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &invalid_ctx ) ==
117 MBEDTLS_MODE_NONE );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200118
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500119 /* mbedtls_cipher_get_iv_size() */
120 TEST_ASSERT( mbedtls_cipher_get_iv_size( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200121
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500122 /* mbedtls_cipher_get_type() */
123 TEST_ASSERT(
124 mbedtls_cipher_get_type( &invalid_ctx ) ==
125 MBEDTLS_CIPHER_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200126
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500127 /* mbedtls_cipher_get_name() */
128 TEST_ASSERT( mbedtls_cipher_get_name( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200129
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500130 /* mbedtls_cipher_get_key_bitlen() */
131 TEST_ASSERT( mbedtls_cipher_get_key_bitlen( &invalid_ctx ) ==
132 MBEDTLS_KEY_LENGTH_NONE );
133
134 /* mbedtls_cipher_get_operation() */
135 TEST_ASSERT( mbedtls_cipher_get_operation( &invalid_ctx ) ==
136 MBEDTLS_OPERATION_NONE );
137
138 /* mbedtls_cipher_setkey() */
139 TEST_ASSERT(
140 mbedtls_cipher_setkey( &invalid_ctx,
141 valid_buffer,
142 valid_bitlen,
143 valid_operation ) ==
144 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
145
146 /* mbedtls_cipher_set_iv() */
147 TEST_ASSERT(
148 mbedtls_cipher_set_iv( &invalid_ctx,
149 valid_buffer,
150 valid_size ) ==
151 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
152
153 /* mbedtls_cipher_reset() */
154 TEST_ASSERT( mbedtls_cipher_reset( &invalid_ctx ) ==
155 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200156
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200157#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500158 /* mbedtls_cipher_update_ad() */
159 TEST_ASSERT(
160 mbedtls_cipher_update_ad( &invalid_ctx,
161 valid_buffer,
162 valid_size ) ==
163 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
164#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
165
166#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
167 /* mbedtls_cipher_set_padding_mode() */
168 TEST_ASSERT( mbedtls_cipher_set_padding_mode( &invalid_ctx, valid_mode ) ==
169 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200170#endif
171
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500172 /* mbedtls_cipher_update() */
173 TEST_ASSERT(
174 mbedtls_cipher_update( &invalid_ctx,
175 valid_buffer,
176 valid_size,
177 valid_buffer,
178 &size_t_var ) ==
179 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200180
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500181 /* mbedtls_cipher_finish() */
182 TEST_ASSERT(
183 mbedtls_cipher_finish( &invalid_ctx,
184 valid_buffer,
185 &size_t_var ) ==
186 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200187
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200188#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500189 /* mbedtls_cipher_write_tag() */
190 TEST_ASSERT(
191 mbedtls_cipher_write_tag( &invalid_ctx,
192 valid_buffer,
193 valid_size ) ==
194 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200195
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500196 /* mbedtls_cipher_check_tag() */
197 TEST_ASSERT(
198 mbedtls_cipher_check_tag( &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
204exit:
205 mbedtls_cipher_free( &invalid_ctx );
206 mbedtls_cipher_free( &valid_ctx );
207}
208/* END_CASE */
209
TRodziewicz062f3532021-05-25 15:15:57 +0200210/* BEGIN_CASE depends_on:NOT_DEFINED */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500211void cipher_invalid_param_conditional( )
212{
213 mbedtls_cipher_context_t valid_ctx;
214
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500215 mbedtls_operation_t invalid_operation = 100;
216 mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
217 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
218 int valid_size = sizeof(valid_buffer);
219 int valid_bitlen = valid_size * 8;
220 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
221 *( mbedtls_cipher_list() ) );
222
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500223 (void)valid_mode; /* In some configurations this is unused */
224
225 /* mbedtls_cipher_init() */
226 TEST_VALID_PARAM( mbedtls_cipher_init( &valid_ctx ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500227
228 /* mbedtls_cipher_setup() */
229 TEST_VALID_PARAM( mbedtls_cipher_setup( &valid_ctx, valid_info ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500230
Ronald Cron875b5fb2021-05-21 08:50:00 +0200231 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500232 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
233 mbedtls_cipher_setkey( &valid_ctx,
234 valid_buffer,
235 valid_bitlen,
236 invalid_operation ) );
237
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500238exit:
239 TEST_VALID_PARAM( mbedtls_cipher_free( &valid_ctx ) );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200240}
241/* END_CASE */
242
Paul Bakker6a9c7252016-07-14 13:46:10 +0100243/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100244void cipher_special_behaviours( )
Paul Bakker6a9c7252016-07-14 13:46:10 +0100245{
246 const mbedtls_cipher_info_t *cipher_info;
247 mbedtls_cipher_context_t ctx;
248 unsigned char input[32];
249 unsigned char output[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300250#if defined (MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100251 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300252#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100253 size_t olen = 0;
254
255 mbedtls_cipher_init( &ctx );
256 memset( input, 0, sizeof( input ) );
257 memset( output, 0, sizeof( output ) );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300258#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100259 memset( iv, 0, sizeof( iv ) );
260
261 /* Check and get info structures */
Ron Eldor7b012442017-09-25 17:03:12 +0300262 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
Paul Bakker6a9c7252016-07-14 13:46:10 +0100263 TEST_ASSERT( NULL != cipher_info );
264
265 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
266
267 /* IV too big */
268 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
269 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
270
271 /* IV too small */
272 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
273 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
274
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300275 mbedtls_cipher_free( &ctx );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300276 mbedtls_cipher_init( &ctx );
Ron Eldor6f90ed82017-09-26 12:08:54 +0300277#endif /* MBEDTLS_CIPHER_MODE_CBC */
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300278 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
Ron Eldor7b012442017-09-25 17:03:12 +0300279 TEST_ASSERT( NULL != cipher_info );
280
281 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
282
Paul Bakker6a9c7252016-07-14 13:46:10 +0100283 /* Update ECB with partial block */
284 TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
285 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
286
287exit:
288 mbedtls_cipher_free( &ctx );
289}
290/* END_CASE */
291
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200292/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100293void enc_dec_buf( int cipher_id, char * cipher_string, int key_len,
Paul Bakker33b43f12013-08-20 11:48:36 +0200294 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200295{
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200296 size_t length = length_val, outlen, total_len, i, block_size;
Jaeden Amerod906b812018-06-08 11:03:16 +0100297 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000298 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200299 unsigned char ad[13];
300 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200301 unsigned char inbuf[64];
302 unsigned char encbuf[64];
303 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 const mbedtls_cipher_info_t *cipher_info;
306 mbedtls_cipher_context_t ctx_dec;
307 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000308
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200309 /*
310 * Prepare contexts
311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_cipher_init( &ctx_dec );
313 mbedtls_cipher_init( &ctx_enc );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200314
315 memset( key, 0x2a, sizeof( key ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000316
317 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000319 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000321
322 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200323 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
324 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
327 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200330 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
333 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200334 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200335#else
336 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200338
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200339 /*
340 * Do a few encode/decode cycles
341 */
342 for( i = 0; i < 3; i++ )
343 {
344 memset( iv , 0x00 + i, sizeof( iv ) );
345 memset( ad, 0x10 + i, sizeof( ad ) );
346 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
347
348 memset( encbuf, 0, sizeof( encbuf ) );
349 memset( decbuf, 0, sizeof( decbuf ) );
350 memset( tag, 0, sizeof( tag ) );
351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
353 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
356 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200357
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200358#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
360 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200361#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000362
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200363 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
364 TEST_ASSERT( block_size != 0 );
365
Paul Bakker8123e9d2011-01-06 15:37:30 +0000366 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200368 total_len = outlen;
369
370 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200371 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200372 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200373 total_len + block_size > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200376 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000377
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200378#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200380#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200381
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200382 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200383 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200384 total_len > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200385 total_len <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000386
387 /* decode the previously encoded string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200389 total_len = outlen;
390
391 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200392 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200393 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200394 total_len + block_size >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200397 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000398
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200399#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200401#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200402
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200403 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200404 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000405 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200406 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000407
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200408 /*
409 * Done
410 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200411exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_cipher_free( &ctx_dec );
413 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200414}
Paul Bakker33b43f12013-08-20 11:48:36 +0200415/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000416
Paul Bakker33b43f12013-08-20 11:48:36 +0200417/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100418void enc_fail( int cipher_id, int pad_mode, int key_len, int length_val,
419 int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200420{
Paul Bakker33b43f12013-08-20 11:48:36 +0200421 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200422 unsigned char key[32];
423 unsigned char iv[16];
424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 const mbedtls_cipher_info_t *cipher_info;
426 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200427
428 unsigned char inbuf[64];
429 unsigned char encbuf[64];
430
431 size_t outlen = 0;
432
433 memset( key, 0, 32 );
434 memset( iv , 0, 16 );
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200437
438 memset( inbuf, 5, 64 );
439 memset( encbuf, 0, 64 );
440
441 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200443 TEST_ASSERT( NULL != cipher_info );
444
445 /* Initialise context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200446 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
448#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
449 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200450#else
451 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
453 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
454 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200455#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200457#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200458
459 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
461 TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200462
463 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200464exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200466}
Paul Bakker33b43f12013-08-20 11:48:36 +0200467/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200468
Paul Bakker33b43f12013-08-20 11:48:36 +0200469/* BEGIN_CASE */
k-stachowiakd8727232019-07-29 17:46:29 +0200470void dec_empty_buf( int cipher,
471 int expected_update_ret,
472 int expected_finish_ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200473{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000474 unsigned char key[32];
475 unsigned char iv[16];
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 mbedtls_cipher_context_t ctx_dec;
478 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000479
480 unsigned char encbuf[64];
481 unsigned char decbuf[64];
482
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000483 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000484
485 memset( key, 0, 32 );
486 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_cipher_init( &ctx_dec );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200489
Paul Bakker8123e9d2011-01-06 15:37:30 +0000490 memset( encbuf, 0, 64 );
491 memset( decbuf, 0, 64 );
492
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200493 /* Initialise context */
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100494 cipher_info = mbedtls_cipher_info_from_type( cipher );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000495 TEST_ASSERT( NULL != cipher_info);
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100496 TEST_ASSERT( sizeof(key) * 8 >= cipher_info->key_bitlen );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200497
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200498 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000499
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100500 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec,
501 key, cipher_info->key_bitlen,
502 MBEDTLS_DECRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200507
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200508#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200510#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000511
512 /* decode 0-byte string */
k-stachowiakd8727232019-07-29 17:46:29 +0200513 TEST_ASSERT( expected_update_ret ==
514 mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000515 TEST_ASSERT( 0 == outlen );
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100516
k-stachowiakd8727232019-07-29 17:46:29 +0200517 if ( expected_finish_ret == 0 &&
518 ( cipher_info->mode == MBEDTLS_MODE_CBC ||
519 cipher_info->mode == MBEDTLS_MODE_ECB ) )
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100520 {
521 /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
522 * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
k-stachowiakd8727232019-07-29 17:46:29 +0200523 * decrypting an empty buffer.
524 * On the other hand, CBC and ECB ciphers need a full block of input.
525 */
526 expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100527 }
528
k-stachowiakd8727232019-07-29 17:46:29 +0200529 TEST_ASSERT( expected_finish_ret == mbedtls_cipher_finish(
530 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000531 TEST_ASSERT( 0 == outlen );
532
Paul Bakkerbd51b262014-07-10 15:26:12 +0200533exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 mbedtls_cipher_free( &ctx_dec );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200535}
Paul Bakker33b43f12013-08-20 11:48:36 +0200536/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000537
Paul Bakker33b43f12013-08-20 11:48:36 +0200538/* BEGIN_CASE */
539void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700540 int second_length_val, int pad_mode,
541 int first_encrypt_output_len, int second_encrypt_output_len,
542 int first_decrypt_output_len, int second_decrypt_output_len )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200543{
Paul Bakker33b43f12013-08-20 11:48:36 +0200544 size_t first_length = first_length_val;
545 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000546 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200547 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000548 unsigned char key[32];
549 unsigned char iv[16];
550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 mbedtls_cipher_context_t ctx_dec;
552 mbedtls_cipher_context_t ctx_enc;
553 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000554
555 unsigned char inbuf[64];
556 unsigned char encbuf[64];
557 unsigned char decbuf[64];
558
Paul Bakker23986e52011-04-24 08:57:21 +0000559 size_t outlen = 0;
560 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000561
562 memset( key, 0, 32 );
563 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_cipher_init( &ctx_dec );
566 mbedtls_cipher_init( &ctx_enc );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200567
Paul Bakker8123e9d2011-01-06 15:37:30 +0000568 memset( inbuf, 5, 64 );
569 memset( encbuf, 0, 64 );
570 memset( decbuf, 0, 64 );
571
572 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000574 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200575
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200576 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
577 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
580 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000581
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700582#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
583 if( -1 != pad_mode )
584 {
585 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
586 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
587 }
588#else
589 (void) pad_mode;
590#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
593 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
596 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200597
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200598#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
600 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200601#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000602
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200603 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
604 TEST_ASSERT( block_size != 0 );
605
Paul Bakker8123e9d2011-01-06 15:37:30 +0000606 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700608 TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000609 totaloutlen = outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700611 TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000612 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200613 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200614 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200615 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200616 totaloutlen + block_size > length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000619 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200620 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200621 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200622 totaloutlen > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200623 totaloutlen <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000624
625 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700626 second_length = totaloutlen - first_length;
627 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
628 TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200629 totaloutlen = outlen;
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700630 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
631 TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
632 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200633
634 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200635 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200636 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200637 totaloutlen + block_size >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200638
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700639 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200640 totaloutlen += outlen;
641
642 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000643
644 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
645
Paul Bakkerbd51b262014-07-10 15:26:12 +0200646exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_cipher_free( &ctx_dec );
648 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200649}
Paul Bakker33b43f12013-08-20 11:48:36 +0200650/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000651
Paul Bakker33b43f12013-08-20 11:48:36 +0200652/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100653void decrypt_test_vec( int cipher_id, int pad_mode, data_t * key,
654 data_t * iv, data_t * cipher,
655 data_t * clear, data_t * ad, data_t * tag,
Azim Khand30ca132017-06-09 04:32:58 +0100656 int finish_result, int tag_result )
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200657{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200658 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200660 size_t outlen, total_len;
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200663
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200664 memset( output, 0x00, sizeof( output ) );
665
Azim Khanf1aaec92017-05-30 14:23:15 +0100666#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100667 ((void) ad);
668 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200669#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200670
671 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200672 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_cipher_info_from_type( cipher_id ) ) );
Azim Khand30ca132017-06-09 04:32:58 +0100674 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200676 if( pad_mode != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200678#else
679 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Azim Khand30ca132017-06-09 04:32:58 +0100681 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv->x, iv->len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200683#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100684 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad->x, ad->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200685#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200686
Azim Khand30ca132017-06-09 04:32:58 +0100687 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200688 total_len = 0;
Azim Khand30ca132017-06-09 04:32:58 +0100689 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher->x, cipher->len, output, &outlen ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200690 total_len += outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200692 &outlen ) );
693 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200694#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100695 TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag->x, tag->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200696#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200697
698 /* check plaintext only if everything went fine */
699 if( 0 == finish_result && 0 == tag_result )
700 {
Azim Khand30ca132017-06-09 04:32:58 +0100701 TEST_ASSERT( total_len == clear->len );
702 TEST_ASSERT( 0 == memcmp( output, clear->x, clear->len ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200703 }
704
Paul Bakkerbd51b262014-07-10 15:26:12 +0200705exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200707}
708/* END_CASE */
709
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100710/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_AUTH_CRYPT */
Azim Khan5fcca462018-06-29 11:05:32 +0100711void auth_crypt_tv( int cipher_id, data_t * key, data_t * iv,
712 data_t * ad, data_t * cipher, data_t * tag,
Hanno Beckera13272d2018-11-12 16:27:30 +0000713 char * result, data_t * clear, int use_psa )
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200714{
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100715 /*
716 * Take an AEAD ciphertext + tag and perform a pair
717 * of AEAD decryption and AEAD encryption. Check that
Hanno Beckera13272d2018-11-12 16:27:30 +0000718 * this results in the expected plaintext, and that
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100719 * decryption and encryption are inverse to one another.
720 */
Hanno Beckera13272d2018-11-12 16:27:30 +0000721
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200722 int ret;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100723 int using_nist_kw, using_nist_kw_padding;
Hanno Beckera13272d2018-11-12 16:27:30 +0000724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200726 size_t outlen;
727
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100728 unsigned char *cipher_plus_tag = NULL;
729 size_t cipher_plus_tag_len;
730 unsigned char *decrypt_buf = NULL;
731 size_t decrypt_buf_len = 0;
732 unsigned char *encrypt_buf = NULL;
733 size_t encrypt_buf_len = 0;
734
Gilles Peskine70edd682020-12-03 20:27:27 +0100735 /* Null pointers are documented as valid for inputs of length 0.
736 * The test framework passes non-null pointers, so set them to NULL.
737 * key, cipher and tag can't be empty. */
738 if( iv->len == 0 )
739 iv->x = NULL;
740 if( ad->len == 0 )
741 ad->x = NULL;
742 if( clear->len == 0 )
743 clear->x = NULL;
744
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +0100745 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200746
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100747 /* Initialize PSA Crypto */
748#if defined(MBEDTLS_USE_PSA_CRYPTO)
749 if( use_psa == 1 )
750 PSA_ASSERT( psa_crypto_init( ) );
Hanno Beckera13272d2018-11-12 16:27:30 +0000751#else
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100752 (void) use_psa;
753#endif
754
755 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100756 * Are we using NIST_KW? with padding?
757 */
758 using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
759 cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
760 cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
761 using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
762 cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
763 cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
764 using_nist_kw_padding;
765
766 /*
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100767 * Prepare context for decryption
768 */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100769 if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
770 MBEDTLS_DECRYPT ) )
771 goto exit;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200772
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100773 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100774 * prepare buffer for decryption
775 * (we need the tag appended to the ciphertext)
776 */
777 cipher_plus_tag_len = cipher->len + tag->len;
778 ASSERT_ALLOC( cipher_plus_tag, cipher_plus_tag_len );
779 memcpy( cipher_plus_tag, cipher->x, cipher->len );
780 memcpy( cipher_plus_tag + cipher->len, tag->x, tag->len );
781
782 /*
783 * Compute length of output buffer according to the documentation
784 */
785 if( using_nist_kw )
786 decrypt_buf_len = cipher_plus_tag_len - 8;
787 else
788 decrypt_buf_len = cipher_plus_tag_len - tag->len;
789
790
791 /*
792 * Try decrypting to a buffer that's 1B too small
793 */
794 if( decrypt_buf_len != 0 )
795 {
796 ASSERT_ALLOC( decrypt_buf, decrypt_buf_len - 1 );
797
798 outlen = 0;
799 ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
800 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
801 decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len );
802 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
803
804 mbedtls_free( decrypt_buf );
805 decrypt_buf = NULL;
806 }
807
808 /*
809 * Authenticate and decrypt, and check result
810 */
811 ASSERT_ALLOC( decrypt_buf, decrypt_buf_len );
812
813 outlen = 0;
814 ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
815 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
816 decrypt_buf, decrypt_buf_len, &outlen, tag->len );
817
818 if( strcmp( result, "FAIL" ) == 0 )
819 {
820 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100821 TEST_ASSERT( buffer_is_all_zero( decrypt_buf, decrypt_buf_len ) );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100822 }
823 else
824 {
825 TEST_ASSERT( ret == 0 );
Gilles Peskinea2971ea2020-12-03 20:36:02 +0100826 ASSERT_COMPARE( decrypt_buf, outlen, clear->x, clear->len );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100827 }
828
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100829 mbedtls_free( decrypt_buf );
830 decrypt_buf = NULL;
831
832 /*
833 * Encrypt back if test data was authentic
834 */
835 if( strcmp( result, "FAIL" ) != 0 )
836 {
837 /* prepare context for encryption */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100838 if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
839 MBEDTLS_ENCRYPT ) )
840 goto exit;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100841
842 /*
843 * Compute size of output buffer according to documentation
844 */
845 if( using_nist_kw )
846 {
847 encrypt_buf_len = clear->len + 8;
848 if( using_nist_kw_padding && encrypt_buf_len % 8 != 0 )
849 encrypt_buf_len += 8 - encrypt_buf_len % 8;
850 }
851 else
852 {
853 encrypt_buf_len = clear->len + tag->len;
854 }
855
856 /*
857 * Try encrypting with an output buffer that's 1B too small
858 */
859 ASSERT_ALLOC( encrypt_buf, encrypt_buf_len - 1 );
860
861 outlen = 0;
862 ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
863 ad->x, ad->len, clear->x, clear->len,
864 encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len );
865 TEST_ASSERT( ret != 0 );
866
867 mbedtls_free( encrypt_buf );
868 encrypt_buf = NULL;
869
870 /*
871 * Encrypt and check the result
872 */
873 ASSERT_ALLOC( encrypt_buf, encrypt_buf_len );
874
875 outlen = 0;
876 ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
877 ad->x, ad->len, clear->x, clear->len,
878 encrypt_buf, encrypt_buf_len, &outlen, tag->len );
879 TEST_ASSERT( ret == 0 );
880
881 TEST_ASSERT( outlen == cipher->len + tag->len );
882 TEST_ASSERT( memcmp( encrypt_buf, cipher->x, cipher->len ) == 0 );
883 TEST_ASSERT( memcmp( encrypt_buf + cipher->len,
884 tag->x, tag->len ) == 0 );
885
886 mbedtls_free( encrypt_buf );
887 encrypt_buf = NULL;
888 }
889
Paul Bakkerbd51b262014-07-10 15:26:12 +0200890exit:
Hanno Beckera13272d2018-11-12 16:27:30 +0000891
Gilles Peskine5386f6b2019-08-01 12:47:40 +0200892 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100893 mbedtls_free( decrypt_buf );
894 mbedtls_free( encrypt_buf );
895 mbedtls_free( cipher_plus_tag );
Gilles Peskine5386f6b2019-08-01 12:47:40 +0200896
Hanno Beckera13272d2018-11-12 16:27:30 +0000897#if defined(MBEDTLS_USE_PSA_CRYPTO)
898 if( use_psa == 1 )
Gilles Peskine5386f6b2019-08-01 12:47:40 +0200899 PSA_DONE( );
Hanno Beckera13272d2018-11-12 16:27:30 +0000900#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200901}
902/* END_CASE */
903
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200904/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100905void test_vec_ecb( int cipher_id, int operation, data_t * key,
906 data_t * input, data_t * result, int finish_result
Azim Khand30ca132017-06-09 04:32:58 +0100907 )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200908{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200910 unsigned char output[32];
911 size_t outlen;
912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200914
Paul Bakker5e0efa72013-09-08 23:04:04 +0200915 memset( output, 0x00, sizeof( output ) );
916
917 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200918 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 mbedtls_cipher_info_from_type( cipher_id ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200920
Paul Bakker5e0efa72013-09-08 23:04:04 +0200921
Azim Khand30ca132017-06-09 04:32:58 +0100922 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200923
Azim Khand30ca132017-06-09 04:32:58 +0100924 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_cipher_get_block_size( &ctx ),
Paul Bakker5e0efa72013-09-08 23:04:04 +0200926 output, &outlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
928 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200929 &outlen ) );
930 TEST_ASSERT( 0 == outlen );
931
932 /* check plaintext only if everything went fine */
933 if( 0 == finish_result )
Azim Khand30ca132017-06-09 04:32:58 +0100934 TEST_ASSERT( 0 == memcmp( output, result->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 mbedtls_cipher_get_block_size( &ctx ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200936
Paul Bakkerbd51b262014-07-10 15:26:12 +0200937exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200938 mbedtls_cipher_free( &ctx );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200939}
940/* END_CASE */
941
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Ronald Cron9ed40732020-06-25 09:03:34 +0200943void test_vec_crypt( int cipher_id, int operation, data_t *key,
944 data_t *iv, data_t *input, data_t *result,
Hanno Beckere43164e2018-11-12 12:46:35 +0000945 int finish_result, int use_psa )
Ron Eldor7b012442017-09-25 17:03:12 +0300946{
Ron Eldor7b012442017-09-25 17:03:12 +0300947 mbedtls_cipher_context_t ctx;
948 unsigned char output[32];
949 size_t outlen;
950
951 mbedtls_cipher_init( &ctx );
952
Ron Eldor7b012442017-09-25 17:03:12 +0300953 memset( output, 0x00, sizeof( output ) );
Ron Eldor7b012442017-09-25 17:03:12 +0300954
955 /* Prepare context */
Hanno Beckere43164e2018-11-12 12:46:35 +0000956#if !defined(MBEDTLS_USE_PSA_CRYPTO)
957 (void) use_psa;
958#else
959 if( use_psa == 1 )
960 {
Gilles Peskine5386f6b2019-08-01 12:47:40 +0200961 PSA_ASSERT( psa_crypto_init( ) );
Hanno Beckere43164e2018-11-12 12:46:35 +0000962 TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( &ctx,
Hanno Beckera13272d2018-11-12 16:27:30 +0000963 mbedtls_cipher_info_from_type( cipher_id ), 0 ) );
Hanno Beckere43164e2018-11-12 12:46:35 +0000964 }
965 else
966#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ron Eldor7b012442017-09-25 17:03:12 +0300967 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Hanno Beckera13272d2018-11-12 16:27:30 +0000968 mbedtls_cipher_info_from_type( cipher_id ) ) );
Ron Eldor7b012442017-09-25 17:03:12 +0300969
Ronald Cron9ed40732020-06-25 09:03:34 +0200970 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Ron Eldor7b012442017-09-25 17:03:12 +0300971 if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode )
972 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) );
973
Ronald Cron9ed40732020-06-25 09:03:34 +0200974 TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv->len ? iv->x : NULL,
975 iv->len, input->x, input->len,
Ron Eldor7b012442017-09-25 17:03:12 +0300976 output, &outlen ) );
Ronald Cron9ed40732020-06-25 09:03:34 +0200977 TEST_ASSERT( result->len == outlen );
Ron Eldor7b012442017-09-25 17:03:12 +0300978 /* check plaintext only if everything went fine */
979 if( 0 == finish_result )
Ronald Cron9ed40732020-06-25 09:03:34 +0200980 TEST_ASSERT( 0 == memcmp( output, result->x, outlen ) );
Ron Eldor7b012442017-09-25 17:03:12 +0300981
982exit:
983 mbedtls_cipher_free( &ctx );
Gilles Peskine5386f6b2019-08-01 12:47:40 +0200984#if defined(MBEDTLS_USE_PSA_CRYPTO)
985 PSA_DONE( );
986#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ron Eldor7b012442017-09-25 17:03:12 +0300987}
988/* END_CASE */
989
990/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +0200991void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200992{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 const mbedtls_cipher_info_t *cipher_info;
994 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200999 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001000 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001003
Paul Bakkerbd51b262014-07-10 15:26:12 +02001004exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001006}
Paul Bakker33b43f12013-08-20 11:48:36 +02001007/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +01001010void check_padding( int pad_mode, data_t * input, int ret, int dlen_check
Azim Khand30ca132017-06-09 04:32:58 +01001011 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001012{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 mbedtls_cipher_info_t cipher_info;
1014 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +01001015 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001016
1017 /* build a fake context just for getting access to get_padding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 mbedtls_cipher_init( &ctx );
1019 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001020 ctx.cipher_info = &cipher_info;
1021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001023
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001024
Azim Khand30ca132017-06-09 04:32:58 +01001025 TEST_ASSERT( ret == ctx.get_padding( input->x, input->len, &dlen ) );
Paul Bakker33b43f12013-08-20 11:48:36 +02001026 if( 0 == ret )
1027 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001028}
Paul Bakker33b43f12013-08-20 11:48:36 +02001029/* END_CASE */