blob: 336357e844cad27efb325b409c6d34e4fc4ea305 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/cipher.h"
k-stachowiakd8727232019-07-29 17:46:29 +02003#include "mbedtls/aes.h"
k-stachowiakd8727232019-07-29 17:46:29 +02004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00006#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +02007#endif
Gilles Peskine5386f6b2019-08-01 12:47:40 +02008
Valerio Settidcee9872023-10-16 11:35:57 +02009#if defined(MBEDTLS_CIPHER_HAVE_SOME_AEAD_VIA_LEGACY_OR_USE_PSA) || defined(MBEDTLS_NIST_KW_C)
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +010010#define MBEDTLS_CIPHER_AUTH_CRYPT
11#endif
12
Gilles Peskine0be02bd2021-07-19 16:32:54 +020013/* Check the internal consistency of a cipher info structure, and
14 * check it against mbedtls_cipher_info_from_xxx(). */
Gilles Peskine449bd832023-01-11 14:50:10 +010015static int check_cipher_info(mbedtls_cipher_type_t type,
16 const mbedtls_cipher_info_t *info)
Gilles Peskine0be02bd2021-07-19 16:32:54 +020017{
Max Fillinger72abd8a2021-11-28 14:02:36 +010018 size_t key_bitlen, block_size, iv_size;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020019
Gilles Peskine449bd832023-01-11 14:50:10 +010020 TEST_ASSERT(info != NULL);
21 TEST_EQUAL(type, mbedtls_cipher_info_get_type(info));
22 TEST_EQUAL(type, info->type);
23 TEST_ASSERT(mbedtls_cipher_info_from_type(type) == info);
Gilles Peskine0be02bd2021-07-19 16:32:54 +020024
Gilles Peskine449bd832023-01-11 14:50:10 +010025 TEST_EQUAL(info->mode, mbedtls_cipher_info_get_mode(info));
Gilles Peskine0be02bd2021-07-19 16:32:54 +020026
27 /* Insist that get_name() return the string from the structure and
28 * not a copy. A copy would have an unknown storage duration. */
Gilles Peskine449bd832023-01-11 14:50:10 +010029 TEST_ASSERT(mbedtls_cipher_info_get_name(info) == info->name);
30 TEST_ASSERT(mbedtls_cipher_info_from_string(info->name) == info);
Gilles Peskine0be02bd2021-07-19 16:32:54 +020031
Gilles Peskine449bd832023-01-11 14:50:10 +010032 key_bitlen = mbedtls_cipher_info_get_key_bitlen(info);
33 block_size = mbedtls_cipher_info_get_block_size(info);
34 iv_size = mbedtls_cipher_info_get_iv_size(info);
35 if (info->type == MBEDTLS_CIPHER_NULL) {
36 TEST_ASSERT(key_bitlen == 0);
37 TEST_ASSERT(block_size == 1);
38 TEST_ASSERT(iv_size == 0);
39 } else if (info->mode == MBEDTLS_MODE_XTS) {
40 TEST_ASSERT(key_bitlen == 256 ||
41 key_bitlen == 384 ||
42 key_bitlen == 512);
43 } else if (!strncmp(info->name, "DES-EDE3-", 9)) {
44 TEST_ASSERT(key_bitlen == 192);
45 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
46 TEST_ASSERT(block_size == 8);
47 } else if (!strncmp(info->name, "DES-EDE-", 8)) {
48 TEST_ASSERT(key_bitlen == 128);
49 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
50 TEST_ASSERT(block_size == 8);
51 } else if (!strncmp(info->name, "DES-", 4)) {
52 TEST_ASSERT(key_bitlen == 64);
53 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
54 TEST_ASSERT(block_size == 8);
55 } else if (!strncmp(info->name, "AES", 3)) {
56 TEST_ASSERT(key_bitlen == 128 ||
57 key_bitlen == 192 ||
58 key_bitlen == 256);
59 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
60 TEST_ASSERT(block_size == 16);
61 } else {
62 TEST_ASSERT(key_bitlen == 128 ||
63 key_bitlen == 192 ||
64 key_bitlen == 256);
Gilles Peskine6ac8f942021-09-01 08:31:49 +020065 }
Gilles Peskine4f4d4b22023-06-14 17:34:31 +020066 TEST_LE_U(key_bitlen, MBEDTLS_MAX_KEY_LENGTH * 8);
67 TEST_LE_U(block_size, MBEDTLS_MAX_BLOCK_LENGTH);
68 TEST_LE_U(iv_size, MBEDTLS_MAX_IV_LENGTH);
Gilles Peskine0be02bd2021-07-19 16:32:54 +020069
Gilles Peskine449bd832023-01-11 14:50:10 +010070 if (strstr(info->name, "-ECB") != NULL) {
71 TEST_ASSERT(iv_size == 0);
72 TEST_ASSERT(!mbedtls_cipher_info_has_variable_iv_size(info));
73 } else if (strstr(info->name, "-CBC") != NULL ||
74 strstr(info->name, "-CTR") != NULL) {
75 TEST_ASSERT(iv_size == block_size);
76 TEST_ASSERT(!mbedtls_cipher_info_has_variable_iv_size(info));
77 } else if (strstr(info->name, "-GCM") != NULL) {
78 TEST_ASSERT(iv_size == block_size - 4);
79 TEST_ASSERT(mbedtls_cipher_info_has_variable_iv_size(info));
Max Fillingerc60c3a02021-11-09 22:38:56 +010080 }
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082 return 1;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020083
84exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010085 return 0;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020086}
87
Valerio Setti2f00b7a2023-10-17 11:43:34 +020088#if defined(MBEDTLS_CIPHER_MODE_AEAD)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010089/* Helper for resetting key/direction
90 *
91 * The documentation doesn't explicitly say whether calling
92 * mbedtls_cipher_setkey() twice is allowed or not. This currently works with
93 * the default software implementation, but only by accident. It isn't
94 * guaranteed to work with new ciphers or with alternative implementations of
95 * individual ciphers, and it doesn't work with the PSA wrappers. So don't do
96 * it, and instead start with a fresh context.
97 */
Gilles Peskine449bd832023-01-11 14:50:10 +010098static int cipher_reset_key(mbedtls_cipher_context_t *ctx, int cipher_id,
99 int use_psa, size_t tag_len, const data_t *key, int direction)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100100{
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 mbedtls_cipher_free(ctx);
102 mbedtls_cipher_init(ctx);
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100103
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -0400104#if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100105 (void) use_psa;
106 (void) tag_len;
107#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 if (use_psa == 1) {
109 TEST_ASSERT(0 == mbedtls_cipher_setup_psa(ctx,
110 mbedtls_cipher_info_from_type(cipher_id),
111 tag_len));
112 } else
Przemek Stekiel476d9c42022-05-19 12:26:33 +0200113#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED */
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100114 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 TEST_ASSERT(0 == mbedtls_cipher_setup(ctx,
116 mbedtls_cipher_info_from_type(cipher_id)));
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100117 }
118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 TEST_ASSERT(0 == mbedtls_cipher_setkey(ctx, key->x, 8 * key->len,
120 direction));
121 return 1;
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100122
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100123exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 return 0;
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100125}
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100126
127/*
128 * Check if a buffer is all-0 bytes:
129 * return 1 if it is,
130 * 0 if it isn't.
131 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100132int buffer_is_all_zero(const uint8_t *buf, size_t size)
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100133{
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 for (size_t i = 0; i < size; i++) {
135 if (buf[i] != 0) {
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100136 return 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 }
138 }
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100139 return 1;
140}
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100141#endif /* MBEDTLS_CIPHER_AUTH_CRYPT */
142
Paul Bakker33b43f12013-08-20 11:48:36 +0200143/* END_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000144
Paul Bakker33b43f12013-08-20 11:48:36 +0200145/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 * depends_on:MBEDTLS_CIPHER_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200147 * END_DEPENDENCIES
148 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000149
Paul Bakker33b43f12013-08-20 11:48:36 +0200150/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100151void mbedtls_cipher_list()
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100152{
153 const int *cipher_type;
154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 for (cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++) {
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200156 const mbedtls_cipher_info_t *info =
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 mbedtls_cipher_info_from_type(*cipher_type);
158 mbedtls_test_set_step(*cipher_type);
159 if (!check_cipher_info(*cipher_type, info)) {
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200160 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 }
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200162 }
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100163}
164/* END_CASE */
165
166/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167void cipher_invalid_param_unconditional()
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200168{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500169 mbedtls_cipher_context_t valid_ctx;
170 mbedtls_cipher_context_t invalid_ctx;
171 mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
172 mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
173 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
174 int valid_size = sizeof(valid_buffer);
175 int valid_bitlen = valid_size * 8;
Valerio Settid3bdccc2023-10-04 12:09:06 +0200176 const int *cipher_list = mbedtls_cipher_list();
177 const mbedtls_cipher_info_t *valid_info;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500178 size_t size_t_var;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 (void) valid_mode; /* In some configurations this is unused */
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 mbedtls_cipher_init(&valid_ctx);
183 mbedtls_cipher_init(&invalid_ctx);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200184
Valerio Settid3bdccc2023-10-04 12:09:06 +0200185 /* Ensure that there is at least 1 supported cipher, otherwise exit gracefully */
186 if (*cipher_list == 0) {
187 goto exit;
188 }
189 valid_info = mbedtls_cipher_info_from_type(*cipher_list);
190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, valid_info) == 0);
Gilles Peskinedc8ecda2021-12-10 14:28:31 +0100192
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500193 /* mbedtls_cipher_setup() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, NULL) ==
195 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200196
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500197 /* mbedtls_cipher_get_block_size() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 TEST_ASSERT(mbedtls_cipher_get_block_size(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200199
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500200 /* mbedtls_cipher_get_cipher_mode() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 TEST_ASSERT(mbedtls_cipher_get_cipher_mode(&invalid_ctx) ==
202 MBEDTLS_MODE_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200203
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500204 /* mbedtls_cipher_get_iv_size() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 TEST_ASSERT(mbedtls_cipher_get_iv_size(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200206
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500207 /* mbedtls_cipher_get_type() */
208 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 mbedtls_cipher_get_type(&invalid_ctx) ==
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500210 MBEDTLS_CIPHER_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200211
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500212 /* mbedtls_cipher_get_name() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 TEST_ASSERT(mbedtls_cipher_get_name(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200214
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500215 /* mbedtls_cipher_get_key_bitlen() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 TEST_ASSERT(mbedtls_cipher_get_key_bitlen(&invalid_ctx) ==
217 MBEDTLS_KEY_LENGTH_NONE);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500218
219 /* mbedtls_cipher_get_operation() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 TEST_ASSERT(mbedtls_cipher_get_operation(&invalid_ctx) ==
221 MBEDTLS_OPERATION_NONE);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500222
223 /* mbedtls_cipher_setkey() */
224 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 mbedtls_cipher_setkey(&invalid_ctx,
226 valid_buffer,
227 valid_bitlen,
228 valid_operation) ==
229 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500230
231 /* mbedtls_cipher_set_iv() */
232 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 mbedtls_cipher_set_iv(&invalid_ctx,
234 valid_buffer,
235 valid_size) ==
236 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500237
238 /* mbedtls_cipher_reset() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 TEST_ASSERT(mbedtls_cipher_reset(&invalid_ctx) ==
240 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200241
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200242#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500243 /* mbedtls_cipher_update_ad() */
244 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 mbedtls_cipher_update_ad(&invalid_ctx,
246 valid_buffer,
247 valid_size) ==
248 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500249#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
250
251#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
252 /* mbedtls_cipher_set_padding_mode() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 TEST_ASSERT(mbedtls_cipher_set_padding_mode(&invalid_ctx, valid_mode) ==
254 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200255#endif
256
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500257 /* mbedtls_cipher_update() */
258 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 mbedtls_cipher_update(&invalid_ctx,
260 valid_buffer,
261 valid_size,
262 valid_buffer,
263 &size_t_var) ==
264 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200265
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500266 /* mbedtls_cipher_finish() */
267 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 mbedtls_cipher_finish(&invalid_ctx,
269 valid_buffer,
270 &size_t_var) ==
271 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200272
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200273#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500274 /* mbedtls_cipher_write_tag() */
275 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 mbedtls_cipher_write_tag(&invalid_ctx,
277 valid_buffer,
278 valid_size) ==
279 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200280
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500281 /* mbedtls_cipher_check_tag() */
282 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 mbedtls_cipher_check_tag(&invalid_ctx,
284 valid_buffer,
285 valid_size) ==
286 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500287#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
288
289exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 mbedtls_cipher_free(&invalid_ctx);
291 mbedtls_cipher_free(&valid_ctx);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500292}
293/* END_CASE */
294
Tuvshinzaya Erdenekhuu6c689272022-07-29 14:45:55 +0100295/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100296void cipher_invalid_param_conditional()
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500297{
298 mbedtls_cipher_context_t valid_ctx;
299
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500300 mbedtls_operation_t invalid_operation = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500301 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
302 int valid_size = sizeof(valid_buffer);
303 int valid_bitlen = valid_size * 8;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500304
Ronald Cron875b5fb2021-05-21 08:50:00 +0200305 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500306 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 mbedtls_cipher_setkey(&valid_ctx,
308 valid_buffer,
309 valid_bitlen,
310 invalid_operation));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500311
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500312exit:
TRodziewicz70199552021-05-27 13:52:59 +0200313 ;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200314}
315/* END_CASE */
316
Paul Bakker6a9c7252016-07-14 13:46:10 +0100317/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318void cipher_special_behaviours()
Paul Bakker6a9c7252016-07-14 13:46:10 +0100319{
320 const mbedtls_cipher_info_t *cipher_info;
321 mbedtls_cipher_context_t ctx;
322 unsigned char input[32];
323 unsigned char output[32];
Gilles Peskine449bd832023-01-11 14:50:10 +0100324#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100325 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300326#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100327 size_t olen = 0;
328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 mbedtls_cipher_init(&ctx);
330 memset(input, 0, sizeof(input));
331 memset(output, 0, sizeof(output));
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300332#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 memset(iv, 0, sizeof(iv));
Paul Bakker6a9c7252016-07-14 13:46:10 +0100334
335 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_CBC);
337 TEST_ASSERT(NULL != cipher_info);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Paul Bakker6a9c7252016-07-14 13:46:10 +0100340
341 /* IV too big */
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1)
343 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100344
345 /* IV too small */
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, 0)
347 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 mbedtls_cipher_free(&ctx);
350 mbedtls_cipher_init(&ctx);
Ron Eldor6f90ed82017-09-26 12:08:54 +0300351#endif /* MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB);
353 TEST_ASSERT(NULL != cipher_info);
Ron Eldor7b012442017-09-25 17:03:12 +0300354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Ron Eldor7b012442017-09-25 17:03:12 +0300356
Paul Bakker6a9c7252016-07-14 13:46:10 +0100357 /* Update ECB with partial block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 TEST_ASSERT(mbedtls_cipher_update(&ctx, input, 1, output, &olen)
359 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100360
361exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 mbedtls_cipher_free(&ctx);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100363}
364/* END_CASE */
365
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200366/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367void enc_dec_buf(int cipher_id, char *cipher_string, int key_len,
368 int length_val, int pad_mode)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200369{
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200370 size_t length = length_val, outlen, total_len, i, block_size, iv_len;
Jaeden Amerod906b812018-06-08 11:03:16 +0100371 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000372 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200373 unsigned char ad[13];
374 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200375 unsigned char inbuf[64];
376 unsigned char encbuf[64];
377 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 const mbedtls_cipher_info_t *cipher_info;
380 mbedtls_cipher_context_t ctx_dec;
381 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000382
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200383 /*
384 * Prepare contexts
385 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 mbedtls_cipher_init(&ctx_dec);
387 mbedtls_cipher_init(&ctx_enc);
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 memset(key, 0x2a, sizeof(key));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000390
391 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
393 TEST_ASSERT(NULL != cipher_info);
394 TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info);
395 TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info),
396 cipher_string) == 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000397
398 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
400 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT));
403 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 if (-1 != pad_mode) {
407 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode));
408 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode));
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200409 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200410#else
411 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200413
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200414 /*
415 * Do a few encode/decode cycles
416 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 for (i = 0; i < 3; i++) {
418 memset(iv, 0x00 + i, sizeof(iv));
419 memset(ad, 0x10 + i, sizeof(ad));
420 memset(inbuf, 0x20 + i, sizeof(inbuf));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 memset(encbuf, 0, sizeof(encbuf));
423 memset(decbuf, 0, sizeof(decbuf));
424 memset(tag, 0, sizeof(tag));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) {
427 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
428 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
429 } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
430 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
431 iv_len = 12;
432 } else {
433 iv_len = sizeof(iv);
434 }
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
437 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
440 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200441
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200442#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
444 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
445 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, ad, sizeof(ad) - i));
448 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, ad, sizeof(ad) - i));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200449#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 block_size = mbedtls_cipher_get_block_size(&ctx_enc);
452 TEST_ASSERT(block_size != 0);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 /* encode length number of bytes from inbuf */
455 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, length, encbuf, &outlen));
456 total_len = outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 TEST_ASSERT(total_len == length ||
459 (total_len % block_size == 0 &&
460 total_len < length &&
461 total_len + block_size > length));
Paul Bakker343a8702011-06-09 14:27:58 +0000462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + outlen, &outlen));
464 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000465
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200466#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 TEST_EQUAL(expected, mbedtls_cipher_write_tag(&ctx_enc, tag, sizeof(tag)));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200468#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 TEST_ASSERT(total_len == length ||
471 (total_len % block_size == 0 &&
472 total_len > length &&
473 total_len <= length + block_size));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 /* decode the previously encoded string */
476 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, total_len, decbuf, &outlen));
477 total_len = outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 TEST_ASSERT(total_len == length ||
480 (total_len % block_size == 0 &&
481 total_len < length &&
482 total_len + block_size >= length));
Paul Bakker343a8702011-06-09 14:27:58 +0000483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + outlen, &outlen));
485 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000486
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200487#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 TEST_EQUAL(expected, mbedtls_cipher_check_tag(&ctx_dec, tag, sizeof(tag)));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200489#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 /* check result */
492 TEST_ASSERT(total_len == length);
493 TEST_ASSERT(0 == memcmp(inbuf, decbuf, length));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200494 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000495
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200496 /*
497 * Done
498 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200499exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 mbedtls_cipher_free(&ctx_dec);
501 mbedtls_cipher_free(&ctx_enc);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200502}
Paul Bakker33b43f12013-08-20 11:48:36 +0200503/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000504
Paul Bakker33b43f12013-08-20 11:48:36 +0200505/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100506void enc_fail(int cipher_id, int pad_mode, int key_len, int length_val,
507 int ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200508{
Paul Bakker33b43f12013-08-20 11:48:36 +0200509 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200510 unsigned char key[32];
511 unsigned char iv[16];
512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 const mbedtls_cipher_info_t *cipher_info;
514 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200515
516 unsigned char inbuf[64];
517 unsigned char encbuf[64];
518
519 size_t outlen = 0;
520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 memset(key, 0, 32);
522 memset(iv, 0, 16);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 memset(inbuf, 5, 64);
527 memset(encbuf, 0, 64);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200528
529 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
531 TEST_ASSERT(NULL != cipher_info);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200532
533 /* Initialise context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
535 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key, key_len, MBEDTLS_ENCRYPT));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200538#else
539 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv, 16));
542 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200543#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
545 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100546 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
547
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200549#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200550
551 /* encode length number of bytes from inbuf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, inbuf, length, encbuf, &outlen));
553 TEST_ASSERT(ret == mbedtls_cipher_finish(&ctx, encbuf + outlen, &outlen));
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200554
555 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200556exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 mbedtls_cipher_free(&ctx);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200558}
Paul Bakker33b43f12013-08-20 11:48:36 +0200559/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200560
Paul Bakker33b43f12013-08-20 11:48:36 +0200561/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100562void dec_empty_buf(int cipher,
563 int expected_update_ret,
564 int expected_finish_ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200565{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000566 unsigned char key[32];
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100567
568 unsigned char *iv = NULL;
569 size_t iv_len = 16;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_cipher_context_t ctx_dec;
572 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000573
574 unsigned char encbuf[64];
575 unsigned char decbuf[64];
576
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000577 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 memset(key, 0, 32);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 mbedtls_cipher_init(&ctx_dec);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 memset(encbuf, 0, 64);
584 memset(decbuf, 0, 64);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000585
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200586 /* Initialise context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 cipher_info = mbedtls_cipher_info_from_type(cipher);
588 TEST_ASSERT(NULL != cipher_info);
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
591 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100592 iv_len = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100594
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100595 TEST_CALLOC(iv, iv_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 memset(iv, 0, iv_len);
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100597
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100598 TEST_ASSERT(sizeof(key) * 8 >= mbedtls_cipher_info_get_key_bitlen(cipher_info));
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec,
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100603 key, mbedtls_cipher_info_get_key_bitlen(cipher_info),
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 MBEDTLS_DECRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000605
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200609
Waleed Elmelegyf4e66512023-09-21 14:04:35 +0100610#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) && defined(MBEDTLS_CIPHER_PADDING_PKCS7)
Waleed Elmelegya7d206f2023-09-07 17:54:46 +0100611 if (ctx_dec.cipher_info->mode == MBEDTLS_MODE_CBC) {
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +0100612 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec,
613 MBEDTLS_PADDING_PKCS7));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +0100614 }
Waleed Elmelegyf4e66512023-09-21 14:04:35 +0100615#endif
Waleed Elmelegya7d206f2023-09-07 17:54:46 +0100616
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200617#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
619 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100620 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200623#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000624
625 /* decode 0-byte string */
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 TEST_ASSERT(expected_update_ret ==
627 mbedtls_cipher_update(&ctx_dec, encbuf, 0, decbuf, &outlen));
628 TEST_ASSERT(0 == outlen);
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 if (expected_finish_ret == 0 &&
631 (cipher_info->mode == MBEDTLS_MODE_CBC ||
632 cipher_info->mode == MBEDTLS_MODE_ECB)) {
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100633 /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
634 * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
k-stachowiakd8727232019-07-29 17:46:29 +0200635 * decrypting an empty buffer.
636 * On the other hand, CBC and ECB ciphers need a full block of input.
637 */
638 expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100639 }
640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 TEST_ASSERT(expected_finish_ret == mbedtls_cipher_finish(
642 &ctx_dec, decbuf + outlen, &outlen));
643 TEST_ASSERT(0 == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000644
Paul Bakkerbd51b262014-07-10 15:26:12 +0200645exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 mbedtls_free(iv);
647 mbedtls_cipher_free(&ctx_dec);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200648}
Paul Bakker33b43f12013-08-20 11:48:36 +0200649/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000650
Paul Bakker33b43f12013-08-20 11:48:36 +0200651/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100652void enc_dec_buf_multipart(int cipher_id, int key_len, int first_length_val,
653 int second_length_val, int pad_mode,
654 int first_encrypt_output_len, int second_encrypt_output_len,
655 int first_decrypt_output_len, int second_decrypt_output_len)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200656{
Paul Bakker33b43f12013-08-20 11:48:36 +0200657 size_t first_length = first_length_val;
658 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000659 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200660 size_t block_size;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200661 size_t iv_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000662 unsigned char key[32];
663 unsigned char iv[16];
664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_cipher_context_t ctx_dec;
666 mbedtls_cipher_context_t ctx_enc;
667 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000668
669 unsigned char inbuf[64];
670 unsigned char encbuf[64];
671 unsigned char decbuf[64];
672
Paul Bakker23986e52011-04-24 08:57:21 +0000673 size_t outlen = 0;
674 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 memset(key, 0, 32);
677 memset(iv, 0, 16);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 mbedtls_cipher_init(&ctx_dec);
680 mbedtls_cipher_init(&ctx_enc);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 memset(inbuf, 5, 64);
683 memset(encbuf, 0, 64);
684 memset(decbuf, 0, 64);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000685
686 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
688 TEST_ASSERT(NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
691 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT));
694 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000695
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700696#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (-1 != pad_mode) {
698 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode));
699 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode));
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700700 }
701#else
702 (void) pad_mode;
703#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
704
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) {
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200706 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
707 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
709 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100710 iv_len = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 } else {
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200712 iv_len = sizeof(iv);
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 }
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
716 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
719 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200720
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200721#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
723 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100724 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0));
727 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200728#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 block_size = mbedtls_cipher_get_block_size(&ctx_enc);
731 TEST_ASSERT(block_size != 0);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200732
Paul Bakker8123e9d2011-01-06 15:37:30 +0000733 /* encode length number of bytes from inbuf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, first_length, encbuf, &outlen));
735 TEST_ASSERT((size_t) first_encrypt_output_len == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000736 totaloutlen = outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 TEST_ASSERT(0 ==
738 mbedtls_cipher_update(&ctx_enc, inbuf + first_length, second_length,
739 encbuf + totaloutlen,
740 &outlen));
741 TEST_ASSERT((size_t) second_encrypt_output_len == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000742 totaloutlen += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 TEST_ASSERT(totaloutlen == length ||
744 (totaloutlen % block_size == 0 &&
745 totaloutlen < length &&
746 totaloutlen + block_size > length));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + totaloutlen, &outlen));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000749 totaloutlen += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 TEST_ASSERT(totaloutlen == length ||
751 (totaloutlen % block_size == 0 &&
752 totaloutlen > length &&
753 totaloutlen <= length + block_size));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000754
755 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700756 second_length = totaloutlen - first_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, first_length, decbuf, &outlen));
758 TEST_ASSERT((size_t) first_decrypt_output_len == outlen);
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200759 totaloutlen = outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 TEST_ASSERT(0 ==
761 mbedtls_cipher_update(&ctx_dec, encbuf + first_length, second_length,
762 decbuf + totaloutlen,
763 &outlen));
764 TEST_ASSERT((size_t) second_decrypt_output_len == outlen);
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700765 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 TEST_ASSERT(totaloutlen == length ||
768 (totaloutlen % block_size == 0 &&
769 totaloutlen < length &&
770 totaloutlen + block_size >= length));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + totaloutlen, &outlen));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200773 totaloutlen += outlen;
774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 TEST_ASSERT(totaloutlen == length);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 TEST_ASSERT(0 == memcmp(inbuf, decbuf, length));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000778
Paul Bakkerbd51b262014-07-10 15:26:12 +0200779exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 mbedtls_cipher_free(&ctx_dec);
781 mbedtls_cipher_free(&ctx_enc);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200782}
Paul Bakker33b43f12013-08-20 11:48:36 +0200783/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000784
Paul Bakker33b43f12013-08-20 11:48:36 +0200785/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100786void decrypt_test_vec(int cipher_id, int pad_mode, data_t *key,
787 data_t *iv, data_t *cipher,
788 data_t *clear, data_t *ad, data_t *tag,
789 int finish_result, int tag_result)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200790{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200791 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200793 size_t outlen, total_len;
794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 memset(output, 0x00, sizeof(output));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200798
Azim Khanf1aaec92017-05-30 14:23:15 +0100799#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100800 ((void) ad);
801 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200802#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200803
804 /* Prepare context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
806 mbedtls_cipher_info_from_type(cipher_id)));
807 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (pad_mode != -1) {
810 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
811 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200812#else
813 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv->x, iv->len));
816 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200817#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 int expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
819 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100820 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, ad->x, ad->len));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200823#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200824
Azim Khand30ca132017-06-09 04:32:58 +0100825 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200826 total_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, cipher->x, cipher->len, output, &outlen));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200828 total_len += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100829 TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen,
830 &outlen));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200831 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200832#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 int tag_expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
834 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100835 tag_result : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 TEST_EQUAL(tag_expected, mbedtls_cipher_check_tag(&ctx, tag->x, tag->len));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200838#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200839
840 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if (0 == finish_result && 0 == tag_result) {
842 TEST_ASSERT(total_len == clear->len);
843 TEST_ASSERT(0 == memcmp(output, clear->x, clear->len));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200844 }
845
Paul Bakkerbd51b262014-07-10 15:26:12 +0200846exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 mbedtls_cipher_free(&ctx);
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200848}
849/* END_CASE */
850
Valerio Setti2f00b7a2023-10-17 11:43:34 +0200851/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */
Gilles Peskine449bd832023-01-11 14:50:10 +0100852void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv,
853 data_t *ad, data_t *cipher, data_t *tag,
854 char *result, data_t *clear, int use_psa)
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200855{
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100856 /*
857 * Take an AEAD ciphertext + tag and perform a pair
858 * of AEAD decryption and AEAD encryption. Check that
Hanno Beckera13272d2018-11-12 16:27:30 +0000859 * this results in the expected plaintext, and that
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100860 * decryption and encryption are inverse to one another.
861 */
Hanno Beckera13272d2018-11-12 16:27:30 +0000862
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200863 int ret;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100864 int using_nist_kw, using_nist_kw_padding;
Hanno Beckera13272d2018-11-12 16:27:30 +0000865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200867 size_t outlen;
868
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100869 unsigned char *cipher_plus_tag = NULL;
870 size_t cipher_plus_tag_len;
871 unsigned char *decrypt_buf = NULL;
872 size_t decrypt_buf_len = 0;
873 unsigned char *encrypt_buf = NULL;
874 size_t encrypt_buf_len = 0;
875
Gilles Peskine70edd682020-12-03 20:27:27 +0100876 /* Null pointers are documented as valid for inputs of length 0.
877 * The test framework passes non-null pointers, so set them to NULL.
878 * key, cipher and tag can't be empty. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 if (iv->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100880 iv->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 }
882 if (ad->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100883 ad->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 }
885 if (clear->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100886 clear->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 }
Gilles Peskine70edd682020-12-03 20:27:27 +0100888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200890
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100891 /* Initialize PSA Crypto */
892#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 if (use_psa == 1) {
894 PSA_ASSERT(psa_crypto_init());
895 }
Hanno Beckera13272d2018-11-12 16:27:30 +0000896#else
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100897 (void) use_psa;
898#endif
899
900 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100901 * Are we using NIST_KW? with padding?
902 */
903 using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
904 cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
905 cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
906 using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
907 cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
908 cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
909 using_nist_kw_padding;
910
911 /*
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100912 * Prepare context for decryption
913 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key,
915 MBEDTLS_DECRYPT)) {
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100916 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 }
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200918
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100919 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100920 * prepare buffer for decryption
921 * (we need the tag appended to the ciphertext)
922 */
923 cipher_plus_tag_len = cipher->len + tag->len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100924 TEST_CALLOC(cipher_plus_tag, cipher_plus_tag_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 memcpy(cipher_plus_tag, cipher->x, cipher->len);
926 memcpy(cipher_plus_tag + cipher->len, tag->x, tag->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100927
928 /*
929 * Compute length of output buffer according to the documentation
930 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 if (using_nist_kw) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100932 decrypt_buf_len = cipher_plus_tag_len - 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 } else {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100934 decrypt_buf_len = cipher_plus_tag_len - tag->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 }
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100936
937
938 /*
939 * Try decrypting to a buffer that's 1B too small
940 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 if (decrypt_buf_len != 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100942 TEST_CALLOC(decrypt_buf, decrypt_buf_len - 1);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100943
944 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
946 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
947 decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len);
948 TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 mbedtls_free(decrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100951 decrypt_buf = NULL;
952 }
953
954 /*
955 * Authenticate and decrypt, and check result
956 */
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100957 TEST_CALLOC(decrypt_buf, decrypt_buf_len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100958
959 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
961 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
962 decrypt_buf, decrypt_buf_len, &outlen, tag->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 if (strcmp(result, "FAIL") == 0) {
965 TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED);
966 TEST_ASSERT(buffer_is_all_zero(decrypt_buf, decrypt_buf_len));
967 } else {
968 TEST_ASSERT(ret == 0);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100969 TEST_MEMORY_COMPARE(decrypt_buf, outlen, clear->x, clear->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100970 }
971
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 mbedtls_free(decrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100973 decrypt_buf = NULL;
974
975 /*
976 * Encrypt back if test data was authentic
977 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 if (strcmp(result, "FAIL") != 0) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100979 /* prepare context for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key,
981 MBEDTLS_ENCRYPT)) {
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100982 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 }
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100984
985 /*
986 * Compute size of output buffer according to documentation
987 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 if (using_nist_kw) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100989 encrypt_buf_len = clear->len + 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if (using_nist_kw_padding && encrypt_buf_len % 8 != 0) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100991 encrypt_buf_len += 8 - encrypt_buf_len % 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 }
993 } else {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100994 encrypt_buf_len = clear->len + tag->len;
995 }
996
997 /*
998 * Try encrypting with an output buffer that's 1B too small
999 */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001000 TEST_CALLOC(encrypt_buf, encrypt_buf_len - 1);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001001
1002 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
1004 ad->x, ad->len, clear->x, clear->len,
1005 encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len);
1006 TEST_ASSERT(ret != 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 mbedtls_free(encrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001009 encrypt_buf = NULL;
1010
1011 /*
1012 * Encrypt and check the result
1013 */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001014 TEST_CALLOC(encrypt_buf, encrypt_buf_len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001015
1016 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
1018 ad->x, ad->len, clear->x, clear->len,
1019 encrypt_buf, encrypt_buf_len, &outlen, tag->len);
1020 TEST_ASSERT(ret == 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 TEST_ASSERT(outlen == cipher->len + tag->len);
1023 TEST_ASSERT(memcmp(encrypt_buf, cipher->x, cipher->len) == 0);
1024 TEST_ASSERT(memcmp(encrypt_buf + cipher->len,
1025 tag->x, tag->len) == 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 mbedtls_free(encrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001028 encrypt_buf = NULL;
1029 }
1030
Paul Bakkerbd51b262014-07-10 15:26:12 +02001031exit:
Hanno Beckera13272d2018-11-12 16:27:30 +00001032
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 mbedtls_cipher_free(&ctx);
1034 mbedtls_free(decrypt_buf);
1035 mbedtls_free(encrypt_buf);
1036 mbedtls_free(cipher_plus_tag);
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001037
Hanno Beckera13272d2018-11-12 16:27:30 +00001038#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 if (use_psa == 1) {
1040 PSA_DONE();
1041 }
Hanno Beckera13272d2018-11-12 16:27:30 +00001042#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001043}
1044/* END_CASE */
1045
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +02001046/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001047void test_vec_ecb(int cipher_id, int operation, data_t *key,
1048 data_t *input, data_t *result, int finish_result
1049 )
Paul Bakker5e0efa72013-09-08 23:04:04 +02001050{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001052 unsigned char output[32];
1053 size_t outlen;
1054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 memset(output, 0x00, sizeof(output));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001058
1059 /* Prepare context */
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
1061 mbedtls_cipher_info_from_type(cipher_id)));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001062
Paul Bakker5e0efa72013-09-08 23:04:04 +02001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001065
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, input->x,
1067 mbedtls_cipher_get_block_size(&ctx),
1068 output, &outlen));
1069 TEST_ASSERT(outlen == mbedtls_cipher_get_block_size(&ctx));
1070 TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen,
1071 &outlen));
1072 TEST_ASSERT(0 == outlen);
Paul Bakker5e0efa72013-09-08 23:04:04 +02001073
1074 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 if (0 == finish_result) {
1076 TEST_ASSERT(0 == memcmp(output, result->x,
1077 mbedtls_cipher_get_block_size(&ctx)));
1078 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001079
Paul Bakkerbd51b262014-07-10 15:26:12 +02001080exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 mbedtls_cipher_free(&ctx);
Paul Bakker5e0efa72013-09-08 23:04:04 +02001082}
1083/* END_CASE */
1084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +01001086void test_vec_crypt(int cipher_id, int operation, data_t *key,
1087 data_t *iv, data_t *input, data_t *result,
1088 int finish_result, int use_psa)
Ron Eldor7b012442017-09-25 17:03:12 +03001089{
Ron Eldor7b012442017-09-25 17:03:12 +03001090 mbedtls_cipher_context_t ctx;
1091 unsigned char output[32];
1092 size_t outlen;
1093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 mbedtls_cipher_init(&ctx);
Ron Eldor7b012442017-09-25 17:03:12 +03001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 memset(output, 0x00, sizeof(output));
Ron Eldor7b012442017-09-25 17:03:12 +03001097
1098 /* Prepare context */
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -04001099#if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
Hanno Beckere43164e2018-11-12 12:46:35 +00001100 (void) use_psa;
1101#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if (use_psa == 1) {
1103 PSA_ASSERT(psa_crypto_init());
1104 TEST_ASSERT(0 == mbedtls_cipher_setup_psa(&ctx,
1105 mbedtls_cipher_info_from_type(cipher_id), 0));
1106 } else
Przemek Stekiel476d9c42022-05-19 12:26:33 +02001107#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
1109 mbedtls_cipher_info_from_type(cipher_id)));
Ron Eldor7b012442017-09-25 17:03:12 +03001110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation));
1112 if (MBEDTLS_MODE_CBC == ctx.cipher_info->mode) {
1113 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE));
1114 }
Ron Eldor7b012442017-09-25 17:03:12 +03001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 TEST_ASSERT(finish_result == mbedtls_cipher_crypt(&ctx, iv->len ? iv->x : NULL,
1117 iv->len, input->x, input->len,
1118 output, &outlen));
1119 TEST_ASSERT(result->len == outlen);
Ron Eldor7b012442017-09-25 17:03:12 +03001120 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if (0 == finish_result) {
1122 TEST_ASSERT(0 == memcmp(output, result->x, outlen));
1123 }
Ron Eldor7b012442017-09-25 17:03:12 +03001124
1125exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 mbedtls_cipher_free(&ctx);
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -04001127#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 PSA_DONE();
Andrzej Kureked052792022-10-21 05:37:54 -04001129#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_TEST_DEPRECATED */
Ron Eldor7b012442017-09-25 17:03:12 +03001130}
1131/* END_CASE */
1132
1133/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +01001134void set_padding(int cipher_id, int pad_mode, int ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 const mbedtls_cipher_info_t *cipher_info;
1137 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1142 TEST_ASSERT(NULL != cipher_info);
1143 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001144
Gilles Peskine449bd832023-01-11 14:50:10 +01001145 TEST_ASSERT(ret == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001146
Paul Bakkerbd51b262014-07-10 15:26:12 +02001147exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 mbedtls_cipher_free(&ctx);
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001149}
Paul Bakker33b43f12013-08-20 11:48:36 +02001150/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +01001153void check_padding(int pad_mode, data_t *input, int ret, int dlen_check
1154 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001155{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 mbedtls_cipher_info_t cipher_info;
1157 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +01001158 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001159
1160 /* build a fake context just for getting access to get_padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001163 ctx.cipher_info = &cipher_info;
1164
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001166
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 TEST_ASSERT(ret == ctx.get_padding(input->x, input->len, &dlen));
1169 if (0 == ret) {
1170 TEST_ASSERT(dlen == (size_t) dlen_check);
1171 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001172}
Paul Bakker33b43f12013-08-20 11:48:36 +02001173/* END_CASE */
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001174
1175/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001176void iv_len_validity(int cipher_id, char *cipher_string,
1177 int iv_len_val, int ret)
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001178{
1179 size_t iv_len = iv_len_val;
1180 unsigned char iv[16];
1181
Thomas Daubney3a066ec2022-02-17 12:01:28 +00001182 /* Initialise iv buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 memset(iv, 0, sizeof(iv));
Thomas Daubney3a066ec2022-02-17 12:01:28 +00001184
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001185 const mbedtls_cipher_info_t *cipher_info;
1186 mbedtls_cipher_context_t ctx_dec;
1187 mbedtls_cipher_context_t ctx_enc;
1188
1189 /*
1190 * Prepare contexts
1191 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 mbedtls_cipher_init(&ctx_dec);
1193 mbedtls_cipher_init(&ctx_enc);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001194
1195 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1197 TEST_ASSERT(NULL != cipher_info);
1198 TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info);
1199 TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info),
1200 cipher_string) == 0);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001201
1202 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
1204 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
1207 TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001208
1209exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 mbedtls_cipher_free(&ctx_dec);
1211 mbedtls_cipher_free(&ctx_enc);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001212}
1213/* END_CASE */
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001214
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001215/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
1216void check_set_padding(int cipher_id)
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001217{
1218 mbedtls_cipher_context_t ctx;
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001219 unsigned char *key = NULL;
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001220 unsigned char iv[16] = { 0 };
1221 unsigned char input[16] = { 0 };
1222 unsigned char output[32] = { 0 };
1223 size_t outlen = 0;
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001224 const mbedtls_cipher_info_t *cipher_info;
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001225 size_t keylen = 0;
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001226
Paul Elliott3bda79b2023-10-18 15:09:09 +01001227 mbedtls_cipher_init(&ctx);
1228
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001229 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1230
1231 if (cipher_info->mode != MBEDTLS_MODE_CBC) {
1232 TEST_FAIL("Cipher mode must be CBC");
1233 }
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001234
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001235 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info);
1236 TEST_CALLOC(key, keylen/8);
1237 memset(key, 0, keylen/8);
1238
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001239 TEST_EQUAL(0, mbedtls_cipher_setup(&ctx, cipher_info));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001240
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001241 TEST_EQUAL(0, mbedtls_cipher_setkey(&ctx, key, keylen,
1242 MBEDTLS_ENCRYPT));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001243
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001244 TEST_EQUAL(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
1245 mbedtls_cipher_crypt(&ctx, iv, sizeof(iv), input,
1246 sizeof(input), output, &outlen));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001247
Waleed Elmelegy6d2c5d52023-09-18 17:41:25 +01001248 TEST_EQUAL(0, mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE));
1249 TEST_EQUAL(0, mbedtls_cipher_crypt(&ctx, iv, sizeof(iv), input,
1250 sizeof(input), output, &outlen));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001251
1252exit:
1253 mbedtls_cipher_free(&ctx);
Waleed Elmelegyf4e66512023-09-21 14:04:35 +01001254 mbedtls_free(key);
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001255}
1256/* END_CASE */