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