| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include "mbedtls/cipher.h" | 
| k-stachowiak | d872723 | 2019-07-29 17:46:29 +0200 | [diff] [blame] | 3 | #include "mbedtls/aes.h" | 
| k-stachowiak | d872723 | 2019-07-29 17:46:29 +0200 | [diff] [blame] | 4 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 5 | #if defined(MBEDTLS_GCM_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 6 | #include "mbedtls/gcm.h" | 
| Manuel Pégourié-Gonnard | f7ce67f | 2013-09-03 20:17:35 +0200 | [diff] [blame] | 7 | #endif | 
| Gilles Peskine | 5386f6b | 2019-08-01 12:47:40 +0200 | [diff] [blame] | 8 |  | 
| David Horstmann | d37e0c4 | 2025-01-16 16:24:35 +0000 | [diff] [blame] | 9 | #include "cipher_invasive.h" | 
|  | 10 |  | 
|  | 11 | #include "test/constant_flow.h" | 
|  | 12 |  | 
| Valerio Setti | dcee987 | 2023-10-16 11:35:57 +0200 | [diff] [blame] | 13 | #if defined(MBEDTLS_CIPHER_HAVE_SOME_AEAD_VIA_LEGACY_OR_USE_PSA) || defined(MBEDTLS_NIST_KW_C) | 
| Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 14 | #define MBEDTLS_CIPHER_AUTH_CRYPT | 
|  | 15 | #endif | 
|  | 16 |  | 
| Gilles Peskine | df00d45 | 2025-07-27 18:03:26 +0200 | [diff] [blame] | 17 | /* Our software AES implementation is not constant-time. For constant-time | 
|  | 18 | * testing involving AES, require a hardware-assisted AES that is | 
|  | 19 | * constant-time. | 
|  | 20 | * | 
|  | 21 | * We assume that if the hardware-assisted version is available in the build, | 
|  | 22 | * it will be available at runtime. The AES tests will fail if run on a | 
|  | 23 | * processor without AESNI/AESCE. | 
|  | 24 | */ | 
|  | 25 | #include "aesce.h" | 
|  | 26 | #include "aesni.h" | 
|  | 27 | #if defined(MBEDTLS_AESCE_HAVE_CODE) || defined(MBEDTLS_AESNI_HAVE_CODE) | 
|  | 28 | #define HAVE_CONSTANT_TIME_AES | 
|  | 29 | #endif | 
|  | 30 |  | 
| Gilles Peskine | 0be02bd | 2021-07-19 16:32:54 +0200 | [diff] [blame] | 31 | /* Check the internal consistency of a cipher info structure, and | 
|  | 32 | * check it against mbedtls_cipher_info_from_xxx(). */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 33 | static int check_cipher_info(mbedtls_cipher_type_t type, | 
|  | 34 | const mbedtls_cipher_info_t *info) | 
| Gilles Peskine | 0be02bd | 2021-07-19 16:32:54 +0200 | [diff] [blame] | 35 | { | 
| Max Fillinger | 72abd8a | 2021-11-28 14:02:36 +0100 | [diff] [blame] | 36 | size_t key_bitlen, block_size, iv_size; | 
| Gilles Peskine | 0be02bd | 2021-07-19 16:32:54 +0200 | [diff] [blame] | 37 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 38 | TEST_ASSERT(info != NULL); | 
|  | 39 | TEST_EQUAL(type, mbedtls_cipher_info_get_type(info)); | 
|  | 40 | TEST_EQUAL(type, info->type); | 
|  | 41 | TEST_ASSERT(mbedtls_cipher_info_from_type(type) == info); | 
| Gilles Peskine | 0be02bd | 2021-07-19 16:32:54 +0200 | [diff] [blame] | 42 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 43 | TEST_EQUAL(info->mode, mbedtls_cipher_info_get_mode(info)); | 
| Gilles Peskine | 0be02bd | 2021-07-19 16:32:54 +0200 | [diff] [blame] | 44 |  | 
|  | 45 | /* Insist that get_name() return the string from the structure and | 
|  | 46 | * not a copy. A copy would have an unknown storage duration. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 47 | TEST_ASSERT(mbedtls_cipher_info_get_name(info) == info->name); | 
|  | 48 | TEST_ASSERT(mbedtls_cipher_info_from_string(info->name) == info); | 
| Gilles Peskine | 0be02bd | 2021-07-19 16:32:54 +0200 | [diff] [blame] | 49 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 50 | key_bitlen = mbedtls_cipher_info_get_key_bitlen(info); | 
|  | 51 | block_size = mbedtls_cipher_info_get_block_size(info); | 
|  | 52 | iv_size = mbedtls_cipher_info_get_iv_size(info); | 
|  | 53 | if (info->type == MBEDTLS_CIPHER_NULL) { | 
|  | 54 | TEST_ASSERT(key_bitlen == 0); | 
|  | 55 | TEST_ASSERT(block_size == 1); | 
|  | 56 | TEST_ASSERT(iv_size == 0); | 
|  | 57 | } else if (info->mode == MBEDTLS_MODE_XTS) { | 
|  | 58 | TEST_ASSERT(key_bitlen == 256 || | 
|  | 59 | key_bitlen == 384 || | 
|  | 60 | key_bitlen == 512); | 
|  | 61 | } else if (!strncmp(info->name, "DES-EDE3-", 9)) { | 
|  | 62 | TEST_ASSERT(key_bitlen == 192); | 
|  | 63 | TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info)); | 
|  | 64 | TEST_ASSERT(block_size == 8); | 
|  | 65 | } else if (!strncmp(info->name, "DES-EDE-", 8)) { | 
|  | 66 | TEST_ASSERT(key_bitlen == 128); | 
|  | 67 | TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info)); | 
|  | 68 | TEST_ASSERT(block_size == 8); | 
|  | 69 | } else if (!strncmp(info->name, "DES-", 4)) { | 
|  | 70 | TEST_ASSERT(key_bitlen == 64); | 
|  | 71 | TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info)); | 
|  | 72 | TEST_ASSERT(block_size == 8); | 
|  | 73 | } else if (!strncmp(info->name, "AES", 3)) { | 
|  | 74 | TEST_ASSERT(key_bitlen == 128 || | 
|  | 75 | key_bitlen == 192 || | 
|  | 76 | key_bitlen == 256); | 
|  | 77 | TEST_ASSERT(!mbedtls_cipher_info_has_variable_key_bitlen(info)); | 
|  | 78 | TEST_ASSERT(block_size == 16); | 
|  | 79 | } else { | 
|  | 80 | TEST_ASSERT(key_bitlen == 128 || | 
|  | 81 | key_bitlen == 192 || | 
|  | 82 | key_bitlen == 256); | 
| Gilles Peskine | 6ac8f94 | 2021-09-01 08:31:49 +0200 | [diff] [blame] | 83 | } | 
| Gilles Peskine | 4f4d4b2 | 2023-06-14 17:34:31 +0200 | [diff] [blame] | 84 | TEST_LE_U(key_bitlen, MBEDTLS_MAX_KEY_LENGTH * 8); | 
|  | 85 | TEST_LE_U(block_size, MBEDTLS_MAX_BLOCK_LENGTH); | 
|  | 86 | TEST_LE_U(iv_size, MBEDTLS_MAX_IV_LENGTH); | 
| Gilles Peskine | 0be02bd | 2021-07-19 16:32:54 +0200 | [diff] [blame] | 87 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | if (strstr(info->name, "-ECB") != NULL) { | 
|  | 89 | TEST_ASSERT(iv_size == 0); | 
|  | 90 | TEST_ASSERT(!mbedtls_cipher_info_has_variable_iv_size(info)); | 
|  | 91 | } else if (strstr(info->name, "-CBC") != NULL || | 
|  | 92 | strstr(info->name, "-CTR") != NULL) { | 
|  | 93 | TEST_ASSERT(iv_size == block_size); | 
|  | 94 | TEST_ASSERT(!mbedtls_cipher_info_has_variable_iv_size(info)); | 
|  | 95 | } else if (strstr(info->name, "-GCM") != NULL) { | 
|  | 96 | TEST_ASSERT(iv_size == block_size - 4); | 
|  | 97 | TEST_ASSERT(mbedtls_cipher_info_has_variable_iv_size(info)); | 
| Max Fillinger | c60c3a0 | 2021-11-09 22:38:56 +0100 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | return 1; | 
| Gilles Peskine | 0be02bd | 2021-07-19 16:32:54 +0200 | [diff] [blame] | 101 |  | 
|  | 102 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 103 | return 0; | 
| Gilles Peskine | 0be02bd | 2021-07-19 16:32:54 +0200 | [diff] [blame] | 104 | } | 
|  | 105 |  | 
| Valerio Setti | 2f00b7a | 2023-10-17 11:43:34 +0200 | [diff] [blame] | 106 | #if defined(MBEDTLS_CIPHER_MODE_AEAD) | 
| Manuel Pégourié-Gonnard | 89a8fe5 | 2020-11-27 09:32:55 +0100 | [diff] [blame] | 107 | /* Helper for resetting key/direction | 
|  | 108 | * | 
|  | 109 | * The documentation doesn't explicitly say whether calling | 
|  | 110 | * mbedtls_cipher_setkey() twice is allowed or not. This currently works with | 
|  | 111 | * the default software implementation, but only by accident. It isn't | 
|  | 112 | * guaranteed to work with new ciphers or with alternative implementations of | 
|  | 113 | * individual ciphers, and it doesn't work with the PSA wrappers. So don't do | 
|  | 114 | * it, and instead start with a fresh context. | 
|  | 115 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | static int cipher_reset_key(mbedtls_cipher_context_t *ctx, int cipher_id, | 
|  | 117 | int use_psa, size_t tag_len, const data_t *key, int direction) | 
| Manuel Pégourié-Gonnard | 89a8fe5 | 2020-11-27 09:32:55 +0100 | [diff] [blame] | 118 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 119 | mbedtls_cipher_free(ctx); | 
|  | 120 | mbedtls_cipher_init(ctx); | 
| Manuel Pégourié-Gonnard | 89a8fe5 | 2020-11-27 09:32:55 +0100 | [diff] [blame] | 121 |  | 
| Andrzej Kurek | 8f26c8a | 2022-10-20 05:19:47 -0400 | [diff] [blame] | 122 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED) | 
| Manuel Pégourié-Gonnard | 89a8fe5 | 2020-11-27 09:32:55 +0100 | [diff] [blame] | 123 | (void) use_psa; | 
|  | 124 | (void) tag_len; | 
|  | 125 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | if (use_psa == 1) { | 
|  | 127 | TEST_ASSERT(0 == mbedtls_cipher_setup_psa(ctx, | 
|  | 128 | mbedtls_cipher_info_from_type(cipher_id), | 
|  | 129 | tag_len)); | 
|  | 130 | } else | 
| Przemek Stekiel | 476d9c4 | 2022-05-19 12:26:33 +0200 | [diff] [blame] | 131 | #endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED */ | 
| Manuel Pégourié-Gonnard | 89a8fe5 | 2020-11-27 09:32:55 +0100 | [diff] [blame] | 132 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | TEST_ASSERT(0 == mbedtls_cipher_setup(ctx, | 
|  | 134 | mbedtls_cipher_info_from_type(cipher_id))); | 
| Manuel Pégourié-Gonnard | 89a8fe5 | 2020-11-27 09:32:55 +0100 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | TEST_ASSERT(0 == mbedtls_cipher_setkey(ctx, key->x, 8 * key->len, | 
|  | 138 | direction)); | 
|  | 139 | return 1; | 
| Gilles Peskine | 8a3d234 | 2020-12-03 21:06:15 +0100 | [diff] [blame] | 140 |  | 
| Manuel Pégourié-Gonnard | 89a8fe5 | 2020-11-27 09:32:55 +0100 | [diff] [blame] | 141 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | return 0; | 
| Manuel Pégourié-Gonnard | 89a8fe5 | 2020-11-27 09:32:55 +0100 | [diff] [blame] | 143 | } | 
| Manuel Pégourié-Gonnard | f215ef8 | 2020-12-03 12:33:31 +0100 | [diff] [blame] | 144 |  | 
|  | 145 | /* | 
|  | 146 | * Check if a buffer is all-0 bytes: | 
|  | 147 | * return   1 if it is, | 
|  | 148 | *          0 if it isn't. | 
|  | 149 | */ | 
| Michael Schuster | b1e33fb | 2024-06-04 02:30:22 +0200 | [diff] [blame] | 150 | static int buffer_is_all_zero(const uint8_t *buf, size_t size) | 
| Manuel Pégourié-Gonnard | f215ef8 | 2020-12-03 12:33:31 +0100 | [diff] [blame] | 151 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | for (size_t i = 0; i < size; i++) { | 
|  | 153 | if (buf[i] != 0) { | 
| Manuel Pégourié-Gonnard | f215ef8 | 2020-12-03 12:33:31 +0100 | [diff] [blame] | 154 | return 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | } | 
|  | 156 | } | 
| Manuel Pégourié-Gonnard | f215ef8 | 2020-12-03 12:33:31 +0100 | [diff] [blame] | 157 | return 1; | 
|  | 158 | } | 
| Manuel Pégourié-Gonnard | 89a8fe5 | 2020-11-27 09:32:55 +0100 | [diff] [blame] | 159 | #endif /* MBEDTLS_CIPHER_AUTH_CRYPT */ | 
|  | 160 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 161 | /* END_HEADER */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 162 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 163 | /* BEGIN_DEPENDENCIES | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 164 | * depends_on:MBEDTLS_CIPHER_C | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 165 | * END_DEPENDENCIES | 
|  | 166 | */ | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 167 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 168 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 169 | void mbedtls_cipher_list() | 
| Manuel Pégourié-Gonnard | 66dfc5a | 2014-03-29 16:10:55 +0100 | [diff] [blame] | 170 | { | 
|  | 171 | const int *cipher_type; | 
|  | 172 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 173 | for (cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++) { | 
| Gilles Peskine | 0be02bd | 2021-07-19 16:32:54 +0200 | [diff] [blame] | 174 | const mbedtls_cipher_info_t *info = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 175 | mbedtls_cipher_info_from_type(*cipher_type); | 
|  | 176 | mbedtls_test_set_step(*cipher_type); | 
|  | 177 | if (!check_cipher_info(*cipher_type, info)) { | 
| Gilles Peskine | 0be02bd | 2021-07-19 16:32:54 +0200 | [diff] [blame] | 178 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 179 | } | 
| Gilles Peskine | 0be02bd | 2021-07-19 16:32:54 +0200 | [diff] [blame] | 180 | } | 
| Manuel Pégourié-Gonnard | 66dfc5a | 2014-03-29 16:10:55 +0100 | [diff] [blame] | 181 | } | 
|  | 182 | /* END_CASE */ | 
|  | 183 |  | 
|  | 184 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 185 | void cipher_invalid_param_unconditional() | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 186 | { | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 187 | mbedtls_cipher_context_t valid_ctx; | 
|  | 188 | mbedtls_cipher_context_t invalid_ctx; | 
|  | 189 | mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT; | 
|  | 190 | mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS; | 
|  | 191 | unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; | 
|  | 192 | int valid_size = sizeof(valid_buffer); | 
|  | 193 | int valid_bitlen = valid_size * 8; | 
| Valerio Setti | d3bdccc | 2023-10-04 12:09:06 +0200 | [diff] [blame] | 194 | const int *cipher_list = mbedtls_cipher_list(); | 
|  | 195 | const mbedtls_cipher_info_t *valid_info; | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 196 | size_t size_t_var; | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 197 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 198 | (void) valid_mode; /* In some configurations this is unused */ | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 199 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 200 | mbedtls_cipher_init(&valid_ctx); | 
|  | 201 | mbedtls_cipher_init(&invalid_ctx); | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 202 |  | 
| Valerio Setti | d3bdccc | 2023-10-04 12:09:06 +0200 | [diff] [blame] | 203 | /* Ensure that there is at least 1 supported cipher, otherwise exit gracefully */ | 
| Valerio Setti | 9d9b4b5 | 2023-10-19 10:51:03 +0200 | [diff] [blame] | 204 | TEST_ASSUME(*cipher_list != 0); | 
| Valerio Setti | d3bdccc | 2023-10-04 12:09:06 +0200 | [diff] [blame] | 205 | valid_info = mbedtls_cipher_info_from_type(*cipher_list); | 
|  | 206 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 207 | TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, valid_info) == 0); | 
| Gilles Peskine | dc8ecda | 2021-12-10 14:28:31 +0100 | [diff] [blame] | 208 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 209 | /* mbedtls_cipher_setup() */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | TEST_ASSERT(mbedtls_cipher_setup(&valid_ctx, NULL) == | 
|  | 211 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 212 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 213 | /* mbedtls_cipher_get_block_size() */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 214 | TEST_ASSERT(mbedtls_cipher_get_block_size(&invalid_ctx) == 0); | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 215 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 216 | /* mbedtls_cipher_get_cipher_mode() */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 217 | TEST_ASSERT(mbedtls_cipher_get_cipher_mode(&invalid_ctx) == | 
|  | 218 | MBEDTLS_MODE_NONE); | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 219 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 220 | /* mbedtls_cipher_get_iv_size() */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 221 | TEST_ASSERT(mbedtls_cipher_get_iv_size(&invalid_ctx) == 0); | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 222 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 223 | /* mbedtls_cipher_get_type() */ | 
|  | 224 | TEST_ASSERT( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 225 | mbedtls_cipher_get_type(&invalid_ctx) == | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 226 | MBEDTLS_CIPHER_NONE); | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 227 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 228 | /* mbedtls_cipher_get_name() */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 229 | TEST_ASSERT(mbedtls_cipher_get_name(&invalid_ctx) == 0); | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 230 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 231 | /* mbedtls_cipher_get_key_bitlen() */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 232 | TEST_ASSERT(mbedtls_cipher_get_key_bitlen(&invalid_ctx) == | 
|  | 233 | MBEDTLS_KEY_LENGTH_NONE); | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 234 |  | 
|  | 235 | /* mbedtls_cipher_get_operation() */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 236 | TEST_ASSERT(mbedtls_cipher_get_operation(&invalid_ctx) == | 
|  | 237 | MBEDTLS_OPERATION_NONE); | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 238 |  | 
|  | 239 | /* mbedtls_cipher_setkey() */ | 
|  | 240 | TEST_ASSERT( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 241 | mbedtls_cipher_setkey(&invalid_ctx, | 
|  | 242 | valid_buffer, | 
|  | 243 | valid_bitlen, | 
|  | 244 | valid_operation) == | 
|  | 245 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 246 |  | 
|  | 247 | /* mbedtls_cipher_set_iv() */ | 
|  | 248 | TEST_ASSERT( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 249 | mbedtls_cipher_set_iv(&invalid_ctx, | 
|  | 250 | valid_buffer, | 
|  | 251 | valid_size) == | 
|  | 252 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 253 |  | 
|  | 254 | /* mbedtls_cipher_reset() */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | TEST_ASSERT(mbedtls_cipher_reset(&invalid_ctx) == | 
|  | 256 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 257 |  | 
| Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 258 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 259 | /* mbedtls_cipher_update_ad() */ | 
|  | 260 | TEST_ASSERT( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 261 | mbedtls_cipher_update_ad(&invalid_ctx, | 
|  | 262 | valid_buffer, | 
|  | 263 | valid_size) == | 
|  | 264 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 265 | #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */ | 
|  | 266 |  | 
|  | 267 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) | 
|  | 268 | /* mbedtls_cipher_set_padding_mode() */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 269 | TEST_ASSERT(mbedtls_cipher_set_padding_mode(&invalid_ctx, valid_mode) == | 
|  | 270 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 271 | #endif | 
|  | 272 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 273 | /* mbedtls_cipher_update() */ | 
|  | 274 | TEST_ASSERT( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 275 | mbedtls_cipher_update(&invalid_ctx, | 
|  | 276 | valid_buffer, | 
|  | 277 | valid_size, | 
|  | 278 | valid_buffer, | 
|  | 279 | &size_t_var) == | 
|  | 280 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 281 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 282 | /* mbedtls_cipher_finish() */ | 
|  | 283 | TEST_ASSERT( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | mbedtls_cipher_finish(&invalid_ctx, | 
|  | 285 | valid_buffer, | 
|  | 286 | &size_t_var) == | 
|  | 287 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 288 |  | 
| Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 289 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 290 | /* mbedtls_cipher_write_tag() */ | 
|  | 291 | TEST_ASSERT( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 292 | mbedtls_cipher_write_tag(&invalid_ctx, | 
|  | 293 | valid_buffer, | 
|  | 294 | valid_size) == | 
|  | 295 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 296 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 297 | /* mbedtls_cipher_check_tag() */ | 
|  | 298 | TEST_ASSERT( | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 299 | mbedtls_cipher_check_tag(&invalid_ctx, | 
|  | 300 | valid_buffer, | 
|  | 301 | valid_size) == | 
|  | 302 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 303 | #endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */ | 
|  | 304 |  | 
|  | 305 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 306 | mbedtls_cipher_free(&invalid_ctx); | 
|  | 307 | mbedtls_cipher_free(&valid_ctx); | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 308 | } | 
|  | 309 | /* END_CASE */ | 
|  | 310 |  | 
| Tuvshinzaya Erdenekhuu | 6c68927 | 2022-07-29 14:45:55 +0100 | [diff] [blame] | 311 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 312 | void cipher_invalid_param_conditional() | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 313 | { | 
|  | 314 | mbedtls_cipher_context_t valid_ctx; | 
|  | 315 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 316 | mbedtls_operation_t invalid_operation = 100; | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 317 | unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; | 
|  | 318 | int valid_size = sizeof(valid_buffer); | 
|  | 319 | int valid_bitlen = valid_size * 8; | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 320 |  | 
| Ronald Cron | 875b5fb | 2021-05-21 08:50:00 +0200 | [diff] [blame] | 321 | TEST_EQUAL( | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 322 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 323 | mbedtls_cipher_setkey(&valid_ctx, | 
|  | 324 | valid_buffer, | 
|  | 325 | valid_bitlen, | 
|  | 326 | invalid_operation)); | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 327 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 328 | exit: | 
| TRodziewicz | 7019955 | 2021-05-27 13:52:59 +0200 | [diff] [blame] | 329 | ; | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 330 | } | 
|  | 331 | /* END_CASE */ | 
|  | 332 |  | 
| Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 333 | /* BEGIN_CASE depends_on:MBEDTLS_AES_C */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 334 | void cipher_special_behaviours() | 
| Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 335 | { | 
|  | 336 | const mbedtls_cipher_info_t *cipher_info; | 
|  | 337 | mbedtls_cipher_context_t ctx; | 
|  | 338 | unsigned char input[32]; | 
|  | 339 | unsigned char output[32]; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 340 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | 
| Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 341 | unsigned char iv[32]; | 
| Ron Eldor | 6f90ed8 | 2017-09-26 12:08:54 +0300 | [diff] [blame] | 342 | #endif | 
| Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 343 | size_t olen = 0; | 
|  | 344 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 345 | mbedtls_cipher_init(&ctx); | 
|  | 346 | memset(input, 0, sizeof(input)); | 
|  | 347 | memset(output, 0, sizeof(output)); | 
| Ron Eldor | bb4bbbb | 2017-10-01 17:04:54 +0300 | [diff] [blame] | 348 | #if defined(MBEDTLS_CIPHER_MODE_CBC) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 349 | memset(iv, 0, sizeof(iv)); | 
| Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 350 |  | 
|  | 351 | /* Check and get info structures */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 352 | cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_CBC); | 
|  | 353 | TEST_ASSERT(NULL != cipher_info); | 
| Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 354 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 355 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info)); | 
| Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 356 |  | 
|  | 357 | /* IV too big */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 358 | TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1) | 
|  | 359 | == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE); | 
| Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 360 |  | 
|  | 361 | /* IV too small */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 362 | TEST_ASSERT(mbedtls_cipher_set_iv(&ctx, iv, 0) | 
|  | 363 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); | 
| Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 364 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 365 | mbedtls_cipher_free(&ctx); | 
|  | 366 | mbedtls_cipher_init(&ctx); | 
| Ron Eldor | 6f90ed8 | 2017-09-26 12:08:54 +0300 | [diff] [blame] | 367 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 368 | cipher_info = mbedtls_cipher_info_from_type(MBEDTLS_CIPHER_AES_128_ECB); | 
|  | 369 | TEST_ASSERT(NULL != cipher_info); | 
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 370 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 371 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info)); | 
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 372 |  | 
| Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 373 | /* Update ECB with partial block */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 374 | TEST_ASSERT(mbedtls_cipher_update(&ctx, input, 1, output, &olen) | 
|  | 375 | == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED); | 
| Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 376 |  | 
|  | 377 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 378 | mbedtls_cipher_free(&ctx); | 
| Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 379 | } | 
|  | 380 | /* END_CASE */ | 
|  | 381 |  | 
| Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 382 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 383 | void enc_dec_buf(int cipher_id, char *cipher_string, int key_len, | 
|  | 384 | int length_val, int pad_mode) | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 385 | { | 
| Mateusz Starzyk | f257a6e | 2021-10-27 16:27:44 +0200 | [diff] [blame] | 386 | size_t length = length_val, outlen, total_len, i, block_size, iv_len; | 
| Jaeden Amero | d906b81 | 2018-06-08 11:03:16 +0100 | [diff] [blame] | 387 | unsigned char key[64]; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 388 | unsigned char iv[16]; | 
| Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 389 | unsigned char ad[13]; | 
|  | 390 | unsigned char tag[16]; | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 391 | unsigned char inbuf[64]; | 
|  | 392 | unsigned char encbuf[64]; | 
|  | 393 | unsigned char decbuf[64]; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 394 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 395 | const mbedtls_cipher_info_t *cipher_info; | 
|  | 396 | mbedtls_cipher_context_t ctx_dec; | 
|  | 397 | mbedtls_cipher_context_t ctx_enc; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 398 |  | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 399 | /* | 
|  | 400 | * Prepare contexts | 
|  | 401 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 402 | mbedtls_cipher_init(&ctx_dec); | 
|  | 403 | mbedtls_cipher_init(&ctx_enc); | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 404 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 405 | memset(key, 0x2a, sizeof(key)); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 406 |  | 
|  | 407 | /* Check and get info structures */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 408 | cipher_info = mbedtls_cipher_info_from_type(cipher_id); | 
|  | 409 | TEST_ASSERT(NULL != cipher_info); | 
|  | 410 | TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info); | 
|  | 411 | TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info), | 
|  | 412 | cipher_string) == 0); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 413 |  | 
|  | 414 | /* Initialise enc and dec contexts */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 415 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info)); | 
|  | 416 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info)); | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 417 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT)); | 
|  | 419 | TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT)); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 420 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 421 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 422 | if (-1 != pad_mode) { | 
|  | 423 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode)); | 
|  | 424 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode)); | 
| Manuel Pégourié-Gonnard | 6c97899 | 2013-07-26 13:20:42 +0200 | [diff] [blame] | 425 | } | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 426 | #else | 
|  | 427 | (void) pad_mode; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 428 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ | 
| Manuel Pégourié-Gonnard | 6c97899 | 2013-07-26 13:20:42 +0200 | [diff] [blame] | 429 |  | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 430 | /* | 
|  | 431 | * Do a few encode/decode cycles | 
|  | 432 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | for (i = 0; i < 3; i++) { | 
|  | 434 | memset(iv, 0x00 + i, sizeof(iv)); | 
|  | 435 | memset(ad, 0x10 + i, sizeof(ad)); | 
|  | 436 | memset(inbuf, 0x20 + i, sizeof(inbuf)); | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 437 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | memset(encbuf, 0, sizeof(encbuf)); | 
|  | 439 | memset(decbuf, 0, sizeof(decbuf)); | 
|  | 440 | memset(tag, 0, sizeof(tag)); | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 441 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 442 | if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) { | 
|  | 443 | iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes. | 
|  | 444 | * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */ | 
|  | 445 | } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 || | 
|  | 446 | cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) { | 
|  | 447 | iv_len = 12; | 
|  | 448 | } else { | 
|  | 449 | iv_len = sizeof(iv); | 
|  | 450 | } | 
| Mateusz Starzyk | f257a6e | 2021-10-27 16:27:44 +0200 | [diff] [blame] | 451 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 452 | TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len)); | 
|  | 453 | TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len)); | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 454 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 455 | TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec)); | 
|  | 456 | TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc)); | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 457 |  | 
| Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 458 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 459 | int expected = (cipher_info->mode == MBEDTLS_MODE_GCM || | 
|  | 460 | cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? | 
|  | 461 | 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; | 
| Tom Cosgrove | c621a6d | 2022-09-30 17:13:35 +0100 | [diff] [blame] | 462 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 463 | TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, ad, sizeof(ad) - i)); | 
|  | 464 | TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, ad, sizeof(ad) - i)); | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 465 | #endif | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 466 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 467 | block_size = mbedtls_cipher_get_block_size(&ctx_enc); | 
|  | 468 | TEST_ASSERT(block_size != 0); | 
| Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 469 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 470 | /* encode length number of bytes from inbuf */ | 
|  | 471 | TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, length, encbuf, &outlen)); | 
|  | 472 | total_len = outlen; | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 473 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | TEST_ASSERT(total_len == length || | 
|  | 475 | (total_len % block_size == 0 && | 
|  | 476 | total_len < length && | 
|  | 477 | total_len + block_size > length)); | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 478 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + outlen, &outlen)); | 
|  | 480 | total_len += outlen; | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 481 |  | 
| Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 482 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | TEST_EQUAL(expected, mbedtls_cipher_write_tag(&ctx_enc, tag, sizeof(tag))); | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 484 | #endif | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 485 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 486 | TEST_ASSERT(total_len == length || | 
|  | 487 | (total_len % block_size == 0 && | 
|  | 488 | total_len > length && | 
|  | 489 | total_len <= length + block_size)); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 490 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 491 | /* decode the previously encoded string */ | 
|  | 492 | TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, total_len, decbuf, &outlen)); | 
|  | 493 | total_len = outlen; | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 494 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 495 | TEST_ASSERT(total_len == length || | 
|  | 496 | (total_len % block_size == 0 && | 
|  | 497 | total_len < length && | 
|  | 498 | total_len + block_size >= length)); | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 499 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 500 | TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + outlen, &outlen)); | 
|  | 501 | total_len += outlen; | 
| Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 502 |  | 
| Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 503 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 504 | TEST_EQUAL(expected, mbedtls_cipher_check_tag(&ctx_dec, tag, sizeof(tag))); | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 505 | #endif | 
| Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 506 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 507 | /* check result */ | 
|  | 508 | TEST_ASSERT(total_len == length); | 
|  | 509 | TEST_ASSERT(0 == memcmp(inbuf, decbuf, length)); | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 510 | } | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 511 |  | 
| Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 512 | /* | 
|  | 513 | * Done | 
|  | 514 | */ | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 515 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 516 | mbedtls_cipher_free(&ctx_dec); | 
|  | 517 | mbedtls_cipher_free(&ctx_enc); | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 518 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 519 | /* END_CASE */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 520 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 521 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 522 | void enc_fail(int cipher_id, int pad_mode, int key_len, int length_val, | 
|  | 523 | int ret) | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 524 | { | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 525 | size_t length = length_val; | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 526 | unsigned char key[32]; | 
|  | 527 | unsigned char iv[16]; | 
|  | 528 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 529 | const mbedtls_cipher_info_t *cipher_info; | 
|  | 530 | mbedtls_cipher_context_t ctx; | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 531 |  | 
|  | 532 | unsigned char inbuf[64]; | 
|  | 533 | unsigned char encbuf[64]; | 
|  | 534 |  | 
|  | 535 | size_t outlen = 0; | 
|  | 536 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 537 | memset(key, 0, 32); | 
|  | 538 | memset(iv, 0, 16); | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 539 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 540 | mbedtls_cipher_init(&ctx); | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 541 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 542 | memset(inbuf, 5, 64); | 
|  | 543 | memset(encbuf, 0, 64); | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 544 |  | 
|  | 545 | /* Check and get info structures */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 546 | cipher_info = mbedtls_cipher_info_from_type(cipher_id); | 
|  | 547 | TEST_ASSERT(NULL != cipher_info); | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 548 |  | 
|  | 549 | /* Initialise context */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 550 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info)); | 
|  | 551 | TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key, key_len, MBEDTLS_ENCRYPT)); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 552 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 553 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode)); | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 554 | #else | 
|  | 555 | (void) pad_mode; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 556 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 557 | TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv, 16)); | 
|  | 558 | TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx)); | 
| Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 559 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 560 | int expected = (cipher_info->mode == MBEDTLS_MODE_GCM || | 
|  | 561 | cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? | 
| Tom Cosgrove | c621a6d | 2022-09-30 17:13:35 +0100 | [diff] [blame] | 562 | 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; | 
|  | 563 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 564 | TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, NULL, 0)); | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 565 | #endif | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 566 |  | 
|  | 567 | /* encode length number of bytes from inbuf */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 568 | TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, inbuf, length, encbuf, &outlen)); | 
|  | 569 | TEST_ASSERT(ret == mbedtls_cipher_finish(&ctx, encbuf + outlen, &outlen)); | 
| Andre Goddard Rosa | 3711734 | 2024-05-01 11:47:12 -0500 | [diff] [blame] | 570 | if (0 != ret) { | 
|  | 571 | /* Check output parameter is set to the least-harmful value on error */ | 
|  | 572 | TEST_ASSERT(0 == outlen); | 
|  | 573 | } | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 574 |  | 
|  | 575 | /* done */ | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 576 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 577 | mbedtls_cipher_free(&ctx); | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 578 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 579 | /* END_CASE */ | 
| Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 580 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 581 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 582 | void dec_empty_buf(int cipher, | 
|  | 583 | int expected_update_ret, | 
|  | 584 | int expected_finish_ret) | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 585 | { | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 586 | unsigned char key[32]; | 
| Andrzej Kurek | b9fbc11 | 2022-01-14 16:31:39 +0100 | [diff] [blame] | 587 |  | 
|  | 588 | unsigned char *iv = NULL; | 
|  | 589 | size_t iv_len = 16; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 590 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 591 | mbedtls_cipher_context_t ctx_dec; | 
|  | 592 | const mbedtls_cipher_info_t *cipher_info; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 593 |  | 
|  | 594 | unsigned char encbuf[64]; | 
|  | 595 | unsigned char decbuf[64]; | 
|  | 596 |  | 
| Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 597 | size_t outlen = 0; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 598 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 599 | memset(key, 0, 32); | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 600 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 601 | mbedtls_cipher_init(&ctx_dec); | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 602 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 603 | memset(encbuf, 0, 64); | 
|  | 604 | memset(decbuf, 0, 64); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 605 |  | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 606 | /* Initialise context */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 607 | cipher_info = mbedtls_cipher_info_from_type(cipher); | 
|  | 608 | TEST_ASSERT(NULL != cipher_info); | 
| Andrzej Kurek | 63439ed | 2021-12-01 22:19:33 +0100 | [diff] [blame] | 609 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 610 | if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 || | 
|  | 611 | cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) { | 
| Andrzej Kurek | 63439ed | 2021-12-01 22:19:33 +0100 | [diff] [blame] | 612 | iv_len = 12; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 613 | } | 
| Andrzej Kurek | 63439ed | 2021-12-01 22:19:33 +0100 | [diff] [blame] | 614 |  | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 615 | TEST_CALLOC(iv, iv_len); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 616 | memset(iv, 0, iv_len); | 
| Andrzej Kurek | b9fbc11 | 2022-01-14 16:31:39 +0100 | [diff] [blame] | 617 |  | 
| Dave Rodgman | 9282d4f | 2023-06-24 11:03:04 +0100 | [diff] [blame] | 618 | TEST_ASSERT(sizeof(key) * 8 >= mbedtls_cipher_info_get_key_bitlen(cipher_info)); | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 619 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 620 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info)); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 621 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 622 | TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, | 
| Dave Rodgman | 9282d4f | 2023-06-24 11:03:04 +0100 | [diff] [blame] | 623 | key, mbedtls_cipher_info_get_key_bitlen(cipher_info), | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 624 | MBEDTLS_DECRYPT)); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 625 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 626 | TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len)); | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 627 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 628 | TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec)); | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 629 |  | 
| Waleed Elmelegy | f4e6651 | 2023-09-21 14:04:35 +0100 | [diff] [blame] | 630 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) && defined(MBEDTLS_CIPHER_PADDING_PKCS7) | 
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 631 | if (ctx_dec.cipher_info->mode == MBEDTLS_MODE_CBC) { | 
| Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 632 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, | 
|  | 633 | MBEDTLS_PADDING_PKCS7)); | 
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 634 | } | 
| Waleed Elmelegy | f4e6651 | 2023-09-21 14:04:35 +0100 | [diff] [blame] | 635 | #endif | 
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 636 |  | 
| Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 637 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 638 | int expected = (cipher_info->mode == MBEDTLS_MODE_GCM || | 
|  | 639 | cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? | 
| Tom Cosgrove | c621a6d | 2022-09-30 17:13:35 +0100 | [diff] [blame] | 640 | 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; | 
|  | 641 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 642 | TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0)); | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 643 | #endif | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 644 |  | 
|  | 645 | /* decode 0-byte string */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 646 | TEST_ASSERT(expected_update_ret == | 
|  | 647 | mbedtls_cipher_update(&ctx_dec, encbuf, 0, decbuf, &outlen)); | 
|  | 648 | TEST_ASSERT(0 == outlen); | 
| Jaeden Amero | 5ab80ef | 2019-06-05 15:35:08 +0100 | [diff] [blame] | 649 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 650 | if (expected_finish_ret == 0 && | 
|  | 651 | (cipher_info->mode == MBEDTLS_MODE_CBC || | 
|  | 652 | cipher_info->mode == MBEDTLS_MODE_ECB)) { | 
| Jaeden Amero | 5ab80ef | 2019-06-05 15:35:08 +0100 | [diff] [blame] | 653 | /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and | 
|  | 654 | * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when | 
| k-stachowiak | d872723 | 2019-07-29 17:46:29 +0200 | [diff] [blame] | 655 | * decrypting an empty buffer. | 
|  | 656 | * On the other hand, CBC and ECB ciphers need a full block of input. | 
|  | 657 | */ | 
|  | 658 | expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED; | 
| Jaeden Amero | 5ab80ef | 2019-06-05 15:35:08 +0100 | [diff] [blame] | 659 | } | 
|  | 660 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 661 | TEST_ASSERT(expected_finish_ret == mbedtls_cipher_finish( | 
|  | 662 | &ctx_dec, decbuf + outlen, &outlen)); | 
|  | 663 | TEST_ASSERT(0 == outlen); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 664 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 665 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 666 | mbedtls_free(iv); | 
|  | 667 | mbedtls_cipher_free(&ctx_dec); | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 668 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 669 | /* END_CASE */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 670 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 671 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 672 | void enc_dec_buf_multipart(int cipher_id, int key_len, int first_length_val, | 
|  | 673 | int second_length_val, int pad_mode, | 
|  | 674 | int first_encrypt_output_len, int second_encrypt_output_len, | 
|  | 675 | int first_decrypt_output_len, int second_decrypt_output_len) | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 676 | { | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 677 | size_t first_length = first_length_val; | 
|  | 678 | size_t second_length = second_length_val; | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 679 | size_t length = first_length + second_length; | 
| Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 680 | size_t block_size; | 
| Mateusz Starzyk | f257a6e | 2021-10-27 16:27:44 +0200 | [diff] [blame] | 681 | size_t iv_len; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 682 | unsigned char key[32]; | 
|  | 683 | unsigned char iv[16]; | 
|  | 684 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 685 | mbedtls_cipher_context_t ctx_dec; | 
|  | 686 | mbedtls_cipher_context_t ctx_enc; | 
|  | 687 | const mbedtls_cipher_info_t *cipher_info; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 688 |  | 
|  | 689 | unsigned char inbuf[64]; | 
|  | 690 | unsigned char encbuf[64]; | 
|  | 691 | unsigned char decbuf[64]; | 
|  | 692 |  | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 693 | size_t outlen = 0; | 
|  | 694 | size_t totaloutlen = 0; | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 695 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 696 | memset(key, 0, 32); | 
|  | 697 | memset(iv, 0, 16); | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 698 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 699 | mbedtls_cipher_init(&ctx_dec); | 
|  | 700 | mbedtls_cipher_init(&ctx_enc); | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 701 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 702 | memset(inbuf, 5, 64); | 
|  | 703 | memset(encbuf, 0, 64); | 
|  | 704 | memset(decbuf, 0, 64); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 705 |  | 
|  | 706 | /* Initialise enc and dec contexts */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 707 | cipher_info = mbedtls_cipher_info_from_type(cipher_id); | 
|  | 708 | TEST_ASSERT(NULL != cipher_info); | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 709 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 710 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info)); | 
|  | 711 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info)); | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 712 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 713 | TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT)); | 
|  | 714 | TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_enc, key, key_len, MBEDTLS_ENCRYPT)); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 715 |  | 
| Jethro Beekman | 6c563fa | 2018-03-27 19:16:17 -0700 | [diff] [blame] | 716 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 717 | if (-1 != pad_mode) { | 
|  | 718 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode)); | 
|  | 719 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode)); | 
| Jethro Beekman | 6c563fa | 2018-03-27 19:16:17 -0700 | [diff] [blame] | 720 | } | 
|  | 721 | #else | 
|  | 722 | (void) pad_mode; | 
|  | 723 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ | 
|  | 724 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 725 | if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) { | 
| Mateusz Starzyk | f257a6e | 2021-10-27 16:27:44 +0200 | [diff] [blame] | 726 | iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes. | 
|  | 727 | * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 728 | } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 || | 
|  | 729 | cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) { | 
| Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 730 | iv_len = 12; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 731 | } else { | 
| Mateusz Starzyk | f257a6e | 2021-10-27 16:27:44 +0200 | [diff] [blame] | 732 | iv_len = sizeof(iv); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 733 | } | 
| Mateusz Starzyk | f257a6e | 2021-10-27 16:27:44 +0200 | [diff] [blame] | 734 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 735 | TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len)); | 
|  | 736 | TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len)); | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 737 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 738 | TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec)); | 
|  | 739 | TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc)); | 
| Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 740 |  | 
| Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 741 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 742 | int expected = (cipher_info->mode == MBEDTLS_MODE_GCM || | 
|  | 743 | cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? | 
| Tom Cosgrove | c621a6d | 2022-09-30 17:13:35 +0100 | [diff] [blame] | 744 | 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; | 
|  | 745 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 746 | TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0)); | 
|  | 747 | TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_enc, NULL, 0)); | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 748 | #endif | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 749 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 750 | block_size = mbedtls_cipher_get_block_size(&ctx_enc); | 
|  | 751 | TEST_ASSERT(block_size != 0); | 
| Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 752 |  | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 753 | /* encode length number of bytes from inbuf */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 754 | TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, first_length, encbuf, &outlen)); | 
|  | 755 | TEST_ASSERT((size_t) first_encrypt_output_len == outlen); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 756 | totaloutlen = outlen; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 757 | TEST_ASSERT(0 == | 
|  | 758 | mbedtls_cipher_update(&ctx_enc, inbuf + first_length, second_length, | 
|  | 759 | encbuf + totaloutlen, | 
|  | 760 | &outlen)); | 
|  | 761 | TEST_ASSERT((size_t) second_encrypt_output_len == outlen); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 762 | totaloutlen += outlen; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 763 | TEST_ASSERT(totaloutlen == length || | 
|  | 764 | (totaloutlen % block_size == 0 && | 
|  | 765 | totaloutlen < length && | 
|  | 766 | totaloutlen + block_size > length)); | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 767 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 768 | TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + totaloutlen, &outlen)); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 769 | totaloutlen += outlen; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 770 | TEST_ASSERT(totaloutlen == length || | 
|  | 771 | (totaloutlen % block_size == 0 && | 
|  | 772 | totaloutlen > length && | 
|  | 773 | totaloutlen <= length + block_size)); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 774 |  | 
|  | 775 | /* decode the previously encoded string */ | 
| Jethro Beekman | 6c563fa | 2018-03-27 19:16:17 -0700 | [diff] [blame] | 776 | second_length = totaloutlen - first_length; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 777 | TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, first_length, decbuf, &outlen)); | 
|  | 778 | TEST_ASSERT((size_t) first_decrypt_output_len == outlen); | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 779 | totaloutlen = outlen; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 780 | TEST_ASSERT(0 == | 
|  | 781 | mbedtls_cipher_update(&ctx_dec, encbuf + first_length, second_length, | 
|  | 782 | decbuf + totaloutlen, | 
|  | 783 | &outlen)); | 
|  | 784 | TEST_ASSERT((size_t) second_decrypt_output_len == outlen); | 
| Jethro Beekman | 6c563fa | 2018-03-27 19:16:17 -0700 | [diff] [blame] | 785 | totaloutlen += outlen; | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 786 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 787 | TEST_ASSERT(totaloutlen == length || | 
|  | 788 | (totaloutlen % block_size == 0 && | 
|  | 789 | totaloutlen < length && | 
|  | 790 | totaloutlen + block_size >= length)); | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 791 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 792 | TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + totaloutlen, &outlen)); | 
| Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 793 | totaloutlen += outlen; | 
|  | 794 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 795 | TEST_ASSERT(totaloutlen == length); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 796 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 797 | TEST_ASSERT(0 == memcmp(inbuf, decbuf, length)); | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 798 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 799 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 800 | mbedtls_cipher_free(&ctx_dec); | 
|  | 801 | mbedtls_cipher_free(&ctx_enc); | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 802 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 803 | /* END_CASE */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 804 |  | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 805 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 806 | void decrypt_test_vec(int cipher_id, int pad_mode, data_t *key, | 
|  | 807 | data_t *iv, data_t *cipher, | 
|  | 808 | data_t *clear, data_t *ad, data_t *tag, | 
|  | 809 | int finish_result, int tag_result) | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 810 | { | 
| Manuel Pégourié-Gonnard | 234e1ce | 2018-05-10 12:54:32 +0200 | [diff] [blame] | 811 | unsigned char output[265]; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 812 | mbedtls_cipher_context_t ctx; | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 813 | size_t outlen, total_len; | 
|  | 814 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 815 | mbedtls_cipher_init(&ctx); | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 816 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 817 | memset(output, 0x00, sizeof(output)); | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 818 |  | 
| Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 819 | #if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C) | 
| Mohammad Azim Khan | cf32c45 | 2017-06-13 14:55:58 +0100 | [diff] [blame] | 820 | ((void) ad); | 
|  | 821 | ((void) tag); | 
| Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 822 | #endif | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 823 |  | 
|  | 824 | /* Prepare context */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 825 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, | 
|  | 826 | mbedtls_cipher_info_from_type(cipher_id))); | 
|  | 827 | TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT)); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 828 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 829 | if (pad_mode != -1) { | 
|  | 830 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode)); | 
|  | 831 | } | 
| Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 832 | #else | 
|  | 833 | (void) pad_mode; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 834 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 835 | TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv->x, iv->len)); | 
|  | 836 | TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx)); | 
| Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 837 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 838 | int expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM || | 
|  | 839 | ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? | 
| Tom Cosgrove | c621a6d | 2022-09-30 17:13:35 +0100 | [diff] [blame] | 840 | 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; | 
|  | 841 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 842 | TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, ad->x, ad->len)); | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 843 | #endif | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 844 |  | 
| Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 845 | /* decode buffer and check tag->x */ | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 846 | total_len = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 847 | TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, cipher->x, cipher->len, output, &outlen)); | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 848 | total_len += outlen; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 849 | TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen, | 
|  | 850 | &outlen)); | 
| Andre Goddard Rosa | 3711734 | 2024-05-01 11:47:12 -0500 | [diff] [blame] | 851 | if (0 != finish_result) { | 
|  | 852 | /* Check output parameter is set to the least-harmful value on error */ | 
|  | 853 | TEST_ASSERT(0 == outlen); | 
|  | 854 | } | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 855 | total_len += outlen; | 
| Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 856 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 857 | int tag_expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM || | 
|  | 858 | ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? | 
| Tom Cosgrove | c621a6d | 2022-09-30 17:13:35 +0100 | [diff] [blame] | 859 | tag_result : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; | 
|  | 860 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 861 | TEST_EQUAL(tag_expected, mbedtls_cipher_check_tag(&ctx, tag->x, tag->len)); | 
| Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 862 | #endif | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 863 |  | 
|  | 864 | /* check plaintext only if everything went fine */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 865 | if (0 == finish_result && 0 == tag_result) { | 
|  | 866 | TEST_ASSERT(total_len == clear->len); | 
|  | 867 | TEST_ASSERT(0 == memcmp(output, clear->x, clear->len)); | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 868 | } | 
|  | 869 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 870 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 871 | mbedtls_cipher_free(&ctx); | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 872 | } | 
|  | 873 | /* END_CASE */ | 
|  | 874 |  | 
| Gilles Peskine | df00d45 | 2025-07-27 18:03:26 +0200 | [diff] [blame] | 875 | /* BEGIN_CASE */ | 
|  | 876 | void decrypt_test_vec_cf(int cipher_id, int pad_mode, data_t *key, | 
|  | 877 | data_t *iv, data_t *cipher, | 
|  | 878 | data_t *clear, data_t *ad, data_t *tag, | 
|  | 879 | int expected_finish_result, int tag_result) | 
|  | 880 | { | 
|  | 881 | unsigned char output[265]; | 
|  | 882 | mbedtls_cipher_context_t ctx; | 
|  | 883 | size_t outlen, total_len; | 
|  | 884 |  | 
|  | 885 | mbedtls_cipher_init(&ctx); | 
|  | 886 |  | 
|  | 887 | memset(output, 0x00, sizeof(output)); | 
|  | 888 |  | 
|  | 889 | #if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 890 | ((void) ad); | 
|  | 891 | ((void) tag); | 
|  | 892 | #endif | 
|  | 893 |  | 
|  | 894 | TEST_CF_SECRET(key->x, key->len); | 
|  | 895 | TEST_CF_SECRET(cipher->x, cipher->len); | 
|  | 896 |  | 
|  | 897 | /* Prepare context */ | 
|  | 898 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, | 
|  | 899 | mbedtls_cipher_info_from_type(cipher_id))); | 
|  | 900 | TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT)); | 
|  | 901 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) | 
|  | 902 | if (pad_mode != -1) { | 
|  | 903 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode)); | 
|  | 904 | } | 
|  | 905 | #else | 
|  | 906 | (void) pad_mode; | 
|  | 907 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ | 
|  | 908 | TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv->x, iv->len)); | 
|  | 909 | TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx)); | 
|  | 910 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 911 | int expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM || | 
|  | 912 | ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? | 
|  | 913 | 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; | 
|  | 914 |  | 
|  | 915 | TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, ad->x, ad->len)); | 
|  | 916 | #endif | 
|  | 917 |  | 
|  | 918 | /* decode buffer and check tag->x */ | 
|  | 919 | total_len = 0; | 
|  | 920 | TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, cipher->x, cipher->len, output, &outlen)); | 
|  | 921 | total_len += outlen; | 
|  | 922 | int actual_finish_result = mbedtls_cipher_finish(&ctx, output + outlen, | 
|  | 923 | &outlen); | 
|  | 924 | TEST_EQUAL(actual_finish_result, expected_finish_result); | 
|  | 925 | if (0 != expected_finish_result) { | 
|  | 926 | /* Check output parameter is set to the least-harmful value on error */ | 
|  | 927 | TEST_ASSERT(0 == outlen); | 
|  | 928 | } | 
|  | 929 | total_len += outlen; | 
|  | 930 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 931 | int tag_expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM || | 
|  | 932 | ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? | 
|  | 933 | tag_result : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; | 
|  | 934 |  | 
|  | 935 | TEST_EQUAL(tag_expected, mbedtls_cipher_check_tag(&ctx, tag->x, tag->len)); | 
|  | 936 | #endif | 
|  | 937 |  | 
|  | 938 | /* check plaintext only if everything went fine */ | 
|  | 939 | if (0 == expected_finish_result && 0 == tag_result) { | 
|  | 940 | TEST_CF_PUBLIC(output, sizeof(output)); | 
|  | 941 | TEST_MEMORY_COMPARE(output, total_len, clear->x, clear->len); | 
|  | 942 | } | 
|  | 943 |  | 
|  | 944 | exit: | 
|  | 945 | mbedtls_cipher_free(&ctx); | 
|  | 946 | } | 
|  | 947 | /* END_CASE */ | 
|  | 948 |  | 
| Gilles Peskine | 155de2a | 2025-07-27 18:53:57 +0200 | [diff] [blame^] | 949 | /* BEGIN_CASE */ | 
|  | 950 | void decrypt_padded_test_vec(int cipher_id, int pad_mode, data_t *key, | 
|  | 951 | data_t *iv, data_t *cipher, | 
|  | 952 | data_t *clear, data_t *ad, data_t *tag, | 
|  | 953 | int expected_finish_result, int tag_result) | 
|  | 954 | { | 
|  | 955 | unsigned char output[265]; | 
|  | 956 | mbedtls_cipher_context_t ctx; | 
|  | 957 | size_t outlen, total_len; | 
|  | 958 |  | 
|  | 959 | mbedtls_cipher_init(&ctx); | 
|  | 960 |  | 
|  | 961 | memset(output, 0x00, sizeof(output)); | 
|  | 962 |  | 
|  | 963 | #if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 964 | ((void) ad); | 
|  | 965 | ((void) tag); | 
|  | 966 | #endif | 
|  | 967 |  | 
|  | 968 | /* Prepare context */ | 
|  | 969 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, | 
|  | 970 | mbedtls_cipher_info_from_type(cipher_id))); | 
|  | 971 | TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT)); | 
|  | 972 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) | 
|  | 973 | if (pad_mode != -1) { | 
|  | 974 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode)); | 
|  | 975 | } | 
|  | 976 | #else | 
|  | 977 | (void) pad_mode; | 
|  | 978 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ | 
|  | 979 | TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv->x, iv->len)); | 
|  | 980 | TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx)); | 
|  | 981 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 982 | int expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM || | 
|  | 983 | ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? | 
|  | 984 | 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; | 
|  | 985 |  | 
|  | 986 | TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, ad->x, ad->len)); | 
|  | 987 | #endif | 
|  | 988 |  | 
|  | 989 | /* decode buffer and check tag->x */ | 
|  | 990 | total_len = 0; | 
|  | 991 | TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, cipher->x, cipher->len, output, &outlen)); | 
|  | 992 | total_len += outlen; | 
|  | 993 |  | 
|  | 994 | size_t invalid_padding = 42; | 
|  | 995 | int actual_finish_result = | 
|  | 996 | mbedtls_cipher_finish_padded(&ctx, output + outlen, &outlen, | 
|  | 997 | &invalid_padding); | 
|  | 998 | switch (expected_finish_result) { | 
|  | 999 | case 0: | 
|  | 1000 | TEST_EQUAL(actual_finish_result, 0); | 
|  | 1001 | TEST_EQUAL(invalid_padding, 0); | 
|  | 1002 | break; | 
|  | 1003 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: | 
|  | 1004 | TEST_EQUAL(actual_finish_result, 0); | 
|  | 1005 | TEST_EQUAL(invalid_padding, SIZE_MAX); | 
|  | 1006 | break; | 
|  | 1007 | default: | 
|  | 1008 | TEST_EQUAL(actual_finish_result, expected_finish_result); | 
|  | 1009 | /* Check output parameter is set to the least-harmful value on error */ | 
|  | 1010 | TEST_EQUAL(0, outlen); | 
|  | 1011 | break; | 
|  | 1012 | } | 
|  | 1013 | total_len += outlen; | 
|  | 1014 |  | 
|  | 1015 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 1016 | int tag_expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM || | 
|  | 1017 | ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? | 
|  | 1018 | tag_result : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; | 
|  | 1019 |  | 
|  | 1020 | TEST_EQUAL(tag_expected, mbedtls_cipher_check_tag(&ctx, tag->x, tag->len)); | 
|  | 1021 | #endif | 
|  | 1022 |  | 
|  | 1023 | /* check plaintext only if everything went fine */ | 
|  | 1024 | if (0 == expected_finish_result && 0 == tag_result) { | 
|  | 1025 | TEST_ASSERT(total_len == clear->len); | 
|  | 1026 | TEST_ASSERT(0 == memcmp(output, clear->x, clear->len)); | 
|  | 1027 | } | 
|  | 1028 |  | 
|  | 1029 | exit: | 
|  | 1030 | mbedtls_cipher_free(&ctx); | 
|  | 1031 | } | 
|  | 1032 | /* END_CASE */ | 
|  | 1033 |  | 
|  | 1034 | /* BEGIN_CASE */ | 
|  | 1035 | void decrypt_padded_test_vec_cf(int cipher_id, int pad_mode, data_t *key, | 
|  | 1036 | data_t *iv, data_t *cipher, | 
|  | 1037 | data_t *clear, data_t *ad, data_t *tag, | 
|  | 1038 | int expected_finish_result, int tag_result) | 
|  | 1039 | { | 
|  | 1040 | unsigned char output[265]; | 
|  | 1041 | mbedtls_cipher_context_t ctx; | 
|  | 1042 | size_t outlen, total_len; | 
|  | 1043 |  | 
|  | 1044 | mbedtls_cipher_init(&ctx); | 
|  | 1045 |  | 
|  | 1046 | memset(output, 0x00, sizeof(output)); | 
|  | 1047 |  | 
|  | 1048 | #if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 1049 | ((void) ad); | 
|  | 1050 | ((void) tag); | 
|  | 1051 | #endif | 
|  | 1052 |  | 
|  | 1053 | TEST_CF_SECRET(key->x, key->len); | 
|  | 1054 | TEST_CF_SECRET(cipher->x, cipher->len); | 
|  | 1055 |  | 
|  | 1056 | /* Prepare context */ | 
|  | 1057 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, | 
|  | 1058 | mbedtls_cipher_info_from_type(cipher_id))); | 
|  | 1059 | TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT)); | 
|  | 1060 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) | 
|  | 1061 | if (pad_mode != -1) { | 
|  | 1062 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode)); | 
|  | 1063 | } | 
|  | 1064 | #else | 
|  | 1065 | (void) pad_mode; | 
|  | 1066 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ | 
|  | 1067 | TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv->x, iv->len)); | 
|  | 1068 | TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx)); | 
|  | 1069 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 1070 | int expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM || | 
|  | 1071 | ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? | 
|  | 1072 | 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; | 
|  | 1073 |  | 
|  | 1074 | TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, ad->x, ad->len)); | 
|  | 1075 | #endif | 
|  | 1076 |  | 
|  | 1077 | /* decode buffer and check tag->x */ | 
|  | 1078 | total_len = 0; | 
|  | 1079 | TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, cipher->x, cipher->len, output, &outlen)); | 
|  | 1080 | total_len += outlen; | 
|  | 1081 |  | 
|  | 1082 | size_t invalid_padding = 42; | 
|  | 1083 | int actual_finish_result = | 
|  | 1084 | mbedtls_cipher_finish_padded(&ctx, output + outlen, &outlen, | 
|  | 1085 | &invalid_padding); | 
|  | 1086 | switch (expected_finish_result) { | 
|  | 1087 | case 0: | 
|  | 1088 | TEST_EQUAL(actual_finish_result, 0); | 
|  | 1089 | TEST_EQUAL(invalid_padding, 0); | 
|  | 1090 | break; | 
|  | 1091 | case MBEDTLS_ERR_CIPHER_INVALID_PADDING: | 
|  | 1092 | TEST_EQUAL(actual_finish_result, 0); | 
|  | 1093 | TEST_EQUAL(invalid_padding, SIZE_MAX); | 
|  | 1094 | break; | 
|  | 1095 | default: | 
|  | 1096 | TEST_EQUAL(actual_finish_result, expected_finish_result); | 
|  | 1097 | /* Check output parameter is set to the least-harmful value on error */ | 
|  | 1098 | TEST_EQUAL(0, outlen); | 
|  | 1099 | break; | 
|  | 1100 | } | 
|  | 1101 | total_len += outlen; | 
|  | 1102 |  | 
|  | 1103 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) | 
|  | 1104 | int tag_expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM || | 
|  | 1105 | ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? | 
|  | 1106 | tag_result : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; | 
|  | 1107 |  | 
|  | 1108 | TEST_EQUAL(tag_expected, mbedtls_cipher_check_tag(&ctx, tag->x, tag->len)); | 
|  | 1109 | #endif | 
|  | 1110 |  | 
|  | 1111 | /* check plaintext only if everything went fine */ | 
|  | 1112 | if (0 == expected_finish_result && 0 == tag_result) { | 
|  | 1113 | TEST_CF_PUBLIC(output, sizeof(output)); | 
|  | 1114 | TEST_MEMORY_COMPARE(output, total_len, clear->x, clear->len); | 
|  | 1115 | } | 
|  | 1116 |  | 
|  | 1117 | exit: | 
|  | 1118 | mbedtls_cipher_free(&ctx); | 
|  | 1119 | } | 
|  | 1120 | /* END_CASE */ | 
|  | 1121 |  | 
| Valerio Setti | 2f00b7a | 2023-10-17 11:43:34 +0200 | [diff] [blame] | 1122 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1123 | void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv, | 
|  | 1124 | data_t *ad, data_t *cipher, data_t *tag, | 
|  | 1125 | char *result, data_t *clear, int use_psa) | 
| Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 1126 | { | 
| Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 1127 | /* | 
|  | 1128 | * Take an AEAD ciphertext + tag and perform a pair | 
|  | 1129 | * of AEAD decryption and AEAD encryption. Check that | 
| Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 1130 | * this results in the expected plaintext, and that | 
| Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 1131 | * decryption and encryption are inverse to one another. | 
|  | 1132 | */ | 
| Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 1133 |  | 
| Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 1134 | int ret; | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1135 | int using_nist_kw, using_nist_kw_padding; | 
| Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 1136 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1137 | mbedtls_cipher_context_t ctx; | 
| Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 1138 | size_t outlen; | 
|  | 1139 |  | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1140 | unsigned char *cipher_plus_tag = NULL; | 
|  | 1141 | size_t cipher_plus_tag_len; | 
|  | 1142 | unsigned char *decrypt_buf = NULL; | 
|  | 1143 | size_t decrypt_buf_len = 0; | 
|  | 1144 | unsigned char *encrypt_buf = NULL; | 
|  | 1145 | size_t encrypt_buf_len = 0; | 
|  | 1146 |  | 
| Gilles Peskine | 70edd68 | 2020-12-03 20:27:27 +0100 | [diff] [blame] | 1147 | /* Null pointers are documented as valid for inputs of length 0. | 
|  | 1148 | * The test framework passes non-null pointers, so set them to NULL. | 
|  | 1149 | * key, cipher and tag can't be empty. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1150 | if (iv->len == 0) { | 
| Gilles Peskine | 70edd68 | 2020-12-03 20:27:27 +0100 | [diff] [blame] | 1151 | iv->x = NULL; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1152 | } | 
|  | 1153 | if (ad->len == 0) { | 
| Gilles Peskine | 70edd68 | 2020-12-03 20:27:27 +0100 | [diff] [blame] | 1154 | ad->x = NULL; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1155 | } | 
|  | 1156 | if (clear->len == 0) { | 
| Gilles Peskine | 70edd68 | 2020-12-03 20:27:27 +0100 | [diff] [blame] | 1157 | clear->x = NULL; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1158 | } | 
| Gilles Peskine | 70edd68 | 2020-12-03 20:27:27 +0100 | [diff] [blame] | 1159 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1160 | mbedtls_cipher_init(&ctx); | 
| Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 1161 |  | 
| Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 1162 | /* Initialize PSA Crypto */ | 
|  | 1163 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1164 | if (use_psa == 1) { | 
|  | 1165 | PSA_ASSERT(psa_crypto_init()); | 
|  | 1166 | } | 
| Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 1167 | #else | 
| Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 1168 | (void) use_psa; | 
|  | 1169 | #endif | 
|  | 1170 |  | 
|  | 1171 | /* | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1172 | * Are we using NIST_KW? with padding? | 
|  | 1173 | */ | 
|  | 1174 | using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP || | 
|  | 1175 | cipher_id == MBEDTLS_CIPHER_AES_192_KWP || | 
|  | 1176 | cipher_id == MBEDTLS_CIPHER_AES_256_KWP; | 
|  | 1177 | using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW || | 
|  | 1178 | cipher_id == MBEDTLS_CIPHER_AES_192_KW || | 
|  | 1179 | cipher_id == MBEDTLS_CIPHER_AES_256_KW || | 
|  | 1180 | using_nist_kw_padding; | 
|  | 1181 |  | 
|  | 1182 | /* | 
| Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 1183 | * Prepare context for decryption | 
|  | 1184 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1185 | if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key, | 
|  | 1186 | MBEDTLS_DECRYPT)) { | 
| Gilles Peskine | 8a3d234 | 2020-12-03 21:06:15 +0100 | [diff] [blame] | 1187 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1188 | } | 
| Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 1189 |  | 
| Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 1190 | /* | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1191 | * prepare buffer for decryption | 
|  | 1192 | * (we need the tag appended to the ciphertext) | 
|  | 1193 | */ | 
|  | 1194 | cipher_plus_tag_len = cipher->len + tag->len; | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1195 | TEST_CALLOC(cipher_plus_tag, cipher_plus_tag_len); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1196 | memcpy(cipher_plus_tag, cipher->x, cipher->len); | 
|  | 1197 | memcpy(cipher_plus_tag + cipher->len, tag->x, tag->len); | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1198 |  | 
|  | 1199 | /* | 
|  | 1200 | * Compute length of output buffer according to the documentation | 
|  | 1201 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1202 | if (using_nist_kw) { | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1203 | decrypt_buf_len = cipher_plus_tag_len - 8; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1204 | } else { | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1205 | decrypt_buf_len = cipher_plus_tag_len - tag->len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1206 | } | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1207 |  | 
|  | 1208 |  | 
|  | 1209 | /* | 
|  | 1210 | * Try decrypting to a buffer that's 1B too small | 
|  | 1211 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1212 | if (decrypt_buf_len != 0) { | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1213 | TEST_CALLOC(decrypt_buf, decrypt_buf_len - 1); | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1214 |  | 
|  | 1215 | outlen = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1216 | ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len, | 
|  | 1217 | ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len, | 
|  | 1218 | decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len); | 
|  | 1219 | TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1220 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1221 | mbedtls_free(decrypt_buf); | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1222 | decrypt_buf = NULL; | 
|  | 1223 | } | 
|  | 1224 |  | 
|  | 1225 | /* | 
|  | 1226 | * Authenticate and decrypt, and check result | 
|  | 1227 | */ | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1228 | TEST_CALLOC(decrypt_buf, decrypt_buf_len); | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1229 |  | 
|  | 1230 | outlen = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1231 | ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len, | 
|  | 1232 | ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len, | 
|  | 1233 | decrypt_buf, decrypt_buf_len, &outlen, tag->len); | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1234 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1235 | if (strcmp(result, "FAIL") == 0) { | 
|  | 1236 | TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED); | 
|  | 1237 | TEST_ASSERT(buffer_is_all_zero(decrypt_buf, decrypt_buf_len)); | 
|  | 1238 | } else { | 
|  | 1239 | TEST_ASSERT(ret == 0); | 
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1240 | TEST_MEMORY_COMPARE(decrypt_buf, outlen, clear->x, clear->len); | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1241 | } | 
|  | 1242 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1243 | mbedtls_free(decrypt_buf); | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1244 | decrypt_buf = NULL; | 
|  | 1245 |  | 
|  | 1246 | /* | 
|  | 1247 | * Encrypt back if test data was authentic | 
|  | 1248 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1249 | if (strcmp(result, "FAIL") != 0) { | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1250 | /* prepare context for encryption */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1251 | if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key, | 
|  | 1252 | MBEDTLS_ENCRYPT)) { | 
| Gilles Peskine | 8a3d234 | 2020-12-03 21:06:15 +0100 | [diff] [blame] | 1253 | goto exit; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1254 | } | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1255 |  | 
|  | 1256 | /* | 
|  | 1257 | * Compute size of output buffer according to documentation | 
|  | 1258 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1259 | if (using_nist_kw) { | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1260 | encrypt_buf_len = clear->len + 8; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1261 | if (using_nist_kw_padding && encrypt_buf_len % 8 != 0) { | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1262 | encrypt_buf_len += 8 - encrypt_buf_len % 8; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1263 | } | 
|  | 1264 | } else { | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1265 | encrypt_buf_len = clear->len + tag->len; | 
|  | 1266 | } | 
|  | 1267 |  | 
|  | 1268 | /* | 
|  | 1269 | * Try encrypting with an output buffer that's 1B too small | 
|  | 1270 | */ | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1271 | TEST_CALLOC(encrypt_buf, encrypt_buf_len - 1); | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1272 |  | 
|  | 1273 | outlen = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1274 | ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len, | 
|  | 1275 | ad->x, ad->len, clear->x, clear->len, | 
|  | 1276 | encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len); | 
|  | 1277 | TEST_ASSERT(ret != 0); | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1278 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1279 | mbedtls_free(encrypt_buf); | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1280 | encrypt_buf = NULL; | 
|  | 1281 |  | 
|  | 1282 | /* | 
|  | 1283 | * Encrypt and check the result | 
|  | 1284 | */ | 
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1285 | TEST_CALLOC(encrypt_buf, encrypt_buf_len); | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1286 |  | 
|  | 1287 | outlen = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1288 | ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len, | 
|  | 1289 | ad->x, ad->len, clear->x, clear->len, | 
|  | 1290 | encrypt_buf, encrypt_buf_len, &outlen, tag->len); | 
|  | 1291 | TEST_ASSERT(ret == 0); | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1292 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1293 | TEST_ASSERT(outlen == cipher->len + tag->len); | 
|  | 1294 | TEST_ASSERT(memcmp(encrypt_buf, cipher->x, cipher->len) == 0); | 
|  | 1295 | TEST_ASSERT(memcmp(encrypt_buf + cipher->len, | 
|  | 1296 | tag->x, tag->len) == 0); | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1297 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1298 | mbedtls_free(encrypt_buf); | 
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1299 | encrypt_buf = NULL; | 
|  | 1300 | } | 
|  | 1301 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1302 | exit: | 
| Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 1303 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1304 | mbedtls_cipher_free(&ctx); | 
|  | 1305 | mbedtls_free(decrypt_buf); | 
|  | 1306 | mbedtls_free(encrypt_buf); | 
|  | 1307 | mbedtls_free(cipher_plus_tag); | 
| Gilles Peskine | 5386f6b | 2019-08-01 12:47:40 +0200 | [diff] [blame] | 1308 |  | 
| Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 1309 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1310 | if (use_psa == 1) { | 
|  | 1311 | PSA_DONE(); | 
|  | 1312 | } | 
| Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 1313 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 1314 | } | 
|  | 1315 | /* END_CASE */ | 
|  | 1316 |  | 
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 1317 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1318 | void test_vec_ecb(int cipher_id, int operation, data_t *key, | 
|  | 1319 | data_t *input, data_t *result, int finish_result | 
|  | 1320 | ) | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1321 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1322 | mbedtls_cipher_context_t ctx; | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1323 | unsigned char output[32]; | 
|  | 1324 | size_t outlen; | 
|  | 1325 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1326 | mbedtls_cipher_init(&ctx); | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 1327 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1328 | memset(output, 0x00, sizeof(output)); | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1329 |  | 
|  | 1330 | /* Prepare context */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1331 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, | 
|  | 1332 | mbedtls_cipher_info_from_type(cipher_id))); | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1333 |  | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1334 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1335 | TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation)); | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1336 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1337 | TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, input->x, | 
|  | 1338 | mbedtls_cipher_get_block_size(&ctx), | 
|  | 1339 | output, &outlen)); | 
|  | 1340 | TEST_ASSERT(outlen == mbedtls_cipher_get_block_size(&ctx)); | 
|  | 1341 | TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen, | 
|  | 1342 | &outlen)); | 
|  | 1343 | TEST_ASSERT(0 == outlen); | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1344 |  | 
|  | 1345 | /* check plaintext only if everything went fine */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1346 | if (0 == finish_result) { | 
|  | 1347 | TEST_ASSERT(0 == memcmp(output, result->x, | 
|  | 1348 | mbedtls_cipher_get_block_size(&ctx))); | 
|  | 1349 | } | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1350 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1351 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1352 | mbedtls_cipher_free(&ctx); | 
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1353 | } | 
|  | 1354 | /* END_CASE */ | 
|  | 1355 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1356 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1357 | void test_vec_crypt(int cipher_id, int operation, data_t *key, | 
|  | 1358 | data_t *iv, data_t *input, data_t *result, | 
|  | 1359 | int finish_result, int use_psa) | 
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1360 | { | 
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1361 | mbedtls_cipher_context_t ctx; | 
|  | 1362 | unsigned char output[32]; | 
|  | 1363 | size_t outlen; | 
|  | 1364 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1365 | mbedtls_cipher_init(&ctx); | 
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1366 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1367 | memset(output, 0x00, sizeof(output)); | 
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1368 |  | 
|  | 1369 | /* Prepare context */ | 
| Andrzej Kurek | 8f26c8a | 2022-10-20 05:19:47 -0400 | [diff] [blame] | 1370 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED) | 
| Hanno Becker | e43164e | 2018-11-12 12:46:35 +0000 | [diff] [blame] | 1371 | (void) use_psa; | 
|  | 1372 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1373 | if (use_psa == 1) { | 
|  | 1374 | PSA_ASSERT(psa_crypto_init()); | 
|  | 1375 | TEST_ASSERT(0 == mbedtls_cipher_setup_psa(&ctx, | 
|  | 1376 | mbedtls_cipher_info_from_type(cipher_id), 0)); | 
|  | 1377 | } else | 
| Przemek Stekiel | 476d9c4 | 2022-05-19 12:26:33 +0200 | [diff] [blame] | 1378 | #endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED*/ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1379 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, | 
|  | 1380 | mbedtls_cipher_info_from_type(cipher_id))); | 
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1381 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1382 | TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation)); | 
|  | 1383 | if (MBEDTLS_MODE_CBC == ctx.cipher_info->mode) { | 
|  | 1384 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE)); | 
|  | 1385 | } | 
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1386 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1387 | TEST_ASSERT(finish_result == mbedtls_cipher_crypt(&ctx, iv->len ? iv->x : NULL, | 
|  | 1388 | iv->len, input->x, input->len, | 
|  | 1389 | output, &outlen)); | 
|  | 1390 | TEST_ASSERT(result->len == outlen); | 
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1391 | /* check plaintext only if everything went fine */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1392 | if (0 == finish_result) { | 
|  | 1393 | TEST_ASSERT(0 == memcmp(output, result->x, outlen)); | 
|  | 1394 | } | 
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1395 |  | 
|  | 1396 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1397 | mbedtls_cipher_free(&ctx); | 
| Andrzej Kurek | 8f26c8a | 2022-10-20 05:19:47 -0400 | [diff] [blame] | 1398 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_TEST_DEPRECATED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1399 | PSA_DONE(); | 
| Andrzej Kurek | ed05279 | 2022-10-21 05:37:54 -0400 | [diff] [blame] | 1400 | #endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_TEST_DEPRECATED */ | 
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1401 | } | 
|  | 1402 | /* END_CASE */ | 
|  | 1403 |  | 
|  | 1404 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */ | 
| Gilles Peskine | 2da5328 | 2025-07-27 18:44:21 +0200 | [diff] [blame] | 1405 | void test_vec_crypt_cf(int cipher_id, int pad_mode, int operation, data_t *key, | 
|  | 1406 | data_t *iv, data_t *input, data_t *result, | 
|  | 1407 | int expected_finish_result, int use_psa) | 
|  | 1408 | { | 
|  | 1409 | mbedtls_cipher_context_t ctx; | 
|  | 1410 | unsigned char output[32]; | 
|  | 1411 | size_t outlen; | 
|  | 1412 |  | 
|  | 1413 | mbedtls_cipher_init(&ctx); | 
|  | 1414 |  | 
|  | 1415 | memset(output, 0x00, sizeof(output)); | 
|  | 1416 |  | 
|  | 1417 | TEST_CF_SECRET(key->x, key->len); | 
|  | 1418 | TEST_CF_SECRET(input->x, input->len); | 
|  | 1419 |  | 
|  | 1420 | /* Prepare context */ | 
|  | 1421 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED) | 
|  | 1422 | (void) use_psa; | 
|  | 1423 | #else | 
|  | 1424 | if (use_psa == 1) { | 
|  | 1425 | PSA_ASSERT(psa_crypto_init()); | 
|  | 1426 | TEST_ASSERT(0 == mbedtls_cipher_setup_psa(&ctx, | 
|  | 1427 | mbedtls_cipher_info_from_type(cipher_id), 0)); | 
|  | 1428 | } else | 
|  | 1429 | #endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED*/ | 
|  | 1430 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, | 
|  | 1431 | mbedtls_cipher_info_from_type(cipher_id))); | 
|  | 1432 |  | 
|  | 1433 | TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation)); | 
|  | 1434 | if (MBEDTLS_MODE_CBC == ctx.cipher_info->mode) { | 
|  | 1435 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode)); | 
|  | 1436 | } | 
|  | 1437 |  | 
|  | 1438 | int actual_finish_result = | 
|  | 1439 | mbedtls_cipher_crypt(&ctx, iv->len ? iv->x : NULL, iv->len, | 
|  | 1440 | input->x, input->len, | 
|  | 1441 | output, &outlen); | 
|  | 1442 | TEST_EQUAL(expected_finish_result, actual_finish_result); | 
|  | 1443 |  | 
|  | 1444 | /* check plaintext only if everything went fine */ | 
|  | 1445 | if (0 == expected_finish_result) { | 
|  | 1446 | TEST_CF_PUBLIC(output, sizeof(output)); | 
|  | 1447 | TEST_MEMORY_COMPARE(output, outlen, result->x, result->len); | 
|  | 1448 | } | 
|  | 1449 |  | 
|  | 1450 | exit: | 
|  | 1451 | mbedtls_cipher_free(&ctx); | 
|  | 1452 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_TEST_DEPRECATED) | 
|  | 1453 | PSA_DONE(); | 
|  | 1454 | #endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_TEST_DEPRECATED */ | 
|  | 1455 | } | 
|  | 1456 | /* END_CASE */ | 
|  | 1457 |  | 
|  | 1458 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1459 | void set_padding(int cipher_id, int pad_mode, int ret) | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1460 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1461 | const mbedtls_cipher_info_t *cipher_info; | 
|  | 1462 | mbedtls_cipher_context_t ctx; | 
| Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 1463 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1464 | mbedtls_cipher_init(&ctx); | 
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 1465 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1466 | cipher_info = mbedtls_cipher_info_from_type(cipher_id); | 
|  | 1467 | TEST_ASSERT(NULL != cipher_info); | 
|  | 1468 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info)); | 
| Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 1469 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1470 | TEST_ASSERT(ret == mbedtls_cipher_set_padding_mode(&ctx, pad_mode)); | 
| Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 1471 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1472 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1473 | mbedtls_cipher_free(&ctx); | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1474 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1475 | /* END_CASE */ | 
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1476 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1477 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1478 | void check_padding(int pad_mode, data_t *input, int ret, int dlen_check | 
|  | 1479 | ) | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1480 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1481 | mbedtls_cipher_info_t cipher_info; | 
|  | 1482 | mbedtls_cipher_context_t ctx; | 
| Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1483 | size_t dlen; | 
| Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 1484 |  | 
|  | 1485 | /* build a fake context just for getting access to get_padding */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1486 | mbedtls_cipher_init(&ctx); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1487 | cipher_info.mode = MBEDTLS_MODE_CBC; | 
| Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 1488 | ctx.cipher_info = &cipher_info; | 
|  | 1489 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1490 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode)); | 
| Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 1491 |  | 
| Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 1492 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1493 | TEST_ASSERT(ret == ctx.get_padding(input->x, input->len, &dlen)); | 
|  | 1494 | if (0 == ret) { | 
|  | 1495 | TEST_ASSERT(dlen == (size_t) dlen_check); | 
|  | 1496 | } | 
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1497 | } | 
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1498 | /* END_CASE */ | 
| Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1499 |  | 
|  | 1500 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1501 | void iv_len_validity(int cipher_id, char *cipher_string, | 
|  | 1502 | int iv_len_val, int ret) | 
| Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1503 | { | 
|  | 1504 | size_t iv_len = iv_len_val; | 
|  | 1505 | unsigned char iv[16]; | 
|  | 1506 |  | 
| Thomas Daubney | 3a066ec | 2022-02-17 12:01:28 +0000 | [diff] [blame] | 1507 | /* Initialise iv buffer */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1508 | memset(iv, 0, sizeof(iv)); | 
| Thomas Daubney | 3a066ec | 2022-02-17 12:01:28 +0000 | [diff] [blame] | 1509 |  | 
| Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1510 | const mbedtls_cipher_info_t *cipher_info; | 
|  | 1511 | mbedtls_cipher_context_t ctx_dec; | 
|  | 1512 | mbedtls_cipher_context_t ctx_enc; | 
|  | 1513 |  | 
|  | 1514 | /* | 
|  | 1515 | * Prepare contexts | 
|  | 1516 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1517 | mbedtls_cipher_init(&ctx_dec); | 
|  | 1518 | mbedtls_cipher_init(&ctx_enc); | 
| Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1519 |  | 
|  | 1520 | /* Check and get info structures */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1521 | cipher_info = mbedtls_cipher_info_from_type(cipher_id); | 
|  | 1522 | TEST_ASSERT(NULL != cipher_info); | 
|  | 1523 | TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info); | 
|  | 1524 | TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info), | 
|  | 1525 | cipher_string) == 0); | 
| Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1526 |  | 
|  | 1527 | /* Initialise enc and dec contexts */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1528 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info)); | 
|  | 1529 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info)); | 
| Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1530 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1531 | TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len)); | 
|  | 1532 | TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len)); | 
| Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1533 |  | 
|  | 1534 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1535 | mbedtls_cipher_free(&ctx_dec); | 
|  | 1536 | mbedtls_cipher_free(&ctx_enc); | 
| Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1537 | } | 
|  | 1538 | /* END_CASE */ | 
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1539 |  | 
| Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 1540 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */ | 
|  | 1541 | void check_set_padding(int cipher_id) | 
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1542 | { | 
|  | 1543 | mbedtls_cipher_context_t ctx; | 
| Waleed Elmelegy | f4e6651 | 2023-09-21 14:04:35 +0100 | [diff] [blame] | 1544 | unsigned char *key = NULL; | 
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1545 | unsigned char iv[16] = { 0 }; | 
|  | 1546 | unsigned char input[16] = { 0 }; | 
|  | 1547 | unsigned char output[32] = { 0 }; | 
|  | 1548 | size_t outlen = 0; | 
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1549 | const mbedtls_cipher_info_t *cipher_info; | 
| Waleed Elmelegy | f4e6651 | 2023-09-21 14:04:35 +0100 | [diff] [blame] | 1550 | size_t keylen = 0; | 
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1551 |  | 
| Paul Elliott | 3bda79b | 2023-10-18 15:09:09 +0100 | [diff] [blame] | 1552 | mbedtls_cipher_init(&ctx); | 
|  | 1553 |  | 
| Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 1554 | cipher_info = mbedtls_cipher_info_from_type(cipher_id); | 
|  | 1555 |  | 
|  | 1556 | if (cipher_info->mode != MBEDTLS_MODE_CBC) { | 
|  | 1557 | TEST_FAIL("Cipher mode must be CBC"); | 
|  | 1558 | } | 
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1559 |  | 
| Waleed Elmelegy | f4e6651 | 2023-09-21 14:04:35 +0100 | [diff] [blame] | 1560 | keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info); | 
|  | 1561 | TEST_CALLOC(key, keylen/8); | 
|  | 1562 | memset(key, 0, keylen/8); | 
|  | 1563 |  | 
| Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 1564 | TEST_EQUAL(0, mbedtls_cipher_setup(&ctx, cipher_info)); | 
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1565 |  | 
| Waleed Elmelegy | f4e6651 | 2023-09-21 14:04:35 +0100 | [diff] [blame] | 1566 | TEST_EQUAL(0, mbedtls_cipher_setkey(&ctx, key, keylen, | 
|  | 1567 | MBEDTLS_ENCRYPT)); | 
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1568 |  | 
| Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 1569 | TEST_EQUAL(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, | 
|  | 1570 | mbedtls_cipher_crypt(&ctx, iv, sizeof(iv), input, | 
|  | 1571 | sizeof(input), output, &outlen)); | 
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1572 |  | 
| Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 1573 | TEST_EQUAL(0, mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE)); | 
|  | 1574 | TEST_EQUAL(0, mbedtls_cipher_crypt(&ctx, iv, sizeof(iv), input, | 
|  | 1575 | sizeof(input), output, &outlen)); | 
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1576 |  | 
|  | 1577 | exit: | 
|  | 1578 | mbedtls_cipher_free(&ctx); | 
| Waleed Elmelegy | f4e6651 | 2023-09-21 14:04:35 +0100 | [diff] [blame] | 1579 | mbedtls_free(key); | 
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1580 | } | 
|  | 1581 | /* END_CASE */ | 
| David Horstmann | d37e0c4 | 2025-01-16 16:24:35 +0000 | [diff] [blame] | 1582 |  | 
|  | 1583 | /* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */ | 
|  | 1584 | void get_pkcs_padding(data_t *decrypted_block, int exp_ret, int exp_len) | 
|  | 1585 | { | 
|  | 1586 | int ret; | 
|  | 1587 | size_t calculated_len; | 
|  | 1588 |  | 
|  | 1589 | TEST_CF_SECRET(decrypted_block->x, decrypted_block->len); | 
| David Horstmann | 5a5440e | 2025-03-28 10:57:45 +0000 | [diff] [blame] | 1590 | ret = mbedtls_get_pkcs_padding(decrypted_block->x, decrypted_block->len, | 
|  | 1591 | &calculated_len); | 
| David Horstmann | d37e0c4 | 2025-01-16 16:24:35 +0000 | [diff] [blame] | 1592 |  | 
|  | 1593 | TEST_EQUAL(ret, exp_ret); | 
|  | 1594 | if (exp_ret == 0) { | 
|  | 1595 | TEST_EQUAL(calculated_len, exp_len); | 
|  | 1596 | } | 
|  | 1597 | } | 
|  | 1598 | /* END_CASE */ |