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