blob: a399c7052d465e1581e6d184af80f342a29481f8 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00005#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +02006#endif
Paul Bakker33b43f12013-08-20 11:48:36 +02007/* END_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +00008
Paul Bakker33b43f12013-08-20 11:48:36 +02009/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010 * depends_on:MBEDTLS_CIPHER_C
Paul Bakker33b43f12013-08-20 11:48:36 +020011 * END_DEPENDENCIES
12 */
Paul Bakker5690efc2011-05-26 13:16:06 +000013
Paul Bakker33b43f12013-08-20 11:48:36 +020014/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010015void mbedtls_cipher_list( )
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +010016{
17 const int *cipher_type;
18
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019 for( cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++ )
20 TEST_ASSERT( mbedtls_cipher_info_from_type( *cipher_type ) != NULL );
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +010021}
22/* END_CASE */
23
k-stachowiaka85edd92018-12-19 18:06:35 +010024/* BEGIN_CASE */
25void cipher_invalid_param_unconditional( )
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +010026{
k-stachowiaka85edd92018-12-19 18:06:35 +010027 mbedtls_cipher_context_t valid_ctx;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +010028 mbedtls_cipher_context_t invalid_ctx;
k-stachowiaka85edd92018-12-19 18:06:35 +010029 mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
30 mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
31 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
32 int valid_size = sizeof(valid_buffer);
33 int valid_bitlen = valid_size * 8;
34 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
35 *( mbedtls_cipher_list() ) );
36 size_t size_t_var;
37
k-stachowiakfb543602018-12-19 18:34:21 +010038 (void)valid_mode; /* In some configurations this is unused */
39
k-stachowiaka85edd92018-12-19 18:06:35 +010040 mbedtls_cipher_init( &valid_ctx );
41 mbedtls_cipher_setup( &valid_ctx, valid_info );
42 mbedtls_cipher_init( &invalid_ctx );
43
44 /* mbedtls_cipher_setup() */
45 TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, NULL ) ==
46 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
47
48 /* mbedtls_cipher_get_block_size() */
49 TEST_ASSERT( mbedtls_cipher_get_block_size( &invalid_ctx ) == 0 );
50
51 /* mbedtls_cipher_get_cipher_mode() */
52 TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &invalid_ctx ) ==
53 MBEDTLS_MODE_NONE );
54
55 /* mbedtls_cipher_get_iv_size() */
56 TEST_ASSERT( mbedtls_cipher_get_iv_size( &invalid_ctx ) == 0 );
57
58 /* mbedtls_cipher_get_type() */
59 TEST_ASSERT(
60 mbedtls_cipher_get_type( &invalid_ctx ) ==
61 MBEDTLS_CIPHER_NONE);
62
63 /* mbedtls_cipher_get_name() */
64 TEST_ASSERT( mbedtls_cipher_get_name( &invalid_ctx ) == 0 );
65
66 /* mbedtls_cipher_get_key_bitlen() */
67 TEST_ASSERT( mbedtls_cipher_get_key_bitlen( &invalid_ctx ) ==
68 MBEDTLS_KEY_LENGTH_NONE );
69
70 /* mbedtls_cipher_get_operation() */
71 TEST_ASSERT( mbedtls_cipher_get_operation( &invalid_ctx ) ==
72 MBEDTLS_OPERATION_NONE );
73
74 /* mbedtls_cipher_setkey() */
75 TEST_ASSERT(
76 mbedtls_cipher_setkey( &invalid_ctx,
77 valid_buffer,
78 valid_bitlen,
79 valid_operation ) ==
80 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
81
82 /* mbedtls_cipher_set_iv() */
83 TEST_ASSERT(
84 mbedtls_cipher_set_iv( &invalid_ctx,
85 valid_buffer,
86 valid_size ) ==
87 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
88
89 /* mbedtls_cipher_reset() */
90 TEST_ASSERT( mbedtls_cipher_reset( &invalid_ctx ) ==
91 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
92
93#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
94 /* mbedtls_cipher_update_ad() */
95 TEST_ASSERT(
96 mbedtls_cipher_update_ad( &invalid_ctx,
97 valid_buffer,
98 valid_size ) ==
99 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
100#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
101
102#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
103 /* mbedtls_cipher_set_padding_mode() */
104 TEST_ASSERT( mbedtls_cipher_set_padding_mode( &invalid_ctx, valid_mode ) ==
105 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
106#endif
107
108 /* mbedtls_cipher_update() */
109 TEST_ASSERT(
110 mbedtls_cipher_update( &invalid_ctx,
111 valid_buffer,
112 valid_size,
113 valid_buffer,
114 &size_t_var ) ==
115 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
116
117 /* mbedtls_cipher_finish() */
118 TEST_ASSERT(
119 mbedtls_cipher_finish( &invalid_ctx,
120 valid_buffer,
121 &size_t_var ) ==
122 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
123
124#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
125 /* mbedtls_cipher_write_tag() */
126 TEST_ASSERT(
127 mbedtls_cipher_write_tag( &invalid_ctx,
128 valid_buffer,
129 valid_size ) ==
130 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
131
132 /* mbedtls_cipher_check_tag() */
133 TEST_ASSERT(
134 mbedtls_cipher_check_tag( &invalid_ctx,
135 valid_buffer,
136 valid_size ) ==
137 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
138#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
139
140exit:
141 mbedtls_cipher_free( &invalid_ctx );
142 mbedtls_cipher_free( &valid_ctx );
143}
144/* END_CASE */
145
146/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
147void cipher_invalid_param_conditional( )
148{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100149 mbedtls_cipher_context_t valid_ctx;
150
k-stachowiaka5390702018-12-17 11:27:03 +0100151 mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100152 mbedtls_operation_t invalid_operation = 100;
153 mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
154 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
155 int valid_size = sizeof(valid_buffer);
156 int valid_bitlen = valid_size * 8;
k-stachowiaka5390702018-12-17 11:27:03 +0100157 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
158 *( mbedtls_cipher_list() ) );
159
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100160 size_t size_t_var;
161
k-stachowiakfb543602018-12-19 18:34:21 +0100162 (void)valid_mode; /* In some configurations this is unused */
163
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100164 /* mbedtls_cipher_init() */
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100165 TEST_VALID_PARAM( mbedtls_cipher_init( &valid_ctx ) );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100166 TEST_INVALID_PARAM( mbedtls_cipher_init( NULL ) );
167
k-stachowiaka5390702018-12-17 11:27:03 +0100168 /* mbedtls_cipher_setup() */
k-stachowiaka85edd92018-12-19 18:06:35 +0100169 TEST_VALID_PARAM( mbedtls_cipher_setup( &valid_ctx, valid_info ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100170 TEST_INVALID_PARAM_RET(
171 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
172 mbedtls_cipher_setup( NULL, valid_info ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100173
k-stachowiak90b8d4a2018-12-18 16:12:34 +0100174 /* mbedtls_cipher_get_block_size() */
175 TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_block_size( NULL ) );
k-stachowiak90b8d4a2018-12-18 16:12:34 +0100176
177 /* mbedtls_cipher_get_cipher_mode() */
178 TEST_INVALID_PARAM_RET(
179 MBEDTLS_MODE_NONE,
180 mbedtls_cipher_get_cipher_mode( NULL ) );
k-stachowiak90b8d4a2018-12-18 16:12:34 +0100181
182 /* mbedtls_cipher_get_iv_size() */
183 TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_iv_size( NULL ) );
k-stachowiak90b8d4a2018-12-18 16:12:34 +0100184
185 /* mbedtls_cipher_get_type() */
186 TEST_INVALID_PARAM_RET(
187 MBEDTLS_CIPHER_NONE,
188 mbedtls_cipher_get_type( NULL ) );
k-stachowiak90b8d4a2018-12-18 16:12:34 +0100189
190 /* mbedtls_cipher_get_name() */
191 TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_name( NULL ) );
k-stachowiak90b8d4a2018-12-18 16:12:34 +0100192
193 /* mbedtls_cipher_get_key_bitlen() */
194 TEST_INVALID_PARAM_RET(
195 MBEDTLS_KEY_LENGTH_NONE,
196 mbedtls_cipher_get_key_bitlen( NULL ) );
k-stachowiak90b8d4a2018-12-18 16:12:34 +0100197
198 /* mbedtls_cipher_get_operation() */
199 TEST_INVALID_PARAM_RET(
200 MBEDTLS_OPERATION_NONE,
201 mbedtls_cipher_get_operation( NULL ) );
k-stachowiak90b8d4a2018-12-18 16:12:34 +0100202
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100203 /* mbedtls_cipher_setkey() */
204 TEST_INVALID_PARAM_RET(
205 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
k-stachowiaka5390702018-12-17 11:27:03 +0100206 mbedtls_cipher_setkey( NULL,
207 valid_buffer,
208 valid_bitlen,
209 valid_operation ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100210 TEST_INVALID_PARAM_RET(
211 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
212 mbedtls_cipher_setkey( &valid_ctx,
213 NULL,
214 valid_bitlen,
215 valid_operation ) );
216 TEST_INVALID_PARAM_RET(
217 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100218 mbedtls_cipher_setkey( &valid_ctx,
219 valid_buffer,
220 valid_bitlen,
221 invalid_operation ) );
222
k-stachowiaka5390702018-12-17 11:27:03 +0100223 /* mbedtls_cipher_set_iv() */
224 TEST_INVALID_PARAM_RET(
225 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
226 mbedtls_cipher_set_iv( NULL,
227 valid_buffer,
228 valid_size ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100229 TEST_INVALID_PARAM_RET(
230 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
231 mbedtls_cipher_set_iv( &valid_ctx,
232 NULL,
233 valid_size ) );
234
235 /* mbedtls_cipher_reset() */
236 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
237 mbedtls_cipher_reset( NULL ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100238
239#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
240 /* mbedtls_cipher_update_ad() */
241 TEST_INVALID_PARAM_RET(
242 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
243 mbedtls_cipher_update_ad( NULL,
244 valid_buffer,
245 valid_size ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100246 TEST_INVALID_PARAM_RET(
247 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
248 mbedtls_cipher_update_ad( &valid_ctx,
249 NULL,
250 valid_size ) );
251#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
252
253#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100254 /* mbedtls_cipher_set_padding_mode() */
255 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
256 mbedtls_cipher_set_padding_mode( NULL, valid_mode ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100257#endif
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100258
259 /* mbedtls_cipher_update() */
260 TEST_INVALID_PARAM_RET(
261 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
k-stachowiaka5390702018-12-17 11:27:03 +0100262 mbedtls_cipher_update( NULL,
263 valid_buffer,
264 valid_size,
265 valid_buffer,
266 &size_t_var ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100267 TEST_INVALID_PARAM_RET(
268 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100269 mbedtls_cipher_update( &valid_ctx,
270 NULL, valid_size,
271 valid_buffer,
272 &size_t_var ) );
273 TEST_INVALID_PARAM_RET(
274 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
275 mbedtls_cipher_update( &valid_ctx,
276 valid_buffer, valid_size,
277 NULL,
278 &size_t_var ) );
279 TEST_INVALID_PARAM_RET(
280 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
281 mbedtls_cipher_update( &valid_ctx,
282 valid_buffer, valid_size,
283 valid_buffer,
284 NULL ) );
285
286 /* mbedtls_cipher_finish() */
287 TEST_INVALID_PARAM_RET(
288 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
k-stachowiaka5390702018-12-17 11:27:03 +0100289 mbedtls_cipher_finish( NULL,
290 valid_buffer,
291 &size_t_var ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100292 TEST_INVALID_PARAM_RET(
293 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100294 mbedtls_cipher_finish( &valid_ctx,
295 NULL,
296 &size_t_var ) );
297 TEST_INVALID_PARAM_RET(
298 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
299 mbedtls_cipher_finish( &valid_ctx,
300 valid_buffer,
301 NULL ) );
302
k-stachowiaka5390702018-12-17 11:27:03 +0100303#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100304 /* mbedtls_cipher_write_tag() */
305 TEST_INVALID_PARAM_RET(
306 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
k-stachowiaka5390702018-12-17 11:27:03 +0100307 mbedtls_cipher_write_tag( NULL,
308 valid_buffer,
309 valid_size ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100310 TEST_INVALID_PARAM_RET(
311 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100312 mbedtls_cipher_write_tag( &valid_ctx,
313 NULL,
314 valid_size ) );
315
316 /* mbedtls_cipher_check_tag() */
317 TEST_INVALID_PARAM_RET(
318 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
k-stachowiaka5390702018-12-17 11:27:03 +0100319 mbedtls_cipher_check_tag( NULL,
320 valid_buffer,
321 valid_size ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100322 TEST_INVALID_PARAM_RET(
323 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100324 mbedtls_cipher_check_tag( &valid_ctx,
325 NULL,
326 valid_size ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100327#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100328
329 /* mbedtls_cipher_crypt() */
330 TEST_INVALID_PARAM_RET(
331 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
332 mbedtls_cipher_crypt( NULL,
333 valid_buffer, valid_size,
334 valid_buffer, valid_size,
335 valid_buffer, &size_t_var ) );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100336 TEST_INVALID_PARAM_RET(
337 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
338 mbedtls_cipher_crypt( &valid_ctx,
339 NULL, valid_size,
340 valid_buffer, valid_size,
341 valid_buffer, &size_t_var ) );
342 TEST_INVALID_PARAM_RET(
343 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
344 mbedtls_cipher_crypt( &valid_ctx,
345 valid_buffer, valid_size,
346 NULL, valid_size,
347 valid_buffer, &size_t_var ) );
348 TEST_INVALID_PARAM_RET(
349 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
350 mbedtls_cipher_crypt( &valid_ctx,
351 valid_buffer, valid_size,
352 valid_buffer, valid_size,
353 NULL, &size_t_var ) );
354 TEST_INVALID_PARAM_RET(
355 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
356 mbedtls_cipher_crypt( &valid_ctx,
357 valid_buffer, valid_size,
358 valid_buffer, valid_size,
359 valid_buffer, NULL ) );
360
k-stachowiaka5390702018-12-17 11:27:03 +0100361#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100362 /* mbedtls_cipher_auth_encrypt() */
363 TEST_INVALID_PARAM_RET(
364 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
365 mbedtls_cipher_auth_encrypt( NULL,
366 valid_buffer, valid_size,
367 valid_buffer, valid_size,
368 valid_buffer, valid_size,
369 valid_buffer, &size_t_var,
370 valid_buffer, valid_size ) );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100371 TEST_INVALID_PARAM_RET(
372 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
373 mbedtls_cipher_auth_encrypt( &valid_ctx,
374 NULL, valid_size,
375 valid_buffer, valid_size,
376 valid_buffer, valid_size,
377 valid_buffer, &size_t_var,
378 valid_buffer, valid_size ) );
379 TEST_INVALID_PARAM_RET(
380 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
381 mbedtls_cipher_auth_encrypt( &valid_ctx,
382 valid_buffer, valid_size,
383 NULL, valid_size,
384 valid_buffer, valid_size,
385 valid_buffer, &size_t_var,
386 valid_buffer, valid_size ) );
387 TEST_INVALID_PARAM_RET(
388 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
389 mbedtls_cipher_auth_encrypt( &valid_ctx,
390 valid_buffer, valid_size,
391 valid_buffer, valid_size,
392 NULL, valid_size,
393 valid_buffer, &size_t_var,
394 valid_buffer, valid_size ) );
395 TEST_INVALID_PARAM_RET(
396 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
397 mbedtls_cipher_auth_encrypt( &valid_ctx,
398 valid_buffer, valid_size,
399 valid_buffer, valid_size,
400 valid_buffer, valid_size,
401 NULL, &size_t_var,
402 valid_buffer, valid_size ) );
403 TEST_INVALID_PARAM_RET(
404 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
405 mbedtls_cipher_auth_encrypt( &valid_ctx,
406 valid_buffer, valid_size,
407 valid_buffer, valid_size,
408 valid_buffer, valid_size,
409 valid_buffer, NULL,
410 valid_buffer, valid_size ) );
411 TEST_INVALID_PARAM_RET(
412 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
413 mbedtls_cipher_auth_encrypt( &valid_ctx,
414 valid_buffer, valid_size,
415 valid_buffer, valid_size,
416 valid_buffer, valid_size,
417 valid_buffer, &size_t_var,
418 NULL, valid_size ) );
419
420 /* mbedtls_cipher_auth_decrypt() */
421 TEST_INVALID_PARAM_RET(
422 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
423 mbedtls_cipher_auth_decrypt( NULL,
424 valid_buffer, valid_size,
425 valid_buffer, valid_size,
426 valid_buffer, valid_size,
427 valid_buffer, &size_t_var,
428 valid_buffer, valid_size ) );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100429 TEST_INVALID_PARAM_RET(
430 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
431 mbedtls_cipher_auth_decrypt( &valid_ctx,
432 NULL, valid_size,
433 valid_buffer, valid_size,
434 valid_buffer, valid_size,
435 valid_buffer, &size_t_var,
436 valid_buffer, valid_size ) );
437 TEST_INVALID_PARAM_RET(
438 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
439 mbedtls_cipher_auth_decrypt( &valid_ctx,
440 valid_buffer, valid_size,
441 NULL, valid_size,
442 valid_buffer, valid_size,
443 valid_buffer, &size_t_var,
444 valid_buffer, valid_size ) );
445 TEST_INVALID_PARAM_RET(
446 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
447 mbedtls_cipher_auth_decrypt( &valid_ctx,
448 valid_buffer, valid_size,
449 valid_buffer, valid_size,
450 NULL, valid_size,
451 valid_buffer, &size_t_var,
452 valid_buffer, valid_size ) );
453 TEST_INVALID_PARAM_RET(
454 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
455 mbedtls_cipher_auth_decrypt( &valid_ctx,
456 valid_buffer, valid_size,
457 valid_buffer, valid_size,
458 valid_buffer, valid_size,
459 NULL, &size_t_var,
460 valid_buffer, valid_size ) );
461 TEST_INVALID_PARAM_RET(
462 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
463 mbedtls_cipher_auth_decrypt( &valid_ctx,
464 valid_buffer, valid_size,
465 valid_buffer, valid_size,
466 valid_buffer, valid_size,
467 valid_buffer, NULL,
468 valid_buffer, valid_size ) );
469 TEST_INVALID_PARAM_RET(
470 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
471 mbedtls_cipher_auth_decrypt( &valid_ctx,
472 valid_buffer, valid_size,
473 valid_buffer, valid_size,
474 valid_buffer, valid_size,
475 valid_buffer, &size_t_var,
476 NULL, valid_size ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100477#endif /* defined(MBEDTLS_CIPHER_MODE_AEAD) */
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100478
479 /* mbedtls_cipher_free() */
480 TEST_VALID_PARAM( mbedtls_cipher_free( NULL ) );
481exit:
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100482 TEST_VALID_PARAM( mbedtls_cipher_free( &valid_ctx ) );
483}
484/* END_CASE */
485
Paul Bakker6a9c7252016-07-14 13:46:10 +0100486/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100487void cipher_special_behaviours( )
Paul Bakker6a9c7252016-07-14 13:46:10 +0100488{
489 const mbedtls_cipher_info_t *cipher_info;
490 mbedtls_cipher_context_t ctx;
491 unsigned char input[32];
492 unsigned char output[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300493#if defined (MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100494 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300495#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100496 size_t olen = 0;
497
498 mbedtls_cipher_init( &ctx );
499 memset( input, 0, sizeof( input ) );
500 memset( output, 0, sizeof( output ) );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300501#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100502 memset( iv, 0, sizeof( iv ) );
503
504 /* Check and get info structures */
Ron Eldor7b012442017-09-25 17:03:12 +0300505 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
Paul Bakker6a9c7252016-07-14 13:46:10 +0100506 TEST_ASSERT( NULL != cipher_info );
507
508 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
509
510 /* IV too big */
511 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
512 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
513
514 /* IV too small */
515 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
516 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
517
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300518 mbedtls_cipher_free( &ctx );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300519 mbedtls_cipher_init( &ctx );
Ron Eldor6f90ed82017-09-26 12:08:54 +0300520#endif /* MBEDTLS_CIPHER_MODE_CBC */
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300521 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
Ron Eldor7b012442017-09-25 17:03:12 +0300522 TEST_ASSERT( NULL != cipher_info );
523
524 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
525
Paul Bakker6a9c7252016-07-14 13:46:10 +0100526 /* Update ECB with partial block */
527 TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
528 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
529
530exit:
531 mbedtls_cipher_free( &ctx );
532}
533/* END_CASE */
534
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200535/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100536void enc_dec_buf( int cipher_id, char * cipher_string, int key_len,
Paul Bakker33b43f12013-08-20 11:48:36 +0200537 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200538{
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200539 size_t length = length_val, outlen, total_len, i, block_size;
Jaeden Amerod906b812018-06-08 11:03:16 +0100540 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000541 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200542 unsigned char ad[13];
543 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200544 unsigned char inbuf[64];
545 unsigned char encbuf[64];
546 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 const mbedtls_cipher_info_t *cipher_info;
549 mbedtls_cipher_context_t ctx_dec;
550 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000551
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200552 /*
553 * Prepare contexts
554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_cipher_init( &ctx_dec );
556 mbedtls_cipher_init( &ctx_enc );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200557
558 memset( key, 0x2a, sizeof( key ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000559
560 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000562 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000564
565 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200566 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
567 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
570 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200573 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
576 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200577 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200578#else
579 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200581
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200582 /*
583 * Do a few encode/decode cycles
584 */
585 for( i = 0; i < 3; i++ )
586 {
587 memset( iv , 0x00 + i, sizeof( iv ) );
588 memset( ad, 0x10 + i, sizeof( ad ) );
589 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
590
591 memset( encbuf, 0, sizeof( encbuf ) );
592 memset( decbuf, 0, sizeof( decbuf ) );
593 memset( tag, 0, sizeof( tag ) );
594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
596 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
599 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200600
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200601#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
603 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200604#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000605
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200606 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
607 TEST_ASSERT( block_size != 0 );
608
Paul Bakker8123e9d2011-01-06 15:37:30 +0000609 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200611 total_len = outlen;
612
613 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200614 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200615 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200616 total_len + block_size > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200619 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000620
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200621#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200623#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200624
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200625 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200626 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200627 total_len > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200628 total_len <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000629
630 /* decode the previously encoded string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200632 total_len = outlen;
633
634 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200635 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200636 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200637 total_len + block_size >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200640 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000641
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200642#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200644#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200645
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200646 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200647 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000648 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200649 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000650
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200651 /*
652 * Done
653 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200654exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_cipher_free( &ctx_dec );
656 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200657}
Paul Bakker33b43f12013-08-20 11:48:36 +0200658/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000659
Paul Bakker33b43f12013-08-20 11:48:36 +0200660/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100661void enc_fail( int cipher_id, int pad_mode, int key_len, int length_val,
662 int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200663{
Paul Bakker33b43f12013-08-20 11:48:36 +0200664 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200665 unsigned char key[32];
666 unsigned char iv[16];
667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 const mbedtls_cipher_info_t *cipher_info;
669 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200670
671 unsigned char inbuf[64];
672 unsigned char encbuf[64];
673
674 size_t outlen = 0;
675
676 memset( key, 0, 32 );
677 memset( iv , 0, 16 );
678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200680
681 memset( inbuf, 5, 64 );
682 memset( encbuf, 0, 64 );
683
684 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200686 TEST_ASSERT( NULL != cipher_info );
687
688 /* Initialise context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200689 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
691#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
692 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200693#else
694 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
696 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
697 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200698#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200700#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200701
702 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
704 TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200705
706 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200707exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200709}
Paul Bakker33b43f12013-08-20 11:48:36 +0200710/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200711
Paul Bakker33b43f12013-08-20 11:48:36 +0200712/* BEGIN_CASE */
Jaeden Amero4e47aa02019-06-05 15:35:08 +0100713void dec_empty_buf( int cipher )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200714{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000715 unsigned char key[32];
716 unsigned char iv[16];
717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 mbedtls_cipher_context_t ctx_dec;
719 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000720
721 unsigned char encbuf[64];
722 unsigned char decbuf[64];
723
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000724 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000725
Jaeden Amero4e47aa02019-06-05 15:35:08 +0100726 int expected_ret;
727
Paul Bakker8123e9d2011-01-06 15:37:30 +0000728 memset( key, 0, 32 );
729 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200730
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_cipher_init( &ctx_dec );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200732
Paul Bakker8123e9d2011-01-06 15:37:30 +0000733 memset( encbuf, 0, 64 );
734 memset( decbuf, 0, 64 );
735
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200736 /* Initialise context */
Jaeden Amero4e47aa02019-06-05 15:35:08 +0100737 cipher_info = mbedtls_cipher_info_from_type( cipher );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000738 TEST_ASSERT( NULL != cipher_info);
Jaeden Amero4e47aa02019-06-05 15:35:08 +0100739 TEST_ASSERT( sizeof(key) * 8 >= cipher_info->key_bitlen );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200740
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200741 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000742
Jaeden Amero4e47aa02019-06-05 15:35:08 +0100743 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec,
744 key, cipher_info->key_bitlen,
745 MBEDTLS_DECRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200750
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200751#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200753#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000754
755 /* decode 0-byte string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000757 TEST_ASSERT( 0 == outlen );
Jaeden Amero4e47aa02019-06-05 15:35:08 +0100758
759 if ( cipher_info->mode == MBEDTLS_MODE_CBC ||
760 cipher_info->mode == MBEDTLS_MODE_ECB )
761 {
762 /* CBC and ECB ciphers need a full block of input. */
763 expected_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
764 }
765 else
766 {
767 /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
768 * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
769 * decrypting an empty buffer. */
770 expected_ret = 0;
771 }
772
773 TEST_ASSERT( expected_ret == mbedtls_cipher_finish(
774 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000775 TEST_ASSERT( 0 == outlen );
776
Paul Bakkerbd51b262014-07-10 15:26:12 +0200777exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_cipher_free( &ctx_dec );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200779}
Paul Bakker33b43f12013-08-20 11:48:36 +0200780/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000781
Paul Bakker33b43f12013-08-20 11:48:36 +0200782/* BEGIN_CASE */
783void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700784 int second_length_val, int pad_mode,
785 int first_encrypt_output_len, int second_encrypt_output_len,
786 int first_decrypt_output_len, int second_decrypt_output_len )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200787{
Paul Bakker33b43f12013-08-20 11:48:36 +0200788 size_t first_length = first_length_val;
789 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000790 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200791 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000792 unsigned char key[32];
793 unsigned char iv[16];
794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_cipher_context_t ctx_dec;
796 mbedtls_cipher_context_t ctx_enc;
797 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000798
799 unsigned char inbuf[64];
800 unsigned char encbuf[64];
801 unsigned char decbuf[64];
802
Paul Bakker23986e52011-04-24 08:57:21 +0000803 size_t outlen = 0;
804 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000805
806 memset( key, 0, 32 );
807 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 mbedtls_cipher_init( &ctx_dec );
810 mbedtls_cipher_init( &ctx_enc );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200811
Paul Bakker8123e9d2011-01-06 15:37:30 +0000812 memset( inbuf, 5, 64 );
813 memset( encbuf, 0, 64 );
814 memset( decbuf, 0, 64 );
815
816 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000818 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200819
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200820 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
821 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
824 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000825
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700826#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
827 if( -1 != pad_mode )
828 {
829 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
830 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
831 }
832#else
833 (void) pad_mode;
834#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
837 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
840 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200841
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200842#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
844 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200845#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000846
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200847 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
848 TEST_ASSERT( block_size != 0 );
849
Paul Bakker8123e9d2011-01-06 15:37:30 +0000850 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700852 TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000853 totaloutlen = outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700855 TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000856 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200857 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200858 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200859 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200860 totaloutlen + block_size > length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000863 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200864 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200865 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200866 totaloutlen > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200867 totaloutlen <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000868
869 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700870 second_length = totaloutlen - first_length;
871 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
872 TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200873 totaloutlen = outlen;
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700874 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
875 TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
876 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200877
878 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200879 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200880 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200881 totaloutlen + block_size >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200882
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700883 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200884 totaloutlen += outlen;
885
886 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000887
888 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
889
Paul Bakkerbd51b262014-07-10 15:26:12 +0200890exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 mbedtls_cipher_free( &ctx_dec );
892 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200893}
Paul Bakker33b43f12013-08-20 11:48:36 +0200894/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000895
Paul Bakker33b43f12013-08-20 11:48:36 +0200896/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100897void decrypt_test_vec( int cipher_id, int pad_mode, data_t * key,
898 data_t * iv, data_t * cipher,
899 data_t * clear, data_t * ad, data_t * tag,
Azim Khand30ca132017-06-09 04:32:58 +0100900 int finish_result, int tag_result )
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200901{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200902 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200904 size_t outlen, total_len;
905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200907
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200908 memset( output, 0x00, sizeof( output ) );
909
Azim Khanf1aaec92017-05-30 14:23:15 +0100910#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100911 ((void) ad);
912 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200913#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200914
915 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200916 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 mbedtls_cipher_info_from_type( cipher_id ) ) );
Azim Khand30ca132017-06-09 04:32:58 +0100918 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200920 if( pad_mode != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200922#else
923 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Azim Khand30ca132017-06-09 04:32:58 +0100925 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv->x, iv->len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200927#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100928 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad->x, ad->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200929#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200930
Azim Khand30ca132017-06-09 04:32:58 +0100931 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200932 total_len = 0;
Azim Khand30ca132017-06-09 04:32:58 +0100933 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher->x, cipher->len, output, &outlen ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200934 total_len += outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200936 &outlen ) );
937 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200938#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100939 TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag->x, tag->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200940#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200941
942 /* check plaintext only if everything went fine */
943 if( 0 == finish_result && 0 == tag_result )
944 {
Azim Khand30ca132017-06-09 04:32:58 +0100945 TEST_ASSERT( total_len == clear->len );
946 TEST_ASSERT( 0 == memcmp( output, clear->x, clear->len ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200947 }
948
Paul Bakkerbd51b262014-07-10 15:26:12 +0200949exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200951}
952/* END_CASE */
953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */
Azim Khan5fcca462018-06-29 11:05:32 +0100955void auth_crypt_tv( int cipher_id, data_t * key, data_t * iv,
956 data_t * ad, data_t * cipher, data_t * tag,
957 char * result, data_t * clear )
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200958{
959 int ret;
Manuel Pégourié-Gonnard69767d12018-05-09 12:25:18 +0200960 unsigned char output[267]; /* above + 2 (overwrite check) */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200961 unsigned char my_tag[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200963 size_t outlen;
964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200966
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200967 memset( output, 0xFF, sizeof( output ) );
Azim Khan46c9b1f2017-05-31 20:46:35 +0100968 memset( my_tag, 0xFF, sizeof( my_tag ) );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200969
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200970
971 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200972 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_cipher_info_from_type( cipher_id ) ) );
Azim Khand30ca132017-06-09 04:32:58 +0100974 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200975
Azim Khand30ca132017-06-09 04:32:58 +0100976 /* decode buffer and check tag->x */
977 ret = mbedtls_cipher_auth_decrypt( &ctx, iv->x, iv->len, ad->x, ad->len,
978 cipher->x, cipher->len, output, &outlen,
979 tag->x, tag->len );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200980
981 /* make sure we didn't overwrite */
982 TEST_ASSERT( output[outlen + 0] == 0xFF );
983 TEST_ASSERT( output[outlen + 1] == 0xFF );
984
985 /* make sure the message is rejected if it should be */
Azim Khan46c9b1f2017-05-31 20:46:35 +0100986 if( strcmp( result, "FAIL" ) == 0 )
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200989 goto exit;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200990 }
991
992 /* otherwise, make sure it was decrypted properly */
993 TEST_ASSERT( ret == 0 );
994
Azim Khand30ca132017-06-09 04:32:58 +0100995 TEST_ASSERT( outlen == clear->len );
996 TEST_ASSERT( memcmp( output, clear->x, clear->len ) == 0 );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200997
Azim Khand30ca132017-06-09 04:32:58 +0100998 /* then encrypt the clear->x and make sure we get the same ciphertext and tag->x */
Jack Lloydb25719b2019-03-07 16:59:14 -0500999 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len,
1000 MBEDTLS_ENCRYPT ) );
1001
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001002 memset( output, 0xFF, sizeof( output ) );
1003 outlen = 0;
1004
Azim Khand30ca132017-06-09 04:32:58 +01001005 ret = mbedtls_cipher_auth_encrypt( &ctx, iv->x, iv->len, ad->x, ad->len,
1006 clear->x, clear->len, output, &outlen,
1007 my_tag, tag->len );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001008 TEST_ASSERT( ret == 0 );
1009
Jack Lloydb25719b2019-03-07 16:59:14 -05001010 TEST_ASSERT( outlen == cipher->len );
1011 TEST_ASSERT( memcmp( output, cipher->x, cipher->len ) == 0 );
Azim Khand30ca132017-06-09 04:32:58 +01001012 TEST_ASSERT( memcmp( my_tag, tag->x, tag->len ) == 0 );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001013
1014 /* make sure we didn't overwrite */
1015 TEST_ASSERT( output[outlen + 0] == 0xFF );
1016 TEST_ASSERT( output[outlen + 1] == 0xFF );
Azim Khand30ca132017-06-09 04:32:58 +01001017 TEST_ASSERT( my_tag[tag->len + 0] == 0xFF );
1018 TEST_ASSERT( my_tag[tag->len + 1] == 0xFF );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001019
1020
Paul Bakkerbd51b262014-07-10 15:26:12 +02001021exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001023}
1024/* END_CASE */
1025
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +02001026/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +01001027void test_vec_ecb( int cipher_id, int operation, data_t * key,
1028 data_t * input, data_t * result, int finish_result
Azim Khand30ca132017-06-09 04:32:58 +01001029 )
Paul Bakker5e0efa72013-09-08 23:04:04 +02001030{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001032 unsigned char output[32];
1033 size_t outlen;
1034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001036
Paul Bakker5e0efa72013-09-08 23:04:04 +02001037 memset( output, 0x00, sizeof( output ) );
1038
1039 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001040 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 mbedtls_cipher_info_from_type( cipher_id ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001042
Paul Bakker5e0efa72013-09-08 23:04:04 +02001043
Azim Khand30ca132017-06-09 04:32:58 +01001044 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001045
Azim Khand30ca132017-06-09 04:32:58 +01001046 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_cipher_get_block_size( &ctx ),
Paul Bakker5e0efa72013-09-08 23:04:04 +02001048 output, &outlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
1050 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001051 &outlen ) );
1052 TEST_ASSERT( 0 == outlen );
1053
1054 /* check plaintext only if everything went fine */
1055 if( 0 == finish_result )
Azim Khand30ca132017-06-09 04:32:58 +01001056 TEST_ASSERT( 0 == memcmp( output, result->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_cipher_get_block_size( &ctx ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001058
Paul Bakkerbd51b262014-07-10 15:26:12 +02001059exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 mbedtls_cipher_free( &ctx );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001061}
1062/* END_CASE */
1063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Ron Eldor7b012442017-09-25 17:03:12 +03001065void test_vec_crypt( int cipher_id, int operation, char *hex_key,
1066 char *hex_iv, char *hex_input, char *hex_result,
1067 int finish_result )
1068{
1069 unsigned char key[50];
1070 unsigned char input[16];
1071 unsigned char result[16];
1072 unsigned char iv[16];
1073 size_t key_len, iv_len, inputlen, resultlen;
1074 mbedtls_cipher_context_t ctx;
1075 unsigned char output[32];
1076 size_t outlen;
1077
1078 mbedtls_cipher_init( &ctx );
1079
1080 memset( key, 0x00, sizeof( key ) );
1081 memset( input, 0x00, sizeof( input ) );
1082 memset( result, 0x00, sizeof( result ) );
1083 memset( output, 0x00, sizeof( output ) );
1084 memset( iv, 0x00, sizeof( iv ) );
1085
1086 /* Prepare context */
1087 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
1088 mbedtls_cipher_info_from_type( cipher_id ) ) );
1089
Ronald Crona0c9ff32020-06-08 17:05:57 +02001090 key_len = mbedtls_test_unhexify( key, hex_key );
1091 inputlen = mbedtls_test_unhexify( input, hex_input );
1092 resultlen = mbedtls_test_unhexify( result, hex_result );
Ron Eldor7b012442017-09-25 17:03:12 +03001093
1094 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, operation ) );
1095 if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode )
1096 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) );
1097
Ronald Crona0c9ff32020-06-08 17:05:57 +02001098 iv_len = mbedtls_test_unhexify( iv, hex_iv );
Ron Eldor7b012442017-09-25 17:03:12 +03001099
1100 TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv_len ? iv : NULL,
1101 iv_len, input, inputlen,
1102 output, &outlen ) );
1103 TEST_ASSERT( resultlen == outlen );
1104 /* check plaintext only if everything went fine */
1105 if( 0 == finish_result )
1106 TEST_ASSERT( 0 == memcmp( output, result, outlen ) );
1107
1108exit:
1109 mbedtls_cipher_free( &ctx );
1110}
1111/* END_CASE */
1112
1113/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +02001114void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001115{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 const mbedtls_cipher_info_t *cipher_info;
1117 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001122 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001123 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001126
Paul Bakkerbd51b262014-07-10 15:26:12 +02001127exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001129}
Paul Bakker33b43f12013-08-20 11:48:36 +02001130/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +01001133void check_padding( int pad_mode, data_t * input, int ret, int dlen_check
Azim Khand30ca132017-06-09 04:32:58 +01001134 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 mbedtls_cipher_info_t cipher_info;
1137 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +01001138 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001139
1140 /* build a fake context just for getting access to get_padding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 mbedtls_cipher_init( &ctx );
1142 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001143 ctx.cipher_info = &cipher_info;
1144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001146
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001147
Azim Khand30ca132017-06-09 04:32:58 +01001148 TEST_ASSERT( ret == ctx.get_padding( input->x, input->len, &dlen ) );
Paul Bakker33b43f12013-08-20 11:48:36 +02001149 if( 0 == ret )
1150 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001151}
Paul Bakker33b43f12013-08-20 11:48:36 +02001152/* END_CASE */