blob: 4fda4f92bd2de654d056390a1a9987adbd9b394b [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
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00007#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +02008#endif
Gilles Peskine5386f6b2019-08-01 12:47:40 +02009
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +010010#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
11#define MBEDTLS_CIPHER_AUTH_CRYPT
12#endif
13
Gilles Peskine0be02bd2021-07-19 16:32:54 +020014/* Check the internal consistency of a cipher info structure, and
15 * check it against mbedtls_cipher_info_from_xxx(). */
Gilles Peskine449bd832023-01-11 14:50:10 +010016static int check_cipher_info(mbedtls_cipher_type_t type,
17 const mbedtls_cipher_info_t *info)
Gilles Peskine0be02bd2021-07-19 16:32:54 +020018{
Max Fillinger72abd8a2021-11-28 14:02:36 +010019 size_t key_bitlen, block_size, iv_size;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020020
Gilles Peskine449bd832023-01-11 14:50:10 +010021 TEST_ASSERT(info != NULL);
22 TEST_EQUAL(type, mbedtls_cipher_info_get_type(info));
23 TEST_EQUAL(type, info->type);
24 TEST_ASSERT(mbedtls_cipher_info_from_type(type) == info);
Gilles Peskine0be02bd2021-07-19 16:32:54 +020025
Gilles Peskine449bd832023-01-11 14:50:10 +010026 TEST_EQUAL(info->mode, mbedtls_cipher_info_get_mode(info));
Gilles Peskine0be02bd2021-07-19 16:32:54 +020027
28 /* Insist that get_name() return the string from the structure and
29 * not a copy. A copy would have an unknown storage duration. */
Gilles Peskine449bd832023-01-11 14:50:10 +010030 TEST_ASSERT(mbedtls_cipher_info_get_name(info) == info->name);
31 TEST_ASSERT(mbedtls_cipher_info_from_string(info->name) == info);
Gilles Peskine0be02bd2021-07-19 16:32:54 +020032
Gilles Peskine449bd832023-01-11 14:50:10 +010033 key_bitlen = mbedtls_cipher_info_get_key_bitlen(info);
34 block_size = mbedtls_cipher_info_get_block_size(info);
35 iv_size = mbedtls_cipher_info_get_iv_size(info);
36 if (info->type == MBEDTLS_CIPHER_NULL) {
37 TEST_ASSERT(key_bitlen == 0);
38 TEST_ASSERT(block_size == 1);
39 TEST_ASSERT(iv_size == 0);
40 } else if (info->mode == MBEDTLS_MODE_XTS) {
41 TEST_ASSERT(key_bitlen == 256 ||
42 key_bitlen == 384 ||
43 key_bitlen == 512);
44 } else if (!strncmp(info->name, "DES-EDE3-", 9)) {
45 TEST_ASSERT(key_bitlen == 192);
46 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
47 TEST_ASSERT(block_size == 8);
48 } else if (!strncmp(info->name, "DES-EDE-", 8)) {
49 TEST_ASSERT(key_bitlen == 128);
50 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
51 TEST_ASSERT(block_size == 8);
52 } else if (!strncmp(info->name, "DES-", 4)) {
53 TEST_ASSERT(key_bitlen == 64);
54 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
55 TEST_ASSERT(block_size == 8);
56 } else if (!strncmp(info->name, "AES", 3)) {
57 TEST_ASSERT(key_bitlen == 128 ||
58 key_bitlen == 192 ||
59 key_bitlen == 256);
60 TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info));
61 TEST_ASSERT(block_size == 16);
62 } else {
63 TEST_ASSERT(key_bitlen == 128 ||
64 key_bitlen == 192 ||
65 key_bitlen == 256);
Gilles Peskine6ac8f942021-09-01 08:31:49 +020066 }
Gilles Peskine4f4d4b22023-06-14 17:34:31 +020067 TEST_LE_U(key_bitlen, MBEDTLS_MAX_KEY_LENGTH * 8);
68 TEST_LE_U(block_size, MBEDTLS_MAX_BLOCK_LENGTH);
69 TEST_LE_U(iv_size, MBEDTLS_MAX_IV_LENGTH);
Gilles Peskine0be02bd2021-07-19 16:32:54 +020070
Gilles Peskine449bd832023-01-11 14:50:10 +010071 if (strstr(info->name, "-ECB") != NULL) {
72 TEST_ASSERT(iv_size == 0);
73 TEST_ASSERT(!mbedtls_cipher_info_has_variable_iv_size(info));
74 } else if (strstr(info->name, "-CBC") != NULL ||
75 strstr(info->name, "-CTR") != NULL) {
76 TEST_ASSERT(iv_size == block_size);
77 TEST_ASSERT(!mbedtls_cipher_info_has_variable_iv_size(info));
78 } else if (strstr(info->name, "-GCM") != NULL) {
79 TEST_ASSERT(iv_size == block_size - 4);
80 TEST_ASSERT(mbedtls_cipher_info_has_variable_iv_size(info));
Max Fillingerc60c3a02021-11-09 22:38:56 +010081 }
82
Gilles Peskine449bd832023-01-11 14:50:10 +010083 return 1;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020084
85exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010086 return 0;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020087}
88
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010089#if defined(MBEDTLS_CIPHER_AUTH_CRYPT)
90/* Helper for resetting key/direction
91 *
92 * The documentation doesn't explicitly say whether calling
93 * mbedtls_cipher_setkey() twice is allowed or not. This currently works with
94 * the default software implementation, but only by accident. It isn't
95 * guaranteed to work with new ciphers or with alternative implementations of
96 * individual ciphers, and it doesn't work with the PSA wrappers. So don't do
97 * it, and instead start with a fresh context.
98 */
Gilles Peskine449bd832023-01-11 14:50:10 +010099static int cipher_reset_key(mbedtls_cipher_context_t *ctx, int cipher_id,
100 int use_psa, size_t tag_len, const data_t *key, int direction)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100101{
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 mbedtls_cipher_free(ctx);
103 mbedtls_cipher_init(ctx);
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100104
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -0400105#if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100106 (void) use_psa;
107 (void) tag_len;
108#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 if (use_psa == 1) {
110 TEST_ASSERT(0 == mbedtls_cipher_setup_psa(ctx,
111 mbedtls_cipher_info_from_type(cipher_id),
112 tag_len));
113 } else
Przemek Stekiel476d9c42022-05-19 12:26:33 +0200114#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED */
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100115 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 TEST_ASSERT(0 == mbedtls_cipher_setup(ctx,
117 mbedtls_cipher_info_from_type(cipher_id)));
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100118 }
119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 TEST_ASSERT(0 == mbedtls_cipher_setkey(ctx, key->x, 8 * key->len,
121 direction));
122 return 1;
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100123
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100124exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return 0;
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100126}
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100127
128/*
129 * Check if a buffer is all-0 bytes:
130 * return 1 if it is,
131 * 0 if it isn't.
132 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133int buffer_is_all_zero(const uint8_t *buf, size_t size)
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100134{
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 for (size_t i = 0; i < size; i++) {
136 if (buf[i] != 0) {
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100137 return 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 }
139 }
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100140 return 1;
141}
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100142#endif /* MBEDTLS_CIPHER_AUTH_CRYPT */
143
Paul Bakker33b43f12013-08-20 11:48:36 +0200144/* END_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000145
Paul Bakker33b43f12013-08-20 11:48:36 +0200146/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 * depends_on:MBEDTLS_CIPHER_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200148 * END_DEPENDENCIES
149 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000150
Paul Bakker33b43f12013-08-20 11:48:36 +0200151/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100152void mbedtls_cipher_list()
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100153{
154 const int *cipher_type;
155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 for (cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++) {
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200157 const mbedtls_cipher_info_t *info =
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 mbedtls_cipher_info_from_type(*cipher_type);
159 mbedtls_test_set_step(*cipher_type);
160 if (!check_cipher_info(*cipher_type, info)) {
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200161 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 }
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200163 }
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100164}
165/* END_CASE */
166
167/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168void cipher_invalid_param_unconditional()
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200169{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500170 mbedtls_cipher_context_t valid_ctx;
171 mbedtls_cipher_context_t invalid_ctx;
172 mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
173 mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
174 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
175 int valid_size = sizeof(valid_buffer);
176 int valid_bitlen = valid_size * 8;
177 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 *(mbedtls_cipher_list()));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500179 size_t size_t_var;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 (void) valid_mode; /* In some configurations this is unused */
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 mbedtls_cipher_init(&valid_ctx);
184 mbedtls_cipher_init(&invalid_ctx);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, valid_info) == 0);
Gilles Peskinedc8ecda2021-12-10 14:28:31 +0100187
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500188 /* mbedtls_cipher_setup() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, NULL) ==
190 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200191
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500192 /* mbedtls_cipher_get_block_size() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 TEST_ASSERT(mbedtls_cipher_get_block_size(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200194
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500195 /* mbedtls_cipher_get_cipher_mode() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 TEST_ASSERT(mbedtls_cipher_get_cipher_mode(&invalid_ctx) ==
197 MBEDTLS_MODE_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200198
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500199 /* mbedtls_cipher_get_iv_size() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 TEST_ASSERT(mbedtls_cipher_get_iv_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_type() */
203 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 mbedtls_cipher_get_type(&invalid_ctx) ==
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500205 MBEDTLS_CIPHER_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200206
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500207 /* mbedtls_cipher_get_name() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 TEST_ASSERT(mbedtls_cipher_get_name(&invalid_ctx) == 0);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200209
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500210 /* mbedtls_cipher_get_key_bitlen() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 TEST_ASSERT(mbedtls_cipher_get_key_bitlen(&invalid_ctx) ==
212 MBEDTLS_KEY_LENGTH_NONE);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500213
214 /* mbedtls_cipher_get_operation() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 TEST_ASSERT(mbedtls_cipher_get_operation(&invalid_ctx) ==
216 MBEDTLS_OPERATION_NONE);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500217
218 /* mbedtls_cipher_setkey() */
219 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 mbedtls_cipher_setkey(&invalid_ctx,
221 valid_buffer,
222 valid_bitlen,
223 valid_operation) ==
224 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500225
226 /* mbedtls_cipher_set_iv() */
227 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 mbedtls_cipher_set_iv(&invalid_ctx,
229 valid_buffer,
230 valid_size) ==
231 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500232
233 /* mbedtls_cipher_reset() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 TEST_ASSERT(mbedtls_cipher_reset(&invalid_ctx) ==
235 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200236
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200237#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500238 /* mbedtls_cipher_update_ad() */
239 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 mbedtls_cipher_update_ad(&invalid_ctx,
241 valid_buffer,
242 valid_size) ==
243 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500244#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
245
246#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
247 /* mbedtls_cipher_set_padding_mode() */
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 TEST_ASSERT(mbedtls_cipher_set_padding_mode(&invalid_ctx, valid_mode) ==
249 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200250#endif
251
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500252 /* mbedtls_cipher_update() */
253 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 mbedtls_cipher_update(&invalid_ctx,
255 valid_buffer,
256 valid_size,
257 valid_buffer,
258 &size_t_var) ==
259 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200260
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500261 /* mbedtls_cipher_finish() */
262 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 mbedtls_cipher_finish(&invalid_ctx,
264 valid_buffer,
265 &size_t_var) ==
266 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200267
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200268#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500269 /* mbedtls_cipher_write_tag() */
270 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 mbedtls_cipher_write_tag(&invalid_ctx,
272 valid_buffer,
273 valid_size) ==
274 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200275
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500276 /* mbedtls_cipher_check_tag() */
277 TEST_ASSERT(
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 mbedtls_cipher_check_tag(&invalid_ctx,
279 valid_buffer,
280 valid_size) ==
281 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500282#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
283
284exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 mbedtls_cipher_free(&invalid_ctx);
286 mbedtls_cipher_free(&valid_ctx);
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500287}
288/* END_CASE */
289
Tuvshinzaya Erdenekhuu6c689272022-07-29 14:45:55 +0100290/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100291void cipher_invalid_param_conditional()
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500292{
293 mbedtls_cipher_context_t valid_ctx;
294
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500295 mbedtls_operation_t invalid_operation = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500296 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
297 int valid_size = sizeof(valid_buffer);
298 int valid_bitlen = valid_size * 8;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500299
Ronald Cron875b5fb2021-05-21 08:50:00 +0200300 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500301 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 mbedtls_cipher_setkey(&valid_ctx,
303 valid_buffer,
304 valid_bitlen,
305 invalid_operation));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500306
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500307exit:
TRodziewicz70199552021-05-27 13:52:59 +0200308 ;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200309}
310/* END_CASE */
311
Paul Bakker6a9c7252016-07-14 13:46:10 +0100312/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100313void cipher_special_behaviours()
Paul Bakker6a9c7252016-07-14 13:46:10 +0100314{
315 const mbedtls_cipher_info_t *cipher_info;
316 mbedtls_cipher_context_t ctx;
317 unsigned char input[32];
318 unsigned char output[32];
Gilles Peskine449bd832023-01-11 14:50:10 +0100319#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100320 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300321#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100322 size_t olen = 0;
323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 mbedtls_cipher_init(&ctx);
325 memset(input, 0, sizeof(input));
326 memset(output, 0, sizeof(output));
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300327#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 memset(iv, 0, sizeof(iv));
Paul Bakker6a9c7252016-07-14 13:46:10 +0100329
330 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_CBC);
332 TEST_ASSERT(NULL != cipher_info);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Paul Bakker6a9c7252016-07-14 13:46:10 +0100335
336 /* IV too big */
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1)
338 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100339
340 /* IV too small */
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, 0)
342 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 mbedtls_cipher_free(&ctx);
345 mbedtls_cipher_init(&ctx);
Ron Eldor6f90ed82017-09-26 12:08:54 +0300346#endif /* MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB);
348 TEST_ASSERT(NULL != cipher_info);
Ron Eldor7b012442017-09-25 17:03:12 +0300349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Ron Eldor7b012442017-09-25 17:03:12 +0300351
Paul Bakker6a9c7252016-07-14 13:46:10 +0100352 /* Update ECB with partial block */
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 TEST_ASSERT(mbedtls_cipher_update(&ctx, input, 1, output, &olen)
354 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100355
356exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 mbedtls_cipher_free(&ctx);
Paul Bakker6a9c7252016-07-14 13:46:10 +0100358}
359/* END_CASE */
360
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200361/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100362void enc_dec_buf(int cipher_id, char *cipher_string, int key_len,
363 int length_val, int pad_mode)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200364{
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200365 size_t length = length_val, outlen, total_len, i, block_size, iv_len;
Jaeden Amerod906b812018-06-08 11:03:16 +0100366 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000367 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200368 unsigned char ad[13];
369 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200370 unsigned char inbuf[64];
371 unsigned char encbuf[64];
372 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 const mbedtls_cipher_info_t *cipher_info;
375 mbedtls_cipher_context_t ctx_dec;
376 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000377
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200378 /*
379 * Prepare contexts
380 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 mbedtls_cipher_init(&ctx_dec);
382 mbedtls_cipher_init(&ctx_enc);
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 memset(key, 0x2a, sizeof(key));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385
386 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
388 TEST_ASSERT(NULL != cipher_info);
389 TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info);
390 TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info),
391 cipher_string) == 0);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000392
393 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
395 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT));
398 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (-1 != pad_mode) {
402 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode));
403 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +0100404 } else {
405 if (ctx_dec.cipher_info->mode == MBEDTLS_MODE_CBC) {
406#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
407 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, MBEDTLS_PADDING_PKCS7));
408 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, MBEDTLS_PADDING_PKCS7));
409#else
410 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, MBEDTLS_PADDING_NONE));
411 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, MBEDTLS_PADDING_NONE));
412#endif
413 }
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200414 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200415#else
416 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200418
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200419 /*
420 * Do a few encode/decode cycles
421 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 for (i = 0; i < 3; i++) {
423 memset(iv, 0x00 + i, sizeof(iv));
424 memset(ad, 0x10 + i, sizeof(ad));
425 memset(inbuf, 0x20 + i, sizeof(inbuf));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 memset(encbuf, 0, sizeof(encbuf));
428 memset(decbuf, 0, sizeof(decbuf));
429 memset(tag, 0, sizeof(tag));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) {
432 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
433 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
434 } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
435 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
436 iv_len = 12;
437 } else {
438 iv_len = sizeof(iv);
439 }
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
442 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
445 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200446
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200447#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
449 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
450 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, ad, sizeof(ad) - i));
453 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, ad, sizeof(ad) - i));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200454#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 block_size = mbedtls_cipher_get_block_size(&ctx_enc);
457 TEST_ASSERT(block_size != 0);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 /* encode length number of bytes from inbuf */
460 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, length, encbuf, &outlen));
461 total_len = outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 TEST_ASSERT(total_len == length ||
464 (total_len % block_size == 0 &&
465 total_len < length &&
466 total_len + block_size > length));
Paul Bakker343a8702011-06-09 14:27:58 +0000467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + outlen, &outlen));
469 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000470
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200471#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 TEST_EQUAL(expected, mbedtls_cipher_write_tag(&ctx_enc, tag, sizeof(tag)));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200473#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 TEST_ASSERT(total_len == length ||
476 (total_len % block_size == 0 &&
477 total_len > length &&
478 total_len <= length + block_size));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 /* decode the previously encoded string */
481 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, total_len, decbuf, &outlen));
482 total_len = outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 TEST_ASSERT(total_len == length ||
485 (total_len % block_size == 0 &&
486 total_len < length &&
487 total_len + block_size >= length));
Paul Bakker343a8702011-06-09 14:27:58 +0000488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + outlen, &outlen));
490 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000491
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200492#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 TEST_EQUAL(expected, mbedtls_cipher_check_tag(&ctx_dec, tag, sizeof(tag)));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200494#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 /* check result */
497 TEST_ASSERT(total_len == length);
498 TEST_ASSERT(0 == memcmp(inbuf, decbuf, length));
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200499 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000500
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200501 /*
502 * Done
503 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200504exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 mbedtls_cipher_free(&ctx_dec);
506 mbedtls_cipher_free(&ctx_enc);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200507}
Paul Bakker33b43f12013-08-20 11:48:36 +0200508/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000509
Paul Bakker33b43f12013-08-20 11:48:36 +0200510/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100511void enc_fail(int cipher_id, int pad_mode, int key_len, int length_val,
512 int ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200513{
Paul Bakker33b43f12013-08-20 11:48:36 +0200514 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200515 unsigned char key[32];
516 unsigned char iv[16];
517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 const mbedtls_cipher_info_t *cipher_info;
519 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200520
521 unsigned char inbuf[64];
522 unsigned char encbuf[64];
523
524 size_t outlen = 0;
525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 memset(key, 0, 32);
527 memset(iv, 0, 16);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 memset(inbuf, 5, 64);
532 memset(encbuf, 0, 64);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200533
534 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
536 TEST_ASSERT(NULL != cipher_info);
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200537
538 /* Initialise context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
540 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key, key_len, MBEDTLS_ENCRYPT));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200543#else
544 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv, 16));
547 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200548#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
550 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100551 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200554#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200555
556 /* encode length number of bytes from inbuf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, inbuf, length, encbuf, &outlen));
558 TEST_ASSERT(ret == mbedtls_cipher_finish(&ctx, encbuf + outlen, &outlen));
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200559
560 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200561exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 mbedtls_cipher_free(&ctx);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200563}
Paul Bakker33b43f12013-08-20 11:48:36 +0200564/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200565
Paul Bakker33b43f12013-08-20 11:48:36 +0200566/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100567void dec_empty_buf(int cipher,
568 int expected_update_ret,
569 int expected_finish_ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200570{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000571 unsigned char key[32];
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100572
573 unsigned char *iv = NULL;
574 size_t iv_len = 16;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_cipher_context_t ctx_dec;
577 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000578
579 unsigned char encbuf[64];
580 unsigned char decbuf[64];
581
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000582 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 memset(key, 0, 32);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 mbedtls_cipher_init(&ctx_dec);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 memset(encbuf, 0, 64);
589 memset(decbuf, 0, 64);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000590
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200591 /* Initialise context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 cipher_info = mbedtls_cipher_info_from_type(cipher);
593 TEST_ASSERT(NULL != cipher_info);
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
596 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100597 iv_len = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 }
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100599
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100600 TEST_CALLOC(iv, iv_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 memset(iv, 0, iv_len);
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100602
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100603 TEST_ASSERT(sizeof(key) * 8 >= mbedtls_cipher_info_get_key_bitlen(cipher_info));
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec,
Dave Rodgman9282d4f2023-06-24 11:03:04 +0100608 key, mbedtls_cipher_info_get_key_bitlen(cipher_info),
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 MBEDTLS_DECRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200614
Waleed Elmelegya7d206f2023-09-07 17:54:46 +0100615#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
616 if (ctx_dec.cipher_info->mode == MBEDTLS_MODE_CBC) {
617#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
618 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, MBEDTLS_PADDING_PKCS7));
619#else
620 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, MBEDTLS_PADDING_NONE));
621#endif
622 }
623#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
624
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200625#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
627 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100628 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200631#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000632
633 /* decode 0-byte string */
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 TEST_ASSERT(expected_update_ret ==
635 mbedtls_cipher_update(&ctx_dec, encbuf, 0, decbuf, &outlen));
636 TEST_ASSERT(0 == outlen);
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (expected_finish_ret == 0 &&
639 (cipher_info->mode == MBEDTLS_MODE_CBC ||
640 cipher_info->mode == MBEDTLS_MODE_ECB)) {
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100641 /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
642 * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
k-stachowiakd8727232019-07-29 17:46:29 +0200643 * decrypting an empty buffer.
644 * On the other hand, CBC and ECB ciphers need a full block of input.
645 */
646 expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100647 }
648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 TEST_ASSERT(expected_finish_ret == mbedtls_cipher_finish(
650 &ctx_dec, decbuf + outlen, &outlen));
651 TEST_ASSERT(0 == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000652
Paul Bakkerbd51b262014-07-10 15:26:12 +0200653exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 mbedtls_free(iv);
655 mbedtls_cipher_free(&ctx_dec);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200656}
Paul Bakker33b43f12013-08-20 11:48:36 +0200657/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000658
Paul Bakker33b43f12013-08-20 11:48:36 +0200659/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100660void enc_dec_buf_multipart(int cipher_id, int key_len, int first_length_val,
661 int second_length_val, int pad_mode,
662 int first_encrypt_output_len, int second_encrypt_output_len,
663 int first_decrypt_output_len, int second_decrypt_output_len)
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200664{
Paul Bakker33b43f12013-08-20 11:48:36 +0200665 size_t first_length = first_length_val;
666 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000667 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200668 size_t block_size;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200669 size_t iv_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000670 unsigned char key[32];
671 unsigned char iv[16];
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_cipher_context_t ctx_dec;
674 mbedtls_cipher_context_t ctx_enc;
675 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000676
677 unsigned char inbuf[64];
678 unsigned char encbuf[64];
679 unsigned char decbuf[64];
680
Paul Bakker23986e52011-04-24 08:57:21 +0000681 size_t outlen = 0;
682 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 memset(key, 0, 32);
685 memset(iv, 0, 16);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 mbedtls_cipher_init(&ctx_dec);
688 mbedtls_cipher_init(&ctx_enc);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 memset(inbuf, 5, 64);
691 memset(encbuf, 0, 64);
692 memset(decbuf, 0, 64);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000693
694 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
696 TEST_ASSERT(NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
699 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT));
702 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000703
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700704#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (-1 != pad_mode) {
706 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode));
707 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode));
Waleed Elmelegya7d206f2023-09-07 17:54:46 +0100708 } else {
709 if (ctx_dec.cipher_info->mode == MBEDTLS_MODE_CBC) {
710#if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
711 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, MBEDTLS_PADDING_PKCS7));
712 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, MBEDTLS_PADDING_PKCS7));
713#else
714 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, MBEDTLS_PADDING_NONE));
715 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, MBEDTLS_PADDING_NONE));
716#endif
717 }
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700718 }
719#else
720 (void) pad_mode;
721#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) {
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200724 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
725 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
727 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) {
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100728 iv_len = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 } else {
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200730 iv_len = sizeof(iv);
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 }
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
734 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec));
737 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc));
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200738
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200739#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 int expected = (cipher_info->mode == MBEDTLS_MODE_GCM ||
741 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100742 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0));
745 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, NULL, 0));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200746#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 block_size = mbedtls_cipher_get_block_size(&ctx_enc);
749 TEST_ASSERT(block_size != 0);
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200750
Paul Bakker8123e9d2011-01-06 15:37:30 +0000751 /* encode length number of bytes from inbuf */
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, first_length, encbuf, &outlen));
753 TEST_ASSERT((size_t) first_encrypt_output_len == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000754 totaloutlen = outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 TEST_ASSERT(0 ==
756 mbedtls_cipher_update(&ctx_enc, inbuf + first_length, second_length,
757 encbuf + totaloutlen,
758 &outlen));
759 TEST_ASSERT((size_t) second_encrypt_output_len == outlen);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000760 totaloutlen += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 TEST_ASSERT(totaloutlen == length ||
762 (totaloutlen % block_size == 0 &&
763 totaloutlen < length &&
764 totaloutlen + block_size > length));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + totaloutlen, &outlen));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000767 totaloutlen += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 TEST_ASSERT(totaloutlen == length ||
769 (totaloutlen % block_size == 0 &&
770 totaloutlen > length &&
771 totaloutlen <= length + block_size));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000772
773 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700774 second_length = totaloutlen - first_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, first_length, decbuf, &outlen));
776 TEST_ASSERT((size_t) first_decrypt_output_len == outlen);
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200777 totaloutlen = outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 TEST_ASSERT(0 ==
779 mbedtls_cipher_update(&ctx_dec, encbuf + first_length, second_length,
780 decbuf + totaloutlen,
781 &outlen));
782 TEST_ASSERT((size_t) second_decrypt_output_len == outlen);
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700783 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 TEST_ASSERT(totaloutlen == length ||
786 (totaloutlen % block_size == 0 &&
787 totaloutlen < length &&
788 totaloutlen + block_size >= length));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + totaloutlen, &outlen));
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200791 totaloutlen += outlen;
792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 TEST_ASSERT(totaloutlen == length);
Paul Bakker8123e9d2011-01-06 15:37:30 +0000794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 TEST_ASSERT(0 == memcmp(inbuf, decbuf, length));
Paul Bakker8123e9d2011-01-06 15:37:30 +0000796
Paul Bakkerbd51b262014-07-10 15:26:12 +0200797exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 mbedtls_cipher_free(&ctx_dec);
799 mbedtls_cipher_free(&ctx_enc);
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200800}
Paul Bakker33b43f12013-08-20 11:48:36 +0200801/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000802
Paul Bakker33b43f12013-08-20 11:48:36 +0200803/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804void decrypt_test_vec(int cipher_id, int pad_mode, data_t *key,
805 data_t *iv, data_t *cipher,
806 data_t *clear, data_t *ad, data_t *tag,
807 int finish_result, int tag_result)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200808{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200809 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200811 size_t outlen, total_len;
812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 memset(output, 0x00, sizeof(output));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200816
Azim Khanf1aaec92017-05-30 14:23:15 +0100817#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100818 ((void) ad);
819 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200820#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200821
822 /* Prepare context */
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
824 mbedtls_cipher_info_from_type(cipher_id)));
825 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT));
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (pad_mode != -1) {
828 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
829 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200830#else
831 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv->x, iv->len));
834 TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx));
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200835#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 int expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
837 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100838 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, ad->x, ad->len));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200841#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200842
Azim Khand30ca132017-06-09 04:32:58 +0100843 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200844 total_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, cipher->x, cipher->len, output, &outlen));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200846 total_len += outlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen,
848 &outlen));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200849 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200850#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 int tag_expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM ||
852 ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ?
Tom Cosgrovec621a6d2022-09-30 17:13:35 +0100853 tag_result : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 TEST_EQUAL(tag_expected, mbedtls_cipher_check_tag(&ctx, tag->x, tag->len));
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200856#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200857
858 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if (0 == finish_result && 0 == tag_result) {
860 TEST_ASSERT(total_len == clear->len);
861 TEST_ASSERT(0 == memcmp(output, clear->x, clear->len));
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200862 }
863
Paul Bakkerbd51b262014-07-10 15:26:12 +0200864exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 mbedtls_cipher_free(&ctx);
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200866}
867/* END_CASE */
868
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100869/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_AUTH_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100870void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv,
871 data_t *ad, data_t *cipher, data_t *tag,
872 char *result, data_t *clear, int use_psa)
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200873{
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100874 /*
875 * Take an AEAD ciphertext + tag and perform a pair
876 * of AEAD decryption and AEAD encryption. Check that
Hanno Beckera13272d2018-11-12 16:27:30 +0000877 * this results in the expected plaintext, and that
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100878 * decryption and encryption are inverse to one another.
879 */
Hanno Beckera13272d2018-11-12 16:27:30 +0000880
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200881 int ret;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100882 int using_nist_kw, using_nist_kw_padding;
Hanno Beckera13272d2018-11-12 16:27:30 +0000883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200885 size_t outlen;
886
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100887 unsigned char *cipher_plus_tag = NULL;
888 size_t cipher_plus_tag_len;
889 unsigned char *decrypt_buf = NULL;
890 size_t decrypt_buf_len = 0;
891 unsigned char *encrypt_buf = NULL;
892 size_t encrypt_buf_len = 0;
893
Gilles Peskine70edd682020-12-03 20:27:27 +0100894 /* Null pointers are documented as valid for inputs of length 0.
895 * The test framework passes non-null pointers, so set them to NULL.
896 * key, cipher and tag can't be empty. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (iv->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100898 iv->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 }
900 if (ad->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100901 ad->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 }
903 if (clear->len == 0) {
Gilles Peskine70edd682020-12-03 20:27:27 +0100904 clear->x = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 }
Gilles Peskine70edd682020-12-03 20:27:27 +0100906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200908
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100909 /* Initialize PSA Crypto */
910#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if (use_psa == 1) {
912 PSA_ASSERT(psa_crypto_init());
913 }
Hanno Beckera13272d2018-11-12 16:27:30 +0000914#else
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100915 (void) use_psa;
916#endif
917
918 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100919 * Are we using NIST_KW? with padding?
920 */
921 using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
922 cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
923 cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
924 using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
925 cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
926 cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
927 using_nist_kw_padding;
928
929 /*
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100930 * Prepare context for decryption
931 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key,
933 MBEDTLS_DECRYPT)) {
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100934 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 }
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200936
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100937 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100938 * prepare buffer for decryption
939 * (we need the tag appended to the ciphertext)
940 */
941 cipher_plus_tag_len = cipher->len + tag->len;
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100942 TEST_CALLOC(cipher_plus_tag, cipher_plus_tag_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 memcpy(cipher_plus_tag, cipher->x, cipher->len);
944 memcpy(cipher_plus_tag + cipher->len, tag->x, tag->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100945
946 /*
947 * Compute length of output buffer according to the documentation
948 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 if (using_nist_kw) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100950 decrypt_buf_len = cipher_plus_tag_len - 8;
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 } else {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100952 decrypt_buf_len = cipher_plus_tag_len - tag->len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 }
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100954
955
956 /*
957 * Try decrypting to a buffer that's 1B too small
958 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 if (decrypt_buf_len != 0) {
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100960 TEST_CALLOC(decrypt_buf, decrypt_buf_len - 1);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100961
962 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
964 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
965 decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len);
966 TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 mbedtls_free(decrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100969 decrypt_buf = NULL;
970 }
971
972 /*
973 * Authenticate and decrypt, and check result
974 */
Tom Cosgrove05b2a872023-07-21 11:31:13 +0100975 TEST_CALLOC(decrypt_buf, decrypt_buf_len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100976
977 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len,
979 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
980 decrypt_buf, decrypt_buf_len, &outlen, tag->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100981
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 if (strcmp(result, "FAIL") == 0) {
983 TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED);
984 TEST_ASSERT(buffer_is_all_zero(decrypt_buf, decrypt_buf_len));
985 } else {
986 TEST_ASSERT(ret == 0);
Tom Cosgrovee4e9e7d2023-07-21 11:40:20 +0100987 TEST_MEMORY_COMPARE(decrypt_buf, outlen, clear->x, clear->len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100988 }
989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 mbedtls_free(decrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100991 decrypt_buf = NULL;
992
993 /*
994 * Encrypt back if test data was authentic
995 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 if (strcmp(result, "FAIL") != 0) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100997 /* prepare context for encryption */
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key,
999 MBEDTLS_ENCRYPT)) {
Gilles Peskine8a3d2342020-12-03 21:06:15 +01001000 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 }
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001002
1003 /*
1004 * Compute size of output buffer according to documentation
1005 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 if (using_nist_kw) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001007 encrypt_buf_len = clear->len + 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 if (using_nist_kw_padding && encrypt_buf_len % 8 != 0) {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001009 encrypt_buf_len += 8 - encrypt_buf_len % 8;
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 }
1011 } else {
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001012 encrypt_buf_len = clear->len + tag->len;
1013 }
1014
1015 /*
1016 * Try encrypting with an output buffer that's 1B too small
1017 */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001018 TEST_CALLOC(encrypt_buf, encrypt_buf_len - 1);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001019
1020 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
1022 ad->x, ad->len, clear->x, clear->len,
1023 encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len);
1024 TEST_ASSERT(ret != 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 mbedtls_free(encrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001027 encrypt_buf = NULL;
1028
1029 /*
1030 * Encrypt and check the result
1031 */
Tom Cosgrove05b2a872023-07-21 11:31:13 +01001032 TEST_CALLOC(encrypt_buf, encrypt_buf_len);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001033
1034 outlen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len,
1036 ad->x, ad->len, clear->x, clear->len,
1037 encrypt_buf, encrypt_buf_len, &outlen, tag->len);
1038 TEST_ASSERT(ret == 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001039
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 TEST_ASSERT(outlen == cipher->len + tag->len);
1041 TEST_ASSERT(memcmp(encrypt_buf, cipher->x, cipher->len) == 0);
1042 TEST_ASSERT(memcmp(encrypt_buf + cipher->len,
1043 tag->x, tag->len) == 0);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001044
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 mbedtls_free(encrypt_buf);
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001046 encrypt_buf = NULL;
1047 }
1048
Paul Bakkerbd51b262014-07-10 15:26:12 +02001049exit:
Hanno Beckera13272d2018-11-12 16:27:30 +00001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 mbedtls_cipher_free(&ctx);
1052 mbedtls_free(decrypt_buf);
1053 mbedtls_free(encrypt_buf);
1054 mbedtls_free(cipher_plus_tag);
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001055
Hanno Beckera13272d2018-11-12 16:27:30 +00001056#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if (use_psa == 1) {
1058 PSA_DONE();
1059 }
Hanno Beckera13272d2018-11-12 16:27:30 +00001060#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001061}
1062/* END_CASE */
1063
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +02001064/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065void test_vec_ecb(int cipher_id, int operation, data_t *key,
1066 data_t *input, data_t *result, int finish_result
1067 )
Paul Bakker5e0efa72013-09-08 23:04:04 +02001068{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001070 unsigned char output[32];
1071 size_t outlen;
1072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 memset(output, 0x00, sizeof(output));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001076
1077 /* Prepare context */
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
1079 mbedtls_cipher_info_from_type(cipher_id)));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001080
Paul Bakker5e0efa72013-09-08 23:04:04 +02001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation));
Paul Bakker5e0efa72013-09-08 23:04:04 +02001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, input->x,
1085 mbedtls_cipher_get_block_size(&ctx),
1086 output, &outlen));
1087 TEST_ASSERT(outlen == mbedtls_cipher_get_block_size(&ctx));
1088 TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen,
1089 &outlen));
1090 TEST_ASSERT(0 == outlen);
Paul Bakker5e0efa72013-09-08 23:04:04 +02001091
1092 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 if (0 == finish_result) {
1094 TEST_ASSERT(0 == memcmp(output, result->x,
1095 mbedtls_cipher_get_block_size(&ctx)));
1096 }
Paul Bakker5e0efa72013-09-08 23:04:04 +02001097
Paul Bakkerbd51b262014-07-10 15:26:12 +02001098exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 mbedtls_cipher_free(&ctx);
Paul Bakker5e0efa72013-09-08 23:04:04 +02001100}
1101/* END_CASE */
1102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +01001104void test_vec_crypt(int cipher_id, int operation, data_t *key,
1105 data_t *iv, data_t *input, data_t *result,
1106 int finish_result, int use_psa)
Ron Eldor7b012442017-09-25 17:03:12 +03001107{
Ron Eldor7b012442017-09-25 17:03:12 +03001108 mbedtls_cipher_context_t ctx;
1109 unsigned char output[32];
1110 size_t outlen;
1111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 mbedtls_cipher_init(&ctx);
Ron Eldor7b012442017-09-25 17:03:12 +03001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 memset(output, 0x00, sizeof(output));
Ron Eldor7b012442017-09-25 17:03:12 +03001115
1116 /* Prepare context */
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -04001117#if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
Hanno Beckere43164e2018-11-12 12:46:35 +00001118 (void) use_psa;
1119#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 if (use_psa == 1) {
1121 PSA_ASSERT(psa_crypto_init());
1122 TEST_ASSERT(0 == mbedtls_cipher_setup_psa(&ctx,
1123 mbedtls_cipher_info_from_type(cipher_id), 0));
1124 } else
Przemek Stekiel476d9c42022-05-19 12:26:33 +02001125#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED*/
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx,
1127 mbedtls_cipher_info_from_type(cipher_id)));
Ron Eldor7b012442017-09-25 17:03:12 +03001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation));
1130 if (MBEDTLS_MODE_CBC == ctx.cipher_info->mode) {
1131 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE));
1132 }
Ron Eldor7b012442017-09-25 17:03:12 +03001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 TEST_ASSERT(finish_result == mbedtls_cipher_crypt(&ctx, iv->len ? iv->x : NULL,
1135 iv->len, input->x, input->len,
1136 output, &outlen));
1137 TEST_ASSERT(result->len == outlen);
Ron Eldor7b012442017-09-25 17:03:12 +03001138 /* check plaintext only if everything went fine */
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 if (0 == finish_result) {
1140 TEST_ASSERT(0 == memcmp(output, result->x, outlen));
1141 }
Ron Eldor7b012442017-09-25 17:03:12 +03001142
1143exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 mbedtls_cipher_free(&ctx);
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -04001145#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 PSA_DONE();
Andrzej Kureked052792022-10-21 05:37:54 -04001147#endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_TEST_DEPRECATED */
Ron Eldor7b012442017-09-25 17:03:12 +03001148}
1149/* END_CASE */
1150
1151/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Gilles Peskine449bd832023-01-11 14:50:10 +01001152void set_padding(int cipher_id, int pad_mode, int ret)
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001153{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 const mbedtls_cipher_info_t *cipher_info;
1155 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001156
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 mbedtls_cipher_init(&ctx);
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1160 TEST_ASSERT(NULL != cipher_info);
1161 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 TEST_ASSERT(ret == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001164
Paul Bakkerbd51b262014-07-10 15:26:12 +02001165exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 mbedtls_cipher_free(&ctx);
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001167}
Paul Bakker33b43f12013-08-20 11:48:36 +02001168/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +01001171void check_padding(int pad_mode, data_t *input, int ret, int dlen_check
1172 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001173{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 mbedtls_cipher_info_t cipher_info;
1175 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +01001176 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001177
1178 /* build a fake context just for getting access to get_padding */
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 mbedtls_cipher_init(&ctx);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001181 ctx.cipher_info = &cipher_info;
1182
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode));
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001184
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001185
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 TEST_ASSERT(ret == ctx.get_padding(input->x, input->len, &dlen));
1187 if (0 == ret) {
1188 TEST_ASSERT(dlen == (size_t) dlen_check);
1189 }
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001190}
Paul Bakker33b43f12013-08-20 11:48:36 +02001191/* END_CASE */
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001192
1193/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +01001194void iv_len_validity(int cipher_id, char *cipher_string,
1195 int iv_len_val, int ret)
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001196{
1197 size_t iv_len = iv_len_val;
1198 unsigned char iv[16];
1199
Thomas Daubney3a066ec2022-02-17 12:01:28 +00001200 /* Initialise iv buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 memset(iv, 0, sizeof(iv));
Thomas Daubney3a066ec2022-02-17 12:01:28 +00001202
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001203 const mbedtls_cipher_info_t *cipher_info;
1204 mbedtls_cipher_context_t ctx_dec;
1205 mbedtls_cipher_context_t ctx_enc;
1206
1207 /*
1208 * Prepare contexts
1209 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 mbedtls_cipher_init(&ctx_dec);
1211 mbedtls_cipher_init(&ctx_enc);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001212
1213 /* Check and get info structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 cipher_info = mbedtls_cipher_info_from_type(cipher_id);
1215 TEST_ASSERT(NULL != cipher_info);
1216 TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info);
1217 TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info),
1218 cipher_string) == 0);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001219
1220 /* Initialise enc and dec contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info));
1222 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info));
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len));
1225 TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len));
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001226
1227exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01001228 mbedtls_cipher_free(&ctx_dec);
1229 mbedtls_cipher_free(&ctx_enc);
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001230}
1231/* END_CASE */
Waleed Elmelegya7d206f2023-09-07 17:54:46 +01001232
1233/* BEGIN_CASE */
1234void check_set_padding()
1235{
1236 mbedtls_cipher_context_t ctx;
1237 unsigned char key[16] = { 0 };
1238 unsigned char iv[16] = { 0 };
1239 unsigned char input[16] = { 0 };
1240 unsigned char output[32] = { 0 };
1241 size_t outlen = 0;
1242 int result = MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
1243 const mbedtls_cipher_info_t *cipher_info;
1244
1245 cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_CBC);
1246
1247 mbedtls_cipher_init(&ctx);
1248
1249 TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info));
1250
1251 TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key, 128, MBEDTLS_ENCRYPT));
1252
1253 TEST_ASSERT(result == mbedtls_cipher_crypt(&ctx, iv, sizeof(iv), input,
1254 sizeof(input), output, &outlen));
1255
1256#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
1257 TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE));
1258 TEST_ASSERT(0 == mbedtls_cipher_crypt(&ctx, iv, sizeof(iv), input,
1259 sizeof(input), output, &outlen));
1260#endif
1261
1262exit:
1263 mbedtls_cipher_free(&ctx);
1264}
1265/* END_CASE */