Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2 | #include <stdint.h> |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 3 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/asn1.h" |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 5 | #include "mbedtls/asn1write.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 6 | #include "mbedtls/oid.h" |
| 7 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 8 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() |
| 9 | * uses mbedtls_ctr_drbg internally. */ |
| 10 | #include "mbedtls/ctr_drbg.h" |
| 11 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 12 | #include "psa/crypto.h" |
Ronald Cron | 4184107 | 2020-09-17 15:28:26 +0200 | [diff] [blame] | 13 | #include "psa_crypto_slot_management.h" |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 14 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 15 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 16 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 17 | #include "test/psa_exercise_key.h" |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 18 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 19 | #include "test/drivers/test_driver.h" |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 20 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION |
| 21 | #else |
| 22 | #define TEST_DRIVER_LOCATION 0x7fffff |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 23 | #endif |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 24 | #include "mbedtls/legacy_or_psa.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 25 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 26 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 27 | #define UNUSED 0xdeadbeef |
| 28 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 29 | /* Assert that an operation is (not) active. |
| 30 | * This serves as a proxy for checking if the operation is aborted. */ |
| 31 | #define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 ) |
| 32 | #define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 33 | |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 34 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 35 | int ecjpake_operation_setup( psa_pake_operation_t *operation, |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 36 | psa_pake_cipher_suite_t *cipher_suite, |
| 37 | psa_pake_role_t role, |
| 38 | mbedtls_svc_key_id_t key, |
| 39 | size_t key_available ) |
| 40 | { |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 41 | PSA_ASSERT( psa_pake_abort( operation ) ); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 42 | |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 43 | PSA_ASSERT( psa_pake_setup( operation, cipher_suite ) ); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 44 | |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 45 | PSA_ASSERT( psa_pake_set_role( operation, role) ); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 46 | |
| 47 | if( key_available ) |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 48 | PSA_ASSERT( psa_pake_set_password_key( operation, key ) ); |
| 49 | return 0; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 50 | exit: |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 51 | return 1; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 52 | } |
| 53 | #endif |
| 54 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 55 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 56 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 57 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 58 | /** Test if a buffer contains a constant byte value. |
| 59 | * |
| 60 | * `mem_is_char(buffer, c, size)` is true after `memset(buffer, c, size)`. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 61 | * |
| 62 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 63 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 64 | * \param size Size of the buffer in bytes. |
| 65 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 66 | * \return 1 if the buffer is all-bits-zero. |
| 67 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 68 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 69 | static int mem_is_char( void *buffer, unsigned char c, size_t size ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 70 | { |
| 71 | size_t i; |
| 72 | for( i = 0; i < size; i++ ) |
| 73 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 74 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 75 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 76 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 77 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 78 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 79 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 80 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 81 | static int asn1_write_10x( unsigned char **p, |
| 82 | unsigned char *start, |
| 83 | size_t bits, |
| 84 | unsigned char x ) |
| 85 | { |
| 86 | int ret; |
| 87 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 88 | if( bits == 0 ) |
| 89 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 90 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 91 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 92 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 93 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 94 | *p -= len; |
| 95 | ( *p )[len-1] = x; |
| 96 | if( bits % 8 == 0 ) |
| 97 | ( *p )[1] |= 1; |
| 98 | else |
| 99 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 100 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 101 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 102 | MBEDTLS_ASN1_INTEGER ) ); |
| 103 | return( len ); |
| 104 | } |
| 105 | |
| 106 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 107 | size_t buffer_size, |
| 108 | unsigned char **p, |
| 109 | size_t bits, |
| 110 | int keypair ) |
| 111 | { |
| 112 | size_t half_bits = ( bits + 1 ) / 2; |
| 113 | int ret; |
| 114 | int len = 0; |
| 115 | /* Construct something that looks like a DER encoding of |
| 116 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 117 | * RSAPrivateKey ::= SEQUENCE { |
| 118 | * version Version, |
| 119 | * modulus INTEGER, -- n |
| 120 | * publicExponent INTEGER, -- e |
| 121 | * privateExponent INTEGER, -- d |
| 122 | * prime1 INTEGER, -- p |
| 123 | * prime2 INTEGER, -- q |
| 124 | * exponent1 INTEGER, -- d mod (p-1) |
| 125 | * exponent2 INTEGER, -- d mod (q-1) |
| 126 | * coefficient INTEGER, -- (inverse of q) mod p |
| 127 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 128 | * } |
| 129 | * Or, for a public key, the same structure with only |
| 130 | * version, modulus and publicExponent. |
| 131 | */ |
| 132 | *p = buffer + buffer_size; |
| 133 | if( keypair ) |
| 134 | { |
| 135 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 136 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 137 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 138 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 139 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 140 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 141 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 142 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 143 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 144 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 145 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 146 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 147 | } |
| 148 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 149 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 150 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 151 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 152 | if( keypair ) |
| 153 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 154 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 155 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 156 | { |
| 157 | const unsigned char tag = |
| 158 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 159 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 160 | } |
| 161 | return( len ); |
| 162 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 163 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 164 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 165 | int exercise_mac_setup( psa_key_type_t key_type, |
| 166 | const unsigned char *key_bytes, |
| 167 | size_t key_length, |
| 168 | psa_algorithm_t alg, |
| 169 | psa_mac_operation_t *operation, |
| 170 | psa_status_t *status ) |
| 171 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 172 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 173 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 174 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 175 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 176 | psa_set_key_algorithm( &attributes, alg ); |
| 177 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 178 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 179 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 180 | *status = psa_mac_sign_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 181 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 182 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 183 | /* If setup failed, reproduce the failure, so that the caller can |
| 184 | * test the resulting state of the operation object. */ |
| 185 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 186 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 187 | TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 190 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 191 | return( 1 ); |
| 192 | |
| 193 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 194 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 195 | return( 0 ); |
| 196 | } |
| 197 | |
| 198 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 199 | const unsigned char *key_bytes, |
| 200 | size_t key_length, |
| 201 | psa_algorithm_t alg, |
| 202 | psa_cipher_operation_t *operation, |
| 203 | psa_status_t *status ) |
| 204 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 205 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 206 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 207 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 208 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 209 | psa_set_key_algorithm( &attributes, alg ); |
| 210 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 211 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 212 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 213 | *status = psa_cipher_encrypt_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 214 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 215 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 216 | /* If setup failed, reproduce the failure, so that the caller can |
| 217 | * test the resulting state of the operation object. */ |
| 218 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 219 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 220 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ), |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 221 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 224 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 225 | return( 1 ); |
| 226 | |
| 227 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 228 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 229 | return( 0 ); |
| 230 | } |
| 231 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 232 | static int test_operations_on_invalid_key( mbedtls_svc_key_id_t key ) |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 233 | { |
| 234 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 235 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 0x6964 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 236 | uint8_t buffer[1]; |
| 237 | size_t length; |
| 238 | int ok = 0; |
| 239 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 240 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 241 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 242 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 243 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 244 | TEST_EQUAL( psa_get_key_attributes( key, &attributes ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 245 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 246 | TEST_EQUAL( |
| 247 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 248 | TEST_EQUAL( |
| 249 | MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( psa_get_key_id( &attributes ) ), 0 ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 250 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 251 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 252 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 253 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 254 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 255 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 256 | TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 257 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 258 | TEST_EQUAL( psa_export_public_key( key, |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 259 | buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 260 | PSA_ERROR_INVALID_HANDLE ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 261 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 262 | ok = 1; |
| 263 | |
| 264 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 265 | /* |
| 266 | * Key attributes may have been returned by psa_get_key_attributes() |
| 267 | * thus reset them as required. |
| 268 | */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 269 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 270 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 271 | return( ok ); |
| 272 | } |
| 273 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 274 | /* Assert that a key isn't reported as having a slot number. */ |
| 275 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 276 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 277 | do \ |
| 278 | { \ |
| 279 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 280 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 281 | attributes, \ |
| 282 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 283 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 284 | } \ |
| 285 | while( 0 ) |
| 286 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 287 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 288 | ( (void) 0 ) |
| 289 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 290 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 291 | /* An overapproximation of the amount of storage needed for a key of the |
| 292 | * given type and with the given content. The API doesn't make it easy |
| 293 | * to find a good value for the size. The current implementation doesn't |
| 294 | * care about the value anyway. */ |
| 295 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 296 | ( data )->len |
| 297 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 298 | typedef enum { |
| 299 | IMPORT_KEY = 0, |
| 300 | GENERATE_KEY = 1, |
| 301 | DERIVE_KEY = 2 |
| 302 | } generate_method; |
| 303 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 304 | typedef enum |
| 305 | { |
| 306 | DO_NOT_SET_LENGTHS = 0, |
| 307 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 308 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 309 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 310 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 311 | typedef enum |
| 312 | { |
| 313 | USE_NULL_TAG = 0, |
| 314 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 315 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 316 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 317 | /*! |
| 318 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 319 | * \param key_type_arg Type of key passed in |
| 320 | * \param key_data The encryption / decryption key data |
| 321 | * \param alg_arg The type of algorithm used |
| 322 | * \param nonce Nonce data |
| 323 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 324 | * \param ad_part_len_arg If not -1, the length of chunks to |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 325 | * feed additional data in to be encrypted / |
| 326 | * decrypted. If -1, no chunking. |
| 327 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 328 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 329 | * the data in to be encrypted / decrypted. If |
| 330 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 331 | * \param set_lengths_method A member of the set_lengths_method_t enum is |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 332 | * expected here, this controls whether or not |
| 333 | * to set lengths, and in what order with |
| 334 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 335 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 336 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 337 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 338 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 339 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 340 | */ |
| 341 | static int aead_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 342 | int alg_arg, |
| 343 | data_t *nonce, |
| 344 | data_t *additional_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 345 | int ad_part_len_arg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 346 | data_t *input_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 347 | int data_part_len_arg, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 348 | set_lengths_method_t set_lengths_method, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 349 | data_t *expected_output, |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 350 | int is_encrypt, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 351 | int do_zero_parts ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 352 | { |
| 353 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 354 | psa_key_type_t key_type = key_type_arg; |
| 355 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 356 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 357 | unsigned char *output_data = NULL; |
| 358 | unsigned char *part_data = NULL; |
| 359 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 360 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 361 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 362 | size_t output_size = 0; |
| 363 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 364 | size_t output_length = 0; |
| 365 | size_t key_bits = 0; |
| 366 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 367 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 368 | size_t part_length = 0; |
| 369 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 370 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 371 | size_t ad_part_len = 0; |
| 372 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 373 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 374 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 375 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 376 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 377 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 378 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 379 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 380 | PSA_ASSERT( psa_crypto_init( ) ); |
| 381 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 382 | if( is_encrypt ) |
| 383 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 384 | else |
| 385 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 386 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 387 | psa_set_key_algorithm( &attributes, alg ); |
| 388 | psa_set_key_type( &attributes, key_type ); |
| 389 | |
| 390 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 391 | &key ) ); |
| 392 | |
| 393 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 394 | key_bits = psa_get_key_bits( &attributes ); |
| 395 | |
| 396 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 397 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 398 | if( is_encrypt ) |
| 399 | { |
| 400 | /* Tag gets written at end of buffer. */ |
| 401 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 402 | ( input_data->len + |
| 403 | tag_length ) ); |
| 404 | data_true_size = input_data->len; |
| 405 | } |
| 406 | else |
| 407 | { |
| 408 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 409 | ( input_data->len - |
| 410 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 411 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 412 | /* Do not want to attempt to decrypt tag. */ |
| 413 | data_true_size = input_data->len - tag_length; |
| 414 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 415 | |
| 416 | ASSERT_ALLOC( output_data, output_size ); |
| 417 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 418 | if( is_encrypt ) |
| 419 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 420 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 421 | TEST_LE_U( final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 422 | } |
| 423 | else |
| 424 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 425 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 426 | TEST_LE_U( final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 427 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 428 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 429 | ASSERT_ALLOC( final_data, final_output_size ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 430 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 431 | if( is_encrypt ) |
| 432 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 433 | else |
| 434 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 435 | |
| 436 | /* If the operation is not supported, just skip and not fail in case the |
| 437 | * encryption involves a common limitation of cryptography hardwares and |
| 438 | * an alternative implementation. */ |
| 439 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 440 | { |
| 441 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 442 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 443 | } |
| 444 | |
| 445 | PSA_ASSERT( status ); |
| 446 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 447 | if( set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 448 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 449 | else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 450 | { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 451 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 452 | data_true_size ) ); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 453 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 454 | } |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 455 | else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 456 | { |
| 457 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 458 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 459 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 460 | data_true_size ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 461 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 462 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 463 | if( ad_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 464 | { |
| 465 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 466 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 467 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 468 | for( part_offset = 0, part_count = 0; |
| 469 | part_offset < additional_data->len; |
| 470 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 471 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 472 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 473 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 474 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 475 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 476 | else if( additional_data->len - part_offset < ad_part_len ) |
| 477 | { |
| 478 | part_length = additional_data->len - part_offset; |
| 479 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 480 | else |
| 481 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 482 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | PSA_ASSERT( psa_aead_update_ad( &operation, |
| 486 | additional_data->x + part_offset, |
| 487 | part_length ) ); |
| 488 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | else |
| 492 | { |
| 493 | /* Pass additional data in one go. */ |
| 494 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 495 | additional_data->len ) ); |
| 496 | } |
| 497 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 498 | if( data_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 499 | { |
| 500 | /* Pass data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 501 | data_part_len = ( size_t ) data_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 502 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 503 | ( size_t ) data_part_len ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 504 | |
| 505 | ASSERT_ALLOC( part_data, part_data_size ); |
| 506 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 507 | for( part_offset = 0, part_count = 0; |
| 508 | part_offset < data_true_size; |
| 509 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 510 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 511 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 512 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 513 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 514 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 515 | else if( ( data_true_size - part_offset ) < data_part_len ) |
| 516 | { |
| 517 | part_length = ( data_true_size - part_offset ); |
| 518 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 519 | else |
| 520 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 521 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | PSA_ASSERT( psa_aead_update( &operation, |
| 525 | ( input_data->x + part_offset ), |
| 526 | part_length, part_data, |
| 527 | part_data_size, |
| 528 | &output_part_length ) ); |
| 529 | |
| 530 | if( output_data && output_part_length ) |
| 531 | { |
Mircea Udrea | 657ff4f | 2022-01-31 13:51:56 +0100 | [diff] [blame] | 532 | memcpy( ( output_data + output_length ), part_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 533 | output_part_length ); |
| 534 | } |
| 535 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 536 | output_length += output_part_length; |
| 537 | } |
| 538 | } |
| 539 | else |
| 540 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 541 | /* Pass all data in one go. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 542 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 543 | data_true_size, output_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 544 | output_size, &output_length ) ); |
| 545 | } |
| 546 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 547 | if( is_encrypt ) |
| 548 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 549 | final_output_size, |
| 550 | &output_part_length, |
| 551 | tag_buffer, tag_length, |
| 552 | &tag_size ) ); |
| 553 | else |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 554 | { |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 555 | PSA_ASSERT( psa_aead_verify( &operation, final_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 556 | final_output_size, |
| 557 | &output_part_length, |
| 558 | ( input_data->x + data_true_size ), |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 559 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 560 | } |
| 561 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 562 | if( output_data && output_part_length ) |
| 563 | memcpy( ( output_data + output_length ), final_data, |
| 564 | output_part_length ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 565 | |
| 566 | output_length += output_part_length; |
| 567 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 568 | |
| 569 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 570 | * should be exact.*/ |
| 571 | if( is_encrypt ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 572 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 573 | TEST_EQUAL( tag_length, tag_size ); |
| 574 | |
| 575 | if( output_data && tag_length ) |
| 576 | memcpy( ( output_data + output_length ), tag_buffer, |
| 577 | tag_length ); |
| 578 | |
| 579 | output_length += tag_length; |
| 580 | |
| 581 | TEST_EQUAL( output_length, |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 582 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 583 | input_data->len ) ); |
| 584 | TEST_LE_U( output_length, |
| 585 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 586 | } |
| 587 | else |
| 588 | { |
| 589 | TEST_EQUAL( output_length, |
| 590 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, |
| 591 | input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 592 | TEST_LE_U( output_length, |
| 593 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 594 | } |
| 595 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 596 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 597 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 598 | output_data, output_length ); |
| 599 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 600 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 601 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 602 | |
| 603 | exit: |
| 604 | psa_destroy_key( key ); |
| 605 | psa_aead_abort( &operation ); |
| 606 | mbedtls_free( output_data ); |
| 607 | mbedtls_free( part_data ); |
| 608 | mbedtls_free( final_data ); |
| 609 | PSA_DONE( ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 610 | |
| 611 | return( test_ok ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 612 | } |
| 613 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 614 | /*! |
| 615 | * \brief Internal Function for MAC multipart tests. |
| 616 | * \param key_type_arg Type of key passed in |
| 617 | * \param key_data The encryption / decryption key data |
| 618 | * \param alg_arg The type of algorithm used |
| 619 | * \param input_data Data to encrypt / decrypt |
| 620 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 621 | * the data in to be encrypted / decrypted. If |
| 622 | * -1, no chunking |
| 623 | * \param expected_output Expected output |
| 624 | * \param is_verify If non-zero this is an verify operation. |
| 625 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 626 | * with normal length chunks. |
| 627 | * \return int Zero on failure, non-zero on success. |
| 628 | */ |
| 629 | static int mac_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 630 | int alg_arg, |
| 631 | data_t *input_data, |
| 632 | int data_part_len_arg, |
| 633 | data_t *expected_output, |
| 634 | int is_verify, |
| 635 | int do_zero_parts ) |
| 636 | { |
| 637 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 638 | psa_key_type_t key_type = key_type_arg; |
| 639 | psa_algorithm_t alg = alg_arg; |
| 640 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 641 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 642 | size_t part_offset = 0; |
| 643 | size_t part_length = 0; |
| 644 | size_t data_part_len = 0; |
| 645 | size_t mac_len = 0; |
| 646 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 647 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 648 | |
| 649 | int test_ok = 0; |
| 650 | size_t part_count = 0; |
| 651 | |
Neil Armstrong | fd4c259 | 2022-03-07 10:11:11 +0100 | [diff] [blame] | 652 | PSA_INIT( ); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 653 | |
| 654 | if( is_verify ) |
| 655 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 656 | else |
| 657 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
| 658 | |
| 659 | psa_set_key_algorithm( &attributes, alg ); |
| 660 | psa_set_key_type( &attributes, key_type ); |
| 661 | |
| 662 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 663 | &key ) ); |
| 664 | |
| 665 | if( is_verify ) |
| 666 | status = psa_mac_verify_setup( &operation, key, alg ); |
| 667 | else |
| 668 | status = psa_mac_sign_setup( &operation, key, alg ); |
| 669 | |
| 670 | PSA_ASSERT( status ); |
| 671 | |
| 672 | if( data_part_len_arg != -1 ) |
| 673 | { |
| 674 | /* Pass data in parts */ |
| 675 | data_part_len = ( size_t ) data_part_len_arg; |
| 676 | |
| 677 | for( part_offset = 0, part_count = 0; |
| 678 | part_offset < input_data->len; |
| 679 | part_offset += part_length, part_count++ ) |
| 680 | { |
| 681 | if( do_zero_parts && ( part_count & 0x01 ) ) |
| 682 | { |
| 683 | part_length = 0; |
| 684 | } |
| 685 | else if( ( input_data->len - part_offset ) < data_part_len ) |
| 686 | { |
| 687 | part_length = ( input_data->len - part_offset ); |
| 688 | } |
| 689 | else |
| 690 | { |
| 691 | part_length = data_part_len; |
| 692 | } |
| 693 | |
| 694 | PSA_ASSERT( psa_mac_update( &operation, |
| 695 | ( input_data->x + part_offset ), |
| 696 | part_length ) ); |
| 697 | } |
| 698 | } |
| 699 | else |
| 700 | { |
| 701 | /* Pass all data in one go. */ |
| 702 | PSA_ASSERT( psa_mac_update( &operation, input_data->x, |
| 703 | input_data->len ) ); |
| 704 | } |
| 705 | |
| 706 | if( is_verify ) |
| 707 | { |
| 708 | PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x, |
| 709 | expected_output->len ) ); |
| 710 | } |
| 711 | else |
| 712 | { |
| 713 | PSA_ASSERT( psa_mac_sign_finish( &operation, mac, |
| 714 | PSA_MAC_MAX_SIZE, &mac_len ) ); |
| 715 | |
| 716 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 717 | mac, mac_len ); |
| 718 | } |
| 719 | |
| 720 | test_ok = 1; |
| 721 | |
| 722 | exit: |
| 723 | psa_destroy_key( key ); |
| 724 | psa_mac_abort( &operation ); |
| 725 | PSA_DONE( ); |
| 726 | |
| 727 | return( test_ok ); |
| 728 | } |
| 729 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 730 | #if defined(PSA_WANT_ALG_JPAKE) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 731 | static void ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive, |
| 732 | psa_pake_operation_t *server, |
| 733 | psa_pake_operation_t *client, |
| 734 | int client_input_first, |
| 735 | int round, int inject_error ) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 736 | { |
| 737 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 738 | size_t buffer_length = ( |
| 739 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 740 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 741 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF)) * 2; |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 742 | /* The output should be exactly this size according to the spec */ |
| 743 | const size_t expected_size_key_share = |
| 744 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); |
| 745 | /* The output should be exactly this size according to the spec */ |
| 746 | const size_t expected_size_zk_public = |
| 747 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); |
| 748 | /* The output can be smaller: the spec allows stripping leading zeroes */ |
| 749 | const size_t max_expected_size_zk_proof = |
| 750 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 751 | size_t buffer0_off = 0; |
| 752 | size_t buffer1_off = 0; |
| 753 | size_t s_g1_len, s_g2_len, s_a_len; |
| 754 | size_t s_g1_off, s_g2_off, s_a_off; |
| 755 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 756 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 757 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 758 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 759 | size_t c_g1_len, c_g2_len, c_a_len; |
| 760 | size_t c_g1_off, c_g2_off, c_a_off; |
| 761 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 762 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 763 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 764 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 765 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 766 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 767 | |
| 768 | ASSERT_ALLOC( buffer0, buffer_length ); |
| 769 | ASSERT_ALLOC( buffer1, buffer_length ); |
| 770 | |
| 771 | switch( round ) |
| 772 | { |
| 773 | case 1: |
| 774 | /* Server first round Output */ |
| 775 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 776 | buffer0 + buffer0_off, |
| 777 | 512 - buffer0_off, &s_g1_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 778 | TEST_EQUAL( s_g1_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 779 | s_g1_off = buffer0_off; |
| 780 | buffer0_off += s_g1_len; |
| 781 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 782 | buffer0 + buffer0_off, |
| 783 | 512 - buffer0_off, &s_x1_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 784 | TEST_EQUAL( s_x1_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 785 | s_x1_pk_off = buffer0_off; |
| 786 | buffer0_off += s_x1_pk_len; |
| 787 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 788 | buffer0 + buffer0_off, |
| 789 | 512 - buffer0_off, &s_x1_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 790 | TEST_LE_U( s_x1_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 791 | s_x1_pr_off = buffer0_off; |
| 792 | buffer0_off += s_x1_pr_len; |
| 793 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 794 | buffer0 + buffer0_off, |
| 795 | 512 - buffer0_off, &s_g2_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 796 | TEST_EQUAL( s_g2_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 797 | s_g2_off = buffer0_off; |
| 798 | buffer0_off += s_g2_len; |
| 799 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 800 | buffer0 + buffer0_off, |
| 801 | 512 - buffer0_off, &s_x2_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 802 | TEST_EQUAL( s_x2_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 803 | s_x2_pk_off = buffer0_off; |
| 804 | buffer0_off += s_x2_pk_len; |
| 805 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 806 | buffer0 + buffer0_off, |
| 807 | 512 - buffer0_off, &s_x2_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 808 | TEST_LE_U( s_x2_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 809 | s_x2_pr_off = buffer0_off; |
| 810 | buffer0_off += s_x2_pr_len; |
| 811 | |
| 812 | if( inject_error == 1 ) |
| 813 | { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 814 | buffer0[s_x1_pr_off + 8] ^= 1; |
| 815 | buffer0[s_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 816 | expected_status = PSA_ERROR_DATA_INVALID; |
| 817 | } |
| 818 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 819 | /* |
| 820 | * When injecting errors in inputs, the implementation is |
| 821 | * free to detect it right away of with a delay. |
| 822 | * This permits delaying the error until the end of the input |
| 823 | * sequence, if no error appears then, this will be treated |
| 824 | * as an error. |
| 825 | */ |
| 826 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 827 | if( client_input_first == 1 ) |
| 828 | { |
| 829 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 830 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 831 | buffer0 + s_g1_off, s_g1_len ); |
| 832 | if( inject_error == 1 && status != PSA_SUCCESS ) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 833 | { |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 834 | TEST_EQUAL( status, expected_status ); |
| 835 | break; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 836 | } |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 837 | else |
| 838 | { |
| 839 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 840 | } |
| 841 | |
| 842 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 843 | buffer0 + s_x1_pk_off, |
| 844 | s_x1_pk_len ); |
| 845 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 846 | { |
| 847 | TEST_EQUAL( status, expected_status ); |
| 848 | break; |
| 849 | } |
| 850 | else |
| 851 | { |
| 852 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 853 | } |
| 854 | |
| 855 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 856 | buffer0 + s_x1_pr_off, |
| 857 | s_x1_pr_len ); |
| 858 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 859 | { |
| 860 | TEST_EQUAL( status, expected_status ); |
| 861 | break; |
| 862 | } |
| 863 | else |
| 864 | { |
| 865 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 866 | } |
| 867 | |
| 868 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 869 | buffer0 + s_g2_off, |
| 870 | s_g2_len ); |
| 871 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 872 | { |
| 873 | TEST_EQUAL( status, expected_status ); |
| 874 | break; |
| 875 | } |
| 876 | else |
| 877 | { |
| 878 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 879 | } |
| 880 | |
| 881 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 882 | buffer0 + s_x2_pk_off, |
| 883 | s_x2_pk_len ); |
| 884 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 885 | { |
| 886 | TEST_EQUAL( status, expected_status ); |
| 887 | break; |
| 888 | } |
| 889 | else |
| 890 | { |
| 891 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 892 | } |
| 893 | |
| 894 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 895 | buffer0 + s_x2_pr_off, |
| 896 | s_x2_pr_len ); |
| 897 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 898 | { |
| 899 | TEST_EQUAL( status, expected_status ); |
| 900 | break; |
| 901 | } |
| 902 | else |
| 903 | { |
| 904 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 905 | } |
| 906 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 907 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 908 | if( inject_error == 1 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 909 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | /* Client first round Output */ |
| 913 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 914 | buffer1 + buffer1_off, |
| 915 | 512 - buffer1_off, &c_g1_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 916 | TEST_EQUAL( c_g1_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 917 | c_g1_off = buffer1_off; |
| 918 | buffer1_off += c_g1_len; |
| 919 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 920 | buffer1 + buffer1_off, |
| 921 | 512 - buffer1_off, &c_x1_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 922 | TEST_EQUAL( c_x1_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 923 | c_x1_pk_off = buffer1_off; |
| 924 | buffer1_off += c_x1_pk_len; |
| 925 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 926 | buffer1 + buffer1_off, |
| 927 | 512 - buffer1_off, &c_x1_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 928 | TEST_LE_U( c_x1_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 929 | c_x1_pr_off = buffer1_off; |
| 930 | buffer1_off += c_x1_pr_len; |
| 931 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 932 | buffer1 + buffer1_off, |
| 933 | 512 - buffer1_off, &c_g2_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 934 | TEST_EQUAL( c_g2_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 935 | c_g2_off = buffer1_off; |
| 936 | buffer1_off += c_g2_len; |
| 937 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 938 | buffer1 + buffer1_off, |
| 939 | 512 - buffer1_off, &c_x2_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 940 | TEST_EQUAL( c_x2_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 941 | c_x2_pk_off = buffer1_off; |
| 942 | buffer1_off += c_x2_pk_len; |
| 943 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 944 | buffer1 + buffer1_off, |
| 945 | 512 - buffer1_off, &c_x2_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 946 | TEST_LE_U( c_x2_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 947 | c_x2_pr_off = buffer1_off; |
| 948 | buffer1_off += c_x2_pr_len; |
| 949 | |
| 950 | if( client_input_first == 0 ) |
| 951 | { |
| 952 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 953 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 954 | buffer0 + s_g1_off, s_g1_len ); |
| 955 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 956 | { |
| 957 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 958 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 959 | } |
| 960 | else |
| 961 | { |
| 962 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 963 | } |
| 964 | |
| 965 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 966 | buffer0 + s_x1_pk_off, |
| 967 | s_x1_pk_len ); |
| 968 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 969 | { |
| 970 | TEST_EQUAL( status, expected_status ); |
| 971 | break; |
| 972 | } |
| 973 | else |
| 974 | { |
| 975 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 976 | } |
| 977 | |
| 978 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 979 | buffer0 + s_x1_pr_off, |
| 980 | s_x1_pr_len ); |
| 981 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 982 | { |
| 983 | TEST_EQUAL( status, expected_status ); |
| 984 | break; |
| 985 | } |
| 986 | else |
| 987 | { |
| 988 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 989 | } |
| 990 | |
| 991 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 992 | buffer0 + s_g2_off, |
| 993 | s_g2_len ); |
| 994 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 995 | { |
| 996 | TEST_EQUAL( status, expected_status ); |
| 997 | break; |
| 998 | } |
| 999 | else |
| 1000 | { |
| 1001 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1002 | } |
| 1003 | |
| 1004 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1005 | buffer0 + s_x2_pk_off, |
| 1006 | s_x2_pk_len ); |
| 1007 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 1008 | { |
| 1009 | TEST_EQUAL( status, expected_status ); |
| 1010 | break; |
| 1011 | } |
| 1012 | else |
| 1013 | { |
| 1014 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1015 | } |
| 1016 | |
| 1017 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1018 | buffer0 + s_x2_pr_off, |
| 1019 | s_x2_pr_len ); |
| 1020 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 1021 | { |
| 1022 | TEST_EQUAL( status, expected_status ); |
| 1023 | break; |
| 1024 | } |
| 1025 | else |
| 1026 | { |
| 1027 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1028 | } |
| 1029 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1030 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1031 | if( inject_error == 1 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1032 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | if( inject_error == 2 ) |
| 1036 | { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 1037 | buffer1[c_x1_pr_off + 12] ^= 1; |
| 1038 | buffer1[c_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1039 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1040 | } |
| 1041 | |
| 1042 | /* Server first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1043 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1044 | buffer1 + c_g1_off, c_g1_len ); |
| 1045 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1046 | { |
| 1047 | TEST_EQUAL( status, expected_status ); |
| 1048 | break; |
| 1049 | } |
| 1050 | else |
| 1051 | { |
| 1052 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1053 | } |
| 1054 | |
| 1055 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1056 | buffer1 + c_x1_pk_off, c_x1_pk_len ); |
| 1057 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1058 | { |
| 1059 | TEST_EQUAL( status, expected_status ); |
| 1060 | break; |
| 1061 | } |
| 1062 | else |
| 1063 | { |
| 1064 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1065 | } |
| 1066 | |
| 1067 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1068 | buffer1 + c_x1_pr_off, c_x1_pr_len ); |
| 1069 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1070 | { |
| 1071 | TEST_EQUAL( status, expected_status ); |
| 1072 | break; |
| 1073 | } |
| 1074 | else |
| 1075 | { |
| 1076 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1077 | } |
| 1078 | |
| 1079 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1080 | buffer1 + c_g2_off, c_g2_len ); |
| 1081 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1082 | { |
| 1083 | TEST_EQUAL( status, expected_status ); |
| 1084 | break; |
| 1085 | } |
| 1086 | else |
| 1087 | { |
| 1088 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1089 | } |
| 1090 | |
| 1091 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1092 | buffer1 + c_x2_pk_off, c_x2_pk_len ); |
| 1093 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1094 | { |
| 1095 | TEST_EQUAL( status, expected_status ); |
| 1096 | break; |
| 1097 | } |
| 1098 | else |
| 1099 | { |
| 1100 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1101 | } |
| 1102 | |
| 1103 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1104 | buffer1 + c_x2_pr_off, c_x2_pr_len ); |
| 1105 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1106 | { |
| 1107 | TEST_EQUAL( status, expected_status ); |
| 1108 | break; |
| 1109 | } |
| 1110 | else |
| 1111 | { |
| 1112 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1113 | } |
| 1114 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1115 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1116 | if( inject_error == 2 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1117 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1118 | |
| 1119 | break; |
| 1120 | |
| 1121 | case 2: |
| 1122 | /* Server second round Output */ |
| 1123 | buffer0_off = 0; |
| 1124 | |
| 1125 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1126 | buffer0 + buffer0_off, |
| 1127 | 512 - buffer0_off, &s_a_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1128 | TEST_EQUAL( s_a_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1129 | s_a_off = buffer0_off; |
| 1130 | buffer0_off += s_a_len; |
| 1131 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1132 | buffer0 + buffer0_off, |
| 1133 | 512 - buffer0_off, &s_x2s_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1134 | TEST_EQUAL( s_x2s_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1135 | s_x2s_pk_off = buffer0_off; |
| 1136 | buffer0_off += s_x2s_pk_len; |
| 1137 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1138 | buffer0 + buffer0_off, |
| 1139 | 512 - buffer0_off, &s_x2s_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1140 | TEST_LE_U( s_x2s_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1141 | s_x2s_pr_off = buffer0_off; |
| 1142 | buffer0_off += s_x2s_pr_len; |
| 1143 | |
| 1144 | if( inject_error == 3 ) |
| 1145 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1146 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1147 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1148 | } |
| 1149 | |
| 1150 | if( client_input_first == 1 ) |
| 1151 | { |
| 1152 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1153 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1154 | buffer0 + s_a_off, s_a_len ); |
| 1155 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1156 | { |
| 1157 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1158 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1159 | } |
| 1160 | else |
| 1161 | { |
| 1162 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1163 | } |
| 1164 | |
| 1165 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1166 | buffer0 + s_x2s_pk_off, |
| 1167 | s_x2s_pk_len ); |
| 1168 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1169 | { |
| 1170 | TEST_EQUAL( status, expected_status ); |
| 1171 | break; |
| 1172 | } |
| 1173 | else |
| 1174 | { |
| 1175 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1176 | } |
| 1177 | |
| 1178 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1179 | buffer0 + s_x2s_pr_off, |
| 1180 | s_x2s_pr_len ); |
| 1181 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1182 | { |
| 1183 | TEST_EQUAL( status, expected_status ); |
| 1184 | break; |
| 1185 | } |
| 1186 | else |
| 1187 | { |
| 1188 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1189 | } |
| 1190 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1191 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1192 | if( inject_error == 3 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1193 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | /* Client second round Output */ |
| 1197 | buffer1_off = 0; |
| 1198 | |
| 1199 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1200 | buffer1 + buffer1_off, |
| 1201 | 512 - buffer1_off, &c_a_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1202 | TEST_EQUAL( c_a_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1203 | c_a_off = buffer1_off; |
| 1204 | buffer1_off += c_a_len; |
| 1205 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1206 | buffer1 + buffer1_off, |
| 1207 | 512 - buffer1_off, &c_x2s_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1208 | TEST_EQUAL( c_x2s_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1209 | c_x2s_pk_off = buffer1_off; |
| 1210 | buffer1_off += c_x2s_pk_len; |
| 1211 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1212 | buffer1 + buffer1_off, |
| 1213 | 512 - buffer1_off, &c_x2s_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1214 | TEST_LE_U( c_x2s_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1215 | c_x2s_pr_off = buffer1_off; |
| 1216 | buffer1_off += c_x2s_pr_len; |
| 1217 | |
| 1218 | if( client_input_first == 0 ) |
| 1219 | { |
| 1220 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1221 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1222 | buffer0 + s_a_off, s_a_len ); |
| 1223 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1224 | { |
| 1225 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1226 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1227 | } |
| 1228 | else |
| 1229 | { |
| 1230 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1231 | } |
| 1232 | |
| 1233 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1234 | buffer0 + s_x2s_pk_off, |
| 1235 | s_x2s_pk_len ); |
| 1236 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1237 | { |
| 1238 | TEST_EQUAL( status, expected_status ); |
| 1239 | break; |
| 1240 | } |
| 1241 | else |
| 1242 | { |
| 1243 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1244 | } |
| 1245 | |
| 1246 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1247 | buffer0 + s_x2s_pr_off, |
| 1248 | s_x2s_pr_len ); |
| 1249 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1250 | { |
| 1251 | TEST_EQUAL( status, expected_status ); |
| 1252 | break; |
| 1253 | } |
| 1254 | else |
| 1255 | { |
| 1256 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1257 | } |
| 1258 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1259 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1260 | if( inject_error == 3 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1261 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | if( inject_error == 4 ) |
| 1265 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1266 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1267 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1268 | } |
| 1269 | |
| 1270 | /* Server second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1271 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1272 | buffer1 + c_a_off, c_a_len ); |
| 1273 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1274 | { |
| 1275 | TEST_EQUAL( status, expected_status ); |
| 1276 | break; |
| 1277 | } |
| 1278 | else |
| 1279 | { |
| 1280 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1281 | } |
| 1282 | |
| 1283 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1284 | buffer1 + c_x2s_pk_off, c_x2s_pk_len ); |
| 1285 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1286 | { |
| 1287 | TEST_EQUAL( status, expected_status ); |
| 1288 | break; |
| 1289 | } |
| 1290 | else |
| 1291 | { |
| 1292 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1293 | } |
| 1294 | |
| 1295 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1296 | buffer1 + c_x2s_pr_off, c_x2s_pr_len ); |
| 1297 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1298 | { |
| 1299 | TEST_EQUAL( status, expected_status ); |
| 1300 | break; |
| 1301 | } |
| 1302 | else |
| 1303 | { |
| 1304 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1305 | } |
| 1306 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1307 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1308 | if( inject_error == 4 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1309 | TEST_ASSERT( ! "One of the last psa_pake_input() calls should have returned the expected error." ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1310 | |
| 1311 | break; |
| 1312 | |
| 1313 | } |
| 1314 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1315 | exit: |
| 1316 | mbedtls_free( buffer0 ); |
| 1317 | mbedtls_free( buffer1 ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1318 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1319 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1320 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1321 | /* END_HEADER */ |
| 1322 | |
| 1323 | /* BEGIN_DEPENDENCIES |
| 1324 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1325 | * END_DEPENDENCIES |
| 1326 | */ |
| 1327 | |
| 1328 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1329 | void static_checks( ) |
| 1330 | { |
| 1331 | size_t max_truncated_mac_size = |
| 1332 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1333 | |
| 1334 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1335 | * encoding. The shifted mask is the maximum truncated value. The |
| 1336 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1337 | TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size ); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1338 | } |
| 1339 | /* END_CASE */ |
| 1340 | |
| 1341 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1342 | void import_with_policy( int type_arg, |
| 1343 | int usage_arg, int alg_arg, |
| 1344 | int expected_status_arg ) |
| 1345 | { |
| 1346 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1347 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1348 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1349 | psa_key_type_t type = type_arg; |
| 1350 | psa_key_usage_t usage = usage_arg; |
| 1351 | psa_algorithm_t alg = alg_arg; |
| 1352 | psa_status_t expected_status = expected_status_arg; |
| 1353 | const uint8_t key_material[16] = {0}; |
| 1354 | psa_status_t status; |
| 1355 | |
| 1356 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1357 | |
| 1358 | psa_set_key_type( &attributes, type ); |
| 1359 | psa_set_key_usage_flags( &attributes, usage ); |
| 1360 | psa_set_key_algorithm( &attributes, alg ); |
| 1361 | |
| 1362 | status = psa_import_key( &attributes, |
| 1363 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1364 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1365 | TEST_EQUAL( status, expected_status ); |
| 1366 | if( status != PSA_SUCCESS ) |
| 1367 | goto exit; |
| 1368 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1369 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1370 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 1371 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1372 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1373 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1374 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1375 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1376 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1377 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1378 | |
| 1379 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1380 | /* |
| 1381 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1382 | * thus reset them as required. |
| 1383 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1384 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1385 | |
| 1386 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1387 | PSA_DONE( ); |
| 1388 | } |
| 1389 | /* END_CASE */ |
| 1390 | |
| 1391 | /* BEGIN_CASE */ |
| 1392 | void import_with_data( data_t *data, int type_arg, |
| 1393 | int attr_bits_arg, |
| 1394 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1395 | { |
| 1396 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1397 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1398 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1399 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1400 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1401 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1402 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1403 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1404 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1405 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1406 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1407 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1408 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1409 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1410 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1411 | if( status != PSA_SUCCESS ) |
| 1412 | goto exit; |
| 1413 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1414 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1415 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1416 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1417 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1418 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1419 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1420 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1421 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1422 | |
| 1423 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1424 | /* |
| 1425 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1426 | * thus reset them as required. |
| 1427 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1428 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1429 | |
| 1430 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1431 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1432 | } |
| 1433 | /* END_CASE */ |
| 1434 | |
| 1435 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1436 | void import_large_key( int type_arg, int byte_size_arg, |
| 1437 | int expected_status_arg ) |
| 1438 | { |
| 1439 | psa_key_type_t type = type_arg; |
| 1440 | size_t byte_size = byte_size_arg; |
| 1441 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1442 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1443 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1444 | psa_status_t status; |
| 1445 | uint8_t *buffer = NULL; |
| 1446 | size_t buffer_size = byte_size + 1; |
| 1447 | size_t n; |
| 1448 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1449 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1450 | * accommodate large keys due to heap size constraints */ |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1451 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1452 | memset( buffer, 'K', byte_size ); |
| 1453 | |
| 1454 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1455 | |
| 1456 | /* Try importing the key */ |
| 1457 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1458 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1459 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 1460 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1461 | TEST_EQUAL( status, expected_status ); |
| 1462 | |
| 1463 | if( status == PSA_SUCCESS ) |
| 1464 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1465 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1466 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1467 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1468 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1469 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1470 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1471 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1472 | for( n = 0; n < byte_size; n++ ) |
| 1473 | TEST_EQUAL( buffer[n], 'K' ); |
| 1474 | for( n = byte_size; n < buffer_size; n++ ) |
| 1475 | TEST_EQUAL( buffer[n], 0 ); |
| 1476 | } |
| 1477 | |
| 1478 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1479 | /* |
| 1480 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1481 | * thus reset them as required. |
| 1482 | */ |
| 1483 | psa_reset_key_attributes( &attributes ); |
| 1484 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1485 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1486 | PSA_DONE( ); |
| 1487 | mbedtls_free( buffer ); |
| 1488 | } |
| 1489 | /* END_CASE */ |
| 1490 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1491 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1492 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1493 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1494 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1495 | size_t bits = bits_arg; |
| 1496 | psa_status_t expected_status = expected_status_arg; |
| 1497 | psa_status_t status; |
| 1498 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1499 | keypair ? PSA_KEY_TYPE_RSA_KEY_PAIR : PSA_KEY_TYPE_RSA_PUBLIC_KEY; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1500 | size_t buffer_size = /* Slight overapproximations */ |
| 1501 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1502 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1503 | unsigned char *p; |
| 1504 | int ret; |
| 1505 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1506 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1507 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1508 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1509 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1510 | |
| 1511 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1512 | bits, keypair ) ) >= 0 ); |
| 1513 | length = ret; |
| 1514 | |
| 1515 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1516 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1517 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1518 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1519 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1520 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1521 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1522 | |
| 1523 | exit: |
| 1524 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1525 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1526 | } |
| 1527 | /* END_CASE */ |
| 1528 | |
| 1529 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1530 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1531 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1532 | int usage_arg, int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1533 | int lifetime_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1534 | int expected_bits, |
| 1535 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1536 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1537 | int canonical_input ) |
| 1538 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1539 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1540 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1541 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1542 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1543 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1544 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1545 | unsigned char *exported = NULL; |
| 1546 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1547 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1548 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1549 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1550 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1551 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1552 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1553 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1554 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1555 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1556 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1557 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1558 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1559 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1560 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1561 | psa_set_key_algorithm( &attributes, alg ); |
| 1562 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1563 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1564 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1565 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1566 | |
| 1567 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1568 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1569 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1570 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1571 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1572 | |
| 1573 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1574 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1575 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1576 | |
| 1577 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1578 | * and export_size. On errors, the exported length must be 0. */ |
| 1579 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1580 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1581 | TEST_LE_U( exported_length, export_size ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1582 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1583 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1584 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1585 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1586 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1587 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1588 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1589 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1590 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1591 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1592 | * this validates the canonical representations. For canonical inputs, |
| 1593 | * this doesn't directly validate the implementation, but it still helps |
| 1594 | * by cross-validating the test data with the sanity check code. */ |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1595 | if( !psa_key_lifetime_is_external( lifetime ) ) |
| 1596 | { |
| 1597 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
| 1598 | goto exit; |
| 1599 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1600 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1601 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1602 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1603 | else |
| 1604 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1605 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1606 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1607 | &key2 ) ); |
| 1608 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1609 | reexported, |
| 1610 | export_size, |
| 1611 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1612 | ASSERT_COMPARE( exported, exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1613 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1614 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1615 | } |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1616 | TEST_LE_U( exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1617 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 1618 | psa_get_key_bits( &got_attributes ) ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1619 | TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1620 | |
| 1621 | destroy: |
| 1622 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1623 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1624 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1625 | |
| 1626 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1627 | /* |
| 1628 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1629 | * thus reset them as required. |
| 1630 | */ |
| 1631 | psa_reset_key_attributes( &got_attributes ); |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1632 | psa_destroy_key( key ) ; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1633 | mbedtls_free( exported ); |
| 1634 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1635 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1636 | } |
| 1637 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1638 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1639 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1640 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1641 | int type_arg, |
| 1642 | int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1643 | int lifetime_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1644 | int export_size_delta, |
| 1645 | int expected_export_status_arg, |
| 1646 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1647 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1648 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1649 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1650 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1651 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1652 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1653 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1654 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1655 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1656 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1657 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1658 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1659 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1660 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1661 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1662 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1663 | psa_set_key_algorithm( &attributes, alg ); |
| 1664 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1665 | |
| 1666 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1667 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1668 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1669 | /* Export the public key */ |
| 1670 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1671 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1672 | exported, export_size, |
| 1673 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1674 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1675 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1676 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1677 | psa_key_type_t public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( type ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1678 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1679 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1680 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1681 | TEST_LE_U( expected_public_key->len, |
| 1682 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1683 | TEST_LE_U( expected_public_key->len, |
| 1684 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1685 | TEST_LE_U( expected_public_key->len, |
| 1686 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1687 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1688 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1689 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1690 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1691 | /* |
| 1692 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1693 | * thus reset them as required. |
| 1694 | */ |
| 1695 | psa_reset_key_attributes( &attributes ); |
| 1696 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1697 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1698 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1699 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1700 | } |
| 1701 | /* END_CASE */ |
| 1702 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1703 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1704 | void import_and_exercise_key( data_t *data, |
| 1705 | int type_arg, |
| 1706 | int bits_arg, |
| 1707 | int alg_arg ) |
| 1708 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1709 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1710 | psa_key_type_t type = type_arg; |
| 1711 | size_t bits = bits_arg; |
| 1712 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1713 | psa_key_usage_t usage = mbedtls_test_psa_usage_to_exercise( type, alg ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1714 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1715 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1716 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1717 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1718 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1719 | psa_set_key_usage_flags( &attributes, usage ); |
| 1720 | psa_set_key_algorithm( &attributes, alg ); |
| 1721 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1722 | |
| 1723 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1724 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1725 | |
| 1726 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1727 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1728 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1729 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1730 | |
| 1731 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1732 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1733 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1734 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1735 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1736 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1737 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1738 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1739 | /* |
| 1740 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1741 | * thus reset them as required. |
| 1742 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1743 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1744 | |
| 1745 | psa_reset_key_attributes( &attributes ); |
| 1746 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1747 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1748 | } |
| 1749 | /* END_CASE */ |
| 1750 | |
| 1751 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1752 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1753 | int bits_arg, int expected_bits_arg, |
| 1754 | int usage_arg, int expected_usage_arg, |
| 1755 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1756 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1757 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1758 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1759 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1760 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1761 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1762 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1763 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1764 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1765 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1766 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1767 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1768 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1769 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1770 | psa_set_key_usage_flags( &attributes, usage ); |
| 1771 | psa_set_key_algorithm( &attributes, alg ); |
| 1772 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1773 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1774 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1775 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1776 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1777 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1778 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1779 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1780 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1781 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1782 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1783 | |
| 1784 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1785 | /* |
| 1786 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1787 | * thus reset them as required. |
| 1788 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1789 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1790 | |
| 1791 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1792 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1793 | } |
| 1794 | /* END_CASE */ |
| 1795 | |
| 1796 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1797 | void check_key_policy( int type_arg, int bits_arg, |
| 1798 | int usage_arg, int alg_arg ) |
| 1799 | { |
| 1800 | test_effective_key_attributes( type_arg, type_arg, bits_arg, bits_arg, |
gabor-mezei-arm | ff8264c | 2021-06-28 14:36:03 +0200 | [diff] [blame] | 1801 | usage_arg, |
| 1802 | mbedtls_test_update_key_usage_flags( usage_arg ), |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1803 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1804 | goto exit; |
| 1805 | } |
| 1806 | /* END_CASE */ |
| 1807 | |
| 1808 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1809 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1810 | { |
| 1811 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1812 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1813 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1814 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1815 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1816 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1817 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1818 | |
| 1819 | memset( &zero, 0, sizeof( zero ) ); |
| 1820 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1821 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1822 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1823 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1824 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1825 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1826 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1827 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1828 | |
| 1829 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1830 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1831 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1832 | |
| 1833 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1834 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1835 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1836 | |
| 1837 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1838 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1839 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1840 | } |
| 1841 | /* END_CASE */ |
| 1842 | |
| 1843 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1844 | void mac_key_policy( int policy_usage_arg, |
| 1845 | int policy_alg_arg, |
| 1846 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1847 | data_t *key_data, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1848 | int exercise_alg_arg, |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1849 | int expected_status_sign_arg, |
| 1850 | int expected_status_verify_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1851 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1852 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1853 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1854 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1855 | psa_key_type_t key_type = key_type_arg; |
| 1856 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1857 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1858 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1859 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1860 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1861 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1862 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1863 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1864 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1865 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1866 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1867 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1868 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1869 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1870 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1871 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1872 | |
gabor-mezei-arm | 40d5cd8 | 2021-06-29 11:06:16 +0200 | [diff] [blame] | 1873 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
| 1874 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1875 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1876 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1877 | TEST_EQUAL( status, expected_status_sign ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1878 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1879 | /* Calculate the MAC, one-shot case. */ |
| 1880 | uint8_t input[128] = {0}; |
| 1881 | size_t mac_len; |
| 1882 | TEST_EQUAL( psa_mac_compute( key, exercise_alg, |
| 1883 | input, 128, |
| 1884 | mac, PSA_MAC_MAX_SIZE, &mac_len ), |
| 1885 | expected_status_sign ); |
| 1886 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1887 | /* Calculate the MAC, multi-part case. */ |
| 1888 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1889 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
| 1890 | if( status == PSA_SUCCESS ) |
| 1891 | { |
| 1892 | status = psa_mac_update( &operation, input, 128 ); |
| 1893 | if( status == PSA_SUCCESS ) |
| 1894 | TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE, |
| 1895 | &mac_len ), |
| 1896 | expected_status_sign ); |
| 1897 | else |
| 1898 | TEST_EQUAL( status, expected_status_sign ); |
| 1899 | } |
| 1900 | else |
| 1901 | { |
| 1902 | TEST_EQUAL( status, expected_status_sign ); |
| 1903 | } |
| 1904 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1905 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1906 | /* Verify correct MAC, one-shot case. */ |
| 1907 | status = psa_mac_verify( key, exercise_alg, input, 128, |
| 1908 | mac, mac_len ); |
| 1909 | |
| 1910 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1911 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1912 | else |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1913 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1914 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1915 | /* Verify correct MAC, multi-part case. */ |
| 1916 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
| 1917 | if( status == PSA_SUCCESS ) |
| 1918 | { |
| 1919 | status = psa_mac_update( &operation, input, 128 ); |
| 1920 | if( status == PSA_SUCCESS ) |
| 1921 | { |
| 1922 | status = psa_mac_verify_finish( &operation, mac, mac_len ); |
| 1923 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1924 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1925 | else |
| 1926 | TEST_EQUAL( status, expected_status_verify ); |
| 1927 | } |
| 1928 | else |
| 1929 | { |
| 1930 | TEST_EQUAL( status, expected_status_verify ); |
| 1931 | } |
| 1932 | } |
| 1933 | else |
| 1934 | { |
| 1935 | TEST_EQUAL( status, expected_status_verify ); |
| 1936 | } |
| 1937 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1938 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1939 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1940 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1941 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1942 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1943 | |
| 1944 | exit: |
| 1945 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1946 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1947 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1948 | } |
| 1949 | /* END_CASE */ |
| 1950 | |
| 1951 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1952 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1953 | int policy_alg, |
| 1954 | int key_type, |
| 1955 | data_t *key_data, |
| 1956 | int exercise_alg ) |
| 1957 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1958 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1959 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1960 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1961 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1962 | size_t output_buffer_size = 0; |
| 1963 | size_t input_buffer_size = 0; |
| 1964 | size_t output_length = 0; |
| 1965 | uint8_t *output = NULL; |
| 1966 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1967 | psa_status_t status; |
| 1968 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1969 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg ); |
| 1970 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg, |
| 1971 | input_buffer_size ); |
| 1972 | |
| 1973 | ASSERT_ALLOC( input, input_buffer_size ); |
| 1974 | ASSERT_ALLOC( output, output_buffer_size ); |
| 1975 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1976 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1977 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1978 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1979 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1980 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1981 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1982 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1983 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1984 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1985 | /* Check if no key usage flag implication is done */ |
| 1986 | TEST_EQUAL( policy_usage, |
| 1987 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1988 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1989 | /* Encrypt check, one-shot */ |
| 1990 | status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size, |
| 1991 | output, output_buffer_size, |
| 1992 | &output_length); |
| 1993 | if( policy_alg == exercise_alg && |
| 1994 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1995 | PSA_ASSERT( status ); |
| 1996 | else |
| 1997 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1998 | |
| 1999 | /* Encrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2000 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2001 | if( policy_alg == exercise_alg && |
| 2002 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2003 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2004 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2005 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2006 | psa_cipher_abort( &operation ); |
| 2007 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2008 | /* Decrypt check, one-shot */ |
| 2009 | status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size, |
| 2010 | input, input_buffer_size, |
| 2011 | &output_length); |
| 2012 | if( policy_alg == exercise_alg && |
| 2013 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 2014 | PSA_ASSERT( status ); |
| 2015 | else |
| 2016 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2017 | |
| 2018 | /* Decrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2019 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2020 | if( policy_alg == exercise_alg && |
| 2021 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2022 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2023 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2024 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2025 | |
| 2026 | exit: |
| 2027 | psa_cipher_abort( &operation ); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2028 | mbedtls_free( input ); |
| 2029 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2030 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2031 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2032 | } |
| 2033 | /* END_CASE */ |
| 2034 | |
| 2035 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2036 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2037 | int policy_alg, |
| 2038 | int key_type, |
| 2039 | data_t *key_data, |
| 2040 | int nonce_length_arg, |
| 2041 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2042 | int exercise_alg, |
| 2043 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2044 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2045 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2046 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2047 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2048 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2049 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2050 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2051 | unsigned char nonce[16] = {0}; |
| 2052 | size_t nonce_length = nonce_length_arg; |
| 2053 | unsigned char tag[16]; |
| 2054 | size_t tag_length = tag_length_arg; |
| 2055 | size_t output_length; |
| 2056 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2057 | TEST_LE_U( nonce_length, sizeof( nonce ) ); |
| 2058 | TEST_LE_U( tag_length, sizeof( tag ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2059 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2060 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2061 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2062 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2063 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2064 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2065 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2066 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2067 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2068 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2069 | /* Check if no key usage implication is done */ |
| 2070 | TEST_EQUAL( policy_usage, |
| 2071 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2072 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2073 | /* Encrypt check, one-shot */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2074 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2075 | nonce, nonce_length, |
| 2076 | NULL, 0, |
| 2077 | NULL, 0, |
| 2078 | tag, tag_length, |
| 2079 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2080 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2081 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2082 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2083 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2084 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2085 | /* Encrypt check, multi-part */ |
| 2086 | status = psa_aead_encrypt_setup( &operation, key, exercise_alg ); |
| 2087 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2088 | TEST_EQUAL( status, expected_status ); |
| 2089 | else |
| 2090 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2091 | |
| 2092 | /* Decrypt check, one-shot */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2093 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2094 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2095 | nonce, nonce_length, |
| 2096 | NULL, 0, |
| 2097 | tag, tag_length, |
| 2098 | NULL, 0, |
| 2099 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2100 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2101 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2102 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2103 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2104 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2105 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2106 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2107 | /* Decrypt check, multi-part */ |
| 2108 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
| 2109 | status = psa_aead_decrypt_setup( &operation, key, exercise_alg ); |
| 2110 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2111 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2112 | else |
| 2113 | TEST_EQUAL( status, expected_status ); |
| 2114 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2115 | exit: |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2116 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2117 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2118 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2119 | } |
| 2120 | /* END_CASE */ |
| 2121 | |
| 2122 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2123 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2124 | int policy_alg, |
| 2125 | int key_type, |
| 2126 | data_t *key_data, |
| 2127 | int exercise_alg ) |
| 2128 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2129 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2130 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2131 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2132 | psa_status_t status; |
| 2133 | size_t key_bits; |
| 2134 | size_t buffer_length; |
| 2135 | unsigned char *buffer = NULL; |
| 2136 | size_t output_length; |
| 2137 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2138 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2139 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2140 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2141 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2142 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2143 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2144 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2145 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2146 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2147 | /* Check if no key usage implication is done */ |
| 2148 | TEST_EQUAL( policy_usage, |
| 2149 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2150 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2151 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2152 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2153 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 2154 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2155 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2156 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2157 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2158 | NULL, 0, |
| 2159 | NULL, 0, |
| 2160 | buffer, buffer_length, |
| 2161 | &output_length ); |
| 2162 | if( policy_alg == exercise_alg && |
| 2163 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2164 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2165 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2166 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2167 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 2168 | if( buffer_length != 0 ) |
| 2169 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2170 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2171 | buffer, buffer_length, |
| 2172 | NULL, 0, |
| 2173 | buffer, buffer_length, |
| 2174 | &output_length ); |
| 2175 | if( policy_alg == exercise_alg && |
| 2176 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2177 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2178 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2179 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2180 | |
| 2181 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2182 | /* |
| 2183 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2184 | * thus reset them as required. |
| 2185 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2186 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2187 | |
| 2188 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2189 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2190 | mbedtls_free( buffer ); |
| 2191 | } |
| 2192 | /* END_CASE */ |
| 2193 | |
| 2194 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2195 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2196 | int policy_alg, |
| 2197 | int key_type, |
| 2198 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2199 | int exercise_alg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2200 | int payload_length_arg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2201 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2202 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2203 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2204 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2205 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2206 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2207 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2208 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 2209 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2210 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2211 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2212 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2213 | int compatible_alg = payload_length_arg > 0; |
| 2214 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2215 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2216 | size_t signature_length; |
| 2217 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2218 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2219 | in the expected usage flags. */ |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2220 | TEST_EQUAL( expected_usage, |
| 2221 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2222 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2223 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2224 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2225 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2226 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2227 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2228 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2229 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2230 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2231 | |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2232 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 2233 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2234 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2235 | payload, payload_length, |
| 2236 | signature, sizeof( signature ), |
| 2237 | &signature_length ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2238 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2239 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2240 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2241 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2242 | |
| 2243 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2244 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2245 | payload, payload_length, |
| 2246 | signature, sizeof( signature ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2247 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2248 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2249 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2250 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2251 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2252 | if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) && |
gabor-mezei-arm | d851d68 | 2021-06-28 14:53:49 +0200 | [diff] [blame] | 2253 | PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) ) |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2254 | { |
| 2255 | status = psa_sign_message( key, exercise_alg, |
| 2256 | payload, payload_length, |
| 2257 | signature, sizeof( signature ), |
| 2258 | &signature_length ); |
| 2259 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 2260 | PSA_ASSERT( status ); |
| 2261 | else |
| 2262 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2263 | |
| 2264 | memset( signature, 0, sizeof( signature ) ); |
| 2265 | status = psa_verify_message( key, exercise_alg, |
| 2266 | payload, payload_length, |
| 2267 | signature, sizeof( signature ) ); |
| 2268 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 2269 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 2270 | else |
| 2271 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2272 | } |
| 2273 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2274 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2275 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2276 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2277 | } |
| 2278 | /* END_CASE */ |
| 2279 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2280 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2281 | void derive_key_policy( int policy_usage, |
| 2282 | int policy_alg, |
| 2283 | int key_type, |
| 2284 | data_t *key_data, |
| 2285 | int exercise_alg ) |
| 2286 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2287 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2288 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2289 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2290 | psa_status_t status; |
| 2291 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2292 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2293 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2294 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2295 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2296 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2297 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2298 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2299 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2300 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2301 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2302 | |
| 2303 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 2304 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2305 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2306 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 2307 | &operation, |
| 2308 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2309 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2310 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2311 | |
| 2312 | status = psa_key_derivation_input_key( &operation, |
| 2313 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2314 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2315 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2316 | if( policy_alg == exercise_alg && |
| 2317 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2318 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2319 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2320 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2321 | |
| 2322 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2323 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2324 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2325 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2326 | } |
| 2327 | /* END_CASE */ |
| 2328 | |
| 2329 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2330 | void agreement_key_policy( int policy_usage, |
| 2331 | int policy_alg, |
| 2332 | int key_type_arg, |
| 2333 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2334 | int exercise_alg, |
| 2335 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2336 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2337 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2338 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2339 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2340 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2341 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2342 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2343 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2344 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2345 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2346 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2347 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2348 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2349 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2350 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2351 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2352 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2353 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2354 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2355 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2356 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2357 | |
| 2358 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2359 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2360 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2361 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2362 | } |
| 2363 | /* END_CASE */ |
| 2364 | |
| 2365 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2366 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2367 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2368 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2369 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2370 | psa_key_type_t key_type = key_type_arg; |
| 2371 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2372 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2373 | psa_key_usage_t usage = usage_arg; |
| 2374 | psa_algorithm_t alg = alg_arg; |
| 2375 | psa_algorithm_t alg2 = alg2_arg; |
| 2376 | |
| 2377 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2378 | |
| 2379 | psa_set_key_usage_flags( &attributes, usage ); |
| 2380 | psa_set_key_algorithm( &attributes, alg ); |
| 2381 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2382 | psa_set_key_type( &attributes, key_type ); |
| 2383 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2384 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2385 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2386 | /* Update the usage flags to obtain implicit usage flags */ |
| 2387 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2388 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2389 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2390 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2391 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2392 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2393 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2394 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2395 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2396 | goto exit; |
| 2397 | |
| 2398 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2399 | /* |
| 2400 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2401 | * thus reset them as required. |
| 2402 | */ |
| 2403 | psa_reset_key_attributes( &got_attributes ); |
| 2404 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2405 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2406 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2407 | } |
| 2408 | /* END_CASE */ |
| 2409 | |
| 2410 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2411 | void raw_agreement_key_policy( int policy_usage, |
| 2412 | int policy_alg, |
| 2413 | int key_type_arg, |
| 2414 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2415 | int exercise_alg, |
| 2416 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2417 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2418 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2419 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2420 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2421 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2422 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2423 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2424 | |
| 2425 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2426 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2427 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2428 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2429 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2430 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2431 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2432 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2433 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2434 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2435 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2436 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2437 | |
| 2438 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2439 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2440 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2441 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2442 | } |
| 2443 | /* END_CASE */ |
| 2444 | |
| 2445 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2446 | void copy_success( int source_usage_arg, |
| 2447 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2448 | unsigned int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2449 | int type_arg, data_t *material, |
| 2450 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2451 | int target_usage_arg, |
| 2452 | int target_alg_arg, int target_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2453 | unsigned int target_lifetime_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2454 | int expected_usage_arg, |
| 2455 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2456 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2457 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2458 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2459 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2460 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2461 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2462 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2463 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2464 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2465 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2466 | uint8_t *export_buffer = NULL; |
| 2467 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2468 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2469 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2470 | /* Prepare the source key. */ |
| 2471 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2472 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2473 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2474 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2475 | psa_set_key_lifetime( &source_attributes, source_lifetime); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2476 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2477 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2478 | &source_key ) ); |
| 2479 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2480 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2481 | /* Prepare the target attributes. */ |
| 2482 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2483 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2484 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2485 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2486 | psa_set_key_lifetime( &target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2487 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2488 | if( target_usage_arg != -1 ) |
| 2489 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2490 | if( target_alg_arg != -1 ) |
| 2491 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2492 | if( target_alg2_arg != -1 ) |
| 2493 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2494 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2495 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2496 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2497 | PSA_ASSERT( psa_copy_key( source_key, |
| 2498 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2499 | |
| 2500 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2501 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2502 | |
| 2503 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2504 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2505 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2506 | psa_get_key_type( &target_attributes ) ); |
| 2507 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2508 | psa_get_key_bits( &target_attributes ) ); |
| 2509 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2510 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2511 | TEST_EQUAL( expected_alg2, |
| 2512 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2513 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2514 | { |
| 2515 | size_t length; |
| 2516 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2517 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2518 | material->len, &length ) ); |
| 2519 | ASSERT_COMPARE( material->x, material->len, |
| 2520 | export_buffer, length ); |
| 2521 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2522 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2523 | if( !psa_key_lifetime_is_external( target_lifetime ) ) |
| 2524 | { |
| 2525 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
| 2526 | goto exit; |
| 2527 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
| 2528 | goto exit; |
| 2529 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2530 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2531 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2532 | |
| 2533 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2534 | /* |
| 2535 | * Source and target key attributes may have been returned by |
| 2536 | * psa_get_key_attributes() thus reset them as required. |
| 2537 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2538 | psa_reset_key_attributes( &source_attributes ); |
| 2539 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2540 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2541 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2542 | mbedtls_free( export_buffer ); |
| 2543 | } |
| 2544 | /* END_CASE */ |
| 2545 | |
| 2546 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2547 | void copy_fail( int source_usage_arg, |
| 2548 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2549 | int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2550 | int type_arg, data_t *material, |
| 2551 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2552 | int target_usage_arg, |
| 2553 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2554 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2555 | int expected_status_arg ) |
| 2556 | { |
| 2557 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2558 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2559 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2560 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2561 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, target_id_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2562 | |
| 2563 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2564 | |
| 2565 | /* Prepare the source key. */ |
| 2566 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2567 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2568 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2569 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2570 | psa_set_key_lifetime( &source_attributes, source_lifetime_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2571 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2572 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2573 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2574 | |
| 2575 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2576 | psa_set_key_id( &target_attributes, key_id ); |
| 2577 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2578 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2579 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2580 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2581 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2582 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2583 | |
| 2584 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2585 | TEST_EQUAL( psa_copy_key( source_key, |
| 2586 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2587 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2588 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2589 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2590 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2591 | exit: |
| 2592 | psa_reset_key_attributes( &source_attributes ); |
| 2593 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2594 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2595 | } |
| 2596 | /* END_CASE */ |
| 2597 | |
| 2598 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2599 | void hash_operation_init( ) |
| 2600 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2601 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2602 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2603 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2604 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 2605 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2606 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2607 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2608 | psa_hash_operation_t zero; |
| 2609 | |
| 2610 | memset( &zero, 0, sizeof( zero ) ); |
| 2611 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2612 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2613 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2614 | PSA_ERROR_BAD_STATE ); |
| 2615 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2616 | PSA_ERROR_BAD_STATE ); |
| 2617 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2618 | PSA_ERROR_BAD_STATE ); |
| 2619 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2620 | /* A default hash operation should be abortable without error. */ |
| 2621 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2622 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2623 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2624 | } |
| 2625 | /* END_CASE */ |
| 2626 | |
| 2627 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2628 | void hash_setup( int alg_arg, |
| 2629 | int expected_status_arg ) |
| 2630 | { |
| 2631 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2632 | uint8_t *output = NULL; |
| 2633 | size_t output_size = 0; |
| 2634 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2635 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2636 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2637 | psa_status_t status; |
| 2638 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2639 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2640 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2641 | /* Hash Setup, one-shot */ |
Neil Armstrong | ee9686b | 2022-02-25 15:47:34 +0100 | [diff] [blame] | 2642 | output_size = PSA_HASH_LENGTH( alg ); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2643 | ASSERT_ALLOC( output, output_size ); |
| 2644 | |
| 2645 | status = psa_hash_compute( alg, NULL, 0, |
| 2646 | output, output_size, &output_length ); |
| 2647 | TEST_EQUAL( status, expected_status ); |
| 2648 | |
| 2649 | /* Hash Setup, multi-part */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2650 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2651 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2652 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2653 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2654 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2655 | |
| 2656 | /* If setup failed, reproduce the failure, so as to |
| 2657 | * test the resulting state of the operation object. */ |
| 2658 | if( status != PSA_SUCCESS ) |
| 2659 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2660 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2661 | /* Now the operation object should be reusable. */ |
| 2662 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2663 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2664 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2665 | #endif |
| 2666 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2667 | exit: |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2668 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2669 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2670 | } |
| 2671 | /* END_CASE */ |
| 2672 | |
| 2673 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2674 | void hash_compute_fail( int alg_arg, data_t *input, |
| 2675 | int output_size_arg, int expected_status_arg ) |
| 2676 | { |
| 2677 | psa_algorithm_t alg = alg_arg; |
| 2678 | uint8_t *output = NULL; |
| 2679 | size_t output_size = output_size_arg; |
| 2680 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2681 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2682 | psa_status_t expected_status = expected_status_arg; |
| 2683 | psa_status_t status; |
| 2684 | |
| 2685 | ASSERT_ALLOC( output, output_size ); |
| 2686 | |
| 2687 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2688 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2689 | /* Hash Compute, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2690 | status = psa_hash_compute( alg, input->x, input->len, |
| 2691 | output, output_size, &output_length ); |
| 2692 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2693 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2694 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2695 | /* Hash Compute, multi-part */ |
| 2696 | status = psa_hash_setup( &operation, alg ); |
| 2697 | if( status == PSA_SUCCESS ) |
| 2698 | { |
| 2699 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2700 | if( status == PSA_SUCCESS ) |
| 2701 | { |
| 2702 | status = psa_hash_finish( &operation, output, output_size, |
| 2703 | &output_length ); |
| 2704 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2705 | TEST_LE_U( output_length, output_size ); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2706 | else |
| 2707 | TEST_EQUAL( status, expected_status ); |
| 2708 | } |
| 2709 | else |
| 2710 | { |
| 2711 | TEST_EQUAL( status, expected_status ); |
| 2712 | } |
| 2713 | } |
| 2714 | else |
| 2715 | { |
| 2716 | TEST_EQUAL( status, expected_status ); |
| 2717 | } |
| 2718 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2719 | exit: |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2720 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2721 | mbedtls_free( output ); |
| 2722 | PSA_DONE( ); |
| 2723 | } |
| 2724 | /* END_CASE */ |
| 2725 | |
| 2726 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2727 | void hash_compare_fail( int alg_arg, data_t *input, |
| 2728 | data_t *reference_hash, |
| 2729 | int expected_status_arg ) |
| 2730 | { |
| 2731 | psa_algorithm_t alg = alg_arg; |
| 2732 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2733 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2734 | psa_status_t status; |
| 2735 | |
| 2736 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2737 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2738 | /* Hash Compare, one-shot */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2739 | status = psa_hash_compare( alg, input->x, input->len, |
| 2740 | reference_hash->x, reference_hash->len ); |
| 2741 | TEST_EQUAL( status, expected_status ); |
| 2742 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2743 | /* Hash Compare, multi-part */ |
| 2744 | status = psa_hash_setup( &operation, alg ); |
| 2745 | if( status == PSA_SUCCESS ) |
| 2746 | { |
| 2747 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2748 | if( status == PSA_SUCCESS ) |
| 2749 | { |
| 2750 | status = psa_hash_verify( &operation, reference_hash->x, |
| 2751 | reference_hash->len ); |
| 2752 | TEST_EQUAL( status, expected_status ); |
| 2753 | } |
| 2754 | else |
| 2755 | { |
| 2756 | TEST_EQUAL( status, expected_status ); |
| 2757 | } |
| 2758 | } |
| 2759 | else |
| 2760 | { |
| 2761 | TEST_EQUAL( status, expected_status ); |
| 2762 | } |
| 2763 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2764 | exit: |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2765 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2766 | PSA_DONE( ); |
| 2767 | } |
| 2768 | /* END_CASE */ |
| 2769 | |
| 2770 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2771 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2772 | data_t *expected_output ) |
| 2773 | { |
| 2774 | psa_algorithm_t alg = alg_arg; |
| 2775 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2776 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2777 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2778 | size_t i; |
| 2779 | |
| 2780 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2781 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2782 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2783 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2784 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2785 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2786 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2787 | ASSERT_COMPARE( output, output_length, |
| 2788 | expected_output->x, expected_output->len ); |
| 2789 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2790 | /* Compute with tight buffer, multi-part */ |
| 2791 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2792 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2793 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2794 | PSA_HASH_LENGTH( alg ), |
| 2795 | &output_length ) ); |
| 2796 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2797 | ASSERT_COMPARE( output, output_length, |
| 2798 | expected_output->x, expected_output->len ); |
| 2799 | |
| 2800 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2801 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2802 | output, sizeof( output ), |
| 2803 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2804 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2805 | ASSERT_COMPARE( output, output_length, |
| 2806 | expected_output->x, expected_output->len ); |
| 2807 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2808 | /* Compute with larger buffer, multi-part */ |
| 2809 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2810 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2811 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2812 | sizeof( output ), &output_length ) ); |
| 2813 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2814 | ASSERT_COMPARE( output, output_length, |
| 2815 | expected_output->x, expected_output->len ); |
| 2816 | |
| 2817 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2818 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2819 | output, output_length ) ); |
| 2820 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2821 | /* Compare with correct hash, multi-part */ |
| 2822 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2823 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2824 | PSA_ASSERT( psa_hash_verify( &operation, output, |
| 2825 | output_length ) ); |
| 2826 | |
| 2827 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2828 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2829 | output, output_length + 1 ), |
| 2830 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2831 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2832 | /* Compare with trailing garbage, multi-part */ |
| 2833 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2834 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2835 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ), |
| 2836 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2837 | |
| 2838 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2839 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2840 | output, output_length - 1 ), |
| 2841 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2842 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2843 | /* Compare with truncated hash, multi-part */ |
| 2844 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2845 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2846 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ), |
| 2847 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2848 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2849 | /* Compare with corrupted value */ |
| 2850 | for( i = 0; i < output_length; i++ ) |
| 2851 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2852 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2853 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2854 | |
| 2855 | /* One-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2856 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2857 | output, output_length ), |
| 2858 | PSA_ERROR_INVALID_SIGNATURE ); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2859 | |
| 2860 | /* Multi-Part */ |
| 2861 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2862 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2863 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length ), |
| 2864 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2865 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2866 | output[i] ^= 1; |
| 2867 | } |
| 2868 | |
| 2869 | exit: |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2870 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2871 | PSA_DONE( ); |
| 2872 | } |
| 2873 | /* END_CASE */ |
| 2874 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2875 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2876 | void hash_bad_order( ) |
| 2877 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2878 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2879 | unsigned char input[] = ""; |
| 2880 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2881 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2882 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2883 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2884 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2885 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2886 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2887 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2888 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2889 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2890 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2891 | /* Call setup twice in a row. */ |
| 2892 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2893 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2894 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2895 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2896 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2897 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2898 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2899 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2900 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2901 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2902 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2903 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2904 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2905 | /* Check that update calls abort on error. */ |
| 2906 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2907 | operation.id = UINT_MAX; |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2908 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
| 2909 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
| 2910 | PSA_ERROR_BAD_STATE ); |
| 2911 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2912 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2913 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2914 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2915 | /* Call update after finish. */ |
| 2916 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2917 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2918 | hash, sizeof( hash ), &hash_len ) ); |
| 2919 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2920 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2921 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2922 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2923 | /* Call verify without calling setup beforehand. */ |
| 2924 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2925 | valid_hash, sizeof( valid_hash ) ), |
| 2926 | PSA_ERROR_BAD_STATE ); |
| 2927 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2928 | |
| 2929 | /* Call verify after finish. */ |
| 2930 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2931 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2932 | hash, sizeof( hash ), &hash_len ) ); |
| 2933 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2934 | valid_hash, sizeof( valid_hash ) ), |
| 2935 | PSA_ERROR_BAD_STATE ); |
| 2936 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2937 | |
| 2938 | /* Call verify twice in a row. */ |
| 2939 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2940 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2941 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2942 | valid_hash, sizeof( valid_hash ) ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2943 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2944 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2945 | valid_hash, sizeof( valid_hash ) ), |
| 2946 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2947 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2948 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2949 | |
| 2950 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2951 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2952 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2953 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2954 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2955 | |
| 2956 | /* Call finish twice in a row. */ |
| 2957 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2958 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2959 | hash, sizeof( hash ), &hash_len ) ); |
| 2960 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2961 | hash, sizeof( hash ), &hash_len ), |
| 2962 | PSA_ERROR_BAD_STATE ); |
| 2963 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2964 | |
| 2965 | /* Call finish after calling verify. */ |
| 2966 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2967 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2968 | valid_hash, sizeof( valid_hash ) ) ); |
| 2969 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2970 | hash, sizeof( hash ), &hash_len ), |
| 2971 | PSA_ERROR_BAD_STATE ); |
| 2972 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2973 | |
| 2974 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2975 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2976 | } |
| 2977 | /* END_CASE */ |
| 2978 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2979 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2980 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2981 | { |
| 2982 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2983 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2984 | * appended to it */ |
| 2985 | unsigned char hash[] = { |
| 2986 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2987 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2988 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2989 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2990 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2991 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2992 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2993 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2994 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2995 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2996 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2997 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2998 | PSA_ERROR_INVALID_SIGNATURE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2999 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 3000 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 3001 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3002 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3003 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3004 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3005 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3006 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3007 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3008 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3009 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3010 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3011 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 3012 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3013 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3014 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3015 | } |
| 3016 | /* END_CASE */ |
| 3017 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3018 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3019 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3020 | { |
| 3021 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3022 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3023 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3024 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3025 | size_t hash_len; |
| 3026 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3027 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3028 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3029 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3030 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3031 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3032 | hash, expected_size - 1, &hash_len ), |
| 3033 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3034 | |
| 3035 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3036 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3037 | } |
| 3038 | /* END_CASE */ |
| 3039 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3040 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3041 | void hash_clone_source_state( ) |
| 3042 | { |
| 3043 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3044 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3045 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3046 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3047 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3048 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3049 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3050 | size_t hash_len; |
| 3051 | |
| 3052 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3053 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 3054 | |
| 3055 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3056 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3057 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3058 | hash, sizeof( hash ), &hash_len ) ); |
| 3059 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3060 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3061 | |
| 3062 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 3063 | PSA_ERROR_BAD_STATE ); |
| 3064 | |
| 3065 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 3066 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 3067 | hash, sizeof( hash ), &hash_len ) ); |
| 3068 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 3069 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3070 | hash, sizeof( hash ), &hash_len ) ); |
| 3071 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 3072 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 3073 | hash, sizeof( hash ), &hash_len ) ); |
| 3074 | |
| 3075 | exit: |
| 3076 | psa_hash_abort( &op_source ); |
| 3077 | psa_hash_abort( &op_init ); |
| 3078 | psa_hash_abort( &op_setup ); |
| 3079 | psa_hash_abort( &op_finished ); |
| 3080 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3081 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3082 | } |
| 3083 | /* END_CASE */ |
| 3084 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3085 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3086 | void hash_clone_target_state( ) |
| 3087 | { |
| 3088 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3089 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3090 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3091 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3092 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3093 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3094 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3095 | size_t hash_len; |
| 3096 | |
| 3097 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3098 | |
| 3099 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3100 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3101 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3102 | hash, sizeof( hash ), &hash_len ) ); |
| 3103 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3104 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3105 | |
| 3106 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 3107 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 3108 | hash, sizeof( hash ), &hash_len ) ); |
| 3109 | |
| 3110 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 3111 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 3112 | PSA_ERROR_BAD_STATE ); |
| 3113 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 3114 | PSA_ERROR_BAD_STATE ); |
| 3115 | |
| 3116 | exit: |
| 3117 | psa_hash_abort( &op_target ); |
| 3118 | psa_hash_abort( &op_init ); |
| 3119 | psa_hash_abort( &op_setup ); |
| 3120 | psa_hash_abort( &op_finished ); |
| 3121 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3122 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3123 | } |
| 3124 | /* END_CASE */ |
| 3125 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3126 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3127 | void mac_operation_init( ) |
| 3128 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3129 | const uint8_t input[1] = { 0 }; |
| 3130 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3131 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3132 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3133 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 3134 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3135 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 3136 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3137 | psa_mac_operation_t zero; |
| 3138 | |
| 3139 | memset( &zero, 0, sizeof( zero ) ); |
| 3140 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3141 | /* A freshly-initialized MAC operation should not be usable. */ |
| 3142 | TEST_EQUAL( psa_mac_update( &func, |
| 3143 | input, sizeof( input ) ), |
| 3144 | PSA_ERROR_BAD_STATE ); |
| 3145 | TEST_EQUAL( psa_mac_update( &init, |
| 3146 | input, sizeof( input ) ), |
| 3147 | PSA_ERROR_BAD_STATE ); |
| 3148 | TEST_EQUAL( psa_mac_update( &zero, |
| 3149 | input, sizeof( input ) ), |
| 3150 | PSA_ERROR_BAD_STATE ); |
| 3151 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3152 | /* A default MAC operation should be abortable without error. */ |
| 3153 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 3154 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 3155 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3156 | } |
| 3157 | /* END_CASE */ |
| 3158 | |
| 3159 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3160 | void mac_setup( int key_type_arg, |
| 3161 | data_t *key, |
| 3162 | int alg_arg, |
| 3163 | int expected_status_arg ) |
| 3164 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3165 | psa_key_type_t key_type = key_type_arg; |
| 3166 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3167 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3168 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3169 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3170 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3171 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3172 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3173 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3174 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3175 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3176 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 3177 | &operation, &status ) ) |
| 3178 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3179 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3180 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3181 | /* The operation object should be reusable. */ |
| 3182 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3183 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3184 | smoke_test_key_data, |
| 3185 | sizeof( smoke_test_key_data ), |
| 3186 | KNOWN_SUPPORTED_MAC_ALG, |
| 3187 | &operation, &status ) ) |
| 3188 | goto exit; |
| 3189 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3190 | #endif |
| 3191 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3192 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3193 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3194 | } |
| 3195 | /* END_CASE */ |
| 3196 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3197 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_HMAC:PSA_WANT_ALG_HMAC:PSA_WANT_ALG_SHA_256 */ |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3198 | void mac_bad_order( ) |
| 3199 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3200 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3201 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3202 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3203 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3204 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3205 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3206 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3207 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3208 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3209 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3210 | size_t sign_mac_length = 0; |
| 3211 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3212 | const uint8_t verify_mac[] = { |
| 3213 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3214 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 3215 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 3216 | |
| 3217 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3218 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3219 | psa_set_key_algorithm( &attributes, alg ); |
| 3220 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3221 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3222 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3223 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3224 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3225 | /* Call update without calling setup beforehand. */ |
| 3226 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3227 | PSA_ERROR_BAD_STATE ); |
| 3228 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3229 | |
| 3230 | /* Call sign finish without calling setup beforehand. */ |
| 3231 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 3232 | &sign_mac_length), |
| 3233 | PSA_ERROR_BAD_STATE ); |
| 3234 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3235 | |
| 3236 | /* Call verify finish without calling setup beforehand. */ |
| 3237 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3238 | verify_mac, sizeof( verify_mac ) ), |
| 3239 | PSA_ERROR_BAD_STATE ); |
| 3240 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3241 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3242 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3243 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3244 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3245 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3246 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3247 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3248 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3249 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3250 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3251 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3252 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3253 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3254 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3255 | sign_mac, sizeof( sign_mac ), |
| 3256 | &sign_mac_length ) ); |
| 3257 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3258 | PSA_ERROR_BAD_STATE ); |
| 3259 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3260 | |
| 3261 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3262 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3263 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3264 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3265 | verify_mac, sizeof( verify_mac ) ) ); |
| 3266 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3267 | PSA_ERROR_BAD_STATE ); |
| 3268 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3269 | |
| 3270 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3271 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3272 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3273 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3274 | sign_mac, sizeof( sign_mac ), |
| 3275 | &sign_mac_length ) ); |
| 3276 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3277 | sign_mac, sizeof( sign_mac ), |
| 3278 | &sign_mac_length ), |
| 3279 | PSA_ERROR_BAD_STATE ); |
| 3280 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3281 | |
| 3282 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3283 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3284 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3285 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3286 | verify_mac, sizeof( verify_mac ) ) ); |
| 3287 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3288 | verify_mac, sizeof( verify_mac ) ), |
| 3289 | PSA_ERROR_BAD_STATE ); |
| 3290 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3291 | |
| 3292 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3293 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3294 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3295 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3296 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3297 | verify_mac, sizeof( verify_mac ) ), |
| 3298 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3299 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3300 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3301 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3302 | |
| 3303 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3304 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3305 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3306 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3307 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3308 | sign_mac, sizeof( sign_mac ), |
| 3309 | &sign_mac_length ), |
| 3310 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3311 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3312 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3313 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3314 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3315 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3316 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3317 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3318 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3319 | } |
| 3320 | /* END_CASE */ |
| 3321 | |
| 3322 | /* BEGIN_CASE */ |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3323 | void mac_sign_verify_multi( int key_type_arg, |
| 3324 | data_t *key_data, |
| 3325 | int alg_arg, |
| 3326 | data_t *input, |
| 3327 | int is_verify, |
| 3328 | data_t *expected_mac ) |
| 3329 | { |
| 3330 | size_t data_part_len = 0; |
| 3331 | |
| 3332 | for( data_part_len = 1; data_part_len <= input->len; data_part_len++ ) |
| 3333 | { |
| 3334 | /* Split data into length(data_part_len) parts. */ |
| 3335 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 3336 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3337 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3338 | alg_arg, |
| 3339 | input, data_part_len, |
| 3340 | expected_mac, |
| 3341 | is_verify, 0 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3342 | break; |
| 3343 | |
| 3344 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3345 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3346 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3347 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3348 | alg_arg, |
| 3349 | input, data_part_len, |
| 3350 | expected_mac, |
| 3351 | is_verify, 1 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3352 | break; |
| 3353 | } |
| 3354 | |
| 3355 | /* Goto is required to silence warnings about unused labels, as we |
| 3356 | * don't actually do any test assertions in this function. */ |
| 3357 | goto exit; |
| 3358 | } |
| 3359 | /* END_CASE */ |
| 3360 | |
| 3361 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3362 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3363 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3364 | int alg_arg, |
| 3365 | data_t *input, |
| 3366 | data_t *expected_mac ) |
| 3367 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3368 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3369 | psa_key_type_t key_type = key_type_arg; |
| 3370 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3371 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3372 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3373 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3374 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3375 | PSA_MAC_LENGTH( key_type, PSA_BYTES_TO_BITS( key_data->len ), alg ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3376 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3377 | const size_t output_sizes_to_test[] = { |
| 3378 | 0, |
| 3379 | 1, |
| 3380 | expected_mac->len - 1, |
| 3381 | expected_mac->len, |
| 3382 | expected_mac->len + 1, |
| 3383 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3384 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3385 | TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3386 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 3387 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3388 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3389 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3390 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3391 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3392 | psa_set_key_algorithm( &attributes, alg ); |
| 3393 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3394 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3395 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3396 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3397 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3398 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 3399 | { |
| 3400 | const size_t output_size = output_sizes_to_test[i]; |
| 3401 | psa_status_t expected_status = |
| 3402 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3403 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3404 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3405 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3406 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3407 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3408 | /* Calculate the MAC, one-shot case. */ |
| 3409 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 3410 | input->x, input->len, |
| 3411 | actual_mac, output_size, &mac_length ), |
| 3412 | expected_status ); |
| 3413 | if( expected_status == PSA_SUCCESS ) |
| 3414 | { |
| 3415 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3416 | actual_mac, mac_length ); |
| 3417 | } |
| 3418 | |
| 3419 | if( output_size > 0 ) |
| 3420 | memset( actual_mac, 0, output_size ); |
| 3421 | |
| 3422 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3423 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3424 | PSA_ASSERT( psa_mac_update( &operation, |
| 3425 | input->x, input->len ) ); |
| 3426 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3427 | actual_mac, output_size, |
| 3428 | &mac_length ), |
| 3429 | expected_status ); |
| 3430 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3431 | |
| 3432 | if( expected_status == PSA_SUCCESS ) |
| 3433 | { |
| 3434 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3435 | actual_mac, mac_length ); |
| 3436 | } |
| 3437 | mbedtls_free( actual_mac ); |
| 3438 | actual_mac = NULL; |
| 3439 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3440 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3441 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3442 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3443 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3444 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3445 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3446 | } |
| 3447 | /* END_CASE */ |
| 3448 | |
| 3449 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3450 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3451 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3452 | int alg_arg, |
| 3453 | data_t *input, |
| 3454 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3455 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3456 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3457 | psa_key_type_t key_type = key_type_arg; |
| 3458 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3459 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3460 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3461 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3462 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3463 | TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3464 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3465 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3466 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3467 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3468 | psa_set_key_algorithm( &attributes, alg ); |
| 3469 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3470 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3471 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3472 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3473 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3474 | /* Verify correct MAC, one-shot case. */ |
| 3475 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 3476 | expected_mac->x, expected_mac->len ) ); |
| 3477 | |
| 3478 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3479 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3480 | PSA_ASSERT( psa_mac_update( &operation, |
| 3481 | input->x, input->len ) ); |
| 3482 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3483 | expected_mac->x, |
| 3484 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3485 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3486 | /* Test a MAC that's too short, one-shot case. */ |
| 3487 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3488 | input->x, input->len, |
| 3489 | expected_mac->x, |
| 3490 | expected_mac->len - 1 ), |
| 3491 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3492 | |
| 3493 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3494 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3495 | PSA_ASSERT( psa_mac_update( &operation, |
| 3496 | input->x, input->len ) ); |
| 3497 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3498 | expected_mac->x, |
| 3499 | expected_mac->len - 1 ), |
| 3500 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3501 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3502 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3503 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 3504 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3505 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3506 | input->x, input->len, |
| 3507 | perturbed_mac, expected_mac->len + 1 ), |
| 3508 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3509 | |
| 3510 | /* Test a MAC that's too long, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3511 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3512 | PSA_ASSERT( psa_mac_update( &operation, |
| 3513 | input->x, input->len ) ); |
| 3514 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3515 | perturbed_mac, |
| 3516 | expected_mac->len + 1 ), |
| 3517 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3518 | |
| 3519 | /* Test changing one byte. */ |
| 3520 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 3521 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3522 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3523 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3524 | |
| 3525 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3526 | input->x, input->len, |
| 3527 | perturbed_mac, expected_mac->len ), |
| 3528 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3529 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3530 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3531 | PSA_ASSERT( psa_mac_update( &operation, |
| 3532 | input->x, input->len ) ); |
| 3533 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3534 | perturbed_mac, |
| 3535 | expected_mac->len ), |
| 3536 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3537 | perturbed_mac[i] ^= 1; |
| 3538 | } |
| 3539 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3540 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3541 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3542 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3543 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3544 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3545 | } |
| 3546 | /* END_CASE */ |
| 3547 | |
| 3548 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3549 | void cipher_operation_init( ) |
| 3550 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3551 | const uint8_t input[1] = { 0 }; |
| 3552 | unsigned char output[1] = { 0 }; |
| 3553 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3554 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3555 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3556 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 3557 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3558 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 3559 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3560 | psa_cipher_operation_t zero; |
| 3561 | |
| 3562 | memset( &zero, 0, sizeof( zero ) ); |
| 3563 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3564 | /* A freshly-initialized cipher operation should not be usable. */ |
| 3565 | TEST_EQUAL( psa_cipher_update( &func, |
| 3566 | input, sizeof( input ), |
| 3567 | output, sizeof( output ), |
| 3568 | &output_length ), |
| 3569 | PSA_ERROR_BAD_STATE ); |
| 3570 | TEST_EQUAL( psa_cipher_update( &init, |
| 3571 | input, sizeof( input ), |
| 3572 | output, sizeof( output ), |
| 3573 | &output_length ), |
| 3574 | PSA_ERROR_BAD_STATE ); |
| 3575 | TEST_EQUAL( psa_cipher_update( &zero, |
| 3576 | input, sizeof( input ), |
| 3577 | output, sizeof( output ), |
| 3578 | &output_length ), |
| 3579 | PSA_ERROR_BAD_STATE ); |
| 3580 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3581 | /* A default cipher operation should be abortable without error. */ |
| 3582 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 3583 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 3584 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3585 | } |
| 3586 | /* END_CASE */ |
| 3587 | |
| 3588 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3589 | void cipher_setup( int key_type_arg, |
| 3590 | data_t *key, |
| 3591 | int alg_arg, |
| 3592 | int expected_status_arg ) |
| 3593 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3594 | psa_key_type_t key_type = key_type_arg; |
| 3595 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3596 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3597 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3598 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3599 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3600 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3601 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3602 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3603 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3604 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3605 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 3606 | &operation, &status ) ) |
| 3607 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3608 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3609 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3610 | /* The operation object should be reusable. */ |
| 3611 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 3612 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3613 | smoke_test_key_data, |
| 3614 | sizeof( smoke_test_key_data ), |
| 3615 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3616 | &operation, &status ) ) |
| 3617 | goto exit; |
| 3618 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3619 | #endif |
| 3620 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3621 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3622 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3623 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3624 | } |
| 3625 | /* END_CASE */ |
| 3626 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3627 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_AES:PSA_WANT_ALG_CBC_PKCS7 */ |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3628 | void cipher_bad_order( ) |
| 3629 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3630 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3631 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3632 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3633 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3634 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3635 | unsigned char iv[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 }; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3636 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3637 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3638 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 3639 | const uint8_t text[] = { |
| 3640 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 3641 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3642 | uint8_t buffer[PSA_BLOCK_CIPHER_BLOCK_LENGTH(PSA_KEY_TYPE_AES)] = { 0 }; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3643 | size_t length = 0; |
| 3644 | |
| 3645 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3646 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3647 | psa_set_key_algorithm( &attributes, alg ); |
| 3648 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3649 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3650 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3651 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3652 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3653 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3654 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3655 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3656 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3657 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3658 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3659 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3660 | |
| 3661 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3662 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3663 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3664 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3665 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3666 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3667 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3668 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3669 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3670 | /* Generate an IV without calling setup beforehand. */ |
| 3671 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3672 | buffer, sizeof( buffer ), |
| 3673 | &length ), |
| 3674 | PSA_ERROR_BAD_STATE ); |
| 3675 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3676 | |
| 3677 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3678 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3679 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3680 | buffer, sizeof( buffer ), |
| 3681 | &length ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3682 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3683 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3684 | buffer, sizeof( buffer ), |
| 3685 | &length ), |
| 3686 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3687 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3688 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3689 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3690 | |
| 3691 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3692 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3693 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3694 | iv, sizeof( iv ) ) ); |
| 3695 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3696 | buffer, sizeof( buffer ), |
| 3697 | &length ), |
| 3698 | PSA_ERROR_BAD_STATE ); |
| 3699 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3700 | |
| 3701 | /* Set an IV without calling setup beforehand. */ |
| 3702 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3703 | iv, sizeof( iv ) ), |
| 3704 | PSA_ERROR_BAD_STATE ); |
| 3705 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3706 | |
| 3707 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3708 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3709 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3710 | iv, sizeof( iv ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3711 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3712 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3713 | iv, sizeof( iv ) ), |
| 3714 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3715 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3716 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3717 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3718 | |
| 3719 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3720 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3721 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3722 | buffer, sizeof( buffer ), |
| 3723 | &length ) ); |
| 3724 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3725 | iv, sizeof( iv ) ), |
| 3726 | PSA_ERROR_BAD_STATE ); |
| 3727 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3728 | |
| 3729 | /* Call update without calling setup beforehand. */ |
| 3730 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3731 | text, sizeof( text ), |
| 3732 | buffer, sizeof( buffer ), |
| 3733 | &length ), |
| 3734 | PSA_ERROR_BAD_STATE ); |
| 3735 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3736 | |
| 3737 | /* Call update without an IV where an IV is required. */ |
Dave Rodgman | 095dadc | 2021-06-23 12:48:52 +0100 | [diff] [blame] | 3738 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3739 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3740 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3741 | text, sizeof( text ), |
| 3742 | buffer, sizeof( buffer ), |
| 3743 | &length ), |
| 3744 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3745 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3746 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3747 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3748 | |
| 3749 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3750 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3751 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3752 | iv, sizeof( iv ) ) ); |
| 3753 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3754 | buffer, sizeof( buffer ), &length ) ); |
| 3755 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3756 | text, sizeof( text ), |
| 3757 | buffer, sizeof( buffer ), |
| 3758 | &length ), |
| 3759 | PSA_ERROR_BAD_STATE ); |
| 3760 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3761 | |
| 3762 | /* Call finish without calling setup beforehand. */ |
| 3763 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3764 | buffer, sizeof( buffer ), &length ), |
| 3765 | PSA_ERROR_BAD_STATE ); |
| 3766 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3767 | |
| 3768 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3769 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3770 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3771 | * for cipher modes with padding. */ |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3772 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3773 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3774 | buffer, sizeof( buffer ), &length ), |
| 3775 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3776 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3777 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3778 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3779 | |
| 3780 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3781 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3782 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3783 | iv, sizeof( iv ) ) ); |
| 3784 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3785 | buffer, sizeof( buffer ), &length ) ); |
| 3786 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3787 | buffer, sizeof( buffer ), &length ), |
| 3788 | PSA_ERROR_BAD_STATE ); |
| 3789 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3790 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3791 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3792 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3793 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3794 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3795 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3796 | } |
| 3797 | /* END_CASE */ |
| 3798 | |
| 3799 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3800 | void cipher_encrypt_fail( int alg_arg, |
| 3801 | int key_type_arg, |
| 3802 | data_t *key_data, |
| 3803 | data_t *input, |
| 3804 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3805 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3806 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3807 | psa_status_t status; |
| 3808 | psa_key_type_t key_type = key_type_arg; |
| 3809 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3810 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3811 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0}; |
| 3812 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3813 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3814 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3815 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3816 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3817 | size_t function_output_length; |
| 3818 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3819 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3820 | |
| 3821 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 3822 | { |
| 3823 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3824 | |
| 3825 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3826 | psa_set_key_algorithm( &attributes, alg ); |
| 3827 | psa_set_key_type( &attributes, key_type ); |
| 3828 | |
| 3829 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3830 | input->len ); |
| 3831 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3832 | |
| 3833 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3834 | &key ) ); |
| 3835 | } |
| 3836 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3837 | /* Encrypt, one-shot */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3838 | status = psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 3839 | output_buffer_size, &output_length ); |
| 3840 | |
| 3841 | TEST_EQUAL( status, expected_status ); |
| 3842 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3843 | /* Encrypt, multi-part */ |
| 3844 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 3845 | if( status == PSA_SUCCESS ) |
| 3846 | { |
| 3847 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3848 | { |
| 3849 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3850 | iv, iv_size, |
| 3851 | &iv_length ) ); |
| 3852 | } |
| 3853 | |
| 3854 | status = psa_cipher_update( &operation, input->x, input->len, |
| 3855 | output, output_buffer_size, |
| 3856 | &function_output_length ); |
| 3857 | if( status == PSA_SUCCESS ) |
| 3858 | { |
| 3859 | output_length += function_output_length; |
| 3860 | |
| 3861 | status = psa_cipher_finish( &operation, output + output_length, |
| 3862 | output_buffer_size - output_length, |
| 3863 | &function_output_length ); |
| 3864 | |
| 3865 | TEST_EQUAL( status, expected_status ); |
| 3866 | } |
| 3867 | else |
| 3868 | { |
| 3869 | TEST_EQUAL( status, expected_status ); |
| 3870 | } |
| 3871 | } |
| 3872 | else |
| 3873 | { |
| 3874 | TEST_EQUAL( status, expected_status ); |
| 3875 | } |
| 3876 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3877 | exit: |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3878 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3879 | mbedtls_free( output ); |
| 3880 | psa_destroy_key( key ); |
| 3881 | PSA_DONE( ); |
| 3882 | } |
| 3883 | /* END_CASE */ |
| 3884 | |
| 3885 | /* BEGIN_CASE */ |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3886 | void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data, |
| 3887 | data_t *input, int iv_length, |
| 3888 | int expected_result ) |
| 3889 | { |
| 3890 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3891 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3892 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3893 | size_t output_buffer_size = 0; |
| 3894 | unsigned char *output = NULL; |
| 3895 | |
| 3896 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3897 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3898 | |
| 3899 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3900 | |
| 3901 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3902 | psa_set_key_algorithm( &attributes, alg ); |
| 3903 | psa_set_key_type( &attributes, key_type ); |
| 3904 | |
| 3905 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3906 | &key ) ); |
| 3907 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3908 | TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output, |
| 3909 | iv_length ) ); |
| 3910 | |
| 3911 | exit: |
| 3912 | psa_cipher_abort( &operation ); |
| 3913 | mbedtls_free( output ); |
| 3914 | psa_destroy_key( key ); |
| 3915 | PSA_DONE( ); |
| 3916 | } |
| 3917 | /* END_CASE */ |
| 3918 | |
| 3919 | /* BEGIN_CASE */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3920 | void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data, |
| 3921 | data_t *plaintext, data_t *ciphertext ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3922 | { |
| 3923 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3924 | psa_key_type_t key_type = key_type_arg; |
| 3925 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3926 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3927 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3928 | unsigned char *output = NULL; |
| 3929 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3930 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3931 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3932 | |
| 3933 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3934 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3935 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3936 | TEST_LE_U( ciphertext->len, |
| 3937 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) ); |
| 3938 | TEST_LE_U( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ), |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3939 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3940 | TEST_LE_U( plaintext->len, |
| 3941 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) ); |
| 3942 | TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ), |
| 3943 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3944 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3945 | |
| 3946 | /* Set up key and output buffer */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3947 | psa_set_key_usage_flags( &attributes, |
| 3948 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3949 | psa_set_key_algorithm( &attributes, alg ); |
| 3950 | psa_set_key_type( &attributes, key_type ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3951 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3952 | &key ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3953 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3954 | plaintext->len ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3955 | ASSERT_ALLOC( output, output_buffer_size ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3956 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3957 | /* set_iv() is not allowed */ |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3958 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3959 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
| 3960 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3961 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3962 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3963 | PSA_ERROR_BAD_STATE ); |
| 3964 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3965 | /* generate_iv() is not allowed */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3966 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3967 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3968 | &length ), |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3969 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3970 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3971 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3972 | &length ), |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3973 | PSA_ERROR_BAD_STATE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3974 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3975 | /* Multipart encryption */ |
| 3976 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3977 | output_length = 0; |
| 3978 | length = ~0; |
| 3979 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3980 | plaintext->x, plaintext->len, |
| 3981 | output, output_buffer_size, |
| 3982 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3983 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3984 | output_length += length; |
| 3985 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3986 | output + output_length, |
| 3987 | output_buffer_size - output_length, |
| 3988 | &length ) ); |
| 3989 | output_length += length; |
| 3990 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3991 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3992 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3993 | /* Multipart encryption */ |
| 3994 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3995 | output_length = 0; |
| 3996 | length = ~0; |
| 3997 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3998 | ciphertext->x, ciphertext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3999 | output, output_buffer_size, |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4000 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4001 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4002 | output_length += length; |
| 4003 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 4004 | output + output_length, |
| 4005 | output_buffer_size - output_length, |
| 4006 | &length ) ); |
| 4007 | output_length += length; |
| 4008 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
| 4009 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4010 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4011 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4012 | output_length = ~0; |
| 4013 | PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len, |
| 4014 | output, output_buffer_size, |
| 4015 | &output_length ) ); |
| 4016 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
| 4017 | output, output_length ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4018 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4019 | /* One-shot decryption */ |
| 4020 | output_length = ~0; |
| 4021 | PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len, |
| 4022 | output, output_buffer_size, |
| 4023 | &output_length ) ); |
| 4024 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4025 | output, output_length ); |
| 4026 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4027 | exit: |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4028 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4029 | mbedtls_free( output ); |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4030 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4031 | psa_destroy_key( key ); |
| 4032 | PSA_DONE( ); |
| 4033 | } |
| 4034 | /* END_CASE */ |
| 4035 | |
| 4036 | /* BEGIN_CASE */ |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4037 | void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data ) |
| 4038 | { |
| 4039 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4040 | psa_algorithm_t alg = alg_arg; |
| 4041 | psa_key_type_t key_type = key_type_arg; |
| 4042 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4043 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4044 | psa_status_t status; |
| 4045 | |
| 4046 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4047 | |
| 4048 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4049 | psa_set_key_algorithm( &attributes, alg ); |
| 4050 | psa_set_key_type( &attributes, key_type ); |
| 4051 | |
| 4052 | /* Usage of either of these two size macros would cause divide by zero |
| 4053 | * with incorrect key types previously. Input length should be irrelevant |
| 4054 | * here. */ |
| 4055 | TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ), |
| 4056 | 0 ); |
| 4057 | TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 ); |
| 4058 | |
| 4059 | |
| 4060 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4061 | &key ) ); |
| 4062 | |
| 4063 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4064 | * Encrypt or decrypt will end up in the same place. */ |
| 4065 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 4066 | |
| 4067 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 4068 | |
| 4069 | exit: |
| 4070 | psa_cipher_abort( &operation ); |
| 4071 | psa_destroy_key( key ); |
| 4072 | PSA_DONE( ); |
| 4073 | } |
| 4074 | /* END_CASE */ |
| 4075 | |
| 4076 | /* BEGIN_CASE */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4077 | void cipher_encrypt_validation( int alg_arg, |
| 4078 | int key_type_arg, |
| 4079 | data_t *key_data, |
| 4080 | data_t *input ) |
| 4081 | { |
| 4082 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4083 | psa_key_type_t key_type = key_type_arg; |
| 4084 | psa_algorithm_t alg = alg_arg; |
| 4085 | size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); |
| 4086 | unsigned char *output1 = NULL; |
| 4087 | size_t output1_buffer_size = 0; |
| 4088 | size_t output1_length = 0; |
| 4089 | unsigned char *output2 = NULL; |
| 4090 | size_t output2_buffer_size = 0; |
| 4091 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4092 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4093 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4094 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4095 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4096 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4097 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4098 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4099 | psa_set_key_algorithm( &attributes, alg ); |
| 4100 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4101 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4102 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 4103 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4104 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4105 | ASSERT_ALLOC( output1, output1_buffer_size ); |
| 4106 | ASSERT_ALLOC( output2, output2_buffer_size ); |
| 4107 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4108 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4109 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4110 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4111 | /* The one-shot cipher encryption uses generated iv so validating |
| 4112 | the output is not possible. Validating with multipart encryption. */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4113 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, |
| 4114 | output1_buffer_size, &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4115 | TEST_LE_U( output1_length, |
| 4116 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4117 | TEST_LE_U( output1_length, |
| 4118 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4119 | |
| 4120 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 4121 | PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4122 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4123 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4124 | input->x, input->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4125 | output2, output2_buffer_size, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4126 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4127 | TEST_LE_U( function_output_length, |
| 4128 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4129 | TEST_LE_U( function_output_length, |
| 4130 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4131 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4132 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4133 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 4134 | output2 + output2_length, |
| 4135 | output2_buffer_size - output2_length, |
| 4136 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4137 | TEST_LE_U( function_output_length, |
| 4138 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4139 | TEST_LE_U( function_output_length, |
| 4140 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4141 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4142 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4143 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4144 | ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, |
| 4145 | output2, output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4146 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4147 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4148 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4149 | mbedtls_free( output1 ); |
| 4150 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4151 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4152 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4153 | } |
| 4154 | /* END_CASE */ |
| 4155 | |
| 4156 | /* BEGIN_CASE */ |
| 4157 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4158 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4159 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4160 | int first_part_size_arg, |
| 4161 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4162 | data_t *expected_output, |
| 4163 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4164 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4165 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4166 | psa_key_type_t key_type = key_type_arg; |
| 4167 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4168 | psa_status_t status; |
| 4169 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4170 | size_t first_part_size = first_part_size_arg; |
| 4171 | size_t output1_length = output1_length_arg; |
| 4172 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4173 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4174 | size_t output_buffer_size = 0; |
| 4175 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4176 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4177 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4178 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4179 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4180 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4181 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4182 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4183 | psa_set_key_algorithm( &attributes, alg ); |
| 4184 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4185 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4186 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4187 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4188 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4189 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4190 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4191 | if( iv->len > 0 ) |
| 4192 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4193 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4194 | } |
| 4195 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4196 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4197 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4198 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4199 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4200 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4201 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 4202 | output, output_buffer_size, |
| 4203 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4204 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4205 | TEST_LE_U( function_output_length, |
| 4206 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4207 | TEST_LE_U( function_output_length, |
| 4208 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4209 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4210 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4211 | if( first_part_size < input->len ) |
| 4212 | { |
| 4213 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4214 | input->x + first_part_size, |
| 4215 | input->len - first_part_size, |
| 4216 | ( output_buffer_size == 0 ? NULL : |
| 4217 | output + total_output_length ), |
| 4218 | output_buffer_size - total_output_length, |
| 4219 | &function_output_length ) ); |
| 4220 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4221 | TEST_LE_U( function_output_length, |
| 4222 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4223 | alg, |
| 4224 | input->len - first_part_size ) ); |
| 4225 | TEST_LE_U( function_output_length, |
| 4226 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4227 | total_output_length += function_output_length; |
| 4228 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4229 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4230 | status = psa_cipher_finish( &operation, |
| 4231 | ( output_buffer_size == 0 ? NULL : |
| 4232 | output + total_output_length ), |
| 4233 | output_buffer_size - total_output_length, |
| 4234 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4235 | TEST_LE_U( function_output_length, |
| 4236 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4237 | TEST_LE_U( function_output_length, |
| 4238 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4239 | total_output_length += function_output_length; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4240 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4241 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4242 | if( expected_status == PSA_SUCCESS ) |
| 4243 | { |
| 4244 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4245 | |
| 4246 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4247 | output, total_output_length ); |
| 4248 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4249 | |
| 4250 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4251 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4252 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4253 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4254 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4255 | } |
| 4256 | /* END_CASE */ |
| 4257 | |
| 4258 | /* BEGIN_CASE */ |
| 4259 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4260 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4261 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4262 | int first_part_size_arg, |
| 4263 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4264 | data_t *expected_output, |
| 4265 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4266 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4267 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4268 | psa_key_type_t key_type = key_type_arg; |
| 4269 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4270 | psa_status_t status; |
| 4271 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4272 | size_t first_part_size = first_part_size_arg; |
| 4273 | size_t output1_length = output1_length_arg; |
| 4274 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4275 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4276 | size_t output_buffer_size = 0; |
| 4277 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4278 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4279 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4280 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4281 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4282 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4283 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4284 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4285 | psa_set_key_algorithm( &attributes, alg ); |
| 4286 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4287 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4288 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4289 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4290 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4291 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4292 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4293 | if( iv->len > 0 ) |
| 4294 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4295 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4296 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4297 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4298 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4299 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4300 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4301 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4302 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4303 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4304 | input->x, first_part_size, |
| 4305 | output, output_buffer_size, |
| 4306 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4307 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4308 | TEST_LE_U( function_output_length, |
| 4309 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4310 | TEST_LE_U( function_output_length, |
| 4311 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4312 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4313 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4314 | if( first_part_size < input->len ) |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4315 | { |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4316 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4317 | input->x + first_part_size, |
| 4318 | input->len - first_part_size, |
| 4319 | ( output_buffer_size == 0 ? NULL : |
| 4320 | output + total_output_length ), |
| 4321 | output_buffer_size - total_output_length, |
| 4322 | &function_output_length ) ); |
| 4323 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4324 | TEST_LE_U( function_output_length, |
| 4325 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4326 | alg, |
| 4327 | input->len - first_part_size ) ); |
| 4328 | TEST_LE_U( function_output_length, |
| 4329 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4330 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4331 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4332 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4333 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 4334 | ( output_buffer_size == 0 ? NULL : |
| 4335 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 4336 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4337 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4338 | TEST_LE_U( function_output_length, |
| 4339 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4340 | TEST_LE_U( function_output_length, |
| 4341 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4342 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4343 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4344 | |
| 4345 | if( expected_status == PSA_SUCCESS ) |
| 4346 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4347 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4348 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4349 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4350 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4351 | } |
| 4352 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4353 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4354 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4355 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4356 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4357 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4358 | } |
| 4359 | /* END_CASE */ |
| 4360 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4361 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 4362 | void cipher_decrypt_fail( int alg_arg, |
| 4363 | int key_type_arg, |
| 4364 | data_t *key_data, |
| 4365 | data_t *iv, |
| 4366 | data_t *input_arg, |
| 4367 | int expected_status_arg ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4368 | { |
| 4369 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4370 | psa_status_t status; |
| 4371 | psa_key_type_t key_type = key_type_arg; |
| 4372 | psa_algorithm_t alg = alg_arg; |
| 4373 | psa_status_t expected_status = expected_status_arg; |
| 4374 | unsigned char *input = NULL; |
| 4375 | size_t input_buffer_size = 0; |
| 4376 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4377 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4378 | size_t output_buffer_size = 0; |
| 4379 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4380 | size_t function_output_length; |
| 4381 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4382 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4383 | |
| 4384 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 4385 | { |
| 4386 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4387 | |
| 4388 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4389 | psa_set_key_algorithm( &attributes, alg ); |
| 4390 | psa_set_key_type( &attributes, key_type ); |
| 4391 | |
| 4392 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4393 | &key ) ); |
| 4394 | } |
| 4395 | |
| 4396 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4397 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4398 | if ( input_buffer_size > 0 ) |
| 4399 | { |
| 4400 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4401 | memcpy( input, iv->x, iv->len ); |
| 4402 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4403 | } |
| 4404 | |
| 4405 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4406 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4407 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4408 | /* Decrypt, one-short */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4409 | status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4410 | output_buffer_size, &output_length ); |
| 4411 | TEST_EQUAL( status, expected_status ); |
| 4412 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4413 | /* Decrypt, multi-part */ |
| 4414 | status = psa_cipher_decrypt_setup( &operation, key, alg ); |
| 4415 | if( status == PSA_SUCCESS ) |
| 4416 | { |
| 4417 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 4418 | input_arg->len ) + |
| 4419 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4420 | ASSERT_ALLOC( output_multi, output_buffer_size ); |
| 4421 | |
| 4422 | if( iv->len > 0 ) |
| 4423 | { |
| 4424 | status = psa_cipher_set_iv( &operation, iv->x, iv->len ); |
| 4425 | |
| 4426 | if( status != PSA_SUCCESS ) |
| 4427 | TEST_EQUAL( status, expected_status ); |
| 4428 | } |
| 4429 | |
| 4430 | if( status == PSA_SUCCESS ) |
| 4431 | { |
| 4432 | status = psa_cipher_update( &operation, |
| 4433 | input_arg->x, input_arg->len, |
| 4434 | output_multi, output_buffer_size, |
| 4435 | &function_output_length ); |
| 4436 | if( status == PSA_SUCCESS ) |
| 4437 | { |
| 4438 | output_length = function_output_length; |
| 4439 | |
| 4440 | status = psa_cipher_finish( &operation, |
| 4441 | output_multi + output_length, |
| 4442 | output_buffer_size - output_length, |
| 4443 | &function_output_length ); |
| 4444 | |
| 4445 | TEST_EQUAL( status, expected_status ); |
| 4446 | } |
| 4447 | else |
| 4448 | { |
| 4449 | TEST_EQUAL( status, expected_status ); |
| 4450 | } |
| 4451 | } |
| 4452 | else |
| 4453 | { |
| 4454 | TEST_EQUAL( status, expected_status ); |
| 4455 | } |
| 4456 | } |
| 4457 | else |
| 4458 | { |
| 4459 | TEST_EQUAL( status, expected_status ); |
| 4460 | } |
| 4461 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4462 | exit: |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4463 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4464 | mbedtls_free( input ); |
| 4465 | mbedtls_free( output ); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4466 | mbedtls_free( output_multi ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4467 | psa_destroy_key( key ); |
| 4468 | PSA_DONE( ); |
| 4469 | } |
| 4470 | /* END_CASE */ |
| 4471 | |
| 4472 | /* BEGIN_CASE */ |
| 4473 | void cipher_decrypt( int alg_arg, |
| 4474 | int key_type_arg, |
| 4475 | data_t *key_data, |
| 4476 | data_t *iv, |
| 4477 | data_t *input_arg, |
| 4478 | data_t *expected_output ) |
| 4479 | { |
| 4480 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4481 | psa_key_type_t key_type = key_type_arg; |
| 4482 | psa_algorithm_t alg = alg_arg; |
| 4483 | unsigned char *input = NULL; |
| 4484 | size_t input_buffer_size = 0; |
| 4485 | unsigned char *output = NULL; |
| 4486 | size_t output_buffer_size = 0; |
| 4487 | size_t output_length = 0; |
| 4488 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4489 | |
| 4490 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4491 | |
| 4492 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4493 | psa_set_key_algorithm( &attributes, alg ); |
| 4494 | psa_set_key_type( &attributes, key_type ); |
| 4495 | |
| 4496 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4497 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4498 | if ( input_buffer_size > 0 ) |
| 4499 | { |
| 4500 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4501 | memcpy( input, iv->x, iv->len ); |
| 4502 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4503 | } |
| 4504 | |
| 4505 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4506 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4507 | |
| 4508 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4509 | &key ) ); |
| 4510 | |
| 4511 | PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4512 | output_buffer_size, &output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4513 | TEST_LE_U( output_length, |
| 4514 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) ); |
| 4515 | TEST_LE_U( output_length, |
| 4516 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4517 | |
| 4518 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4519 | output, output_length ); |
| 4520 | exit: |
| 4521 | mbedtls_free( input ); |
| 4522 | mbedtls_free( output ); |
| 4523 | psa_destroy_key( key ); |
| 4524 | PSA_DONE( ); |
| 4525 | } |
| 4526 | /* END_CASE */ |
| 4527 | |
| 4528 | /* BEGIN_CASE */ |
| 4529 | void cipher_verify_output( int alg_arg, |
| 4530 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4531 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4532 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4533 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4534 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4535 | psa_key_type_t key_type = key_type_arg; |
| 4536 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4537 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4538 | size_t output1_size = 0; |
| 4539 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4540 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4541 | size_t output2_size = 0; |
| 4542 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4543 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4544 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4545 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4546 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4547 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4548 | psa_set_key_algorithm( &attributes, alg ); |
| 4549 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4550 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4551 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4552 | &key ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4553 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4554 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4555 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4556 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, |
| 4557 | output1, output1_size, |
| 4558 | &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4559 | TEST_LE_U( output1_length, |
| 4560 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4561 | TEST_LE_U( output1_length, |
| 4562 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4563 | |
| 4564 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4565 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4566 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4567 | PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length, |
| 4568 | output2, output2_size, |
| 4569 | &output2_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4570 | TEST_LE_U( output2_length, |
| 4571 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4572 | TEST_LE_U( output2_length, |
| 4573 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4574 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4575 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4576 | |
| 4577 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4578 | mbedtls_free( output1 ); |
| 4579 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4580 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4581 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4582 | } |
| 4583 | /* END_CASE */ |
| 4584 | |
| 4585 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4586 | void cipher_verify_output_multipart( int alg_arg, |
| 4587 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4588 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4589 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4590 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4591 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4592 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4593 | psa_key_type_t key_type = key_type_arg; |
| 4594 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4595 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4596 | unsigned char iv[16] = {0}; |
| 4597 | size_t iv_size = 16; |
| 4598 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4599 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4600 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4601 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4602 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4603 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4604 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4605 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4606 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4607 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4608 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4609 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4610 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4611 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4612 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4613 | psa_set_key_algorithm( &attributes, alg ); |
| 4614 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4615 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4616 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4617 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4618 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4619 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 4620 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4621 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4622 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 4623 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4624 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 4625 | iv, iv_size, |
| 4626 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4627 | } |
| 4628 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4629 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4630 | TEST_LE_U( output1_buffer_size, |
| 4631 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4632 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4633 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4634 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4635 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4636 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 4637 | output1, output1_buffer_size, |
| 4638 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4639 | TEST_LE_U( function_output_length, |
| 4640 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4641 | TEST_LE_U( function_output_length, |
| 4642 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4643 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4644 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4645 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 4646 | input->x + first_part_size, |
| 4647 | input->len - first_part_size, |
| 4648 | output1, output1_buffer_size, |
| 4649 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4650 | TEST_LE_U( function_output_length, |
| 4651 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4652 | alg, |
| 4653 | input->len - first_part_size ) ); |
| 4654 | TEST_LE_U( function_output_length, |
| 4655 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4656 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4657 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4658 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 4659 | output1 + output1_length, |
| 4660 | output1_buffer_size - output1_length, |
| 4661 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4662 | TEST_LE_U( function_output_length, |
| 4663 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4664 | TEST_LE_U( function_output_length, |
| 4665 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4666 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4667 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4668 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4669 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4670 | output2_buffer_size = output1_length; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4671 | TEST_LE_U( output2_buffer_size, |
| 4672 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4673 | TEST_LE_U( output2_buffer_size, |
| 4674 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4675 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4676 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4677 | if( iv_length > 0 ) |
| 4678 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4679 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 4680 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4681 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4682 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4683 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 4684 | output2, output2_buffer_size, |
| 4685 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4686 | TEST_LE_U( function_output_length, |
| 4687 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4688 | TEST_LE_U( function_output_length, |
| 4689 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4690 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4691 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4692 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 4693 | output1 + first_part_size, |
| 4694 | output1_length - first_part_size, |
| 4695 | output2, output2_buffer_size, |
| 4696 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4697 | TEST_LE_U( function_output_length, |
| 4698 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4699 | alg, |
| 4700 | output1_length - first_part_size ) ); |
| 4701 | TEST_LE_U( function_output_length, |
| 4702 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4703 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4704 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4705 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 4706 | output2 + output2_length, |
| 4707 | output2_buffer_size - output2_length, |
| 4708 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4709 | TEST_LE_U( function_output_length, |
| 4710 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4711 | TEST_LE_U( function_output_length, |
| 4712 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4713 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4714 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4715 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4716 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4717 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4718 | |
| 4719 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4720 | psa_cipher_abort( &operation1 ); |
| 4721 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4722 | mbedtls_free( output1 ); |
| 4723 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4724 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4725 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4726 | } |
| 4727 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4728 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4729 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4730 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4731 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4732 | data_t *nonce, |
| 4733 | data_t *additional_data, |
| 4734 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4735 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4736 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4737 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4738 | psa_key_type_t key_type = key_type_arg; |
| 4739 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4740 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4741 | unsigned char *output_data = NULL; |
| 4742 | size_t output_size = 0; |
| 4743 | size_t output_length = 0; |
| 4744 | unsigned char *output_data2 = NULL; |
| 4745 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4746 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4747 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4748 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4749 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4750 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4751 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4752 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4753 | psa_set_key_algorithm( &attributes, alg ); |
| 4754 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4755 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4756 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4757 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4758 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4759 | key_bits = psa_get_key_bits( &attributes ); |
| 4760 | |
| 4761 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4762 | alg ); |
| 4763 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4764 | * should be exact. */ |
| 4765 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4766 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4767 | { |
| 4768 | TEST_EQUAL( output_size, |
| 4769 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4770 | TEST_LE_U( output_size, |
| 4771 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4772 | } |
| 4773 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4774 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4775 | status = psa_aead_encrypt( key, alg, |
| 4776 | nonce->x, nonce->len, |
| 4777 | additional_data->x, |
| 4778 | additional_data->len, |
| 4779 | input_data->x, input_data->len, |
| 4780 | output_data, output_size, |
| 4781 | &output_length ); |
| 4782 | |
| 4783 | /* If the operation is not supported, just skip and not fail in case the |
| 4784 | * encryption involves a common limitation of cryptography hardwares and |
| 4785 | * an alternative implementation. */ |
| 4786 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4787 | { |
| 4788 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4789 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4790 | } |
| 4791 | |
| 4792 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4793 | |
| 4794 | if( PSA_SUCCESS == expected_result ) |
| 4795 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4796 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4797 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4798 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4799 | * should be exact. */ |
| 4800 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4801 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4802 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4803 | TEST_LE_U( input_data->len, |
| 4804 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4805 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4806 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4807 | nonce->x, nonce->len, |
| 4808 | additional_data->x, |
| 4809 | additional_data->len, |
| 4810 | output_data, output_length, |
| 4811 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4812 | &output_length2 ), |
| 4813 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4814 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4815 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4816 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4817 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4818 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4819 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4820 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4821 | mbedtls_free( output_data ); |
| 4822 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4823 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4824 | } |
| 4825 | /* END_CASE */ |
| 4826 | |
| 4827 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4828 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 4829 | int alg_arg, |
| 4830 | data_t *nonce, |
| 4831 | data_t *additional_data, |
| 4832 | data_t *input_data, |
| 4833 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4834 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4835 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4836 | psa_key_type_t key_type = key_type_arg; |
| 4837 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4838 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4839 | unsigned char *output_data = NULL; |
| 4840 | size_t output_size = 0; |
| 4841 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4842 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4843 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4844 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4845 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4846 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4847 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4848 | psa_set_key_algorithm( &attributes, alg ); |
| 4849 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4850 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4851 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4852 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4853 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4854 | key_bits = psa_get_key_bits( &attributes ); |
| 4855 | |
| 4856 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4857 | alg ); |
| 4858 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4859 | * should be exact. */ |
| 4860 | TEST_EQUAL( output_size, |
| 4861 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4862 | TEST_LE_U( output_size, |
| 4863 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4864 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4865 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4866 | status = psa_aead_encrypt( key, alg, |
| 4867 | nonce->x, nonce->len, |
| 4868 | additional_data->x, additional_data->len, |
| 4869 | input_data->x, input_data->len, |
| 4870 | output_data, output_size, |
| 4871 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4872 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4873 | /* If the operation is not supported, just skip and not fail in case the |
| 4874 | * encryption involves a common limitation of cryptography hardwares and |
| 4875 | * an alternative implementation. */ |
| 4876 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4877 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4878 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4879 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4880 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4881 | |
| 4882 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4883 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 4884 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4885 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4886 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4887 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4888 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4889 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4890 | } |
| 4891 | /* END_CASE */ |
| 4892 | |
| 4893 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4894 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 4895 | int alg_arg, |
| 4896 | data_t *nonce, |
| 4897 | data_t *additional_data, |
| 4898 | data_t *input_data, |
| 4899 | data_t *expected_data, |
| 4900 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4901 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4902 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4903 | psa_key_type_t key_type = key_type_arg; |
| 4904 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4905 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4906 | unsigned char *output_data = NULL; |
| 4907 | size_t output_size = 0; |
| 4908 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4909 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4910 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4911 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4912 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4913 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4914 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4915 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4916 | psa_set_key_algorithm( &attributes, alg ); |
| 4917 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4918 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4919 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4920 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4921 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4922 | key_bits = psa_get_key_bits( &attributes ); |
| 4923 | |
| 4924 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4925 | alg ); |
| 4926 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4927 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4928 | { |
| 4929 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4930 | * should be exact. */ |
| 4931 | TEST_EQUAL( output_size, |
| 4932 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4933 | TEST_LE_U( output_size, |
| 4934 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4935 | } |
| 4936 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4937 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4938 | status = psa_aead_decrypt( key, alg, |
| 4939 | nonce->x, nonce->len, |
| 4940 | additional_data->x, |
| 4941 | additional_data->len, |
| 4942 | input_data->x, input_data->len, |
| 4943 | output_data, output_size, |
| 4944 | &output_length ); |
| 4945 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4946 | /* If the operation is not supported, just skip and not fail in case the |
| 4947 | * decryption involves a common limitation of cryptography hardwares and |
| 4948 | * an alternative implementation. */ |
| 4949 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4950 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4951 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4952 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4953 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4954 | |
| 4955 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4956 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4957 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4958 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4959 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4960 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4961 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4962 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4963 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4964 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4965 | } |
| 4966 | /* END_CASE */ |
| 4967 | |
| 4968 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4969 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 4970 | int alg_arg, |
| 4971 | data_t *nonce, |
| 4972 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4973 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 4974 | int do_set_lengths, |
| 4975 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4976 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4977 | size_t ad_part_len = 0; |
| 4978 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4979 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4980 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4981 | for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4982 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4983 | mbedtls_test_set_step( ad_part_len ); |
| 4984 | |
| 4985 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4986 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4987 | if( ad_part_len & 0x01 ) |
| 4988 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4989 | else |
| 4990 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4991 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4992 | |
| 4993 | /* Split ad into length(ad_part_len) parts. */ |
| 4994 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4995 | alg_arg, nonce, |
| 4996 | additional_data, |
| 4997 | ad_part_len, |
| 4998 | input_data, -1, |
| 4999 | set_lengths_method, |
| 5000 | expected_output, |
| 5001 | 1, 0 ) ) |
| 5002 | break; |
| 5003 | |
| 5004 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5005 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 5006 | |
| 5007 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5008 | alg_arg, nonce, |
| 5009 | additional_data, |
| 5010 | ad_part_len, |
| 5011 | input_data, -1, |
| 5012 | set_lengths_method, |
| 5013 | expected_output, |
| 5014 | 1, 1 ) ) |
| 5015 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5016 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5017 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5018 | for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5019 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5020 | /* Split data into length(data_part_len) parts. */ |
| 5021 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 5022 | |
| 5023 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5024 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5025 | if( data_part_len & 0x01 ) |
| 5026 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5027 | else |
| 5028 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5029 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5030 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5031 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5032 | alg_arg, nonce, |
| 5033 | additional_data, -1, |
| 5034 | input_data, data_part_len, |
| 5035 | set_lengths_method, |
| 5036 | expected_output, |
| 5037 | 1, 0 ) ) |
| 5038 | break; |
| 5039 | |
| 5040 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 5041 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 5042 | |
| 5043 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5044 | alg_arg, nonce, |
| 5045 | additional_data, -1, |
| 5046 | input_data, data_part_len, |
| 5047 | set_lengths_method, |
| 5048 | expected_output, |
| 5049 | 1, 1 ) ) |
| 5050 | break; |
| 5051 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5052 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5053 | /* Goto is required to silence warnings about unused labels, as we |
| 5054 | * don't actually do any test assertions in this function. */ |
| 5055 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5056 | } |
| 5057 | /* END_CASE */ |
| 5058 | |
| 5059 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5060 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 5061 | int alg_arg, |
| 5062 | data_t *nonce, |
| 5063 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5064 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 5065 | int do_set_lengths, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5066 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5067 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5068 | size_t ad_part_len = 0; |
| 5069 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5070 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5071 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5072 | for( ad_part_len = 1; ad_part_len <= additional_data->len; ad_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5073 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5074 | /* Split ad into length(ad_part_len) parts. */ |
| 5075 | mbedtls_test_set_step( ad_part_len ); |
| 5076 | |
| 5077 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5078 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5079 | if( ad_part_len & 0x01 ) |
| 5080 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5081 | else |
| 5082 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5083 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5084 | |
| 5085 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5086 | alg_arg, nonce, |
| 5087 | additional_data, |
| 5088 | ad_part_len, |
| 5089 | input_data, -1, |
| 5090 | set_lengths_method, |
| 5091 | expected_output, |
| 5092 | 0, 0 ) ) |
| 5093 | break; |
| 5094 | |
| 5095 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5096 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 5097 | |
| 5098 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5099 | alg_arg, nonce, |
| 5100 | additional_data, |
| 5101 | ad_part_len, |
| 5102 | input_data, -1, |
| 5103 | set_lengths_method, |
| 5104 | expected_output, |
| 5105 | 0, 1 ) ) |
| 5106 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5107 | } |
| 5108 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5109 | for( data_part_len = 1; data_part_len <= input_data->len; data_part_len++ ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5110 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5111 | /* Split data into length(data_part_len) parts. */ |
| 5112 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 5113 | |
| 5114 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5115 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5116 | if( data_part_len & 0x01 ) |
| 5117 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5118 | else |
| 5119 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5120 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5121 | |
| 5122 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5123 | alg_arg, nonce, |
| 5124 | additional_data, -1, |
| 5125 | input_data, data_part_len, |
| 5126 | set_lengths_method, |
| 5127 | expected_output, |
| 5128 | 0, 0 ) ) |
| 5129 | break; |
| 5130 | |
| 5131 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 5132 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 5133 | |
| 5134 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5135 | alg_arg, nonce, |
| 5136 | additional_data, -1, |
| 5137 | input_data, data_part_len, |
| 5138 | set_lengths_method, |
| 5139 | expected_output, |
| 5140 | 0, 1 ) ) |
| 5141 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5142 | } |
| 5143 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5144 | /* Goto is required to silence warnings about unused labels, as we |
| 5145 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5146 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5147 | } |
| 5148 | /* END_CASE */ |
| 5149 | |
| 5150 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5151 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 5152 | int alg_arg, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5153 | int nonce_length, |
| 5154 | int expected_nonce_length_arg, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5155 | data_t *additional_data, |
| 5156 | data_t *input_data, |
| 5157 | int expected_status_arg ) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5158 | { |
| 5159 | |
| 5160 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5161 | psa_key_type_t key_type = key_type_arg; |
| 5162 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5163 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5164 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5165 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5166 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5167 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5168 | size_t actual_nonce_length = 0; |
| 5169 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5170 | unsigned char *output = NULL; |
| 5171 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5172 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5173 | size_t ciphertext_size = 0; |
| 5174 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5175 | size_t tag_length = 0; |
| 5176 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5177 | |
| 5178 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5179 | |
| 5180 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5181 | psa_set_key_algorithm( & attributes, alg ); |
| 5182 | psa_set_key_type( & attributes, key_type ); |
| 5183 | |
| 5184 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5185 | &key ) ); |
| 5186 | |
| 5187 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5188 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5189 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5190 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5191 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5192 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5193 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5194 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5195 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5196 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5197 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5198 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5199 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5200 | |
| 5201 | /* If the operation is not supported, just skip and not fail in case the |
| 5202 | * encryption involves a common limitation of cryptography hardwares and |
| 5203 | * an alternative implementation. */ |
| 5204 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5205 | { |
| 5206 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5207 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5208 | } |
| 5209 | |
| 5210 | PSA_ASSERT( status ); |
| 5211 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5212 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5213 | nonce_length, |
| 5214 | &actual_nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5215 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5216 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5217 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5218 | TEST_EQUAL( actual_nonce_length, expected_nonce_length ); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5219 | |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5220 | if( expected_status == PSA_SUCCESS ) |
| 5221 | TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type, |
| 5222 | alg ) ); |
| 5223 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5224 | TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE ); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5225 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5226 | if( expected_status == PSA_SUCCESS ) |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5227 | { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5228 | /* Ensure we can still complete operation. */ |
Paul Elliott | d79c5c5 | 2021-10-06 21:49:41 +0100 | [diff] [blame] | 5229 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5230 | input_data->len ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5231 | |
| 5232 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5233 | additional_data->len ) ); |
| 5234 | |
| 5235 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5236 | output, output_size, |
| 5237 | &ciphertext_length ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5238 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5239 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5240 | &ciphertext_length, tag_buffer, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5241 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5242 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5243 | |
| 5244 | exit: |
| 5245 | psa_destroy_key( key ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5246 | mbedtls_free( output ); |
| 5247 | mbedtls_free( ciphertext ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5248 | psa_aead_abort( &operation ); |
| 5249 | PSA_DONE( ); |
| 5250 | } |
| 5251 | /* END_CASE */ |
| 5252 | |
| 5253 | /* BEGIN_CASE */ |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5254 | void aead_multipart_set_nonce( int key_type_arg, data_t *key_data, |
| 5255 | int alg_arg, |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5256 | int nonce_length_arg, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5257 | int set_lengths_method_arg, |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5258 | data_t *additional_data, |
| 5259 | data_t *input_data, |
| 5260 | int expected_status_arg ) |
| 5261 | { |
| 5262 | |
| 5263 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5264 | psa_key_type_t key_type = key_type_arg; |
| 5265 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5266 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5267 | uint8_t *nonce_buffer = NULL; |
| 5268 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5269 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5270 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5271 | unsigned char *output = NULL; |
| 5272 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5273 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5274 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5275 | size_t ciphertext_size = 0; |
| 5276 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5277 | size_t tag_length = 0; |
| 5278 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5279 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5280 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5281 | |
| 5282 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5283 | |
| 5284 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5285 | psa_set_key_algorithm( &attributes, alg ); |
| 5286 | psa_set_key_type( &attributes, key_type ); |
| 5287 | |
| 5288 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5289 | &key ) ); |
| 5290 | |
| 5291 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5292 | |
| 5293 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5294 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5295 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5296 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5297 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5298 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5299 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5300 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5301 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5302 | |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5303 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5304 | |
| 5305 | /* If the operation is not supported, just skip and not fail in case the |
| 5306 | * encryption involves a common limitation of cryptography hardwares and |
| 5307 | * an alternative implementation. */ |
| 5308 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5309 | { |
| 5310 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5311 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce_length_arg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5312 | } |
| 5313 | |
| 5314 | PSA_ASSERT( status ); |
| 5315 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5316 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
| 5317 | if( nonce_length_arg == -1 ) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5318 | { |
Paul Elliott | 5e69aa5 | 2021-08-25 17:24:37 +0100 | [diff] [blame] | 5319 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5320 | ASSERT_ALLOC( nonce_buffer, 4 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5321 | nonce_length = 0; |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5322 | } |
| 5323 | else |
| 5324 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5325 | /* If length is zero, then this will return NULL. */ |
| 5326 | nonce_length = ( size_t ) nonce_length_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5327 | ASSERT_ALLOC( nonce_buffer, nonce_length ); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5328 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5329 | if( nonce_buffer ) |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5330 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5331 | for( index = 0; index < nonce_length - 1; ++index ) |
| 5332 | { |
| 5333 | nonce_buffer[index] = 'a' + index; |
| 5334 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5335 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5336 | } |
| 5337 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5338 | if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
| 5339 | { |
| 5340 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5341 | input_data->len ) ); |
| 5342 | } |
| 5343 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5344 | status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5345 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5346 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5347 | |
| 5348 | if( expected_status == PSA_SUCCESS ) |
| 5349 | { |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5350 | if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
| 5351 | { |
| 5352 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5353 | input_data->len ) ); |
| 5354 | } |
| 5355 | if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 5356 | expected_status = PSA_ERROR_BAD_STATE; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5357 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5358 | /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */ |
| 5359 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5360 | additional_data->len ), |
| 5361 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5362 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5363 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5364 | output, output_size, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5365 | &ciphertext_length ), |
| 5366 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5367 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5368 | TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5369 | &ciphertext_length, tag_buffer, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5370 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ), |
| 5371 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5372 | } |
| 5373 | |
| 5374 | exit: |
| 5375 | psa_destroy_key( key ); |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5376 | mbedtls_free( output ); |
| 5377 | mbedtls_free( ciphertext ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5378 | mbedtls_free( nonce_buffer ); |
| 5379 | psa_aead_abort( &operation ); |
| 5380 | PSA_DONE( ); |
| 5381 | } |
| 5382 | /* END_CASE */ |
| 5383 | |
| 5384 | /* BEGIN_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5385 | void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data, |
| 5386 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5387 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5388 | data_t *nonce, |
| 5389 | data_t *additional_data, |
| 5390 | data_t *input_data, |
| 5391 | int expected_status_arg ) |
| 5392 | { |
| 5393 | |
| 5394 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5395 | psa_key_type_t key_type = key_type_arg; |
| 5396 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5397 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5398 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5399 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5400 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5401 | unsigned char *output = NULL; |
| 5402 | unsigned char *ciphertext = NULL; |
| 5403 | size_t output_size = output_size_arg; |
| 5404 | size_t ciphertext_size = 0; |
| 5405 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5406 | size_t tag_length = 0; |
| 5407 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5408 | |
| 5409 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5410 | |
| 5411 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5412 | psa_set_key_algorithm( &attributes, alg ); |
| 5413 | psa_set_key_type( &attributes, key_type ); |
| 5414 | |
| 5415 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5416 | &key ) ); |
| 5417 | |
| 5418 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5419 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5420 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5421 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5422 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5423 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5424 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5425 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5426 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5427 | |
| 5428 | /* If the operation is not supported, just skip and not fail in case the |
| 5429 | * encryption involves a common limitation of cryptography hardwares and |
| 5430 | * an alternative implementation. */ |
| 5431 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5432 | { |
| 5433 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5434 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5435 | } |
| 5436 | |
| 5437 | PSA_ASSERT( status ); |
| 5438 | |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5439 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5440 | input_data->len ) ); |
| 5441 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5442 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5443 | |
| 5444 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5445 | additional_data->len ) ); |
| 5446 | |
| 5447 | status = psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5448 | output, output_size, &ciphertext_length ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5449 | |
| 5450 | TEST_EQUAL( status, expected_status ); |
| 5451 | |
| 5452 | if( expected_status == PSA_SUCCESS ) |
| 5453 | { |
| 5454 | /* Ensure we can still complete operation. */ |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5455 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5456 | &ciphertext_length, tag_buffer, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5457 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5458 | } |
| 5459 | |
| 5460 | exit: |
| 5461 | psa_destroy_key( key ); |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5462 | mbedtls_free( output ); |
| 5463 | mbedtls_free( ciphertext ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5464 | psa_aead_abort( &operation ); |
| 5465 | PSA_DONE( ); |
| 5466 | } |
| 5467 | /* END_CASE */ |
| 5468 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5469 | /* BEGIN_CASE */ |
| 5470 | void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data, |
| 5471 | int alg_arg, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5472 | int finish_ciphertext_size_arg, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5473 | int tag_size_arg, |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5474 | data_t *nonce, |
| 5475 | data_t *additional_data, |
| 5476 | data_t *input_data, |
| 5477 | int expected_status_arg ) |
| 5478 | { |
| 5479 | |
| 5480 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5481 | psa_key_type_t key_type = key_type_arg; |
| 5482 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5483 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5484 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5485 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5486 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5487 | unsigned char *ciphertext = NULL; |
| 5488 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5489 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5490 | size_t ciphertext_size = 0; |
| 5491 | size_t ciphertext_length = 0; |
| 5492 | size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5493 | size_t tag_size = ( size_t ) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5494 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5495 | |
| 5496 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5497 | |
| 5498 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5499 | psa_set_key_algorithm( &attributes, alg ); |
| 5500 | psa_set_key_type( &attributes, key_type ); |
| 5501 | |
| 5502 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5503 | &key ) ); |
| 5504 | |
| 5505 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5506 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5507 | ciphertext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5508 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5509 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5510 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5511 | ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5512 | |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5513 | ASSERT_ALLOC( tag_buffer, tag_size ); |
| 5514 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5515 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5516 | |
| 5517 | /* If the operation is not supported, just skip and not fail in case the |
| 5518 | * encryption involves a common limitation of cryptography hardwares and |
| 5519 | * an alternative implementation. */ |
| 5520 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5521 | { |
| 5522 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5523 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5524 | } |
| 5525 | |
| 5526 | PSA_ASSERT( status ); |
| 5527 | |
| 5528 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5529 | |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5530 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5531 | input_data->len ) ); |
| 5532 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5533 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5534 | additional_data->len ) ); |
| 5535 | |
| 5536 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5537 | ciphertext, ciphertext_size, &ciphertext_length ) ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5538 | |
| 5539 | /* Ensure we can still complete operation. */ |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5540 | status = psa_aead_finish( &operation, finish_ciphertext, |
| 5541 | finish_ciphertext_size, |
| 5542 | &ciphertext_length, tag_buffer, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5543 | tag_size, &tag_length ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5544 | |
| 5545 | TEST_EQUAL( status, expected_status ); |
| 5546 | |
| 5547 | exit: |
| 5548 | psa_destroy_key( key ); |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5549 | mbedtls_free( ciphertext ); |
| 5550 | mbedtls_free( finish_ciphertext ); |
Paul Elliott | 4a76088 | 2021-09-20 09:42:21 +0100 | [diff] [blame] | 5551 | mbedtls_free( tag_buffer ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5552 | psa_aead_abort( &operation ); |
| 5553 | PSA_DONE( ); |
| 5554 | } |
| 5555 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5556 | |
| 5557 | /* BEGIN_CASE */ |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5558 | void aead_multipart_verify( int key_type_arg, data_t *key_data, |
| 5559 | int alg_arg, |
| 5560 | data_t *nonce, |
| 5561 | data_t *additional_data, |
| 5562 | data_t *input_data, |
| 5563 | data_t *tag, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5564 | int tag_usage_arg, |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5565 | int expected_setup_status_arg, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5566 | int expected_status_arg ) |
| 5567 | { |
| 5568 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5569 | psa_key_type_t key_type = key_type_arg; |
| 5570 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5571 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5572 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5573 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5574 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5575 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5576 | unsigned char *plaintext = NULL; |
| 5577 | unsigned char *finish_plaintext = NULL; |
| 5578 | size_t plaintext_size = 0; |
| 5579 | size_t plaintext_length = 0; |
| 5580 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5581 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5582 | unsigned char *tag_buffer = NULL; |
| 5583 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5584 | |
| 5585 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5586 | |
| 5587 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5588 | psa_set_key_algorithm( &attributes, alg ); |
| 5589 | psa_set_key_type( &attributes, key_type ); |
| 5590 | |
| 5591 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5592 | &key ) ); |
| 5593 | |
| 5594 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5595 | |
| 5596 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 5597 | input_data->len ); |
| 5598 | |
| 5599 | ASSERT_ALLOC( plaintext, plaintext_size ); |
| 5600 | |
| 5601 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 5602 | |
| 5603 | ASSERT_ALLOC( finish_plaintext, verify_plaintext_size ); |
| 5604 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5605 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5606 | |
| 5607 | /* If the operation is not supported, just skip and not fail in case the |
| 5608 | * encryption involves a common limitation of cryptography hardwares and |
| 5609 | * an alternative implementation. */ |
| 5610 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5611 | { |
| 5612 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5613 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5614 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5615 | TEST_EQUAL( status, expected_setup_status ); |
| 5616 | |
| 5617 | if( status != PSA_SUCCESS ) |
| 5618 | goto exit; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5619 | |
| 5620 | PSA_ASSERT( status ); |
| 5621 | |
| 5622 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5623 | |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5624 | status = psa_aead_set_lengths( &operation, additional_data->len, |
| 5625 | input_data->len ); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5626 | PSA_ASSERT( status ); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5627 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5628 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5629 | additional_data->len ) ); |
| 5630 | |
| 5631 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5632 | input_data->len, |
| 5633 | plaintext, plaintext_size, |
| 5634 | &plaintext_length ) ); |
| 5635 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5636 | if( tag_usage == USE_GIVEN_TAG ) |
| 5637 | { |
| 5638 | tag_buffer = tag->x; |
| 5639 | tag_size = tag->len; |
| 5640 | } |
| 5641 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5642 | status = psa_aead_verify( &operation, finish_plaintext, |
| 5643 | verify_plaintext_size, |
| 5644 | &plaintext_length, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5645 | tag_buffer, tag_size ); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5646 | |
| 5647 | TEST_EQUAL( status, expected_status ); |
| 5648 | |
| 5649 | exit: |
| 5650 | psa_destroy_key( key ); |
| 5651 | mbedtls_free( plaintext ); |
| 5652 | mbedtls_free( finish_plaintext ); |
| 5653 | psa_aead_abort( &operation ); |
| 5654 | PSA_DONE( ); |
| 5655 | } |
| 5656 | /* END_CASE */ |
| 5657 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5658 | /* BEGIN_CASE */ |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5659 | void aead_multipart_setup( int key_type_arg, data_t *key_data, |
| 5660 | int alg_arg, int expected_status_arg ) |
| 5661 | { |
| 5662 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5663 | psa_key_type_t key_type = key_type_arg; |
| 5664 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5665 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5666 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5667 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5668 | psa_status_t expected_status = expected_status_arg; |
| 5669 | |
| 5670 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5671 | |
| 5672 | psa_set_key_usage_flags( &attributes, |
| 5673 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5674 | psa_set_key_algorithm( &attributes, alg ); |
| 5675 | psa_set_key_type( &attributes, key_type ); |
| 5676 | |
| 5677 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5678 | &key ) ); |
| 5679 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5680 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5681 | |
| 5682 | TEST_EQUAL( status, expected_status ); |
| 5683 | |
| 5684 | psa_aead_abort( &operation ); |
| 5685 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5686 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5687 | |
| 5688 | TEST_EQUAL(status, expected_status ); |
| 5689 | |
| 5690 | exit: |
| 5691 | psa_destroy_key( key ); |
| 5692 | psa_aead_abort( &operation ); |
| 5693 | PSA_DONE( ); |
| 5694 | } |
| 5695 | /* END_CASE */ |
| 5696 | |
| 5697 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5698 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 5699 | int alg_arg, |
| 5700 | data_t *nonce, |
| 5701 | data_t *additional_data, |
| 5702 | data_t *input_data ) |
| 5703 | { |
| 5704 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5705 | psa_key_type_t key_type = key_type_arg; |
| 5706 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5707 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5708 | unsigned char *output_data = NULL; |
| 5709 | unsigned char *final_data = NULL; |
| 5710 | size_t output_size = 0; |
| 5711 | size_t finish_output_size = 0; |
| 5712 | size_t output_length = 0; |
| 5713 | size_t key_bits = 0; |
| 5714 | size_t tag_length = 0; |
| 5715 | size_t tag_size = 0; |
| 5716 | size_t nonce_length = 0; |
| 5717 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5718 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5719 | size_t output_part_length = 0; |
| 5720 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5721 | |
| 5722 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5723 | |
| 5724 | psa_set_key_usage_flags( & attributes, |
| 5725 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5726 | psa_set_key_algorithm( & attributes, alg ); |
| 5727 | psa_set_key_type( & attributes, key_type ); |
| 5728 | |
| 5729 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5730 | &key ) ); |
| 5731 | |
| 5732 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5733 | key_bits = psa_get_key_bits( &attributes ); |
| 5734 | |
| 5735 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 5736 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5737 | TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5738 | |
| 5739 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5740 | |
| 5741 | ASSERT_ALLOC( output_data, output_size ); |
| 5742 | |
| 5743 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 5744 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5745 | TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5746 | |
| 5747 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 5748 | |
| 5749 | /* Test all operations error without calling setup first. */ |
| 5750 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5751 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5752 | PSA_ERROR_BAD_STATE ); |
| 5753 | |
| 5754 | psa_aead_abort( &operation ); |
| 5755 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5756 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5757 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5758 | &nonce_length ), |
| 5759 | PSA_ERROR_BAD_STATE ); |
| 5760 | |
| 5761 | psa_aead_abort( &operation ); |
| 5762 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5763 | /* ------------------------------------------------------- */ |
| 5764 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5765 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5766 | input_data->len ), |
| 5767 | PSA_ERROR_BAD_STATE ); |
| 5768 | |
| 5769 | psa_aead_abort( &operation ); |
| 5770 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5771 | /* ------------------------------------------------------- */ |
| 5772 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5773 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5774 | additional_data->len ), |
| 5775 | PSA_ERROR_BAD_STATE ); |
| 5776 | |
| 5777 | psa_aead_abort( &operation ); |
| 5778 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5779 | /* ------------------------------------------------------- */ |
| 5780 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5781 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5782 | input_data->len, output_data, |
| 5783 | output_size, &output_length ), |
| 5784 | PSA_ERROR_BAD_STATE ); |
| 5785 | |
| 5786 | psa_aead_abort( &operation ); |
| 5787 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5788 | /* ------------------------------------------------------- */ |
| 5789 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5790 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5791 | finish_output_size, |
| 5792 | &output_part_length, |
| 5793 | tag_buffer, tag_length, |
| 5794 | &tag_size ), |
| 5795 | PSA_ERROR_BAD_STATE ); |
| 5796 | |
| 5797 | psa_aead_abort( &operation ); |
| 5798 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5799 | /* ------------------------------------------------------- */ |
| 5800 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5801 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5802 | finish_output_size, |
| 5803 | &output_part_length, |
| 5804 | tag_buffer, |
| 5805 | tag_length ), |
| 5806 | PSA_ERROR_BAD_STATE ); |
| 5807 | |
| 5808 | psa_aead_abort( &operation ); |
| 5809 | |
| 5810 | /* Test for double setups. */ |
| 5811 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5812 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5813 | |
| 5814 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5815 | PSA_ERROR_BAD_STATE ); |
| 5816 | |
| 5817 | psa_aead_abort( &operation ); |
| 5818 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5819 | /* ------------------------------------------------------- */ |
| 5820 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5821 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5822 | |
| 5823 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5824 | PSA_ERROR_BAD_STATE ); |
| 5825 | |
| 5826 | psa_aead_abort( &operation ); |
| 5827 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5828 | /* ------------------------------------------------------- */ |
| 5829 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5830 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5831 | |
| 5832 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5833 | PSA_ERROR_BAD_STATE ); |
| 5834 | |
| 5835 | psa_aead_abort( &operation ); |
| 5836 | |
| 5837 | /* ------------------------------------------------------- */ |
| 5838 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5839 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5840 | |
| 5841 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5842 | PSA_ERROR_BAD_STATE ); |
| 5843 | |
| 5844 | psa_aead_abort( &operation ); |
| 5845 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5846 | /* Test for not setting a nonce. */ |
| 5847 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5848 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5849 | |
| 5850 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5851 | additional_data->len ), |
| 5852 | PSA_ERROR_BAD_STATE ); |
| 5853 | |
| 5854 | psa_aead_abort( &operation ); |
| 5855 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5856 | /* ------------------------------------------------------- */ |
| 5857 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5858 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5859 | |
| 5860 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5861 | input_data->len, output_data, |
| 5862 | output_size, &output_length ), |
| 5863 | PSA_ERROR_BAD_STATE ); |
| 5864 | |
| 5865 | psa_aead_abort( &operation ); |
| 5866 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5867 | /* ------------------------------------------------------- */ |
| 5868 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5869 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5870 | |
| 5871 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5872 | finish_output_size, |
| 5873 | &output_part_length, |
| 5874 | tag_buffer, tag_length, |
| 5875 | &tag_size ), |
| 5876 | PSA_ERROR_BAD_STATE ); |
| 5877 | |
| 5878 | psa_aead_abort( &operation ); |
| 5879 | |
| 5880 | /* ------------------------------------------------------- */ |
| 5881 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5882 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5883 | |
| 5884 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5885 | finish_output_size, |
| 5886 | &output_part_length, |
| 5887 | tag_buffer, |
| 5888 | tag_length ), |
| 5889 | PSA_ERROR_BAD_STATE ); |
| 5890 | |
| 5891 | psa_aead_abort( &operation ); |
| 5892 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5893 | /* Test for double setting nonce. */ |
| 5894 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5895 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5896 | |
| 5897 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5898 | |
| 5899 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5900 | PSA_ERROR_BAD_STATE ); |
| 5901 | |
| 5902 | psa_aead_abort( &operation ); |
| 5903 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5904 | /* Test for double generating nonce. */ |
| 5905 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5906 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5907 | |
| 5908 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5909 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5910 | &nonce_length ) ); |
| 5911 | |
| 5912 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5913 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5914 | &nonce_length ), |
| 5915 | PSA_ERROR_BAD_STATE ); |
| 5916 | |
| 5917 | |
| 5918 | psa_aead_abort( &operation ); |
| 5919 | |
| 5920 | /* Test for generate nonce then set and vice versa */ |
| 5921 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5922 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5923 | |
| 5924 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5925 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5926 | &nonce_length ) ); |
| 5927 | |
| 5928 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5929 | PSA_ERROR_BAD_STATE ); |
| 5930 | |
| 5931 | psa_aead_abort( &operation ); |
| 5932 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5933 | /* Test for generating nonce after calling set lengths */ |
| 5934 | |
| 5935 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5936 | |
| 5937 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5938 | input_data->len ) ); |
| 5939 | |
| 5940 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5941 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5942 | &nonce_length ) ); |
| 5943 | |
| 5944 | psa_aead_abort( &operation ); |
| 5945 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5946 | /* Test for generating nonce after calling set lengths with UINT32_MAX ad_data length */ |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5947 | |
| 5948 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5949 | |
| 5950 | if( operation.alg == PSA_ALG_CCM ) |
| 5951 | { |
| 5952 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5953 | input_data->len ), |
| 5954 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5955 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5956 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5957 | &nonce_length ), |
| 5958 | PSA_ERROR_BAD_STATE ); |
| 5959 | } |
| 5960 | else |
| 5961 | { |
| 5962 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5963 | input_data->len ) ); |
| 5964 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5965 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5966 | &nonce_length ) ); |
| 5967 | } |
| 5968 | |
| 5969 | psa_aead_abort( &operation ); |
| 5970 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5971 | /* Test for generating nonce after calling set lengths with SIZE_MAX ad_data length */ |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5972 | #if SIZE_MAX > UINT32_MAX |
| 5973 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5974 | |
| 5975 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 5976 | { |
| 5977 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5978 | input_data->len ), |
| 5979 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5980 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5981 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5982 | &nonce_length ), |
| 5983 | PSA_ERROR_BAD_STATE ); |
| 5984 | } |
| 5985 | else |
| 5986 | { |
| 5987 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5988 | input_data->len ) ); |
| 5989 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5990 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5991 | &nonce_length ) ); |
| 5992 | } |
| 5993 | |
| 5994 | psa_aead_abort( &operation ); |
| 5995 | #endif |
| 5996 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5997 | /* Test for calling set lengths with a UINT32_MAX ad_data length, after generating nonce */ |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5998 | |
| 5999 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6000 | |
| 6001 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6002 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6003 | &nonce_length ) ); |
| 6004 | |
| 6005 | if( operation.alg == PSA_ALG_CCM ) |
| 6006 | { |
| 6007 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6008 | input_data->len ), |
| 6009 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6010 | } |
| 6011 | else |
| 6012 | { |
| 6013 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6014 | input_data->len ) ); |
| 6015 | } |
| 6016 | |
| 6017 | psa_aead_abort( &operation ); |
| 6018 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6019 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6020 | /* Test for setting nonce after calling set lengths */ |
| 6021 | |
| 6022 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6023 | |
| 6024 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6025 | input_data->len ) ); |
| 6026 | |
| 6027 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6028 | |
| 6029 | psa_aead_abort( &operation ); |
| 6030 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6031 | /* Test for setting nonce after calling set lengths with UINT32_MAX ad_data length */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6032 | |
| 6033 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6034 | |
| 6035 | if( operation.alg == PSA_ALG_CCM ) |
| 6036 | { |
| 6037 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6038 | input_data->len ), |
| 6039 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6040 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6041 | PSA_ERROR_BAD_STATE ); |
| 6042 | } |
| 6043 | else |
| 6044 | { |
| 6045 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6046 | input_data->len ) ); |
| 6047 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6048 | } |
| 6049 | |
| 6050 | psa_aead_abort( &operation ); |
| 6051 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6052 | /* Test for setting nonce after calling set lengths with SIZE_MAX ad_data length */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6053 | #if SIZE_MAX > UINT32_MAX |
| 6054 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6055 | |
| 6056 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 6057 | { |
| 6058 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6059 | input_data->len ), |
| 6060 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6061 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6062 | PSA_ERROR_BAD_STATE ); |
| 6063 | } |
| 6064 | else |
| 6065 | { |
| 6066 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6067 | input_data->len ) ); |
| 6068 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6069 | } |
| 6070 | |
| 6071 | psa_aead_abort( &operation ); |
| 6072 | #endif |
| 6073 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6074 | /* Test for calling set lengths with an ad_data length of UINT32_MAX, after setting nonce */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6075 | |
| 6076 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6077 | |
| 6078 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6079 | |
| 6080 | if( operation.alg == PSA_ALG_CCM ) |
| 6081 | { |
| 6082 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6083 | input_data->len ), |
| 6084 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6085 | } |
| 6086 | else |
| 6087 | { |
| 6088 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6089 | input_data->len ) ); |
| 6090 | } |
| 6091 | |
| 6092 | psa_aead_abort( &operation ); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6093 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6094 | /* Test for setting nonce after calling set lengths with plaintext length of SIZE_MAX */ |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6095 | #if SIZE_MAX > UINT32_MAX |
| 6096 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6097 | |
| 6098 | if( operation.alg == PSA_ALG_GCM ) |
| 6099 | { |
| 6100 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6101 | SIZE_MAX ), |
| 6102 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6103 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6104 | PSA_ERROR_BAD_STATE ); |
| 6105 | } |
| 6106 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6107 | { |
| 6108 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6109 | SIZE_MAX ) ); |
| 6110 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6111 | } |
| 6112 | |
| 6113 | psa_aead_abort( &operation ); |
| 6114 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6115 | /* Test for calling set lengths with an plaintext length of SIZE_MAX, after setting nonce */ |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6116 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6117 | |
| 6118 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6119 | |
| 6120 | if( operation.alg == PSA_ALG_GCM ) |
| 6121 | { |
| 6122 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6123 | SIZE_MAX ), |
| 6124 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6125 | } |
| 6126 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6127 | { |
| 6128 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6129 | SIZE_MAX ) ); |
| 6130 | } |
| 6131 | |
| 6132 | psa_aead_abort( &operation ); |
| 6133 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6134 | |
| 6135 | /* ------------------------------------------------------- */ |
| 6136 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6137 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6138 | |
| 6139 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6140 | |
| 6141 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6142 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6143 | &nonce_length ), |
| 6144 | PSA_ERROR_BAD_STATE ); |
| 6145 | |
| 6146 | psa_aead_abort( &operation ); |
| 6147 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6148 | /* Test for generating nonce in decrypt setup. */ |
| 6149 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6150 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6151 | |
| 6152 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6153 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6154 | &nonce_length ), |
| 6155 | PSA_ERROR_BAD_STATE ); |
| 6156 | |
| 6157 | psa_aead_abort( &operation ); |
| 6158 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6159 | /* Test for setting lengths twice. */ |
| 6160 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6161 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6162 | |
| 6163 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6164 | |
| 6165 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6166 | input_data->len ) ); |
| 6167 | |
| 6168 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6169 | input_data->len ), |
| 6170 | PSA_ERROR_BAD_STATE ); |
| 6171 | |
| 6172 | psa_aead_abort( &operation ); |
| 6173 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6174 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6175 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6176 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6177 | |
| 6178 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6179 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6180 | if( operation.alg == PSA_ALG_CCM ) |
| 6181 | { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6182 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6183 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6184 | additional_data->len ), |
| 6185 | PSA_ERROR_BAD_STATE ); |
| 6186 | } |
| 6187 | else |
| 6188 | { |
| 6189 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6190 | additional_data->len ) ); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6191 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6192 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6193 | input_data->len ), |
| 6194 | PSA_ERROR_BAD_STATE ); |
| 6195 | } |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6196 | psa_aead_abort( &operation ); |
| 6197 | |
| 6198 | /* ------------------------------------------------------- */ |
| 6199 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6200 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6201 | |
| 6202 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6203 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6204 | if( operation.alg == PSA_ALG_CCM ) |
| 6205 | { |
| 6206 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6207 | input_data->len, output_data, |
| 6208 | output_size, &output_length ), |
| 6209 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6210 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6211 | } |
| 6212 | else |
| 6213 | { |
| 6214 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6215 | input_data->len, output_data, |
| 6216 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6217 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6218 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6219 | input_data->len ), |
| 6220 | PSA_ERROR_BAD_STATE ); |
| 6221 | } |
| 6222 | psa_aead_abort( &operation ); |
| 6223 | |
| 6224 | /* ------------------------------------------------------- */ |
| 6225 | |
| 6226 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6227 | |
| 6228 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6229 | |
| 6230 | if( operation.alg == PSA_ALG_CCM ) |
| 6231 | { |
| 6232 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6233 | finish_output_size, |
| 6234 | &output_part_length, |
| 6235 | tag_buffer, tag_length, |
| 6236 | &tag_size ) ); |
| 6237 | } |
| 6238 | else |
| 6239 | { |
| 6240 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6241 | finish_output_size, |
| 6242 | &output_part_length, |
| 6243 | tag_buffer, tag_length, |
| 6244 | &tag_size ) ); |
| 6245 | |
| 6246 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6247 | input_data->len ), |
| 6248 | PSA_ERROR_BAD_STATE ); |
| 6249 | } |
| 6250 | psa_aead_abort( &operation ); |
| 6251 | |
| 6252 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6253 | |
| 6254 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6255 | |
| 6256 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6257 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6258 | &nonce_length ) ); |
| 6259 | if( operation.alg == PSA_ALG_CCM ) |
| 6260 | { |
| 6261 | |
| 6262 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6263 | additional_data->len ), |
| 6264 | PSA_ERROR_BAD_STATE ); |
| 6265 | } |
| 6266 | else |
| 6267 | { |
| 6268 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6269 | additional_data->len ) ); |
| 6270 | |
| 6271 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6272 | input_data->len ), |
| 6273 | PSA_ERROR_BAD_STATE ); |
| 6274 | } |
| 6275 | psa_aead_abort( &operation ); |
| 6276 | |
| 6277 | /* ------------------------------------------------------- */ |
| 6278 | |
| 6279 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6280 | |
| 6281 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6282 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6283 | &nonce_length ) ); |
| 6284 | if( operation.alg == PSA_ALG_CCM ) |
| 6285 | { |
| 6286 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6287 | input_data->len, output_data, |
| 6288 | output_size, &output_length ), |
| 6289 | PSA_ERROR_BAD_STATE ); |
| 6290 | |
| 6291 | } |
| 6292 | else |
| 6293 | { |
| 6294 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6295 | input_data->len, output_data, |
| 6296 | output_size, &output_length ) ); |
| 6297 | |
| 6298 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6299 | input_data->len ), |
| 6300 | PSA_ERROR_BAD_STATE ); |
| 6301 | } |
| 6302 | psa_aead_abort( &operation ); |
| 6303 | |
| 6304 | /* ------------------------------------------------------- */ |
| 6305 | |
| 6306 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6307 | |
| 6308 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6309 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6310 | &nonce_length ) ); |
| 6311 | if( operation.alg == PSA_ALG_CCM ) |
| 6312 | { |
| 6313 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6314 | finish_output_size, |
| 6315 | &output_part_length, |
| 6316 | tag_buffer, tag_length, |
| 6317 | &tag_size ) ); |
| 6318 | } |
| 6319 | else |
| 6320 | { |
| 6321 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6322 | finish_output_size, |
| 6323 | &output_part_length, |
| 6324 | tag_buffer, tag_length, |
| 6325 | &tag_size ) ); |
| 6326 | |
| 6327 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6328 | input_data->len ), |
| 6329 | PSA_ERROR_BAD_STATE ); |
| 6330 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6331 | psa_aead_abort( &operation ); |
| 6332 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6333 | /* Test for not sending any additional data or data after setting non zero |
| 6334 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6335 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6336 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6337 | |
| 6338 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6339 | |
| 6340 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6341 | input_data->len ) ); |
| 6342 | |
| 6343 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6344 | finish_output_size, |
| 6345 | &output_part_length, |
| 6346 | tag_buffer, tag_length, |
| 6347 | &tag_size ), |
| 6348 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6349 | |
| 6350 | psa_aead_abort( &operation ); |
| 6351 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6352 | /* Test for not sending any additional data or data after setting non-zero |
| 6353 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6354 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6355 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6356 | |
| 6357 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6358 | |
| 6359 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6360 | input_data->len ) ); |
| 6361 | |
| 6362 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6363 | finish_output_size, |
| 6364 | &output_part_length, |
| 6365 | tag_buffer, |
| 6366 | tag_length ), |
| 6367 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6368 | |
| 6369 | psa_aead_abort( &operation ); |
| 6370 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6371 | /* Test for not sending any additional data after setting a non-zero length |
| 6372 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6373 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6374 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6375 | |
| 6376 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6377 | |
| 6378 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6379 | input_data->len ) ); |
| 6380 | |
| 6381 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6382 | input_data->len, output_data, |
| 6383 | output_size, &output_length ), |
| 6384 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6385 | |
| 6386 | psa_aead_abort( &operation ); |
| 6387 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6388 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6389 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6390 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6391 | |
| 6392 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6393 | |
| 6394 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6395 | input_data->len ) ); |
| 6396 | |
| 6397 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6398 | additional_data->len ) ); |
| 6399 | |
| 6400 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6401 | finish_output_size, |
| 6402 | &output_part_length, |
| 6403 | tag_buffer, tag_length, |
| 6404 | &tag_size ), |
| 6405 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6406 | |
| 6407 | psa_aead_abort( &operation ); |
| 6408 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6409 | /* Test for sending too much additional data after setting lengths. */ |
| 6410 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6411 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6412 | |
| 6413 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6414 | |
| 6415 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6416 | |
| 6417 | |
| 6418 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6419 | additional_data->len ), |
| 6420 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6421 | |
| 6422 | psa_aead_abort( &operation ); |
| 6423 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6424 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6425 | |
| 6426 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6427 | |
| 6428 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6429 | |
| 6430 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6431 | input_data->len ) ); |
| 6432 | |
| 6433 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6434 | additional_data->len ) ); |
| 6435 | |
| 6436 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6437 | 1 ), |
| 6438 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6439 | |
| 6440 | psa_aead_abort( &operation ); |
| 6441 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6442 | /* Test for sending too much data after setting lengths. */ |
| 6443 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6444 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6445 | |
| 6446 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6447 | |
| 6448 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6449 | |
| 6450 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6451 | input_data->len, output_data, |
| 6452 | output_size, &output_length ), |
| 6453 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6454 | |
| 6455 | psa_aead_abort( &operation ); |
| 6456 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6457 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6458 | |
| 6459 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6460 | |
| 6461 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6462 | |
| 6463 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6464 | input_data->len ) ); |
| 6465 | |
| 6466 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6467 | additional_data->len ) ); |
| 6468 | |
| 6469 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6470 | input_data->len, output_data, |
| 6471 | output_size, &output_length ) ); |
| 6472 | |
| 6473 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6474 | 1, output_data, |
| 6475 | output_size, &output_length ), |
| 6476 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6477 | |
| 6478 | psa_aead_abort( &operation ); |
| 6479 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6480 | /* Test sending additional data after data. */ |
| 6481 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6482 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6483 | |
| 6484 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6485 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6486 | if( operation.alg != PSA_ALG_CCM ) |
| 6487 | { |
| 6488 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6489 | input_data->len, output_data, |
| 6490 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6491 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6492 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6493 | additional_data->len ), |
| 6494 | PSA_ERROR_BAD_STATE ); |
| 6495 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6496 | psa_aead_abort( &operation ); |
| 6497 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6498 | /* Test calling finish on decryption. */ |
| 6499 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6500 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6501 | |
| 6502 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6503 | |
| 6504 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6505 | finish_output_size, |
| 6506 | &output_part_length, |
| 6507 | tag_buffer, tag_length, |
| 6508 | &tag_size ), |
| 6509 | PSA_ERROR_BAD_STATE ); |
| 6510 | |
| 6511 | psa_aead_abort( &operation ); |
| 6512 | |
| 6513 | /* Test calling verify on encryption. */ |
| 6514 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6515 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6516 | |
| 6517 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6518 | |
| 6519 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6520 | finish_output_size, |
| 6521 | &output_part_length, |
| 6522 | tag_buffer, |
| 6523 | tag_length ), |
Paul Elliott | 5b065cb | 2021-06-23 08:33:22 +0100 | [diff] [blame] | 6524 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6525 | |
| 6526 | psa_aead_abort( &operation ); |
| 6527 | |
| 6528 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6529 | exit: |
| 6530 | psa_destroy_key( key ); |
| 6531 | psa_aead_abort( &operation ); |
| 6532 | mbedtls_free( output_data ); |
| 6533 | mbedtls_free( final_data ); |
| 6534 | PSA_DONE( ); |
| 6535 | } |
| 6536 | /* END_CASE */ |
| 6537 | |
| 6538 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6539 | void signature_size( int type_arg, |
| 6540 | int bits, |
| 6541 | int alg_arg, |
| 6542 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6543 | { |
| 6544 | psa_key_type_t type = type_arg; |
| 6545 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6546 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6547 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6548 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6549 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6550 | exit: |
| 6551 | ; |
| 6552 | } |
| 6553 | /* END_CASE */ |
| 6554 | |
| 6555 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6556 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 6557 | int alg_arg, data_t *input_data, |
| 6558 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6559 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6560 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6561 | psa_key_type_t key_type = key_type_arg; |
| 6562 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6563 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6564 | unsigned char *signature = NULL; |
| 6565 | size_t signature_size; |
| 6566 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6567 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6568 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6569 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6570 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6571 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6572 | psa_set_key_algorithm( &attributes, alg ); |
| 6573 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6574 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6575 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6576 | &key ) ); |
| 6577 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6578 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6579 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6580 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6581 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6582 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6583 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6584 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6585 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6586 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6587 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6588 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6589 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6590 | input_data->x, input_data->len, |
| 6591 | signature, signature_size, |
| 6592 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6593 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6594 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6595 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6596 | |
| 6597 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6598 | /* |
| 6599 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6600 | * thus reset them as required. |
| 6601 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6602 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6603 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6604 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 6605 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6606 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6607 | } |
| 6608 | /* END_CASE */ |
| 6609 | |
| 6610 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6611 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 6612 | int alg_arg, data_t *input_data, |
| 6613 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6614 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6615 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6616 | psa_key_type_t key_type = key_type_arg; |
| 6617 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6618 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6619 | psa_status_t actual_status; |
| 6620 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6621 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6622 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6623 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6624 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6625 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6626 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6627 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6628 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6629 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6630 | psa_set_key_algorithm( &attributes, alg ); |
| 6631 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6632 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6633 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6634 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6635 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6636 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6637 | input_data->x, input_data->len, |
| 6638 | signature, signature_size, |
| 6639 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6640 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6641 | /* The value of *signature_length is unspecified on error, but |
| 6642 | * whatever it is, it should be less than signature_size, so that |
| 6643 | * if the caller tries to read *signature_length bytes without |
| 6644 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6645 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6646 | |
| 6647 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6648 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6649 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6650 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6651 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6652 | } |
| 6653 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6654 | |
| 6655 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6656 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 6657 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6658 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6659 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6660 | psa_key_type_t key_type = key_type_arg; |
| 6661 | psa_algorithm_t alg = alg_arg; |
| 6662 | size_t key_bits; |
| 6663 | unsigned char *signature = NULL; |
| 6664 | size_t signature_size; |
| 6665 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6666 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6667 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6668 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6669 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6670 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6671 | psa_set_key_algorithm( &attributes, alg ); |
| 6672 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6673 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6674 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6675 | &key ) ); |
| 6676 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6677 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6678 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6679 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6680 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6681 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6682 | key_bits, alg ); |
| 6683 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6684 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6685 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6686 | |
| 6687 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6688 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6689 | input_data->x, input_data->len, |
| 6690 | signature, signature_size, |
| 6691 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6692 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6693 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6694 | TEST_ASSERT( signature_length > 0 ); |
| 6695 | |
| 6696 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6697 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6698 | input_data->x, input_data->len, |
| 6699 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6700 | |
| 6701 | if( input_data->len != 0 ) |
| 6702 | { |
| 6703 | /* Flip a bit in the input and verify that the signature is now |
| 6704 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6705 | * because ECDSA may ignore the last few bits of the input. */ |
| 6706 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6707 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6708 | input_data->x, input_data->len, |
| 6709 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 6710 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6711 | } |
| 6712 | |
| 6713 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6714 | /* |
| 6715 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6716 | * thus reset them as required. |
| 6717 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6718 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6719 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6720 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6721 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6722 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6723 | } |
| 6724 | /* END_CASE */ |
| 6725 | |
| 6726 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6727 | void verify_hash( int key_type_arg, data_t *key_data, |
| 6728 | int alg_arg, data_t *hash_data, |
| 6729 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6730 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6731 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6732 | psa_key_type_t key_type = key_type_arg; |
| 6733 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6734 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6735 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6736 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 6737 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6738 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6739 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6740 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6741 | psa_set_key_algorithm( &attributes, alg ); |
| 6742 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6743 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6744 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6745 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6746 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6747 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6748 | hash_data->x, hash_data->len, |
| 6749 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 6750 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6751 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6752 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6753 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6754 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6755 | } |
| 6756 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6757 | |
| 6758 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6759 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 6760 | int alg_arg, data_t *hash_data, |
| 6761 | data_t *signature_data, |
| 6762 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6763 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6764 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6765 | psa_key_type_t key_type = key_type_arg; |
| 6766 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6767 | psa_status_t actual_status; |
| 6768 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6769 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6770 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6771 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6772 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6773 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6774 | psa_set_key_algorithm( &attributes, alg ); |
| 6775 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6776 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6777 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6778 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6779 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6780 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6781 | hash_data->x, hash_data->len, |
| 6782 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6783 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6784 | |
| 6785 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6786 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6787 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6788 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6789 | } |
| 6790 | /* END_CASE */ |
| 6791 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6792 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6793 | void sign_message_deterministic( int key_type_arg, |
| 6794 | data_t *key_data, |
| 6795 | int alg_arg, |
| 6796 | data_t *input_data, |
| 6797 | data_t *output_data ) |
| 6798 | { |
| 6799 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6800 | psa_key_type_t key_type = key_type_arg; |
| 6801 | psa_algorithm_t alg = alg_arg; |
| 6802 | size_t key_bits; |
| 6803 | unsigned char *signature = NULL; |
| 6804 | size_t signature_size; |
| 6805 | size_t signature_length = 0xdeadbeef; |
| 6806 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6807 | |
| 6808 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6809 | |
| 6810 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6811 | psa_set_key_algorithm( &attributes, alg ); |
| 6812 | psa_set_key_type( &attributes, key_type ); |
| 6813 | |
| 6814 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6815 | &key ) ); |
| 6816 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6817 | key_bits = psa_get_key_bits( &attributes ); |
| 6818 | |
| 6819 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6820 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6821 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6822 | ASSERT_ALLOC( signature, signature_size ); |
| 6823 | |
| 6824 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6825 | input_data->x, input_data->len, |
| 6826 | signature, signature_size, |
| 6827 | &signature_length ) ); |
| 6828 | |
| 6829 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6830 | signature, signature_length ); |
| 6831 | |
| 6832 | exit: |
| 6833 | psa_reset_key_attributes( &attributes ); |
| 6834 | |
| 6835 | psa_destroy_key( key ); |
| 6836 | mbedtls_free( signature ); |
| 6837 | PSA_DONE( ); |
| 6838 | |
| 6839 | } |
| 6840 | /* END_CASE */ |
| 6841 | |
| 6842 | /* BEGIN_CASE */ |
| 6843 | void sign_message_fail( int key_type_arg, |
| 6844 | data_t *key_data, |
| 6845 | int alg_arg, |
| 6846 | data_t *input_data, |
| 6847 | int signature_size_arg, |
| 6848 | int expected_status_arg ) |
| 6849 | { |
| 6850 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6851 | psa_key_type_t key_type = key_type_arg; |
| 6852 | psa_algorithm_t alg = alg_arg; |
| 6853 | size_t signature_size = signature_size_arg; |
| 6854 | psa_status_t actual_status; |
| 6855 | psa_status_t expected_status = expected_status_arg; |
| 6856 | unsigned char *signature = NULL; |
| 6857 | size_t signature_length = 0xdeadbeef; |
| 6858 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6859 | |
| 6860 | ASSERT_ALLOC( signature, signature_size ); |
| 6861 | |
| 6862 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6863 | |
| 6864 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6865 | psa_set_key_algorithm( &attributes, alg ); |
| 6866 | psa_set_key_type( &attributes, key_type ); |
| 6867 | |
| 6868 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6869 | &key ) ); |
| 6870 | |
| 6871 | actual_status = psa_sign_message( key, alg, |
| 6872 | input_data->x, input_data->len, |
| 6873 | signature, signature_size, |
| 6874 | &signature_length ); |
| 6875 | TEST_EQUAL( actual_status, expected_status ); |
| 6876 | /* The value of *signature_length is unspecified on error, but |
| 6877 | * whatever it is, it should be less than signature_size, so that |
| 6878 | * if the caller tries to read *signature_length bytes without |
| 6879 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6880 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6881 | |
| 6882 | exit: |
| 6883 | psa_reset_key_attributes( &attributes ); |
| 6884 | psa_destroy_key( key ); |
| 6885 | mbedtls_free( signature ); |
| 6886 | PSA_DONE( ); |
| 6887 | } |
| 6888 | /* END_CASE */ |
| 6889 | |
| 6890 | /* BEGIN_CASE */ |
| 6891 | void sign_verify_message( int key_type_arg, |
| 6892 | data_t *key_data, |
| 6893 | int alg_arg, |
| 6894 | data_t *input_data ) |
| 6895 | { |
| 6896 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6897 | psa_key_type_t key_type = key_type_arg; |
| 6898 | psa_algorithm_t alg = alg_arg; |
| 6899 | size_t key_bits; |
| 6900 | unsigned char *signature = NULL; |
| 6901 | size_t signature_size; |
| 6902 | size_t signature_length = 0xdeadbeef; |
| 6903 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6904 | |
| 6905 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6906 | |
| 6907 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 6908 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6909 | psa_set_key_algorithm( &attributes, alg ); |
| 6910 | psa_set_key_type( &attributes, key_type ); |
| 6911 | |
| 6912 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6913 | &key ) ); |
| 6914 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6915 | key_bits = psa_get_key_bits( &attributes ); |
| 6916 | |
| 6917 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6918 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6919 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6920 | ASSERT_ALLOC( signature, signature_size ); |
| 6921 | |
| 6922 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6923 | input_data->x, input_data->len, |
| 6924 | signature, signature_size, |
| 6925 | &signature_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6926 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6927 | TEST_ASSERT( signature_length > 0 ); |
| 6928 | |
| 6929 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6930 | input_data->x, input_data->len, |
| 6931 | signature, signature_length ) ); |
| 6932 | |
| 6933 | if( input_data->len != 0 ) |
| 6934 | { |
| 6935 | /* Flip a bit in the input and verify that the signature is now |
| 6936 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6937 | * because ECDSA may ignore the last few bits of the input. */ |
| 6938 | input_data->x[0] ^= 1; |
| 6939 | TEST_EQUAL( psa_verify_message( key, alg, |
| 6940 | input_data->x, input_data->len, |
| 6941 | signature, signature_length ), |
| 6942 | PSA_ERROR_INVALID_SIGNATURE ); |
| 6943 | } |
| 6944 | |
| 6945 | exit: |
| 6946 | psa_reset_key_attributes( &attributes ); |
| 6947 | |
| 6948 | psa_destroy_key( key ); |
| 6949 | mbedtls_free( signature ); |
| 6950 | PSA_DONE( ); |
| 6951 | } |
| 6952 | /* END_CASE */ |
| 6953 | |
| 6954 | /* BEGIN_CASE */ |
| 6955 | void verify_message( int key_type_arg, |
| 6956 | data_t *key_data, |
| 6957 | int alg_arg, |
| 6958 | data_t *input_data, |
| 6959 | data_t *signature_data ) |
| 6960 | { |
| 6961 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6962 | psa_key_type_t key_type = key_type_arg; |
| 6963 | psa_algorithm_t alg = alg_arg; |
| 6964 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6965 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6966 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6967 | |
| 6968 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6969 | |
| 6970 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6971 | psa_set_key_algorithm( &attributes, alg ); |
| 6972 | psa_set_key_type( &attributes, key_type ); |
| 6973 | |
| 6974 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6975 | &key ) ); |
| 6976 | |
| 6977 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6978 | input_data->x, input_data->len, |
| 6979 | signature_data->x, signature_data->len ) ); |
| 6980 | |
| 6981 | exit: |
| 6982 | psa_reset_key_attributes( &attributes ); |
| 6983 | psa_destroy_key( key ); |
| 6984 | PSA_DONE( ); |
| 6985 | } |
| 6986 | /* END_CASE */ |
| 6987 | |
| 6988 | /* BEGIN_CASE */ |
| 6989 | void verify_message_fail( int key_type_arg, |
| 6990 | data_t *key_data, |
| 6991 | int alg_arg, |
| 6992 | data_t *hash_data, |
| 6993 | data_t *signature_data, |
| 6994 | int expected_status_arg ) |
| 6995 | { |
| 6996 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6997 | psa_key_type_t key_type = key_type_arg; |
| 6998 | psa_algorithm_t alg = alg_arg; |
| 6999 | psa_status_t actual_status; |
| 7000 | psa_status_t expected_status = expected_status_arg; |
| 7001 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7002 | |
| 7003 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7004 | |
| 7005 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 7006 | psa_set_key_algorithm( &attributes, alg ); |
| 7007 | psa_set_key_type( &attributes, key_type ); |
| 7008 | |
| 7009 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 7010 | &key ) ); |
| 7011 | |
| 7012 | actual_status = psa_verify_message( key, alg, |
| 7013 | hash_data->x, hash_data->len, |
| 7014 | signature_data->x, |
| 7015 | signature_data->len ); |
| 7016 | TEST_EQUAL( actual_status, expected_status ); |
| 7017 | |
| 7018 | exit: |
| 7019 | psa_reset_key_attributes( &attributes ); |
| 7020 | psa_destroy_key( key ); |
| 7021 | PSA_DONE( ); |
| 7022 | } |
| 7023 | /* END_CASE */ |
| 7024 | |
| 7025 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7026 | void asymmetric_encrypt( int key_type_arg, |
| 7027 | data_t *key_data, |
| 7028 | int alg_arg, |
| 7029 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7030 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7031 | int expected_output_length_arg, |
| 7032 | int expected_status_arg ) |
| 7033 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7034 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7035 | psa_key_type_t key_type = key_type_arg; |
| 7036 | psa_algorithm_t alg = alg_arg; |
| 7037 | size_t expected_output_length = expected_output_length_arg; |
| 7038 | size_t key_bits; |
| 7039 | unsigned char *output = NULL; |
| 7040 | size_t output_size; |
| 7041 | size_t output_length = ~0; |
| 7042 | psa_status_t actual_status; |
| 7043 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7044 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7045 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7046 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 7047 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7048 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7049 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7050 | psa_set_key_algorithm( &attributes, alg ); |
| 7051 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7052 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7053 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7054 | |
| 7055 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7056 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7057 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7058 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7059 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7060 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7061 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7062 | |
| 7063 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7064 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7065 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7066 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7067 | output, output_size, |
| 7068 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7069 | TEST_EQUAL( actual_status, expected_status ); |
| 7070 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7071 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7072 | /* If the label is empty, the test framework puts a non-null pointer |
| 7073 | * in label->x. Test that a null pointer works as well. */ |
| 7074 | if( label->len == 0 ) |
| 7075 | { |
| 7076 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7077 | if( output_size != 0 ) |
| 7078 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7079 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7080 | input_data->x, input_data->len, |
| 7081 | NULL, label->len, |
| 7082 | output, output_size, |
| 7083 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7084 | TEST_EQUAL( actual_status, expected_status ); |
| 7085 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7086 | } |
| 7087 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7088 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7089 | /* |
| 7090 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7091 | * thus reset them as required. |
| 7092 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7093 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7094 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7095 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7096 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7097 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7098 | } |
| 7099 | /* END_CASE */ |
| 7100 | |
| 7101 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7102 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 7103 | data_t *key_data, |
| 7104 | int alg_arg, |
| 7105 | data_t *input_data, |
| 7106 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7107 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7108 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7109 | psa_key_type_t key_type = key_type_arg; |
| 7110 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7111 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7112 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7113 | size_t output_size; |
| 7114 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7115 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7116 | size_t output2_size; |
| 7117 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7118 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7119 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7120 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7121 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7122 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 7123 | psa_set_key_algorithm( &attributes, alg ); |
| 7124 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7125 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7126 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7127 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7128 | |
| 7129 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7130 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7131 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7132 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7133 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7134 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7135 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7136 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7137 | output2_size = input_data->len; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7138 | TEST_LE_U( output2_size, |
| 7139 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 7140 | TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7141 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7142 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 7143 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 7144 | * the original plaintext because of the non-optional random |
| 7145 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7146 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7147 | input_data->x, input_data->len, |
| 7148 | label->x, label->len, |
| 7149 | output, output_size, |
| 7150 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7151 | /* We don't know what ciphertext length to expect, but check that |
| 7152 | * it looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7153 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7154 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7155 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7156 | output, output_length, |
| 7157 | label->x, label->len, |
| 7158 | output2, output2_size, |
| 7159 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7160 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 7161 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7162 | |
| 7163 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7164 | /* |
| 7165 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7166 | * thus reset them as required. |
| 7167 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7168 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7169 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7170 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7171 | mbedtls_free( output ); |
| 7172 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7173 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7174 | } |
| 7175 | /* END_CASE */ |
| 7176 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7177 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7178 | void asymmetric_decrypt( int key_type_arg, |
| 7179 | data_t *key_data, |
| 7180 | int alg_arg, |
| 7181 | data_t *input_data, |
| 7182 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 7183 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7184 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7185 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7186 | psa_key_type_t key_type = key_type_arg; |
| 7187 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7188 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7189 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 7190 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7191 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7192 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7193 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7194 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7195 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7196 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7197 | psa_set_key_algorithm( &attributes, alg ); |
| 7198 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7199 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7200 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7201 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7202 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7203 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 7204 | key_bits = psa_get_key_bits( &attributes ); |
| 7205 | |
| 7206 | /* Determine the maximum ciphertext length */ |
| 7207 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7208 | TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7209 | ASSERT_ALLOC( output, output_size ); |
| 7210 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7211 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7212 | input_data->x, input_data->len, |
| 7213 | label->x, label->len, |
| 7214 | output, |
| 7215 | output_size, |
| 7216 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7217 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7218 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7219 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7220 | /* If the label is empty, the test framework puts a non-null pointer |
| 7221 | * in label->x. Test that a null pointer works as well. */ |
| 7222 | if( label->len == 0 ) |
| 7223 | { |
| 7224 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7225 | if( output_size != 0 ) |
| 7226 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7227 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7228 | input_data->x, input_data->len, |
| 7229 | NULL, label->len, |
| 7230 | output, |
| 7231 | output_size, |
| 7232 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7233 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7234 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7235 | } |
| 7236 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7237 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7238 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7239 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7240 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7241 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7242 | } |
| 7243 | /* END_CASE */ |
| 7244 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7245 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7246 | void asymmetric_decrypt_fail( int key_type_arg, |
| 7247 | data_t *key_data, |
| 7248 | int alg_arg, |
| 7249 | data_t *input_data, |
| 7250 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7251 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7252 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7253 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7254 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7255 | psa_key_type_t key_type = key_type_arg; |
| 7256 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7257 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7258 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7259 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7260 | psa_status_t actual_status; |
| 7261 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7262 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7263 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7264 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7265 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7266 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7267 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7268 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7269 | psa_set_key_algorithm( &attributes, alg ); |
| 7270 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7271 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7272 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7273 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7274 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7275 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7276 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7277 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7278 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7279 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7280 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7281 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7282 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7283 | /* If the label is empty, the test framework puts a non-null pointer |
| 7284 | * in label->x. Test that a null pointer works as well. */ |
| 7285 | if( label->len == 0 ) |
| 7286 | { |
| 7287 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7288 | if( output_size != 0 ) |
| 7289 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7290 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7291 | input_data->x, input_data->len, |
| 7292 | NULL, label->len, |
| 7293 | output, output_size, |
| 7294 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7295 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7296 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7297 | } |
| 7298 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7299 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7300 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7301 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7302 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7303 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7304 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7305 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7306 | |
| 7307 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 7308 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7309 | { |
| 7310 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 7311 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 7312 | * though it's OK by the C standard. We could test for this, but we'd need |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 7313 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7314 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7315 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 7316 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7317 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7318 | |
| 7319 | memset( &zero, 0, sizeof( zero ) ); |
| 7320 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7321 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7322 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7323 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7324 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7325 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7326 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7327 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7328 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7329 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7330 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 7331 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 7332 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7333 | } |
| 7334 | /* END_CASE */ |
| 7335 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7336 | /* BEGIN_CASE */ |
| 7337 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7338 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7339 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7340 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7341 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7342 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7343 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7344 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7345 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7346 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7347 | |
| 7348 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7349 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7350 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7351 | } |
| 7352 | /* END_CASE */ |
| 7353 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7354 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7355 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 7356 | int expected_status_arg ) |
| 7357 | { |
| 7358 | psa_algorithm_t alg = alg_arg; |
| 7359 | size_t capacity = capacity_arg; |
| 7360 | psa_status_t expected_status = expected_status_arg; |
| 7361 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7362 | |
| 7363 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7364 | |
| 7365 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7366 | |
| 7367 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 7368 | expected_status ); |
| 7369 | |
| 7370 | exit: |
| 7371 | psa_key_derivation_abort( &operation ); |
| 7372 | PSA_DONE( ); |
| 7373 | } |
| 7374 | /* END_CASE */ |
| 7375 | |
| 7376 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7377 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7378 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7379 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7380 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7381 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7382 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7383 | int expected_status_arg3, |
| 7384 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7385 | { |
| 7386 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7387 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 7388 | psa_key_type_t key_types[] = {key_type_arg1, key_type_arg2, key_type_arg3}; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7389 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 7390 | expected_status_arg2, |
| 7391 | expected_status_arg3}; |
| 7392 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7393 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 7394 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7395 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7396 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7397 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7398 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7399 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7400 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7401 | psa_status_t expected_output_status = expected_output_status_arg; |
| 7402 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7403 | |
| 7404 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7405 | |
| 7406 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7407 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7408 | |
| 7409 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7410 | |
| 7411 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 7412 | { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 7413 | mbedtls_test_set_step( i ); |
| 7414 | if( steps[i] == 0 ) |
| 7415 | { |
| 7416 | /* Skip this step */ |
| 7417 | } |
| 7418 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7419 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7420 | psa_set_key_type( &attributes, key_types[i] ); |
| 7421 | PSA_ASSERT( psa_import_key( &attributes, |
| 7422 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7423 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7424 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 7425 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 7426 | { |
| 7427 | // When taking a private key as secret input, use key agreement |
| 7428 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7429 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 7430 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7431 | expected_statuses[i] ); |
| 7432 | } |
| 7433 | else |
| 7434 | { |
| 7435 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7436 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7437 | expected_statuses[i] ); |
| 7438 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7439 | } |
| 7440 | else |
| 7441 | { |
| 7442 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 7443 | &operation, steps[i], |
| 7444 | inputs[i]->x, inputs[i]->len ), |
| 7445 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7446 | } |
| 7447 | } |
| 7448 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7449 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 7450 | { |
| 7451 | psa_reset_key_attributes( &attributes ); |
Dave Rodgman | 491d849 | 2021-11-16 12:12:49 +0000 | [diff] [blame] | 7452 | psa_set_key_type( &attributes, output_key_type ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7453 | psa_set_key_bits( &attributes, 8 ); |
| 7454 | actual_output_status = |
| 7455 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7456 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7457 | } |
| 7458 | else |
| 7459 | { |
| 7460 | uint8_t buffer[1]; |
| 7461 | actual_output_status = |
| 7462 | psa_key_derivation_output_bytes( &operation, |
| 7463 | buffer, sizeof( buffer ) ); |
| 7464 | } |
| 7465 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 7466 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7467 | exit: |
| 7468 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7469 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7470 | psa_destroy_key( keys[i] ); |
| 7471 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7472 | PSA_DONE( ); |
| 7473 | } |
| 7474 | /* END_CASE */ |
| 7475 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7476 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7477 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7478 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7479 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7480 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 7481 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7482 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7483 | unsigned char input1[] = "Input 1"; |
| 7484 | size_t input1_length = sizeof( input1 ); |
| 7485 | unsigned char input2[] = "Input 2"; |
| 7486 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7487 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 7488 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 7489 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7490 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7491 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7492 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7493 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7494 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7495 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7496 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7497 | psa_set_key_algorithm( &attributes, alg ); |
| 7498 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7499 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 7500 | PSA_ASSERT( psa_import_key( &attributes, |
| 7501 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7502 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7503 | |
| 7504 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7505 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7506 | input1, input1_length, |
| 7507 | input2, input2_length, |
| 7508 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7509 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7510 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7511 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7512 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7513 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7514 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7515 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7516 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7517 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7518 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7519 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7520 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7521 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7522 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7523 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7524 | } |
| 7525 | /* END_CASE */ |
| 7526 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7527 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7528 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7529 | { |
| 7530 | uint8_t output_buffer[16]; |
| 7531 | size_t buffer_size = 16; |
| 7532 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7533 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7534 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7535 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7536 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7537 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7538 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7539 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7540 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7541 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7542 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7543 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7544 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7545 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7546 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7547 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7548 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7549 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7550 | |
| 7551 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7552 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7553 | } |
| 7554 | /* END_CASE */ |
| 7555 | |
| 7556 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7557 | void derive_output( int alg_arg, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7558 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 7559 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 7560 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 7561 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 7562 | data_t *key_agreement_peer_key, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7563 | int requested_capacity_arg, |
| 7564 | data_t *expected_output1, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7565 | data_t *expected_output2, |
| 7566 | int other_key_input_type, |
| 7567 | int key_input_type, |
| 7568 | int derive_type ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7569 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7570 | psa_algorithm_t alg = alg_arg; |
Przemek Stekiel | ffbb7d3 | 2022-03-31 11:13:47 +0200 | [diff] [blame] | 7571 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg}; |
| 7572 | data_t *inputs[] = {input1, input2, input3, input4}; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7573 | mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT, |
| 7574 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7575 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7576 | MBEDTLS_SVC_KEY_ID_INIT}; |
| 7577 | psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2, |
| 7578 | expected_status_arg3, expected_status_arg4}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7579 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7580 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7581 | uint8_t *expected_outputs[2] = |
| 7582 | {expected_output1->x, expected_output2->x}; |
| 7583 | size_t output_sizes[2] = |
| 7584 | {expected_output1->len, expected_output2->len}; |
| 7585 | size_t output_buffer_size = 0; |
| 7586 | uint8_t *output_buffer = NULL; |
| 7587 | size_t expected_capacity; |
| 7588 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7589 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 7590 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 7591 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 7592 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7593 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7594 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7595 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7596 | |
| 7597 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 7598 | { |
| 7599 | if( output_sizes[i] > output_buffer_size ) |
| 7600 | output_buffer_size = output_sizes[i]; |
| 7601 | if( output_sizes[i] == 0 ) |
| 7602 | expected_outputs[i] = NULL; |
| 7603 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7604 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7605 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7606 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7607 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7608 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7609 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 7610 | requested_capacity ) ); |
| 7611 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7612 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7613 | switch( steps[i] ) |
| 7614 | { |
| 7615 | case 0: |
| 7616 | break; |
| 7617 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7618 | switch( key_input_type ) |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7619 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7620 | case 0: // input bytes |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7621 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7622 | &operation, steps[i], |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7623 | inputs[i]->x, inputs[i]->len ), |
| 7624 | statuses[i] ); |
| 7625 | |
| 7626 | if( statuses[i] != PSA_SUCCESS ) |
| 7627 | goto exit; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7628 | break; |
| 7629 | case 1: // input key |
| 7630 | psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE ); |
| 7631 | psa_set_key_algorithm( &attributes1, alg ); |
| 7632 | psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE ); |
| 7633 | |
| 7634 | PSA_ASSERT( psa_import_key( &attributes1, |
| 7635 | inputs[i]->x, inputs[i]->len, |
| 7636 | &keys[i] ) ); |
| 7637 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7638 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7639 | { |
| 7640 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7641 | TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ), |
| 7642 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7643 | } |
| 7644 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7645 | PSA_ASSERT( psa_key_derivation_input_key( &operation, |
| 7646 | steps[i], |
| 7647 | keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7648 | break; |
| 7649 | default: |
| 7650 | TEST_ASSERT( ! "default case not supported" ); |
| 7651 | break; |
| 7652 | } |
| 7653 | break; |
| 7654 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7655 | switch( other_key_input_type ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7656 | { |
| 7657 | case 0: // input bytes |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7658 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
| 7659 | steps[i], |
| 7660 | inputs[i]->x, |
| 7661 | inputs[i]->len ), |
| 7662 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7663 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7664 | case 1: // input key, type DERIVE |
| 7665 | case 11: // input key, type RAW |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7666 | psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE ); |
| 7667 | psa_set_key_algorithm( &attributes2, alg ); |
| 7668 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE ); |
| 7669 | |
| 7670 | // other secret of type RAW_DATA passed with input_key |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7671 | if( other_key_input_type == 11 ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7672 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA ); |
| 7673 | |
| 7674 | PSA_ASSERT( psa_import_key( &attributes2, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7675 | inputs[i]->x, inputs[i]->len, |
| 7676 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7677 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7678 | TEST_EQUAL( psa_key_derivation_input_key( &operation, |
| 7679 | steps[i], |
| 7680 | keys[i] ), |
| 7681 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7682 | break; |
| 7683 | case 2: // key agreement |
| 7684 | psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE ); |
| 7685 | psa_set_key_algorithm( &attributes3, alg ); |
| 7686 | psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) ); |
| 7687 | |
| 7688 | PSA_ASSERT( psa_import_key( &attributes3, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7689 | inputs[i]->x, inputs[i]->len, |
| 7690 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7691 | |
| 7692 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 7693 | &operation, |
| 7694 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 7695 | keys[i], key_agreement_peer_key->x, |
| 7696 | key_agreement_peer_key->len ), statuses[i] ); |
| 7697 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7698 | default: |
| 7699 | TEST_ASSERT( ! "default case not supported" ); |
| 7700 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7701 | } |
| 7702 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7703 | if( statuses[i] != PSA_SUCCESS ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7704 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7705 | break; |
| 7706 | default: |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7707 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7708 | &operation, steps[i], |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7709 | inputs[i]->x, inputs[i]->len ), statuses[i] ); |
| 7710 | |
| 7711 | if( statuses[i] != PSA_SUCCESS ) |
| 7712 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7713 | break; |
| 7714 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7715 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7716 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7717 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7718 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7719 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7720 | expected_capacity = requested_capacity; |
| 7721 | |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7722 | if( derive_type == 1 ) // output key |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7723 | { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7724 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 7725 | |
| 7726 | /* For output key derivation secret must be provided using |
| 7727 | input key, otherwise operation is not permitted. */ |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 7728 | if( key_input_type == 1 ) |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7729 | expected_status = PSA_SUCCESS; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7730 | |
| 7731 | psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT ); |
| 7732 | psa_set_key_algorithm( &attributes4, alg ); |
| 7733 | psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE ); |
Przemek Stekiel | 0e99391 | 2022-06-03 15:01:14 +0200 | [diff] [blame] | 7734 | psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7735 | |
| 7736 | TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation, |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7737 | &derived_key ), expected_status ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7738 | } |
| 7739 | else // output bytes |
| 7740 | { |
| 7741 | /* Expansion phase. */ |
| 7742 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7743 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7744 | /* Read some bytes. */ |
| 7745 | status = psa_key_derivation_output_bytes( &operation, |
| 7746 | output_buffer, output_sizes[i] ); |
| 7747 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 7748 | { |
| 7749 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 7750 | TEST_ASSERT( status == PSA_SUCCESS || |
| 7751 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
| 7752 | continue; |
| 7753 | } |
| 7754 | else if( expected_capacity == 0 || |
| 7755 | output_sizes[i] > expected_capacity ) |
| 7756 | { |
| 7757 | /* Capacity exceeded. */ |
| 7758 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
| 7759 | expected_capacity = 0; |
| 7760 | continue; |
| 7761 | } |
| 7762 | /* Success. Check the read data. */ |
| 7763 | PSA_ASSERT( status ); |
| 7764 | if( output_sizes[i] != 0 ) |
| 7765 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 7766 | expected_outputs[i], output_sizes[i] ); |
| 7767 | /* Check the operation status. */ |
| 7768 | expected_capacity -= output_sizes[i]; |
| 7769 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
| 7770 | ¤t_capacity ) ); |
| 7771 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7772 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7773 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7774 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7775 | |
| 7776 | exit: |
| 7777 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7778 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7779 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7780 | psa_destroy_key( keys[i] ); |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7781 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7782 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7783 | } |
| 7784 | /* END_CASE */ |
| 7785 | |
| 7786 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7787 | void derive_full( int alg_arg, |
| 7788 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7789 | data_t *input1, |
| 7790 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7791 | int requested_capacity_arg ) |
| 7792 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7793 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7794 | psa_algorithm_t alg = alg_arg; |
| 7795 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7796 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7797 | unsigned char output_buffer[16]; |
| 7798 | size_t expected_capacity = requested_capacity; |
| 7799 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7800 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7801 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7802 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7803 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7804 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7805 | psa_set_key_algorithm( &attributes, alg ); |
| 7806 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7807 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7808 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7809 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7810 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7811 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7812 | input1->x, input1->len, |
| 7813 | input2->x, input2->len, |
| 7814 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 7815 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7816 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7817 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7818 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7819 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7820 | |
| 7821 | /* Expansion phase. */ |
| 7822 | while( current_capacity > 0 ) |
| 7823 | { |
| 7824 | size_t read_size = sizeof( output_buffer ); |
| 7825 | if( read_size > current_capacity ) |
| 7826 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7827 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7828 | output_buffer, |
| 7829 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7830 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7831 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7832 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7833 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7834 | } |
| 7835 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7836 | /* Check that the operation refuses to go over capacity. */ |
| 7837 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7838 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7839 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7840 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7841 | |
| 7842 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7843 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7844 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7845 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7846 | } |
| 7847 | /* END_CASE */ |
| 7848 | |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 7849 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256:MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */ |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7850 | void derive_ecjpake_to_pms( data_t *input, int expected_input_status_arg, |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7851 | int derivation_step, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7852 | int capacity, int expected_capacity_status_arg, |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7853 | data_t *expected_output, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7854 | int expected_output_status_arg ) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7855 | { |
| 7856 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 7857 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 7858 | psa_key_derivation_step_t step = (psa_key_derivation_step_t) derivation_step; |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7859 | uint8_t *output_buffer = NULL; |
| 7860 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7861 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 7862 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 7863 | psa_status_t expected_output_status = (psa_status_t) expected_output_status_arg; |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7864 | |
| 7865 | ASSERT_ALLOC( output_buffer, expected_output->len ); |
| 7866 | PSA_ASSERT( psa_crypto_init() ); |
| 7867 | |
| 7868 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7869 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7870 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7871 | |
| 7872 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7873 | step, input->x, input->len ), |
| 7874 | expected_input_status ); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7875 | |
| 7876 | if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS ) |
| 7877 | goto exit; |
| 7878 | |
| 7879 | status = psa_key_derivation_output_bytes( &operation, output_buffer, |
| 7880 | expected_output->len ); |
| 7881 | |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7882 | TEST_EQUAL( status, expected_output_status ); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7883 | if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS ) |
| 7884 | ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x, |
| 7885 | expected_output->len ); |
| 7886 | |
| 7887 | exit: |
| 7888 | mbedtls_free( output_buffer ); |
| 7889 | psa_key_derivation_abort( &operation ); |
| 7890 | PSA_DONE(); |
| 7891 | } |
| 7892 | /* END_CASE */ |
| 7893 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7894 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7895 | void derive_key_exercise( int alg_arg, |
| 7896 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7897 | data_t *input1, |
| 7898 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7899 | int derived_type_arg, |
| 7900 | int derived_bits_arg, |
| 7901 | int derived_usage_arg, |
| 7902 | int derived_alg_arg ) |
| 7903 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7904 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7905 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7906 | psa_algorithm_t alg = alg_arg; |
| 7907 | psa_key_type_t derived_type = derived_type_arg; |
| 7908 | size_t derived_bits = derived_bits_arg; |
| 7909 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 7910 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 7911 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7912 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7913 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7914 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7915 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7916 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7917 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7918 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7919 | psa_set_key_algorithm( &attributes, alg ); |
| 7920 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7921 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7922 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7923 | |
| 7924 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7925 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7926 | input1->x, input1->len, |
| 7927 | input2->x, input2->len, |
| 7928 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7929 | goto exit; |
| 7930 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7931 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 7932 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 7933 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7934 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7935 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7936 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7937 | |
| 7938 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7939 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7940 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 7941 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7942 | |
| 7943 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7944 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7945 | goto exit; |
| 7946 | |
| 7947 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7948 | /* |
| 7949 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7950 | * thus reset them as required. |
| 7951 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7952 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7953 | |
| 7954 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7955 | psa_destroy_key( base_key ); |
| 7956 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7957 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7958 | } |
| 7959 | /* END_CASE */ |
| 7960 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7961 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7962 | void derive_key_export( int alg_arg, |
| 7963 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7964 | data_t *input1, |
| 7965 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7966 | int bytes1_arg, |
| 7967 | int bytes2_arg ) |
| 7968 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7969 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7970 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7971 | psa_algorithm_t alg = alg_arg; |
| 7972 | size_t bytes1 = bytes1_arg; |
| 7973 | size_t bytes2 = bytes2_arg; |
| 7974 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7975 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7976 | uint8_t *output_buffer = NULL; |
| 7977 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7978 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7979 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7980 | size_t length; |
| 7981 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7982 | ASSERT_ALLOC( output_buffer, capacity ); |
| 7983 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7984 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7985 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7986 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7987 | psa_set_key_algorithm( &base_attributes, alg ); |
| 7988 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7989 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7990 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7991 | |
| 7992 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7993 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7994 | input1->x, input1->len, |
| 7995 | input2->x, input2->len, |
| 7996 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7997 | goto exit; |
| 7998 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7999 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8000 | output_buffer, |
| 8001 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8002 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8003 | |
| 8004 | /* Derive the same output again, but this time store it in key objects. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8005 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 8006 | input1->x, input1->len, |
| 8007 | input2->x, input2->len, |
| 8008 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8009 | goto exit; |
| 8010 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8011 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8012 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 8013 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8014 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8015 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8016 | &derived_key ) ); |
| 8017 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8018 | export_buffer, bytes1, |
| 8019 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8020 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8021 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8022 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8023 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8024 | &derived_key ) ); |
| 8025 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8026 | export_buffer + bytes1, bytes2, |
| 8027 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8028 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8029 | |
| 8030 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8031 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 8032 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8033 | |
| 8034 | exit: |
| 8035 | mbedtls_free( output_buffer ); |
| 8036 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8037 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8038 | psa_destroy_key( base_key ); |
| 8039 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8040 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8041 | } |
| 8042 | /* END_CASE */ |
| 8043 | |
| 8044 | /* BEGIN_CASE */ |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8045 | void derive_key_type( int alg_arg, |
| 8046 | data_t *key_data, |
| 8047 | data_t *input1, |
| 8048 | data_t *input2, |
| 8049 | int key_type_arg, int bits_arg, |
| 8050 | data_t *expected_export ) |
| 8051 | { |
| 8052 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8053 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8054 | const psa_algorithm_t alg = alg_arg; |
| 8055 | const psa_key_type_t key_type = key_type_arg; |
| 8056 | const size_t bits = bits_arg; |
| 8057 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8058 | const size_t export_buffer_size = |
| 8059 | PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits ); |
| 8060 | uint8_t *export_buffer = NULL; |
| 8061 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8062 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8063 | size_t export_length; |
| 8064 | |
| 8065 | ASSERT_ALLOC( export_buffer, export_buffer_size ); |
| 8066 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8067 | |
| 8068 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8069 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8070 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8071 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 8072 | &base_key ) ); |
| 8073 | |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 8074 | if( mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8075 | &operation, base_key, alg, |
| 8076 | input1->x, input1->len, |
| 8077 | input2->x, input2->len, |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 8078 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 ) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8079 | goto exit; |
| 8080 | |
| 8081 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8082 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 8083 | psa_set_key_type( &derived_attributes, key_type ); |
| 8084 | psa_set_key_bits( &derived_attributes, bits ); |
| 8085 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 8086 | &derived_key ) ); |
| 8087 | |
| 8088 | PSA_ASSERT( psa_export_key( derived_key, |
| 8089 | export_buffer, export_buffer_size, |
| 8090 | &export_length ) ); |
| 8091 | ASSERT_COMPARE( export_buffer, export_length, |
| 8092 | expected_export->x, expected_export->len ); |
| 8093 | |
| 8094 | exit: |
| 8095 | mbedtls_free( export_buffer ); |
| 8096 | psa_key_derivation_abort( &operation ); |
| 8097 | psa_destroy_key( base_key ); |
| 8098 | psa_destroy_key( derived_key ); |
| 8099 | PSA_DONE( ); |
| 8100 | } |
| 8101 | /* END_CASE */ |
| 8102 | |
| 8103 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8104 | void derive_key( int alg_arg, |
| 8105 | data_t *key_data, data_t *input1, data_t *input2, |
| 8106 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8107 | int expected_status_arg, |
| 8108 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8109 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8110 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8111 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8112 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8113 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8114 | size_t bits = bits_arg; |
| 8115 | psa_status_t expected_status = expected_status_arg; |
| 8116 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8117 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8118 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8119 | |
| 8120 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8121 | |
| 8122 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8123 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8124 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8125 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8126 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8127 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8128 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 8129 | input1->x, input1->len, |
| 8130 | input2->x, input2->len, |
| 8131 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8132 | goto exit; |
| 8133 | |
| 8134 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8135 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8136 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8137 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8138 | |
| 8139 | psa_status_t status = |
| 8140 | psa_key_derivation_output_key( &derived_attributes, |
| 8141 | &operation, |
| 8142 | &derived_key ); |
| 8143 | if( is_large_output > 0 ) |
| 8144 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8145 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8146 | |
| 8147 | exit: |
| 8148 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8149 | psa_destroy_key( base_key ); |
| 8150 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8151 | PSA_DONE( ); |
| 8152 | } |
| 8153 | /* END_CASE */ |
| 8154 | |
| 8155 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8156 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 8157 | int our_key_type_arg, int our_key_alg_arg, |
| 8158 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8159 | int expected_status_arg ) |
| 8160 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8161 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8162 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8163 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8164 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8165 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8166 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8167 | psa_status_t expected_status = expected_status_arg; |
| 8168 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8169 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8170 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8171 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8172 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8173 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8174 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8175 | PSA_ASSERT( psa_import_key( &attributes, |
| 8176 | our_key_data->x, our_key_data->len, |
| 8177 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8178 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8179 | /* The tests currently include inputs that should fail at either step. |
| 8180 | * Test cases that fail at the setup step should be changed to call |
| 8181 | * key_derivation_setup instead, and this function should be renamed |
| 8182 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8183 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8184 | if( status == PSA_SUCCESS ) |
| 8185 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8186 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 8187 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 8188 | our_key, |
| 8189 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8190 | expected_status ); |
| 8191 | } |
| 8192 | else |
| 8193 | { |
| 8194 | TEST_ASSERT( status == expected_status ); |
| 8195 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8196 | |
| 8197 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8198 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8199 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8200 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8201 | } |
| 8202 | /* END_CASE */ |
| 8203 | |
| 8204 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8205 | void raw_key_agreement( int alg_arg, |
| 8206 | int our_key_type_arg, data_t *our_key_data, |
| 8207 | data_t *peer_key_data, |
| 8208 | data_t *expected_output ) |
| 8209 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8210 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8211 | psa_algorithm_t alg = alg_arg; |
| 8212 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8213 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8214 | unsigned char *output = NULL; |
| 8215 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8216 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8217 | |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8218 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8219 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8220 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8221 | psa_set_key_algorithm( &attributes, alg ); |
| 8222 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8223 | PSA_ASSERT( psa_import_key( &attributes, |
| 8224 | our_key_data->x, our_key_data->len, |
| 8225 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8226 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8227 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 8228 | key_bits = psa_get_key_bits( &attributes ); |
| 8229 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8230 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8231 | TEST_LE_U( expected_output->len, |
| 8232 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 8233 | TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ), |
| 8234 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8235 | |
| 8236 | /* Good case with exact output size */ |
| 8237 | ASSERT_ALLOC( output, expected_output->len ); |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 8238 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8239 | peer_key_data->x, peer_key_data->len, |
| 8240 | output, expected_output->len, |
| 8241 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8242 | ASSERT_COMPARE( output, output_length, |
| 8243 | expected_output->x, expected_output->len ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8244 | mbedtls_free( output ); |
| 8245 | output = NULL; |
| 8246 | output_length = ~0; |
| 8247 | |
| 8248 | /* Larger buffer */ |
| 8249 | ASSERT_ALLOC( output, expected_output->len + 1 ); |
| 8250 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8251 | peer_key_data->x, peer_key_data->len, |
| 8252 | output, expected_output->len + 1, |
| 8253 | &output_length ) ); |
| 8254 | ASSERT_COMPARE( output, output_length, |
| 8255 | expected_output->x, expected_output->len ); |
| 8256 | mbedtls_free( output ); |
| 8257 | output = NULL; |
| 8258 | output_length = ~0; |
| 8259 | |
| 8260 | /* Buffer too small */ |
| 8261 | ASSERT_ALLOC( output, expected_output->len - 1 ); |
| 8262 | TEST_EQUAL( psa_raw_key_agreement( alg, our_key, |
| 8263 | peer_key_data->x, peer_key_data->len, |
| 8264 | output, expected_output->len - 1, |
| 8265 | &output_length ), |
| 8266 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8267 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8268 | TEST_LE_U( output_length, expected_output->len - 1 ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8269 | mbedtls_free( output ); |
| 8270 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8271 | |
| 8272 | exit: |
| 8273 | mbedtls_free( output ); |
| 8274 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8275 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8276 | } |
| 8277 | /* END_CASE */ |
| 8278 | |
| 8279 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8280 | void key_agreement_capacity( int alg_arg, |
| 8281 | int our_key_type_arg, data_t *our_key_data, |
| 8282 | data_t *peer_key_data, |
| 8283 | int expected_capacity_arg ) |
| 8284 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8285 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8286 | psa_algorithm_t alg = alg_arg; |
| 8287 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8288 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8289 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8290 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8291 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8292 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8293 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8294 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8295 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8296 | psa_set_key_algorithm( &attributes, alg ); |
| 8297 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8298 | PSA_ASSERT( psa_import_key( &attributes, |
| 8299 | our_key_data->x, our_key_data->len, |
| 8300 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8301 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8302 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8303 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8304 | &operation, |
| 8305 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8306 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8307 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8308 | { |
| 8309 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8310 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8311 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8312 | NULL, 0 ) ); |
| 8313 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8314 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 8315 | /* Test the advertised capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8316 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8317 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8318 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8319 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8320 | /* Test the actual capacity by reading the output. */ |
| 8321 | while( actual_capacity > sizeof( output ) ) |
| 8322 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8323 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8324 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8325 | actual_capacity -= sizeof( output ); |
| 8326 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8327 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8328 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8329 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 8330 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8331 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8332 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8333 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8334 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8335 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8336 | } |
| 8337 | /* END_CASE */ |
| 8338 | |
| 8339 | /* BEGIN_CASE */ |
| 8340 | void key_agreement_output( int alg_arg, |
| 8341 | int our_key_type_arg, data_t *our_key_data, |
| 8342 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8343 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8344 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8345 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8346 | psa_algorithm_t alg = alg_arg; |
| 8347 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8348 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8349 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8350 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8351 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8352 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 8353 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8354 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8355 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8356 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8357 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8358 | psa_set_key_algorithm( &attributes, alg ); |
| 8359 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8360 | PSA_ASSERT( psa_import_key( &attributes, |
| 8361 | our_key_data->x, our_key_data->len, |
| 8362 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8363 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8364 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8365 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8366 | &operation, |
| 8367 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8368 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8369 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8370 | { |
| 8371 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8372 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8373 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8374 | NULL, 0 ) ); |
| 8375 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8376 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8377 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8378 | actual_output, |
| 8379 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8380 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 8381 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8382 | if( expected_output2->len != 0 ) |
| 8383 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8384 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8385 | actual_output, |
| 8386 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8387 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 8388 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8389 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8390 | |
| 8391 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8392 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8393 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8394 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8395 | mbedtls_free( actual_output ); |
| 8396 | } |
| 8397 | /* END_CASE */ |
| 8398 | |
| 8399 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8400 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8401 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8402 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8403 | unsigned char *output = NULL; |
| 8404 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8405 | size_t i; |
| 8406 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8407 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 8408 | TEST_ASSERT( bytes_arg >= 0 ); |
| 8409 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 8410 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8411 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8412 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8413 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8414 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8415 | /* Run several times, to ensure that every output byte will be |
| 8416 | * nonzero at least once with overwhelming probability |
| 8417 | * (2^(-8*number_of_runs)). */ |
| 8418 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8419 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 8420 | if( bytes != 0 ) |
| 8421 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8422 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8423 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8424 | for( i = 0; i < bytes; i++ ) |
| 8425 | { |
| 8426 | if( output[i] != 0 ) |
| 8427 | ++changed[i]; |
| 8428 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8429 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8430 | |
| 8431 | /* Check that every byte was changed to nonzero at least once. This |
| 8432 | * validates that psa_generate_random is overwriting every byte of |
| 8433 | * the output buffer. */ |
| 8434 | for( i = 0; i < bytes; i++ ) |
| 8435 | { |
| 8436 | TEST_ASSERT( changed[i] != 0 ); |
| 8437 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8438 | |
| 8439 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8440 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8441 | mbedtls_free( output ); |
| 8442 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8443 | } |
| 8444 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8445 | |
| 8446 | /* BEGIN_CASE */ |
| 8447 | void generate_key( int type_arg, |
| 8448 | int bits_arg, |
| 8449 | int usage_arg, |
| 8450 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8451 | int expected_status_arg, |
| 8452 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8453 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8454 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8455 | psa_key_type_t type = type_arg; |
| 8456 | psa_key_usage_t usage = usage_arg; |
| 8457 | size_t bits = bits_arg; |
| 8458 | psa_algorithm_t alg = alg_arg; |
| 8459 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8460 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8461 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8462 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8463 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8464 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8465 | psa_set_key_usage_flags( &attributes, usage ); |
| 8466 | psa_set_key_algorithm( &attributes, alg ); |
| 8467 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8468 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8469 | |
| 8470 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8471 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 8472 | |
| 8473 | if( is_large_key > 0 ) |
| 8474 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8475 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8476 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8477 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8478 | |
| 8479 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8480 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8481 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 8482 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8483 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 8484 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8485 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 8486 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8487 | |
| 8488 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8489 | /* |
| 8490 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8491 | * thus reset them as required. |
| 8492 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8493 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8494 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8495 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8496 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8497 | } |
| 8498 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 8499 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 8500 | /* BEGIN_CASE depends_on:PSA_WANT_KEY_TYPE_RSA_KEY_PAIR:PSA_WANT_ALG_RSA_PKCS1V15_CRYPT:PSA_WANT_ALG_RSA_PKCS1V15_SIGN:MBEDTLS_GENPRIME */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8501 | void generate_key_rsa( int bits_arg, |
| 8502 | data_t *e_arg, |
| 8503 | int expected_status_arg ) |
| 8504 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8505 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 8506 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8507 | size_t bits = bits_arg; |
| 8508 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 8509 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 8510 | psa_status_t expected_status = expected_status_arg; |
| 8511 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8512 | uint8_t *exported = NULL; |
| 8513 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8514 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8515 | size_t exported_length = SIZE_MAX; |
| 8516 | uint8_t *e_read_buffer = NULL; |
| 8517 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 8518 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8519 | size_t e_read_length = SIZE_MAX; |
| 8520 | |
| 8521 | if( e_arg->len == 0 || |
| 8522 | ( e_arg->len == 3 && |
| 8523 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 8524 | { |
| 8525 | is_default_public_exponent = 1; |
| 8526 | e_read_size = 0; |
| 8527 | } |
| 8528 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 8529 | ASSERT_ALLOC( exported, exported_size ); |
| 8530 | |
| 8531 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8532 | |
| 8533 | psa_set_key_usage_flags( &attributes, usage ); |
| 8534 | psa_set_key_algorithm( &attributes, alg ); |
| 8535 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 8536 | e_arg->x, e_arg->len ) ); |
| 8537 | psa_set_key_bits( &attributes, bits ); |
| 8538 | |
| 8539 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8540 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8541 | if( expected_status != PSA_SUCCESS ) |
| 8542 | goto exit; |
| 8543 | |
| 8544 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8545 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8546 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8547 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 8548 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 8549 | e_read_buffer, e_read_size, |
| 8550 | &e_read_length ) ); |
| 8551 | if( is_default_public_exponent ) |
| 8552 | TEST_EQUAL( e_read_length, 0 ); |
| 8553 | else |
| 8554 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 8555 | |
| 8556 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8557 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8558 | goto exit; |
| 8559 | |
| 8560 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8561 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8562 | exported, exported_size, |
| 8563 | &exported_length ) ); |
| 8564 | { |
| 8565 | uint8_t *p = exported; |
| 8566 | uint8_t *end = exported + exported_length; |
| 8567 | size_t len; |
| 8568 | /* RSAPublicKey ::= SEQUENCE { |
| 8569 | * modulus INTEGER, -- n |
| 8570 | * publicExponent INTEGER } -- e |
| 8571 | */ |
| 8572 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8573 | MBEDTLS_ASN1_SEQUENCE | |
| 8574 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 8575 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8576 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 8577 | MBEDTLS_ASN1_INTEGER ) ); |
| 8578 | if( len >= 1 && p[0] == 0 ) |
| 8579 | { |
| 8580 | ++p; |
| 8581 | --len; |
| 8582 | } |
| 8583 | if( e_arg->len == 0 ) |
| 8584 | { |
| 8585 | TEST_EQUAL( len, 3 ); |
| 8586 | TEST_EQUAL( p[0], 1 ); |
| 8587 | TEST_EQUAL( p[1], 0 ); |
| 8588 | TEST_EQUAL( p[2], 1 ); |
| 8589 | } |
| 8590 | else |
| 8591 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 8592 | } |
| 8593 | |
| 8594 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8595 | /* |
| 8596 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 8597 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 8598 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8599 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8600 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8601 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8602 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8603 | mbedtls_free( e_read_buffer ); |
| 8604 | mbedtls_free( exported ); |
| 8605 | } |
| 8606 | /* END_CASE */ |
| 8607 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8608 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8609 | void persistent_key_load_key_from_storage( data_t *data, |
| 8610 | int type_arg, int bits_arg, |
| 8611 | int usage_flags_arg, int alg_arg, |
| 8612 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8613 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 8614 | mbedtls_svc_key_id_t key_id = mbedtls_svc_key_id_make( 1, 1 ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8615 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8616 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8617 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8618 | psa_key_type_t type = type_arg; |
| 8619 | size_t bits = bits_arg; |
| 8620 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 8621 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8622 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8623 | unsigned char *first_export = NULL; |
| 8624 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8625 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8626 | size_t first_exported_length; |
| 8627 | size_t second_exported_length; |
| 8628 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8629 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8630 | { |
| 8631 | ASSERT_ALLOC( first_export, export_size ); |
| 8632 | ASSERT_ALLOC( second_export, export_size ); |
| 8633 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8634 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8635 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8636 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 8637 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8638 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 8639 | psa_set_key_algorithm( &attributes, alg ); |
| 8640 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8641 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8642 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8643 | switch( generation_method ) |
| 8644 | { |
| 8645 | case IMPORT_KEY: |
| 8646 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8647 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8648 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8649 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8650 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8651 | case GENERATE_KEY: |
| 8652 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8653 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8654 | break; |
| 8655 | |
| 8656 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 8657 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8658 | { |
| 8659 | /* Create base key */ |
| 8660 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 8661 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8662 | psa_set_key_usage_flags( &base_attributes, |
| 8663 | PSA_KEY_USAGE_DERIVE ); |
| 8664 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 8665 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8666 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 8667 | data->x, data->len, |
| 8668 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8669 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8670 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8671 | PSA_ASSERT( psa_key_derivation_input_key( |
| 8672 | &operation, |
| 8673 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8674 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8675 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8676 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8677 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 8678 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8679 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8680 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8681 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8682 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8683 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 8684 | #else |
| 8685 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 8686 | #endif |
| 8687 | break; |
| 8688 | |
| 8689 | default: |
| 8690 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 8691 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8692 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8693 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8694 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8695 | /* Export the key if permitted by the key policy. */ |
| 8696 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8697 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8698 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8699 | first_export, export_size, |
| 8700 | &first_exported_length ) ); |
| 8701 | if( generation_method == IMPORT_KEY ) |
| 8702 | ASSERT_COMPARE( data->x, data->len, |
| 8703 | first_export, first_exported_length ); |
| 8704 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8705 | |
| 8706 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8707 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8708 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8709 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8710 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8711 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8712 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 8713 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 8714 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8715 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 8716 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 8717 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8718 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 8719 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 8720 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8721 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8722 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8723 | /* Export the key again if permitted by the key policy. */ |
| 8724 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8725 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8726 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8727 | second_export, export_size, |
| 8728 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8729 | ASSERT_COMPARE( first_export, first_exported_length, |
| 8730 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8731 | } |
| 8732 | |
| 8733 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8734 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8735 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8736 | |
| 8737 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8738 | /* |
| 8739 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8740 | * thus reset them as required. |
| 8741 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8742 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8743 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8744 | mbedtls_free( first_export ); |
| 8745 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8746 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8747 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8748 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8749 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8750 | } |
| 8751 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8752 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8753 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8754 | void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 8755 | int primitive_arg, int hash_arg, int role_arg, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8756 | int input_first, data_t *pw_data, |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8757 | int expected_status_setup_arg, |
| 8758 | int expected_status_set_role_arg, |
| 8759 | int expected_status_set_password_key_arg, |
| 8760 | int expected_status_input_output_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8761 | { |
| 8762 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8763 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 8764 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8765 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8766 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 8767 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8768 | psa_algorithm_t hash_alg = hash_arg; |
| 8769 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8770 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8771 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8772 | psa_status_t expected_status_setup = expected_status_setup_arg; |
| 8773 | psa_status_t expected_status_set_role = expected_status_set_role_arg; |
| 8774 | psa_status_t expected_status_set_password_key = |
| 8775 | expected_status_set_password_key_arg; |
| 8776 | psa_status_t expected_status_input_output = |
| 8777 | expected_status_input_output_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8778 | unsigned char *output_buffer = NULL; |
| 8779 | size_t output_len = 0; |
| 8780 | |
| 8781 | PSA_INIT( ); |
| 8782 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8783 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
| 8784 | PSA_PAKE_STEP_KEY_SHARE); |
| 8785 | ASSERT_ALLOC( output_buffer, buf_size ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8786 | |
| 8787 | if( pw_data->len > 0 ) |
| 8788 | { |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8789 | psa_set_key_usage_flags( &attributes, key_usage_pw ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8790 | psa_set_key_algorithm( &attributes, alg ); |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8791 | psa_set_key_type( &attributes, key_type_pw ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8792 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8793 | &key ) ); |
| 8794 | } |
| 8795 | |
| 8796 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8797 | psa_pake_cs_set_primitive( &cipher_suite, primitive ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8798 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8799 | |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8800 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8801 | |
| 8802 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8803 | PSA_ERROR_BAD_STATE ); |
| 8804 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8805 | PSA_ERROR_BAD_STATE ); |
| 8806 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8807 | PSA_ERROR_BAD_STATE ); |
| 8808 | TEST_EQUAL( psa_pake_set_role( &operation, role ), |
| 8809 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8810 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8811 | NULL, 0, NULL ), |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8812 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8813 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8814 | PSA_ERROR_BAD_STATE ); |
| 8815 | |
| 8816 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8817 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8818 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8819 | expected_status_setup ); |
| 8820 | if( expected_status_setup != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8821 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8822 | |
Neil Armstrong | 50de0ae | 2022-06-08 17:46:24 +0200 | [diff] [blame] | 8823 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8824 | PSA_ERROR_BAD_STATE ); |
| 8825 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8826 | TEST_EQUAL( psa_pake_set_role( &operation, role), |
| 8827 | expected_status_set_role ); |
| 8828 | if( expected_status_set_role != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8829 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8830 | |
| 8831 | if( pw_data->len > 0 ) |
| 8832 | { |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8833 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8834 | expected_status_set_password_key ); |
| 8835 | if( expected_status_set_password_key != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8836 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8837 | } |
| 8838 | |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 8839 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8840 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8841 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8842 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8843 | |
| 8844 | const uint8_t unsupported_id[] = "abcd"; |
| 8845 | |
| 8846 | TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ), |
| 8847 | PSA_ERROR_NOT_SUPPORTED ); |
| 8848 | TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ), |
| 8849 | PSA_ERROR_NOT_SUPPORTED ); |
| 8850 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8851 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8852 | PSA_PAKE_STEP_KEY_SHARE ); |
| 8853 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8854 | PSA_PAKE_STEP_ZK_PUBLIC ); |
| 8855 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8856 | PSA_PAKE_STEP_ZK_PROOF ); |
| 8857 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8858 | /* First round */ |
| 8859 | if( input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8860 | { |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8861 | /* Invalid parameters (input) */ |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8862 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8863 | NULL, 0 ), |
| 8864 | PSA_ERROR_INVALID_ARGUMENT ); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8865 | /* Invalid parameters (step) */ |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 8866 | TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8867 | key, pw_data->len ) , 0 ); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8868 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8869 | output_buffer, size_zk_proof ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8870 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8871 | /* Invalid first step */ |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 8872 | TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8873 | key, pw_data->len ), 0 ); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8874 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8875 | output_buffer, size_zk_proof ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8876 | PSA_ERROR_BAD_STATE ); |
| 8877 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8878 | /* Possibly valid */ |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 8879 | TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8880 | key, pw_data->len ), 0 ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8881 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8882 | output_buffer, size_key_share ), |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8883 | expected_status_input_output); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8884 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8885 | if( expected_status_input_output == PSA_SUCCESS ) |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8886 | { |
| 8887 | /* Buffer too large */ |
| 8888 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8889 | output_buffer, size_zk_public + 1 ), |
| 8890 | PSA_ERROR_INVALID_ARGUMENT ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8891 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8892 | /* The operation's state should be invalidated at this point */ |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8893 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8894 | output_buffer, size_zk_public ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8895 | PSA_ERROR_BAD_STATE ); |
| 8896 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8897 | } |
| 8898 | else |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8899 | { |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8900 | /* Invalid parameters (output) */ |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8901 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8902 | NULL, 0, NULL ), |
| 8903 | PSA_ERROR_INVALID_ARGUMENT ); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8904 | /* Invalid parameters (step) */ |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 8905 | TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8906 | key, pw_data->len ), 0 ); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8907 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8908 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8909 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8910 | /* Invalid first step */ |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 8911 | TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8912 | key, pw_data->len ), 0 ); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8913 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8914 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8915 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8916 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8917 | /* Possibly valid */ |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 8918 | TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8919 | key, pw_data->len ), 0 ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8920 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8921 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8922 | expected_status_input_output ); |
Neil Armstrong | 98506ab | 2022-06-08 17:43:20 +0200 | [diff] [blame] | 8923 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8924 | if( expected_status_input_output == PSA_SUCCESS ) |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8925 | { |
| 8926 | TEST_ASSERT( output_len > 0 ); |
| 8927 | |
| 8928 | /* Buffer too small */ |
| 8929 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8930 | output_buffer, size_zk_public - 1, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8931 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8932 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8933 | /* The operation's state should be invalidated at this point */ |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8934 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8935 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8936 | PSA_ERROR_BAD_STATE ); |
| 8937 | } |
| 8938 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8939 | |
| 8940 | exit: |
| 8941 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 8942 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8943 | mbedtls_free( output_buffer ); |
| 8944 | PSA_DONE( ); |
| 8945 | } |
| 8946 | /* END_CASE */ |
| 8947 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8948 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8949 | void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg, |
| 8950 | int client_input_first, int inject_error, |
| 8951 | data_t *pw_data ) |
| 8952 | { |
| 8953 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8954 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8955 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8956 | psa_algorithm_t alg = alg_arg; |
| 8957 | psa_algorithm_t hash_alg = hash_arg; |
| 8958 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8959 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8960 | |
| 8961 | PSA_INIT( ); |
| 8962 | |
| 8963 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8964 | psa_set_key_algorithm( &attributes, alg ); |
| 8965 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8966 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8967 | &key ) ); |
| 8968 | |
| 8969 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8970 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8971 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8972 | |
| 8973 | |
| 8974 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 8975 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 8976 | |
| 8977 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 8978 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 8979 | |
| 8980 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 8981 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 8982 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8983 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8984 | client_input_first, 1, inject_error ); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8985 | |
| 8986 | if( inject_error == 1 || inject_error == 2 ) |
| 8987 | goto exit; |
| 8988 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8989 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8990 | client_input_first, 2, inject_error ); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8991 | |
| 8992 | exit: |
| 8993 | psa_destroy_key( key ); |
| 8994 | psa_pake_abort( &server ); |
| 8995 | psa_pake_abort( &client ); |
| 8996 | PSA_DONE( ); |
| 8997 | } |
| 8998 | /* END_CASE */ |
| 8999 | |
| 9000 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9001 | void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg, |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9002 | int derive_alg_arg, data_t *pw_data, |
Przemek Stekiel | cd356c3 | 2022-11-20 19:05:20 +0100 | [diff] [blame] | 9003 | int client_input_first, int destroy_key ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9004 | { |
| 9005 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9006 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 9007 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 9008 | psa_algorithm_t alg = alg_arg; |
| 9009 | psa_algorithm_t hash_alg = hash_arg; |
| 9010 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 9011 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9012 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9013 | psa_key_derivation_operation_t server_derive = |
| 9014 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9015 | psa_key_derivation_operation_t client_derive = |
| 9016 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9017 | |
| 9018 | PSA_INIT( ); |
| 9019 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9020 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 9021 | psa_set_key_algorithm( &attributes, alg ); |
| 9022 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 9023 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 9024 | &key ) ); |
| 9025 | |
| 9026 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 9027 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 9028 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 9029 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9030 | /* Get shared key */ |
| 9031 | PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) ); |
| 9032 | PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) ); |
| 9033 | |
| 9034 | if( PSA_ALG_IS_TLS12_PRF( derive_alg ) || |
| 9035 | PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) ) |
| 9036 | { |
| 9037 | PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive, |
| 9038 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9039 | (const uint8_t*) "", 0) ); |
| 9040 | PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive, |
| 9041 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9042 | (const uint8_t*) "", 0) ); |
| 9043 | } |
| 9044 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9045 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 9046 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 9047 | |
| 9048 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 9049 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 9050 | |
| 9051 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 9052 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 9053 | |
Przemek Stekiel | cd356c3 | 2022-11-20 19:05:20 +0100 | [diff] [blame] | 9054 | if( destroy_key == 1 ) |
| 9055 | psa_destroy_key( key ); |
| 9056 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9057 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 9058 | PSA_ERROR_BAD_STATE ); |
| 9059 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 9060 | PSA_ERROR_BAD_STATE ); |
| 9061 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9062 | /* First round */ |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 9063 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 9064 | client_input_first, 1, 0 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9065 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9066 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 9067 | PSA_ERROR_BAD_STATE ); |
| 9068 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 9069 | PSA_ERROR_BAD_STATE ); |
| 9070 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9071 | /* Second round */ |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 9072 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 9073 | client_input_first, 2, 0 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9074 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9075 | PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) ); |
| 9076 | PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) ); |
| 9077 | |
| 9078 | exit: |
| 9079 | psa_key_derivation_abort( &server_derive ); |
| 9080 | psa_key_derivation_abort( &client_derive ); |
| 9081 | psa_destroy_key( key ); |
| 9082 | psa_pake_abort( &server ); |
| 9083 | psa_pake_abort( &client ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9084 | PSA_DONE( ); |
| 9085 | } |
| 9086 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9087 | |
| 9088 | /* BEGIN_CASE */ |
| 9089 | void ecjpake_size_macros( ) |
| 9090 | { |
| 9091 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 9092 | const size_t bits = 256; |
| 9093 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
| 9094 | PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits ); |
| 9095 | const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( |
| 9096 | PSA_ECC_FAMILY_SECP_R1 ); |
| 9097 | |
| 9098 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 9099 | /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */ |
| 9100 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9101 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) ); |
| 9102 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9103 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) ); |
| 9104 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
| 9105 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9106 | PSA_BITS_TO_BYTES( bits ) ); |
| 9107 | |
| 9108 | /* Input sizes are the same as output sizes */ |
| 9109 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9110 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE) ); |
| 9111 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9112 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC) ); |
| 9113 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9114 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF) ); |
| 9115 | |
| 9116 | /* These inequalities will always hold even when other PAKEs are added */ |
| 9117 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9118 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9119 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9120 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9121 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9122 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9123 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9124 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9125 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9126 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9127 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9128 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9129 | } |
| 9130 | /* END_CASE */ |