blob: aa2849bc85190f25ef2dbf91b7542d3632cdbcbd [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"
k-stachowiakd8727232019-07-29 17:46:29 +02003#include "mbedtls/aes.h"
k-stachowiakd8727232019-07-29 17:46:29 +02004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00006#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +02007#endif
Gilles Peskine5386f6b2019-08-01 12:47:40 +02008
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +01009#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
10#define MBEDTLS_CIPHER_AUTH_CRYPT
11#endif
12
Gilles Peskine0be02bd2021-07-19 16:32:54 +020013/* Check the internal consistency of a cipher info structure, and
14 * check it against mbedtls_cipher_info_from_xxx(). */
Gilles Peskine449bd832023-01-11 14:50:10 +010015static int check_cipher_info(mbedtls_cipher_type_t type,
16 const mbedtls_cipher_info_t *info)
Gilles Peskine0be02bd2021-07-19 16:32:54 +020017{
Max Fillinger72abd8a2021-11-28 14:02:36 +010018 size_t key_bitlen, block_size, iv_size;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020019
Gilles Peskine449bd832023-01-11 14:50:10 +010020 TEST_ASSERT(info != NULL);
21 TEST_EQUAL(type, mbedtls_cipher_info_get_type(info));
22 TEST_EQUAL(type, info->type);
23 TEST_ASSERT(mbedtls_cipher_info_from_type(type) == info);
Gilles Peskine0be02bd2021-07-19 16:32:54 +020024
Gilles Peskine449bd832023-01-11 14:50:10 +010025 TEST_EQUAL(info->mode, mbedtls_cipher_info_get_mode(info));
Gilles Peskine0be02bd2021-07-19 16:32:54 +020026
27 /* Insist that get_name() return the string from the structure and
28 * not a copy. A copy would have an unknown storage duration. */
Gilles Peskine449bd832023-01-11 14:50:10 +010029 TEST_ASSERT(mbedtls_cipher_info_get_name(info) == info->name);
30 TEST_ASSERT(mbedtls_cipher_info_from_string(info->name) == info);
Gilles Peskine0be02bd2021-07-19 16:32:54 +020031
Gilles Peskine449bd832023-01-11 14:50:10 +010032 key_bitlen = mbedtls_cipher_info_get_key_bitlen(info);
33 block_size = mbedtls_cipher_info_get_block_size(info);
34 iv_size = mbedtls_cipher_info_get_iv_size(info);
35 if (info->type == MBEDTLS_CIPHER_NULL) {
36 TEST_ASSERT(key_bitlen == 0);
37 TEST_ASSERT(block_size == 1);
38 TEST_ASSERT(iv_size == 0);
39 } else if (info->mode == MBEDTLS_MODE_XTS) {
40 TEST_ASSERT(key_bitlen == 256 ||
41 key_bitlen == 384 ||
42 key_bitlen == 512);
43 } else if (!strncmp(info->name, "DES-EDE3-", 9)) {
44 TEST_ASSERT(key_bitlen == 192);
45 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
46 TEST_ASSERT(block_size == 8);
47 } else if (!strncmp(info->name, "DES-EDE-", 8)) {
48 TEST_ASSERT(key_bitlen == 128);
49 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
50 TEST_ASSERT(block_size == 8);
51 } else if (!strncmp(info->name, "DES-", 4)) {
52 TEST_ASSERT(key_bitlen == 64);
53 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
54 TEST_ASSERT(block_size == 8);
55 } else if (!strncmp(info->name, "AES", 3)) {
56 TEST_ASSERT(key_bitlen == 128 ||
57 key_bitlen == 192 ||
58 key_bitlen == 256);
59 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
60 TEST_ASSERT(block_size == 16);
61 } else {
62 TEST_ASSERT(key_bitlen == 128 ||
63 key_bitlen == 192 ||
64 key_bitlen == 256);
Gilles Peskine6ac8f942021-09-01 08:31:49 +020065 }
Gilles Peskine0be02bd2021-07-19 16:32:54 +020066
Gilles Peskine449bd832023-01-11 14:50:10 +010067 if (strstr(info->name, "-ECB") != NULL) {
68 TEST_ASSERT(iv_size == 0);
69 TEST_ASSERT(!mbedtls_cipher_info_has_variable_iv_size(info));
70 } else if (strstr(info->name, "-CBC") != NULL ||
71 strstr(info->name, "-CTR") != NULL) {
72 TEST_ASSERT(iv_size == block_size);
73 TEST_ASSERT(!mbedtls_cipher_info_has_variable_iv_size(info));
74 } else if (strstr(info->name, "-GCM") != NULL) {
75 TEST_ASSERT(iv_size == block_size - 4);
76 TEST_ASSERT(mbedtls_cipher_info_has_variable_iv_size(info));
Max Fillingerc60c3a02021-11-09 22:38:56 +010077 }
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079 return 1;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020080
81exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010082 return 0;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020083}
84
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010085#if defined(MBEDTLS_CIPHER_AUTH_CRYPT)
86/* Helper for resetting key/direction
87 *
88 * The documentation doesn't explicitly say whether calling
89 * mbedtls_cipher_setkey() twice is allowed or not. This currently works with
90 * the default software implementation, but only by accident. It isn't
91 * guaranteed to work with new ciphers or with alternative implementations of
92 * individual ciphers, and it doesn't work with the PSA wrappers. So don't do
93 * it, and instead start with a fresh context.
94 */
Gilles Peskine449bd832023-01-11 14:50:10 +010095static int cipher_reset_key(mbedtls_cipher_context_t *ctx, int cipher_id,
96 int use_psa, size_t tag_len, const data_t *key, int direction)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010097{
Gilles Peskine449bd832023-01-11 14:50:10 +010098 mbedtls_cipher_free(ctx);
99 mbedtls_cipher_init(ctx);
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100100
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -0400101#if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100102 (void) use_psa;
103 (void) tag_len;
104#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 if (use_psa == 1) {
106 TEST_ASSERT(0 == mbedtls_cipher_setup_psa(ctx,
107 mbedtls_cipher_info_from_type(cipher_id),
108 tag_len));
109 } else
Przemek Stekiel476d9c42022-05-19 12:26:33 +0200110#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED */
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100111 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 TEST_ASSERT(0 == mbedtls_cipher_setup(ctx,
113 mbedtls_cipher_info_from_type(cipher_id)));
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100114 }
115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 TEST_ASSERT(0 == mbedtls_cipher_setkey(ctx, key->x, 8 * key->len,
117 direction));
118 return 1;
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100119
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100120exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 return 0;
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100122}
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100123
124/*
125 * Check if a buffer is all-0 bytes:
126 * return 1 if it is,
127 * 0 if it isn't.
128 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100129int buffer_is_all_zero(const uint8_t *buf, size_t size)
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100130{
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 for (size_t i = 0; i < size; i++) {
132 if (buf[i] != 0) {
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100133 return 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 }
135 }
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100136 return 1;
137}
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100138#endif /* MBEDTLS_CIPHER_AUTH_CRYPT */
139
Paul Bakker33b43f12013-08-20 11:48:36 +0200140/* END_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000141
Paul Bakker33b43f12013-08-20 11:48:36 +0200142/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 * depends_on:MBEDTLS_CIPHER_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200144 * END_DEPENDENCIES
145 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000146
Paul Bakker33b43f12013-08-20 11:48:36 +0200147/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148void mbedtls_cipher_list()
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100149{
150 const int *cipher_type;
151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 for (cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++) {
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200153 const mbedtls_cipher_info_t *info =
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 mbedtls_cipher_info_from_type(*cipher_type);
155 mbedtls_test_set_step(*cipher_type);
156 if (!check_cipher_info(*cipher_type, info)) {
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200157 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 }
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200159 }
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100160}
161/* END_CASE */
162
163/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164void cipher_invalid_param_unconditional()
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200165{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500166 mbedtls_cipher_context_t valid_ctx;
167 mbedtls_cipher_context_t invalid_ctx;
168 mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
169 mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
170 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
171 int valid_size = sizeof(valid_buffer);
172 int valid_bitlen = valid_size * 8;
173 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 *(mbedtls_cipher_list()));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500175 size_t size_t_var;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 (void) valid_mode; /* In some configurations this is unused */
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 mbedtls_cipher_init(&valid_ctx);
180 mbedtls_cipher_init(&invalid_ctx);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, valid_info) == 0);
Gilles Peskinedc8ecda2021-12-10 14:28:31 +0100183
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500184 /* mbedtls_cipher_setup() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, NULL) ==
186 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200187
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500188 /* mbedtls_cipher_get_block_size() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 TEST_ASSERT(mbedtls_cipher_get_block_size(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200190
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500191 /* mbedtls_cipher_get_cipher_mode() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 TEST_ASSERT(mbedtls_cipher_get_cipher_mode(&invalid_ctx) ==
193 MBEDTLS_MODE_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200194
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500195 /* mbedtls_cipher_get_iv_size() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 TEST_ASSERT(mbedtls_cipher_get_iv_size(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200197
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500198 /* mbedtls_cipher_get_type() */
199 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 mbedtls_cipher_get_type(&invalid_ctx) ==
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500201 MBEDTLS_CIPHER_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200202
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500203 /* mbedtls_cipher_get_name() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 TEST_ASSERT(mbedtls_cipher_get_name(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200205
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500206 /* mbedtls_cipher_get_key_bitlen() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 TEST_ASSERT(mbedtls_cipher_get_key_bitlen(&invalid_ctx) ==
208 MBEDTLS_KEY_LENGTH_NONE);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500209
210 /* mbedtls_cipher_get_operation() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 TEST_ASSERT(mbedtls_cipher_get_operation(&invalid_ctx) ==
212 MBEDTLS_OPERATION_NONE);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500213
214 /* mbedtls_cipher_setkey() */
215 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 mbedtls_cipher_setkey(&invalid_ctx,
217 valid_buffer,
218 valid_bitlen,
219 valid_operation) ==
220 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500221
222 /* mbedtls_cipher_set_iv() */
223 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 mbedtls_cipher_set_iv(&invalid_ctx,
225 valid_buffer,
226 valid_size) ==
227 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500228
229 /* mbedtls_cipher_reset() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 TEST_ASSERT(mbedtls_cipher_reset(&invalid_ctx) ==
231 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200232
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200233#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500234 /* mbedtls_cipher_update_ad() */
235 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 mbedtls_cipher_update_ad(&invalid_ctx,
237 valid_buffer,
238 valid_size) ==
239 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500240#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
241
242#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
243 /* mbedtls_cipher_set_padding_mode() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 TEST_ASSERT(mbedtls_cipher_set_padding_mode(&invalid_ctx, valid_mode) ==
245 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200246#endif
247
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500248 /* mbedtls_cipher_update() */
249 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 mbedtls_cipher_update(&invalid_ctx,
251 valid_buffer,
252 valid_size,
253 valid_buffer,
254 &size_t_var) ==
255 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200256
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500257 /* mbedtls_cipher_finish() */
258 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 mbedtls_cipher_finish(&invalid_ctx,
260 valid_buffer,
261 &size_t_var) ==
262 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200263
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200264#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500265 /* mbedtls_cipher_write_tag() */
266 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 mbedtls_cipher_write_tag(&invalid_ctx,
268 valid_buffer,
269 valid_size) ==
270 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200271
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500272 /* mbedtls_cipher_check_tag() */
273 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 mbedtls_cipher_check_tag(&invalid_ctx,
275 valid_buffer,
276 valid_size) ==
277 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500278#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
279
280exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 mbedtls_cipher_free(&invalid_ctx);
282 mbedtls_cipher_free(&valid_ctx);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500283}
284/* END_CASE */
285
Tuvshinzaya Erdenekhuu6c689272022-07-29 14:45:55 +0100286/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100287void cipher_invalid_param_conditional()
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500288{
289 mbedtls_cipher_context_t valid_ctx;
290
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500291 mbedtls_operation_t invalid_operation = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500292 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
293 int valid_size = sizeof(valid_buffer);
294 int valid_bitlen = valid_size * 8;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500295
Ronald Cron875b5fb2021-05-21 08:50:00 +0200296 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500297 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 mbedtls_cipher_setkey(&valid_ctx,
299 valid_buffer,
300 valid_bitlen,
301 invalid_operation));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500302
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500303exit:
TRodziewicz70199552021-05-27 13:52:59 +0200304 ;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200305}
306/* END_CASE */
307
Paul Bakker6a9c7252016-07-14 13:46:10 +0100308/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100309void cipher_special_behaviours()
Paul Bakker6a9c7252016-07-14 13:46:10 +0100310{
311 const mbedtls_cipher_info_t *cipher_info;
312 mbedtls_cipher_context_t ctx;
313 unsigned char input[32];
314 unsigned char output[32];
Gilles Peskine449bd832023-01-11 14:50:10 +0100315#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100316 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300317#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100318 size_t olen = 0;
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 mbedtls_cipher_init(&ctx);
321 memset(input, 0, sizeof(input));
322 memset(output, 0, sizeof(output));
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300323#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 memset(iv, 0, sizeof(iv));
Paul Bakker6a9c7252016-07-14 13:46:10 +0100325
326 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_CBC);
328 TEST_ASSERT(NULL != cipher_info);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Paul Bakker6a9c7252016-07-14 13:46:10 +0100331
332 /* IV too big */
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1)
334 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100335
336 /* IV too small */
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, 0)
338 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 mbedtls_cipher_free(&ctx);
341 mbedtls_cipher_init(&ctx);
Ron Eldor6f90ed82017-09-26 12:08:54 +0300342#endif /* MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB);
344 TEST_ASSERT(NULL != cipher_info);
Ron Eldor7b012442017-09-25 17:03:12 +0300345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Ron Eldor7b012442017-09-25 17:03:12 +0300347
Paul Bakker6a9c7252016-07-14 13:46:10 +0100348 /* Update ECB with partial block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 TEST_ASSERT(mbedtls_cipher_update(&ctx, input, 1, output, &olen)
350 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100351
352exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 mbedtls_cipher_free(&ctx);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100354}
355/* END_CASE */
356
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200357/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358void enc_dec_buf(int cipher_id, char *cipher_string, int key_len,
359 int length_val, int pad_mode)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200360{
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200361 size_t length = length_val, outlen, total_len, i, block_size, iv_len;
Jaeden Amerod906b812018-06-08 11:03:16 +0100362 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000363 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200364 unsigned char ad[13];
365 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200366 unsigned char inbuf[64];
367 unsigned char encbuf[64];
368 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 const mbedtls_cipher_info_t *cipher_info;
371 mbedtls_cipher_context_t ctx_dec;
372 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000373
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200374 /*
375 * Prepare contexts
376 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 mbedtls_cipher_init(&ctx_dec);
378 mbedtls_cipher_init(&ctx_enc);
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 memset(key, 0x2a, sizeof(key));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000381
382 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
384 TEST_ASSERT(NULL != cipher_info);
385 TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info);
386 TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info),
387 cipher_string) == 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000388
389 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
391 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT));
394 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 if (-1 != pad_mode) {
398 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode));
399 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode));
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200400 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200401#else
402 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200404
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200405 /*
406 * Do a few encode/decode cycles
407 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 for (i = 0; i < 3; i++) {
409 memset(iv, 0x00 + i, sizeof(iv));
410 memset(ad, 0x10 + i, sizeof(ad));
411 memset(inbuf, 0x20 + i, sizeof(inbuf));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 memset(encbuf, 0, sizeof(encbuf));
414 memset(decbuf, 0, sizeof(decbuf));
415 memset(tag, 0, sizeof(tag));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) {
418 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
419 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
420 } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
421 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
422 iv_len = 12;
423 } else {
424 iv_len = sizeof(iv);
425 }
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
428 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
431 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200432
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200433#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
435 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
436 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, ad, sizeof(ad) - i));
439 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, ad, sizeof(ad) - i));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200440#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 block_size = mbedtls_cipher_get_block_size(&ctx_enc);
443 TEST_ASSERT(block_size != 0);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 /* encode length number of bytes from inbuf */
446 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, length, encbuf, &outlen));
447 total_len = outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 TEST_ASSERT(total_len == length ||
450 (total_len % block_size == 0 &&
451 total_len < length &&
452 total_len + block_size > length));
Paul Bakker343a8702011-06-09 14:27:58 +0000453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + outlen, &outlen));
455 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000456
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200457#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 TEST_EQUAL(expected, mbedtls_cipher_write_tag(&ctx_enc, tag, sizeof(tag)));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200459#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 TEST_ASSERT(total_len == length ||
462 (total_len % block_size == 0 &&
463 total_len > length &&
464 total_len <= length + block_size));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 /* decode the previously encoded string */
467 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, total_len, decbuf, &outlen));
468 total_len = outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 TEST_ASSERT(total_len == length ||
471 (total_len % block_size == 0 &&
472 total_len < length &&
473 total_len + block_size >= length));
Paul Bakker343a8702011-06-09 14:27:58 +0000474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + outlen, &outlen));
476 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000477
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200478#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 TEST_EQUAL(expected, mbedtls_cipher_check_tag(&ctx_dec, tag, sizeof(tag)));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200480#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 /* check result */
483 TEST_ASSERT(total_len == length);
484 TEST_ASSERT(0 == memcmp(inbuf, decbuf, length));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200485 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000486
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200487 /*
488 * Done
489 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200490exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 mbedtls_cipher_free(&ctx_dec);
492 mbedtls_cipher_free(&ctx_enc);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200493}
Paul Bakker33b43f12013-08-20 11:48:36 +0200494/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000495
Paul Bakker33b43f12013-08-20 11:48:36 +0200496/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497void enc_fail(int cipher_id, int pad_mode, int key_len, int length_val,
498 int ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200499{
Paul Bakker33b43f12013-08-20 11:48:36 +0200500 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200501 unsigned char key[32];
502 unsigned char iv[16];
503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 const mbedtls_cipher_info_t *cipher_info;
505 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200506
507 unsigned char inbuf[64];
508 unsigned char encbuf[64];
509
510 size_t outlen = 0;
511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 memset(key, 0, 32);
513 memset(iv, 0, 16);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 memset(inbuf, 5, 64);
518 memset(encbuf, 0, 64);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200519
520 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
522 TEST_ASSERT(NULL != cipher_info);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200523
524 /* Initialise context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
526 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key, key_len, MBEDTLS_ENCRYPT));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200529#else
530 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv, 16));
533 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200534#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
536 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100537 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200540#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200541
542 /* encode length number of bytes from inbuf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, inbuf, length, encbuf, &outlen));
544 TEST_ASSERT(ret == mbedtls_cipher_finish(&ctx, encbuf + outlen, &outlen));
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200545
546 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200547exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 mbedtls_cipher_free(&ctx);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200549}
Paul Bakker33b43f12013-08-20 11:48:36 +0200550/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200551
Paul Bakker33b43f12013-08-20 11:48:36 +0200552/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100553void dec_empty_buf(int cipher,
554 int expected_update_ret,
555 int expected_finish_ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200556{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000557 unsigned char key[32];
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100558
559 unsigned char *iv = NULL;
560 size_t iv_len = 16;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_cipher_context_t ctx_dec;
563 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000564
565 unsigned char encbuf[64];
566 unsigned char decbuf[64];
567
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000568 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 memset(key, 0, 32);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 mbedtls_cipher_init(&ctx_dec);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 memset(encbuf, 0, 64);
575 memset(decbuf, 0, 64);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000576
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200577 /* Initialise context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 cipher_info = mbedtls_cipher_info_from_type(cipher);
579 TEST_ASSERT(NULL != cipher_info);
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
582 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100583 iv_len = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 ASSERT_ALLOC(iv, iv_len);
587 memset(iv, 0, iv_len);
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100588
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100589 TEST_ASSERT(sizeof(key) * 8 >= mbedtls_cipher_info_get_key_bitlen(cipher_info));
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec,
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100594 key, mbedtls_cipher_info_get_key_bitlen(cipher_info),
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 MBEDTLS_DECRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
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)
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
603 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100604 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200607#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000608
609 /* decode 0-byte string */
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 TEST_ASSERT(expected_update_ret ==
611 mbedtls_cipher_update(&ctx_dec, encbuf, 0, decbuf, &outlen));
612 TEST_ASSERT(0 == outlen);
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if (expected_finish_ret == 0 &&
615 (cipher_info->mode == MBEDTLS_MODE_CBC ||
616 cipher_info->mode == MBEDTLS_MODE_ECB)) {
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100617 /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
618 * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
k-stachowiakd8727232019-07-29 17:46:29 +0200619 * decrypting an empty buffer.
620 * On the other hand, CBC and ECB ciphers need a full block of input.
621 */
622 expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100623 }
624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 TEST_ASSERT(expected_finish_ret == mbedtls_cipher_finish(
626 &ctx_dec, decbuf + outlen, &outlen));
627 TEST_ASSERT(0 == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000628
Paul Bakkerbd51b262014-07-10 15:26:12 +0200629exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 mbedtls_free(iv);
631 mbedtls_cipher_free(&ctx_dec);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200632}
Paul Bakker33b43f12013-08-20 11:48:36 +0200633/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000634
Paul Bakker33b43f12013-08-20 11:48:36 +0200635/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100636void enc_dec_buf_multipart(int cipher_id, int key_len, int first_length_val,
637 int second_length_val, int pad_mode,
638 int first_encrypt_output_len, int second_encrypt_output_len,
639 int first_decrypt_output_len, int second_decrypt_output_len)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200640{
Paul Bakker33b43f12013-08-20 11:48:36 +0200641 size_t first_length = first_length_val;
642 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000643 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200644 size_t block_size;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200645 size_t iv_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000646 unsigned char key[32];
647 unsigned char iv[16];
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_cipher_context_t ctx_dec;
650 mbedtls_cipher_context_t ctx_enc;
651 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000652
653 unsigned char inbuf[64];
654 unsigned char encbuf[64];
655 unsigned char decbuf[64];
656
Paul Bakker23986e52011-04-24 08:57:21 +0000657 size_t outlen = 0;
658 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 memset(key, 0, 32);
661 memset(iv, 0, 16);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 mbedtls_cipher_init(&ctx_dec);
664 mbedtls_cipher_init(&ctx_enc);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 memset(inbuf, 5, 64);
667 memset(encbuf, 0, 64);
668 memset(decbuf, 0, 64);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000669
670 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
672 TEST_ASSERT(NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
675 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT));
678 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000679
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700680#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 if (-1 != pad_mode) {
682 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode));
683 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode));
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700684 }
685#else
686 (void) pad_mode;
687#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) {
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200690 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
691 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
693 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100694 iv_len = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 } else {
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200696 iv_len = sizeof(iv);
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 }
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
700 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
703 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200704
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200705#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
707 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100708 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0));
711 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200712#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 block_size = mbedtls_cipher_get_block_size(&ctx_enc);
715 TEST_ASSERT(block_size != 0);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200716
Paul Bakker8123e9d2011-01-06 15:37:30 +0000717 /* encode length number of bytes from inbuf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, first_length, encbuf, &outlen));
719 TEST_ASSERT((size_t) first_encrypt_output_len == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000720 totaloutlen = outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 TEST_ASSERT(0 ==
722 mbedtls_cipher_update(&ctx_enc, inbuf + first_length, second_length,
723 encbuf + totaloutlen,
724 &outlen));
725 TEST_ASSERT((size_t) second_encrypt_output_len == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000726 totaloutlen += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 TEST_ASSERT(totaloutlen == length ||
728 (totaloutlen % block_size == 0 &&
729 totaloutlen < length &&
730 totaloutlen + block_size > length));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + totaloutlen, &outlen));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000733 totaloutlen += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 TEST_ASSERT(totaloutlen == length ||
735 (totaloutlen % block_size == 0 &&
736 totaloutlen > length &&
737 totaloutlen <= length + block_size));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000738
739 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700740 second_length = totaloutlen - first_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, first_length, decbuf, &outlen));
742 TEST_ASSERT((size_t) first_decrypt_output_len == outlen);
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200743 totaloutlen = outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 TEST_ASSERT(0 ==
745 mbedtls_cipher_update(&ctx_dec, encbuf + first_length, second_length,
746 decbuf + totaloutlen,
747 &outlen));
748 TEST_ASSERT((size_t) second_decrypt_output_len == outlen);
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700749 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 TEST_ASSERT(totaloutlen == length ||
752 (totaloutlen % block_size == 0 &&
753 totaloutlen < length &&
754 totaloutlen + block_size >= length));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + totaloutlen, &outlen));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200757 totaloutlen += outlen;
758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 TEST_ASSERT(totaloutlen == length);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 TEST_ASSERT(0 == memcmp(inbuf, decbuf, length));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000762
Paul Bakkerbd51b262014-07-10 15:26:12 +0200763exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 mbedtls_cipher_free(&ctx_dec);
765 mbedtls_cipher_free(&ctx_enc);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200766}
Paul Bakker33b43f12013-08-20 11:48:36 +0200767/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000768
Paul Bakker33b43f12013-08-20 11:48:36 +0200769/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100770void decrypt_test_vec(int cipher_id, int pad_mode, data_t *key,
771 data_t *iv, data_t *cipher,
772 data_t *clear, data_t *ad, data_t *tag,
773 int finish_result, int tag_result)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200774{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200775 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200777 size_t outlen, total_len;
778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 memset(output, 0x00, sizeof(output));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200782
Azim Khanf1aaec92017-05-30 14:23:15 +0100783#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100784 ((void) ad);
785 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200786#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200787
788 /* Prepare context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
790 mbedtls_cipher_info_from_type(cipher_id)));
791 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (pad_mode != -1) {
794 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
795 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200796#else
797 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv->x, iv->len));
800 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200801#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 int expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
803 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100804 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, ad->x, ad->len));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200807#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200808
Azim Khand30ca132017-06-09 04:32:58 +0100809 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200810 total_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, cipher->x, cipher->len, output, &outlen));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200812 total_len += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen,
814 &outlen));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200815 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200816#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 int tag_expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
818 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100819 tag_result : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 TEST_EQUAL(tag_expected, mbedtls_cipher_check_tag(&ctx, tag->x, tag->len));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200822#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200823
824 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (0 == finish_result && 0 == tag_result) {
826 TEST_ASSERT(total_len == clear->len);
827 TEST_ASSERT(0 == memcmp(output, clear->x, clear->len));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200828 }
829
Paul Bakkerbd51b262014-07-10 15:26:12 +0200830exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 mbedtls_cipher_free(&ctx);
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200832}
833/* END_CASE */
834
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100835/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_AUTH_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100836void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv,
837 data_t *ad, data_t *cipher, data_t *tag,
838 char *result, data_t *clear, int use_psa)
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200839{
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100840 /*
841 * Take an AEAD ciphertext + tag and perform a pair
842 * of AEAD decryption and AEAD encryption. Check that
Hanno Beckera13272d2018-11-12 16:27:30 +0000843 * this results in the expected plaintext, and that
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100844 * decryption and encryption are inverse to one another.
845 */
Hanno Beckera13272d2018-11-12 16:27:30 +0000846
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200847 int ret;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100848 int using_nist_kw, using_nist_kw_padding;
Hanno Beckera13272d2018-11-12 16:27:30 +0000849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200851 size_t outlen;
852
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100853 unsigned char *cipher_plus_tag = NULL;
854 size_t cipher_plus_tag_len;
855 unsigned char *decrypt_buf = NULL;
856 size_t decrypt_buf_len = 0;
857 unsigned char *encrypt_buf = NULL;
858 size_t encrypt_buf_len = 0;
859
Gilles Peskine70edd682020-12-03 20:27:27 +0100860 /* Null pointers are documented as valid for inputs of length 0.
861 * The test framework passes non-null pointers, so set them to NULL.
862 * key, cipher and tag can't be empty. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 if (iv->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100864 iv->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 }
866 if (ad->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100867 ad->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 }
869 if (clear->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100870 clear->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 }
Gilles Peskine70edd682020-12-03 20:27:27 +0100872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200874
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100875 /* Initialize PSA Crypto */
876#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 if (use_psa == 1) {
878 PSA_ASSERT(psa_crypto_init());
879 }
Hanno Beckera13272d2018-11-12 16:27:30 +0000880#else
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100881 (void) use_psa;
882#endif
883
884 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100885 * Are we using NIST_KW? with padding?
886 */
887 using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
888 cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
889 cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
890 using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
891 cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
892 cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
893 using_nist_kw_padding;
894
895 /*
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100896 * Prepare context for decryption
897 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key,
899 MBEDTLS_DECRYPT)) {
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100900 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 }
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200902
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100903 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100904 * prepare buffer for decryption
905 * (we need the tag appended to the ciphertext)
906 */
907 cipher_plus_tag_len = cipher->len + tag->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 ASSERT_ALLOC(cipher_plus_tag, cipher_plus_tag_len);
909 memcpy(cipher_plus_tag, cipher->x, cipher->len);
910 memcpy(cipher_plus_tag + cipher->len, tag->x, tag->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100911
912 /*
913 * Compute length of output buffer according to the documentation
914 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 if (using_nist_kw) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100916 decrypt_buf_len = cipher_plus_tag_len - 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 } else {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100918 decrypt_buf_len = cipher_plus_tag_len - tag->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 }
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100920
921
922 /*
923 * Try decrypting to a buffer that's 1B too small
924 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 if (decrypt_buf_len != 0) {
926 ASSERT_ALLOC(decrypt_buf, decrypt_buf_len - 1);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100927
928 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
930 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
931 decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len);
932 TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 mbedtls_free(decrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100935 decrypt_buf = NULL;
936 }
937
938 /*
939 * Authenticate and decrypt, and check result
940 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 ASSERT_ALLOC(decrypt_buf, decrypt_buf_len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100942
943 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
945 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
946 decrypt_buf, decrypt_buf_len, &outlen, tag->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (strcmp(result, "FAIL") == 0) {
949 TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED);
950 TEST_ASSERT(buffer_is_all_zero(decrypt_buf, decrypt_buf_len));
951 } else {
952 TEST_ASSERT(ret == 0);
953 ASSERT_COMPARE(decrypt_buf, outlen, clear->x, clear->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100954 }
955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 mbedtls_free(decrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100957 decrypt_buf = NULL;
958
959 /*
960 * Encrypt back if test data was authentic
961 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (strcmp(result, "FAIL") != 0) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100963 /* prepare context for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key,
965 MBEDTLS_ENCRYPT)) {
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100966 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 }
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100968
969 /*
970 * Compute size of output buffer according to documentation
971 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 if (using_nist_kw) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100973 encrypt_buf_len = clear->len + 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 if (using_nist_kw_padding && encrypt_buf_len % 8 != 0) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100975 encrypt_buf_len += 8 - encrypt_buf_len % 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 }
977 } else {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100978 encrypt_buf_len = clear->len + tag->len;
979 }
980
981 /*
982 * Try encrypting with an output buffer that's 1B too small
983 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 ASSERT_ALLOC(encrypt_buf, encrypt_buf_len - 1);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100985
986 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
988 ad->x, ad->len, clear->x, clear->len,
989 encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len);
990 TEST_ASSERT(ret != 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100991
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 mbedtls_free(encrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100993 encrypt_buf = NULL;
994
995 /*
996 * Encrypt and check the result
997 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 ASSERT_ALLOC(encrypt_buf, encrypt_buf_len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100999
1000 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
1002 ad->x, ad->len, clear->x, clear->len,
1003 encrypt_buf, encrypt_buf_len, &outlen, tag->len);
1004 TEST_ASSERT(ret == 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 TEST_ASSERT(outlen == cipher->len + tag->len);
1007 TEST_ASSERT(memcmp(encrypt_buf, cipher->x, cipher->len) == 0);
1008 TEST_ASSERT(memcmp(encrypt_buf + cipher->len,
1009 tag->x, tag->len) == 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001010
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 mbedtls_free(encrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001012 encrypt_buf = NULL;
1013 }
1014
Paul Bakkerbd51b262014-07-10 15:26:12 +02001015exit:
Hanno Beckera13272d2018-11-12 16:27:30 +00001016
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 mbedtls_cipher_free(&ctx);
1018 mbedtls_free(decrypt_buf);
1019 mbedtls_free(encrypt_buf);
1020 mbedtls_free(cipher_plus_tag);
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001021
Hanno Beckera13272d2018-11-12 16:27:30 +00001022#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 if (use_psa == 1) {
1024 PSA_DONE();
1025 }
Hanno Beckera13272d2018-11-12 16:27:30 +00001026#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001027}
1028/* END_CASE */
1029
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +02001030/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001031void test_vec_ecb(int cipher_id, int operation, data_t *key,
1032 data_t *input, data_t *result, int finish_result
1033 )
Paul Bakker5e0efa72013-09-08 23:04:04 +02001034{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001036 unsigned char output[32];
1037 size_t outlen;
1038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001040
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 memset(output, 0x00, sizeof(output));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001042
1043 /* Prepare context */
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
1045 mbedtls_cipher_info_from_type(cipher_id)));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001046
Paul Bakker5e0efa72013-09-08 23:04:04 +02001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001049
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, input->x,
1051 mbedtls_cipher_get_block_size(&ctx),
1052 output, &outlen));
1053 TEST_ASSERT(outlen == mbedtls_cipher_get_block_size(&ctx));
1054 TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen,
1055 &outlen));
1056 TEST_ASSERT(0 == outlen);
Paul Bakker5e0efa72013-09-08 23:04:04 +02001057
1058 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if (0 == finish_result) {
1060 TEST_ASSERT(0 == memcmp(output, result->x,
1061 mbedtls_cipher_get_block_size(&ctx)));
1062 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001063
Paul Bakkerbd51b262014-07-10 15:26:12 +02001064exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 mbedtls_cipher_free(&ctx);
Paul Bakker5e0efa72013-09-08 23:04:04 +02001066}
1067/* END_CASE */
1068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070void test_vec_crypt(int cipher_id, int operation, data_t *key,
1071 data_t *iv, data_t *input, data_t *result,
1072 int finish_result, int use_psa)
Ron Eldor7b012442017-09-25 17:03:12 +03001073{
Ron Eldor7b012442017-09-25 17:03:12 +03001074 mbedtls_cipher_context_t ctx;
1075 unsigned char output[32];
1076 size_t outlen;
1077
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 mbedtls_cipher_init(&ctx);
Ron Eldor7b012442017-09-25 17:03:12 +03001079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 memset(output, 0x00, sizeof(output));
Ron Eldor7b012442017-09-25 17:03:12 +03001081
1082 /* Prepare context */
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -04001083#if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
Hanno Beckere43164e2018-11-12 12:46:35 +00001084 (void) use_psa;
1085#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 if (use_psa == 1) {
1087 PSA_ASSERT(psa_crypto_init());
1088 TEST_ASSERT(0 == mbedtls_cipher_setup_psa(&ctx,
1089 mbedtls_cipher_info_from_type(cipher_id), 0));
1090 } else
Przemek Stekiel476d9c42022-05-19 12:26:33 +02001091#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
1093 mbedtls_cipher_info_from_type(cipher_id)));
Ron Eldor7b012442017-09-25 17:03:12 +03001094
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation));
1096 if (MBEDTLS_MODE_CBC == ctx.cipher_info->mode) {
1097 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE));
1098 }
Ron Eldor7b012442017-09-25 17:03:12 +03001099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 TEST_ASSERT(finish_result == mbedtls_cipher_crypt(&ctx, iv->len ? iv->x : NULL,
1101 iv->len, input->x, input->len,
1102 output, &outlen));
1103 TEST_ASSERT(result->len == outlen);
Ron Eldor7b012442017-09-25 17:03:12 +03001104 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if (0 == finish_result) {
1106 TEST_ASSERT(0 == memcmp(output, result->x, outlen));
1107 }
Ron Eldor7b012442017-09-25 17:03:12 +03001108
1109exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 mbedtls_cipher_free(&ctx);
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -04001111#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 PSA_DONE();
Andrzej Kureked052792022-10-21 05:37:54 -04001113#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_TEST_DEPRECATED */
Ron Eldor7b012442017-09-25 17:03:12 +03001114}
1115/* END_CASE */
1116
1117/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +01001118void set_padding(int cipher_id, int pad_mode, int ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001119{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 const mbedtls_cipher_info_t *cipher_info;
1121 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1126 TEST_ASSERT(NULL != cipher_info);
1127 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 TEST_ASSERT(ret == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001130
Paul Bakkerbd51b262014-07-10 15:26:12 +02001131exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 mbedtls_cipher_free(&ctx);
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001133}
Paul Bakker33b43f12013-08-20 11:48:36 +02001134/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137void check_padding(int pad_mode, data_t *input, int ret, int dlen_check
1138 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001139{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 mbedtls_cipher_info_t cipher_info;
1141 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +01001142 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001143
1144 /* build a fake context just for getting access to get_padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001147 ctx.cipher_info = &cipher_info;
1148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001150
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 TEST_ASSERT(ret == ctx.get_padding(input->x, input->len, &dlen));
1153 if (0 == ret) {
1154 TEST_ASSERT(dlen == (size_t) dlen_check);
1155 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001156}
Paul Bakker33b43f12013-08-20 11:48:36 +02001157/* END_CASE */
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001158
1159/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001160void iv_len_validity(int cipher_id, char *cipher_string,
1161 int iv_len_val, int ret)
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001162{
1163 size_t iv_len = iv_len_val;
1164 unsigned char iv[16];
1165
Thomas Daubney3a066ec2022-02-17 12:01:28 +00001166 /* Initialise iv buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 memset(iv, 0, sizeof(iv));
Thomas Daubney3a066ec2022-02-17 12:01:28 +00001168
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001169 const mbedtls_cipher_info_t *cipher_info;
1170 mbedtls_cipher_context_t ctx_dec;
1171 mbedtls_cipher_context_t ctx_enc;
1172
1173 /*
1174 * Prepare contexts
1175 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 mbedtls_cipher_init(&ctx_dec);
1177 mbedtls_cipher_init(&ctx_enc);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001178
1179 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1181 TEST_ASSERT(NULL != cipher_info);
1182 TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info);
1183 TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info),
1184 cipher_string) == 0);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001185
1186 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
1188 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
1191 TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001192
1193exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 mbedtls_cipher_free(&ctx_dec);
1195 mbedtls_cipher_free(&ctx_enc);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001196}
1197/* END_CASE */