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