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