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