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