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