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