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