blob: 2444ef9d8fecc58d98eba2332bedd4fd376b305c [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 Peskine0be02bd2021-07-19 16:32:54 +020017/* Check the internal consistency of a cipher info structure, and
18 * check it against mbedtls_cipher_info_from_xxx(). */
Gilles Peskine449bd832023-01-11 14:50:10 +010019static int check_cipher_info(mbedtls_cipher_type_t type,
20 const mbedtls_cipher_info_t *info)
Gilles Peskine0be02bd2021-07-19 16:32:54 +020021{
Max Fillinger72abd8a2021-11-28 14:02:36 +010022 size_t key_bitlen, block_size, iv_size;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020023
Gilles Peskine449bd832023-01-11 14:50:10 +010024 TEST_ASSERT(info != NULL);
25 TEST_EQUAL(type, mbedtls_cipher_info_get_type(info));
26 TEST_EQUAL(type, info->type);
27 TEST_ASSERT(mbedtls_cipher_info_from_type(type) == info);
Gilles Peskine0be02bd2021-07-19 16:32:54 +020028
Gilles Peskine449bd832023-01-11 14:50:10 +010029 TEST_EQUAL(info->mode, mbedtls_cipher_info_get_mode(info));
Gilles Peskine0be02bd2021-07-19 16:32:54 +020030
31 /* Insist that get_name() return the string from the structure and
32 * not a copy. A copy would have an unknown storage duration. */
Gilles Peskine449bd832023-01-11 14:50:10 +010033 TEST_ASSERT(mbedtls_cipher_info_get_name(info) == info->name);
34 TEST_ASSERT(mbedtls_cipher_info_from_string(info->name) == info);
Gilles Peskine0be02bd2021-07-19 16:32:54 +020035
Gilles Peskine449bd832023-01-11 14:50:10 +010036 key_bitlen = mbedtls_cipher_info_get_key_bitlen(info);
37 block_size = mbedtls_cipher_info_get_block_size(info);
38 iv_size = mbedtls_cipher_info_get_iv_size(info);
39 if (info->type == MBEDTLS_CIPHER_NULL) {
40 TEST_ASSERT(key_bitlen == 0);
41 TEST_ASSERT(block_size == 1);
42 TEST_ASSERT(iv_size == 0);
43 } else if (info->mode == MBEDTLS_MODE_XTS) {
44 TEST_ASSERT(key_bitlen == 256 ||
45 key_bitlen == 384 ||
46 key_bitlen == 512);
47 } else if (!strncmp(info->name, "DES-EDE3-", 9)) {
48 TEST_ASSERT(key_bitlen == 192);
49 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
50 TEST_ASSERT(block_size == 8);
51 } else if (!strncmp(info->name, "DES-EDE-", 8)) {
52 TEST_ASSERT(key_bitlen == 128);
53 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
54 TEST_ASSERT(block_size == 8);
55 } else if (!strncmp(info->name, "DES-", 4)) {
56 TEST_ASSERT(key_bitlen == 64);
57 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
58 TEST_ASSERT(block_size == 8);
59 } else if (!strncmp(info->name, "AES", 3)) {
60 TEST_ASSERT(key_bitlen == 128 ||
61 key_bitlen == 192 ||
62 key_bitlen == 256);
63 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
64 TEST_ASSERT(block_size == 16);
65 } else {
66 TEST_ASSERT(key_bitlen == 128 ||
67 key_bitlen == 192 ||
68 key_bitlen == 256);
Gilles Peskine6ac8f942021-09-01 08:31:49 +020069 }
Gilles Peskine4f4d4b22023-06-14 17:34:31 +020070 TEST_LE_U(key_bitlen, MBEDTLS_MAX_KEY_LENGTH * 8);
71 TEST_LE_U(block_size, MBEDTLS_MAX_BLOCK_LENGTH);
72 TEST_LE_U(iv_size, MBEDTLS_MAX_IV_LENGTH);
Gilles Peskine0be02bd2021-07-19 16:32:54 +020073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 if (strstr(info->name, "-ECB") != NULL) {
75 TEST_ASSERT(iv_size == 0);
76 TEST_ASSERT(!mbedtls_cipher_info_has_variable_iv_size(info));
77 } else if (strstr(info->name, "-CBC") != NULL ||
78 strstr(info->name, "-CTR") != NULL) {
79 TEST_ASSERT(iv_size == block_size);
80 TEST_ASSERT(!mbedtls_cipher_info_has_variable_iv_size(info));
81 } else if (strstr(info->name, "-GCM") != NULL) {
82 TEST_ASSERT(iv_size == block_size - 4);
83 TEST_ASSERT(mbedtls_cipher_info_has_variable_iv_size(info));
Max Fillingerc60c3a02021-11-09 22:38:56 +010084 }
85
Gilles Peskine449bd832023-01-11 14:50:10 +010086 return 1;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020087
88exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010089 return 0;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020090}
91
Valerio Setti2f00b7a2023-10-17 11:43:34 +020092#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010093/* Helper for resetting key/direction
94 *
95 * The documentation doesn't explicitly say whether calling
96 * mbedtls_cipher_setkey() twice is allowed or not. This currently works with
97 * the default software implementation, but only by accident. It isn't
98 * guaranteed to work with new ciphers or with alternative implementations of
99 * individual ciphers, and it doesn't work with the PSA wrappers. So don't do
100 * it, and instead start with a fresh context.
101 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100102static int cipher_reset_key(mbedtls_cipher_context_t *ctx, int cipher_id,
103 int use_psa, size_t tag_len, const data_t *key, int direction)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100104{
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 mbedtls_cipher_free(ctx);
106 mbedtls_cipher_init(ctx);
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100107
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -0400108#if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100109 (void) use_psa;
110 (void) tag_len;
111#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if (use_psa == 1) {
113 TEST_ASSERT(0 == mbedtls_cipher_setup_psa(ctx,
114 mbedtls_cipher_info_from_type(cipher_id),
115 tag_len));
116 } else
Przemek Stekiel476d9c42022-05-19 12:26:33 +0200117#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED */
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100118 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 TEST_ASSERT(0 == mbedtls_cipher_setup(ctx,
120 mbedtls_cipher_info_from_type(cipher_id)));
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100121 }
122
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 TEST_ASSERT(0 == mbedtls_cipher_setkey(ctx, key->x, 8 * key->len,
124 direction));
125 return 1;
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100126
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100127exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 return 0;
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100129}
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100130
131/*
132 * Check if a buffer is all-0 bytes:
133 * return 1 if it is,
134 * 0 if it isn't.
135 */
Michael Schusterb1e33fb2024-06-04 02:30:22 +0200136static int buffer_is_all_zero(const uint8_t *buf, size_t size)
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100137{
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 for (size_t i = 0; i < size; i++) {
139 if (buf[i] != 0) {
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100140 return 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 }
142 }
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100143 return 1;
144}
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100145#endif /* MBEDTLS_CIPHER_AUTH_CRYPT */
146
Paul Bakker33b43f12013-08-20 11:48:36 +0200147/* END_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000148
Paul Bakker33b43f12013-08-20 11:48:36 +0200149/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 * depends_on:MBEDTLS_CIPHER_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200151 * END_DEPENDENCIES
152 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000153
Paul Bakker33b43f12013-08-20 11:48:36 +0200154/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100155void mbedtls_cipher_list()
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100156{
157 const int *cipher_type;
158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 for (cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++) {
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200160 const mbedtls_cipher_info_t *info =
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 mbedtls_cipher_info_from_type(*cipher_type);
162 mbedtls_test_set_step(*cipher_type);
163 if (!check_cipher_info(*cipher_type, info)) {
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200164 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 }
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200166 }
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100167}
168/* END_CASE */
169
170/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100171void cipher_invalid_param_unconditional()
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200172{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500173 mbedtls_cipher_context_t valid_ctx;
174 mbedtls_cipher_context_t invalid_ctx;
175 mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
176 mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
177 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
178 int valid_size = sizeof(valid_buffer);
179 int valid_bitlen = valid_size * 8;
Valerio Settid3bdccc2023-10-04 12:09:06 +0200180 const int *cipher_list = mbedtls_cipher_list();
181 const mbedtls_cipher_info_t *valid_info;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500182 size_t size_t_var;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 (void) valid_mode; /* In some configurations this is unused */
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 mbedtls_cipher_init(&valid_ctx);
187 mbedtls_cipher_init(&invalid_ctx);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200188
Valerio Settid3bdccc2023-10-04 12:09:06 +0200189 /* Ensure that there is at least 1 supported cipher, otherwise exit gracefully */
Valerio Setti9d9b4b52023-10-19 10:51:03 +0200190 TEST_ASSUME(*cipher_list != 0);
Valerio Settid3bdccc2023-10-04 12:09:06 +0200191 valid_info = mbedtls_cipher_info_from_type(*cipher_list);
192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, valid_info) == 0);
Gilles Peskinedc8ecda2021-12-10 14:28:31 +0100194
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500195 /* mbedtls_cipher_setup() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, NULL) ==
197 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200198
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500199 /* mbedtls_cipher_get_block_size() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 TEST_ASSERT(mbedtls_cipher_get_block_size(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200201
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500202 /* mbedtls_cipher_get_cipher_mode() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 TEST_ASSERT(mbedtls_cipher_get_cipher_mode(&invalid_ctx) ==
204 MBEDTLS_MODE_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200205
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500206 /* mbedtls_cipher_get_iv_size() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 TEST_ASSERT(mbedtls_cipher_get_iv_size(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200208
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500209 /* mbedtls_cipher_get_type() */
210 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 mbedtls_cipher_get_type(&invalid_ctx) ==
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500212 MBEDTLS_CIPHER_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200213
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500214 /* mbedtls_cipher_get_name() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 TEST_ASSERT(mbedtls_cipher_get_name(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200216
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500217 /* mbedtls_cipher_get_key_bitlen() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 TEST_ASSERT(mbedtls_cipher_get_key_bitlen(&invalid_ctx) ==
219 MBEDTLS_KEY_LENGTH_NONE);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500220
221 /* mbedtls_cipher_get_operation() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 TEST_ASSERT(mbedtls_cipher_get_operation(&invalid_ctx) ==
223 MBEDTLS_OPERATION_NONE);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500224
225 /* mbedtls_cipher_setkey() */
226 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 mbedtls_cipher_setkey(&invalid_ctx,
228 valid_buffer,
229 valid_bitlen,
230 valid_operation) ==
231 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500232
233 /* mbedtls_cipher_set_iv() */
234 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 mbedtls_cipher_set_iv(&invalid_ctx,
236 valid_buffer,
237 valid_size) ==
238 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500239
240 /* mbedtls_cipher_reset() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 TEST_ASSERT(mbedtls_cipher_reset(&invalid_ctx) ==
242 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200243
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200244#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500245 /* mbedtls_cipher_update_ad() */
246 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 mbedtls_cipher_update_ad(&invalid_ctx,
248 valid_buffer,
249 valid_size) ==
250 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500251#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
252
253#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
254 /* mbedtls_cipher_set_padding_mode() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 TEST_ASSERT(mbedtls_cipher_set_padding_mode(&invalid_ctx, valid_mode) ==
256 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200257#endif
258
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500259 /* mbedtls_cipher_update() */
260 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 mbedtls_cipher_update(&invalid_ctx,
262 valid_buffer,
263 valid_size,
264 valid_buffer,
265 &size_t_var) ==
266 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200267
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500268 /* mbedtls_cipher_finish() */
269 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 mbedtls_cipher_finish(&invalid_ctx,
271 valid_buffer,
272 &size_t_var) ==
273 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200274
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200275#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500276 /* mbedtls_cipher_write_tag() */
277 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 mbedtls_cipher_write_tag(&invalid_ctx,
279 valid_buffer,
280 valid_size) ==
281 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200282
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500283 /* mbedtls_cipher_check_tag() */
284 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 mbedtls_cipher_check_tag(&invalid_ctx,
286 valid_buffer,
287 valid_size) ==
288 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500289#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
290
291exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 mbedtls_cipher_free(&invalid_ctx);
293 mbedtls_cipher_free(&valid_ctx);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500294}
295/* END_CASE */
296
Tuvshinzaya Erdenekhuu6c689272022-07-29 14:45:55 +0100297/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100298void cipher_invalid_param_conditional()
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500299{
300 mbedtls_cipher_context_t valid_ctx;
301
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500302 mbedtls_operation_t invalid_operation = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500303 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
304 int valid_size = sizeof(valid_buffer);
305 int valid_bitlen = valid_size * 8;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500306
Ronald Cron875b5fb2021-05-21 08:50:00 +0200307 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500308 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 mbedtls_cipher_setkey(&valid_ctx,
310 valid_buffer,
311 valid_bitlen,
312 invalid_operation));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500313
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500314exit:
TRodziewicz70199552021-05-27 13:52:59 +0200315 ;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200316}
317/* END_CASE */
318
Paul Bakker6a9c7252016-07-14 13:46:10 +0100319/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100320void cipher_special_behaviours()
Paul Bakker6a9c7252016-07-14 13:46:10 +0100321{
322 const mbedtls_cipher_info_t *cipher_info;
323 mbedtls_cipher_context_t ctx;
324 unsigned char input[32];
325 unsigned char output[32];
Gilles Peskine449bd832023-01-11 14:50:10 +0100326#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100327 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300328#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100329 size_t olen = 0;
330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 mbedtls_cipher_init(&ctx);
332 memset(input, 0, sizeof(input));
333 memset(output, 0, sizeof(output));
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300334#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 memset(iv, 0, sizeof(iv));
Paul Bakker6a9c7252016-07-14 13:46:10 +0100336
337 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_CBC);
339 TEST_ASSERT(NULL != cipher_info);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Paul Bakker6a9c7252016-07-14 13:46:10 +0100342
343 /* IV too big */
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1)
345 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100346
347 /* IV too small */
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, 0)
349 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 mbedtls_cipher_free(&ctx);
352 mbedtls_cipher_init(&ctx);
Ron Eldor6f90ed82017-09-26 12:08:54 +0300353#endif /* MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB);
355 TEST_ASSERT(NULL != cipher_info);
Ron Eldor7b012442017-09-25 17:03:12 +0300356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Ron Eldor7b012442017-09-25 17:03:12 +0300358
Paul Bakker6a9c7252016-07-14 13:46:10 +0100359 /* Update ECB with partial block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 TEST_ASSERT(mbedtls_cipher_update(&ctx, input, 1, output, &olen)
361 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100362
363exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 mbedtls_cipher_free(&ctx);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100365}
366/* END_CASE */
367
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200368/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100369void enc_dec_buf(int cipher_id, char *cipher_string, int key_len,
370 int length_val, int pad_mode)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200371{
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200372 size_t length = length_val, outlen, total_len, i, block_size, iv_len;
Jaeden Amerod906b812018-06-08 11:03:16 +0100373 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000374 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200375 unsigned char ad[13];
376 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200377 unsigned char inbuf[64];
378 unsigned char encbuf[64];
379 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 const mbedtls_cipher_info_t *cipher_info;
382 mbedtls_cipher_context_t ctx_dec;
383 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000384
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200385 /*
386 * Prepare contexts
387 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 mbedtls_cipher_init(&ctx_dec);
389 mbedtls_cipher_init(&ctx_enc);
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 memset(key, 0x2a, sizeof(key));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000392
393 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
395 TEST_ASSERT(NULL != cipher_info);
396 TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info);
397 TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info),
398 cipher_string) == 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000399
400 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
402 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT));
405 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (-1 != pad_mode) {
409 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode));
410 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode));
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200411 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200412#else
413 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200415
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200416 /*
417 * Do a few encode/decode cycles
418 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 for (i = 0; i < 3; i++) {
420 memset(iv, 0x00 + i, sizeof(iv));
421 memset(ad, 0x10 + i, sizeof(ad));
422 memset(inbuf, 0x20 + i, sizeof(inbuf));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 memset(encbuf, 0, sizeof(encbuf));
425 memset(decbuf, 0, sizeof(decbuf));
426 memset(tag, 0, sizeof(tag));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) {
429 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
430 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
431 } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
432 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
433 iv_len = 12;
434 } else {
435 iv_len = sizeof(iv);
436 }
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
439 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
442 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200443
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200444#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
446 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
447 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, ad, sizeof(ad) - i));
450 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, ad, sizeof(ad) - i));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200451#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 block_size = mbedtls_cipher_get_block_size(&ctx_enc);
454 TEST_ASSERT(block_size != 0);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 /* encode length number of bytes from inbuf */
457 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, length, encbuf, &outlen));
458 total_len = outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 TEST_ASSERT(total_len == length ||
461 (total_len % block_size == 0 &&
462 total_len < length &&
463 total_len + block_size > length));
Paul Bakker343a8702011-06-09 14:27:58 +0000464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + outlen, &outlen));
466 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000467
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200468#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 TEST_EQUAL(expected, mbedtls_cipher_write_tag(&ctx_enc, tag, sizeof(tag)));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200470#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 TEST_ASSERT(total_len == length ||
473 (total_len % block_size == 0 &&
474 total_len > length &&
475 total_len <= length + block_size));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 /* decode the previously encoded string */
478 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, total_len, decbuf, &outlen));
479 total_len = outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 TEST_ASSERT(total_len == length ||
482 (total_len % block_size == 0 &&
483 total_len < length &&
484 total_len + block_size >= length));
Paul Bakker343a8702011-06-09 14:27:58 +0000485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + outlen, &outlen));
487 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000488
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200489#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 TEST_EQUAL(expected, mbedtls_cipher_check_tag(&ctx_dec, tag, sizeof(tag)));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200491#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 /* check result */
494 TEST_ASSERT(total_len == length);
495 TEST_ASSERT(0 == memcmp(inbuf, decbuf, length));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200496 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000497
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200498 /*
499 * Done
500 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200501exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 mbedtls_cipher_free(&ctx_dec);
503 mbedtls_cipher_free(&ctx_enc);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200504}
Paul Bakker33b43f12013-08-20 11:48:36 +0200505/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000506
Paul Bakker33b43f12013-08-20 11:48:36 +0200507/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100508void enc_fail(int cipher_id, int pad_mode, int key_len, int length_val,
509 int ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200510{
Paul Bakker33b43f12013-08-20 11:48:36 +0200511 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200512 unsigned char key[32];
513 unsigned char iv[16];
514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 const mbedtls_cipher_info_t *cipher_info;
516 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200517
518 unsigned char inbuf[64];
519 unsigned char encbuf[64];
520
521 size_t outlen = 0;
522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 memset(key, 0, 32);
524 memset(iv, 0, 16);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 memset(inbuf, 5, 64);
529 memset(encbuf, 0, 64);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200530
531 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
533 TEST_ASSERT(NULL != cipher_info);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200534
535 /* Initialise context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
537 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key, key_len, MBEDTLS_ENCRYPT));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200540#else
541 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv, 16));
544 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200545#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
547 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100548 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200551#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200552
553 /* encode length number of bytes from inbuf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, inbuf, length, encbuf, &outlen));
555 TEST_ASSERT(ret == mbedtls_cipher_finish(&ctx, encbuf + outlen, &outlen));
Andre Goddard Rosa37117342024-05-01 11:47:12 -0500556 if (0 != ret) {
557 /* Check output parameter is set to the least-harmful value on error */
558 TEST_ASSERT(0 == outlen);
559 }
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200560
561 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200562exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 mbedtls_cipher_free(&ctx);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200564}
Paul Bakker33b43f12013-08-20 11:48:36 +0200565/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200566
Paul Bakker33b43f12013-08-20 11:48:36 +0200567/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100568void dec_empty_buf(int cipher,
569 int expected_update_ret,
570 int expected_finish_ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200571{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000572 unsigned char key[32];
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100573
574 unsigned char *iv = NULL;
575 size_t iv_len = 16;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_cipher_context_t ctx_dec;
578 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000579
580 unsigned char encbuf[64];
581 unsigned char decbuf[64];
582
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000583 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 memset(key, 0, 32);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 mbedtls_cipher_init(&ctx_dec);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 memset(encbuf, 0, 64);
590 memset(decbuf, 0, 64);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000591
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200592 /* Initialise context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 cipher_info = mbedtls_cipher_info_from_type(cipher);
594 TEST_ASSERT(NULL != cipher_info);
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
597 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100598 iv_len = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100600
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100601 TEST_CALLOC(iv, iv_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 memset(iv, 0, iv_len);
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100603
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100604 TEST_ASSERT(sizeof(key) * 8 >= mbedtls_cipher_info_get_key_bitlen(cipher_info));
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec,
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100609 key, mbedtls_cipher_info_get_key_bitlen(cipher_info),
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 MBEDTLS_DECRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200615
Waleed Elmelegyf4e66512023-09-21 14:04:35 +0100616#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Waleed Elmelegya7d206f2023-09-07 17:54:46 +0100617 if (ctx_dec.cipher_info->mode == MBEDTLS_MODE_CBC) {
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +0100618 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec,
619 MBEDTLS_PADDING_PKCS7));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +0100620 }
Waleed Elmelegyf4e66512023-09-21 14:04:35 +0100621#endif
Waleed Elmelegya7d206f2023-09-07 17:54:46 +0100622
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200623#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
625 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100626 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200629#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000630
631 /* decode 0-byte string */
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 TEST_ASSERT(expected_update_ret ==
633 mbedtls_cipher_update(&ctx_dec, encbuf, 0, decbuf, &outlen));
634 TEST_ASSERT(0 == outlen);
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 if (expected_finish_ret == 0 &&
637 (cipher_info->mode == MBEDTLS_MODE_CBC ||
638 cipher_info->mode == MBEDTLS_MODE_ECB)) {
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100639 /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
640 * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
k-stachowiakd8727232019-07-29 17:46:29 +0200641 * decrypting an empty buffer.
642 * On the other hand, CBC and ECB ciphers need a full block of input.
643 */
644 expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100645 }
646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 TEST_ASSERT(expected_finish_ret == mbedtls_cipher_finish(
648 &ctx_dec, decbuf + outlen, &outlen));
649 TEST_ASSERT(0 == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000650
Paul Bakkerbd51b262014-07-10 15:26:12 +0200651exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 mbedtls_free(iv);
653 mbedtls_cipher_free(&ctx_dec);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200654}
Paul Bakker33b43f12013-08-20 11:48:36 +0200655/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000656
Paul Bakker33b43f12013-08-20 11:48:36 +0200657/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100658void enc_dec_buf_multipart(int cipher_id, int key_len, int first_length_val,
659 int second_length_val, int pad_mode,
660 int first_encrypt_output_len, int second_encrypt_output_len,
661 int first_decrypt_output_len, int second_decrypt_output_len)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200662{
Paul Bakker33b43f12013-08-20 11:48:36 +0200663 size_t first_length = first_length_val;
664 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000665 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200666 size_t block_size;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200667 size_t iv_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000668 unsigned char key[32];
669 unsigned char iv[16];
670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_cipher_context_t ctx_dec;
672 mbedtls_cipher_context_t ctx_enc;
673 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000674
675 unsigned char inbuf[64];
676 unsigned char encbuf[64];
677 unsigned char decbuf[64];
678
Paul Bakker23986e52011-04-24 08:57:21 +0000679 size_t outlen = 0;
680 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 memset(key, 0, 32);
683 memset(iv, 0, 16);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 mbedtls_cipher_init(&ctx_dec);
686 mbedtls_cipher_init(&ctx_enc);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 memset(inbuf, 5, 64);
689 memset(encbuf, 0, 64);
690 memset(decbuf, 0, 64);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000691
692 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
694 TEST_ASSERT(NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
697 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT));
700 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000701
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700702#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if (-1 != pad_mode) {
704 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode));
705 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode));
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700706 }
707#else
708 (void) pad_mode;
709#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) {
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200712 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
713 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
715 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100716 iv_len = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 } else {
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200718 iv_len = sizeof(iv);
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 }
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
722 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
725 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200726
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200727#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
729 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100730 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0));
733 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200734#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 block_size = mbedtls_cipher_get_block_size(&ctx_enc);
737 TEST_ASSERT(block_size != 0);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200738
Paul Bakker8123e9d2011-01-06 15:37:30 +0000739 /* encode length number of bytes from inbuf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, first_length, encbuf, &outlen));
741 TEST_ASSERT((size_t) first_encrypt_output_len == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000742 totaloutlen = outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 TEST_ASSERT(0 ==
744 mbedtls_cipher_update(&ctx_enc, inbuf + first_length, second_length,
745 encbuf + totaloutlen,
746 &outlen));
747 TEST_ASSERT((size_t) second_encrypt_output_len == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000748 totaloutlen += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 TEST_ASSERT(totaloutlen == length ||
750 (totaloutlen % block_size == 0 &&
751 totaloutlen < length &&
752 totaloutlen + block_size > length));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + totaloutlen, &outlen));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000755 totaloutlen += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 TEST_ASSERT(totaloutlen == length ||
757 (totaloutlen % block_size == 0 &&
758 totaloutlen > length &&
759 totaloutlen <= length + block_size));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000760
761 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700762 second_length = totaloutlen - first_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, first_length, decbuf, &outlen));
764 TEST_ASSERT((size_t) first_decrypt_output_len == outlen);
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200765 totaloutlen = outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 TEST_ASSERT(0 ==
767 mbedtls_cipher_update(&ctx_dec, encbuf + first_length, second_length,
768 decbuf + totaloutlen,
769 &outlen));
770 TEST_ASSERT((size_t) second_decrypt_output_len == outlen);
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700771 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 TEST_ASSERT(totaloutlen == length ||
774 (totaloutlen % block_size == 0 &&
775 totaloutlen < length &&
776 totaloutlen + block_size >= length));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + totaloutlen, &outlen));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200779 totaloutlen += outlen;
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 TEST_ASSERT(totaloutlen == length);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 TEST_ASSERT(0 == memcmp(inbuf, decbuf, length));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000784
Paul Bakkerbd51b262014-07-10 15:26:12 +0200785exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 mbedtls_cipher_free(&ctx_dec);
787 mbedtls_cipher_free(&ctx_enc);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200788}
Paul Bakker33b43f12013-08-20 11:48:36 +0200789/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000790
Paul Bakker33b43f12013-08-20 11:48:36 +0200791/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792void decrypt_test_vec(int cipher_id, int pad_mode, data_t *key,
793 data_t *iv, data_t *cipher,
794 data_t *clear, data_t *ad, data_t *tag,
795 int finish_result, int tag_result)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200796{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200797 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200799 size_t outlen, total_len;
800
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 memset(output, 0x00, sizeof(output));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200804
Azim Khanf1aaec92017-05-30 14:23:15 +0100805#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100806 ((void) ad);
807 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200808#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200809
810 /* Prepare context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
812 mbedtls_cipher_info_from_type(cipher_id)));
813 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (pad_mode != -1) {
816 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
817 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200818#else
819 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv->x, iv->len));
822 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200823#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 int expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
825 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100826 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, ad->x, ad->len));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200829#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200830
Azim Khand30ca132017-06-09 04:32:58 +0100831 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200832 total_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, cipher->x, cipher->len, output, &outlen));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200834 total_len += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen,
836 &outlen));
Andre Goddard Rosa37117342024-05-01 11:47:12 -0500837 if (0 != finish_result) {
838 /* Check output parameter is set to the least-harmful value on error */
839 TEST_ASSERT(0 == outlen);
840 }
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200841 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200842#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 int tag_expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
844 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100845 tag_result : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 TEST_EQUAL(tag_expected, mbedtls_cipher_check_tag(&ctx, tag->x, tag->len));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200848#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200849
850 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 if (0 == finish_result && 0 == tag_result) {
852 TEST_ASSERT(total_len == clear->len);
853 TEST_ASSERT(0 == memcmp(output, clear->x, clear->len));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200854 }
855
Paul Bakkerbd51b262014-07-10 15:26:12 +0200856exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 mbedtls_cipher_free(&ctx);
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200858}
859/* END_CASE */
860
Valerio Setti2f00b7a2023-10-17 11:43:34 +0200861/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv,
863 data_t *ad, data_t *cipher, data_t *tag,
864 char *result, data_t *clear, int use_psa)
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200865{
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100866 /*
867 * Take an AEAD ciphertext + tag and perform a pair
868 * of AEAD decryption and AEAD encryption. Check that
Hanno Beckera13272d2018-11-12 16:27:30 +0000869 * this results in the expected plaintext, and that
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100870 * decryption and encryption are inverse to one another.
871 */
Hanno Beckera13272d2018-11-12 16:27:30 +0000872
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200873 int ret;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100874 int using_nist_kw, using_nist_kw_padding;
Hanno Beckera13272d2018-11-12 16:27:30 +0000875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200877 size_t outlen;
878
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100879 unsigned char *cipher_plus_tag = NULL;
880 size_t cipher_plus_tag_len;
881 unsigned char *decrypt_buf = NULL;
882 size_t decrypt_buf_len = 0;
883 unsigned char *encrypt_buf = NULL;
884 size_t encrypt_buf_len = 0;
885
Gilles Peskine70edd682020-12-03 20:27:27 +0100886 /* Null pointers are documented as valid for inputs of length 0.
887 * The test framework passes non-null pointers, so set them to NULL.
888 * key, cipher and tag can't be empty. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (iv->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100890 iv->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 }
892 if (ad->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100893 ad->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 }
895 if (clear->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100896 clear->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 }
Gilles Peskine70edd682020-12-03 20:27:27 +0100898
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200900
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100901 /* Initialize PSA Crypto */
902#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 if (use_psa == 1) {
904 PSA_ASSERT(psa_crypto_init());
905 }
Hanno Beckera13272d2018-11-12 16:27:30 +0000906#else
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100907 (void) use_psa;
908#endif
909
910 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100911 * Are we using NIST_KW? with padding?
912 */
913 using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
914 cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
915 cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
916 using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
917 cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
918 cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
919 using_nist_kw_padding;
920
921 /*
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100922 * Prepare context for decryption
923 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key,
925 MBEDTLS_DECRYPT)) {
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100926 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 }
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200928
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100929 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100930 * prepare buffer for decryption
931 * (we need the tag appended to the ciphertext)
932 */
933 cipher_plus_tag_len = cipher->len + tag->len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100934 TEST_CALLOC(cipher_plus_tag, cipher_plus_tag_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 memcpy(cipher_plus_tag, cipher->x, cipher->len);
936 memcpy(cipher_plus_tag + cipher->len, tag->x, tag->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100937
938 /*
939 * Compute length of output buffer according to the documentation
940 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if (using_nist_kw) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100942 decrypt_buf_len = cipher_plus_tag_len - 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 } else {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100944 decrypt_buf_len = cipher_plus_tag_len - tag->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 }
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100946
947
948 /*
949 * Try decrypting to a buffer that's 1B too small
950 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if (decrypt_buf_len != 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100952 TEST_CALLOC(decrypt_buf, decrypt_buf_len - 1);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100953
954 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
956 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
957 decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len);
958 TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100959
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 mbedtls_free(decrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100961 decrypt_buf = NULL;
962 }
963
964 /*
965 * Authenticate and decrypt, and check result
966 */
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100967 TEST_CALLOC(decrypt_buf, decrypt_buf_len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100968
969 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
971 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
972 decrypt_buf, decrypt_buf_len, &outlen, tag->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 if (strcmp(result, "FAIL") == 0) {
975 TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED);
976 TEST_ASSERT(buffer_is_all_zero(decrypt_buf, decrypt_buf_len));
977 } else {
978 TEST_ASSERT(ret == 0);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100979 TEST_MEMORY_COMPARE(decrypt_buf, outlen, clear->x, clear->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100980 }
981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 mbedtls_free(decrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100983 decrypt_buf = NULL;
984
985 /*
986 * Encrypt back if test data was authentic
987 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 if (strcmp(result, "FAIL") != 0) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100989 /* prepare context for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key,
991 MBEDTLS_ENCRYPT)) {
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100992 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 }
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100994
995 /*
996 * Compute size of output buffer according to documentation
997 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 if (using_nist_kw) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100999 encrypt_buf_len = clear->len + 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if (using_nist_kw_padding && encrypt_buf_len % 8 != 0) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001001 encrypt_buf_len += 8 - encrypt_buf_len % 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 }
1003 } else {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001004 encrypt_buf_len = clear->len + tag->len;
1005 }
1006
1007 /*
1008 * Try encrypting with an output buffer that's 1B too small
1009 */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001010 TEST_CALLOC(encrypt_buf, encrypt_buf_len - 1);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001011
1012 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
1014 ad->x, ad->len, clear->x, clear->len,
1015 encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len);
1016 TEST_ASSERT(ret != 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 mbedtls_free(encrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001019 encrypt_buf = NULL;
1020
1021 /*
1022 * Encrypt and check the result
1023 */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001024 TEST_CALLOC(encrypt_buf, encrypt_buf_len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001025
1026 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
1028 ad->x, ad->len, clear->x, clear->len,
1029 encrypt_buf, encrypt_buf_len, &outlen, tag->len);
1030 TEST_ASSERT(ret == 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 TEST_ASSERT(outlen == cipher->len + tag->len);
1033 TEST_ASSERT(memcmp(encrypt_buf, cipher->x, cipher->len) == 0);
1034 TEST_ASSERT(memcmp(encrypt_buf + cipher->len,
1035 tag->x, tag->len) == 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001036
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 mbedtls_free(encrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001038 encrypt_buf = NULL;
1039 }
1040
Paul Bakkerbd51b262014-07-10 15:26:12 +02001041exit:
Hanno Beckera13272d2018-11-12 16:27:30 +00001042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 mbedtls_cipher_free(&ctx);
1044 mbedtls_free(decrypt_buf);
1045 mbedtls_free(encrypt_buf);
1046 mbedtls_free(cipher_plus_tag);
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001047
Hanno Beckera13272d2018-11-12 16:27:30 +00001048#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if (use_psa == 1) {
1050 PSA_DONE();
1051 }
Hanno Beckera13272d2018-11-12 16:27:30 +00001052#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001053}
1054/* END_CASE */
1055
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +02001056/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001057void test_vec_ecb(int cipher_id, int operation, data_t *key,
1058 data_t *input, data_t *result, int finish_result
1059 )
Paul Bakker5e0efa72013-09-08 23:04:04 +02001060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001062 unsigned char output[32];
1063 size_t outlen;
1064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 memset(output, 0x00, sizeof(output));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001068
1069 /* Prepare context */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
1071 mbedtls_cipher_info_from_type(cipher_id)));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001072
Paul Bakker5e0efa72013-09-08 23:04:04 +02001073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, input->x,
1077 mbedtls_cipher_get_block_size(&ctx),
1078 output, &outlen));
1079 TEST_ASSERT(outlen == mbedtls_cipher_get_block_size(&ctx));
1080 TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen,
1081 &outlen));
1082 TEST_ASSERT(0 == outlen);
Paul Bakker5e0efa72013-09-08 23:04:04 +02001083
1084 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 if (0 == finish_result) {
1086 TEST_ASSERT(0 == memcmp(output, result->x,
1087 mbedtls_cipher_get_block_size(&ctx)));
1088 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001089
Paul Bakkerbd51b262014-07-10 15:26:12 +02001090exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 mbedtls_cipher_free(&ctx);
Paul Bakker5e0efa72013-09-08 23:04:04 +02001092}
1093/* END_CASE */
1094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +01001096void test_vec_crypt(int cipher_id, int operation, data_t *key,
1097 data_t *iv, data_t *input, data_t *result,
1098 int finish_result, int use_psa)
Ron Eldor7b012442017-09-25 17:03:12 +03001099{
Ron Eldor7b012442017-09-25 17:03:12 +03001100 mbedtls_cipher_context_t ctx;
1101 unsigned char output[32];
1102 size_t outlen;
1103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 mbedtls_cipher_init(&ctx);
Ron Eldor7b012442017-09-25 17:03:12 +03001105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 memset(output, 0x00, sizeof(output));
Ron Eldor7b012442017-09-25 17:03:12 +03001107
1108 /* Prepare context */
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -04001109#if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
Hanno Beckere43164e2018-11-12 12:46:35 +00001110 (void) use_psa;
1111#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if (use_psa == 1) {
1113 PSA_ASSERT(psa_crypto_init());
1114 TEST_ASSERT(0 == mbedtls_cipher_setup_psa(&ctx,
1115 mbedtls_cipher_info_from_type(cipher_id), 0));
1116 } else
Przemek Stekiel476d9c42022-05-19 12:26:33 +02001117#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
1119 mbedtls_cipher_info_from_type(cipher_id)));
Ron Eldor7b012442017-09-25 17:03:12 +03001120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation));
1122 if (MBEDTLS_MODE_CBC == ctx.cipher_info->mode) {
1123 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE));
1124 }
Ron Eldor7b012442017-09-25 17:03:12 +03001125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 TEST_ASSERT(finish_result == mbedtls_cipher_crypt(&ctx, iv->len ? iv->x : NULL,
1127 iv->len, input->x, input->len,
1128 output, &outlen));
1129 TEST_ASSERT(result->len == outlen);
Ron Eldor7b012442017-09-25 17:03:12 +03001130 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if (0 == finish_result) {
1132 TEST_ASSERT(0 == memcmp(output, result->x, outlen));
1133 }
Ron Eldor7b012442017-09-25 17:03:12 +03001134
1135exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 mbedtls_cipher_free(&ctx);
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -04001137#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 PSA_DONE();
Andrzej Kureked052792022-10-21 05:37:54 -04001139#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_TEST_DEPRECATED */
Ron Eldor7b012442017-09-25 17:03:12 +03001140}
1141/* END_CASE */
1142
1143/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +01001144void set_padding(int cipher_id, int pad_mode, int ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001145{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 const mbedtls_cipher_info_t *cipher_info;
1147 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1152 TEST_ASSERT(NULL != cipher_info);
1153 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 TEST_ASSERT(ret == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001156
Paul Bakkerbd51b262014-07-10 15:26:12 +02001157exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 mbedtls_cipher_free(&ctx);
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001159}
Paul Bakker33b43f12013-08-20 11:48:36 +02001160/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163void check_padding(int pad_mode, data_t *input, int ret, int dlen_check
1164 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001165{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 mbedtls_cipher_info_t cipher_info;
1167 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +01001168 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001169
1170 /* build a fake context just for getting access to get_padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001173 ctx.cipher_info = &cipher_info;
1174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001176
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 TEST_ASSERT(ret == ctx.get_padding(input->x, input->len, &dlen));
1179 if (0 == ret) {
1180 TEST_ASSERT(dlen == (size_t) dlen_check);
1181 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001182}
Paul Bakker33b43f12013-08-20 11:48:36 +02001183/* END_CASE */
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001184
1185/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001186void iv_len_validity(int cipher_id, char *cipher_string,
1187 int iv_len_val, int ret)
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001188{
1189 size_t iv_len = iv_len_val;
1190 unsigned char iv[16];
1191
Thomas Daubney3a066ec2022-02-17 12:01:28 +00001192 /* Initialise iv buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 memset(iv, 0, sizeof(iv));
Thomas Daubney3a066ec2022-02-17 12:01:28 +00001194
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001195 const mbedtls_cipher_info_t *cipher_info;
1196 mbedtls_cipher_context_t ctx_dec;
1197 mbedtls_cipher_context_t ctx_enc;
1198
1199 /*
1200 * Prepare contexts
1201 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 mbedtls_cipher_init(&ctx_dec);
1203 mbedtls_cipher_init(&ctx_enc);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001204
1205 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1207 TEST_ASSERT(NULL != cipher_info);
1208 TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info);
1209 TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info),
1210 cipher_string) == 0);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001211
1212 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
1214 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
1217 TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001218
1219exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 mbedtls_cipher_free(&ctx_dec);
1221 mbedtls_cipher_free(&ctx_enc);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001222}
1223/* END_CASE */
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001224
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001225/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
1226void check_set_padding(int cipher_id)
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001227{
1228 mbedtls_cipher_context_t ctx;
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001229 unsigned char *key = NULL;
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001230 unsigned char iv[16] = { 0 };
1231 unsigned char input[16] = { 0 };
1232 unsigned char output[32] = { 0 };
1233 size_t outlen = 0;
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001234 const mbedtls_cipher_info_t *cipher_info;
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001235 size_t keylen = 0;
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001236
Paul Elliott3bda79b2023-10-18 15:09:09 +01001237 mbedtls_cipher_init(&ctx);
1238
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001239 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1240
1241 if (cipher_info->mode != MBEDTLS_MODE_CBC) {
1242 TEST_FAIL("Cipher mode must be CBC");
1243 }
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001244
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001245 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info);
1246 TEST_CALLOC(key, keylen/8);
1247 memset(key, 0, keylen/8);
1248
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001249 TEST_EQUAL(0, mbedtls_cipher_setup(&ctx, cipher_info));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001250
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001251 TEST_EQUAL(0, mbedtls_cipher_setkey(&ctx, key, keylen,
1252 MBEDTLS_ENCRYPT));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001253
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001254 TEST_EQUAL(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
1255 mbedtls_cipher_crypt(&ctx, iv, sizeof(iv), input,
1256 sizeof(input), output, &outlen));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001257
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001258 TEST_EQUAL(0, mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE));
1259 TEST_EQUAL(0, mbedtls_cipher_crypt(&ctx, iv, sizeof(iv), input,
1260 sizeof(input), output, &outlen));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001261
1262exit:
1263 mbedtls_cipher_free(&ctx);
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001264 mbedtls_free(key);
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001265}
1266/* END_CASE */
David Horstmannd37e0c42025-01-16 16:24:35 +00001267
1268/* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */
1269void get_pkcs_padding(data_t *decrypted_block, int exp_ret, int exp_len)
1270{
1271 int ret;
1272 size_t calculated_len;
1273
1274 TEST_CF_SECRET(decrypted_block->x, decrypted_block->len);
1275 ret = get_pkcs_padding(decrypted_block->x, decrypted_block->len,
1276 &calculated_len);
1277 TEST_CF_PUBLIC(decrypted_block->x, decrypted_block->len);
1278
1279 TEST_EQUAL(ret, exp_ret);
1280 if (exp_ret == 0) {
1281 TEST_EQUAL(calculated_len, exp_len);
1282 }
1283}
1284/* END_CASE */