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