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)); |
Manuel Pégourié-Gonnard | 6c97899 | 2013-07-26 13:20:42 +0200 | [diff] [blame] | 404 | } |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 405 | #else |
| 406 | (void) pad_mode; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 407 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
Manuel Pégourié-Gonnard | 6c97899 | 2013-07-26 13:20:42 +0200 | [diff] [blame] | 408 | |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 409 | /* |
| 410 | * Do a few encode/decode cycles |
| 411 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 412 | for (i = 0; i < 3; i++) { |
| 413 | memset(iv, 0x00 + i, sizeof(iv)); |
| 414 | memset(ad, 0x10 + i, sizeof(ad)); |
| 415 | memset(inbuf, 0x20 + i, sizeof(inbuf)); |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 416 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 417 | memset(encbuf, 0, sizeof(encbuf)); |
| 418 | memset(decbuf, 0, sizeof(decbuf)); |
| 419 | memset(tag, 0, sizeof(tag)); |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 421 | if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) { |
| 422 | iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes. |
| 423 | * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */ |
| 424 | } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 || |
| 425 | cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) { |
| 426 | iv_len = 12; |
| 427 | } else { |
| 428 | iv_len = sizeof(iv); |
| 429 | } |
Mateusz Starzyk | f257a6e | 2021-10-27 16:27:44 +0200 | [diff] [blame] | 430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 431 | TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len)); |
| 432 | 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] | 433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 434 | TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec)); |
| 435 | TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc)); |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 436 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 437 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | int expected = (cipher_info->mode == MBEDTLS_MODE_GCM || |
| 439 | cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? |
| 440 | 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
Tom Cosgrove | c621a6d | 2022-09-30 17:13:35 +0100 | [diff] [blame] | 441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 442 | TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, ad, sizeof(ad) - i)); |
| 443 | 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] | 444 | #endif |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 446 | block_size = mbedtls_cipher_get_block_size(&ctx_enc); |
| 447 | TEST_ASSERT(block_size != 0); |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 448 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | /* encode length number of bytes from inbuf */ |
| 450 | TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, length, encbuf, &outlen)); |
| 451 | total_len = outlen; |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 452 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 453 | TEST_ASSERT(total_len == length || |
| 454 | (total_len % block_size == 0 && |
| 455 | total_len < length && |
| 456 | total_len + block_size > length)); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 458 | TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + outlen, &outlen)); |
| 459 | total_len += outlen; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 460 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 461 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 462 | 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] | 463 | #endif |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 464 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 465 | TEST_ASSERT(total_len == length || |
| 466 | (total_len % block_size == 0 && |
| 467 | total_len > length && |
| 468 | total_len <= length + block_size)); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 469 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 470 | /* decode the previously encoded string */ |
| 471 | TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, total_len, decbuf, &outlen)); |
| 472 | total_len = outlen; |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 473 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 474 | TEST_ASSERT(total_len == length || |
| 475 | (total_len % block_size == 0 && |
| 476 | total_len < length && |
| 477 | total_len + block_size >= length)); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_dec, decbuf + outlen, &outlen)); |
| 480 | total_len += outlen; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 481 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 482 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | TEST_EQUAL(expected, mbedtls_cipher_check_tag(&ctx_dec, tag, sizeof(tag))); |
Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 484 | #endif |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 486 | /* check result */ |
| 487 | TEST_ASSERT(total_len == length); |
| 488 | TEST_ASSERT(0 == memcmp(inbuf, decbuf, length)); |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 489 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 490 | |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 491 | /* |
| 492 | * Done |
| 493 | */ |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 494 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 495 | mbedtls_cipher_free(&ctx_dec); |
| 496 | mbedtls_cipher_free(&ctx_enc); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 497 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 498 | /* END_CASE */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 499 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 500 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 501 | void enc_fail(int cipher_id, int pad_mode, int key_len, int length_val, |
| 502 | int ret) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 503 | { |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 504 | size_t length = length_val; |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 505 | unsigned char key[32]; |
| 506 | unsigned char iv[16]; |
| 507 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 508 | const mbedtls_cipher_info_t *cipher_info; |
| 509 | mbedtls_cipher_context_t ctx; |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 510 | |
| 511 | unsigned char inbuf[64]; |
| 512 | unsigned char encbuf[64]; |
| 513 | |
| 514 | size_t outlen = 0; |
| 515 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 516 | memset(key, 0, 32); |
| 517 | memset(iv, 0, 16); |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 518 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 519 | mbedtls_cipher_init(&ctx); |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 520 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 521 | memset(inbuf, 5, 64); |
| 522 | memset(encbuf, 0, 64); |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 523 | |
| 524 | /* Check and get info structures */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 525 | cipher_info = mbedtls_cipher_info_from_type(cipher_id); |
| 526 | TEST_ASSERT(NULL != cipher_info); |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 527 | |
| 528 | /* Initialise context */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 529 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info)); |
| 530 | 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] | 531 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 532 | 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] | 533 | #else |
| 534 | (void) pad_mode; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 535 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 536 | TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv, 16)); |
| 537 | TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx)); |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 538 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 539 | int expected = (cipher_info->mode == MBEDTLS_MODE_GCM || |
| 540 | cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? |
Tom Cosgrove | c621a6d | 2022-09-30 17:13:35 +0100 | [diff] [blame] | 541 | 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 543 | TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx, NULL, 0)); |
Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 544 | #endif |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 545 | |
| 546 | /* encode length number of bytes from inbuf */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 547 | TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, inbuf, length, encbuf, &outlen)); |
| 548 | TEST_ASSERT(ret == mbedtls_cipher_finish(&ctx, encbuf + outlen, &outlen)); |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 549 | |
| 550 | /* done */ |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 551 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 552 | mbedtls_cipher_free(&ctx); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 553 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 554 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 555 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 556 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 557 | void dec_empty_buf(int cipher, |
| 558 | int expected_update_ret, |
| 559 | int expected_finish_ret) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 560 | { |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 561 | unsigned char key[32]; |
Andrzej Kurek | b9fbc11 | 2022-01-14 16:31:39 +0100 | [diff] [blame] | 562 | |
| 563 | unsigned char *iv = NULL; |
| 564 | size_t iv_len = 16; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 565 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 566 | mbedtls_cipher_context_t ctx_dec; |
| 567 | const mbedtls_cipher_info_t *cipher_info; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 568 | |
| 569 | unsigned char encbuf[64]; |
| 570 | unsigned char decbuf[64]; |
| 571 | |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 572 | size_t outlen = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 573 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 574 | memset(key, 0, 32); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 576 | mbedtls_cipher_init(&ctx_dec); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 577 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 578 | memset(encbuf, 0, 64); |
| 579 | memset(decbuf, 0, 64); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 580 | |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 581 | /* Initialise context */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 582 | cipher_info = mbedtls_cipher_info_from_type(cipher); |
| 583 | TEST_ASSERT(NULL != cipher_info); |
Andrzej Kurek | 63439ed | 2021-12-01 22:19:33 +0100 | [diff] [blame] | 584 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 585 | if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 || |
| 586 | cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) { |
Andrzej Kurek | 63439ed | 2021-12-01 22:19:33 +0100 | [diff] [blame] | 587 | iv_len = 12; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 588 | } |
Andrzej Kurek | 63439ed | 2021-12-01 22:19:33 +0100 | [diff] [blame] | 589 | |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 590 | TEST_CALLOC(iv, iv_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 591 | memset(iv, 0, iv_len); |
Andrzej Kurek | b9fbc11 | 2022-01-14 16:31:39 +0100 | [diff] [blame] | 592 | |
Dave Rodgman | 9282d4f | 2023-06-24 11:03:04 +0100 | [diff] [blame] | 593 | 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] | 594 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 595 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info)); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 596 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 597 | TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, |
Dave Rodgman | 9282d4f | 2023-06-24 11:03:04 +0100 | [diff] [blame] | 598 | key, mbedtls_cipher_info_get_key_bitlen(cipher_info), |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 599 | MBEDTLS_DECRYPT)); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 600 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 601 | 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] | 602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 603 | TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec)); |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 604 | |
Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 605 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
| 606 | if (ctx_dec.cipher_info->mode == MBEDTLS_MODE_CBC) { |
| 607 | #if defined(MBEDTLS_CIPHER_PADDING_PKCS7) |
Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 608 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, |
| 609 | MBEDTLS_PADDING_PKCS7)); |
Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 610 | #endif |
| 611 | } |
| 612 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
| 613 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 614 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 615 | int expected = (cipher_info->mode == MBEDTLS_MODE_GCM || |
| 616 | cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? |
Tom Cosgrove | c621a6d | 2022-09-30 17:13:35 +0100 | [diff] [blame] | 617 | 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 618 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 619 | 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] | 620 | #endif |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 621 | |
| 622 | /* decode 0-byte string */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 623 | TEST_ASSERT(expected_update_ret == |
| 624 | mbedtls_cipher_update(&ctx_dec, encbuf, 0, decbuf, &outlen)); |
| 625 | TEST_ASSERT(0 == outlen); |
Jaeden Amero | 5ab80ef | 2019-06-05 15:35:08 +0100 | [diff] [blame] | 626 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 627 | if (expected_finish_ret == 0 && |
| 628 | (cipher_info->mode == MBEDTLS_MODE_CBC || |
| 629 | cipher_info->mode == MBEDTLS_MODE_ECB)) { |
Jaeden Amero | 5ab80ef | 2019-06-05 15:35:08 +0100 | [diff] [blame] | 630 | /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and |
| 631 | * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when |
k-stachowiak | d872723 | 2019-07-29 17:46:29 +0200 | [diff] [blame] | 632 | * decrypting an empty buffer. |
| 633 | * On the other hand, CBC and ECB ciphers need a full block of input. |
| 634 | */ |
| 635 | expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED; |
Jaeden Amero | 5ab80ef | 2019-06-05 15:35:08 +0100 | [diff] [blame] | 636 | } |
| 637 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 638 | TEST_ASSERT(expected_finish_ret == mbedtls_cipher_finish( |
| 639 | &ctx_dec, decbuf + outlen, &outlen)); |
| 640 | TEST_ASSERT(0 == outlen); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 641 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 642 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 643 | mbedtls_free(iv); |
| 644 | mbedtls_cipher_free(&ctx_dec); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 645 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 646 | /* END_CASE */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 647 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 648 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 649 | void enc_dec_buf_multipart(int cipher_id, int key_len, int first_length_val, |
| 650 | int second_length_val, int pad_mode, |
| 651 | int first_encrypt_output_len, int second_encrypt_output_len, |
| 652 | int first_decrypt_output_len, int second_decrypt_output_len) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 653 | { |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 654 | size_t first_length = first_length_val; |
| 655 | size_t second_length = second_length_val; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 656 | size_t length = first_length + second_length; |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 657 | size_t block_size; |
Mateusz Starzyk | f257a6e | 2021-10-27 16:27:44 +0200 | [diff] [blame] | 658 | size_t iv_len; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 659 | unsigned char key[32]; |
| 660 | unsigned char iv[16]; |
| 661 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 662 | mbedtls_cipher_context_t ctx_dec; |
| 663 | mbedtls_cipher_context_t ctx_enc; |
| 664 | const mbedtls_cipher_info_t *cipher_info; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 665 | |
| 666 | unsigned char inbuf[64]; |
| 667 | unsigned char encbuf[64]; |
| 668 | unsigned char decbuf[64]; |
| 669 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 670 | size_t outlen = 0; |
| 671 | size_t totaloutlen = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 672 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 673 | memset(key, 0, 32); |
| 674 | memset(iv, 0, 16); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 675 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 676 | mbedtls_cipher_init(&ctx_dec); |
| 677 | mbedtls_cipher_init(&ctx_enc); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 678 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 679 | memset(inbuf, 5, 64); |
| 680 | memset(encbuf, 0, 64); |
| 681 | memset(decbuf, 0, 64); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 682 | |
| 683 | /* Initialise enc and dec contexts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 684 | cipher_info = mbedtls_cipher_info_from_type(cipher_id); |
| 685 | TEST_ASSERT(NULL != cipher_info); |
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 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info)); |
| 688 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info)); |
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 | TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx_dec, key, key_len, MBEDTLS_DECRYPT)); |
| 691 | 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] | 692 | |
Jethro Beekman | 6c563fa | 2018-03-27 19:16:17 -0700 | [diff] [blame] | 693 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 694 | if (-1 != pad_mode) { |
| 695 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_dec, pad_mode)); |
| 696 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx_enc, pad_mode)); |
Jethro Beekman | 6c563fa | 2018-03-27 19:16:17 -0700 | [diff] [blame] | 697 | } |
| 698 | #else |
| 699 | (void) pad_mode; |
| 700 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
| 701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 702 | if (NULL != strstr(cipher_info->name, "CCM*-NO-TAG")) { |
Mateusz Starzyk | f257a6e | 2021-10-27 16:27:44 +0200 | [diff] [blame] | 703 | iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes. |
| 704 | * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 705 | } else if (cipher_info->type == MBEDTLS_CIPHER_CHACHA20 || |
| 706 | cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) { |
Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 707 | iv_len = 12; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 708 | } else { |
Mateusz Starzyk | f257a6e | 2021-10-27 16:27:44 +0200 | [diff] [blame] | 709 | iv_len = sizeof(iv); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 710 | } |
Mateusz Starzyk | f257a6e | 2021-10-27 16:27:44 +0200 | [diff] [blame] | 711 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 712 | TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len)); |
| 713 | 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] | 714 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 715 | TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_dec)); |
| 716 | TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx_enc)); |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 717 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 718 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 719 | int expected = (cipher_info->mode == MBEDTLS_MODE_GCM || |
| 720 | cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? |
Tom Cosgrove | c621a6d | 2022-09-30 17:13:35 +0100 | [diff] [blame] | 721 | 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 722 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 723 | TEST_EQUAL(expected, mbedtls_cipher_update_ad(&ctx_dec, NULL, 0)); |
| 724 | 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] | 725 | #endif |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 727 | block_size = mbedtls_cipher_get_block_size(&ctx_enc); |
| 728 | TEST_ASSERT(block_size != 0); |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 729 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 730 | /* encode length number of bytes from inbuf */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 731 | TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_enc, inbuf, first_length, encbuf, &outlen)); |
| 732 | TEST_ASSERT((size_t) first_encrypt_output_len == outlen); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 733 | totaloutlen = outlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 734 | TEST_ASSERT(0 == |
| 735 | mbedtls_cipher_update(&ctx_enc, inbuf + first_length, second_length, |
| 736 | encbuf + totaloutlen, |
| 737 | &outlen)); |
| 738 | TEST_ASSERT((size_t) second_encrypt_output_len == outlen); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 739 | totaloutlen += outlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 740 | TEST_ASSERT(totaloutlen == length || |
| 741 | (totaloutlen % block_size == 0 && |
| 742 | totaloutlen < length && |
| 743 | totaloutlen + block_size > length)); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 744 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 745 | TEST_ASSERT(0 == mbedtls_cipher_finish(&ctx_enc, encbuf + totaloutlen, &outlen)); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 746 | totaloutlen += outlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 747 | TEST_ASSERT(totaloutlen == length || |
| 748 | (totaloutlen % block_size == 0 && |
| 749 | totaloutlen > length && |
| 750 | totaloutlen <= length + block_size)); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 751 | |
| 752 | /* decode the previously encoded string */ |
Jethro Beekman | 6c563fa | 2018-03-27 19:16:17 -0700 | [diff] [blame] | 753 | second_length = totaloutlen - first_length; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 754 | TEST_ASSERT(0 == mbedtls_cipher_update(&ctx_dec, encbuf, first_length, decbuf, &outlen)); |
| 755 | TEST_ASSERT((size_t) first_decrypt_output_len == outlen); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 756 | totaloutlen = outlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 757 | TEST_ASSERT(0 == |
| 758 | mbedtls_cipher_update(&ctx_dec, encbuf + first_length, second_length, |
| 759 | decbuf + totaloutlen, |
| 760 | &outlen)); |
| 761 | TEST_ASSERT((size_t) second_decrypt_output_len == outlen); |
Jethro Beekman | 6c563fa | 2018-03-27 19:16:17 -0700 | [diff] [blame] | 762 | totaloutlen += outlen; |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 763 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 764 | TEST_ASSERT(totaloutlen == length || |
| 765 | (totaloutlen % block_size == 0 && |
| 766 | totaloutlen < length && |
| 767 | totaloutlen + block_size >= length)); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 768 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 769 | 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] | 770 | totaloutlen += outlen; |
| 771 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 772 | TEST_ASSERT(totaloutlen == length); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 773 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 774 | TEST_ASSERT(0 == memcmp(inbuf, decbuf, length)); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 775 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 776 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 777 | mbedtls_cipher_free(&ctx_dec); |
| 778 | mbedtls_cipher_free(&ctx_enc); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 779 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 780 | /* END_CASE */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 781 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 782 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 783 | void decrypt_test_vec(int cipher_id, int pad_mode, data_t *key, |
| 784 | data_t *iv, data_t *cipher, |
| 785 | data_t *clear, data_t *ad, data_t *tag, |
| 786 | int finish_result, int tag_result) |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 787 | { |
Manuel Pégourié-Gonnard | 234e1ce | 2018-05-10 12:54:32 +0200 | [diff] [blame] | 788 | unsigned char output[265]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 789 | mbedtls_cipher_context_t ctx; |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 790 | size_t outlen, total_len; |
| 791 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 792 | mbedtls_cipher_init(&ctx); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 793 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 794 | memset(output, 0x00, sizeof(output)); |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 795 | |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 796 | #if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C) |
Mohammad Azim Khan | cf32c45 | 2017-06-13 14:55:58 +0100 | [diff] [blame] | 797 | ((void) ad); |
| 798 | ((void) tag); |
Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 799 | #endif |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 800 | |
| 801 | /* Prepare context */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 802 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, |
| 803 | mbedtls_cipher_info_from_type(cipher_id))); |
| 804 | 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] | 805 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 806 | if (pad_mode != -1) { |
| 807 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, pad_mode)); |
| 808 | } |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 809 | #else |
| 810 | (void) pad_mode; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 811 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 812 | TEST_ASSERT(0 == mbedtls_cipher_set_iv(&ctx, iv->x, iv->len)); |
| 813 | TEST_ASSERT(0 == mbedtls_cipher_reset(&ctx)); |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 814 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 815 | int expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM || |
| 816 | ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? |
Tom Cosgrove | c621a6d | 2022-09-30 17:13:35 +0100 | [diff] [blame] | 817 | 0 : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 818 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 819 | 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] | 820 | #endif |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 821 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 822 | /* decode buffer and check tag->x */ |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 823 | total_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 824 | 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] | 825 | total_len += outlen; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 826 | TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen, |
| 827 | &outlen)); |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 828 | total_len += outlen; |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 829 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 830 | int tag_expected = (ctx.cipher_info->mode == MBEDTLS_MODE_GCM || |
| 831 | ctx.cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305) ? |
Tom Cosgrove | c621a6d | 2022-09-30 17:13:35 +0100 | [diff] [blame] | 832 | tag_result : MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE; |
| 833 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 834 | 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] | 835 | #endif |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 836 | |
| 837 | /* check plaintext only if everything went fine */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 838 | if (0 == finish_result && 0 == tag_result) { |
| 839 | TEST_ASSERT(total_len == clear->len); |
| 840 | TEST_ASSERT(0 == memcmp(output, clear->x, clear->len)); |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 841 | } |
| 842 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 843 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 844 | mbedtls_cipher_free(&ctx); |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 845 | } |
| 846 | /* END_CASE */ |
| 847 | |
Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 848 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_AUTH_CRYPT */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 849 | void auth_crypt_tv(int cipher_id, data_t *key, data_t *iv, |
| 850 | data_t *ad, data_t *cipher, data_t *tag, |
| 851 | char *result, data_t *clear, int use_psa) |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 852 | { |
Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 853 | /* |
| 854 | * Take an AEAD ciphertext + tag and perform a pair |
| 855 | * of AEAD decryption and AEAD encryption. Check that |
Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 856 | * this results in the expected plaintext, and that |
Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 857 | * decryption and encryption are inverse to one another. |
| 858 | */ |
Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 859 | |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 860 | int ret; |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 861 | int using_nist_kw, using_nist_kw_padding; |
Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 862 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 863 | mbedtls_cipher_context_t ctx; |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 864 | size_t outlen; |
| 865 | |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 866 | unsigned char *cipher_plus_tag = NULL; |
| 867 | size_t cipher_plus_tag_len; |
| 868 | unsigned char *decrypt_buf = NULL; |
| 869 | size_t decrypt_buf_len = 0; |
| 870 | unsigned char *encrypt_buf = NULL; |
| 871 | size_t encrypt_buf_len = 0; |
| 872 | |
Gilles Peskine | 70edd68 | 2020-12-03 20:27:27 +0100 | [diff] [blame] | 873 | /* Null pointers are documented as valid for inputs of length 0. |
| 874 | * The test framework passes non-null pointers, so set them to NULL. |
| 875 | * key, cipher and tag can't be empty. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 876 | if (iv->len == 0) { |
Gilles Peskine | 70edd68 | 2020-12-03 20:27:27 +0100 | [diff] [blame] | 877 | iv->x = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 878 | } |
| 879 | if (ad->len == 0) { |
Gilles Peskine | 70edd68 | 2020-12-03 20:27:27 +0100 | [diff] [blame] | 880 | ad->x = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 881 | } |
| 882 | if (clear->len == 0) { |
Gilles Peskine | 70edd68 | 2020-12-03 20:27:27 +0100 | [diff] [blame] | 883 | clear->x = NULL; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 884 | } |
Gilles Peskine | 70edd68 | 2020-12-03 20:27:27 +0100 | [diff] [blame] | 885 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 886 | mbedtls_cipher_init(&ctx); |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 887 | |
Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 888 | /* Initialize PSA Crypto */ |
| 889 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 890 | if (use_psa == 1) { |
| 891 | PSA_ASSERT(psa_crypto_init()); |
| 892 | } |
Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 893 | #else |
Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 894 | (void) use_psa; |
| 895 | #endif |
| 896 | |
| 897 | /* |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 898 | * Are we using NIST_KW? with padding? |
| 899 | */ |
| 900 | using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP || |
| 901 | cipher_id == MBEDTLS_CIPHER_AES_192_KWP || |
| 902 | cipher_id == MBEDTLS_CIPHER_AES_256_KWP; |
| 903 | using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW || |
| 904 | cipher_id == MBEDTLS_CIPHER_AES_192_KW || |
| 905 | cipher_id == MBEDTLS_CIPHER_AES_256_KW || |
| 906 | using_nist_kw_padding; |
| 907 | |
| 908 | /* |
Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 909 | * Prepare context for decryption |
| 910 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 911 | if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key, |
| 912 | MBEDTLS_DECRYPT)) { |
Gilles Peskine | 8a3d234 | 2020-12-03 21:06:15 +0100 | [diff] [blame] | 913 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 914 | } |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 915 | |
Manuel Pégourié-Gonnard | 4c1a100 | 2020-11-26 10:22:50 +0100 | [diff] [blame] | 916 | /* |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 917 | * prepare buffer for decryption |
| 918 | * (we need the tag appended to the ciphertext) |
| 919 | */ |
| 920 | cipher_plus_tag_len = cipher->len + tag->len; |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 921 | TEST_CALLOC(cipher_plus_tag, cipher_plus_tag_len); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 922 | memcpy(cipher_plus_tag, cipher->x, cipher->len); |
| 923 | memcpy(cipher_plus_tag + cipher->len, tag->x, tag->len); |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 924 | |
| 925 | /* |
| 926 | * Compute length of output buffer according to the documentation |
| 927 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 928 | if (using_nist_kw) { |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 929 | decrypt_buf_len = cipher_plus_tag_len - 8; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 930 | } else { |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 931 | decrypt_buf_len = cipher_plus_tag_len - tag->len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 932 | } |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 933 | |
| 934 | |
| 935 | /* |
| 936 | * Try decrypting to a buffer that's 1B too small |
| 937 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 938 | if (decrypt_buf_len != 0) { |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 939 | TEST_CALLOC(decrypt_buf, decrypt_buf_len - 1); |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 940 | |
| 941 | outlen = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 942 | ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len, |
| 943 | ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len, |
| 944 | decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len); |
| 945 | TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA); |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 946 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 947 | mbedtls_free(decrypt_buf); |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 948 | decrypt_buf = NULL; |
| 949 | } |
| 950 | |
| 951 | /* |
| 952 | * Authenticate and decrypt, and check result |
| 953 | */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 954 | TEST_CALLOC(decrypt_buf, decrypt_buf_len); |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 955 | |
| 956 | outlen = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 957 | ret = mbedtls_cipher_auth_decrypt_ext(&ctx, iv->x, iv->len, |
| 958 | ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len, |
| 959 | decrypt_buf, decrypt_buf_len, &outlen, tag->len); |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 960 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 961 | if (strcmp(result, "FAIL") == 0) { |
| 962 | TEST_ASSERT(ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED); |
| 963 | TEST_ASSERT(buffer_is_all_zero(decrypt_buf, decrypt_buf_len)); |
| 964 | } else { |
| 965 | TEST_ASSERT(ret == 0); |
Tom Cosgrove | e4e9e7d | 2023-07-21 11:40:20 +0100 | [diff] [blame] | 966 | TEST_MEMORY_COMPARE(decrypt_buf, outlen, clear->x, clear->len); |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 967 | } |
| 968 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 969 | mbedtls_free(decrypt_buf); |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 970 | decrypt_buf = NULL; |
| 971 | |
| 972 | /* |
| 973 | * Encrypt back if test data was authentic |
| 974 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 975 | if (strcmp(result, "FAIL") != 0) { |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 976 | /* prepare context for encryption */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 977 | if (!cipher_reset_key(&ctx, cipher_id, use_psa, tag->len, key, |
| 978 | MBEDTLS_ENCRYPT)) { |
Gilles Peskine | 8a3d234 | 2020-12-03 21:06:15 +0100 | [diff] [blame] | 979 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 980 | } |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 981 | |
| 982 | /* |
| 983 | * Compute size of output buffer according to documentation |
| 984 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 985 | if (using_nist_kw) { |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 986 | encrypt_buf_len = clear->len + 8; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 987 | if (using_nist_kw_padding && encrypt_buf_len % 8 != 0) { |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 988 | encrypt_buf_len += 8 - encrypt_buf_len % 8; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 989 | } |
| 990 | } else { |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 991 | encrypt_buf_len = clear->len + tag->len; |
| 992 | } |
| 993 | |
| 994 | /* |
| 995 | * Try encrypting with an output buffer that's 1B too small |
| 996 | */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 997 | TEST_CALLOC(encrypt_buf, encrypt_buf_len - 1); |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 998 | |
| 999 | outlen = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1000 | ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len, |
| 1001 | ad->x, ad->len, clear->x, clear->len, |
| 1002 | encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len); |
| 1003 | TEST_ASSERT(ret != 0); |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1004 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1005 | mbedtls_free(encrypt_buf); |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1006 | encrypt_buf = NULL; |
| 1007 | |
| 1008 | /* |
| 1009 | * Encrypt and check the result |
| 1010 | */ |
Tom Cosgrove | 05b2a87 | 2023-07-21 11:31:13 +0100 | [diff] [blame] | 1011 | TEST_CALLOC(encrypt_buf, encrypt_buf_len); |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1012 | |
| 1013 | outlen = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1014 | ret = mbedtls_cipher_auth_encrypt_ext(&ctx, iv->x, iv->len, |
| 1015 | ad->x, ad->len, clear->x, clear->len, |
| 1016 | encrypt_buf, encrypt_buf_len, &outlen, tag->len); |
| 1017 | TEST_ASSERT(ret == 0); |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1018 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1019 | TEST_ASSERT(outlen == cipher->len + tag->len); |
| 1020 | TEST_ASSERT(memcmp(encrypt_buf, cipher->x, cipher->len) == 0); |
| 1021 | TEST_ASSERT(memcmp(encrypt_buf + cipher->len, |
| 1022 | tag->x, tag->len) == 0); |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1023 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1024 | mbedtls_free(encrypt_buf); |
Manuel Pégourié-Gonnard | 53f10e7 | 2020-11-30 10:17:01 +0100 | [diff] [blame] | 1025 | encrypt_buf = NULL; |
| 1026 | } |
| 1027 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1028 | exit: |
Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 1029 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1030 | mbedtls_cipher_free(&ctx); |
| 1031 | mbedtls_free(decrypt_buf); |
| 1032 | mbedtls_free(encrypt_buf); |
| 1033 | mbedtls_free(cipher_plus_tag); |
Gilles Peskine | 5386f6b | 2019-08-01 12:47:40 +0200 | [diff] [blame] | 1034 | |
Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 1035 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1036 | if (use_psa == 1) { |
| 1037 | PSA_DONE(); |
| 1038 | } |
Hanno Becker | a13272d | 2018-11-12 16:27:30 +0000 | [diff] [blame] | 1039 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 1040 | } |
| 1041 | /* END_CASE */ |
| 1042 | |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 1043 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1044 | void test_vec_ecb(int cipher_id, int operation, data_t *key, |
| 1045 | data_t *input, data_t *result, int finish_result |
| 1046 | ) |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1047 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1048 | mbedtls_cipher_context_t ctx; |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1049 | unsigned char output[32]; |
| 1050 | size_t outlen; |
| 1051 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1052 | mbedtls_cipher_init(&ctx); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 1053 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1054 | memset(output, 0x00, sizeof(output)); |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1055 | |
| 1056 | /* Prepare context */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1057 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, |
| 1058 | mbedtls_cipher_info_from_type(cipher_id))); |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1059 | |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1060 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1061 | 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] | 1062 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1063 | TEST_ASSERT(0 == mbedtls_cipher_update(&ctx, input->x, |
| 1064 | mbedtls_cipher_get_block_size(&ctx), |
| 1065 | output, &outlen)); |
| 1066 | TEST_ASSERT(outlen == mbedtls_cipher_get_block_size(&ctx)); |
| 1067 | TEST_ASSERT(finish_result == mbedtls_cipher_finish(&ctx, output + outlen, |
| 1068 | &outlen)); |
| 1069 | TEST_ASSERT(0 == outlen); |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1070 | |
| 1071 | /* check plaintext only if everything went fine */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1072 | if (0 == finish_result) { |
| 1073 | TEST_ASSERT(0 == memcmp(output, result->x, |
| 1074 | mbedtls_cipher_get_block_size(&ctx))); |
| 1075 | } |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1076 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1077 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1078 | mbedtls_cipher_free(&ctx); |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 1079 | } |
| 1080 | /* END_CASE */ |
| 1081 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1082 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1083 | void test_vec_crypt(int cipher_id, int operation, data_t *key, |
| 1084 | data_t *iv, data_t *input, data_t *result, |
| 1085 | int finish_result, int use_psa) |
Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1086 | { |
Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1087 | mbedtls_cipher_context_t ctx; |
| 1088 | unsigned char output[32]; |
| 1089 | size_t outlen; |
| 1090 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1091 | mbedtls_cipher_init(&ctx); |
Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1092 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1093 | memset(output, 0x00, sizeof(output)); |
Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1094 | |
| 1095 | /* Prepare context */ |
Andrzej Kurek | 8f26c8a | 2022-10-20 05:19:47 -0400 | [diff] [blame] | 1096 | #if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED) |
Hanno Becker | e43164e | 2018-11-12 12:46:35 +0000 | [diff] [blame] | 1097 | (void) use_psa; |
| 1098 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1099 | if (use_psa == 1) { |
| 1100 | PSA_ASSERT(psa_crypto_init()); |
| 1101 | TEST_ASSERT(0 == mbedtls_cipher_setup_psa(&ctx, |
| 1102 | mbedtls_cipher_info_from_type(cipher_id), 0)); |
| 1103 | } else |
Przemek Stekiel | 476d9c4 | 2022-05-19 12:26:33 +0200 | [diff] [blame] | 1104 | #endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED*/ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1105 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, |
| 1106 | mbedtls_cipher_info_from_type(cipher_id))); |
Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1107 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1108 | TEST_ASSERT(0 == mbedtls_cipher_setkey(&ctx, key->x, 8 * key->len, operation)); |
| 1109 | if (MBEDTLS_MODE_CBC == ctx.cipher_info->mode) { |
| 1110 | TEST_ASSERT(0 == mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE)); |
| 1111 | } |
Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1112 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1113 | TEST_ASSERT(finish_result == mbedtls_cipher_crypt(&ctx, iv->len ? iv->x : NULL, |
| 1114 | iv->len, input->x, input->len, |
| 1115 | output, &outlen)); |
| 1116 | TEST_ASSERT(result->len == outlen); |
Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1117 | /* check plaintext only if everything went fine */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1118 | if (0 == finish_result) { |
| 1119 | TEST_ASSERT(0 == memcmp(output, result->x, outlen)); |
| 1120 | } |
Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1121 | |
| 1122 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1123 | mbedtls_cipher_free(&ctx); |
Andrzej Kurek | 8f26c8a | 2022-10-20 05:19:47 -0400 | [diff] [blame] | 1124 | #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_TEST_DEPRECATED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1125 | PSA_DONE(); |
Andrzej Kurek | ed05279 | 2022-10-21 05:37:54 -0400 | [diff] [blame] | 1126 | #endif /* MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_TEST_DEPRECATED */ |
Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1127 | } |
| 1128 | /* END_CASE */ |
| 1129 | |
| 1130 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1131 | void set_padding(int cipher_id, int pad_mode, int ret) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1132 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1133 | const mbedtls_cipher_info_t *cipher_info; |
| 1134 | mbedtls_cipher_context_t ctx; |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 1135 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1136 | mbedtls_cipher_init(&ctx); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 1137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1138 | cipher_info = mbedtls_cipher_info_from_type(cipher_id); |
| 1139 | TEST_ASSERT(NULL != cipher_info); |
| 1140 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx, cipher_info)); |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 1141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1142 | 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] | 1143 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1144 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1145 | mbedtls_cipher_free(&ctx); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1146 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1147 | /* END_CASE */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1148 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1149 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1150 | void check_padding(int pad_mode, data_t *input, int ret, int dlen_check |
| 1151 | ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1152 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1153 | mbedtls_cipher_info_t cipher_info; |
| 1154 | mbedtls_cipher_context_t ctx; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1155 | size_t dlen; |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 1156 | |
| 1157 | /* build a fake context just for getting access to get_padding */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1158 | mbedtls_cipher_init(&ctx); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1159 | cipher_info.mode = MBEDTLS_MODE_CBC; |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 1160 | ctx.cipher_info = &cipher_info; |
| 1161 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1162 | 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] | 1163 | |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 1164 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1165 | TEST_ASSERT(ret == ctx.get_padding(input->x, input->len, &dlen)); |
| 1166 | if (0 == ret) { |
| 1167 | TEST_ASSERT(dlen == (size_t) dlen_check); |
| 1168 | } |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1169 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1170 | /* END_CASE */ |
Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1171 | |
| 1172 | /* BEGIN_CASE */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1173 | void iv_len_validity(int cipher_id, char *cipher_string, |
| 1174 | int iv_len_val, int ret) |
Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1175 | { |
| 1176 | size_t iv_len = iv_len_val; |
| 1177 | unsigned char iv[16]; |
| 1178 | |
Thomas Daubney | 3a066ec | 2022-02-17 12:01:28 +0000 | [diff] [blame] | 1179 | /* Initialise iv buffer */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1180 | memset(iv, 0, sizeof(iv)); |
Thomas Daubney | 3a066ec | 2022-02-17 12:01:28 +0000 | [diff] [blame] | 1181 | |
Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1182 | const mbedtls_cipher_info_t *cipher_info; |
| 1183 | mbedtls_cipher_context_t ctx_dec; |
| 1184 | mbedtls_cipher_context_t ctx_enc; |
| 1185 | |
| 1186 | /* |
| 1187 | * Prepare contexts |
| 1188 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1189 | mbedtls_cipher_init(&ctx_dec); |
| 1190 | mbedtls_cipher_init(&ctx_enc); |
Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1191 | |
| 1192 | /* Check and get info structures */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1193 | cipher_info = mbedtls_cipher_info_from_type(cipher_id); |
| 1194 | TEST_ASSERT(NULL != cipher_info); |
| 1195 | TEST_ASSERT(mbedtls_cipher_info_from_string(cipher_string) == cipher_info); |
| 1196 | TEST_ASSERT(strcmp(mbedtls_cipher_info_get_name(cipher_info), |
| 1197 | cipher_string) == 0); |
Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1198 | |
| 1199 | /* Initialise enc and dec contexts */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1200 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_dec, cipher_info)); |
| 1201 | TEST_ASSERT(0 == mbedtls_cipher_setup(&ctx_enc, cipher_info)); |
Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1202 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1203 | TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_dec, iv, iv_len)); |
| 1204 | TEST_ASSERT(ret == mbedtls_cipher_set_iv(&ctx_enc, iv, iv_len)); |
Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1205 | |
| 1206 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 1207 | mbedtls_cipher_free(&ctx_dec); |
| 1208 | mbedtls_cipher_free(&ctx_enc); |
Andrzej Kurek | 33ca6af | 2021-12-01 21:58:05 +0100 | [diff] [blame] | 1209 | } |
| 1210 | /* END_CASE */ |
Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1211 | |
Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 1212 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
| 1213 | void check_set_padding(int cipher_id) |
Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1214 | { |
| 1215 | mbedtls_cipher_context_t ctx; |
| 1216 | unsigned char key[16] = { 0 }; |
| 1217 | unsigned char iv[16] = { 0 }; |
| 1218 | unsigned char input[16] = { 0 }; |
| 1219 | unsigned char output[32] = { 0 }; |
| 1220 | size_t outlen = 0; |
Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1221 | const mbedtls_cipher_info_t *cipher_info; |
| 1222 | |
Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 1223 | cipher_info = mbedtls_cipher_info_from_type(cipher_id); |
| 1224 | |
| 1225 | if (cipher_info->mode != MBEDTLS_MODE_CBC) { |
| 1226 | TEST_FAIL("Cipher mode must be CBC"); |
| 1227 | } |
Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1228 | |
| 1229 | mbedtls_cipher_init(&ctx); |
| 1230 | |
Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 1231 | TEST_EQUAL(0, mbedtls_cipher_setup(&ctx, cipher_info)); |
Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1232 | |
Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 1233 | TEST_EQUAL(0, mbedtls_cipher_setkey(&ctx, key, 128, MBEDTLS_ENCRYPT)); |
Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1234 | |
Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 1235 | TEST_EQUAL(MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 1236 | mbedtls_cipher_crypt(&ctx, iv, sizeof(iv), input, |
| 1237 | sizeof(input), output, &outlen)); |
Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1238 | |
| 1239 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Waleed Elmelegy | 6d2c5d5 | 2023-09-18 17:41:25 +0100 | [diff] [blame] | 1240 | TEST_EQUAL(0, mbedtls_cipher_set_padding_mode(&ctx, MBEDTLS_PADDING_NONE)); |
| 1241 | TEST_EQUAL(0, mbedtls_cipher_crypt(&ctx, iv, sizeof(iv), input, |
| 1242 | sizeof(input), output, &outlen)); |
Waleed Elmelegy | a7d206f | 2023-09-07 17:54:46 +0100 | [diff] [blame] | 1243 | #endif |
| 1244 | |
| 1245 | exit: |
| 1246 | mbedtls_cipher_free(&ctx); |
| 1247 | } |
| 1248 | /* END_CASE */ |