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