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