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