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 ) ); |
Krzysztof Stachowiak | e0215d7 | 2018-12-17 10:20:30 +0100 | [diff] [blame] | 270 | TEST_INVALID_PARAM_RET( |
| 271 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 272 | mbedtls_cipher_crypt( &valid_ctx, |
| 273 | NULL, valid_size, |
| 274 | valid_buffer, valid_size, |
| 275 | valid_buffer, &size_t_var ) ); |
| 276 | TEST_INVALID_PARAM_RET( |
| 277 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 278 | mbedtls_cipher_crypt( &valid_ctx, |
| 279 | valid_buffer, valid_size, |
| 280 | NULL, 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 | valid_buffer, valid_size, |
| 287 | NULL, &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 | valid_buffer, NULL ) ); |
| 294 | |
k-stachowiak | a539070 | 2018-12-17 11:27:03 +0100 | [diff] [blame] | 295 | #if defined(MBEDTLS_CIPHER_MODE_AEAD) |
Krzysztof Stachowiak | e0215d7 | 2018-12-17 10:20:30 +0100 | [diff] [blame] | 296 | /* mbedtls_cipher_auth_encrypt() */ |
| 297 | TEST_INVALID_PARAM_RET( |
| 298 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 299 | mbedtls_cipher_auth_encrypt( NULL, |
| 300 | valid_buffer, valid_size, |
| 301 | valid_buffer, valid_size, |
| 302 | valid_buffer, valid_size, |
| 303 | valid_buffer, &size_t_var, |
| 304 | valid_buffer, valid_size ) ); |
Krzysztof Stachowiak | e0215d7 | 2018-12-17 10:20:30 +0100 | [diff] [blame] | 305 | TEST_INVALID_PARAM_RET( |
| 306 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 307 | mbedtls_cipher_auth_encrypt( &valid_ctx, |
| 308 | NULL, valid_size, |
| 309 | valid_buffer, valid_size, |
| 310 | valid_buffer, valid_size, |
| 311 | valid_buffer, &size_t_var, |
| 312 | valid_buffer, valid_size ) ); |
| 313 | TEST_INVALID_PARAM_RET( |
| 314 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 315 | mbedtls_cipher_auth_encrypt( &valid_ctx, |
| 316 | valid_buffer, valid_size, |
| 317 | NULL, valid_size, |
| 318 | valid_buffer, valid_size, |
| 319 | valid_buffer, &size_t_var, |
| 320 | valid_buffer, valid_size ) ); |
| 321 | TEST_INVALID_PARAM_RET( |
| 322 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 323 | mbedtls_cipher_auth_encrypt( &valid_ctx, |
| 324 | valid_buffer, valid_size, |
| 325 | valid_buffer, valid_size, |
| 326 | NULL, valid_size, |
| 327 | valid_buffer, &size_t_var, |
| 328 | valid_buffer, valid_size ) ); |
| 329 | TEST_INVALID_PARAM_RET( |
| 330 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 331 | mbedtls_cipher_auth_encrypt( &valid_ctx, |
| 332 | valid_buffer, valid_size, |
| 333 | valid_buffer, valid_size, |
| 334 | valid_buffer, valid_size, |
| 335 | NULL, &size_t_var, |
| 336 | valid_buffer, valid_size ) ); |
| 337 | TEST_INVALID_PARAM_RET( |
| 338 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 339 | mbedtls_cipher_auth_encrypt( &valid_ctx, |
| 340 | valid_buffer, valid_size, |
| 341 | valid_buffer, valid_size, |
| 342 | valid_buffer, valid_size, |
| 343 | valid_buffer, NULL, |
| 344 | valid_buffer, valid_size ) ); |
| 345 | TEST_INVALID_PARAM_RET( |
| 346 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 347 | mbedtls_cipher_auth_encrypt( &valid_ctx, |
| 348 | valid_buffer, valid_size, |
| 349 | valid_buffer, valid_size, |
| 350 | valid_buffer, valid_size, |
| 351 | valid_buffer, &size_t_var, |
| 352 | NULL, valid_size ) ); |
| 353 | |
| 354 | /* mbedtls_cipher_auth_decrypt() */ |
| 355 | TEST_INVALID_PARAM_RET( |
| 356 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 357 | mbedtls_cipher_auth_decrypt( NULL, |
| 358 | valid_buffer, valid_size, |
| 359 | valid_buffer, valid_size, |
| 360 | valid_buffer, valid_size, |
| 361 | valid_buffer, &size_t_var, |
| 362 | valid_buffer, valid_size ) ); |
Krzysztof Stachowiak | e0215d7 | 2018-12-17 10:20:30 +0100 | [diff] [blame] | 363 | TEST_INVALID_PARAM_RET( |
| 364 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 365 | mbedtls_cipher_auth_decrypt( &valid_ctx, |
| 366 | NULL, valid_size, |
| 367 | valid_buffer, valid_size, |
| 368 | valid_buffer, valid_size, |
| 369 | valid_buffer, &size_t_var, |
| 370 | valid_buffer, valid_size ) ); |
| 371 | TEST_INVALID_PARAM_RET( |
| 372 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 373 | mbedtls_cipher_auth_decrypt( &valid_ctx, |
| 374 | valid_buffer, valid_size, |
| 375 | NULL, 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_decrypt( &valid_ctx, |
| 382 | valid_buffer, valid_size, |
| 383 | valid_buffer, valid_size, |
| 384 | NULL, 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_decrypt( &valid_ctx, |
| 390 | valid_buffer, valid_size, |
| 391 | valid_buffer, valid_size, |
| 392 | valid_buffer, valid_size, |
| 393 | NULL, &size_t_var, |
| 394 | valid_buffer, valid_size ) ); |
| 395 | TEST_INVALID_PARAM_RET( |
| 396 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 397 | mbedtls_cipher_auth_decrypt( &valid_ctx, |
| 398 | valid_buffer, valid_size, |
| 399 | valid_buffer, valid_size, |
| 400 | valid_buffer, valid_size, |
| 401 | valid_buffer, NULL, |
| 402 | valid_buffer, valid_size ) ); |
| 403 | TEST_INVALID_PARAM_RET( |
| 404 | MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, |
| 405 | mbedtls_cipher_auth_decrypt( &valid_ctx, |
| 406 | valid_buffer, valid_size, |
| 407 | valid_buffer, valid_size, |
| 408 | valid_buffer, valid_size, |
| 409 | valid_buffer, &size_t_var, |
| 410 | NULL, valid_size ) ); |
k-stachowiak | a539070 | 2018-12-17 11:27:03 +0100 | [diff] [blame] | 411 | #endif /* defined(MBEDTLS_CIPHER_MODE_AEAD) */ |
Krzysztof Stachowiak | e0215d7 | 2018-12-17 10:20:30 +0100 | [diff] [blame] | 412 | |
| 413 | /* mbedtls_cipher_free() */ |
| 414 | TEST_VALID_PARAM( mbedtls_cipher_free( NULL ) ); |
| 415 | exit: |
| 416 | TEST_VALID_PARAM( mbedtls_cipher_free( &invalid_ctx ) ); |
| 417 | TEST_VALID_PARAM( mbedtls_cipher_free( &valid_ctx ) ); |
| 418 | } |
| 419 | /* END_CASE */ |
| 420 | |
Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 421 | /* BEGIN_CASE depends_on:MBEDTLS_AES_C */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 422 | void cipher_special_behaviours( ) |
Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 423 | { |
| 424 | const mbedtls_cipher_info_t *cipher_info; |
| 425 | mbedtls_cipher_context_t ctx; |
| 426 | unsigned char input[32]; |
| 427 | unsigned char output[32]; |
Ron Eldor | 6f90ed8 | 2017-09-26 12:08:54 +0300 | [diff] [blame] | 428 | #if defined (MBEDTLS_CIPHER_MODE_CBC) |
Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 429 | unsigned char iv[32]; |
Ron Eldor | 6f90ed8 | 2017-09-26 12:08:54 +0300 | [diff] [blame] | 430 | #endif |
Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 431 | size_t olen = 0; |
| 432 | |
| 433 | mbedtls_cipher_init( &ctx ); |
| 434 | memset( input, 0, sizeof( input ) ); |
| 435 | memset( output, 0, sizeof( output ) ); |
Ron Eldor | bb4bbbb | 2017-10-01 17:04:54 +0300 | [diff] [blame] | 436 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 437 | memset( iv, 0, sizeof( iv ) ); |
| 438 | |
| 439 | /* Check and get info structures */ |
Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 440 | cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC ); |
Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 441 | TEST_ASSERT( NULL != cipher_info ); |
| 442 | |
| 443 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) ); |
| 444 | |
| 445 | /* IV too big */ |
| 446 | TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 ) |
| 447 | == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE ); |
| 448 | |
| 449 | /* IV too small */ |
| 450 | TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 ) |
| 451 | == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA ); |
| 452 | |
Ron Eldor | 4e64e0b | 2017-09-25 18:22:32 +0300 | [diff] [blame] | 453 | mbedtls_cipher_free( &ctx ); |
Ron Eldor | bb4bbbb | 2017-10-01 17:04:54 +0300 | [diff] [blame] | 454 | mbedtls_cipher_init( &ctx ); |
Ron Eldor | 6f90ed8 | 2017-09-26 12:08:54 +0300 | [diff] [blame] | 455 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Ron Eldor | 4e64e0b | 2017-09-25 18:22:32 +0300 | [diff] [blame] | 456 | cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB ); |
Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 457 | TEST_ASSERT( NULL != cipher_info ); |
| 458 | |
| 459 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) ); |
| 460 | |
Paul Bakker | 6a9c725 | 2016-07-14 13:46:10 +0100 | [diff] [blame] | 461 | /* Update ECB with partial block */ |
| 462 | TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen ) |
| 463 | == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED ); |
| 464 | |
| 465 | exit: |
| 466 | mbedtls_cipher_free( &ctx ); |
| 467 | } |
| 468 | /* END_CASE */ |
| 469 | |
Manuel Pégourié-Gonnard | 5e7693f | 2014-06-13 16:08:07 +0200 | [diff] [blame] | 470 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 471 | 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] | 472 | int length_val, int pad_mode ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 473 | { |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 474 | size_t length = length_val, outlen, total_len, i, block_size; |
Jaeden Amero | d906b81 | 2018-06-08 11:03:16 +0100 | [diff] [blame] | 475 | unsigned char key[64]; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 476 | unsigned char iv[16]; |
Manuel Pégourié-Gonnard | 9241be7 | 2013-08-31 17:31:03 +0200 | [diff] [blame] | 477 | unsigned char ad[13]; |
| 478 | unsigned char tag[16]; |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 479 | unsigned char inbuf[64]; |
| 480 | unsigned char encbuf[64]; |
| 481 | unsigned char decbuf[64]; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 482 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 483 | const mbedtls_cipher_info_t *cipher_info; |
| 484 | mbedtls_cipher_context_t ctx_dec; |
| 485 | mbedtls_cipher_context_t ctx_enc; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 486 | |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 487 | /* |
| 488 | * Prepare contexts |
| 489 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 490 | mbedtls_cipher_init( &ctx_dec ); |
| 491 | mbedtls_cipher_init( &ctx_enc ); |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 492 | |
| 493 | memset( key, 0x2a, sizeof( key ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 494 | |
| 495 | /* Check and get info structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 496 | cipher_info = mbedtls_cipher_info_from_type( cipher_id ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 497 | TEST_ASSERT( NULL != cipher_info ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 498 | TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 499 | |
| 500 | /* Initialise enc and dec contexts */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 501 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) ); |
| 502 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) ); |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 503 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 504 | TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) ); |
| 505 | 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] | 506 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 507 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 508 | if( -1 != pad_mode ) |
Manuel Pégourié-Gonnard | 6c97899 | 2013-07-26 13:20:42 +0200 | [diff] [blame] | 509 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 510 | TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) ); |
| 511 | 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] | 512 | } |
Manuel Pégourié-Gonnard | 989ed38 | 2013-09-13 14:41:45 +0200 | [diff] [blame] | 513 | #else |
| 514 | (void) pad_mode; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 515 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
Manuel Pégourié-Gonnard | 6c97899 | 2013-07-26 13:20:42 +0200 | [diff] [blame] | 516 | |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 517 | /* |
| 518 | * Do a few encode/decode cycles |
| 519 | */ |
| 520 | for( i = 0; i < 3; i++ ) |
| 521 | { |
| 522 | memset( iv , 0x00 + i, sizeof( iv ) ); |
| 523 | memset( ad, 0x10 + i, sizeof( ad ) ); |
| 524 | memset( inbuf, 0x20 + i, sizeof( inbuf ) ); |
| 525 | |
| 526 | memset( encbuf, 0, sizeof( encbuf ) ); |
| 527 | memset( decbuf, 0, sizeof( decbuf ) ); |
| 528 | memset( tag, 0, sizeof( tag ) ); |
| 529 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 530 | TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) ); |
| 531 | 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] | 532 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 533 | TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) ); |
| 534 | TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) ); |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 535 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 536 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 537 | TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) ); |
| 538 | 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] | 539 | #endif |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 540 | |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 541 | block_size = mbedtls_cipher_get_block_size( &ctx_enc ); |
| 542 | TEST_ASSERT( block_size != 0 ); |
| 543 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 544 | /* encode length number of bytes from inbuf */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 545 | 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] | 546 | total_len = outlen; |
| 547 | |
| 548 | TEST_ASSERT( total_len == length || |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 549 | ( total_len % block_size == 0 && |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 550 | total_len < length && |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 551 | total_len + block_size > length ) ); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 552 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 553 | 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] | 554 | total_len += outlen; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 555 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 556 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 557 | 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] | 558 | #endif |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 559 | |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 560 | TEST_ASSERT( total_len == length || |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 561 | ( total_len % block_size == 0 && |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 562 | total_len > length && |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 563 | total_len <= length + block_size ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 564 | |
| 565 | /* decode the previously encoded string */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 566 | 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] | 567 | total_len = outlen; |
| 568 | |
| 569 | TEST_ASSERT( total_len == length || |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 570 | ( total_len % block_size == 0 && |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 571 | total_len < length && |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 572 | total_len + block_size >= length ) ); |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 573 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 574 | 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] | 575 | total_len += outlen; |
Paul Bakker | 343a870 | 2011-06-09 14:27:58 +0000 | [diff] [blame] | 576 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 577 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 578 | 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] | 579 | #endif |
Manuel Pégourié-Gonnard | aa9ffc5 | 2013-09-03 16:19:22 +0200 | [diff] [blame] | 580 | |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 581 | /* check result */ |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 582 | TEST_ASSERT( total_len == length ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 583 | TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 584 | } |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 585 | |
Manuel Pégourié-Gonnard | 1af50a2 | 2013-09-05 10:30:32 +0200 | [diff] [blame] | 586 | /* |
| 587 | * Done |
| 588 | */ |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 589 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 590 | mbedtls_cipher_free( &ctx_dec ); |
| 591 | mbedtls_cipher_free( &ctx_enc ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 592 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 593 | /* END_CASE */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 594 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 595 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 596 | void enc_fail( int cipher_id, int pad_mode, int key_len, int length_val, |
| 597 | int ret ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 598 | { |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 599 | size_t length = length_val; |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 600 | unsigned char key[32]; |
| 601 | unsigned char iv[16]; |
| 602 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 603 | const mbedtls_cipher_info_t *cipher_info; |
| 604 | mbedtls_cipher_context_t ctx; |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 605 | |
| 606 | unsigned char inbuf[64]; |
| 607 | unsigned char encbuf[64]; |
| 608 | |
| 609 | size_t outlen = 0; |
| 610 | |
| 611 | memset( key, 0, 32 ); |
| 612 | memset( iv , 0, 16 ); |
| 613 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 614 | mbedtls_cipher_init( &ctx ); |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 615 | |
| 616 | memset( inbuf, 5, 64 ); |
| 617 | memset( encbuf, 0, 64 ); |
| 618 | |
| 619 | /* Check and get info structures */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 620 | cipher_info = mbedtls_cipher_info_from_type( cipher_id ); |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 621 | TEST_ASSERT( NULL != cipher_info ); |
| 622 | |
| 623 | /* Initialise context */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 624 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 625 | TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) ); |
| 626 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
| 627 | 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] | 628 | #else |
| 629 | (void) pad_mode; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 630 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
| 631 | TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) ); |
| 632 | TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) ); |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 633 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 634 | TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) ); |
Manuel Pégourié-Gonnard | 8f62563 | 2014-06-24 15:26:28 +0200 | [diff] [blame] | 635 | #endif |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 636 | |
| 637 | /* encode length number of bytes from inbuf */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 638 | TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) ); |
| 639 | TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) ); |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 640 | |
| 641 | /* done */ |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 642 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 643 | mbedtls_cipher_free( &ctx ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 644 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 645 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ebdc413 | 2013-07-26 16:50:44 +0200 | [diff] [blame] | 646 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 647 | /* BEGIN_CASE */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 648 | void dec_empty_buf( ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 649 | { |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 650 | unsigned char key[32]; |
| 651 | unsigned char iv[16]; |
| 652 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 653 | mbedtls_cipher_context_t ctx_dec; |
| 654 | const mbedtls_cipher_info_t *cipher_info; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 655 | |
| 656 | unsigned char encbuf[64]; |
| 657 | unsigned char decbuf[64]; |
| 658 | |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 659 | size_t outlen = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 660 | |
| 661 | memset( key, 0, 32 ); |
| 662 | memset( iv , 0, 16 ); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 663 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 664 | mbedtls_cipher_init( &ctx_dec ); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 665 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 666 | memset( encbuf, 0, 64 ); |
| 667 | memset( decbuf, 0, 64 ); |
| 668 | |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 669 | /* Initialise context */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 670 | cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 671 | TEST_ASSERT( NULL != cipher_info); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 672 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 673 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 674 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 675 | TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, 128, MBEDTLS_DECRYPT ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 676 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 677 | 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] | 678 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 679 | TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) ); |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 680 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 681 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 682 | 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] | 683 | #endif |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 684 | |
| 685 | /* decode 0-byte string */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 686 | TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 687 | TEST_ASSERT( 0 == outlen ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 688 | 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] | 689 | &ctx_dec, decbuf + outlen, &outlen ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 690 | TEST_ASSERT( 0 == outlen ); |
| 691 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 692 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 693 | mbedtls_cipher_free( &ctx_dec ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 694 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 695 | /* END_CASE */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 696 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 697 | /* BEGIN_CASE */ |
| 698 | 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] | 699 | int second_length_val, int pad_mode, |
| 700 | int first_encrypt_output_len, int second_encrypt_output_len, |
| 701 | int first_decrypt_output_len, int second_decrypt_output_len ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 702 | { |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 703 | size_t first_length = first_length_val; |
| 704 | size_t second_length = second_length_val; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 705 | size_t length = first_length + second_length; |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 706 | size_t block_size; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 707 | unsigned char key[32]; |
| 708 | unsigned char iv[16]; |
| 709 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 710 | mbedtls_cipher_context_t ctx_dec; |
| 711 | mbedtls_cipher_context_t ctx_enc; |
| 712 | const mbedtls_cipher_info_t *cipher_info; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 713 | |
| 714 | unsigned char inbuf[64]; |
| 715 | unsigned char encbuf[64]; |
| 716 | unsigned char decbuf[64]; |
| 717 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 718 | size_t outlen = 0; |
| 719 | size_t totaloutlen = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 720 | |
| 721 | memset( key, 0, 32 ); |
| 722 | memset( iv , 0, 16 ); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 723 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 724 | mbedtls_cipher_init( &ctx_dec ); |
| 725 | mbedtls_cipher_init( &ctx_enc ); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 726 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 727 | memset( inbuf, 5, 64 ); |
| 728 | memset( encbuf, 0, 64 ); |
| 729 | memset( decbuf, 0, 64 ); |
| 730 | |
| 731 | /* Initialise enc and dec contexts */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 732 | cipher_info = mbedtls_cipher_info_from_type( cipher_id ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 733 | TEST_ASSERT( NULL != cipher_info); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 734 | |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 735 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) ); |
| 736 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) ); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 737 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 738 | TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) ); |
| 739 | 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] | 740 | |
Jethro Beekman | 6c563fa | 2018-03-27 19:16:17 -0700 | [diff] [blame] | 741 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
| 742 | if( -1 != pad_mode ) |
| 743 | { |
| 744 | TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) ); |
| 745 | TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) ); |
| 746 | } |
| 747 | #else |
| 748 | (void) pad_mode; |
| 749 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
| 750 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 751 | TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) ); |
| 752 | 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] | 753 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 754 | TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) ); |
| 755 | TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) ); |
Manuel Pégourié-Gonnard | 2adc40c | 2013-09-03 13:54:12 +0200 | [diff] [blame] | 756 | |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 757 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 758 | TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) ); |
| 759 | 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] | 760 | #endif |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 761 | |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 762 | block_size = mbedtls_cipher_get_block_size( &ctx_enc ); |
| 763 | TEST_ASSERT( block_size != 0 ); |
| 764 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 765 | /* encode length number of bytes from inbuf */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 766 | 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] | 767 | TEST_ASSERT( (size_t)first_encrypt_output_len == outlen ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 768 | totaloutlen = outlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 769 | TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); |
Jethro Beekman | 6c563fa | 2018-03-27 19:16:17 -0700 | [diff] [blame] | 770 | TEST_ASSERT( (size_t)second_encrypt_output_len == outlen ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 771 | totaloutlen += outlen; |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 772 | TEST_ASSERT( totaloutlen == length || |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 773 | ( totaloutlen % block_size == 0 && |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 774 | totaloutlen < length && |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 775 | totaloutlen + block_size > length ) ); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 776 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 777 | TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 778 | totaloutlen += outlen; |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 779 | TEST_ASSERT( totaloutlen == length || |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 780 | ( totaloutlen % block_size == 0 && |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 781 | totaloutlen > length && |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 782 | totaloutlen <= length + block_size ) ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 783 | |
| 784 | /* decode the previously encoded string */ |
Jethro Beekman | 6c563fa | 2018-03-27 19:16:17 -0700 | [diff] [blame] | 785 | second_length = totaloutlen - first_length; |
| 786 | TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) ); |
| 787 | TEST_ASSERT( (size_t)first_decrypt_output_len == outlen ); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 788 | totaloutlen = outlen; |
Jethro Beekman | 6c563fa | 2018-03-27 19:16:17 -0700 | [diff] [blame] | 789 | TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) ); |
| 790 | TEST_ASSERT( (size_t)second_decrypt_output_len == outlen ); |
| 791 | totaloutlen += outlen; |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 792 | |
| 793 | TEST_ASSERT( totaloutlen == length || |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 794 | ( totaloutlen % block_size == 0 && |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 795 | totaloutlen < length && |
Manuel Pégourié-Gonnard | ac5361f | 2015-06-24 01:08:09 +0200 | [diff] [blame] | 796 | totaloutlen + block_size >= length ) ); |
Manuel Pégourié-Gonnard | 725680f | 2013-07-25 15:26:54 +0200 | [diff] [blame] | 797 | |
Jethro Beekman | 6c563fa | 2018-03-27 19:16:17 -0700 | [diff] [blame] | 798 | 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] | 799 | totaloutlen += outlen; |
| 800 | |
| 801 | TEST_ASSERT( totaloutlen == length ); |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 802 | |
| 803 | TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); |
| 804 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 805 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 806 | mbedtls_cipher_free( &ctx_dec ); |
| 807 | mbedtls_cipher_free( &ctx_enc ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 808 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 809 | /* END_CASE */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 810 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 811 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 812 | void decrypt_test_vec( int cipher_id, int pad_mode, data_t * key, |
| 813 | data_t * iv, data_t * cipher, |
| 814 | data_t * clear, data_t * ad, data_t * tag, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 815 | int finish_result, int tag_result ) |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 816 | { |
Manuel Pégourié-Gonnard | 234e1ce | 2018-05-10 12:54:32 +0200 | [diff] [blame] | 817 | unsigned char output[265]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 818 | mbedtls_cipher_context_t ctx; |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 819 | size_t outlen, total_len; |
| 820 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 821 | mbedtls_cipher_init( &ctx ); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 822 | |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 823 | memset( output, 0x00, sizeof( output ) ); |
| 824 | |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 825 | #if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C) |
Mohammad Azim Khan | cf32c45 | 2017-06-13 14:55:58 +0100 | [diff] [blame] | 826 | ((void) ad); |
| 827 | ((void) tag); |
Manuel Pégourié-Gonnard | a7496f0 | 2013-09-20 11:29:59 +0200 | [diff] [blame] | 828 | #endif |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 829 | |
| 830 | /* Prepare context */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 831 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 832 | mbedtls_cipher_info_from_type( cipher_id ) ) ); |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 833 | 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] | 834 | #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING) |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 835 | if( pad_mode != -1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 836 | 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] | 837 | #else |
| 838 | (void) pad_mode; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 839 | #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 840 | 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] | 841 | TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) ); |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 842 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 843 | 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] | 844 | #endif |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 845 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 846 | /* decode buffer and check tag->x */ |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 847 | total_len = 0; |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 848 | 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] | 849 | total_len += outlen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 850 | TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen, |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 851 | &outlen ) ); |
| 852 | total_len += outlen; |
Manuel Pégourié-Gonnard | dca3a5d | 2018-05-07 10:43:27 +0200 | [diff] [blame] | 853 | #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 854 | 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] | 855 | #endif |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 856 | |
| 857 | /* check plaintext only if everything went fine */ |
| 858 | if( 0 == finish_result && 0 == tag_result ) |
| 859 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 860 | TEST_ASSERT( total_len == clear->len ); |
| 861 | TEST_ASSERT( 0 == memcmp( output, clear->x, clear->len ) ); |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 862 | } |
| 863 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 864 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 865 | mbedtls_cipher_free( &ctx ); |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 866 | } |
| 867 | /* END_CASE */ |
| 868 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 869 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 870 | void auth_crypt_tv( int cipher_id, data_t * key, data_t * iv, |
| 871 | data_t * ad, data_t * cipher, data_t * tag, |
| 872 | char * result, data_t * clear ) |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 873 | { |
| 874 | int ret; |
Manuel Pégourié-Gonnard | 69767d1 | 2018-05-09 12:25:18 +0200 | [diff] [blame] | 875 | unsigned char output[267]; /* above + 2 (overwrite check) */ |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 876 | unsigned char my_tag[20]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 877 | mbedtls_cipher_context_t ctx; |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 878 | size_t outlen; |
| 879 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 880 | mbedtls_cipher_init( &ctx ); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 881 | |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 882 | memset( output, 0xFF, sizeof( output ) ); |
Azim Khan | 46c9b1f | 2017-05-31 20:46:35 +0100 | [diff] [blame] | 883 | memset( my_tag, 0xFF, sizeof( my_tag ) ); |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 884 | |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 885 | |
| 886 | /* Prepare context */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 887 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 888 | mbedtls_cipher_info_from_type( cipher_id ) ) ); |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 889 | 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] | 890 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 891 | /* decode buffer and check tag->x */ |
| 892 | ret = mbedtls_cipher_auth_decrypt( &ctx, iv->x, iv->len, ad->x, ad->len, |
| 893 | cipher->x, cipher->len, output, &outlen, |
| 894 | tag->x, tag->len ); |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 895 | |
| 896 | /* make sure we didn't overwrite */ |
| 897 | TEST_ASSERT( output[outlen + 0] == 0xFF ); |
| 898 | TEST_ASSERT( output[outlen + 1] == 0xFF ); |
| 899 | |
| 900 | /* make sure the message is rejected if it should be */ |
Azim Khan | 46c9b1f | 2017-05-31 20:46:35 +0100 | [diff] [blame] | 901 | if( strcmp( result, "FAIL" ) == 0 ) |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 902 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 903 | TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED ); |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 904 | goto exit; |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | /* otherwise, make sure it was decrypted properly */ |
| 908 | TEST_ASSERT( ret == 0 ); |
| 909 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 910 | TEST_ASSERT( outlen == clear->len ); |
| 911 | TEST_ASSERT( memcmp( output, clear->x, clear->len ) == 0 ); |
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 | /* 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] | 914 | memset( output, 0xFF, sizeof( output ) ); |
| 915 | outlen = 0; |
| 916 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 917 | ret = mbedtls_cipher_auth_encrypt( &ctx, iv->x, iv->len, ad->x, ad->len, |
| 918 | clear->x, clear->len, output, &outlen, |
| 919 | my_tag, tag->len ); |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 920 | TEST_ASSERT( ret == 0 ); |
| 921 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 922 | TEST_ASSERT( outlen == clear->len ); |
| 923 | TEST_ASSERT( memcmp( output, cipher->x, clear->len ) == 0 ); |
| 924 | TEST_ASSERT( memcmp( my_tag, tag->x, tag->len ) == 0 ); |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 925 | |
| 926 | /* make sure we didn't overwrite */ |
| 927 | TEST_ASSERT( output[outlen + 0] == 0xFF ); |
| 928 | TEST_ASSERT( output[outlen + 1] == 0xFF ); |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 929 | TEST_ASSERT( my_tag[tag->len + 0] == 0xFF ); |
| 930 | TEST_ASSERT( my_tag[tag->len + 1] == 0xFF ); |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 931 | |
| 932 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 933 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 934 | mbedtls_cipher_free( &ctx ); |
Manuel Pégourié-Gonnard | 542eac5 | 2014-05-15 16:03:07 +0200 | [diff] [blame] | 935 | } |
| 936 | /* END_CASE */ |
| 937 | |
Manuel Pégourié-Gonnard | 8eccab5 | 2013-09-03 18:31:25 +0200 | [diff] [blame] | 938 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 939 | void test_vec_ecb( int cipher_id, int operation, data_t * key, |
| 940 | data_t * input, data_t * result, int finish_result |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 941 | ) |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 942 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 943 | mbedtls_cipher_context_t ctx; |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 944 | unsigned char output[32]; |
| 945 | size_t outlen; |
| 946 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 947 | mbedtls_cipher_init( &ctx ); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 948 | |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 949 | memset( output, 0x00, sizeof( output ) ); |
| 950 | |
| 951 | /* Prepare context */ |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 952 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 953 | mbedtls_cipher_info_from_type( cipher_id ) ) ); |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 954 | |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 955 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 956 | 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] | 957 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 958 | TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input->x, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 959 | mbedtls_cipher_get_block_size( &ctx ), |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 960 | output, &outlen ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 961 | TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) ); |
| 962 | TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen, |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 963 | &outlen ) ); |
| 964 | TEST_ASSERT( 0 == outlen ); |
| 965 | |
| 966 | /* check plaintext only if everything went fine */ |
| 967 | if( 0 == finish_result ) |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 968 | TEST_ASSERT( 0 == memcmp( output, result->x, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 969 | mbedtls_cipher_get_block_size( &ctx ) ) ); |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 970 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 971 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 972 | mbedtls_cipher_free( &ctx ); |
Paul Bakker | 5e0efa7 | 2013-09-08 23:04:04 +0200 | [diff] [blame] | 973 | } |
| 974 | /* END_CASE */ |
| 975 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 976 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 977 | void test_vec_crypt( int cipher_id, int operation, char *hex_key, |
| 978 | char *hex_iv, char *hex_input, char *hex_result, |
| 979 | int finish_result ) |
| 980 | { |
| 981 | unsigned char key[50]; |
| 982 | unsigned char input[16]; |
| 983 | unsigned char result[16]; |
| 984 | unsigned char iv[16]; |
| 985 | size_t key_len, iv_len, inputlen, resultlen; |
| 986 | mbedtls_cipher_context_t ctx; |
| 987 | unsigned char output[32]; |
| 988 | size_t outlen; |
| 989 | |
| 990 | mbedtls_cipher_init( &ctx ); |
| 991 | |
| 992 | memset( key, 0x00, sizeof( key ) ); |
| 993 | memset( input, 0x00, sizeof( input ) ); |
| 994 | memset( result, 0x00, sizeof( result ) ); |
| 995 | memset( output, 0x00, sizeof( output ) ); |
| 996 | memset( iv, 0x00, sizeof( iv ) ); |
| 997 | |
| 998 | /* Prepare context */ |
| 999 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, |
| 1000 | mbedtls_cipher_info_from_type( cipher_id ) ) ); |
| 1001 | |
| 1002 | key_len = unhexify( key, hex_key ); |
| 1003 | inputlen = unhexify( input, hex_input ); |
| 1004 | resultlen = unhexify( result, hex_result ); |
| 1005 | |
| 1006 | TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, operation ) ); |
| 1007 | if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode ) |
| 1008 | TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) ); |
| 1009 | |
Ron Eldor | 4e64e0b | 2017-09-25 18:22:32 +0300 | [diff] [blame] | 1010 | iv_len = unhexify( iv, hex_iv ); |
Ron Eldor | 7b01244 | 2017-09-25 17:03:12 +0300 | [diff] [blame] | 1011 | |
| 1012 | TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv_len ? iv : NULL, |
| 1013 | iv_len, input, inputlen, |
| 1014 | output, &outlen ) ); |
| 1015 | TEST_ASSERT( resultlen == outlen ); |
| 1016 | /* check plaintext only if everything went fine */ |
| 1017 | if( 0 == finish_result ) |
| 1018 | TEST_ASSERT( 0 == memcmp( output, result, outlen ) ); |
| 1019 | |
| 1020 | exit: |
| 1021 | mbedtls_cipher_free( &ctx ); |
| 1022 | } |
| 1023 | /* END_CASE */ |
| 1024 | |
| 1025 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1026 | void set_padding( int cipher_id, int pad_mode, int ret ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1027 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1028 | const mbedtls_cipher_info_t *cipher_info; |
| 1029 | mbedtls_cipher_context_t ctx; |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 1030 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1031 | mbedtls_cipher_init( &ctx ); |
Paul Bakker | d2a2d61 | 2014-07-01 15:45:49 +0200 | [diff] [blame] | 1032 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1033 | cipher_info = mbedtls_cipher_info_from_type( cipher_id ); |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 1034 | TEST_ASSERT( NULL != cipher_info ); |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 1035 | TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) ); |
Manuel Pégourié-Gonnard | d5fdcaf | 2013-07-24 18:05:00 +0200 | [diff] [blame] | 1036 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1037 | 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] | 1038 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 1039 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1040 | mbedtls_cipher_free( &ctx ); |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1041 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1042 | /* END_CASE */ |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1043 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1044 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 1045 | 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] | 1046 | ) |
Paul Bakker | dbd443d | 2013-08-16 13:38:47 +0200 | [diff] [blame] | 1047 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1048 | mbedtls_cipher_info_t cipher_info; |
| 1049 | mbedtls_cipher_context_t ctx; |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 1050 | size_t dlen; |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 1051 | |
| 1052 | /* 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] | 1053 | mbedtls_cipher_init( &ctx ); |
| 1054 | cipher_info.mode = MBEDTLS_MODE_CBC; |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 1055 | ctx.cipher_info = &cipher_info; |
| 1056 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1057 | 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] | 1058 | |
Manuel Pégourié-Gonnard | a640849 | 2013-07-26 10:55:02 +0200 | [diff] [blame] | 1059 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 1060 | TEST_ASSERT( ret == ctx.get_padding( input->x, input->len, &dlen ) ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1061 | if( 0 == ret ) |
| 1062 | TEST_ASSERT( dlen == (size_t) dlen_check ); |
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 */ |