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