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