blob: 8e49d2d3b5ff9cf52f668a38f22d9498bd789422 [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
Valerio Settidcee9872023-10-16 11:35:57 +02009#if defined(MBEDTLS_CIPHER_HAVE_SOME_AEAD_VIA_LEGACY_OR_USE_PSA) || defined(MBEDTLS_NIST_KW_C)
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +010010#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
Valerio Setti2f00b7a2023-10-17 11:43:34 +020088#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010089/* 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;
Valerio Settid3bdccc2023-10-04 12:09:06 +0200176 const int *cipher_list = mbedtls_cipher_list();
177 const mbedtls_cipher_info_t *valid_info;
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
Valerio Settid3bdccc2023-10-04 12:09:06 +0200185 /* Ensure that there is at least 1 supported cipher, otherwise exit gracefully */
Valerio Setti9d9b4b52023-10-19 10:51:03 +0200186 TEST_ASSUME(*cipher_list != 0);
Valerio Settid3bdccc2023-10-04 12:09:06 +0200187 valid_info = mbedtls_cipher_info_from_type(*cipher_list);
188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, valid_info) == 0);
Gilles Peskinedc8ecda2021-12-10 14:28:31 +0100190
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500191 /* mbedtls_cipher_setup() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, NULL) ==
193 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200194
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500195 /* mbedtls_cipher_get_block_size() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 TEST_ASSERT(mbedtls_cipher_get_block_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_cipher_mode() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 TEST_ASSERT(mbedtls_cipher_get_cipher_mode(&invalid_ctx) ==
200 MBEDTLS_MODE_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200201
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500202 /* mbedtls_cipher_get_iv_size() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 TEST_ASSERT(mbedtls_cipher_get_iv_size(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200204
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500205 /* mbedtls_cipher_get_type() */
206 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 mbedtls_cipher_get_type(&invalid_ctx) ==
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500208 MBEDTLS_CIPHER_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200209
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500210 /* mbedtls_cipher_get_name() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 TEST_ASSERT(mbedtls_cipher_get_name(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200212
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500213 /* mbedtls_cipher_get_key_bitlen() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 TEST_ASSERT(mbedtls_cipher_get_key_bitlen(&invalid_ctx) ==
215 MBEDTLS_KEY_LENGTH_NONE);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500216
217 /* mbedtls_cipher_get_operation() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 TEST_ASSERT(mbedtls_cipher_get_operation(&invalid_ctx) ==
219 MBEDTLS_OPERATION_NONE);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500220
221 /* mbedtls_cipher_setkey() */
222 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 mbedtls_cipher_setkey(&invalid_ctx,
224 valid_buffer,
225 valid_bitlen,
226 valid_operation) ==
227 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500228
229 /* mbedtls_cipher_set_iv() */
230 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 mbedtls_cipher_set_iv(&invalid_ctx,
232 valid_buffer,
233 valid_size) ==
234 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500235
236 /* mbedtls_cipher_reset() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 TEST_ASSERT(mbedtls_cipher_reset(&invalid_ctx) ==
238 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200239
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200240#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500241 /* mbedtls_cipher_update_ad() */
242 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 mbedtls_cipher_update_ad(&invalid_ctx,
244 valid_buffer,
245 valid_size) ==
246 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500247#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
248
249#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
250 /* mbedtls_cipher_set_padding_mode() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 TEST_ASSERT(mbedtls_cipher_set_padding_mode(&invalid_ctx, valid_mode) ==
252 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200253#endif
254
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500255 /* mbedtls_cipher_update() */
256 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 mbedtls_cipher_update(&invalid_ctx,
258 valid_buffer,
259 valid_size,
260 valid_buffer,
261 &size_t_var) ==
262 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200263
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500264 /* mbedtls_cipher_finish() */
265 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 mbedtls_cipher_finish(&invalid_ctx,
267 valid_buffer,
268 &size_t_var) ==
269 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200270
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200271#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500272 /* mbedtls_cipher_write_tag() */
273 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 mbedtls_cipher_write_tag(&invalid_ctx,
275 valid_buffer,
276 valid_size) ==
277 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200278
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500279 /* mbedtls_cipher_check_tag() */
280 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 mbedtls_cipher_check_tag(&invalid_ctx,
282 valid_buffer,
283 valid_size) ==
284 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500285#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
286
287exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 mbedtls_cipher_free(&invalid_ctx);
289 mbedtls_cipher_free(&valid_ctx);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500290}
291/* END_CASE */
292
Tuvshinzaya Erdenekhuu6c689272022-07-29 14:45:55 +0100293/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100294void cipher_invalid_param_conditional()
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500295{
296 mbedtls_cipher_context_t valid_ctx;
297
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500298 mbedtls_operation_t invalid_operation = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500299 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
300 int valid_size = sizeof(valid_buffer);
301 int valid_bitlen = valid_size * 8;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500302
Ronald Cron875b5fb2021-05-21 08:50:00 +0200303 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500304 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 mbedtls_cipher_setkey(&valid_ctx,
306 valid_buffer,
307 valid_bitlen,
308 invalid_operation));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500309
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500310exit:
TRodziewicz70199552021-05-27 13:52:59 +0200311 ;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200312}
313/* END_CASE */
314
Paul Bakker6a9c7252016-07-14 13:46:10 +0100315/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100316void cipher_special_behaviours()
Paul Bakker6a9c7252016-07-14 13:46:10 +0100317{
318 const mbedtls_cipher_info_t *cipher_info;
319 mbedtls_cipher_context_t ctx;
320 unsigned char input[32];
321 unsigned char output[32];
Gilles Peskine449bd832023-01-11 14:50:10 +0100322#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100323 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300324#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100325 size_t olen = 0;
326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 mbedtls_cipher_init(&ctx);
328 memset(input, 0, sizeof(input));
329 memset(output, 0, sizeof(output));
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300330#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 memset(iv, 0, sizeof(iv));
Paul Bakker6a9c7252016-07-14 13:46:10 +0100332
333 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_CBC);
335 TEST_ASSERT(NULL != cipher_info);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Paul Bakker6a9c7252016-07-14 13:46:10 +0100338
339 /* IV too big */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1)
341 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100342
343 /* IV too small */
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, 0)
345 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 mbedtls_cipher_free(&ctx);
348 mbedtls_cipher_init(&ctx);
Ron Eldor6f90ed82017-09-26 12:08:54 +0300349#endif /* MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB);
351 TEST_ASSERT(NULL != cipher_info);
Ron Eldor7b012442017-09-25 17:03:12 +0300352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Ron Eldor7b012442017-09-25 17:03:12 +0300354
Paul Bakker6a9c7252016-07-14 13:46:10 +0100355 /* Update ECB with partial block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 TEST_ASSERT(mbedtls_cipher_update(&ctx, input, 1, output, &olen)
357 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100358
359exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 mbedtls_cipher_free(&ctx);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100361}
362/* END_CASE */
363
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200364/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365void enc_dec_buf(int cipher_id, char *cipher_string, int key_len,
366 int length_val, int pad_mode)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200367{
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200368 size_t length = length_val, outlen, total_len, i, block_size, iv_len;
Jaeden Amerod906b812018-06-08 11:03:16 +0100369 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000370 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200371 unsigned char ad[13];
372 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200373 unsigned char inbuf[64];
374 unsigned char encbuf[64];
375 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 const mbedtls_cipher_info_t *cipher_info;
378 mbedtls_cipher_context_t ctx_dec;
379 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000380
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200381 /*
382 * Prepare contexts
383 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 mbedtls_cipher_init(&ctx_dec);
385 mbedtls_cipher_init(&ctx_enc);
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 memset(key, 0x2a, sizeof(key));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000388
389 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
391 TEST_ASSERT(NULL != cipher_info);
392 TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info);
393 TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info),
394 cipher_string) == 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000395
396 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
398 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT));
401 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (-1 != pad_mode) {
405 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode));
406 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode));
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200407 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200408#else
409 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200411
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200412 /*
413 * Do a few encode/decode cycles
414 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 for (i = 0; i < 3; i++) {
416 memset(iv, 0x00 + i, sizeof(iv));
417 memset(ad, 0x10 + i, sizeof(ad));
418 memset(inbuf, 0x20 + i, sizeof(inbuf));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 memset(encbuf, 0, sizeof(encbuf));
421 memset(decbuf, 0, sizeof(decbuf));
422 memset(tag, 0, sizeof(tag));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) {
425 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
426 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
427 } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
428 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
429 iv_len = 12;
430 } else {
431 iv_len = sizeof(iv);
432 }
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
435 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
438 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200439
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200440#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
442 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
443 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, ad, sizeof(ad) - i));
446 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, ad, sizeof(ad) - i));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200447#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 block_size = mbedtls_cipher_get_block_size(&ctx_enc);
450 TEST_ASSERT(block_size != 0);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 /* encode length number of bytes from inbuf */
453 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, length, encbuf, &outlen));
454 total_len = outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 TEST_ASSERT(total_len == length ||
457 (total_len % block_size == 0 &&
458 total_len < length &&
459 total_len + block_size > length));
Paul Bakker343a8702011-06-09 14:27:58 +0000460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + outlen, &outlen));
462 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000463
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200464#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 TEST_EQUAL(expected, mbedtls_cipher_write_tag(&ctx_enc, tag, sizeof(tag)));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200466#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 TEST_ASSERT(total_len == length ||
469 (total_len % block_size == 0 &&
470 total_len > length &&
471 total_len <= length + block_size));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 /* decode the previously encoded string */
474 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, total_len, decbuf, &outlen));
475 total_len = outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 TEST_ASSERT(total_len == length ||
478 (total_len % block_size == 0 &&
479 total_len < length &&
480 total_len + block_size >= length));
Paul Bakker343a8702011-06-09 14:27:58 +0000481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + outlen, &outlen));
483 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000484
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200485#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 TEST_EQUAL(expected, mbedtls_cipher_check_tag(&ctx_dec, tag, sizeof(tag)));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200487#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 /* check result */
490 TEST_ASSERT(total_len == length);
491 TEST_ASSERT(0 == memcmp(inbuf, decbuf, length));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200492 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000493
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200494 /*
495 * Done
496 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200497exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 mbedtls_cipher_free(&ctx_dec);
499 mbedtls_cipher_free(&ctx_enc);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200500}
Paul Bakker33b43f12013-08-20 11:48:36 +0200501/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000502
Paul Bakker33b43f12013-08-20 11:48:36 +0200503/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100504void enc_fail(int cipher_id, int pad_mode, int key_len, int length_val,
505 int ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200506{
Paul Bakker33b43f12013-08-20 11:48:36 +0200507 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200508 unsigned char key[32];
509 unsigned char iv[16];
510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 const mbedtls_cipher_info_t *cipher_info;
512 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200513
514 unsigned char inbuf[64];
515 unsigned char encbuf[64];
516
517 size_t outlen = 0;
518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 memset(key, 0, 32);
520 memset(iv, 0, 16);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 memset(inbuf, 5, 64);
525 memset(encbuf, 0, 64);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200526
527 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
529 TEST_ASSERT(NULL != cipher_info);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200530
531 /* Initialise context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
533 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key, key_len, MBEDTLS_ENCRYPT));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200536#else
537 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv, 16));
540 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200541#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
543 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100544 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200547#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200548
549 /* encode length number of bytes from inbuf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, inbuf, length, encbuf, &outlen));
551 TEST_ASSERT(ret == mbedtls_cipher_finish(&ctx, encbuf + outlen, &outlen));
Andre Goddard Rosa37117342024-05-01 11:47:12 -0500552 if (0 != ret) {
553 /* Check output parameter is set to the least-harmful value on error */
554 TEST_ASSERT(0 == outlen);
555 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200556
557 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200558exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 mbedtls_cipher_free(&ctx);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200560}
Paul Bakker33b43f12013-08-20 11:48:36 +0200561/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200562
Paul Bakker33b43f12013-08-20 11:48:36 +0200563/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100564void dec_empty_buf(int cipher,
565 int expected_update_ret,
566 int expected_finish_ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200567{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000568 unsigned char key[32];
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100569
570 unsigned char *iv = NULL;
571 size_t iv_len = 16;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_cipher_context_t ctx_dec;
574 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000575
576 unsigned char encbuf[64];
577 unsigned char decbuf[64];
578
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000579 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 memset(key, 0, 32);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 mbedtls_cipher_init(&ctx_dec);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 memset(encbuf, 0, 64);
586 memset(decbuf, 0, 64);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000587
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200588 /* Initialise context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 cipher_info = mbedtls_cipher_info_from_type(cipher);
590 TEST_ASSERT(NULL != cipher_info);
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
593 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100594 iv_len = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100596
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100597 TEST_CALLOC(iv, iv_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 memset(iv, 0, iv_len);
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100599
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100600 TEST_ASSERT(sizeof(key) * 8 >= mbedtls_cipher_info_get_key_bitlen(cipher_info));
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec,
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100605 key, mbedtls_cipher_info_get_key_bitlen(cipher_info),
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 MBEDTLS_DECRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200611
Waleed Elmelegyf4e66512023-09-21 14:04:35 +0100612#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Waleed Elmelegya7d206f2023-09-07 17:54:46 +0100613 if (ctx_dec.cipher_info->mode == MBEDTLS_MODE_CBC) {
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +0100614 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec,
615 MBEDTLS_PADDING_PKCS7));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +0100616 }
Waleed Elmelegyf4e66512023-09-21 14:04:35 +0100617#endif
Waleed Elmelegya7d206f2023-09-07 17:54:46 +0100618
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200619#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
621 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100622 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200625#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000626
627 /* decode 0-byte string */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 TEST_ASSERT(expected_update_ret ==
629 mbedtls_cipher_update(&ctx_dec, encbuf, 0, decbuf, &outlen));
630 TEST_ASSERT(0 == outlen);
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if (expected_finish_ret == 0 &&
633 (cipher_info->mode == MBEDTLS_MODE_CBC ||
634 cipher_info->mode == MBEDTLS_MODE_ECB)) {
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100635 /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
636 * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
k-stachowiakd8727232019-07-29 17:46:29 +0200637 * decrypting an empty buffer.
638 * On the other hand, CBC and ECB ciphers need a full block of input.
639 */
640 expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100641 }
642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 TEST_ASSERT(expected_finish_ret == mbedtls_cipher_finish(
644 &ctx_dec, decbuf + outlen, &outlen));
645 TEST_ASSERT(0 == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000646
Paul Bakkerbd51b262014-07-10 15:26:12 +0200647exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 mbedtls_free(iv);
649 mbedtls_cipher_free(&ctx_dec);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200650}
Paul Bakker33b43f12013-08-20 11:48:36 +0200651/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000652
Paul Bakker33b43f12013-08-20 11:48:36 +0200653/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100654void enc_dec_buf_multipart(int cipher_id, int key_len, int first_length_val,
655 int second_length_val, int pad_mode,
656 int first_encrypt_output_len, int second_encrypt_output_len,
657 int first_decrypt_output_len, int second_decrypt_output_len)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200658{
Paul Bakker33b43f12013-08-20 11:48:36 +0200659 size_t first_length = first_length_val;
660 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000661 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200662 size_t block_size;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200663 size_t iv_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664 unsigned char key[32];
665 unsigned char iv[16];
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_cipher_context_t ctx_dec;
668 mbedtls_cipher_context_t ctx_enc;
669 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000670
671 unsigned char inbuf[64];
672 unsigned char encbuf[64];
673 unsigned char decbuf[64];
674
Paul Bakker23986e52011-04-24 08:57:21 +0000675 size_t outlen = 0;
676 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 memset(key, 0, 32);
679 memset(iv, 0, 16);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 mbedtls_cipher_init(&ctx_dec);
682 mbedtls_cipher_init(&ctx_enc);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 memset(inbuf, 5, 64);
685 memset(encbuf, 0, 64);
686 memset(decbuf, 0, 64);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000687
688 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
690 TEST_ASSERT(NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
693 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200694
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT));
696 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000697
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700698#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 if (-1 != pad_mode) {
700 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode));
701 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode));
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700702 }
703#else
704 (void) pad_mode;
705#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) {
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200708 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
709 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
711 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100712 iv_len = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 } else {
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200714 iv_len = sizeof(iv);
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 }
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
718 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
721 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200722
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200723#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
725 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100726 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0));
729 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200730#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 block_size = mbedtls_cipher_get_block_size(&ctx_enc);
733 TEST_ASSERT(block_size != 0);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200734
Paul Bakker8123e9d2011-01-06 15:37:30 +0000735 /* encode length number of bytes from inbuf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, first_length, encbuf, &outlen));
737 TEST_ASSERT((size_t) first_encrypt_output_len == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000738 totaloutlen = outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 TEST_ASSERT(0 ==
740 mbedtls_cipher_update(&ctx_enc, inbuf + first_length, second_length,
741 encbuf + totaloutlen,
742 &outlen));
743 TEST_ASSERT((size_t) second_encrypt_output_len == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000744 totaloutlen += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 TEST_ASSERT(totaloutlen == length ||
746 (totaloutlen % block_size == 0 &&
747 totaloutlen < length &&
748 totaloutlen + block_size > length));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + totaloutlen, &outlen));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000751 totaloutlen += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 TEST_ASSERT(totaloutlen == length ||
753 (totaloutlen % block_size == 0 &&
754 totaloutlen > length &&
755 totaloutlen <= length + block_size));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000756
757 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700758 second_length = totaloutlen - first_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, first_length, decbuf, &outlen));
760 TEST_ASSERT((size_t) first_decrypt_output_len == outlen);
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200761 totaloutlen = outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 TEST_ASSERT(0 ==
763 mbedtls_cipher_update(&ctx_dec, encbuf + first_length, second_length,
764 decbuf + totaloutlen,
765 &outlen));
766 TEST_ASSERT((size_t) second_decrypt_output_len == outlen);
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700767 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 TEST_ASSERT(totaloutlen == length ||
770 (totaloutlen % block_size == 0 &&
771 totaloutlen < length &&
772 totaloutlen + block_size >= length));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + totaloutlen, &outlen));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200775 totaloutlen += outlen;
776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 TEST_ASSERT(totaloutlen == length);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 TEST_ASSERT(0 == memcmp(inbuf, decbuf, length));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000780
Paul Bakkerbd51b262014-07-10 15:26:12 +0200781exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 mbedtls_cipher_free(&ctx_dec);
783 mbedtls_cipher_free(&ctx_enc);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200784}
Paul Bakker33b43f12013-08-20 11:48:36 +0200785/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000786
Paul Bakker33b43f12013-08-20 11:48:36 +0200787/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100788void decrypt_test_vec(int cipher_id, int pad_mode, data_t *key,
789 data_t *iv, data_t *cipher,
790 data_t *clear, data_t *ad, data_t *tag,
791 int finish_result, int tag_result)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200792{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200793 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200795 size_t outlen, total_len;
796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 memset(output, 0x00, sizeof(output));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200800
Azim Khanf1aaec92017-05-30 14:23:15 +0100801#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100802 ((void) ad);
803 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200804#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200805
806 /* Prepare context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
808 mbedtls_cipher_info_from_type(cipher_id)));
809 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 if (pad_mode != -1) {
812 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
813 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200814#else
815 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv->x, iv->len));
818 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
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 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 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, ad->x, ad->len));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200825#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200826
Azim Khand30ca132017-06-09 04:32:58 +0100827 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200828 total_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, cipher->x, cipher->len, output, &outlen));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200830 total_len += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen,
832 &outlen));
Andre Goddard Rosa37117342024-05-01 11:47:12 -0500833 if (0 != finish_result) {
834 /* Check output parameter is set to the least-harmful value on error */
835 TEST_ASSERT(0 == outlen);
836 }
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200837 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200838#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 int tag_expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
840 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100841 tag_result : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 TEST_EQUAL(tag_expected, mbedtls_cipher_check_tag(&ctx, tag->x, tag->len));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200844#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200845
846 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (0 == finish_result && 0 == tag_result) {
848 TEST_ASSERT(total_len == clear->len);
849 TEST_ASSERT(0 == memcmp(output, clear->x, clear->len));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200850 }
851
Paul Bakkerbd51b262014-07-10 15:26:12 +0200852exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 mbedtls_cipher_free(&ctx);
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200854}
855/* END_CASE */
856
Valerio Setti2f00b7a2023-10-17 11:43:34 +0200857/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */
Gilles Peskine449bd832023-01-11 14:50:10 +0100858void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv,
859 data_t *ad, data_t *cipher, data_t *tag,
860 char *result, data_t *clear, int use_psa)
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200861{
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100862 /*
863 * Take an AEAD ciphertext + tag and perform a pair
864 * of AEAD decryption and AEAD encryption. Check that
Hanno Beckera13272d2018-11-12 16:27:30 +0000865 * this results in the expected plaintext, and that
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100866 * decryption and encryption are inverse to one another.
867 */
Hanno Beckera13272d2018-11-12 16:27:30 +0000868
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200869 int ret;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100870 int using_nist_kw, using_nist_kw_padding;
Hanno Beckera13272d2018-11-12 16:27:30 +0000871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200873 size_t outlen;
874
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100875 unsigned char *cipher_plus_tag = NULL;
876 size_t cipher_plus_tag_len;
877 unsigned char *decrypt_buf = NULL;
878 size_t decrypt_buf_len = 0;
879 unsigned char *encrypt_buf = NULL;
880 size_t encrypt_buf_len = 0;
881
Gilles Peskine70edd682020-12-03 20:27:27 +0100882 /* Null pointers are documented as valid for inputs of length 0.
883 * The test framework passes non-null pointers, so set them to NULL.
884 * key, cipher and tag can't be empty. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if (iv->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100886 iv->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 }
888 if (ad->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100889 ad->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 }
891 if (clear->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100892 clear->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 }
Gilles Peskine70edd682020-12-03 20:27:27 +0100894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200896
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100897 /* Initialize PSA Crypto */
898#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if (use_psa == 1) {
900 PSA_ASSERT(psa_crypto_init());
901 }
Hanno Beckera13272d2018-11-12 16:27:30 +0000902#else
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100903 (void) use_psa;
904#endif
905
906 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100907 * Are we using NIST_KW? with padding?
908 */
909 using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
910 cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
911 cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
912 using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
913 cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
914 cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
915 using_nist_kw_padding;
916
917 /*
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100918 * Prepare context for decryption
919 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key,
921 MBEDTLS_DECRYPT)) {
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100922 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 }
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200924
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100925 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100926 * prepare buffer for decryption
927 * (we need the tag appended to the ciphertext)
928 */
929 cipher_plus_tag_len = cipher->len + tag->len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100930 TEST_CALLOC(cipher_plus_tag, cipher_plus_tag_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 memcpy(cipher_plus_tag, cipher->x, cipher->len);
932 memcpy(cipher_plus_tag + cipher->len, tag->x, tag->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100933
934 /*
935 * Compute length of output buffer according to the documentation
936 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (using_nist_kw) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100938 decrypt_buf_len = cipher_plus_tag_len - 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 } else {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100940 decrypt_buf_len = cipher_plus_tag_len - tag->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 }
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100942
943
944 /*
945 * Try decrypting to a buffer that's 1B too small
946 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 if (decrypt_buf_len != 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100948 TEST_CALLOC(decrypt_buf, decrypt_buf_len - 1);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100949
950 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
952 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
953 decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len);
954 TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100955
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 /*
961 * Authenticate and decrypt, and check result
962 */
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100963 TEST_CALLOC(decrypt_buf, decrypt_buf_len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100964
965 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
967 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
968 decrypt_buf, decrypt_buf_len, &outlen, tag->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100969
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if (strcmp(result, "FAIL") == 0) {
971 TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED);
972 TEST_ASSERT(buffer_is_all_zero(decrypt_buf, decrypt_buf_len));
973 } else {
974 TEST_ASSERT(ret == 0);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100975 TEST_MEMORY_COMPARE(decrypt_buf, outlen, clear->x, clear->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100976 }
977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 mbedtls_free(decrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100979 decrypt_buf = NULL;
980
981 /*
982 * Encrypt back if test data was authentic
983 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 if (strcmp(result, "FAIL") != 0) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100985 /* prepare context for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key,
987 MBEDTLS_ENCRYPT)) {
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100988 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 }
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100990
991 /*
992 * Compute size of output buffer according to documentation
993 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 if (using_nist_kw) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100995 encrypt_buf_len = clear->len + 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 if (using_nist_kw_padding && encrypt_buf_len % 8 != 0) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100997 encrypt_buf_len += 8 - encrypt_buf_len % 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 }
999 } else {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001000 encrypt_buf_len = clear->len + tag->len;
1001 }
1002
1003 /*
1004 * Try encrypting with an output buffer that's 1B too small
1005 */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001006 TEST_CALLOC(encrypt_buf, encrypt_buf_len - 1);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001007
1008 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
1010 ad->x, ad->len, clear->x, clear->len,
1011 encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len);
1012 TEST_ASSERT(ret != 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 /*
1018 * Encrypt and check the result
1019 */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001020 TEST_CALLOC(encrypt_buf, encrypt_buf_len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001021
1022 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
1024 ad->x, ad->len, clear->x, clear->len,
1025 encrypt_buf, encrypt_buf_len, &outlen, tag->len);
1026 TEST_ASSERT(ret == 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 TEST_ASSERT(outlen == cipher->len + tag->len);
1029 TEST_ASSERT(memcmp(encrypt_buf, cipher->x, cipher->len) == 0);
1030 TEST_ASSERT(memcmp(encrypt_buf + cipher->len,
1031 tag->x, tag->len) == 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 mbedtls_free(encrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001034 encrypt_buf = NULL;
1035 }
1036
Paul Bakkerbd51b262014-07-10 15:26:12 +02001037exit:
Hanno Beckera13272d2018-11-12 16:27:30 +00001038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 mbedtls_cipher_free(&ctx);
1040 mbedtls_free(decrypt_buf);
1041 mbedtls_free(encrypt_buf);
1042 mbedtls_free(cipher_plus_tag);
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001043
Hanno Beckera13272d2018-11-12 16:27:30 +00001044#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 if (use_psa == 1) {
1046 PSA_DONE();
1047 }
Hanno Beckera13272d2018-11-12 16:27:30 +00001048#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001049}
1050/* END_CASE */
1051
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +02001052/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001053void test_vec_ecb(int cipher_id, int operation, data_t *key,
1054 data_t *input, data_t *result, int finish_result
1055 )
Paul Bakker5e0efa72013-09-08 23:04:04 +02001056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001058 unsigned char output[32];
1059 size_t outlen;
1060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001062
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 memset(output, 0x00, sizeof(output));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001064
1065 /* Prepare context */
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
1067 mbedtls_cipher_info_from_type(cipher_id)));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001068
Paul Bakker5e0efa72013-09-08 23:04:04 +02001069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, input->x,
1073 mbedtls_cipher_get_block_size(&ctx),
1074 output, &outlen));
1075 TEST_ASSERT(outlen == mbedtls_cipher_get_block_size(&ctx));
1076 TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen,
1077 &outlen));
1078 TEST_ASSERT(0 == outlen);
Paul Bakker5e0efa72013-09-08 23:04:04 +02001079
1080 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 if (0 == finish_result) {
1082 TEST_ASSERT(0 == memcmp(output, result->x,
1083 mbedtls_cipher_get_block_size(&ctx)));
1084 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001085
Paul Bakkerbd51b262014-07-10 15:26:12 +02001086exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 mbedtls_cipher_free(&ctx);
Paul Bakker5e0efa72013-09-08 23:04:04 +02001088}
1089/* END_CASE */
1090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +01001092void test_vec_crypt(int cipher_id, int operation, data_t *key,
1093 data_t *iv, data_t *input, data_t *result,
1094 int finish_result, int use_psa)
Ron Eldor7b012442017-09-25 17:03:12 +03001095{
Ron Eldor7b012442017-09-25 17:03:12 +03001096 mbedtls_cipher_context_t ctx;
1097 unsigned char output[32];
1098 size_t outlen;
1099
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 mbedtls_cipher_init(&ctx);
Ron Eldor7b012442017-09-25 17:03:12 +03001101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 memset(output, 0x00, sizeof(output));
Ron Eldor7b012442017-09-25 17:03:12 +03001103
1104 /* Prepare context */
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -04001105#if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
Hanno Beckere43164e2018-11-12 12:46:35 +00001106 (void) use_psa;
1107#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 if (use_psa == 1) {
1109 PSA_ASSERT(psa_crypto_init());
1110 TEST_ASSERT(0 == mbedtls_cipher_setup_psa(&ctx,
1111 mbedtls_cipher_info_from_type(cipher_id), 0));
1112 } else
Przemek Stekiel476d9c42022-05-19 12:26:33 +02001113#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
1115 mbedtls_cipher_info_from_type(cipher_id)));
Ron Eldor7b012442017-09-25 17:03:12 +03001116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation));
1118 if (MBEDTLS_MODE_CBC == ctx.cipher_info->mode) {
1119 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE));
1120 }
Ron Eldor7b012442017-09-25 17:03:12 +03001121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 TEST_ASSERT(finish_result == mbedtls_cipher_crypt(&ctx, iv->len ? iv->x : NULL,
1123 iv->len, input->x, input->len,
1124 output, &outlen));
1125 TEST_ASSERT(result->len == outlen);
Ron Eldor7b012442017-09-25 17:03:12 +03001126 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 if (0 == finish_result) {
1128 TEST_ASSERT(0 == memcmp(output, result->x, outlen));
1129 }
Ron Eldor7b012442017-09-25 17:03:12 +03001130
1131exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 mbedtls_cipher_free(&ctx);
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -04001133#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 PSA_DONE();
Andrzej Kureked052792022-10-21 05:37:54 -04001135#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_TEST_DEPRECATED */
Ron Eldor7b012442017-09-25 17:03:12 +03001136}
1137/* END_CASE */
1138
1139/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +01001140void set_padding(int cipher_id, int pad_mode, int ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001141{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 const mbedtls_cipher_info_t *cipher_info;
1143 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1148 TEST_ASSERT(NULL != cipher_info);
1149 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 TEST_ASSERT(ret == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001152
Paul Bakkerbd51b262014-07-10 15:26:12 +02001153exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 mbedtls_cipher_free(&ctx);
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001155}
Paul Bakker33b43f12013-08-20 11:48:36 +02001156/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +01001159void check_padding(int pad_mode, data_t *input, int ret, int dlen_check
1160 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001161{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 mbedtls_cipher_info_t cipher_info;
1163 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +01001164 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001165
1166 /* build a fake context just for getting access to get_padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001169 ctx.cipher_info = &cipher_info;
1170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001172
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001173
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 TEST_ASSERT(ret == ctx.get_padding(input->x, input->len, &dlen));
1175 if (0 == ret) {
1176 TEST_ASSERT(dlen == (size_t) dlen_check);
1177 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001178}
Paul Bakker33b43f12013-08-20 11:48:36 +02001179/* END_CASE */
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001180
1181/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001182void iv_len_validity(int cipher_id, char *cipher_string,
1183 int iv_len_val, int ret)
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001184{
1185 size_t iv_len = iv_len_val;
1186 unsigned char iv[16];
1187
Thomas Daubney3a066ec2022-02-17 12:01:28 +00001188 /* Initialise iv buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 memset(iv, 0, sizeof(iv));
Thomas Daubney3a066ec2022-02-17 12:01:28 +00001190
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001191 const mbedtls_cipher_info_t *cipher_info;
1192 mbedtls_cipher_context_t ctx_dec;
1193 mbedtls_cipher_context_t ctx_enc;
1194
1195 /*
1196 * Prepare contexts
1197 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 mbedtls_cipher_init(&ctx_dec);
1199 mbedtls_cipher_init(&ctx_enc);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001200
1201 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1203 TEST_ASSERT(NULL != cipher_info);
1204 TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info);
1205 TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info),
1206 cipher_string) == 0);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001207
1208 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
1210 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
1213 TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001214
1215exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 mbedtls_cipher_free(&ctx_dec);
1217 mbedtls_cipher_free(&ctx_enc);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001218}
1219/* END_CASE */
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001220
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001221/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
1222void check_set_padding(int cipher_id)
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001223{
1224 mbedtls_cipher_context_t ctx;
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001225 unsigned char *key = NULL;
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001226 unsigned char iv[16] = { 0 };
1227 unsigned char input[16] = { 0 };
1228 unsigned char output[32] = { 0 };
1229 size_t outlen = 0;
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001230 const mbedtls_cipher_info_t *cipher_info;
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001231 size_t keylen = 0;
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001232
Paul Elliott3bda79b2023-10-18 15:09:09 +01001233 mbedtls_cipher_init(&ctx);
1234
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001235 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1236
1237 if (cipher_info->mode != MBEDTLS_MODE_CBC) {
1238 TEST_FAIL("Cipher mode must be CBC");
1239 }
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001240
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001241 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info);
1242 TEST_CALLOC(key, keylen/8);
1243 memset(key, 0, keylen/8);
1244
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001245 TEST_EQUAL(0, mbedtls_cipher_setup(&ctx, cipher_info));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001246
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001247 TEST_EQUAL(0, mbedtls_cipher_setkey(&ctx, key, keylen,
1248 MBEDTLS_ENCRYPT));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001249
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001250 TEST_EQUAL(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
1251 mbedtls_cipher_crypt(&ctx, iv, sizeof(iv), input,
1252 sizeof(input), output, &outlen));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001253
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001254 TEST_EQUAL(0, mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE));
1255 TEST_EQUAL(0, mbedtls_cipher_crypt(&ctx, iv, sizeof(iv), input,
1256 sizeof(input), output, &outlen));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001257
1258exit:
1259 mbedtls_cipher_free(&ctx);
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001260 mbedtls_free(key);
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001261}
1262/* END_CASE */