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