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