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