blob: 9e3e026834334f96c2fe615a626132c05e5f4707 [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
David Horstmannd37e0c42025-01-16 16:24:35 +00009#include "cipher_invasive.h"
10
11#include "test/constant_flow.h"
12
Valerio Settidcee9872023-10-16 11:35:57 +020013#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 +010014#define MBEDTLS_CIPHER_AUTH_CRYPT
15#endif
16
Gilles Peskinedf00d452025-07-27 18:03:26 +020017/* Our software AES implementation is not constant-time. For constant-time
18 * testing involving AES, require a hardware-assisted AES that is
19 * constant-time.
20 *
21 * We assume that if the hardware-assisted version is available in the build,
22 * it will be available at runtime. The AES tests will fail if run on a
23 * processor without AESNI/AESCE.
24 */
25#include "aesce.h"
26#include "aesni.h"
27#if defined(MBEDTLS_AESCE_HAVE_CODE) || defined(MBEDTLS_AESNI_HAVE_CODE)
28#define HAVE_CONSTANT_TIME_AES
29#endif
30
Gilles Peskine0be02bd2021-07-19 16:32:54 +020031/* Check the internal consistency of a cipher info structure, and
32 * check it against mbedtls_cipher_info_from_xxx(). */
Gilles Peskine449bd832023-01-11 14:50:10 +010033static int check_cipher_info(mbedtls_cipher_type_t type,
34 const mbedtls_cipher_info_t *info)
Gilles Peskine0be02bd2021-07-19 16:32:54 +020035{
Max Fillinger72abd8a2021-11-28 14:02:36 +010036 size_t key_bitlen, block_size, iv_size;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020037
Gilles Peskine449bd832023-01-11 14:50:10 +010038 TEST_ASSERT(info != NULL);
39 TEST_EQUAL(type, mbedtls_cipher_info_get_type(info));
40 TEST_EQUAL(type, info->type);
41 TEST_ASSERT(mbedtls_cipher_info_from_type(type) == info);
Gilles Peskine0be02bd2021-07-19 16:32:54 +020042
Gilles Peskine449bd832023-01-11 14:50:10 +010043 TEST_EQUAL(info->mode, mbedtls_cipher_info_get_mode(info));
Gilles Peskine0be02bd2021-07-19 16:32:54 +020044
45 /* Insist that get_name() return the string from the structure and
46 * not a copy. A copy would have an unknown storage duration. */
Gilles Peskine449bd832023-01-11 14:50:10 +010047 TEST_ASSERT(mbedtls_cipher_info_get_name(info) == info->name);
48 TEST_ASSERT(mbedtls_cipher_info_from_string(info->name) == info);
Gilles Peskine0be02bd2021-07-19 16:32:54 +020049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 key_bitlen = mbedtls_cipher_info_get_key_bitlen(info);
51 block_size = mbedtls_cipher_info_get_block_size(info);
52 iv_size = mbedtls_cipher_info_get_iv_size(info);
53 if (info->type == MBEDTLS_CIPHER_NULL) {
54 TEST_ASSERT(key_bitlen == 0);
55 TEST_ASSERT(block_size == 1);
56 TEST_ASSERT(iv_size == 0);
57 } else if (info->mode == MBEDTLS_MODE_XTS) {
58 TEST_ASSERT(key_bitlen == 256 ||
59 key_bitlen == 384 ||
60 key_bitlen == 512);
61 } else if (!strncmp(info->name, "DES-EDE3-", 9)) {
62 TEST_ASSERT(key_bitlen == 192);
63 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
64 TEST_ASSERT(block_size == 8);
65 } else if (!strncmp(info->name, "DES-EDE-", 8)) {
66 TEST_ASSERT(key_bitlen == 128);
67 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
68 TEST_ASSERT(block_size == 8);
69 } else if (!strncmp(info->name, "DES-", 4)) {
70 TEST_ASSERT(key_bitlen == 64);
71 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
72 TEST_ASSERT(block_size == 8);
73 } else if (!strncmp(info->name, "AES", 3)) {
74 TEST_ASSERT(key_bitlen == 128 ||
75 key_bitlen == 192 ||
76 key_bitlen == 256);
77 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
78 TEST_ASSERT(block_size == 16);
79 } else {
80 TEST_ASSERT(key_bitlen == 128 ||
81 key_bitlen == 192 ||
82 key_bitlen == 256);
Gilles Peskine6ac8f942021-09-01 08:31:49 +020083 }
Gilles Peskine4f4d4b22023-06-14 17:34:31 +020084 TEST_LE_U(key_bitlen, MBEDTLS_MAX_KEY_LENGTH * 8);
85 TEST_LE_U(block_size, MBEDTLS_MAX_BLOCK_LENGTH);
86 TEST_LE_U(iv_size, MBEDTLS_MAX_IV_LENGTH);
Gilles Peskine0be02bd2021-07-19 16:32:54 +020087
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (strstr(info->name, "-ECB") != NULL) {
89 TEST_ASSERT(iv_size == 0);
90 TEST_ASSERT(!mbedtls_cipher_info_has_variable_iv_size(info));
91 } else if (strstr(info->name, "-CBC") != NULL ||
92 strstr(info->name, "-CTR") != NULL) {
93 TEST_ASSERT(iv_size == block_size);
94 TEST_ASSERT(!mbedtls_cipher_info_has_variable_iv_size(info));
95 } else if (strstr(info->name, "-GCM") != NULL) {
96 TEST_ASSERT(iv_size == block_size - 4);
97 TEST_ASSERT(mbedtls_cipher_info_has_variable_iv_size(info));
Max Fillingerc60c3a02021-11-09 22:38:56 +010098 }
99
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 return 1;
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200101
102exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 return 0;
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200104}
105
Valerio Setti2f00b7a2023-10-17 11:43:34 +0200106#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100107/* Helper for resetting key/direction
108 *
109 * The documentation doesn't explicitly say whether calling
110 * mbedtls_cipher_setkey() twice is allowed or not. This currently works with
111 * the default software implementation, but only by accident. It isn't
112 * guaranteed to work with new ciphers or with alternative implementations of
113 * individual ciphers, and it doesn't work with the PSA wrappers. So don't do
114 * it, and instead start with a fresh context.
115 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116static int cipher_reset_key(mbedtls_cipher_context_t *ctx, int cipher_id,
117 int use_psa, size_t tag_len, const data_t *key, int direction)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100118{
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 mbedtls_cipher_free(ctx);
120 mbedtls_cipher_init(ctx);
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100121
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -0400122#if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100123 (void) use_psa;
124 (void) tag_len;
125#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if (use_psa == 1) {
127 TEST_ASSERT(0 == mbedtls_cipher_setup_psa(ctx,
128 mbedtls_cipher_info_from_type(cipher_id),
129 tag_len));
130 } else
Przemek Stekiel476d9c42022-05-19 12:26:33 +0200131#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED */
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100132 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 TEST_ASSERT(0 == mbedtls_cipher_setup(ctx,
134 mbedtls_cipher_info_from_type(cipher_id)));
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100135 }
136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 TEST_ASSERT(0 == mbedtls_cipher_setkey(ctx, key->x, 8 * key->len,
138 direction));
139 return 1;
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100140
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100141exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return 0;
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100143}
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100144
145/*
146 * Check if a buffer is all-0 bytes:
147 * return 1 if it is,
148 * 0 if it isn't.
149 */
Michael Schusterb1e33fb2024-06-04 02:30:22 +0200150static int buffer_is_all_zero(const uint8_t *buf, size_t size)
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100151{
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 for (size_t i = 0; i < size; i++) {
153 if (buf[i] != 0) {
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100154 return 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 }
156 }
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100157 return 1;
158}
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100159#endif /* MBEDTLS_CIPHER_AUTH_CRYPT */
160
Paul Bakker33b43f12013-08-20 11:48:36 +0200161/* END_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000162
Paul Bakker33b43f12013-08-20 11:48:36 +0200163/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 * depends_on:MBEDTLS_CIPHER_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200165 * END_DEPENDENCIES
166 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000167
Paul Bakker33b43f12013-08-20 11:48:36 +0200168/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100169void mbedtls_cipher_list()
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100170{
171 const int *cipher_type;
172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 for (cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++) {
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200174 const mbedtls_cipher_info_t *info =
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 mbedtls_cipher_info_from_type(*cipher_type);
176 mbedtls_test_set_step(*cipher_type);
177 if (!check_cipher_info(*cipher_type, info)) {
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200178 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 }
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200180 }
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100181}
182/* END_CASE */
183
184/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100185void cipher_invalid_param_unconditional()
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200186{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500187 mbedtls_cipher_context_t valid_ctx;
188 mbedtls_cipher_context_t invalid_ctx;
189 mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
190 mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
191 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
192 int valid_size = sizeof(valid_buffer);
193 int valid_bitlen = valid_size * 8;
Valerio Settid3bdccc2023-10-04 12:09:06 +0200194 const int *cipher_list = mbedtls_cipher_list();
195 const mbedtls_cipher_info_t *valid_info;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500196 size_t size_t_var;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 (void) valid_mode; /* In some configurations this is unused */
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 mbedtls_cipher_init(&valid_ctx);
201 mbedtls_cipher_init(&invalid_ctx);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200202
Valerio Settid3bdccc2023-10-04 12:09:06 +0200203 /* Ensure that there is at least 1 supported cipher, otherwise exit gracefully */
Valerio Setti9d9b4b52023-10-19 10:51:03 +0200204 TEST_ASSUME(*cipher_list != 0);
Valerio Settid3bdccc2023-10-04 12:09:06 +0200205 valid_info = mbedtls_cipher_info_from_type(*cipher_list);
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, valid_info) == 0);
Gilles Peskinedc8ecda2021-12-10 14:28:31 +0100208
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500209 /* mbedtls_cipher_setup() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, NULL) ==
211 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200212
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500213 /* mbedtls_cipher_get_block_size() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 TEST_ASSERT(mbedtls_cipher_get_block_size(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200215
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500216 /* mbedtls_cipher_get_cipher_mode() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 TEST_ASSERT(mbedtls_cipher_get_cipher_mode(&invalid_ctx) ==
218 MBEDTLS_MODE_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200219
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500220 /* mbedtls_cipher_get_iv_size() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 TEST_ASSERT(mbedtls_cipher_get_iv_size(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200222
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500223 /* mbedtls_cipher_get_type() */
224 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 mbedtls_cipher_get_type(&invalid_ctx) ==
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500226 MBEDTLS_CIPHER_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200227
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500228 /* mbedtls_cipher_get_name() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 TEST_ASSERT(mbedtls_cipher_get_name(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200230
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500231 /* mbedtls_cipher_get_key_bitlen() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 TEST_ASSERT(mbedtls_cipher_get_key_bitlen(&invalid_ctx) ==
233 MBEDTLS_KEY_LENGTH_NONE);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500234
235 /* mbedtls_cipher_get_operation() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 TEST_ASSERT(mbedtls_cipher_get_operation(&invalid_ctx) ==
237 MBEDTLS_OPERATION_NONE);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500238
239 /* mbedtls_cipher_setkey() */
240 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 mbedtls_cipher_setkey(&invalid_ctx,
242 valid_buffer,
243 valid_bitlen,
244 valid_operation) ==
245 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500246
247 /* mbedtls_cipher_set_iv() */
248 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 mbedtls_cipher_set_iv(&invalid_ctx,
250 valid_buffer,
251 valid_size) ==
252 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500253
254 /* mbedtls_cipher_reset() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 TEST_ASSERT(mbedtls_cipher_reset(&invalid_ctx) ==
256 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200257
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200258#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500259 /* mbedtls_cipher_update_ad() */
260 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 mbedtls_cipher_update_ad(&invalid_ctx,
262 valid_buffer,
263 valid_size) ==
264 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500265#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
266
267#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
268 /* mbedtls_cipher_set_padding_mode() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 TEST_ASSERT(mbedtls_cipher_set_padding_mode(&invalid_ctx, valid_mode) ==
270 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200271#endif
272
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500273 /* mbedtls_cipher_update() */
274 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 mbedtls_cipher_update(&invalid_ctx,
276 valid_buffer,
277 valid_size,
278 valid_buffer,
279 &size_t_var) ==
280 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200281
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500282 /* mbedtls_cipher_finish() */
283 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 mbedtls_cipher_finish(&invalid_ctx,
285 valid_buffer,
286 &size_t_var) ==
287 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200288
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200289#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500290 /* mbedtls_cipher_write_tag() */
291 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 mbedtls_cipher_write_tag(&invalid_ctx,
293 valid_buffer,
294 valid_size) ==
295 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200296
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500297 /* mbedtls_cipher_check_tag() */
298 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 mbedtls_cipher_check_tag(&invalid_ctx,
300 valid_buffer,
301 valid_size) ==
302 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500303#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
304
305exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 mbedtls_cipher_free(&invalid_ctx);
307 mbedtls_cipher_free(&valid_ctx);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500308}
309/* END_CASE */
310
Tuvshinzaya Erdenekhuu6c689272022-07-29 14:45:55 +0100311/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100312void cipher_invalid_param_conditional()
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500313{
314 mbedtls_cipher_context_t valid_ctx;
315
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500316 mbedtls_operation_t invalid_operation = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500317 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
318 int valid_size = sizeof(valid_buffer);
319 int valid_bitlen = valid_size * 8;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500320
Ronald Cron875b5fb2021-05-21 08:50:00 +0200321 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500322 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 mbedtls_cipher_setkey(&valid_ctx,
324 valid_buffer,
325 valid_bitlen,
326 invalid_operation));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500327
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500328exit:
TRodziewicz70199552021-05-27 13:52:59 +0200329 ;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200330}
331/* END_CASE */
332
Paul Bakker6a9c7252016-07-14 13:46:10 +0100333/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100334void cipher_special_behaviours()
Paul Bakker6a9c7252016-07-14 13:46:10 +0100335{
336 const mbedtls_cipher_info_t *cipher_info;
337 mbedtls_cipher_context_t ctx;
338 unsigned char input[32];
339 unsigned char output[32];
Gilles Peskine449bd832023-01-11 14:50:10 +0100340#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100341 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300342#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100343 size_t olen = 0;
344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 mbedtls_cipher_init(&ctx);
346 memset(input, 0, sizeof(input));
347 memset(output, 0, sizeof(output));
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300348#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 memset(iv, 0, sizeof(iv));
Paul Bakker6a9c7252016-07-14 13:46:10 +0100350
351 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_CBC);
353 TEST_ASSERT(NULL != cipher_info);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Paul Bakker6a9c7252016-07-14 13:46:10 +0100356
357 /* IV too big */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1)
359 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100360
361 /* IV too small */
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, 0)
363 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 mbedtls_cipher_free(&ctx);
366 mbedtls_cipher_init(&ctx);
Ron Eldor6f90ed82017-09-26 12:08:54 +0300367#endif /* MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB);
369 TEST_ASSERT(NULL != cipher_info);
Ron Eldor7b012442017-09-25 17:03:12 +0300370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Ron Eldor7b012442017-09-25 17:03:12 +0300372
Paul Bakker6a9c7252016-07-14 13:46:10 +0100373 /* Update ECB with partial block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 TEST_ASSERT(mbedtls_cipher_update(&ctx, input, 1, output, &olen)
375 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100376
377exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 mbedtls_cipher_free(&ctx);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100379}
380/* END_CASE */
381
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200382/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100383void enc_dec_buf(int cipher_id, char *cipher_string, int key_len,
384 int length_val, int pad_mode)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200385{
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200386 size_t length = length_val, outlen, total_len, i, block_size, iv_len;
Jaeden Amerod906b812018-06-08 11:03:16 +0100387 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000388 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200389 unsigned char ad[13];
390 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200391 unsigned char inbuf[64];
392 unsigned char encbuf[64];
393 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 const mbedtls_cipher_info_t *cipher_info;
396 mbedtls_cipher_context_t ctx_dec;
397 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000398
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200399 /*
400 * Prepare contexts
401 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 mbedtls_cipher_init(&ctx_dec);
403 mbedtls_cipher_init(&ctx_enc);
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 memset(key, 0x2a, sizeof(key));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000406
407 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
409 TEST_ASSERT(NULL != cipher_info);
410 TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info);
411 TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info),
412 cipher_string) == 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000413
414 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
416 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT));
419 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 if (-1 != pad_mode) {
423 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode));
424 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode));
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200425 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200426#else
427 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200429
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200430 /*
431 * Do a few encode/decode cycles
432 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 for (i = 0; i < 3; i++) {
434 memset(iv, 0x00 + i, sizeof(iv));
435 memset(ad, 0x10 + i, sizeof(ad));
436 memset(inbuf, 0x20 + i, sizeof(inbuf));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 memset(encbuf, 0, sizeof(encbuf));
439 memset(decbuf, 0, sizeof(decbuf));
440 memset(tag, 0, sizeof(tag));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) {
443 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
444 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
445 } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
446 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
447 iv_len = 12;
448 } else {
449 iv_len = sizeof(iv);
450 }
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
453 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
456 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200457
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200458#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
460 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
461 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, ad, sizeof(ad) - i));
464 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, ad, sizeof(ad) - i));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200465#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 block_size = mbedtls_cipher_get_block_size(&ctx_enc);
468 TEST_ASSERT(block_size != 0);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 /* encode length number of bytes from inbuf */
471 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, length, encbuf, &outlen));
472 total_len = outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 TEST_ASSERT(total_len == length ||
475 (total_len % block_size == 0 &&
476 total_len < length &&
477 total_len + block_size > length));
Paul Bakker343a8702011-06-09 14:27:58 +0000478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + outlen, &outlen));
480 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000481
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200482#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 TEST_EQUAL(expected, mbedtls_cipher_write_tag(&ctx_enc, tag, sizeof(tag)));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200484#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 TEST_ASSERT(total_len == length ||
487 (total_len % block_size == 0 &&
488 total_len > length &&
489 total_len <= length + block_size));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 /* decode the previously encoded string */
492 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, total_len, decbuf, &outlen));
493 total_len = outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 TEST_ASSERT(total_len == length ||
496 (total_len % block_size == 0 &&
497 total_len < length &&
498 total_len + block_size >= length));
Paul Bakker343a8702011-06-09 14:27:58 +0000499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + outlen, &outlen));
501 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000502
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200503#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 TEST_EQUAL(expected, mbedtls_cipher_check_tag(&ctx_dec, tag, sizeof(tag)));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200505#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200506
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 /* check result */
508 TEST_ASSERT(total_len == length);
509 TEST_ASSERT(0 == memcmp(inbuf, decbuf, length));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200510 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000511
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200512 /*
513 * Done
514 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200515exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 mbedtls_cipher_free(&ctx_dec);
517 mbedtls_cipher_free(&ctx_enc);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200518}
Paul Bakker33b43f12013-08-20 11:48:36 +0200519/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000520
Paul Bakker33b43f12013-08-20 11:48:36 +0200521/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100522void enc_fail(int cipher_id, int pad_mode, int key_len, int length_val,
523 int ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200524{
Paul Bakker33b43f12013-08-20 11:48:36 +0200525 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200526 unsigned char key[32];
527 unsigned char iv[16];
528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 const mbedtls_cipher_info_t *cipher_info;
530 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200531
532 unsigned char inbuf[64];
533 unsigned char encbuf[64];
534
535 size_t outlen = 0;
536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 memset(key, 0, 32);
538 memset(iv, 0, 16);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 memset(inbuf, 5, 64);
543 memset(encbuf, 0, 64);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200544
545 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
547 TEST_ASSERT(NULL != cipher_info);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200548
549 /* Initialise context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
551 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key, key_len, MBEDTLS_ENCRYPT));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200554#else
555 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv, 16));
558 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200559#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
561 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100562 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200565#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200566
567 /* encode length number of bytes from inbuf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, inbuf, length, encbuf, &outlen));
569 TEST_ASSERT(ret == mbedtls_cipher_finish(&ctx, encbuf + outlen, &outlen));
Andre Goddard Rosa37117342024-05-01 11:47:12 -0500570 if (0 != ret) {
571 /* Check output parameter is set to the least-harmful value on error */
572 TEST_ASSERT(0 == outlen);
573 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200574
575 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200576exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 mbedtls_cipher_free(&ctx);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200578}
Paul Bakker33b43f12013-08-20 11:48:36 +0200579/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200580
Paul Bakker33b43f12013-08-20 11:48:36 +0200581/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100582void dec_empty_buf(int cipher,
583 int expected_update_ret,
584 int expected_finish_ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200585{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000586 unsigned char key[32];
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100587
588 unsigned char *iv = NULL;
589 size_t iv_len = 16;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 mbedtls_cipher_context_t ctx_dec;
592 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000593
594 unsigned char encbuf[64];
595 unsigned char decbuf[64];
596
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000597 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 memset(key, 0, 32);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 mbedtls_cipher_init(&ctx_dec);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 memset(encbuf, 0, 64);
604 memset(decbuf, 0, 64);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000605
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200606 /* Initialise context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 cipher_info = mbedtls_cipher_info_from_type(cipher);
608 TEST_ASSERT(NULL != cipher_info);
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
611 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100612 iv_len = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100614
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100615 TEST_CALLOC(iv, iv_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 memset(iv, 0, iv_len);
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100617
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100618 TEST_ASSERT(sizeof(key) * 8 >= mbedtls_cipher_info_get_key_bitlen(cipher_info));
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec,
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100623 key, mbedtls_cipher_info_get_key_bitlen(cipher_info),
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 MBEDTLS_DECRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200629
Waleed Elmelegyf4e66512023-09-21 14:04:35 +0100630#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Waleed Elmelegya7d206f2023-09-07 17:54:46 +0100631 if (ctx_dec.cipher_info->mode == MBEDTLS_MODE_CBC) {
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +0100632 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec,
633 MBEDTLS_PADDING_PKCS7));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +0100634 }
Waleed Elmelegyf4e66512023-09-21 14:04:35 +0100635#endif
Waleed Elmelegya7d206f2023-09-07 17:54:46 +0100636
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200637#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
639 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100640 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
641
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200643#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000644
645 /* decode 0-byte string */
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 TEST_ASSERT(expected_update_ret ==
647 mbedtls_cipher_update(&ctx_dec, encbuf, 0, decbuf, &outlen));
648 TEST_ASSERT(0 == outlen);
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (expected_finish_ret == 0 &&
651 (cipher_info->mode == MBEDTLS_MODE_CBC ||
652 cipher_info->mode == MBEDTLS_MODE_ECB)) {
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100653 /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
654 * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
k-stachowiakd8727232019-07-29 17:46:29 +0200655 * decrypting an empty buffer.
656 * On the other hand, CBC and ECB ciphers need a full block of input.
657 */
658 expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100659 }
660
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 TEST_ASSERT(expected_finish_ret == mbedtls_cipher_finish(
662 &ctx_dec, decbuf + outlen, &outlen));
663 TEST_ASSERT(0 == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664
Paul Bakkerbd51b262014-07-10 15:26:12 +0200665exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 mbedtls_free(iv);
667 mbedtls_cipher_free(&ctx_dec);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200668}
Paul Bakker33b43f12013-08-20 11:48:36 +0200669/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000670
Paul Bakker33b43f12013-08-20 11:48:36 +0200671/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100672void enc_dec_buf_multipart(int cipher_id, int key_len, int first_length_val,
673 int second_length_val, int pad_mode,
674 int first_encrypt_output_len, int second_encrypt_output_len,
675 int first_decrypt_output_len, int second_decrypt_output_len)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200676{
Paul Bakker33b43f12013-08-20 11:48:36 +0200677 size_t first_length = first_length_val;
678 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000679 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200680 size_t block_size;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200681 size_t iv_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000682 unsigned char key[32];
683 unsigned char iv[16];
684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_cipher_context_t ctx_dec;
686 mbedtls_cipher_context_t ctx_enc;
687 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000688
689 unsigned char inbuf[64];
690 unsigned char encbuf[64];
691 unsigned char decbuf[64];
692
Paul Bakker23986e52011-04-24 08:57:21 +0000693 size_t outlen = 0;
694 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 memset(key, 0, 32);
697 memset(iv, 0, 16);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 mbedtls_cipher_init(&ctx_dec);
700 mbedtls_cipher_init(&ctx_enc);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 memset(inbuf, 5, 64);
703 memset(encbuf, 0, 64);
704 memset(decbuf, 0, 64);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000705
706 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
708 TEST_ASSERT(NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
711 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT));
714 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000715
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700716#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (-1 != pad_mode) {
718 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode));
719 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode));
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700720 }
721#else
722 (void) pad_mode;
723#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) {
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200726 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
727 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
729 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100730 iv_len = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 } else {
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200732 iv_len = sizeof(iv);
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 }
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
736 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
739 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200740
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200741#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
743 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100744 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0));
747 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200748#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 block_size = mbedtls_cipher_get_block_size(&ctx_enc);
751 TEST_ASSERT(block_size != 0);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200752
Paul Bakker8123e9d2011-01-06 15:37:30 +0000753 /* encode length number of bytes from inbuf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, first_length, encbuf, &outlen));
755 TEST_ASSERT((size_t) first_encrypt_output_len == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000756 totaloutlen = outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 TEST_ASSERT(0 ==
758 mbedtls_cipher_update(&ctx_enc, inbuf + first_length, second_length,
759 encbuf + totaloutlen,
760 &outlen));
761 TEST_ASSERT((size_t) second_encrypt_output_len == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000762 totaloutlen += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 TEST_ASSERT(totaloutlen == length ||
764 (totaloutlen % block_size == 0 &&
765 totaloutlen < length &&
766 totaloutlen + block_size > length));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + totaloutlen, &outlen));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000769 totaloutlen += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 TEST_ASSERT(totaloutlen == length ||
771 (totaloutlen % block_size == 0 &&
772 totaloutlen > length &&
773 totaloutlen <= length + block_size));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000774
775 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700776 second_length = totaloutlen - first_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, first_length, decbuf, &outlen));
778 TEST_ASSERT((size_t) first_decrypt_output_len == outlen);
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200779 totaloutlen = outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 TEST_ASSERT(0 ==
781 mbedtls_cipher_update(&ctx_dec, encbuf + first_length, second_length,
782 decbuf + totaloutlen,
783 &outlen));
784 TEST_ASSERT((size_t) second_decrypt_output_len == outlen);
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700785 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 TEST_ASSERT(totaloutlen == length ||
788 (totaloutlen % block_size == 0 &&
789 totaloutlen < length &&
790 totaloutlen + block_size >= length));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + totaloutlen, &outlen));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200793 totaloutlen += outlen;
794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 TEST_ASSERT(totaloutlen == length);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 TEST_ASSERT(0 == memcmp(inbuf, decbuf, length));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000798
Paul Bakkerbd51b262014-07-10 15:26:12 +0200799exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 mbedtls_cipher_free(&ctx_dec);
801 mbedtls_cipher_free(&ctx_enc);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200802}
Paul Bakker33b43f12013-08-20 11:48:36 +0200803/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000804
Paul Bakker33b43f12013-08-20 11:48:36 +0200805/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806void decrypt_test_vec(int cipher_id, int pad_mode, data_t *key,
807 data_t *iv, data_t *cipher,
808 data_t *clear, data_t *ad, data_t *tag,
809 int finish_result, int tag_result)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200810{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200811 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200813 size_t outlen, total_len;
814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200816
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 memset(output, 0x00, sizeof(output));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200818
Azim Khanf1aaec92017-05-30 14:23:15 +0100819#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100820 ((void) ad);
821 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200822#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200823
824 /* Prepare context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
826 mbedtls_cipher_info_from_type(cipher_id)));
827 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 if (pad_mode != -1) {
830 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
831 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200832#else
833 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv->x, iv->len));
836 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200837#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 int expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
839 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100840 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, ad->x, ad->len));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200843#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200844
Azim Khand30ca132017-06-09 04:32:58 +0100845 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200846 total_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, cipher->x, cipher->len, output, &outlen));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200848 total_len += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen,
850 &outlen));
Andre Goddard Rosa37117342024-05-01 11:47:12 -0500851 if (0 != finish_result) {
852 /* Check output parameter is set to the least-harmful value on error */
853 TEST_ASSERT(0 == outlen);
854 }
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200855 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200856#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 int tag_expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
858 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100859 tag_result : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 TEST_EQUAL(tag_expected, mbedtls_cipher_check_tag(&ctx, tag->x, tag->len));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200862#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200863
864 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (0 == finish_result && 0 == tag_result) {
866 TEST_ASSERT(total_len == clear->len);
867 TEST_ASSERT(0 == memcmp(output, clear->x, clear->len));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200868 }
869
Paul Bakkerbd51b262014-07-10 15:26:12 +0200870exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 mbedtls_cipher_free(&ctx);
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200872}
873/* END_CASE */
874
Gilles Peskinedf00d452025-07-27 18:03:26 +0200875/* BEGIN_CASE */
876void decrypt_test_vec_cf(int cipher_id, int pad_mode, data_t *key,
877 data_t *iv, data_t *cipher,
878 data_t *clear, data_t *ad, data_t *tag,
879 int expected_finish_result, int tag_result)
880{
881 unsigned char output[265];
882 mbedtls_cipher_context_t ctx;
883 size_t outlen, total_len;
884
885 mbedtls_cipher_init(&ctx);
886
887 memset(output, 0x00, sizeof(output));
888
889#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
890 ((void) ad);
891 ((void) tag);
892#endif
893
894 TEST_CF_SECRET(key->x, key->len);
895 TEST_CF_SECRET(cipher->x, cipher->len);
896
897 /* Prepare context */
898 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
899 mbedtls_cipher_info_from_type(cipher_id)));
900 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT));
901#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
902 if (pad_mode != -1) {
903 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
904 }
905#else
906 (void) pad_mode;
907#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
908 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv->x, iv->len));
909 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
910#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
911 int expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
912 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
913 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
914
915 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, ad->x, ad->len));
916#endif
917
918 /* decode buffer and check tag->x */
919 total_len = 0;
920 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, cipher->x, cipher->len, output, &outlen));
921 total_len += outlen;
922 int actual_finish_result = mbedtls_cipher_finish(&ctx, output + outlen,
923 &outlen);
924 TEST_EQUAL(actual_finish_result, expected_finish_result);
925 if (0 != expected_finish_result) {
926 /* Check output parameter is set to the least-harmful value on error */
927 TEST_ASSERT(0 == outlen);
928 }
929 total_len += outlen;
930#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
931 int tag_expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
932 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
933 tag_result : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
934
935 TEST_EQUAL(tag_expected, mbedtls_cipher_check_tag(&ctx, tag->x, tag->len));
936#endif
937
938 /* check plaintext only if everything went fine */
939 if (0 == expected_finish_result && 0 == tag_result) {
940 TEST_CF_PUBLIC(output, sizeof(output));
941 TEST_MEMORY_COMPARE(output, total_len, clear->x, clear->len);
942 }
943
944exit:
945 mbedtls_cipher_free(&ctx);
946}
947/* END_CASE */
948
Gilles Peskine155de2a2025-07-27 18:53:57 +0200949/* BEGIN_CASE */
950void decrypt_padded_test_vec(int cipher_id, int pad_mode, data_t *key,
951 data_t *iv, data_t *cipher,
952 data_t *clear, data_t *ad, data_t *tag,
953 int expected_finish_result, int tag_result)
954{
955 unsigned char output[265];
956 mbedtls_cipher_context_t ctx;
957 size_t outlen, total_len;
958
959 mbedtls_cipher_init(&ctx);
960
961 memset(output, 0x00, sizeof(output));
962
963#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
964 ((void) ad);
965 ((void) tag);
966#endif
967
968 /* Prepare context */
969 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
970 mbedtls_cipher_info_from_type(cipher_id)));
971 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT));
972#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
973 if (pad_mode != -1) {
974 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
975 }
976#else
977 (void) pad_mode;
978#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
979 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv->x, iv->len));
980 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
981#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
982 int expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
983 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
984 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
985
986 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, ad->x, ad->len));
987#endif
988
989 /* decode buffer and check tag->x */
990 total_len = 0;
991 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, cipher->x, cipher->len, output, &outlen));
992 total_len += outlen;
993
994 size_t invalid_padding = 42;
995 int actual_finish_result =
996 mbedtls_cipher_finish_padded(&ctx, output + outlen, &outlen,
997 &invalid_padding);
998 switch (expected_finish_result) {
999 case 0:
1000 TEST_EQUAL(actual_finish_result, 0);
1001 TEST_EQUAL(invalid_padding, 0);
1002 break;
1003 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
1004 TEST_EQUAL(actual_finish_result, 0);
1005 TEST_EQUAL(invalid_padding, SIZE_MAX);
1006 break;
1007 default:
1008 TEST_EQUAL(actual_finish_result, expected_finish_result);
1009 /* Check output parameter is set to the least-harmful value on error */
1010 TEST_EQUAL(0, outlen);
1011 break;
1012 }
1013 total_len += outlen;
1014
1015#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
1016 int tag_expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
1017 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
1018 tag_result : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1019
1020 TEST_EQUAL(tag_expected, mbedtls_cipher_check_tag(&ctx, tag->x, tag->len));
1021#endif
1022
1023 /* check plaintext only if everything went fine */
1024 if (0 == expected_finish_result && 0 == tag_result) {
1025 TEST_ASSERT(total_len == clear->len);
1026 TEST_ASSERT(0 == memcmp(output, clear->x, clear->len));
1027 }
1028
1029exit:
1030 mbedtls_cipher_free(&ctx);
1031}
1032/* END_CASE */
1033
1034/* BEGIN_CASE */
1035void decrypt_padded_test_vec_cf(int cipher_id, int pad_mode, data_t *key,
1036 data_t *iv, data_t *cipher,
1037 data_t *clear, data_t *ad, data_t *tag,
1038 int expected_finish_result, int tag_result)
1039{
1040 unsigned char output[265];
1041 mbedtls_cipher_context_t ctx;
1042 size_t outlen, total_len;
1043
1044 mbedtls_cipher_init(&ctx);
1045
1046 memset(output, 0x00, sizeof(output));
1047
1048#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
1049 ((void) ad);
1050 ((void) tag);
1051#endif
1052
1053 TEST_CF_SECRET(key->x, key->len);
1054 TEST_CF_SECRET(cipher->x, cipher->len);
1055
1056 /* Prepare context */
1057 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
1058 mbedtls_cipher_info_from_type(cipher_id)));
1059 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT));
1060#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
1061 if (pad_mode != -1) {
1062 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
1063 }
1064#else
1065 (void) pad_mode;
1066#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
1067 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv->x, iv->len));
1068 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
1069#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
1070 int expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
1071 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
1072 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1073
1074 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, ad->x, ad->len));
1075#endif
1076
1077 /* decode buffer and check tag->x */
1078 total_len = 0;
1079 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, cipher->x, cipher->len, output, &outlen));
1080 total_len += outlen;
1081
1082 size_t invalid_padding = 42;
1083 int actual_finish_result =
1084 mbedtls_cipher_finish_padded(&ctx, output + outlen, &outlen,
1085 &invalid_padding);
1086 switch (expected_finish_result) {
1087 case 0:
1088 TEST_EQUAL(actual_finish_result, 0);
1089 TEST_EQUAL(invalid_padding, 0);
1090 break;
1091 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
1092 TEST_EQUAL(actual_finish_result, 0);
1093 TEST_EQUAL(invalid_padding, SIZE_MAX);
1094 break;
1095 default:
1096 TEST_EQUAL(actual_finish_result, expected_finish_result);
1097 /* Check output parameter is set to the least-harmful value on error */
1098 TEST_EQUAL(0, outlen);
1099 break;
1100 }
1101 total_len += outlen;
1102
1103#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
1104 int tag_expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
1105 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
1106 tag_result : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
1107
1108 TEST_EQUAL(tag_expected, mbedtls_cipher_check_tag(&ctx, tag->x, tag->len));
1109#endif
1110
1111 /* check plaintext only if everything went fine */
1112 if (0 == expected_finish_result && 0 == tag_result) {
1113 TEST_CF_PUBLIC(output, sizeof(output));
1114 TEST_MEMORY_COMPARE(output, total_len, clear->x, clear->len);
1115 }
1116
1117exit:
1118 mbedtls_cipher_free(&ctx);
1119}
1120/* END_CASE */
1121
Valerio Setti2f00b7a2023-10-17 11:43:34 +02001122/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */
Gilles Peskine449bd832023-01-11 14:50:10 +01001123void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv,
1124 data_t *ad, data_t *cipher, data_t *tag,
1125 char *result, data_t *clear, int use_psa)
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001126{
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +01001127 /*
1128 * Take an AEAD ciphertext + tag and perform a pair
1129 * of AEAD decryption and AEAD encryption. Check that
Hanno Beckera13272d2018-11-12 16:27:30 +00001130 * this results in the expected plaintext, and that
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +01001131 * decryption and encryption are inverse to one another.
1132 */
Hanno Beckera13272d2018-11-12 16:27:30 +00001133
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001134 int ret;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001135 int using_nist_kw, using_nist_kw_padding;
Hanno Beckera13272d2018-11-12 16:27:30 +00001136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001138 size_t outlen;
1139
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001140 unsigned char *cipher_plus_tag = NULL;
1141 size_t cipher_plus_tag_len;
1142 unsigned char *decrypt_buf = NULL;
1143 size_t decrypt_buf_len = 0;
1144 unsigned char *encrypt_buf = NULL;
1145 size_t encrypt_buf_len = 0;
1146
Gilles Peskine70edd682020-12-03 20:27:27 +01001147 /* Null pointers are documented as valid for inputs of length 0.
1148 * The test framework passes non-null pointers, so set them to NULL.
1149 * key, cipher and tag can't be empty. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 if (iv->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +01001151 iv->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 }
1153 if (ad->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +01001154 ad->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 }
1156 if (clear->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +01001157 clear->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 }
Gilles Peskine70edd682020-12-03 20:27:27 +01001159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001161
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +01001162 /* Initialize PSA Crypto */
1163#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 if (use_psa == 1) {
1165 PSA_ASSERT(psa_crypto_init());
1166 }
Hanno Beckera13272d2018-11-12 16:27:30 +00001167#else
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +01001168 (void) use_psa;
1169#endif
1170
1171 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001172 * Are we using NIST_KW? with padding?
1173 */
1174 using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
1175 cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
1176 cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
1177 using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
1178 cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
1179 cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
1180 using_nist_kw_padding;
1181
1182 /*
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +01001183 * Prepare context for decryption
1184 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key,
1186 MBEDTLS_DECRYPT)) {
Gilles Peskine8a3d2342020-12-03 21:06:15 +01001187 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 }
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001189
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +01001190 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001191 * prepare buffer for decryption
1192 * (we need the tag appended to the ciphertext)
1193 */
1194 cipher_plus_tag_len = cipher->len + tag->len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001195 TEST_CALLOC(cipher_plus_tag, cipher_plus_tag_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 memcpy(cipher_plus_tag, cipher->x, cipher->len);
1197 memcpy(cipher_plus_tag + cipher->len, tag->x, tag->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001198
1199 /*
1200 * Compute length of output buffer according to the documentation
1201 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if (using_nist_kw) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001203 decrypt_buf_len = cipher_plus_tag_len - 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 } else {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001205 decrypt_buf_len = cipher_plus_tag_len - tag->len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 }
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001207
1208
1209 /*
1210 * Try decrypting to a buffer that's 1B too small
1211 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 if (decrypt_buf_len != 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001213 TEST_CALLOC(decrypt_buf, decrypt_buf_len - 1);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001214
1215 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
1217 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
1218 decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len);
1219 TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 mbedtls_free(decrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001222 decrypt_buf = NULL;
1223 }
1224
1225 /*
1226 * Authenticate and decrypt, and check result
1227 */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001228 TEST_CALLOC(decrypt_buf, decrypt_buf_len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001229
1230 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
1232 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
1233 decrypt_buf, decrypt_buf_len, &outlen, tag->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 if (strcmp(result, "FAIL") == 0) {
1236 TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED);
1237 TEST_ASSERT(buffer_is_all_zero(decrypt_buf, decrypt_buf_len));
1238 } else {
1239 TEST_ASSERT(ret == 0);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +01001240 TEST_MEMORY_COMPARE(decrypt_buf, outlen, clear->x, clear->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001241 }
1242
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 mbedtls_free(decrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001244 decrypt_buf = NULL;
1245
1246 /*
1247 * Encrypt back if test data was authentic
1248 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 if (strcmp(result, "FAIL") != 0) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001250 /* prepare context for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key,
1252 MBEDTLS_ENCRYPT)) {
Gilles Peskine8a3d2342020-12-03 21:06:15 +01001253 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001254 }
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001255
1256 /*
1257 * Compute size of output buffer according to documentation
1258 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 if (using_nist_kw) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001260 encrypt_buf_len = clear->len + 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 if (using_nist_kw_padding && encrypt_buf_len % 8 != 0) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001262 encrypt_buf_len += 8 - encrypt_buf_len % 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 }
1264 } else {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001265 encrypt_buf_len = clear->len + tag->len;
1266 }
1267
1268 /*
1269 * Try encrypting with an output buffer that's 1B too small
1270 */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001271 TEST_CALLOC(encrypt_buf, encrypt_buf_len - 1);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001272
1273 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
1275 ad->x, ad->len, clear->x, clear->len,
1276 encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len);
1277 TEST_ASSERT(ret != 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 mbedtls_free(encrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001280 encrypt_buf = NULL;
1281
1282 /*
1283 * Encrypt and check the result
1284 */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001285 TEST_CALLOC(encrypt_buf, encrypt_buf_len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001286
1287 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
1289 ad->x, ad->len, clear->x, clear->len,
1290 encrypt_buf, encrypt_buf_len, &outlen, tag->len);
1291 TEST_ASSERT(ret == 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001292
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 TEST_ASSERT(outlen == cipher->len + tag->len);
1294 TEST_ASSERT(memcmp(encrypt_buf, cipher->x, cipher->len) == 0);
1295 TEST_ASSERT(memcmp(encrypt_buf + cipher->len,
1296 tag->x, tag->len) == 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 mbedtls_free(encrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001299 encrypt_buf = NULL;
1300 }
1301
Paul Bakkerbd51b262014-07-10 15:26:12 +02001302exit:
Hanno Beckera13272d2018-11-12 16:27:30 +00001303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 mbedtls_cipher_free(&ctx);
1305 mbedtls_free(decrypt_buf);
1306 mbedtls_free(encrypt_buf);
1307 mbedtls_free(cipher_plus_tag);
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001308
Hanno Beckera13272d2018-11-12 16:27:30 +00001309#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 if (use_psa == 1) {
1311 PSA_DONE();
1312 }
Hanno Beckera13272d2018-11-12 16:27:30 +00001313#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001314}
1315/* END_CASE */
1316
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +02001317/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001318void test_vec_ecb(int cipher_id, int operation, data_t *key,
1319 data_t *input, data_t *result, int finish_result
1320 )
Paul Bakker5e0efa72013-09-08 23:04:04 +02001321{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001323 unsigned char output[32];
1324 size_t outlen;
1325
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 memset(output, 0x00, sizeof(output));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001329
1330 /* Prepare context */
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
1332 mbedtls_cipher_info_from_type(cipher_id)));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001333
Paul Bakker5e0efa72013-09-08 23:04:04 +02001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, input->x,
1338 mbedtls_cipher_get_block_size(&ctx),
1339 output, &outlen));
1340 TEST_ASSERT(outlen == mbedtls_cipher_get_block_size(&ctx));
1341 TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen,
1342 &outlen));
1343 TEST_ASSERT(0 == outlen);
Paul Bakker5e0efa72013-09-08 23:04:04 +02001344
1345 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if (0 == finish_result) {
1347 TEST_ASSERT(0 == memcmp(output, result->x,
1348 mbedtls_cipher_get_block_size(&ctx)));
1349 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001350
Paul Bakkerbd51b262014-07-10 15:26:12 +02001351exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 mbedtls_cipher_free(&ctx);
Paul Bakker5e0efa72013-09-08 23:04:04 +02001353}
1354/* END_CASE */
1355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +01001357void test_vec_crypt(int cipher_id, int operation, data_t *key,
1358 data_t *iv, data_t *input, data_t *result,
1359 int finish_result, int use_psa)
Ron Eldor7b012442017-09-25 17:03:12 +03001360{
Ron Eldor7b012442017-09-25 17:03:12 +03001361 mbedtls_cipher_context_t ctx;
1362 unsigned char output[32];
1363 size_t outlen;
1364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 mbedtls_cipher_init(&ctx);
Ron Eldor7b012442017-09-25 17:03:12 +03001366
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 memset(output, 0x00, sizeof(output));
Ron Eldor7b012442017-09-25 17:03:12 +03001368
1369 /* Prepare context */
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -04001370#if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
Hanno Beckere43164e2018-11-12 12:46:35 +00001371 (void) use_psa;
1372#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 if (use_psa == 1) {
1374 PSA_ASSERT(psa_crypto_init());
1375 TEST_ASSERT(0 == mbedtls_cipher_setup_psa(&ctx,
1376 mbedtls_cipher_info_from_type(cipher_id), 0));
1377 } else
Przemek Stekiel476d9c42022-05-19 12:26:33 +02001378#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
1380 mbedtls_cipher_info_from_type(cipher_id)));
Ron Eldor7b012442017-09-25 17:03:12 +03001381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation));
1383 if (MBEDTLS_MODE_CBC == ctx.cipher_info->mode) {
1384 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE));
1385 }
Ron Eldor7b012442017-09-25 17:03:12 +03001386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 TEST_ASSERT(finish_result == mbedtls_cipher_crypt(&ctx, iv->len ? iv->x : NULL,
1388 iv->len, input->x, input->len,
1389 output, &outlen));
1390 TEST_ASSERT(result->len == outlen);
Ron Eldor7b012442017-09-25 17:03:12 +03001391 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 if (0 == finish_result) {
1393 TEST_ASSERT(0 == memcmp(output, result->x, outlen));
1394 }
Ron Eldor7b012442017-09-25 17:03:12 +03001395
1396exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 mbedtls_cipher_free(&ctx);
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -04001398#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 PSA_DONE();
Andrzej Kureked052792022-10-21 05:37:54 -04001400#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_TEST_DEPRECATED */
Ron Eldor7b012442017-09-25 17:03:12 +03001401}
1402/* END_CASE */
1403
1404/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine2da53282025-07-27 18:44:21 +02001405void test_vec_crypt_cf(int cipher_id, int pad_mode, int operation, data_t *key,
1406 data_t *iv, data_t *input, data_t *result,
1407 int expected_finish_result, int use_psa)
1408{
1409 mbedtls_cipher_context_t ctx;
1410 unsigned char output[32];
1411 size_t outlen;
1412
1413 mbedtls_cipher_init(&ctx);
1414
1415 memset(output, 0x00, sizeof(output));
1416
1417 TEST_CF_SECRET(key->x, key->len);
1418 TEST_CF_SECRET(input->x, input->len);
1419
1420 /* Prepare context */
1421#if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
1422 (void) use_psa;
1423#else
1424 if (use_psa == 1) {
1425 PSA_ASSERT(psa_crypto_init());
1426 TEST_ASSERT(0 == mbedtls_cipher_setup_psa(&ctx,
1427 mbedtls_cipher_info_from_type(cipher_id), 0));
1428 } else
1429#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED*/
1430 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
1431 mbedtls_cipher_info_from_type(cipher_id)));
1432
1433 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation));
1434 if (MBEDTLS_MODE_CBC == ctx.cipher_info->mode) {
1435 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
1436 }
1437
1438 int actual_finish_result =
1439 mbedtls_cipher_crypt(&ctx, iv->len ? iv->x : NULL, iv->len,
1440 input->x, input->len,
1441 output, &outlen);
1442 TEST_EQUAL(expected_finish_result, actual_finish_result);
1443
1444 /* check plaintext only if everything went fine */
1445 if (0 == expected_finish_result) {
1446 TEST_CF_PUBLIC(output, sizeof(output));
1447 TEST_MEMORY_COMPARE(output, outlen, result->x, result->len);
1448 }
1449
1450exit:
1451 mbedtls_cipher_free(&ctx);
1452#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_TEST_DEPRECATED)
1453 PSA_DONE();
1454#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_TEST_DEPRECATED */
1455}
1456/* END_CASE */
1457
1458/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +01001459void set_padding(int cipher_id, int pad_mode, int ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001460{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 const mbedtls_cipher_info_t *cipher_info;
1462 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1467 TEST_ASSERT(NULL != cipher_info);
1468 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 TEST_ASSERT(ret == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001471
Paul Bakkerbd51b262014-07-10 15:26:12 +02001472exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 mbedtls_cipher_free(&ctx);
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001474}
Paul Bakker33b43f12013-08-20 11:48:36 +02001475/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine6cb9f352025-07-27 21:22:39 +02001478void check_padding(int pad_mode, data_t *input,
1479 int expected_ret, int dlen_check)
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001480{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 mbedtls_cipher_info_t cipher_info;
1482 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +01001483 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001484
1485 /* build a fake context just for getting access to get_padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001488 ctx.cipher_info = &cipher_info;
1489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001491
Gilles Peskine6cb9f352025-07-27 21:22:39 +02001492 size_t invalid_padding = 42;
1493 int ret = ctx.get_padding(input->x, input->len, &dlen, &invalid_padding);
1494 switch (expected_ret) {
1495 case 0:
1496 TEST_EQUAL(ret, 0);
1497 TEST_EQUAL(invalid_padding, 0);
1498 TEST_EQUAL(dlen, dlen_check);
1499 break;
1500 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
1501 TEST_EQUAL(ret, 0);
1502 TEST_EQUAL(invalid_padding, SIZE_MAX);
1503 break;
1504 default:
1505 TEST_EQUAL(ret, expected_ret);
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001507}
Paul Bakker33b43f12013-08-20 11:48:36 +02001508/* END_CASE */
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001509
1510/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001511void iv_len_validity(int cipher_id, char *cipher_string,
1512 int iv_len_val, int ret)
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001513{
1514 size_t iv_len = iv_len_val;
1515 unsigned char iv[16];
1516
Thomas Daubney3a066ec2022-02-17 12:01:28 +00001517 /* Initialise iv buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 memset(iv, 0, sizeof(iv));
Thomas Daubney3a066ec2022-02-17 12:01:28 +00001519
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001520 const mbedtls_cipher_info_t *cipher_info;
1521 mbedtls_cipher_context_t ctx_dec;
1522 mbedtls_cipher_context_t ctx_enc;
1523
1524 /*
1525 * Prepare contexts
1526 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 mbedtls_cipher_init(&ctx_dec);
1528 mbedtls_cipher_init(&ctx_enc);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001529
1530 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1532 TEST_ASSERT(NULL != cipher_info);
1533 TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info);
1534 TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info),
1535 cipher_string) == 0);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001536
1537 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
1539 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
1542 TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001543
1544exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 mbedtls_cipher_free(&ctx_dec);
1546 mbedtls_cipher_free(&ctx_enc);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001547}
1548/* END_CASE */
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001549
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001550/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
1551void check_set_padding(int cipher_id)
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001552{
1553 mbedtls_cipher_context_t ctx;
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001554 unsigned char *key = NULL;
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001555 unsigned char iv[16] = { 0 };
1556 unsigned char input[16] = { 0 };
1557 unsigned char output[32] = { 0 };
1558 size_t outlen = 0;
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001559 const mbedtls_cipher_info_t *cipher_info;
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001560 size_t keylen = 0;
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001561
Paul Elliott3bda79b2023-10-18 15:09:09 +01001562 mbedtls_cipher_init(&ctx);
1563
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001564 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1565
1566 if (cipher_info->mode != MBEDTLS_MODE_CBC) {
1567 TEST_FAIL("Cipher mode must be CBC");
1568 }
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001569
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001570 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info);
1571 TEST_CALLOC(key, keylen/8);
1572 memset(key, 0, keylen/8);
1573
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001574 TEST_EQUAL(0, mbedtls_cipher_setup(&ctx, cipher_info));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001575
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001576 TEST_EQUAL(0, mbedtls_cipher_setkey(&ctx, key, keylen,
1577 MBEDTLS_ENCRYPT));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001578
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001579 TEST_EQUAL(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
1580 mbedtls_cipher_crypt(&ctx, iv, sizeof(iv), input,
1581 sizeof(input), output, &outlen));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001582
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001583 TEST_EQUAL(0, mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE));
1584 TEST_EQUAL(0, mbedtls_cipher_crypt(&ctx, iv, sizeof(iv), input,
1585 sizeof(input), output, &outlen));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001586
1587exit:
1588 mbedtls_cipher_free(&ctx);
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001589 mbedtls_free(key);
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001590}
1591/* END_CASE */
David Horstmannd37e0c42025-01-16 16:24:35 +00001592
1593/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
1594void get_pkcs_padding(data_t *decrypted_block, int exp_ret, int exp_len)
1595{
1596 int ret;
1597 size_t calculated_len;
Gilles Peskine6cb9f352025-07-27 21:22:39 +02001598 size_t invalid_padding;
David Horstmannd37e0c42025-01-16 16:24:35 +00001599
1600 TEST_CF_SECRET(decrypted_block->x, decrypted_block->len);
David Horstmann5a5440e2025-03-28 10:57:45 +00001601 ret = mbedtls_get_pkcs_padding(decrypted_block->x, decrypted_block->len,
Gilles Peskine6cb9f352025-07-27 21:22:39 +02001602 &calculated_len, &invalid_padding);
David Horstmannd37e0c42025-01-16 16:24:35 +00001603
Gilles Peskine6cb9f352025-07-27 21:22:39 +02001604 switch (exp_ret) {
1605 case 0:
1606 TEST_EQUAL(ret, 0);
1607 TEST_EQUAL(invalid_padding, 0);
1608 TEST_EQUAL(calculated_len, exp_len);
1609 break;
1610
1611 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
1612 TEST_EQUAL(ret, 0);
1613 TEST_EQUAL(invalid_padding, ~(size_t) 0);
1614 break;
1615
1616 default:
1617 TEST_EQUAL(ret, exp_ret);
1618 break;
David Horstmannd37e0c42025-01-16 16:24:35 +00001619 }
1620}
1621/* END_CASE */