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