blob: 01b3f2c7a5858804a25a6e8ccc4518464f7faa0c [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 Peskine4f4d4b22023-06-14 17:34:31 +020066 TEST_LE_U(key_bitlen, MBEDTLS_MAX_KEY_LENGTH * 8);
67 TEST_LE_U(block_size, MBEDTLS_MAX_BLOCK_LENGTH);
68 TEST_LE_U(iv_size, MBEDTLS_MAX_IV_LENGTH);
Gilles Peskine0be02bd2021-07-19 16:32:54 +020069
Gilles Peskine449bd832023-01-11 14:50:10 +010070 if (strstr(info->name, "-ECB") != NULL) {
71 TEST_ASSERT(iv_size == 0);
72 TEST_ASSERT(!mbedtls_cipher_info_has_variable_iv_size(info));
73 } else if (strstr(info->name, "-CBC") != NULL ||
74 strstr(info->name, "-CTR") != NULL) {
75 TEST_ASSERT(iv_size == block_size);
76 TEST_ASSERT(!mbedtls_cipher_info_has_variable_iv_size(info));
77 } else if (strstr(info->name, "-GCM") != NULL) {
78 TEST_ASSERT(iv_size == block_size - 4);
79 TEST_ASSERT(mbedtls_cipher_info_has_variable_iv_size(info));
Max Fillingerc60c3a02021-11-09 22:38:56 +010080 }
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082 return 1;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020083
84exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010085 return 0;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020086}
87
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010088#if defined(MBEDTLS_CIPHER_AUTH_CRYPT)
89/* Helper for resetting key/direction
90 *
91 * The documentation doesn't explicitly say whether calling
92 * mbedtls_cipher_setkey() twice is allowed or not. This currently works with
93 * the default software implementation, but only by accident. It isn't
94 * guaranteed to work with new ciphers or with alternative implementations of
95 * individual ciphers, and it doesn't work with the PSA wrappers. So don't do
96 * it, and instead start with a fresh context.
97 */
Gilles Peskine449bd832023-01-11 14:50:10 +010098static int cipher_reset_key(mbedtls_cipher_context_t *ctx, int cipher_id,
99 int use_psa, size_t tag_len, const data_t *key, int direction)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100100{
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 mbedtls_cipher_free(ctx);
102 mbedtls_cipher_init(ctx);
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100103
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -0400104#if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100105 (void) use_psa;
106 (void) tag_len;
107#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 if (use_psa == 1) {
109 TEST_ASSERT(0 == mbedtls_cipher_setup_psa(ctx,
110 mbedtls_cipher_info_from_type(cipher_id),
111 tag_len));
112 } else
Przemek Stekiel476d9c42022-05-19 12:26:33 +0200113#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED */
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100114 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 TEST_ASSERT(0 == mbedtls_cipher_setup(ctx,
116 mbedtls_cipher_info_from_type(cipher_id)));
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100117 }
118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 TEST_ASSERT(0 == mbedtls_cipher_setkey(ctx, key->x, 8 * key->len,
120 direction));
121 return 1;
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100122
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100123exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 return 0;
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100125}
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100126
127/*
128 * Check if a buffer is all-0 bytes:
129 * return 1 if it is,
130 * 0 if it isn't.
131 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100132int buffer_is_all_zero(const uint8_t *buf, size_t size)
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100133{
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 for (size_t i = 0; i < size; i++) {
135 if (buf[i] != 0) {
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100136 return 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 }
138 }
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100139 return 1;
140}
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100141#endif /* MBEDTLS_CIPHER_AUTH_CRYPT */
142
Paul Bakker33b43f12013-08-20 11:48:36 +0200143/* END_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000144
Paul Bakker33b43f12013-08-20 11:48:36 +0200145/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 * depends_on:MBEDTLS_CIPHER_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200147 * END_DEPENDENCIES
148 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000149
Paul Bakker33b43f12013-08-20 11:48:36 +0200150/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151void mbedtls_cipher_list()
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100152{
153 const int *cipher_type;
154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 for (cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++) {
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200156 const mbedtls_cipher_info_t *info =
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 mbedtls_cipher_info_from_type(*cipher_type);
158 mbedtls_test_set_step(*cipher_type);
159 if (!check_cipher_info(*cipher_type, info)) {
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200160 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 }
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200162 }
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100163}
164/* END_CASE */
165
166/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167void cipher_invalid_param_unconditional()
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200168{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500169 mbedtls_cipher_context_t valid_ctx;
170 mbedtls_cipher_context_t invalid_ctx;
171 mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
172 mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
173 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
174 int valid_size = sizeof(valid_buffer);
175 int valid_bitlen = valid_size * 8;
176 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 *(mbedtls_cipher_list()));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500178 size_t size_t_var;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 (void) valid_mode; /* In some configurations this is unused */
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 mbedtls_cipher_init(&valid_ctx);
183 mbedtls_cipher_init(&invalid_ctx);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, valid_info) == 0);
Gilles Peskinedc8ecda2021-12-10 14:28:31 +0100186
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500187 /* mbedtls_cipher_setup() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, NULL) ==
189 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200190
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500191 /* mbedtls_cipher_get_block_size() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 TEST_ASSERT(mbedtls_cipher_get_block_size(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200193
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500194 /* mbedtls_cipher_get_cipher_mode() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 TEST_ASSERT(mbedtls_cipher_get_cipher_mode(&invalid_ctx) ==
196 MBEDTLS_MODE_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200197
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500198 /* mbedtls_cipher_get_iv_size() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 TEST_ASSERT(mbedtls_cipher_get_iv_size(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200200
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500201 /* mbedtls_cipher_get_type() */
202 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 mbedtls_cipher_get_type(&invalid_ctx) ==
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500204 MBEDTLS_CIPHER_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200205
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500206 /* mbedtls_cipher_get_name() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 TEST_ASSERT(mbedtls_cipher_get_name(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200208
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500209 /* mbedtls_cipher_get_key_bitlen() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 TEST_ASSERT(mbedtls_cipher_get_key_bitlen(&invalid_ctx) ==
211 MBEDTLS_KEY_LENGTH_NONE);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500212
213 /* mbedtls_cipher_get_operation() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 TEST_ASSERT(mbedtls_cipher_get_operation(&invalid_ctx) ==
215 MBEDTLS_OPERATION_NONE);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500216
217 /* mbedtls_cipher_setkey() */
218 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 mbedtls_cipher_setkey(&invalid_ctx,
220 valid_buffer,
221 valid_bitlen,
222 valid_operation) ==
223 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500224
225 /* mbedtls_cipher_set_iv() */
226 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 mbedtls_cipher_set_iv(&invalid_ctx,
228 valid_buffer,
229 valid_size) ==
230 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500231
232 /* mbedtls_cipher_reset() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 TEST_ASSERT(mbedtls_cipher_reset(&invalid_ctx) ==
234 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200235
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200236#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500237 /* mbedtls_cipher_update_ad() */
238 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_cipher_update_ad(&invalid_ctx,
240 valid_buffer,
241 valid_size) ==
242 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500243#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
244
245#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
246 /* mbedtls_cipher_set_padding_mode() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 TEST_ASSERT(mbedtls_cipher_set_padding_mode(&invalid_ctx, valid_mode) ==
248 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200249#endif
250
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500251 /* mbedtls_cipher_update() */
252 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 mbedtls_cipher_update(&invalid_ctx,
254 valid_buffer,
255 valid_size,
256 valid_buffer,
257 &size_t_var) ==
258 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200259
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500260 /* mbedtls_cipher_finish() */
261 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 mbedtls_cipher_finish(&invalid_ctx,
263 valid_buffer,
264 &size_t_var) ==
265 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200266
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200267#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500268 /* mbedtls_cipher_write_tag() */
269 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 mbedtls_cipher_write_tag(&invalid_ctx,
271 valid_buffer,
272 valid_size) ==
273 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200274
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500275 /* mbedtls_cipher_check_tag() */
276 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 mbedtls_cipher_check_tag(&invalid_ctx,
278 valid_buffer,
279 valid_size) ==
280 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500281#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
282
283exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 mbedtls_cipher_free(&invalid_ctx);
285 mbedtls_cipher_free(&valid_ctx);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500286}
287/* END_CASE */
288
Tuvshinzaya Erdenekhuu6c689272022-07-29 14:45:55 +0100289/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100290void cipher_invalid_param_conditional()
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500291{
292 mbedtls_cipher_context_t valid_ctx;
293
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500294 mbedtls_operation_t invalid_operation = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500295 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
296 int valid_size = sizeof(valid_buffer);
297 int valid_bitlen = valid_size * 8;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500298
Ronald Cron875b5fb2021-05-21 08:50:00 +0200299 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500300 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 mbedtls_cipher_setkey(&valid_ctx,
302 valid_buffer,
303 valid_bitlen,
304 invalid_operation));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500305
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500306exit:
TRodziewicz70199552021-05-27 13:52:59 +0200307 ;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200308}
309/* END_CASE */
310
Paul Bakker6a9c7252016-07-14 13:46:10 +0100311/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100312void cipher_special_behaviours()
Paul Bakker6a9c7252016-07-14 13:46:10 +0100313{
314 const mbedtls_cipher_info_t *cipher_info;
315 mbedtls_cipher_context_t ctx;
316 unsigned char input[32];
317 unsigned char output[32];
Gilles Peskine449bd832023-01-11 14:50:10 +0100318#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100319 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300320#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100321 size_t olen = 0;
322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 mbedtls_cipher_init(&ctx);
324 memset(input, 0, sizeof(input));
325 memset(output, 0, sizeof(output));
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300326#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 memset(iv, 0, sizeof(iv));
Paul Bakker6a9c7252016-07-14 13:46:10 +0100328
329 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_CBC);
331 TEST_ASSERT(NULL != cipher_info);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Paul Bakker6a9c7252016-07-14 13:46:10 +0100334
335 /* IV too big */
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1)
337 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100338
339 /* IV too small */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, 0)
341 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 mbedtls_cipher_free(&ctx);
344 mbedtls_cipher_init(&ctx);
Ron Eldor6f90ed82017-09-26 12:08:54 +0300345#endif /* MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB);
347 TEST_ASSERT(NULL != cipher_info);
Ron Eldor7b012442017-09-25 17:03:12 +0300348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Ron Eldor7b012442017-09-25 17:03:12 +0300350
Paul Bakker6a9c7252016-07-14 13:46:10 +0100351 /* Update ECB with partial block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 TEST_ASSERT(mbedtls_cipher_update(&ctx, input, 1, output, &olen)
353 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100354
355exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 mbedtls_cipher_free(&ctx);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100357}
358/* END_CASE */
359
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200360/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100361void enc_dec_buf(int cipher_id, char *cipher_string, int key_len,
362 int length_val, int pad_mode)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200363{
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200364 size_t length = length_val, outlen, total_len, i, block_size, iv_len;
Jaeden Amerod906b812018-06-08 11:03:16 +0100365 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000366 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200367 unsigned char ad[13];
368 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200369 unsigned char inbuf[64];
370 unsigned char encbuf[64];
371 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 const mbedtls_cipher_info_t *cipher_info;
374 mbedtls_cipher_context_t ctx_dec;
375 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000376
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200377 /*
378 * Prepare contexts
379 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 mbedtls_cipher_init(&ctx_dec);
381 mbedtls_cipher_init(&ctx_enc);
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 memset(key, 0x2a, sizeof(key));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000384
385 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
387 TEST_ASSERT(NULL != cipher_info);
388 TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info);
389 TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info),
390 cipher_string) == 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000391
392 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
394 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT));
397 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (-1 != pad_mode) {
401 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode));
402 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode));
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200403 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200404#else
405 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200407
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200408 /*
409 * Do a few encode/decode cycles
410 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 for (i = 0; i < 3; i++) {
412 memset(iv, 0x00 + i, sizeof(iv));
413 memset(ad, 0x10 + i, sizeof(ad));
414 memset(inbuf, 0x20 + i, sizeof(inbuf));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 memset(encbuf, 0, sizeof(encbuf));
417 memset(decbuf, 0, sizeof(decbuf));
418 memset(tag, 0, sizeof(tag));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) {
421 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
422 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
423 } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
424 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
425 iv_len = 12;
426 } else {
427 iv_len = sizeof(iv);
428 }
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
431 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
434 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200435
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200436#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
438 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
439 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, ad, sizeof(ad) - i));
442 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, ad, sizeof(ad) - i));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200443#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 block_size = mbedtls_cipher_get_block_size(&ctx_enc);
446 TEST_ASSERT(block_size != 0);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 /* encode length number of bytes from inbuf */
449 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, length, encbuf, &outlen));
450 total_len = outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 TEST_ASSERT(total_len == length ||
453 (total_len % block_size == 0 &&
454 total_len < length &&
455 total_len + block_size > length));
Paul Bakker343a8702011-06-09 14:27:58 +0000456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + outlen, &outlen));
458 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000459
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200460#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 TEST_EQUAL(expected, mbedtls_cipher_write_tag(&ctx_enc, tag, sizeof(tag)));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200462#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 TEST_ASSERT(total_len == length ||
465 (total_len % block_size == 0 &&
466 total_len > length &&
467 total_len <= length + block_size));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 /* decode the previously encoded string */
470 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, total_len, decbuf, &outlen));
471 total_len = outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 TEST_ASSERT(total_len == length ||
474 (total_len % block_size == 0 &&
475 total_len < length &&
476 total_len + block_size >= length));
Paul Bakker343a8702011-06-09 14:27:58 +0000477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + outlen, &outlen));
479 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000480
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200481#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 TEST_EQUAL(expected, mbedtls_cipher_check_tag(&ctx_dec, tag, sizeof(tag)));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200483#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 /* check result */
486 TEST_ASSERT(total_len == length);
487 TEST_ASSERT(0 == memcmp(inbuf, decbuf, length));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200488 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000489
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200490 /*
491 * Done
492 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200493exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 mbedtls_cipher_free(&ctx_dec);
495 mbedtls_cipher_free(&ctx_enc);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200496}
Paul Bakker33b43f12013-08-20 11:48:36 +0200497/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000498
Paul Bakker33b43f12013-08-20 11:48:36 +0200499/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100500void enc_fail(int cipher_id, int pad_mode, int key_len, int length_val,
501 int ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200502{
Paul Bakker33b43f12013-08-20 11:48:36 +0200503 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200504 unsigned char key[32];
505 unsigned char iv[16];
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 const mbedtls_cipher_info_t *cipher_info;
508 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200509
510 unsigned char inbuf[64];
511 unsigned char encbuf[64];
512
513 size_t outlen = 0;
514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 memset(key, 0, 32);
516 memset(iv, 0, 16);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 memset(inbuf, 5, 64);
521 memset(encbuf, 0, 64);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200522
523 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
525 TEST_ASSERT(NULL != cipher_info);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200526
527 /* Initialise context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
529 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key, key_len, MBEDTLS_ENCRYPT));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200532#else
533 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv, 16));
536 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200537#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
539 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100540 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200543#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200544
545 /* encode length number of bytes from inbuf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, inbuf, length, encbuf, &outlen));
547 TEST_ASSERT(ret == mbedtls_cipher_finish(&ctx, encbuf + outlen, &outlen));
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200548
549 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200550exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 mbedtls_cipher_free(&ctx);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200552}
Paul Bakker33b43f12013-08-20 11:48:36 +0200553/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200554
Paul Bakker33b43f12013-08-20 11:48:36 +0200555/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556void dec_empty_buf(int cipher,
557 int expected_update_ret,
558 int expected_finish_ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200559{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000560 unsigned char key[32];
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100561
562 unsigned char *iv = NULL;
563 size_t iv_len = 16;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 mbedtls_cipher_context_t ctx_dec;
566 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000567
568 unsigned char encbuf[64];
569 unsigned char decbuf[64];
570
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000571 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 memset(key, 0, 32);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 mbedtls_cipher_init(&ctx_dec);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 memset(encbuf, 0, 64);
578 memset(decbuf, 0, 64);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000579
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200580 /* Initialise context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 cipher_info = mbedtls_cipher_info_from_type(cipher);
582 TEST_ASSERT(NULL != cipher_info);
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
585 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100586 iv_len = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 ASSERT_ALLOC(iv, iv_len);
590 memset(iv, 0, iv_len);
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 TEST_ASSERT(sizeof(key) * 8 >= cipher_info->key_bitlen);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200593
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec,
597 key, cipher_info->key_bitlen,
598 MBEDTLS_DECRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200603
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200604#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
606 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100607 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200610#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000611
612 /* decode 0-byte string */
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 TEST_ASSERT(expected_update_ret ==
614 mbedtls_cipher_update(&ctx_dec, encbuf, 0, decbuf, &outlen));
615 TEST_ASSERT(0 == outlen);
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100616
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 if (expected_finish_ret == 0 &&
618 (cipher_info->mode == MBEDTLS_MODE_CBC ||
619 cipher_info->mode == MBEDTLS_MODE_ECB)) {
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100620 /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
621 * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
k-stachowiakd8727232019-07-29 17:46:29 +0200622 * decrypting an empty buffer.
623 * On the other hand, CBC and ECB ciphers need a full block of input.
624 */
625 expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100626 }
627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 TEST_ASSERT(expected_finish_ret == mbedtls_cipher_finish(
629 &ctx_dec, decbuf + outlen, &outlen));
630 TEST_ASSERT(0 == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000631
Paul Bakkerbd51b262014-07-10 15:26:12 +0200632exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 mbedtls_free(iv);
634 mbedtls_cipher_free(&ctx_dec);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200635}
Paul Bakker33b43f12013-08-20 11:48:36 +0200636/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000637
Paul Bakker33b43f12013-08-20 11:48:36 +0200638/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100639void enc_dec_buf_multipart(int cipher_id, int key_len, int first_length_val,
640 int second_length_val, int pad_mode,
641 int first_encrypt_output_len, int second_encrypt_output_len,
642 int first_decrypt_output_len, int second_decrypt_output_len)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200643{
Paul Bakker33b43f12013-08-20 11:48:36 +0200644 size_t first_length = first_length_val;
645 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000646 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200647 size_t block_size;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200648 size_t iv_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000649 unsigned char key[32];
650 unsigned char iv[16];
651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_cipher_context_t ctx_dec;
653 mbedtls_cipher_context_t ctx_enc;
654 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000655
656 unsigned char inbuf[64];
657 unsigned char encbuf[64];
658 unsigned char decbuf[64];
659
Paul Bakker23986e52011-04-24 08:57:21 +0000660 size_t outlen = 0;
661 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 memset(key, 0, 32);
664 memset(iv, 0, 16);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 mbedtls_cipher_init(&ctx_dec);
667 mbedtls_cipher_init(&ctx_enc);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 memset(inbuf, 5, 64);
670 memset(encbuf, 0, 64);
671 memset(decbuf, 0, 64);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000672
673 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
675 TEST_ASSERT(NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
678 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT));
681 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000682
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700683#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 if (-1 != pad_mode) {
685 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode));
686 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode));
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700687 }
688#else
689 (void) pad_mode;
690#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) {
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200693 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
694 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
696 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100697 iv_len = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 } else {
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200699 iv_len = sizeof(iv);
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 }
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
703 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200704
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
706 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200707
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200708#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
710 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100711 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0));
714 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200715#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 block_size = mbedtls_cipher_get_block_size(&ctx_enc);
718 TEST_ASSERT(block_size != 0);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200719
Paul Bakker8123e9d2011-01-06 15:37:30 +0000720 /* encode length number of bytes from inbuf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, first_length, encbuf, &outlen));
722 TEST_ASSERT((size_t) first_encrypt_output_len == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000723 totaloutlen = outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 TEST_ASSERT(0 ==
725 mbedtls_cipher_update(&ctx_enc, inbuf + first_length, second_length,
726 encbuf + totaloutlen,
727 &outlen));
728 TEST_ASSERT((size_t) second_encrypt_output_len == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000729 totaloutlen += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 TEST_ASSERT(totaloutlen == length ||
731 (totaloutlen % block_size == 0 &&
732 totaloutlen < length &&
733 totaloutlen + block_size > length));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + totaloutlen, &outlen));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000736 totaloutlen += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 TEST_ASSERT(totaloutlen == length ||
738 (totaloutlen % block_size == 0 &&
739 totaloutlen > length &&
740 totaloutlen <= length + block_size));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000741
742 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700743 second_length = totaloutlen - first_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, first_length, decbuf, &outlen));
745 TEST_ASSERT((size_t) first_decrypt_output_len == outlen);
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200746 totaloutlen = outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 TEST_ASSERT(0 ==
748 mbedtls_cipher_update(&ctx_dec, encbuf + first_length, second_length,
749 decbuf + totaloutlen,
750 &outlen));
751 TEST_ASSERT((size_t) second_decrypt_output_len == outlen);
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700752 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 TEST_ASSERT(totaloutlen == length ||
755 (totaloutlen % block_size == 0 &&
756 totaloutlen < length &&
757 totaloutlen + block_size >= length));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + totaloutlen, &outlen));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200760 totaloutlen += outlen;
761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 TEST_ASSERT(totaloutlen == length);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000763
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 TEST_ASSERT(0 == memcmp(inbuf, decbuf, length));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000765
Paul Bakkerbd51b262014-07-10 15:26:12 +0200766exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 mbedtls_cipher_free(&ctx_dec);
768 mbedtls_cipher_free(&ctx_enc);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200769}
Paul Bakker33b43f12013-08-20 11:48:36 +0200770/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000771
Paul Bakker33b43f12013-08-20 11:48:36 +0200772/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773void decrypt_test_vec(int cipher_id, int pad_mode, data_t *key,
774 data_t *iv, data_t *cipher,
775 data_t *clear, data_t *ad, data_t *tag,
776 int finish_result, int tag_result)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200777{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200778 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200780 size_t outlen, total_len;
781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200783
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 memset(output, 0x00, sizeof(output));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200785
Azim Khanf1aaec92017-05-30 14:23:15 +0100786#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100787 ((void) ad);
788 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200789#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200790
791 /* Prepare context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
793 mbedtls_cipher_info_from_type(cipher_id)));
794 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 if (pad_mode != -1) {
797 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
798 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200799#else
800 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv->x, iv->len));
803 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200804#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 int expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
806 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100807 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, ad->x, ad->len));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200810#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200811
Azim Khand30ca132017-06-09 04:32:58 +0100812 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200813 total_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, cipher->x, cipher->len, output, &outlen));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200815 total_len += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen,
817 &outlen));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200818 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200819#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 int tag_expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
821 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100822 tag_result : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 TEST_EQUAL(tag_expected, mbedtls_cipher_check_tag(&ctx, tag->x, tag->len));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200825#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200826
827 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if (0 == finish_result && 0 == tag_result) {
829 TEST_ASSERT(total_len == clear->len);
830 TEST_ASSERT(0 == memcmp(output, clear->x, clear->len));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200831 }
832
Paul Bakkerbd51b262014-07-10 15:26:12 +0200833exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 mbedtls_cipher_free(&ctx);
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200835}
836/* END_CASE */
837
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100838/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_AUTH_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv,
840 data_t *ad, data_t *cipher, data_t *tag,
841 char *result, data_t *clear, int use_psa)
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200842{
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100843 /*
844 * Take an AEAD ciphertext + tag and perform a pair
845 * of AEAD decryption and AEAD encryption. Check that
Hanno Beckera13272d2018-11-12 16:27:30 +0000846 * this results in the expected plaintext, and that
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100847 * decryption and encryption are inverse to one another.
848 */
Hanno Beckera13272d2018-11-12 16:27:30 +0000849
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200850 int ret;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100851 int using_nist_kw, using_nist_kw_padding;
Hanno Beckera13272d2018-11-12 16:27:30 +0000852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200854 size_t outlen;
855
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100856 unsigned char *cipher_plus_tag = NULL;
857 size_t cipher_plus_tag_len;
858 unsigned char *decrypt_buf = NULL;
859 size_t decrypt_buf_len = 0;
860 unsigned char *encrypt_buf = NULL;
861 size_t encrypt_buf_len = 0;
862
Gilles Peskine70edd682020-12-03 20:27:27 +0100863 /* Null pointers are documented as valid for inputs of length 0.
864 * The test framework passes non-null pointers, so set them to NULL.
865 * key, cipher and tag can't be empty. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (iv->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100867 iv->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 }
869 if (ad->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100870 ad->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 }
872 if (clear->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100873 clear->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 }
Gilles Peskine70edd682020-12-03 20:27:27 +0100875
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200877
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100878 /* Initialize PSA Crypto */
879#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 if (use_psa == 1) {
881 PSA_ASSERT(psa_crypto_init());
882 }
Hanno Beckera13272d2018-11-12 16:27:30 +0000883#else
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100884 (void) use_psa;
885#endif
886
887 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100888 * Are we using NIST_KW? with padding?
889 */
890 using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
891 cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
892 cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
893 using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
894 cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
895 cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
896 using_nist_kw_padding;
897
898 /*
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100899 * Prepare context for decryption
900 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key,
902 MBEDTLS_DECRYPT)) {
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100903 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 }
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200905
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100906 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100907 * prepare buffer for decryption
908 * (we need the tag appended to the ciphertext)
909 */
910 cipher_plus_tag_len = cipher->len + tag->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 ASSERT_ALLOC(cipher_plus_tag, cipher_plus_tag_len);
912 memcpy(cipher_plus_tag, cipher->x, cipher->len);
913 memcpy(cipher_plus_tag + cipher->len, tag->x, tag->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100914
915 /*
916 * Compute length of output buffer according to the documentation
917 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (using_nist_kw) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100919 decrypt_buf_len = cipher_plus_tag_len - 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 } else {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100921 decrypt_buf_len = cipher_plus_tag_len - tag->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 }
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100923
924
925 /*
926 * Try decrypting to a buffer that's 1B too small
927 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 if (decrypt_buf_len != 0) {
929 ASSERT_ALLOC(decrypt_buf, decrypt_buf_len - 1);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100930
931 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
933 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
934 decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len);
935 TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 mbedtls_free(decrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100938 decrypt_buf = NULL;
939 }
940
941 /*
942 * Authenticate and decrypt, and check result
943 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 ASSERT_ALLOC(decrypt_buf, decrypt_buf_len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100945
946 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
948 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
949 decrypt_buf, decrypt_buf_len, &outlen, tag->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if (strcmp(result, "FAIL") == 0) {
952 TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED);
953 TEST_ASSERT(buffer_is_all_zero(decrypt_buf, decrypt_buf_len));
954 } else {
955 TEST_ASSERT(ret == 0);
956 ASSERT_COMPARE(decrypt_buf, outlen, clear->x, clear->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100957 }
958
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 mbedtls_free(decrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100960 decrypt_buf = NULL;
961
962 /*
963 * Encrypt back if test data was authentic
964 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 if (strcmp(result, "FAIL") != 0) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100966 /* prepare context for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key,
968 MBEDTLS_ENCRYPT)) {
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100969 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 }
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100971
972 /*
973 * Compute size of output buffer according to documentation
974 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 if (using_nist_kw) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100976 encrypt_buf_len = clear->len + 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if (using_nist_kw_padding && encrypt_buf_len % 8 != 0) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100978 encrypt_buf_len += 8 - encrypt_buf_len % 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 }
980 } else {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100981 encrypt_buf_len = clear->len + tag->len;
982 }
983
984 /*
985 * Try encrypting with an output buffer that's 1B too small
986 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 ASSERT_ALLOC(encrypt_buf, encrypt_buf_len - 1);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100988
989 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
991 ad->x, ad->len, clear->x, clear->len,
992 encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len);
993 TEST_ASSERT(ret != 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100994
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 mbedtls_free(encrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100996 encrypt_buf = NULL;
997
998 /*
999 * Encrypt and check the result
1000 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 ASSERT_ALLOC(encrypt_buf, encrypt_buf_len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001002
1003 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
1005 ad->x, ad->len, clear->x, clear->len,
1006 encrypt_buf, encrypt_buf_len, &outlen, tag->len);
1007 TEST_ASSERT(ret == 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001008
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 TEST_ASSERT(outlen == cipher->len + tag->len);
1010 TEST_ASSERT(memcmp(encrypt_buf, cipher->x, cipher->len) == 0);
1011 TEST_ASSERT(memcmp(encrypt_buf + cipher->len,
1012 tag->x, tag->len) == 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001013
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 mbedtls_free(encrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001015 encrypt_buf = NULL;
1016 }
1017
Paul Bakkerbd51b262014-07-10 15:26:12 +02001018exit:
Hanno Beckera13272d2018-11-12 16:27:30 +00001019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 mbedtls_cipher_free(&ctx);
1021 mbedtls_free(decrypt_buf);
1022 mbedtls_free(encrypt_buf);
1023 mbedtls_free(cipher_plus_tag);
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001024
Hanno Beckera13272d2018-11-12 16:27:30 +00001025#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 if (use_psa == 1) {
1027 PSA_DONE();
1028 }
Hanno Beckera13272d2018-11-12 16:27:30 +00001029#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001030}
1031/* END_CASE */
1032
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +02001033/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001034void test_vec_ecb(int cipher_id, int operation, data_t *key,
1035 data_t *input, data_t *result, int finish_result
1036 )
Paul Bakker5e0efa72013-09-08 23:04:04 +02001037{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001039 unsigned char output[32];
1040 size_t outlen;
1041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 memset(output, 0x00, sizeof(output));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001045
1046 /* Prepare context */
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
1048 mbedtls_cipher_info_from_type(cipher_id)));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001049
Paul Bakker5e0efa72013-09-08 23:04:04 +02001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, input->x,
1054 mbedtls_cipher_get_block_size(&ctx),
1055 output, &outlen));
1056 TEST_ASSERT(outlen == mbedtls_cipher_get_block_size(&ctx));
1057 TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen,
1058 &outlen));
1059 TEST_ASSERT(0 == outlen);
Paul Bakker5e0efa72013-09-08 23:04:04 +02001060
1061 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 if (0 == finish_result) {
1063 TEST_ASSERT(0 == memcmp(output, result->x,
1064 mbedtls_cipher_get_block_size(&ctx)));
1065 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001066
Paul Bakkerbd51b262014-07-10 15:26:12 +02001067exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 mbedtls_cipher_free(&ctx);
Paul Bakker5e0efa72013-09-08 23:04:04 +02001069}
1070/* END_CASE */
1071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +01001073void test_vec_crypt(int cipher_id, int operation, data_t *key,
1074 data_t *iv, data_t *input, data_t *result,
1075 int finish_result, int use_psa)
Ron Eldor7b012442017-09-25 17:03:12 +03001076{
Ron Eldor7b012442017-09-25 17:03:12 +03001077 mbedtls_cipher_context_t ctx;
1078 unsigned char output[32];
1079 size_t outlen;
1080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 mbedtls_cipher_init(&ctx);
Ron Eldor7b012442017-09-25 17:03:12 +03001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 memset(output, 0x00, sizeof(output));
Ron Eldor7b012442017-09-25 17:03:12 +03001084
1085 /* Prepare context */
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -04001086#if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
Hanno Beckere43164e2018-11-12 12:46:35 +00001087 (void) use_psa;
1088#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 if (use_psa == 1) {
1090 PSA_ASSERT(psa_crypto_init());
1091 TEST_ASSERT(0 == mbedtls_cipher_setup_psa(&ctx,
1092 mbedtls_cipher_info_from_type(cipher_id), 0));
1093 } else
Przemek Stekiel476d9c42022-05-19 12:26:33 +02001094#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
1096 mbedtls_cipher_info_from_type(cipher_id)));
Ron Eldor7b012442017-09-25 17:03:12 +03001097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation));
1099 if (MBEDTLS_MODE_CBC == ctx.cipher_info->mode) {
1100 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE));
1101 }
Ron Eldor7b012442017-09-25 17:03:12 +03001102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 TEST_ASSERT(finish_result == mbedtls_cipher_crypt(&ctx, iv->len ? iv->x : NULL,
1104 iv->len, input->x, input->len,
1105 output, &outlen));
1106 TEST_ASSERT(result->len == outlen);
Ron Eldor7b012442017-09-25 17:03:12 +03001107 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 if (0 == finish_result) {
1109 TEST_ASSERT(0 == memcmp(output, result->x, outlen));
1110 }
Ron Eldor7b012442017-09-25 17:03:12 +03001111
1112exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 mbedtls_cipher_free(&ctx);
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -04001114#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 PSA_DONE();
Andrzej Kureked052792022-10-21 05:37:54 -04001116#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_TEST_DEPRECATED */
Ron Eldor7b012442017-09-25 17:03:12 +03001117}
1118/* END_CASE */
1119
1120/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +01001121void set_padding(int cipher_id, int pad_mode, int ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001122{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 const mbedtls_cipher_info_t *cipher_info;
1124 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001127
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1129 TEST_ASSERT(NULL != cipher_info);
1130 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001131
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 TEST_ASSERT(ret == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001133
Paul Bakkerbd51b262014-07-10 15:26:12 +02001134exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 mbedtls_cipher_free(&ctx);
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001136}
Paul Bakker33b43f12013-08-20 11:48:36 +02001137/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +01001140void check_padding(int pad_mode, data_t *input, int ret, int dlen_check
1141 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001142{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 mbedtls_cipher_info_t cipher_info;
1144 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +01001145 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001146
1147 /* build a fake context just for getting access to get_padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001150 ctx.cipher_info = &cipher_info;
1151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001153
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 TEST_ASSERT(ret == ctx.get_padding(input->x, input->len, &dlen));
1156 if (0 == ret) {
1157 TEST_ASSERT(dlen == (size_t) dlen_check);
1158 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001159}
Paul Bakker33b43f12013-08-20 11:48:36 +02001160/* END_CASE */
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001161
1162/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163void iv_len_validity(int cipher_id, char *cipher_string,
1164 int iv_len_val, int ret)
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001165{
1166 size_t iv_len = iv_len_val;
1167 unsigned char iv[16];
1168
Thomas Daubney3a066ec2022-02-17 12:01:28 +00001169 /* Initialise iv buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 memset(iv, 0, sizeof(iv));
Thomas Daubney3a066ec2022-02-17 12:01:28 +00001171
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001172 const mbedtls_cipher_info_t *cipher_info;
1173 mbedtls_cipher_context_t ctx_dec;
1174 mbedtls_cipher_context_t ctx_enc;
1175
1176 /*
1177 * Prepare contexts
1178 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 mbedtls_cipher_init(&ctx_dec);
1180 mbedtls_cipher_init(&ctx_enc);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001181
1182 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1184 TEST_ASSERT(NULL != cipher_info);
1185 TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info);
1186 TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info),
1187 cipher_string) == 0);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001188
1189 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
1191 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
1194 TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001195
1196exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 mbedtls_cipher_free(&ctx_dec);
1198 mbedtls_cipher_free(&ctx_enc);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001199}
1200/* END_CASE */