| 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 | |
| Valerio Setti | 2f00b7a | 2023-10-17 11:43:34 +0200 | [diff] [blame] | 949 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 950 | void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv, |
| 951 | data_t *ad, data_t *cipher, data_t *tag, |
| 952 | char *result, data_t *clear, int use_psa) |
| Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 953 | { |
| Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 954 | /* |
| 955 | * Take an AEAD ciphertext + tag and perform a pair |
| 956 | * of AEAD decryption and AEAD encryption. Check that |
| Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 957 | * this results in the expected plaintext, and that |
| Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 958 | * decryption and encryption are inverse to one another. |
| 959 | */ |
| Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 960 | |
| Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 961 | int ret; |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 962 | int using_nist_kw, using_nist_kw_padding; |
| Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 963 | |
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 964 | mbedtls_cipher_context_t ctx; |
| Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 965 | size_t outlen; |
| 966 | |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 967 | unsigned char *cipher_plus_tag = NULL; |
| 968 | size_t cipher_plus_tag_len; |
| 969 | unsigned char *decrypt_buf = NULL; |
| 970 | size_t decrypt_buf_len = 0; |
| 971 | unsigned char *encrypt_buf = NULL; |
| 972 | size_t encrypt_buf_len = 0; |
| 973 | |
| Gilles Peskine | 70edd68 | 2020-12-03 20:27:27 +0100 | [diff] [blame] | 974 | /* Null pointers are documented as valid for inputs of length 0. |
| 975 | * The test framework passes non-null pointers, so set them to NULL. |
| 976 | * key, cipher and tag can't be empty. */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 977 | if (iv->len == 0) { |
| Gilles Peskine | 70edd68 | 2020-12-03 20:27:27 +0100 | [diff] [blame] | 978 | iv->x = NULL; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 979 | } |
| 980 | if (ad->len == 0) { |
| Gilles Peskine | 70edd68 | 2020-12-03 20:27:27 +0100 | [diff] [blame] | 981 | ad->x = NULL; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 982 | } |
| 983 | if (clear->len == 0) { |
| Gilles Peskine | 70edd68 | 2020-12-03 20:27:27 +0100 | [diff] [blame] | 984 | clear->x = NULL; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 985 | } |
| Gilles Peskine | 70edd68 | 2020-12-03 20:27:27 +0100 | [diff] [blame] | 986 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 987 | mbedtls_cipher_init(&ctx); |
| Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 988 | |
| Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 989 | /* Initialize PSA Crypto */ |
| 990 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 991 | if (use_psa == 1) { |
| 992 | PSA_ASSERT(psa_crypto_init()); |
| 993 | } |
| Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 994 | #else |
| Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 995 | (void) use_psa; |
| 996 | #endif |
| 997 | |
| 998 | /* |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 999 | * Are we using NIST_KW? with padding? |
| 1000 | */ |
| 1001 | using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP || |
| 1002 | cipher_id == MBEDTLS_CIPHER_AES_192_KWP || |
| 1003 | cipher_id == MBEDTLS_CIPHER_AES_256_KWP; |
| 1004 | using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW || |
| 1005 | cipher_id == MBEDTLS_CIPHER_AES_192_KW || |
| 1006 | cipher_id == MBEDTLS_CIPHER_AES_256_KW || |
| 1007 | using_nist_kw_padding; |
| 1008 | |
| 1009 | /* |
| Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 1010 | * Prepare context for decryption |
| 1011 | */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1012 | if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key, |
| 1013 | MBEDTLS_DECRYPT)) { |
| Gilles Peskine | 8a3d234 | 2020-12-03 21:06:15 +0100 | [diff] [blame] | 1014 | goto exit; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1015 | } |
| Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 1016 | |
| Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 1017 | /* |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1018 | * prepare buffer for decryption |
| 1019 | * (we need the tag appended to the ciphertext) |
| 1020 | */ |
| 1021 | cipher_plus_tag_len = cipher->len + tag->len; |
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1022 | TEST_CALLOC(cipher_plus_tag, cipher_plus_tag_len); |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1023 | memcpy(cipher_plus_tag, cipher->x, cipher->len); |
| 1024 | memcpy(cipher_plus_tag + cipher->len, tag->x, tag->len); |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1025 | |
| 1026 | /* |
| 1027 | * Compute length of output buffer according to the documentation |
| 1028 | */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1029 | if (using_nist_kw) { |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1030 | decrypt_buf_len = cipher_plus_tag_len - 8; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1031 | } else { |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1032 | decrypt_buf_len = cipher_plus_tag_len - tag->len; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1033 | } |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1034 | |
| 1035 | |
| 1036 | /* |
| 1037 | * Try decrypting to a buffer that's 1B too small |
| 1038 | */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1039 | if (decrypt_buf_len != 0) { |
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1040 | TEST_CALLOC(decrypt_buf, decrypt_buf_len - 1); |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1041 | |
| 1042 | outlen = 0; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1043 | ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len, |
| 1044 | ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len, |
| 1045 | decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len); |
| 1046 | TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1047 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1048 | mbedtls_free(decrypt_buf); |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1049 | decrypt_buf = NULL; |
| 1050 | } |
| 1051 | |
| 1052 | /* |
| 1053 | * Authenticate and decrypt, and check result |
| 1054 | */ |
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1055 | TEST_CALLOC(decrypt_buf, decrypt_buf_len); |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1056 | |
| 1057 | outlen = 0; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1058 | ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len, |
| 1059 | ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len, |
| 1060 | decrypt_buf, decrypt_buf_len, &outlen, tag->len); |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1061 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1062 | if (strcmp(result, "FAIL") == 0) { |
| 1063 | TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED); |
| 1064 | TEST_ASSERT(buffer_is_all_zero(decrypt_buf, decrypt_buf_len)); |
| 1065 | } else { |
| 1066 | TEST_ASSERT(ret == 0); |
| Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 1067 | TEST_MEMORY_COMPARE(decrypt_buf, outlen, clear->x, clear->len); |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1068 | } |
| 1069 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1070 | mbedtls_free(decrypt_buf); |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1071 | decrypt_buf = NULL; |
| 1072 | |
| 1073 | /* |
| 1074 | * Encrypt back if test data was authentic |
| 1075 | */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1076 | if (strcmp(result, "FAIL") != 0) { |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1077 | /* prepare context for encryption */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1078 | if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key, |
| 1079 | MBEDTLS_ENCRYPT)) { |
| Gilles Peskine | 8a3d234 | 2020-12-03 21:06:15 +0100 | [diff] [blame] | 1080 | goto exit; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1081 | } |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1082 | |
| 1083 | /* |
| 1084 | * Compute size of output buffer according to documentation |
| 1085 | */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1086 | if (using_nist_kw) { |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1087 | encrypt_buf_len = clear->len + 8; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1088 | if (using_nist_kw_padding && encrypt_buf_len % 8 != 0) { |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1089 | encrypt_buf_len += 8 - encrypt_buf_len % 8; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1090 | } |
| 1091 | } else { |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1092 | encrypt_buf_len = clear->len + tag->len; |
| 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * Try encrypting with an output buffer that's 1B too small |
| 1097 | */ |
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1098 | TEST_CALLOC(encrypt_buf, encrypt_buf_len - 1); |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1099 | |
| 1100 | outlen = 0; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1101 | ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len, |
| 1102 | ad->x, ad->len, clear->x, clear->len, |
| 1103 | encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len); |
| 1104 | TEST_ASSERT(ret != 0); |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1105 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1106 | mbedtls_free(encrypt_buf); |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1107 | encrypt_buf = NULL; |
| 1108 | |
| 1109 | /* |
| 1110 | * Encrypt and check the result |
| 1111 | */ |
| Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1112 | TEST_CALLOC(encrypt_buf, encrypt_buf_len); |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1113 | |
| 1114 | outlen = 0; |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1115 | ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len, |
| 1116 | ad->x, ad->len, clear->x, clear->len, |
| 1117 | encrypt_buf, encrypt_buf_len, &outlen, tag->len); |
| 1118 | TEST_ASSERT(ret == 0); |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1119 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1120 | TEST_ASSERT(outlen == cipher->len + tag->len); |
| 1121 | TEST_ASSERT(memcmp(encrypt_buf, cipher->x, cipher->len) == 0); |
| 1122 | TEST_ASSERT(memcmp(encrypt_buf + cipher->len, |
| 1123 | tag->x, tag->len) == 0); |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1124 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1125 | mbedtls_free(encrypt_buf); |
| Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1126 | encrypt_buf = NULL; |
| 1127 | } |
| 1128 | |
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1129 | exit: |
| Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 1130 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1131 | mbedtls_cipher_free(&ctx); |
| 1132 | mbedtls_free(decrypt_buf); |
| 1133 | mbedtls_free(encrypt_buf); |
| 1134 | mbedtls_free(cipher_plus_tag); |
| Gilles Peskine | 5386f6b | 2019-08-01 12:47:40 +0200 | [diff] [blame] | 1135 | |
| Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 1136 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1137 | if (use_psa == 1) { |
| 1138 | PSA_DONE(); |
| 1139 | } |
| Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 1140 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 1141 | } |
| 1142 | /* END_CASE */ |
| 1143 | |
| Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 1144 | /* BEGIN_CASE */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1145 | void test_vec_ecb(int cipher_id, int operation, data_t *key, |
| 1146 | data_t *input, data_t *result, int finish_result |
| 1147 | ) |
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1148 | { |
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1149 | mbedtls_cipher_context_t ctx; |
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1150 | unsigned char output[32]; |
| 1151 | size_t outlen; |
| 1152 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1153 | mbedtls_cipher_init(&ctx); |
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 1154 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1155 | memset(output, 0x00, sizeof(output)); |
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1156 | |
| 1157 | /* Prepare context */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1158 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, |
| 1159 | mbedtls_cipher_info_from_type(cipher_id))); |
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1160 | |
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1161 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1162 | 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] | 1163 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1164 | TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, input->x, |
| 1165 | mbedtls_cipher_get_block_size(&ctx), |
| 1166 | output, &outlen)); |
| 1167 | TEST_ASSERT(outlen == mbedtls_cipher_get_block_size(&ctx)); |
| 1168 | TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen, |
| 1169 | &outlen)); |
| 1170 | TEST_ASSERT(0 == outlen); |
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1171 | |
| 1172 | /* check plaintext only if everything went fine */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1173 | if (0 == finish_result) { |
| 1174 | TEST_ASSERT(0 == memcmp(output, result->x, |
| 1175 | mbedtls_cipher_get_block_size(&ctx))); |
| 1176 | } |
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1177 | |
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1178 | exit: |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1179 | mbedtls_cipher_free(&ctx); |
| Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1180 | } |
| 1181 | /* END_CASE */ |
| 1182 | |
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1183 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1184 | void test_vec_crypt(int cipher_id, int operation, data_t *key, |
| 1185 | data_t *iv, data_t *input, data_t *result, |
| 1186 | int finish_result, int use_psa) |
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1187 | { |
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1188 | mbedtls_cipher_context_t ctx; |
| 1189 | unsigned char output[32]; |
| 1190 | size_t outlen; |
| 1191 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1192 | mbedtls_cipher_init(&ctx); |
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1193 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1194 | memset(output, 0x00, sizeof(output)); |
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1195 | |
| 1196 | /* Prepare context */ |
| Andrzej Kurek | 8f26c8a | 2022-10-20 05:19:47 -0400 | [diff] [blame] | 1197 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED) |
| Hanno Becker | e43164e | 2018-11-12 12:46:35 +0000 | [diff] [blame] | 1198 | (void) use_psa; |
| 1199 | #else |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1200 | if (use_psa == 1) { |
| 1201 | PSA_ASSERT(psa_crypto_init()); |
| 1202 | TEST_ASSERT(0 == mbedtls_cipher_setup_psa(&ctx, |
| 1203 | mbedtls_cipher_info_from_type(cipher_id), 0)); |
| 1204 | } else |
| Przemek Stekiel | 476d9c4 | 2022-05-19 12:26:33 +0200 | [diff] [blame] | 1205 | #endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED*/ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1206 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, |
| 1207 | mbedtls_cipher_info_from_type(cipher_id))); |
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1208 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1209 | TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation)); |
| 1210 | if (MBEDTLS_MODE_CBC == ctx.cipher_info->mode) { |
| 1211 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE)); |
| 1212 | } |
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1213 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1214 | TEST_ASSERT(finish_result == mbedtls_cipher_crypt(&ctx, iv->len ? iv->x : NULL, |
| 1215 | iv->len, input->x, input->len, |
| 1216 | output, &outlen)); |
| 1217 | TEST_ASSERT(result->len == outlen); |
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1218 | /* check plaintext only if everything went fine */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1219 | if (0 == finish_result) { |
| 1220 | TEST_ASSERT(0 == memcmp(output, result->x, outlen)); |
| 1221 | } |
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1222 | |
| 1223 | exit: |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1224 | mbedtls_cipher_free(&ctx); |
| Andrzej Kurek | 8f26c8a | 2022-10-20 05:19:47 -0400 | [diff] [blame] | 1225 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_TEST_DEPRECATED) |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1226 | PSA_DONE(); |
| Andrzej Kurek | ed05279 | 2022-10-21 05:37:54 -0400 | [diff] [blame] | 1227 | #endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_TEST_DEPRECATED */ |
| Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1228 | } |
| 1229 | /* END_CASE */ |
| 1230 | |
| 1231 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1232 | void set_padding(int cipher_id, int pad_mode, int ret) |
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1233 | { |
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1234 | const mbedtls_cipher_info_t *cipher_info; |
| 1235 | mbedtls_cipher_context_t ctx; |
| Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 1236 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1237 | mbedtls_cipher_init(&ctx); |
| Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 1238 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1239 | cipher_info = mbedtls_cipher_info_from_type(cipher_id); |
| 1240 | TEST_ASSERT(NULL != cipher_info); |
| 1241 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info)); |
| Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 1242 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1243 | 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] | 1244 | |
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1245 | exit: |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1246 | mbedtls_cipher_free(&ctx); |
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1247 | } |
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1248 | /* END_CASE */ |
| Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1249 | |
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1250 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1251 | void check_padding(int pad_mode, data_t *input, int ret, int dlen_check |
| 1252 | ) |
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1253 | { |
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1254 | mbedtls_cipher_info_t cipher_info; |
| 1255 | mbedtls_cipher_context_t ctx; |
| Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1256 | size_t dlen; |
| Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 1257 | |
| 1258 | /* build a fake context just for getting access to get_padding */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1259 | mbedtls_cipher_init(&ctx); |
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1260 | cipher_info.mode = MBEDTLS_MODE_CBC; |
| Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 1261 | ctx.cipher_info = &cipher_info; |
| 1262 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1263 | 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] | 1264 | |
| Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 1265 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1266 | TEST_ASSERT(ret == ctx.get_padding(input->x, input->len, &dlen)); |
| 1267 | if (0 == ret) { |
| 1268 | TEST_ASSERT(dlen == (size_t) dlen_check); |
| 1269 | } |
| Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1270 | } |
| Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1271 | /* END_CASE */ |
| Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1272 | |
| 1273 | /* BEGIN_CASE */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1274 | void iv_len_validity(int cipher_id, char *cipher_string, |
| 1275 | int iv_len_val, int ret) |
| Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1276 | { |
| 1277 | size_t iv_len = iv_len_val; |
| 1278 | unsigned char iv[16]; |
| 1279 | |
| Thomas Daubney | 3a066ec | 2022-02-17 12:01:28 +0000 | [diff] [blame] | 1280 | /* Initialise iv buffer */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1281 | memset(iv, 0, sizeof(iv)); |
| Thomas Daubney | 3a066ec | 2022-02-17 12:01:28 +0000 | [diff] [blame] | 1282 | |
| Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1283 | const mbedtls_cipher_info_t *cipher_info; |
| 1284 | mbedtls_cipher_context_t ctx_dec; |
| 1285 | mbedtls_cipher_context_t ctx_enc; |
| 1286 | |
| 1287 | /* |
| 1288 | * Prepare contexts |
| 1289 | */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1290 | mbedtls_cipher_init(&ctx_dec); |
| 1291 | mbedtls_cipher_init(&ctx_enc); |
| Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1292 | |
| 1293 | /* Check and get info structures */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1294 | cipher_info = mbedtls_cipher_info_from_type(cipher_id); |
| 1295 | TEST_ASSERT(NULL != cipher_info); |
| 1296 | TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info); |
| 1297 | TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info), |
| 1298 | cipher_string) == 0); |
| Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1299 | |
| 1300 | /* Initialise enc and dec contexts */ |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1301 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info)); |
| 1302 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info)); |
| Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1303 | |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1304 | TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len)); |
| 1305 | TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len)); |
| Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1306 | |
| 1307 | exit: |
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1308 | mbedtls_cipher_free(&ctx_dec); |
| 1309 | mbedtls_cipher_free(&ctx_enc); |
| Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1310 | } |
| 1311 | /* END_CASE */ |
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1312 | |
| Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 1313 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
| 1314 | void check_set_padding(int cipher_id) |
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1315 | { |
| 1316 | mbedtls_cipher_context_t ctx; |
| Waleed Elmelegy | f4e6651 | 2023-09-21 14:04:35 +0100 | [diff] [blame] | 1317 | unsigned char *key = NULL; |
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1318 | unsigned char iv[16] = { 0 }; |
| 1319 | unsigned char input[16] = { 0 }; |
| 1320 | unsigned char output[32] = { 0 }; |
| 1321 | size_t outlen = 0; |
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1322 | const mbedtls_cipher_info_t *cipher_info; |
| Waleed Elmelegy | f4e6651 | 2023-09-21 14:04:35 +0100 | [diff] [blame] | 1323 | size_t keylen = 0; |
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1324 | |
| Paul Elliott | 3bda79b | 2023-10-18 15:09:09 +0100 | [diff] [blame] | 1325 | mbedtls_cipher_init(&ctx); |
| 1326 | |
| Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 1327 | cipher_info = mbedtls_cipher_info_from_type(cipher_id); |
| 1328 | |
| 1329 | if (cipher_info->mode != MBEDTLS_MODE_CBC) { |
| 1330 | TEST_FAIL("Cipher mode must be CBC"); |
| 1331 | } |
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1332 | |
| Waleed Elmelegy | f4e6651 | 2023-09-21 14:04:35 +0100 | [diff] [blame] | 1333 | keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info); |
| 1334 | TEST_CALLOC(key, keylen/8); |
| 1335 | memset(key, 0, keylen/8); |
| 1336 | |
| Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 1337 | TEST_EQUAL(0, mbedtls_cipher_setup(&ctx, cipher_info)); |
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1338 | |
| Waleed Elmelegy | f4e6651 | 2023-09-21 14:04:35 +0100 | [diff] [blame] | 1339 | TEST_EQUAL(0, mbedtls_cipher_setkey(&ctx, key, keylen, |
| 1340 | MBEDTLS_ENCRYPT)); |
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1341 | |
| Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 1342 | TEST_EQUAL(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 1343 | mbedtls_cipher_crypt(&ctx, iv, sizeof(iv), input, |
| 1344 | sizeof(input), output, &outlen)); |
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1345 | |
| Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 1346 | TEST_EQUAL(0, mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE)); |
| 1347 | TEST_EQUAL(0, mbedtls_cipher_crypt(&ctx, iv, sizeof(iv), input, |
| 1348 | sizeof(input), output, &outlen)); |
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1349 | |
| 1350 | exit: |
| 1351 | mbedtls_cipher_free(&ctx); |
| Waleed Elmelegy | f4e6651 | 2023-09-21 14:04:35 +0100 | [diff] [blame] | 1352 | mbedtls_free(key); |
| Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1353 | } |
| 1354 | /* END_CASE */ |
| David Horstmann | d37e0c4 | 2025-01-16 16:24:35 +0000 | [diff] [blame] | 1355 | |
| 1356 | /* BEGIN_CASE depends_on:MBEDTLS_TEST_HOOKS */ |
| 1357 | void get_pkcs_padding(data_t *decrypted_block, int exp_ret, int exp_len) |
| 1358 | { |
| 1359 | int ret; |
| 1360 | size_t calculated_len; |
| 1361 | |
| 1362 | TEST_CF_SECRET(decrypted_block->x, decrypted_block->len); |
| David Horstmann | 5a5440e | 2025-03-28 10:57:45 +0000 | [diff] [blame] | 1363 | ret = mbedtls_get_pkcs_padding(decrypted_block->x, decrypted_block->len, |
| 1364 | &calculated_len); |
| David Horstmann | d37e0c4 | 2025-01-16 16:24:35 +0000 | [diff] [blame] | 1365 | |
| 1366 | TEST_EQUAL(ret, exp_ret); |
| 1367 | if (exp_ret == 0) { |
| 1368 | TEST_EQUAL(calculated_len, exp_len); |
| 1369 | } |
| 1370 | } |
| 1371 | /* END_CASE */ |