blob: c9cfde529559242d05593826824f3559eef956f9 [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 );
k-stachowiaka85edd92018-12-19 18:06:35 +010041 mbedtls_cipher_init( &invalid_ctx );
42
Gilles Peskinea0b0dce2021-12-10 14:28:31 +010043 TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, valid_info ) == 0 );
44
k-stachowiaka85edd92018-12-19 18:06:35 +010045 /* mbedtls_cipher_setup() */
46 TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, NULL ) ==
47 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
48
49 /* mbedtls_cipher_get_block_size() */
50 TEST_ASSERT( mbedtls_cipher_get_block_size( &invalid_ctx ) == 0 );
51
52 /* mbedtls_cipher_get_cipher_mode() */
53 TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &invalid_ctx ) ==
54 MBEDTLS_MODE_NONE );
55
56 /* mbedtls_cipher_get_iv_size() */
57 TEST_ASSERT( mbedtls_cipher_get_iv_size( &invalid_ctx ) == 0 );
58
59 /* mbedtls_cipher_get_type() */
60 TEST_ASSERT(
61 mbedtls_cipher_get_type( &invalid_ctx ) ==
62 MBEDTLS_CIPHER_NONE);
63
64 /* mbedtls_cipher_get_name() */
65 TEST_ASSERT( mbedtls_cipher_get_name( &invalid_ctx ) == 0 );
66
67 /* mbedtls_cipher_get_key_bitlen() */
68 TEST_ASSERT( mbedtls_cipher_get_key_bitlen( &invalid_ctx ) ==
69 MBEDTLS_KEY_LENGTH_NONE );
70
71 /* mbedtls_cipher_get_operation() */
72 TEST_ASSERT( mbedtls_cipher_get_operation( &invalid_ctx ) ==
73 MBEDTLS_OPERATION_NONE );
74
75 /* mbedtls_cipher_setkey() */
76 TEST_ASSERT(
77 mbedtls_cipher_setkey( &invalid_ctx,
78 valid_buffer,
79 valid_bitlen,
80 valid_operation ) ==
81 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
82
83 /* mbedtls_cipher_set_iv() */
84 TEST_ASSERT(
85 mbedtls_cipher_set_iv( &invalid_ctx,
86 valid_buffer,
87 valid_size ) ==
88 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
89
90 /* mbedtls_cipher_reset() */
91 TEST_ASSERT( mbedtls_cipher_reset( &invalid_ctx ) ==
92 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
93
94#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
95 /* mbedtls_cipher_update_ad() */
96 TEST_ASSERT(
97 mbedtls_cipher_update_ad( &invalid_ctx,
98 valid_buffer,
99 valid_size ) ==
100 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
101#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
102
103#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
104 /* mbedtls_cipher_set_padding_mode() */
105 TEST_ASSERT( mbedtls_cipher_set_padding_mode( &invalid_ctx, valid_mode ) ==
106 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
107#endif
108
109 /* mbedtls_cipher_update() */
110 TEST_ASSERT(
111 mbedtls_cipher_update( &invalid_ctx,
112 valid_buffer,
113 valid_size,
114 valid_buffer,
115 &size_t_var ) ==
116 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
117
118 /* mbedtls_cipher_finish() */
119 TEST_ASSERT(
120 mbedtls_cipher_finish( &invalid_ctx,
121 valid_buffer,
122 &size_t_var ) ==
123 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
124
125#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
126 /* mbedtls_cipher_write_tag() */
127 TEST_ASSERT(
128 mbedtls_cipher_write_tag( &invalid_ctx,
129 valid_buffer,
130 valid_size ) ==
131 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
132
133 /* mbedtls_cipher_check_tag() */
134 TEST_ASSERT(
135 mbedtls_cipher_check_tag( &invalid_ctx,
136 valid_buffer,
137 valid_size ) ==
138 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
139#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
140
141exit:
142 mbedtls_cipher_free( &invalid_ctx );
143 mbedtls_cipher_free( &valid_ctx );
144}
145/* END_CASE */
146
147/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
148void cipher_invalid_param_conditional( )
149{
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100150 mbedtls_cipher_context_t valid_ctx;
151
k-stachowiaka5390702018-12-17 11:27:03 +0100152 mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100153 mbedtls_operation_t invalid_operation = 100;
154 mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
155 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
156 int valid_size = sizeof(valid_buffer);
157 int valid_bitlen = valid_size * 8;
k-stachowiaka5390702018-12-17 11:27:03 +0100158 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
159 *( mbedtls_cipher_list() ) );
160
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100161 size_t size_t_var;
162
k-stachowiakfb543602018-12-19 18:34:21 +0100163 (void)valid_mode; /* In some configurations this is unused */
164
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100165 /* mbedtls_cipher_init() */
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100166 TEST_VALID_PARAM( mbedtls_cipher_init( &valid_ctx ) );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100167 TEST_INVALID_PARAM( mbedtls_cipher_init( NULL ) );
168
k-stachowiaka5390702018-12-17 11:27:03 +0100169 /* mbedtls_cipher_setup() */
k-stachowiaka85edd92018-12-19 18:06:35 +0100170 TEST_VALID_PARAM( mbedtls_cipher_setup( &valid_ctx, valid_info ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100171 TEST_INVALID_PARAM_RET(
172 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
173 mbedtls_cipher_setup( NULL, valid_info ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100174
k-stachowiak90b8d4a2018-12-18 16:12:34 +0100175 /* mbedtls_cipher_get_block_size() */
176 TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_block_size( NULL ) );
k-stachowiak90b8d4a2018-12-18 16:12:34 +0100177
178 /* mbedtls_cipher_get_cipher_mode() */
179 TEST_INVALID_PARAM_RET(
180 MBEDTLS_MODE_NONE,
181 mbedtls_cipher_get_cipher_mode( NULL ) );
k-stachowiak90b8d4a2018-12-18 16:12:34 +0100182
183 /* mbedtls_cipher_get_iv_size() */
184 TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_iv_size( NULL ) );
k-stachowiak90b8d4a2018-12-18 16:12:34 +0100185
186 /* mbedtls_cipher_get_type() */
187 TEST_INVALID_PARAM_RET(
188 MBEDTLS_CIPHER_NONE,
189 mbedtls_cipher_get_type( NULL ) );
k-stachowiak90b8d4a2018-12-18 16:12:34 +0100190
191 /* mbedtls_cipher_get_name() */
192 TEST_INVALID_PARAM_RET( 0, mbedtls_cipher_get_name( NULL ) );
k-stachowiak90b8d4a2018-12-18 16:12:34 +0100193
194 /* mbedtls_cipher_get_key_bitlen() */
195 TEST_INVALID_PARAM_RET(
196 MBEDTLS_KEY_LENGTH_NONE,
197 mbedtls_cipher_get_key_bitlen( NULL ) );
k-stachowiak90b8d4a2018-12-18 16:12:34 +0100198
199 /* mbedtls_cipher_get_operation() */
200 TEST_INVALID_PARAM_RET(
201 MBEDTLS_OPERATION_NONE,
202 mbedtls_cipher_get_operation( NULL ) );
k-stachowiak90b8d4a2018-12-18 16:12:34 +0100203
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100204 /* mbedtls_cipher_setkey() */
205 TEST_INVALID_PARAM_RET(
206 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
k-stachowiaka5390702018-12-17 11:27:03 +0100207 mbedtls_cipher_setkey( NULL,
208 valid_buffer,
209 valid_bitlen,
210 valid_operation ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100211 TEST_INVALID_PARAM_RET(
212 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
213 mbedtls_cipher_setkey( &valid_ctx,
214 NULL,
215 valid_bitlen,
216 valid_operation ) );
217 TEST_INVALID_PARAM_RET(
218 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100219 mbedtls_cipher_setkey( &valid_ctx,
220 valid_buffer,
221 valid_bitlen,
222 invalid_operation ) );
223
k-stachowiaka5390702018-12-17 11:27:03 +0100224 /* mbedtls_cipher_set_iv() */
225 TEST_INVALID_PARAM_RET(
226 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
227 mbedtls_cipher_set_iv( NULL,
228 valid_buffer,
229 valid_size ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100230 TEST_INVALID_PARAM_RET(
231 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
232 mbedtls_cipher_set_iv( &valid_ctx,
233 NULL,
234 valid_size ) );
235
236 /* mbedtls_cipher_reset() */
237 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
238 mbedtls_cipher_reset( NULL ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100239
240#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
241 /* mbedtls_cipher_update_ad() */
242 TEST_INVALID_PARAM_RET(
243 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
244 mbedtls_cipher_update_ad( NULL,
245 valid_buffer,
246 valid_size ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100247 TEST_INVALID_PARAM_RET(
248 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
249 mbedtls_cipher_update_ad( &valid_ctx,
250 NULL,
251 valid_size ) );
252#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
253
254#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100255 /* mbedtls_cipher_set_padding_mode() */
256 TEST_INVALID_PARAM_RET( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
257 mbedtls_cipher_set_padding_mode( NULL, valid_mode ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100258#endif
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100259
260 /* mbedtls_cipher_update() */
261 TEST_INVALID_PARAM_RET(
262 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
k-stachowiaka5390702018-12-17 11:27:03 +0100263 mbedtls_cipher_update( NULL,
264 valid_buffer,
265 valid_size,
266 valid_buffer,
267 &size_t_var ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100268 TEST_INVALID_PARAM_RET(
269 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100270 mbedtls_cipher_update( &valid_ctx,
271 NULL, valid_size,
272 valid_buffer,
273 &size_t_var ) );
274 TEST_INVALID_PARAM_RET(
275 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
276 mbedtls_cipher_update( &valid_ctx,
277 valid_buffer, valid_size,
278 NULL,
279 &size_t_var ) );
280 TEST_INVALID_PARAM_RET(
281 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
282 mbedtls_cipher_update( &valid_ctx,
283 valid_buffer, valid_size,
284 valid_buffer,
285 NULL ) );
286
287 /* mbedtls_cipher_finish() */
288 TEST_INVALID_PARAM_RET(
289 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
k-stachowiaka5390702018-12-17 11:27:03 +0100290 mbedtls_cipher_finish( NULL,
291 valid_buffer,
292 &size_t_var ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100293 TEST_INVALID_PARAM_RET(
294 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100295 mbedtls_cipher_finish( &valid_ctx,
296 NULL,
297 &size_t_var ) );
298 TEST_INVALID_PARAM_RET(
299 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
300 mbedtls_cipher_finish( &valid_ctx,
301 valid_buffer,
302 NULL ) );
303
k-stachowiaka5390702018-12-17 11:27:03 +0100304#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100305 /* mbedtls_cipher_write_tag() */
306 TEST_INVALID_PARAM_RET(
307 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
k-stachowiaka5390702018-12-17 11:27:03 +0100308 mbedtls_cipher_write_tag( NULL,
309 valid_buffer,
310 valid_size ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100311 TEST_INVALID_PARAM_RET(
312 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100313 mbedtls_cipher_write_tag( &valid_ctx,
314 NULL,
315 valid_size ) );
316
317 /* mbedtls_cipher_check_tag() */
318 TEST_INVALID_PARAM_RET(
319 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
k-stachowiaka5390702018-12-17 11:27:03 +0100320 mbedtls_cipher_check_tag( NULL,
321 valid_buffer,
322 valid_size ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100323 TEST_INVALID_PARAM_RET(
324 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100325 mbedtls_cipher_check_tag( &valid_ctx,
326 NULL,
327 valid_size ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100328#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100329
330 /* mbedtls_cipher_crypt() */
331 TEST_INVALID_PARAM_RET(
332 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
333 mbedtls_cipher_crypt( NULL,
334 valid_buffer, valid_size,
335 valid_buffer, valid_size,
336 valid_buffer, &size_t_var ) );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100337 TEST_INVALID_PARAM_RET(
338 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
339 mbedtls_cipher_crypt( &valid_ctx,
340 NULL, valid_size,
341 valid_buffer, valid_size,
342 valid_buffer, &size_t_var ) );
343 TEST_INVALID_PARAM_RET(
344 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
345 mbedtls_cipher_crypt( &valid_ctx,
346 valid_buffer, valid_size,
347 NULL, valid_size,
348 valid_buffer, &size_t_var ) );
349 TEST_INVALID_PARAM_RET(
350 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
351 mbedtls_cipher_crypt( &valid_ctx,
352 valid_buffer, valid_size,
353 valid_buffer, valid_size,
354 NULL, &size_t_var ) );
355 TEST_INVALID_PARAM_RET(
356 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
357 mbedtls_cipher_crypt( &valid_ctx,
358 valid_buffer, valid_size,
359 valid_buffer, valid_size,
360 valid_buffer, NULL ) );
361
k-stachowiaka5390702018-12-17 11:27:03 +0100362#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100363 /* mbedtls_cipher_auth_encrypt() */
364 TEST_INVALID_PARAM_RET(
365 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
366 mbedtls_cipher_auth_encrypt( NULL,
367 valid_buffer, valid_size,
368 valid_buffer, valid_size,
369 valid_buffer, valid_size,
370 valid_buffer, &size_t_var,
371 valid_buffer, valid_size ) );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100372 TEST_INVALID_PARAM_RET(
373 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
374 mbedtls_cipher_auth_encrypt( &valid_ctx,
375 NULL, valid_size,
376 valid_buffer, valid_size,
377 valid_buffer, valid_size,
378 valid_buffer, &size_t_var,
379 valid_buffer, valid_size ) );
380 TEST_INVALID_PARAM_RET(
381 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
382 mbedtls_cipher_auth_encrypt( &valid_ctx,
383 valid_buffer, valid_size,
384 NULL, valid_size,
385 valid_buffer, valid_size,
386 valid_buffer, &size_t_var,
387 valid_buffer, valid_size ) );
388 TEST_INVALID_PARAM_RET(
389 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
390 mbedtls_cipher_auth_encrypt( &valid_ctx,
391 valid_buffer, valid_size,
392 valid_buffer, valid_size,
393 NULL, valid_size,
394 valid_buffer, &size_t_var,
395 valid_buffer, valid_size ) );
396 TEST_INVALID_PARAM_RET(
397 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
398 mbedtls_cipher_auth_encrypt( &valid_ctx,
399 valid_buffer, valid_size,
400 valid_buffer, valid_size,
401 valid_buffer, valid_size,
402 NULL, &size_t_var,
403 valid_buffer, valid_size ) );
404 TEST_INVALID_PARAM_RET(
405 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
406 mbedtls_cipher_auth_encrypt( &valid_ctx,
407 valid_buffer, valid_size,
408 valid_buffer, valid_size,
409 valid_buffer, valid_size,
410 valid_buffer, NULL,
411 valid_buffer, valid_size ) );
412 TEST_INVALID_PARAM_RET(
413 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
414 mbedtls_cipher_auth_encrypt( &valid_ctx,
415 valid_buffer, valid_size,
416 valid_buffer, valid_size,
417 valid_buffer, valid_size,
418 valid_buffer, &size_t_var,
419 NULL, valid_size ) );
420
421 /* mbedtls_cipher_auth_decrypt() */
422 TEST_INVALID_PARAM_RET(
423 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
424 mbedtls_cipher_auth_decrypt( NULL,
425 valid_buffer, valid_size,
426 valid_buffer, valid_size,
427 valid_buffer, valid_size,
428 valid_buffer, &size_t_var,
429 valid_buffer, valid_size ) );
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100430 TEST_INVALID_PARAM_RET(
431 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
432 mbedtls_cipher_auth_decrypt( &valid_ctx,
433 NULL, valid_size,
434 valid_buffer, valid_size,
435 valid_buffer, valid_size,
436 valid_buffer, &size_t_var,
437 valid_buffer, valid_size ) );
438 TEST_INVALID_PARAM_RET(
439 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
440 mbedtls_cipher_auth_decrypt( &valid_ctx,
441 valid_buffer, valid_size,
442 NULL, valid_size,
443 valid_buffer, valid_size,
444 valid_buffer, &size_t_var,
445 valid_buffer, valid_size ) );
446 TEST_INVALID_PARAM_RET(
447 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
448 mbedtls_cipher_auth_decrypt( &valid_ctx,
449 valid_buffer, valid_size,
450 valid_buffer, valid_size,
451 NULL, valid_size,
452 valid_buffer, &size_t_var,
453 valid_buffer, valid_size ) );
454 TEST_INVALID_PARAM_RET(
455 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
456 mbedtls_cipher_auth_decrypt( &valid_ctx,
457 valid_buffer, valid_size,
458 valid_buffer, valid_size,
459 valid_buffer, valid_size,
460 NULL, &size_t_var,
461 valid_buffer, valid_size ) );
462 TEST_INVALID_PARAM_RET(
463 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
464 mbedtls_cipher_auth_decrypt( &valid_ctx,
465 valid_buffer, valid_size,
466 valid_buffer, valid_size,
467 valid_buffer, valid_size,
468 valid_buffer, NULL,
469 valid_buffer, valid_size ) );
470 TEST_INVALID_PARAM_RET(
471 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
472 mbedtls_cipher_auth_decrypt( &valid_ctx,
473 valid_buffer, valid_size,
474 valid_buffer, valid_size,
475 valid_buffer, valid_size,
476 valid_buffer, &size_t_var,
477 NULL, valid_size ) );
k-stachowiaka5390702018-12-17 11:27:03 +0100478#endif /* defined(MBEDTLS_CIPHER_MODE_AEAD) */
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100479
480 /* mbedtls_cipher_free() */
481 TEST_VALID_PARAM( mbedtls_cipher_free( NULL ) );
482exit:
Krzysztof Stachowiake0215d72018-12-17 10:20:30 +0100483 TEST_VALID_PARAM( mbedtls_cipher_free( &valid_ctx ) );
484}
485/* END_CASE */
486
Paul Bakker6a9c7252016-07-14 13:46:10 +0100487/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100488void cipher_special_behaviours( )
Paul Bakker6a9c7252016-07-14 13:46:10 +0100489{
490 const mbedtls_cipher_info_t *cipher_info;
491 mbedtls_cipher_context_t ctx;
492 unsigned char input[32];
493 unsigned char output[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300494#if defined (MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100495 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300496#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100497 size_t olen = 0;
498
499 mbedtls_cipher_init( &ctx );
500 memset( input, 0, sizeof( input ) );
501 memset( output, 0, sizeof( output ) );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300502#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100503 memset( iv, 0, sizeof( iv ) );
504
505 /* Check and get info structures */
Ron Eldor7b012442017-09-25 17:03:12 +0300506 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
Paul Bakker6a9c7252016-07-14 13:46:10 +0100507 TEST_ASSERT( NULL != cipher_info );
508
509 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
510
511 /* IV too big */
512 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
513 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
514
515 /* IV too small */
516 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
517 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
518
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300519 mbedtls_cipher_free( &ctx );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300520 mbedtls_cipher_init( &ctx );
Ron Eldor6f90ed82017-09-26 12:08:54 +0300521#endif /* MBEDTLS_CIPHER_MODE_CBC */
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300522 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
Ron Eldor7b012442017-09-25 17:03:12 +0300523 TEST_ASSERT( NULL != cipher_info );
524
525 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
526
Paul Bakker6a9c7252016-07-14 13:46:10 +0100527 /* Update ECB with partial block */
528 TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
529 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
530
531exit:
532 mbedtls_cipher_free( &ctx );
533}
534/* END_CASE */
535
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200536/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100537void enc_dec_buf( int cipher_id, char * cipher_string, int key_len,
Paul Bakker33b43f12013-08-20 11:48:36 +0200538 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200539{
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200540 size_t length = length_val, outlen, total_len, i, block_size;
Jaeden Amerod906b812018-06-08 11:03:16 +0100541 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000542 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200543 unsigned char ad[13];
544 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200545 unsigned char inbuf[64];
546 unsigned char encbuf[64];
547 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 const mbedtls_cipher_info_t *cipher_info;
550 mbedtls_cipher_context_t ctx_dec;
551 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000552
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200553 /*
554 * Prepare contexts
555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_cipher_init( &ctx_dec );
557 mbedtls_cipher_init( &ctx_enc );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200558
559 memset( key, 0x2a, sizeof( key ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000560
561 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000563 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000565
566 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200567 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
568 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
571 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200574 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
577 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200578 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200579#else
580 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200582
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200583 /*
584 * Do a few encode/decode cycles
585 */
586 for( i = 0; i < 3; i++ )
587 {
588 memset( iv , 0x00 + i, sizeof( iv ) );
589 memset( ad, 0x10 + i, sizeof( ad ) );
590 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
591
592 memset( encbuf, 0, sizeof( encbuf ) );
593 memset( decbuf, 0, sizeof( decbuf ) );
594 memset( tag, 0, sizeof( tag ) );
595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
597 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
600 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200601
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200602#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
604 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200605#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000606
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200607 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
608 TEST_ASSERT( block_size != 0 );
609
Paul Bakker8123e9d2011-01-06 15:37:30 +0000610 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200612 total_len = outlen;
613
614 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200615 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200616 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200617 total_len + block_size > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200620 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000621
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200622#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200624#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200625
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200626 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200627 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200628 total_len > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200629 total_len <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000630
631 /* decode the previously encoded string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200633 total_len = outlen;
634
635 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200636 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200637 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200638 total_len + block_size >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200641 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000642
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200643#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200645#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200646
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200647 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200648 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000649 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200650 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000651
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200652 /*
653 * Done
654 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200655exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_cipher_free( &ctx_dec );
657 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200658}
Paul Bakker33b43f12013-08-20 11:48:36 +0200659/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000660
Paul Bakker33b43f12013-08-20 11:48:36 +0200661/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100662void enc_fail( int cipher_id, int pad_mode, int key_len, int length_val,
663 int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200664{
Paul Bakker33b43f12013-08-20 11:48:36 +0200665 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200666 unsigned char key[32];
667 unsigned char iv[16];
668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 const mbedtls_cipher_info_t *cipher_info;
670 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200671
672 unsigned char inbuf[64];
673 unsigned char encbuf[64];
674
675 size_t outlen = 0;
676
677 memset( key, 0, 32 );
678 memset( iv , 0, 16 );
679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200681
682 memset( inbuf, 5, 64 );
683 memset( encbuf, 0, 64 );
684
685 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200687 TEST_ASSERT( NULL != cipher_info );
688
689 /* Initialise context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200690 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
692#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
693 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200694#else
695 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
697 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
698 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200699#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200701#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200702
703 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
705 TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200706
707 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200708exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200710}
Paul Bakker33b43f12013-08-20 11:48:36 +0200711/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200712
Paul Bakker33b43f12013-08-20 11:48:36 +0200713/* BEGIN_CASE */
Jaeden Amero4e47aa02019-06-05 15:35:08 +0100714void dec_empty_buf( int cipher )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200715{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000716 unsigned char key[32];
717 unsigned char iv[16];
718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200719 mbedtls_cipher_context_t ctx_dec;
720 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000721
722 unsigned char encbuf[64];
723 unsigned char decbuf[64];
724
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000725 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000726
Jaeden Amero4e47aa02019-06-05 15:35:08 +0100727 int expected_ret;
728
Paul Bakker8123e9d2011-01-06 15:37:30 +0000729 memset( key, 0, 32 );
730 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 mbedtls_cipher_init( &ctx_dec );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200733
Paul Bakker8123e9d2011-01-06 15:37:30 +0000734 memset( encbuf, 0, 64 );
735 memset( decbuf, 0, 64 );
736
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200737 /* Initialise context */
Jaeden Amero4e47aa02019-06-05 15:35:08 +0100738 cipher_info = mbedtls_cipher_info_from_type( cipher );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000739 TEST_ASSERT( NULL != cipher_info);
Jaeden Amero4e47aa02019-06-05 15:35:08 +0100740 TEST_ASSERT( sizeof(key) * 8 >= cipher_info->key_bitlen );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200741
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200742 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000743
Jaeden Amero4e47aa02019-06-05 15:35:08 +0100744 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec,
745 key, cipher_info->key_bitlen,
746 MBEDTLS_DECRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200751
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200752#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200754#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000755
756 /* decode 0-byte string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000758 TEST_ASSERT( 0 == outlen );
Jaeden Amero4e47aa02019-06-05 15:35:08 +0100759
760 if ( cipher_info->mode == MBEDTLS_MODE_CBC ||
761 cipher_info->mode == MBEDTLS_MODE_ECB )
762 {
763 /* CBC and ECB ciphers need a full block of input. */
764 expected_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
765 }
766 else
767 {
768 /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
769 * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
770 * decrypting an empty buffer. */
771 expected_ret = 0;
772 }
773
774 TEST_ASSERT( expected_ret == mbedtls_cipher_finish(
775 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000776 TEST_ASSERT( 0 == outlen );
777
Paul Bakkerbd51b262014-07-10 15:26:12 +0200778exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_cipher_free( &ctx_dec );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200780}
Paul Bakker33b43f12013-08-20 11:48:36 +0200781/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000782
Paul Bakker33b43f12013-08-20 11:48:36 +0200783/* BEGIN_CASE */
784void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700785 int second_length_val, int pad_mode,
786 int first_encrypt_output_len, int second_encrypt_output_len,
787 int first_decrypt_output_len, int second_decrypt_output_len )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200788{
Paul Bakker33b43f12013-08-20 11:48:36 +0200789 size_t first_length = first_length_val;
790 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000791 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200792 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000793 unsigned char key[32];
794 unsigned char iv[16];
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_cipher_context_t ctx_dec;
797 mbedtls_cipher_context_t ctx_enc;
798 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000799
800 unsigned char inbuf[64];
801 unsigned char encbuf[64];
802 unsigned char decbuf[64];
803
Paul Bakker23986e52011-04-24 08:57:21 +0000804 size_t outlen = 0;
805 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000806
807 memset( key, 0, 32 );
808 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_cipher_init( &ctx_dec );
811 mbedtls_cipher_init( &ctx_enc );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200812
Paul Bakker8123e9d2011-01-06 15:37:30 +0000813 memset( inbuf, 5, 64 );
814 memset( encbuf, 0, 64 );
815 memset( decbuf, 0, 64 );
816
817 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000819 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200820
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200821 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
822 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
825 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000826
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700827#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
828 if( -1 != pad_mode )
829 {
830 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
831 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
832 }
833#else
834 (void) pad_mode;
835#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
838 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
841 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200842
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200843#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
845 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200846#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000847
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200848 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
849 TEST_ASSERT( block_size != 0 );
850
Paul Bakker8123e9d2011-01-06 15:37:30 +0000851 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700853 TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000854 totaloutlen = outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700856 TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000857 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200858 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200859 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200860 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200861 totaloutlen + block_size > length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000864 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200865 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200866 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200867 totaloutlen > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200868 totaloutlen <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000869
870 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700871 second_length = totaloutlen - first_length;
872 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
873 TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200874 totaloutlen = outlen;
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700875 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
876 TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
877 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200878
879 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200880 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200881 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200882 totaloutlen + block_size >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200883
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700884 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200885 totaloutlen += outlen;
886
887 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000888
889 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
890
Paul Bakkerbd51b262014-07-10 15:26:12 +0200891exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 mbedtls_cipher_free( &ctx_dec );
893 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200894}
Paul Bakker33b43f12013-08-20 11:48:36 +0200895/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000896
Paul Bakker33b43f12013-08-20 11:48:36 +0200897/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100898void decrypt_test_vec( int cipher_id, int pad_mode, data_t * key,
899 data_t * iv, data_t * cipher,
900 data_t * clear, data_t * ad, data_t * tag,
Azim Khand30ca132017-06-09 04:32:58 +0100901 int finish_result, int tag_result )
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200902{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200903 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200905 size_t outlen, total_len;
906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200908
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200909 memset( output, 0x00, sizeof( output ) );
910
Azim Khanf1aaec92017-05-30 14:23:15 +0100911#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100912 ((void) ad);
913 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200914#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200915
916 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200917 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_cipher_info_from_type( cipher_id ) ) );
Azim Khand30ca132017-06-09 04:32:58 +0100919 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200921 if( pad_mode != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200922 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200923#else
924 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Azim Khand30ca132017-06-09 04:32:58 +0100926 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv->x, iv->len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200928#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100929 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad->x, ad->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200930#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200931
Azim Khand30ca132017-06-09 04:32:58 +0100932 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200933 total_len = 0;
Azim Khand30ca132017-06-09 04:32:58 +0100934 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher->x, cipher->len, output, &outlen ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200935 total_len += outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200937 &outlen ) );
938 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200939#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100940 TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag->x, tag->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200941#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200942
943 /* check plaintext only if everything went fine */
944 if( 0 == finish_result && 0 == tag_result )
945 {
Azim Khand30ca132017-06-09 04:32:58 +0100946 TEST_ASSERT( total_len == clear->len );
947 TEST_ASSERT( 0 == memcmp( output, clear->x, clear->len ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200948 }
949
Paul Bakkerbd51b262014-07-10 15:26:12 +0200950exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200952}
953/* END_CASE */
954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */
Azim Khan5fcca462018-06-29 11:05:32 +0100956void auth_crypt_tv( int cipher_id, data_t * key, data_t * iv,
957 data_t * ad, data_t * cipher, data_t * tag,
958 char * result, data_t * clear )
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200959{
960 int ret;
Manuel Pégourié-Gonnard69767d12018-05-09 12:25:18 +0200961 unsigned char output[267]; /* above + 2 (overwrite check) */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200962 unsigned char my_tag[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200964 size_t outlen;
965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200967
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200968 memset( output, 0xFF, sizeof( output ) );
Azim Khan46c9b1f2017-05-31 20:46:35 +0100969 memset( my_tag, 0xFF, sizeof( my_tag ) );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200970
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200971
972 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200973 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 mbedtls_cipher_info_from_type( cipher_id ) ) );
Azim Khand30ca132017-06-09 04:32:58 +0100975 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200976
Azim Khand30ca132017-06-09 04:32:58 +0100977 /* decode buffer and check tag->x */
978 ret = mbedtls_cipher_auth_decrypt( &ctx, iv->x, iv->len, ad->x, ad->len,
979 cipher->x, cipher->len, output, &outlen,
980 tag->x, tag->len );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200981
982 /* make sure we didn't overwrite */
983 TEST_ASSERT( output[outlen + 0] == 0xFF );
984 TEST_ASSERT( output[outlen + 1] == 0xFF );
985
986 /* make sure the message is rejected if it should be */
Azim Khan46c9b1f2017-05-31 20:46:35 +0100987 if( strcmp( result, "FAIL" ) == 0 )
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200988 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200990 goto exit;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200991 }
992
993 /* otherwise, make sure it was decrypted properly */
994 TEST_ASSERT( ret == 0 );
995
Azim Khand30ca132017-06-09 04:32:58 +0100996 TEST_ASSERT( outlen == clear->len );
997 TEST_ASSERT( memcmp( output, clear->x, clear->len ) == 0 );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200998
Azim Khand30ca132017-06-09 04:32:58 +0100999 /* then encrypt the clear->x and make sure we get the same ciphertext and tag->x */
Jack Lloydb25719b2019-03-07 16:59:14 -05001000 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len,
1001 MBEDTLS_ENCRYPT ) );
1002
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001003 memset( output, 0xFF, sizeof( output ) );
1004 outlen = 0;
1005
Azim Khand30ca132017-06-09 04:32:58 +01001006 ret = mbedtls_cipher_auth_encrypt( &ctx, iv->x, iv->len, ad->x, ad->len,
1007 clear->x, clear->len, output, &outlen,
1008 my_tag, tag->len );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001009 TEST_ASSERT( ret == 0 );
1010
Jack Lloydb25719b2019-03-07 16:59:14 -05001011 TEST_ASSERT( outlen == cipher->len );
1012 TEST_ASSERT( memcmp( output, cipher->x, cipher->len ) == 0 );
Azim Khand30ca132017-06-09 04:32:58 +01001013 TEST_ASSERT( memcmp( my_tag, tag->x, tag->len ) == 0 );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001014
1015 /* make sure we didn't overwrite */
1016 TEST_ASSERT( output[outlen + 0] == 0xFF );
1017 TEST_ASSERT( output[outlen + 1] == 0xFF );
Azim Khand30ca132017-06-09 04:32:58 +01001018 TEST_ASSERT( my_tag[tag->len + 0] == 0xFF );
1019 TEST_ASSERT( my_tag[tag->len + 1] == 0xFF );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001020
1021
Paul Bakkerbd51b262014-07-10 15:26:12 +02001022exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001024}
1025/* END_CASE */
1026
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +02001027/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +01001028void test_vec_ecb( int cipher_id, int operation, data_t * key,
1029 data_t * input, data_t * result, int finish_result
Azim Khand30ca132017-06-09 04:32:58 +01001030 )
Paul Bakker5e0efa72013-09-08 23:04:04 +02001031{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001033 unsigned char output[32];
1034 size_t outlen;
1035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001037
Paul Bakker5e0efa72013-09-08 23:04:04 +02001038 memset( output, 0x00, sizeof( output ) );
1039
1040 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001041 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 mbedtls_cipher_info_from_type( cipher_id ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001043
Paul Bakker5e0efa72013-09-08 23:04:04 +02001044
Azim Khand30ca132017-06-09 04:32:58 +01001045 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001046
Azim Khand30ca132017-06-09 04:32:58 +01001047 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 mbedtls_cipher_get_block_size( &ctx ),
Paul Bakker5e0efa72013-09-08 23:04:04 +02001049 output, &outlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
1051 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001052 &outlen ) );
1053 TEST_ASSERT( 0 == outlen );
1054
1055 /* check plaintext only if everything went fine */
1056 if( 0 == finish_result )
Azim Khand30ca132017-06-09 04:32:58 +01001057 TEST_ASSERT( 0 == memcmp( output, result->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 mbedtls_cipher_get_block_size( &ctx ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001059
Paul Bakkerbd51b262014-07-10 15:26:12 +02001060exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 mbedtls_cipher_free( &ctx );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001062}
1063/* END_CASE */
1064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Ronald Cron14a56452020-06-25 09:03:34 +02001066void test_vec_crypt( int cipher_id, int operation, data_t *key,
1067 data_t *iv, data_t *input, data_t *result,
1068 int finish_result )
Ron Eldor7b012442017-09-25 17:03:12 +03001069{
Ron Eldor7b012442017-09-25 17:03:12 +03001070 mbedtls_cipher_context_t ctx;
1071 unsigned char output[32];
1072 size_t outlen;
1073
1074 mbedtls_cipher_init( &ctx );
1075
Ron Eldor7b012442017-09-25 17:03:12 +03001076 memset( output, 0x00, sizeof( output ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001077
1078 /* Prepare context */
1079 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
1080 mbedtls_cipher_info_from_type( cipher_id ) ) );
1081
Ronald Cron14a56452020-06-25 09:03:34 +02001082 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001083 if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode )
1084 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) );
1085
Ronald Cron14a56452020-06-25 09:03:34 +02001086 TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv->len ? iv->x : NULL,
1087 iv->len, input->x, input->len,
Ron Eldor7b012442017-09-25 17:03:12 +03001088 output, &outlen ) );
Ronald Cron14a56452020-06-25 09:03:34 +02001089 TEST_ASSERT( result->len == outlen );
Ron Eldor7b012442017-09-25 17:03:12 +03001090 /* check plaintext only if everything went fine */
1091 if( 0 == finish_result )
Ronald Cron14a56452020-06-25 09:03:34 +02001092 TEST_ASSERT( 0 == memcmp( output, result->x, outlen ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001093
1094exit:
1095 mbedtls_cipher_free( &ctx );
1096}
1097/* END_CASE */
1098
1099/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +02001100void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001101{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 const mbedtls_cipher_info_t *cipher_info;
1103 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001108 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001109 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001112
Paul Bakkerbd51b262014-07-10 15:26:12 +02001113exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001115}
Paul Bakker33b43f12013-08-20 11:48:36 +02001116/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +01001119void check_padding( int pad_mode, data_t * input, int ret, int dlen_check
Azim Khand30ca132017-06-09 04:32:58 +01001120 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001121{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 mbedtls_cipher_info_t cipher_info;
1123 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +01001124 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001125
1126 /* build a fake context just for getting access to get_padding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 mbedtls_cipher_init( &ctx );
1128 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001129 ctx.cipher_info = &cipher_info;
1130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001132
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001133
Azim Khand30ca132017-06-09 04:32:58 +01001134 TEST_ASSERT( ret == ctx.get_padding( input->x, input->len, &dlen ) );
Paul Bakker33b43f12013-08-20 11:48:36 +02001135 if( 0 == ret )
1136 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001137}
Paul Bakker33b43f12013-08-20 11:48:36 +02001138/* END_CASE */