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