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 | |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 1321 | typedef enum |
| 1322 | { |
| 1323 | INJECT_ERR_NONE = 0, |
| 1324 | INJECT_ERR_UNINITIALIZED_ACCESS, |
| 1325 | INJECT_ERR_DUPLICATE_SETUP, |
| 1326 | INJECT_ERR_INVALID_USER, |
| 1327 | INJECT_ERR_INVALID_PEER, |
| 1328 | INJECT_ERR_SET_USER, |
| 1329 | INJECT_ERR_SET_PEER, |
| 1330 | INJECT_EMPTY_IO_BUFFER, |
| 1331 | INJECT_UNKNOWN_STEP, |
| 1332 | INJECT_INVALID_FIRST_STEP, |
| 1333 | INJECT_WRONG_BUFFER_SIZE, |
| 1334 | INJECT_VALID_OPERATION_AFTER_FAILURE, |
| 1335 | INJECT_ANTICIPATE_KEY_DERIVATION_1, |
| 1336 | INJECT_ANTICIPATE_KEY_DERIVATION_2, |
| 1337 | } ecjpake_injected_failure_t; |
| 1338 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1339 | /* END_HEADER */ |
| 1340 | |
| 1341 | /* BEGIN_DEPENDENCIES |
| 1342 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1343 | * END_DEPENDENCIES |
| 1344 | */ |
| 1345 | |
| 1346 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1347 | void static_checks( ) |
| 1348 | { |
| 1349 | size_t max_truncated_mac_size = |
| 1350 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1351 | |
| 1352 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1353 | * encoding. The shifted mask is the maximum truncated value. The |
| 1354 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1355 | TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size ); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1356 | } |
| 1357 | /* END_CASE */ |
| 1358 | |
| 1359 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1360 | void import_with_policy( int type_arg, |
| 1361 | int usage_arg, int alg_arg, |
| 1362 | int expected_status_arg ) |
| 1363 | { |
| 1364 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1365 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1366 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1367 | psa_key_type_t type = type_arg; |
| 1368 | psa_key_usage_t usage = usage_arg; |
| 1369 | psa_algorithm_t alg = alg_arg; |
| 1370 | psa_status_t expected_status = expected_status_arg; |
| 1371 | const uint8_t key_material[16] = {0}; |
| 1372 | psa_status_t status; |
| 1373 | |
| 1374 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1375 | |
| 1376 | psa_set_key_type( &attributes, type ); |
| 1377 | psa_set_key_usage_flags( &attributes, usage ); |
| 1378 | psa_set_key_algorithm( &attributes, alg ); |
| 1379 | |
| 1380 | status = psa_import_key( &attributes, |
| 1381 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1382 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1383 | TEST_EQUAL( status, expected_status ); |
| 1384 | if( status != PSA_SUCCESS ) |
| 1385 | goto exit; |
| 1386 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1387 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1388 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 1389 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1390 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1391 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1392 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1393 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1394 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1395 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1396 | |
| 1397 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1398 | /* |
| 1399 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1400 | * thus reset them as required. |
| 1401 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1402 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1403 | |
| 1404 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1405 | PSA_DONE( ); |
| 1406 | } |
| 1407 | /* END_CASE */ |
| 1408 | |
| 1409 | /* BEGIN_CASE */ |
| 1410 | void import_with_data( data_t *data, int type_arg, |
| 1411 | int attr_bits_arg, |
| 1412 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1413 | { |
| 1414 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1415 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1416 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1417 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1418 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1419 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1420 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1421 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1422 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1423 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1424 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1425 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1426 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1427 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1428 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1429 | if( status != PSA_SUCCESS ) |
| 1430 | goto exit; |
| 1431 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1432 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1433 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1434 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1435 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1436 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1437 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1438 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1439 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1440 | |
| 1441 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1442 | /* |
| 1443 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1444 | * thus reset them as required. |
| 1445 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1446 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1447 | |
| 1448 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1449 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1450 | } |
| 1451 | /* END_CASE */ |
| 1452 | |
| 1453 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1454 | void import_large_key( int type_arg, int byte_size_arg, |
| 1455 | int expected_status_arg ) |
| 1456 | { |
| 1457 | psa_key_type_t type = type_arg; |
| 1458 | size_t byte_size = byte_size_arg; |
| 1459 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1460 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1461 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1462 | psa_status_t status; |
| 1463 | uint8_t *buffer = NULL; |
| 1464 | size_t buffer_size = byte_size + 1; |
| 1465 | size_t n; |
| 1466 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1467 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1468 | * accommodate large keys due to heap size constraints */ |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1469 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1470 | memset( buffer, 'K', byte_size ); |
| 1471 | |
| 1472 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1473 | |
| 1474 | /* Try importing the key */ |
| 1475 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1476 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1477 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 1478 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1479 | TEST_EQUAL( status, expected_status ); |
| 1480 | |
| 1481 | if( status == PSA_SUCCESS ) |
| 1482 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1483 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1484 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1485 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1486 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1487 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1488 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1489 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1490 | for( n = 0; n < byte_size; n++ ) |
| 1491 | TEST_EQUAL( buffer[n], 'K' ); |
| 1492 | for( n = byte_size; n < buffer_size; n++ ) |
| 1493 | TEST_EQUAL( buffer[n], 0 ); |
| 1494 | } |
| 1495 | |
| 1496 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1497 | /* |
| 1498 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1499 | * thus reset them as required. |
| 1500 | */ |
| 1501 | psa_reset_key_attributes( &attributes ); |
| 1502 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1503 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1504 | PSA_DONE( ); |
| 1505 | mbedtls_free( buffer ); |
| 1506 | } |
| 1507 | /* END_CASE */ |
| 1508 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1509 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1510 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1511 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1512 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1513 | size_t bits = bits_arg; |
| 1514 | psa_status_t expected_status = expected_status_arg; |
| 1515 | psa_status_t status; |
| 1516 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1517 | 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] | 1518 | size_t buffer_size = /* Slight overapproximations */ |
| 1519 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1520 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1521 | unsigned char *p; |
| 1522 | int ret; |
| 1523 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1524 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1525 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1526 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1527 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1528 | |
| 1529 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1530 | bits, keypair ) ) >= 0 ); |
| 1531 | length = ret; |
| 1532 | |
| 1533 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1534 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1535 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1536 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1537 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1538 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1539 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1540 | |
| 1541 | exit: |
| 1542 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1543 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1544 | } |
| 1545 | /* END_CASE */ |
| 1546 | |
| 1547 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1548 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1549 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1550 | int usage_arg, int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1551 | int lifetime_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1552 | int expected_bits, |
| 1553 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1554 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1555 | int canonical_input ) |
| 1556 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1557 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1558 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1559 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1560 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1561 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1562 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1563 | unsigned char *exported = NULL; |
| 1564 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1565 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1566 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1567 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1568 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1569 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1570 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1571 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1572 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1573 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1574 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1575 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1576 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1577 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1578 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1579 | psa_set_key_algorithm( &attributes, alg ); |
| 1580 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1581 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1582 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1583 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1584 | |
| 1585 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1586 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1587 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1588 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1589 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1590 | |
| 1591 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1592 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1593 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1594 | |
| 1595 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1596 | * and export_size. On errors, the exported length must be 0. */ |
| 1597 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1598 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1599 | TEST_LE_U( exported_length, export_size ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1600 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1601 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1602 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1603 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1604 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1605 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1606 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1607 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1608 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1609 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1610 | * this validates the canonical representations. For canonical inputs, |
| 1611 | * this doesn't directly validate the implementation, but it still helps |
| 1612 | * by cross-validating the test data with the sanity check code. */ |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1613 | if( !psa_key_lifetime_is_external( lifetime ) ) |
| 1614 | { |
| 1615 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
| 1616 | goto exit; |
| 1617 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1618 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1619 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1620 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1621 | else |
| 1622 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1623 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1624 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1625 | &key2 ) ); |
| 1626 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1627 | reexported, |
| 1628 | export_size, |
| 1629 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1630 | ASSERT_COMPARE( exported, exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1631 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1632 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1633 | } |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1634 | TEST_LE_U( exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1635 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 1636 | psa_get_key_bits( &got_attributes ) ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1637 | TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1638 | |
| 1639 | destroy: |
| 1640 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1641 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1642 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1643 | |
| 1644 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1645 | /* |
| 1646 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1647 | * thus reset them as required. |
| 1648 | */ |
| 1649 | psa_reset_key_attributes( &got_attributes ); |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1650 | psa_destroy_key( key ) ; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1651 | mbedtls_free( exported ); |
| 1652 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1653 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1654 | } |
| 1655 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1656 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1657 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1658 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1659 | int type_arg, |
| 1660 | int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1661 | int lifetime_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1662 | int export_size_delta, |
| 1663 | int expected_export_status_arg, |
| 1664 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1665 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1666 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1667 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1668 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1669 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1670 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1671 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1672 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1673 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1674 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1675 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1676 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1677 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1678 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1679 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1680 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1681 | psa_set_key_algorithm( &attributes, alg ); |
| 1682 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1683 | |
| 1684 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1685 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1686 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1687 | /* Export the public key */ |
| 1688 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1689 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1690 | exported, export_size, |
| 1691 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1692 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1693 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1694 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1695 | 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] | 1696 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1697 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1698 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1699 | TEST_LE_U( expected_public_key->len, |
| 1700 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1701 | TEST_LE_U( expected_public_key->len, |
| 1702 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1703 | TEST_LE_U( expected_public_key->len, |
| 1704 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1705 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1706 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1707 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1708 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1709 | /* |
| 1710 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1711 | * thus reset them as required. |
| 1712 | */ |
| 1713 | psa_reset_key_attributes( &attributes ); |
| 1714 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1715 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1716 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1717 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1718 | } |
| 1719 | /* END_CASE */ |
| 1720 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1721 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1722 | void import_and_exercise_key( data_t *data, |
| 1723 | int type_arg, |
| 1724 | int bits_arg, |
| 1725 | int alg_arg ) |
| 1726 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1727 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1728 | psa_key_type_t type = type_arg; |
| 1729 | size_t bits = bits_arg; |
| 1730 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1731 | 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] | 1732 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1733 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1734 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1735 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1736 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1737 | psa_set_key_usage_flags( &attributes, usage ); |
| 1738 | psa_set_key_algorithm( &attributes, alg ); |
| 1739 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1740 | |
| 1741 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1742 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1743 | |
| 1744 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1745 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1746 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1747 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1748 | |
| 1749 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1750 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1751 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1752 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1753 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1754 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1755 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1756 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1757 | /* |
| 1758 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1759 | * thus reset them as required. |
| 1760 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1761 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1762 | |
| 1763 | psa_reset_key_attributes( &attributes ); |
| 1764 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1765 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1766 | } |
| 1767 | /* END_CASE */ |
| 1768 | |
| 1769 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1770 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1771 | int bits_arg, int expected_bits_arg, |
| 1772 | int usage_arg, int expected_usage_arg, |
| 1773 | int alg_arg, int expected_alg_arg ) |
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 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1776 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1777 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1778 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1779 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1780 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1781 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1782 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1783 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1784 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1785 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1786 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1787 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1788 | psa_set_key_usage_flags( &attributes, usage ); |
| 1789 | psa_set_key_algorithm( &attributes, alg ); |
| 1790 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1791 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1792 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1793 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1794 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1795 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1796 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1797 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1798 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1799 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1800 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1801 | |
| 1802 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1803 | /* |
| 1804 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1805 | * thus reset them as required. |
| 1806 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1807 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1808 | |
| 1809 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1810 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1811 | } |
| 1812 | /* END_CASE */ |
| 1813 | |
| 1814 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1815 | void check_key_policy( int type_arg, int bits_arg, |
| 1816 | int usage_arg, int alg_arg ) |
| 1817 | { |
| 1818 | 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] | 1819 | usage_arg, |
| 1820 | mbedtls_test_update_key_usage_flags( usage_arg ), |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1821 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1822 | goto exit; |
| 1823 | } |
| 1824 | /* END_CASE */ |
| 1825 | |
| 1826 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1827 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1828 | { |
| 1829 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1830 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1831 | * 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] | 1832 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1833 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1834 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1835 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1836 | |
| 1837 | memset( &zero, 0, sizeof( zero ) ); |
| 1838 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1839 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1840 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1841 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1842 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1843 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1844 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1845 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1846 | |
| 1847 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1848 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1849 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1850 | |
| 1851 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1852 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1853 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1854 | |
| 1855 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1856 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1857 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1858 | } |
| 1859 | /* END_CASE */ |
| 1860 | |
| 1861 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1862 | void mac_key_policy( int policy_usage_arg, |
| 1863 | int policy_alg_arg, |
| 1864 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1865 | data_t *key_data, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1866 | int exercise_alg_arg, |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1867 | int expected_status_sign_arg, |
| 1868 | int expected_status_verify_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1869 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1870 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1871 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1872 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1873 | psa_key_type_t key_type = key_type_arg; |
| 1874 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1875 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1876 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1877 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1878 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1879 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1880 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1881 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1882 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1883 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1884 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1885 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1886 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1887 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1888 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1889 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1890 | |
gabor-mezei-arm | 40d5cd8 | 2021-06-29 11:06:16 +0200 | [diff] [blame] | 1891 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
| 1892 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1893 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1894 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1895 | TEST_EQUAL( status, expected_status_sign ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1896 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1897 | /* Calculate the MAC, one-shot case. */ |
| 1898 | uint8_t input[128] = {0}; |
| 1899 | size_t mac_len; |
| 1900 | TEST_EQUAL( psa_mac_compute( key, exercise_alg, |
| 1901 | input, 128, |
| 1902 | mac, PSA_MAC_MAX_SIZE, &mac_len ), |
| 1903 | expected_status_sign ); |
| 1904 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1905 | /* Calculate the MAC, multi-part case. */ |
| 1906 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1907 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
| 1908 | if( status == PSA_SUCCESS ) |
| 1909 | { |
| 1910 | status = psa_mac_update( &operation, input, 128 ); |
| 1911 | if( status == PSA_SUCCESS ) |
| 1912 | TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE, |
| 1913 | &mac_len ), |
| 1914 | expected_status_sign ); |
| 1915 | else |
| 1916 | TEST_EQUAL( status, expected_status_sign ); |
| 1917 | } |
| 1918 | else |
| 1919 | { |
| 1920 | TEST_EQUAL( status, expected_status_sign ); |
| 1921 | } |
| 1922 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1923 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1924 | /* Verify correct MAC, one-shot case. */ |
| 1925 | status = psa_mac_verify( key, exercise_alg, input, 128, |
| 1926 | mac, mac_len ); |
| 1927 | |
| 1928 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1929 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1930 | else |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1931 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1932 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1933 | /* Verify correct MAC, multi-part case. */ |
| 1934 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
| 1935 | if( status == PSA_SUCCESS ) |
| 1936 | { |
| 1937 | status = psa_mac_update( &operation, input, 128 ); |
| 1938 | if( status == PSA_SUCCESS ) |
| 1939 | { |
| 1940 | status = psa_mac_verify_finish( &operation, mac, mac_len ); |
| 1941 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1942 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1943 | else |
| 1944 | TEST_EQUAL( status, expected_status_verify ); |
| 1945 | } |
| 1946 | else |
| 1947 | { |
| 1948 | TEST_EQUAL( status, expected_status_verify ); |
| 1949 | } |
| 1950 | } |
| 1951 | else |
| 1952 | { |
| 1953 | TEST_EQUAL( status, expected_status_verify ); |
| 1954 | } |
| 1955 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1956 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1957 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1958 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1959 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1960 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1961 | |
| 1962 | exit: |
| 1963 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1964 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1965 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1966 | } |
| 1967 | /* END_CASE */ |
| 1968 | |
| 1969 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1970 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1971 | int policy_alg, |
| 1972 | int key_type, |
| 1973 | data_t *key_data, |
| 1974 | int exercise_alg ) |
| 1975 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1976 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1977 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1978 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1979 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1980 | size_t output_buffer_size = 0; |
| 1981 | size_t input_buffer_size = 0; |
| 1982 | size_t output_length = 0; |
| 1983 | uint8_t *output = NULL; |
| 1984 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1985 | psa_status_t status; |
| 1986 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1987 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg ); |
| 1988 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg, |
| 1989 | input_buffer_size ); |
| 1990 | |
| 1991 | ASSERT_ALLOC( input, input_buffer_size ); |
| 1992 | ASSERT_ALLOC( output, output_buffer_size ); |
| 1993 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1994 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1995 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1996 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1997 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1998 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1999 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2000 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2001 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2002 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2003 | /* Check if no key usage flag implication is done */ |
| 2004 | TEST_EQUAL( policy_usage, |
| 2005 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2006 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2007 | /* Encrypt check, one-shot */ |
| 2008 | status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size, |
| 2009 | output, output_buffer_size, |
| 2010 | &output_length); |
| 2011 | if( policy_alg == exercise_alg && |
| 2012 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2013 | PSA_ASSERT( status ); |
| 2014 | else |
| 2015 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2016 | |
| 2017 | /* Encrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2018 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2019 | if( policy_alg == exercise_alg && |
| 2020 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2021 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2022 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2023 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2024 | psa_cipher_abort( &operation ); |
| 2025 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2026 | /* Decrypt check, one-shot */ |
| 2027 | status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size, |
| 2028 | input, input_buffer_size, |
| 2029 | &output_length); |
| 2030 | if( policy_alg == exercise_alg && |
| 2031 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 2032 | PSA_ASSERT( status ); |
| 2033 | else |
| 2034 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2035 | |
| 2036 | /* Decrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2037 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2038 | if( policy_alg == exercise_alg && |
| 2039 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2040 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2041 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2042 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2043 | |
| 2044 | exit: |
| 2045 | psa_cipher_abort( &operation ); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2046 | mbedtls_free( input ); |
| 2047 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2048 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2049 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2050 | } |
| 2051 | /* END_CASE */ |
| 2052 | |
| 2053 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2054 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2055 | int policy_alg, |
| 2056 | int key_type, |
| 2057 | data_t *key_data, |
| 2058 | int nonce_length_arg, |
| 2059 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2060 | int exercise_alg, |
| 2061 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2062 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2063 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2064 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2065 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2066 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2067 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2068 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2069 | unsigned char nonce[16] = {0}; |
| 2070 | size_t nonce_length = nonce_length_arg; |
| 2071 | unsigned char tag[16]; |
| 2072 | size_t tag_length = tag_length_arg; |
| 2073 | size_t output_length; |
| 2074 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2075 | TEST_LE_U( nonce_length, sizeof( nonce ) ); |
| 2076 | TEST_LE_U( tag_length, sizeof( tag ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2077 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2078 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2079 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2080 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2081 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2082 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2083 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2084 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2085 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2086 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2087 | /* Check if no key usage implication is done */ |
| 2088 | TEST_EQUAL( policy_usage, |
| 2089 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2090 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2091 | /* Encrypt check, one-shot */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2092 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2093 | nonce, nonce_length, |
| 2094 | NULL, 0, |
| 2095 | NULL, 0, |
| 2096 | tag, tag_length, |
| 2097 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2098 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2099 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2100 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2101 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2102 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2103 | /* Encrypt check, multi-part */ |
| 2104 | status = psa_aead_encrypt_setup( &operation, key, exercise_alg ); |
| 2105 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2106 | TEST_EQUAL( status, expected_status ); |
| 2107 | else |
| 2108 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2109 | |
| 2110 | /* Decrypt check, one-shot */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2111 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2112 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2113 | nonce, nonce_length, |
| 2114 | NULL, 0, |
| 2115 | tag, tag_length, |
| 2116 | NULL, 0, |
| 2117 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2118 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2119 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2120 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2121 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2122 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2123 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2124 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2125 | /* Decrypt check, multi-part */ |
| 2126 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
| 2127 | status = psa_aead_decrypt_setup( &operation, key, exercise_alg ); |
| 2128 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2129 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2130 | else |
| 2131 | TEST_EQUAL( status, expected_status ); |
| 2132 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2133 | exit: |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2134 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2135 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2136 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2137 | } |
| 2138 | /* END_CASE */ |
| 2139 | |
| 2140 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2141 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2142 | int policy_alg, |
| 2143 | int key_type, |
| 2144 | data_t *key_data, |
| 2145 | int exercise_alg ) |
| 2146 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2147 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2148 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2149 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2150 | psa_status_t status; |
| 2151 | size_t key_bits; |
| 2152 | size_t buffer_length; |
| 2153 | unsigned char *buffer = NULL; |
| 2154 | size_t output_length; |
| 2155 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2156 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2157 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2158 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2159 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2160 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2161 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2162 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2163 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2164 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2165 | /* Check if no key usage implication is done */ |
| 2166 | TEST_EQUAL( policy_usage, |
| 2167 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2168 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2169 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2170 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2171 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 2172 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2173 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2174 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2175 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2176 | NULL, 0, |
| 2177 | NULL, 0, |
| 2178 | buffer, buffer_length, |
| 2179 | &output_length ); |
| 2180 | if( policy_alg == exercise_alg && |
| 2181 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2182 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2183 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2184 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2185 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 2186 | if( buffer_length != 0 ) |
| 2187 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2188 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2189 | buffer, buffer_length, |
| 2190 | NULL, 0, |
| 2191 | buffer, buffer_length, |
| 2192 | &output_length ); |
| 2193 | if( policy_alg == exercise_alg && |
| 2194 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2195 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2196 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2197 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2198 | |
| 2199 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2200 | /* |
| 2201 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2202 | * thus reset them as required. |
| 2203 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2204 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2205 | |
| 2206 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2207 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2208 | mbedtls_free( buffer ); |
| 2209 | } |
| 2210 | /* END_CASE */ |
| 2211 | |
| 2212 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2213 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2214 | int policy_alg, |
| 2215 | int key_type, |
| 2216 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2217 | int exercise_alg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2218 | int payload_length_arg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2219 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2220 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2221 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2222 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2223 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2224 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2225 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2226 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 2227 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2228 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2229 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2230 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2231 | int compatible_alg = payload_length_arg > 0; |
| 2232 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2233 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2234 | size_t signature_length; |
| 2235 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2236 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2237 | in the expected usage flags. */ |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2238 | TEST_EQUAL( expected_usage, |
| 2239 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2240 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2241 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2242 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2243 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2244 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2245 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2246 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2247 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2248 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2249 | |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2250 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 2251 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2252 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2253 | payload, payload_length, |
| 2254 | signature, sizeof( signature ), |
| 2255 | &signature_length ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2256 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2257 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2258 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2259 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2260 | |
| 2261 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2262 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2263 | payload, payload_length, |
| 2264 | signature, sizeof( signature ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2265 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2266 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2267 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2268 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2269 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2270 | if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) && |
gabor-mezei-arm | d851d68 | 2021-06-28 14:53:49 +0200 | [diff] [blame] | 2271 | PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) ) |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2272 | { |
| 2273 | status = psa_sign_message( key, exercise_alg, |
| 2274 | payload, payload_length, |
| 2275 | signature, sizeof( signature ), |
| 2276 | &signature_length ); |
| 2277 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 2278 | PSA_ASSERT( status ); |
| 2279 | else |
| 2280 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2281 | |
| 2282 | memset( signature, 0, sizeof( signature ) ); |
| 2283 | status = psa_verify_message( key, exercise_alg, |
| 2284 | payload, payload_length, |
| 2285 | signature, sizeof( signature ) ); |
| 2286 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 2287 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 2288 | else |
| 2289 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2290 | } |
| 2291 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2292 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2293 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2294 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2295 | } |
| 2296 | /* END_CASE */ |
| 2297 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2298 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2299 | void derive_key_policy( int policy_usage, |
| 2300 | int policy_alg, |
| 2301 | int key_type, |
| 2302 | data_t *key_data, |
| 2303 | int exercise_alg ) |
| 2304 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2305 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2306 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2307 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2308 | psa_status_t status; |
| 2309 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2310 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2311 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2312 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2313 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2314 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2315 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2316 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2317 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2318 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2319 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2320 | |
| 2321 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 2322 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2323 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2324 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 2325 | &operation, |
| 2326 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2327 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2328 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2329 | |
| 2330 | status = psa_key_derivation_input_key( &operation, |
| 2331 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2332 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2333 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2334 | if( policy_alg == exercise_alg && |
| 2335 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2336 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2337 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2338 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2339 | |
| 2340 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2341 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2342 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2343 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2344 | } |
| 2345 | /* END_CASE */ |
| 2346 | |
| 2347 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2348 | void agreement_key_policy( int policy_usage, |
| 2349 | int policy_alg, |
| 2350 | int key_type_arg, |
| 2351 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2352 | int exercise_alg, |
| 2353 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2354 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2355 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2356 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2357 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2358 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2359 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2360 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2361 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2362 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2363 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2364 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2365 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2366 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2367 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2368 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2369 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2370 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2371 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2372 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2373 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2374 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2375 | |
| 2376 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2377 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2378 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2379 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2380 | } |
| 2381 | /* END_CASE */ |
| 2382 | |
| 2383 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2384 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2385 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2386 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2387 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2388 | psa_key_type_t key_type = key_type_arg; |
| 2389 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2390 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2391 | psa_key_usage_t usage = usage_arg; |
| 2392 | psa_algorithm_t alg = alg_arg; |
| 2393 | psa_algorithm_t alg2 = alg2_arg; |
| 2394 | |
| 2395 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2396 | |
| 2397 | psa_set_key_usage_flags( &attributes, usage ); |
| 2398 | psa_set_key_algorithm( &attributes, alg ); |
| 2399 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2400 | psa_set_key_type( &attributes, key_type ); |
| 2401 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2402 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2403 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2404 | /* Update the usage flags to obtain implicit usage flags */ |
| 2405 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2406 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2407 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2408 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2409 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2410 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2411 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2412 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2413 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2414 | goto exit; |
| 2415 | |
| 2416 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2417 | /* |
| 2418 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2419 | * thus reset them as required. |
| 2420 | */ |
| 2421 | psa_reset_key_attributes( &got_attributes ); |
| 2422 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2423 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2424 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2425 | } |
| 2426 | /* END_CASE */ |
| 2427 | |
| 2428 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2429 | void raw_agreement_key_policy( int policy_usage, |
| 2430 | int policy_alg, |
| 2431 | int key_type_arg, |
| 2432 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2433 | int exercise_alg, |
| 2434 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2435 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2436 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2437 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2438 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2439 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2440 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2441 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2442 | |
| 2443 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2444 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2445 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2446 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2447 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2448 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2449 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2450 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2451 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2452 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2453 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2454 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2455 | |
| 2456 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2457 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2458 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2459 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2460 | } |
| 2461 | /* END_CASE */ |
| 2462 | |
| 2463 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2464 | void copy_success( int source_usage_arg, |
| 2465 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2466 | unsigned int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2467 | int type_arg, data_t *material, |
| 2468 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2469 | int target_usage_arg, |
| 2470 | int target_alg_arg, int target_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2471 | unsigned int target_lifetime_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2472 | int expected_usage_arg, |
| 2473 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2474 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2475 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2476 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2477 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2478 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2479 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2480 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2481 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2482 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2483 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2484 | uint8_t *export_buffer = NULL; |
| 2485 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2486 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2487 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2488 | /* Prepare the source key. */ |
| 2489 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2490 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2491 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2492 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2493 | psa_set_key_lifetime( &source_attributes, source_lifetime); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2494 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2495 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2496 | &source_key ) ); |
| 2497 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2498 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2499 | /* Prepare the target attributes. */ |
| 2500 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2501 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2502 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2503 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2504 | psa_set_key_lifetime( &target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2505 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2506 | if( target_usage_arg != -1 ) |
| 2507 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2508 | if( target_alg_arg != -1 ) |
| 2509 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2510 | if( target_alg2_arg != -1 ) |
| 2511 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2512 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2513 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2514 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2515 | PSA_ASSERT( psa_copy_key( source_key, |
| 2516 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2517 | |
| 2518 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2519 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2520 | |
| 2521 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2522 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2523 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2524 | psa_get_key_type( &target_attributes ) ); |
| 2525 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2526 | psa_get_key_bits( &target_attributes ) ); |
| 2527 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2528 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2529 | TEST_EQUAL( expected_alg2, |
| 2530 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2531 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2532 | { |
| 2533 | size_t length; |
| 2534 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2535 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2536 | material->len, &length ) ); |
| 2537 | ASSERT_COMPARE( material->x, material->len, |
| 2538 | export_buffer, length ); |
| 2539 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2540 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2541 | if( !psa_key_lifetime_is_external( target_lifetime ) ) |
| 2542 | { |
| 2543 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
| 2544 | goto exit; |
| 2545 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
| 2546 | goto exit; |
| 2547 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2548 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2549 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2550 | |
| 2551 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2552 | /* |
| 2553 | * Source and target key attributes may have been returned by |
| 2554 | * psa_get_key_attributes() thus reset them as required. |
| 2555 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2556 | psa_reset_key_attributes( &source_attributes ); |
| 2557 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2558 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2559 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2560 | mbedtls_free( export_buffer ); |
| 2561 | } |
| 2562 | /* END_CASE */ |
| 2563 | |
| 2564 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2565 | void copy_fail( int source_usage_arg, |
| 2566 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2567 | int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2568 | int type_arg, data_t *material, |
| 2569 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2570 | int target_usage_arg, |
| 2571 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2572 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2573 | int expected_status_arg ) |
| 2574 | { |
| 2575 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2576 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2577 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2578 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2579 | 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] | 2580 | |
| 2581 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2582 | |
| 2583 | /* Prepare the source key. */ |
| 2584 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2585 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2586 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2587 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2588 | psa_set_key_lifetime( &source_attributes, source_lifetime_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2589 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2590 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2591 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2592 | |
| 2593 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2594 | psa_set_key_id( &target_attributes, key_id ); |
| 2595 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2596 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2597 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2598 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2599 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2600 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2601 | |
| 2602 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2603 | TEST_EQUAL( psa_copy_key( source_key, |
| 2604 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2605 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2606 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2607 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2608 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2609 | exit: |
| 2610 | psa_reset_key_attributes( &source_attributes ); |
| 2611 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2612 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2613 | } |
| 2614 | /* END_CASE */ |
| 2615 | |
| 2616 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2617 | void hash_operation_init( ) |
| 2618 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2619 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2620 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2621 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2622 | * 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] | 2623 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2624 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2625 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2626 | psa_hash_operation_t zero; |
| 2627 | |
| 2628 | memset( &zero, 0, sizeof( zero ) ); |
| 2629 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2630 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2631 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2632 | PSA_ERROR_BAD_STATE ); |
| 2633 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2634 | PSA_ERROR_BAD_STATE ); |
| 2635 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2636 | PSA_ERROR_BAD_STATE ); |
| 2637 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2638 | /* A default hash operation should be abortable without error. */ |
| 2639 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2640 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2641 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2642 | } |
| 2643 | /* END_CASE */ |
| 2644 | |
| 2645 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2646 | void hash_setup( int alg_arg, |
| 2647 | int expected_status_arg ) |
| 2648 | { |
| 2649 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2650 | uint8_t *output = NULL; |
| 2651 | size_t output_size = 0; |
| 2652 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2653 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2654 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2655 | psa_status_t status; |
| 2656 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2657 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2658 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2659 | /* Hash Setup, one-shot */ |
Neil Armstrong | ee9686b | 2022-02-25 15:47:34 +0100 | [diff] [blame] | 2660 | output_size = PSA_HASH_LENGTH( alg ); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2661 | ASSERT_ALLOC( output, output_size ); |
| 2662 | |
| 2663 | status = psa_hash_compute( alg, NULL, 0, |
| 2664 | output, output_size, &output_length ); |
| 2665 | TEST_EQUAL( status, expected_status ); |
| 2666 | |
| 2667 | /* Hash Setup, multi-part */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2668 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2669 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2670 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2671 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2672 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2673 | |
| 2674 | /* If setup failed, reproduce the failure, so as to |
| 2675 | * test the resulting state of the operation object. */ |
| 2676 | if( status != PSA_SUCCESS ) |
| 2677 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2678 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2679 | /* Now the operation object should be reusable. */ |
| 2680 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2681 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2682 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2683 | #endif |
| 2684 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2685 | exit: |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2686 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2687 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2688 | } |
| 2689 | /* END_CASE */ |
| 2690 | |
| 2691 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2692 | void hash_compute_fail( int alg_arg, data_t *input, |
| 2693 | int output_size_arg, int expected_status_arg ) |
| 2694 | { |
| 2695 | psa_algorithm_t alg = alg_arg; |
| 2696 | uint8_t *output = NULL; |
| 2697 | size_t output_size = output_size_arg; |
| 2698 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2699 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2700 | psa_status_t expected_status = expected_status_arg; |
| 2701 | psa_status_t status; |
| 2702 | |
| 2703 | ASSERT_ALLOC( output, output_size ); |
| 2704 | |
| 2705 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2706 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2707 | /* Hash Compute, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2708 | status = psa_hash_compute( alg, input->x, input->len, |
| 2709 | output, output_size, &output_length ); |
| 2710 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2711 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2712 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2713 | /* Hash Compute, multi-part */ |
| 2714 | status = psa_hash_setup( &operation, alg ); |
| 2715 | if( status == PSA_SUCCESS ) |
| 2716 | { |
| 2717 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2718 | if( status == PSA_SUCCESS ) |
| 2719 | { |
| 2720 | status = psa_hash_finish( &operation, output, output_size, |
| 2721 | &output_length ); |
| 2722 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2723 | TEST_LE_U( output_length, output_size ); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2724 | else |
| 2725 | TEST_EQUAL( status, expected_status ); |
| 2726 | } |
| 2727 | else |
| 2728 | { |
| 2729 | TEST_EQUAL( status, expected_status ); |
| 2730 | } |
| 2731 | } |
| 2732 | else |
| 2733 | { |
| 2734 | TEST_EQUAL( status, expected_status ); |
| 2735 | } |
| 2736 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2737 | exit: |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2738 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2739 | mbedtls_free( output ); |
| 2740 | PSA_DONE( ); |
| 2741 | } |
| 2742 | /* END_CASE */ |
| 2743 | |
| 2744 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2745 | void hash_compare_fail( int alg_arg, data_t *input, |
| 2746 | data_t *reference_hash, |
| 2747 | int expected_status_arg ) |
| 2748 | { |
| 2749 | psa_algorithm_t alg = alg_arg; |
| 2750 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2751 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2752 | psa_status_t status; |
| 2753 | |
| 2754 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2755 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2756 | /* Hash Compare, one-shot */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2757 | status = psa_hash_compare( alg, input->x, input->len, |
| 2758 | reference_hash->x, reference_hash->len ); |
| 2759 | TEST_EQUAL( status, expected_status ); |
| 2760 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2761 | /* Hash Compare, multi-part */ |
| 2762 | status = psa_hash_setup( &operation, alg ); |
| 2763 | if( status == PSA_SUCCESS ) |
| 2764 | { |
| 2765 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2766 | if( status == PSA_SUCCESS ) |
| 2767 | { |
| 2768 | status = psa_hash_verify( &operation, reference_hash->x, |
| 2769 | reference_hash->len ); |
| 2770 | TEST_EQUAL( status, expected_status ); |
| 2771 | } |
| 2772 | else |
| 2773 | { |
| 2774 | TEST_EQUAL( status, expected_status ); |
| 2775 | } |
| 2776 | } |
| 2777 | else |
| 2778 | { |
| 2779 | TEST_EQUAL( status, expected_status ); |
| 2780 | } |
| 2781 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2782 | exit: |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2783 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2784 | PSA_DONE( ); |
| 2785 | } |
| 2786 | /* END_CASE */ |
| 2787 | |
| 2788 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2789 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2790 | data_t *expected_output ) |
| 2791 | { |
| 2792 | psa_algorithm_t alg = alg_arg; |
| 2793 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2794 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2795 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2796 | size_t i; |
| 2797 | |
| 2798 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2799 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2800 | /* Compute with tight 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, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2802 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 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 tight 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 | PSA_HASH_LENGTH( alg ), |
| 2813 | &output_length ) ); |
| 2814 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2815 | ASSERT_COMPARE( output, output_length, |
| 2816 | expected_output->x, expected_output->len ); |
| 2817 | |
| 2818 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2819 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2820 | output, sizeof( output ), |
| 2821 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2822 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2823 | ASSERT_COMPARE( output, output_length, |
| 2824 | expected_output->x, expected_output->len ); |
| 2825 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2826 | /* Compute with larger buffer, multi-part */ |
| 2827 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2828 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2829 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2830 | sizeof( output ), &output_length ) ); |
| 2831 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2832 | ASSERT_COMPARE( output, output_length, |
| 2833 | expected_output->x, expected_output->len ); |
| 2834 | |
| 2835 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2836 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2837 | output, output_length ) ); |
| 2838 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2839 | /* Compare with correct hash, multi-part */ |
| 2840 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2841 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2842 | PSA_ASSERT( psa_hash_verify( &operation, output, |
| 2843 | output_length ) ); |
| 2844 | |
| 2845 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2846 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2847 | output, output_length + 1 ), |
| 2848 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2849 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2850 | /* Compare with trailing garbage, multi-part */ |
| 2851 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2852 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2853 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ), |
| 2854 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2855 | |
| 2856 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2857 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2858 | output, output_length - 1 ), |
| 2859 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2860 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2861 | /* Compare with truncated hash, multi-part */ |
| 2862 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2863 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2864 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ), |
| 2865 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2866 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2867 | /* Compare with corrupted value */ |
| 2868 | for( i = 0; i < output_length; i++ ) |
| 2869 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2870 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2871 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2872 | |
| 2873 | /* One-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2874 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2875 | output, output_length ), |
| 2876 | PSA_ERROR_INVALID_SIGNATURE ); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2877 | |
| 2878 | /* Multi-Part */ |
| 2879 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2880 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2881 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length ), |
| 2882 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2883 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2884 | output[i] ^= 1; |
| 2885 | } |
| 2886 | |
| 2887 | exit: |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2888 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2889 | PSA_DONE( ); |
| 2890 | } |
| 2891 | /* END_CASE */ |
| 2892 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2893 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2894 | void hash_bad_order( ) |
| 2895 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2896 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2897 | unsigned char input[] = ""; |
| 2898 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2899 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2900 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2901 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2902 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2903 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2904 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2905 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2906 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2907 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2908 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2909 | /* Call setup twice in a row. */ |
| 2910 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2911 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2912 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2913 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2914 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2915 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2916 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2917 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2918 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 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 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2923 | /* Check that update calls abort on error. */ |
| 2924 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2925 | operation.id = UINT_MAX; |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2926 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
| 2927 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
| 2928 | PSA_ERROR_BAD_STATE ); |
| 2929 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2930 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2931 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2932 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2933 | /* Call update after finish. */ |
| 2934 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2935 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2936 | hash, sizeof( hash ), &hash_len ) ); |
| 2937 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2938 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2939 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2940 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2941 | /* Call verify without calling setup beforehand. */ |
| 2942 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2943 | valid_hash, sizeof( valid_hash ) ), |
| 2944 | PSA_ERROR_BAD_STATE ); |
| 2945 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2946 | |
| 2947 | /* Call verify after finish. */ |
| 2948 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2949 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2950 | hash, sizeof( hash ), &hash_len ) ); |
| 2951 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2952 | valid_hash, sizeof( valid_hash ) ), |
| 2953 | PSA_ERROR_BAD_STATE ); |
| 2954 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2955 | |
| 2956 | /* Call verify twice in a row. */ |
| 2957 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2958 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2959 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2960 | valid_hash, sizeof( valid_hash ) ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2961 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2962 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2963 | valid_hash, sizeof( valid_hash ) ), |
| 2964 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2965 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2966 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2967 | |
| 2968 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2969 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2970 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2971 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2972 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2973 | |
| 2974 | /* Call finish twice in a row. */ |
| 2975 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2976 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2977 | hash, sizeof( hash ), &hash_len ) ); |
| 2978 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2979 | hash, sizeof( hash ), &hash_len ), |
| 2980 | PSA_ERROR_BAD_STATE ); |
| 2981 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2982 | |
| 2983 | /* Call finish after calling verify. */ |
| 2984 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2985 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2986 | valid_hash, sizeof( valid_hash ) ) ); |
| 2987 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2988 | hash, sizeof( hash ), &hash_len ), |
| 2989 | PSA_ERROR_BAD_STATE ); |
| 2990 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2991 | |
| 2992 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2993 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2994 | } |
| 2995 | /* END_CASE */ |
| 2996 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2997 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2998 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2999 | { |
| 3000 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3001 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 3002 | * appended to it */ |
| 3003 | unsigned char hash[] = { |
| 3004 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 3005 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 3006 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3007 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3008 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3009 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3010 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3011 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3012 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3013 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3014 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3015 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3016 | PSA_ERROR_INVALID_SIGNATURE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3017 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 3018 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 3019 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3020 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3021 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3022 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3023 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3024 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3025 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3026 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3027 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3028 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3029 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 3030 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3031 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3032 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3033 | } |
| 3034 | /* END_CASE */ |
| 3035 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3036 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3037 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3038 | { |
| 3039 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3040 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3041 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3042 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3043 | size_t hash_len; |
| 3044 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3045 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3046 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3047 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3048 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3049 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3050 | hash, expected_size - 1, &hash_len ), |
| 3051 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3052 | |
| 3053 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3054 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3055 | } |
| 3056 | /* END_CASE */ |
| 3057 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3058 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3059 | void hash_clone_source_state( ) |
| 3060 | { |
| 3061 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3062 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3063 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3064 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3065 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3066 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3067 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3068 | size_t hash_len; |
| 3069 | |
| 3070 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3071 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 3072 | |
| 3073 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3074 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3075 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3076 | hash, sizeof( hash ), &hash_len ) ); |
| 3077 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3078 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3079 | |
| 3080 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 3081 | PSA_ERROR_BAD_STATE ); |
| 3082 | |
| 3083 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 3084 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 3085 | hash, sizeof( hash ), &hash_len ) ); |
| 3086 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 3087 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3088 | hash, sizeof( hash ), &hash_len ) ); |
| 3089 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 3090 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 3091 | hash, sizeof( hash ), &hash_len ) ); |
| 3092 | |
| 3093 | exit: |
| 3094 | psa_hash_abort( &op_source ); |
| 3095 | psa_hash_abort( &op_init ); |
| 3096 | psa_hash_abort( &op_setup ); |
| 3097 | psa_hash_abort( &op_finished ); |
| 3098 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3099 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3100 | } |
| 3101 | /* END_CASE */ |
| 3102 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3103 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3104 | void hash_clone_target_state( ) |
| 3105 | { |
| 3106 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3107 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3108 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3109 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3110 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3111 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3112 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3113 | size_t hash_len; |
| 3114 | |
| 3115 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3116 | |
| 3117 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3118 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3119 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3120 | hash, sizeof( hash ), &hash_len ) ); |
| 3121 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3122 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3123 | |
| 3124 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 3125 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 3126 | hash, sizeof( hash ), &hash_len ) ); |
| 3127 | |
| 3128 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 3129 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 3130 | PSA_ERROR_BAD_STATE ); |
| 3131 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 3132 | PSA_ERROR_BAD_STATE ); |
| 3133 | |
| 3134 | exit: |
| 3135 | psa_hash_abort( &op_target ); |
| 3136 | psa_hash_abort( &op_init ); |
| 3137 | psa_hash_abort( &op_setup ); |
| 3138 | psa_hash_abort( &op_finished ); |
| 3139 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3140 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3141 | } |
| 3142 | /* END_CASE */ |
| 3143 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3144 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3145 | void mac_operation_init( ) |
| 3146 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3147 | const uint8_t input[1] = { 0 }; |
| 3148 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3149 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3150 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3151 | * 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] | 3152 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3153 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 3154 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3155 | psa_mac_operation_t zero; |
| 3156 | |
| 3157 | memset( &zero, 0, sizeof( zero ) ); |
| 3158 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3159 | /* A freshly-initialized MAC operation should not be usable. */ |
| 3160 | TEST_EQUAL( psa_mac_update( &func, |
| 3161 | input, sizeof( input ) ), |
| 3162 | PSA_ERROR_BAD_STATE ); |
| 3163 | TEST_EQUAL( psa_mac_update( &init, |
| 3164 | input, sizeof( input ) ), |
| 3165 | PSA_ERROR_BAD_STATE ); |
| 3166 | TEST_EQUAL( psa_mac_update( &zero, |
| 3167 | input, sizeof( input ) ), |
| 3168 | PSA_ERROR_BAD_STATE ); |
| 3169 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3170 | /* A default MAC operation should be abortable without error. */ |
| 3171 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 3172 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 3173 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3174 | } |
| 3175 | /* END_CASE */ |
| 3176 | |
| 3177 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3178 | void mac_setup( int key_type_arg, |
| 3179 | data_t *key, |
| 3180 | int alg_arg, |
| 3181 | int expected_status_arg ) |
| 3182 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3183 | psa_key_type_t key_type = key_type_arg; |
| 3184 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3185 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3186 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3187 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3188 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3189 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3190 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3191 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3192 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3193 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3194 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 3195 | &operation, &status ) ) |
| 3196 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3197 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3198 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3199 | /* The operation object should be reusable. */ |
| 3200 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3201 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3202 | smoke_test_key_data, |
| 3203 | sizeof( smoke_test_key_data ), |
| 3204 | KNOWN_SUPPORTED_MAC_ALG, |
| 3205 | &operation, &status ) ) |
| 3206 | goto exit; |
| 3207 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3208 | #endif |
| 3209 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3210 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3211 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3212 | } |
| 3213 | /* END_CASE */ |
| 3214 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3215 | /* 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] | 3216 | void mac_bad_order( ) |
| 3217 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3218 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3219 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3220 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3221 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3222 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3223 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3224 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3225 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3226 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3227 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3228 | size_t sign_mac_length = 0; |
| 3229 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3230 | const uint8_t verify_mac[] = { |
| 3231 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3232 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 3233 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 3234 | |
| 3235 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3236 | 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] | 3237 | psa_set_key_algorithm( &attributes, alg ); |
| 3238 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3239 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3240 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3241 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3242 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3243 | /* Call update without calling setup beforehand. */ |
| 3244 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3245 | PSA_ERROR_BAD_STATE ); |
| 3246 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3247 | |
| 3248 | /* Call sign finish without calling setup beforehand. */ |
| 3249 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 3250 | &sign_mac_length), |
| 3251 | PSA_ERROR_BAD_STATE ); |
| 3252 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3253 | |
| 3254 | /* Call verify finish without calling setup beforehand. */ |
| 3255 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3256 | verify_mac, sizeof( verify_mac ) ), |
| 3257 | PSA_ERROR_BAD_STATE ); |
| 3258 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3259 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3260 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3261 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3262 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3263 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3264 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3265 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3266 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3267 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3268 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3269 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3270 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3271 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3272 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3273 | sign_mac, sizeof( sign_mac ), |
| 3274 | &sign_mac_length ) ); |
| 3275 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3276 | PSA_ERROR_BAD_STATE ); |
| 3277 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3278 | |
| 3279 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3280 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3281 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3282 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3283 | verify_mac, sizeof( verify_mac ) ) ); |
| 3284 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3285 | PSA_ERROR_BAD_STATE ); |
| 3286 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3287 | |
| 3288 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3289 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3290 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3291 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3292 | sign_mac, sizeof( sign_mac ), |
| 3293 | &sign_mac_length ) ); |
| 3294 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3295 | sign_mac, sizeof( sign_mac ), |
| 3296 | &sign_mac_length ), |
| 3297 | PSA_ERROR_BAD_STATE ); |
| 3298 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3299 | |
| 3300 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3301 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3302 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3303 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3304 | verify_mac, sizeof( verify_mac ) ) ); |
| 3305 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3306 | verify_mac, sizeof( verify_mac ) ), |
| 3307 | PSA_ERROR_BAD_STATE ); |
| 3308 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3309 | |
| 3310 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3311 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3312 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3313 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3314 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3315 | verify_mac, sizeof( verify_mac ) ), |
| 3316 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3317 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3318 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3319 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3320 | |
| 3321 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3322 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3323 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3324 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3325 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3326 | sign_mac, sizeof( sign_mac ), |
| 3327 | &sign_mac_length ), |
| 3328 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3329 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3330 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3331 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3332 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3333 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3334 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3335 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3336 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3337 | } |
| 3338 | /* END_CASE */ |
| 3339 | |
| 3340 | /* BEGIN_CASE */ |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3341 | void mac_sign_verify_multi( int key_type_arg, |
| 3342 | data_t *key_data, |
| 3343 | int alg_arg, |
| 3344 | data_t *input, |
| 3345 | int is_verify, |
| 3346 | data_t *expected_mac ) |
| 3347 | { |
| 3348 | size_t data_part_len = 0; |
| 3349 | |
| 3350 | for( data_part_len = 1; data_part_len <= input->len; data_part_len++ ) |
| 3351 | { |
| 3352 | /* Split data into length(data_part_len) parts. */ |
| 3353 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 3354 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3355 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3356 | alg_arg, |
| 3357 | input, data_part_len, |
| 3358 | expected_mac, |
| 3359 | is_verify, 0 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3360 | break; |
| 3361 | |
| 3362 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3363 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3364 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3365 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3366 | alg_arg, |
| 3367 | input, data_part_len, |
| 3368 | expected_mac, |
| 3369 | is_verify, 1 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3370 | break; |
| 3371 | } |
| 3372 | |
| 3373 | /* Goto is required to silence warnings about unused labels, as we |
| 3374 | * don't actually do any test assertions in this function. */ |
| 3375 | goto exit; |
| 3376 | } |
| 3377 | /* END_CASE */ |
| 3378 | |
| 3379 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3380 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3381 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3382 | int alg_arg, |
| 3383 | data_t *input, |
| 3384 | data_t *expected_mac ) |
| 3385 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3386 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3387 | psa_key_type_t key_type = key_type_arg; |
| 3388 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3389 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3390 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3391 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3392 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3393 | 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] | 3394 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3395 | const size_t output_sizes_to_test[] = { |
| 3396 | 0, |
| 3397 | 1, |
| 3398 | expected_mac->len - 1, |
| 3399 | expected_mac->len, |
| 3400 | expected_mac->len + 1, |
| 3401 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3402 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3403 | TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3404 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 3405 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3406 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3407 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3408 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3409 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3410 | psa_set_key_algorithm( &attributes, alg ); |
| 3411 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3412 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3413 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3414 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3415 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3416 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 3417 | { |
| 3418 | const size_t output_size = output_sizes_to_test[i]; |
| 3419 | psa_status_t expected_status = |
| 3420 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3421 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3422 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3423 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3424 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3425 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3426 | /* Calculate the MAC, one-shot case. */ |
| 3427 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 3428 | input->x, input->len, |
| 3429 | actual_mac, output_size, &mac_length ), |
| 3430 | expected_status ); |
| 3431 | if( expected_status == PSA_SUCCESS ) |
| 3432 | { |
| 3433 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3434 | actual_mac, mac_length ); |
| 3435 | } |
| 3436 | |
| 3437 | if( output_size > 0 ) |
| 3438 | memset( actual_mac, 0, output_size ); |
| 3439 | |
| 3440 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3441 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3442 | PSA_ASSERT( psa_mac_update( &operation, |
| 3443 | input->x, input->len ) ); |
| 3444 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3445 | actual_mac, output_size, |
| 3446 | &mac_length ), |
| 3447 | expected_status ); |
| 3448 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3449 | |
| 3450 | if( expected_status == PSA_SUCCESS ) |
| 3451 | { |
| 3452 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3453 | actual_mac, mac_length ); |
| 3454 | } |
| 3455 | mbedtls_free( actual_mac ); |
| 3456 | actual_mac = NULL; |
| 3457 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3458 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3459 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3460 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3461 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3462 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3463 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3464 | } |
| 3465 | /* END_CASE */ |
| 3466 | |
| 3467 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3468 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3469 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3470 | int alg_arg, |
| 3471 | data_t *input, |
| 3472 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3473 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3474 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3475 | psa_key_type_t key_type = key_type_arg; |
| 3476 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3477 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3478 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3479 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3480 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3481 | TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3482 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3483 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3484 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3485 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3486 | psa_set_key_algorithm( &attributes, alg ); |
| 3487 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3488 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3489 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3490 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3491 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3492 | /* Verify correct MAC, one-shot case. */ |
| 3493 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 3494 | expected_mac->x, expected_mac->len ) ); |
| 3495 | |
| 3496 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3497 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3498 | PSA_ASSERT( psa_mac_update( &operation, |
| 3499 | input->x, input->len ) ); |
| 3500 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3501 | expected_mac->x, |
| 3502 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3503 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3504 | /* Test a MAC that's too short, one-shot case. */ |
| 3505 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3506 | input->x, input->len, |
| 3507 | expected_mac->x, |
| 3508 | expected_mac->len - 1 ), |
| 3509 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3510 | |
| 3511 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3512 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3513 | PSA_ASSERT( psa_mac_update( &operation, |
| 3514 | input->x, input->len ) ); |
| 3515 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3516 | expected_mac->x, |
| 3517 | expected_mac->len - 1 ), |
| 3518 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3519 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3520 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3521 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 3522 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3523 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3524 | input->x, input->len, |
| 3525 | perturbed_mac, expected_mac->len + 1 ), |
| 3526 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3527 | |
| 3528 | /* Test a MAC that's too long, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3529 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3530 | PSA_ASSERT( psa_mac_update( &operation, |
| 3531 | input->x, input->len ) ); |
| 3532 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3533 | perturbed_mac, |
| 3534 | expected_mac->len + 1 ), |
| 3535 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3536 | |
| 3537 | /* Test changing one byte. */ |
| 3538 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 3539 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3540 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3541 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3542 | |
| 3543 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3544 | input->x, input->len, |
| 3545 | perturbed_mac, expected_mac->len ), |
| 3546 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3547 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3548 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3549 | PSA_ASSERT( psa_mac_update( &operation, |
| 3550 | input->x, input->len ) ); |
| 3551 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3552 | perturbed_mac, |
| 3553 | expected_mac->len ), |
| 3554 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3555 | perturbed_mac[i] ^= 1; |
| 3556 | } |
| 3557 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3558 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3559 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3560 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3561 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3562 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3563 | } |
| 3564 | /* END_CASE */ |
| 3565 | |
| 3566 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3567 | void cipher_operation_init( ) |
| 3568 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3569 | const uint8_t input[1] = { 0 }; |
| 3570 | unsigned char output[1] = { 0 }; |
| 3571 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3572 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3573 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3574 | * 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] | 3575 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3576 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 3577 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3578 | psa_cipher_operation_t zero; |
| 3579 | |
| 3580 | memset( &zero, 0, sizeof( zero ) ); |
| 3581 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3582 | /* A freshly-initialized cipher operation should not be usable. */ |
| 3583 | TEST_EQUAL( psa_cipher_update( &func, |
| 3584 | input, sizeof( input ), |
| 3585 | output, sizeof( output ), |
| 3586 | &output_length ), |
| 3587 | PSA_ERROR_BAD_STATE ); |
| 3588 | TEST_EQUAL( psa_cipher_update( &init, |
| 3589 | input, sizeof( input ), |
| 3590 | output, sizeof( output ), |
| 3591 | &output_length ), |
| 3592 | PSA_ERROR_BAD_STATE ); |
| 3593 | TEST_EQUAL( psa_cipher_update( &zero, |
| 3594 | input, sizeof( input ), |
| 3595 | output, sizeof( output ), |
| 3596 | &output_length ), |
| 3597 | PSA_ERROR_BAD_STATE ); |
| 3598 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3599 | /* A default cipher operation should be abortable without error. */ |
| 3600 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 3601 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 3602 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3603 | } |
| 3604 | /* END_CASE */ |
| 3605 | |
| 3606 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3607 | void cipher_setup( int key_type_arg, |
| 3608 | data_t *key, |
| 3609 | int alg_arg, |
| 3610 | int expected_status_arg ) |
| 3611 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3612 | psa_key_type_t key_type = key_type_arg; |
| 3613 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3614 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3615 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3616 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3617 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3618 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3619 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3620 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3621 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3622 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3623 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 3624 | &operation, &status ) ) |
| 3625 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3626 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3627 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3628 | /* The operation object should be reusable. */ |
| 3629 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 3630 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3631 | smoke_test_key_data, |
| 3632 | sizeof( smoke_test_key_data ), |
| 3633 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3634 | &operation, &status ) ) |
| 3635 | goto exit; |
| 3636 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3637 | #endif |
| 3638 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3639 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3640 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3641 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3642 | } |
| 3643 | /* END_CASE */ |
| 3644 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3645 | /* 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] | 3646 | void cipher_bad_order( ) |
| 3647 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3648 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3649 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3650 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3651 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3652 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3653 | 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] | 3654 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3655 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3656 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 3657 | const uint8_t text[] = { |
| 3658 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 3659 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3660 | 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] | 3661 | size_t length = 0; |
| 3662 | |
| 3663 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3664 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3665 | psa_set_key_algorithm( &attributes, alg ); |
| 3666 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3667 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3668 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3669 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3670 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3671 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3672 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3673 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3674 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3675 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3676 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3677 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3678 | |
| 3679 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3680 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3681 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3682 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3683 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3684 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3685 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3686 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3687 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3688 | /* Generate an IV without calling setup beforehand. */ |
| 3689 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3690 | buffer, sizeof( buffer ), |
| 3691 | &length ), |
| 3692 | PSA_ERROR_BAD_STATE ); |
| 3693 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3694 | |
| 3695 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3696 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3697 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3698 | buffer, sizeof( buffer ), |
| 3699 | &length ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3700 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3701 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3702 | buffer, sizeof( buffer ), |
| 3703 | &length ), |
| 3704 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3705 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3706 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3707 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3708 | |
| 3709 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3710 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3711 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3712 | iv, sizeof( iv ) ) ); |
| 3713 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3714 | buffer, sizeof( buffer ), |
| 3715 | &length ), |
| 3716 | PSA_ERROR_BAD_STATE ); |
| 3717 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3718 | |
| 3719 | /* Set an IV without calling setup beforehand. */ |
| 3720 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3721 | iv, sizeof( iv ) ), |
| 3722 | PSA_ERROR_BAD_STATE ); |
| 3723 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3724 | |
| 3725 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3726 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3727 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3728 | iv, sizeof( iv ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3729 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3730 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3731 | iv, sizeof( iv ) ), |
| 3732 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3733 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3734 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3735 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3736 | |
| 3737 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3738 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3739 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3740 | buffer, sizeof( buffer ), |
| 3741 | &length ) ); |
| 3742 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3743 | iv, sizeof( iv ) ), |
| 3744 | PSA_ERROR_BAD_STATE ); |
| 3745 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3746 | |
| 3747 | /* Call update without calling setup beforehand. */ |
| 3748 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3749 | text, sizeof( text ), |
| 3750 | buffer, sizeof( buffer ), |
| 3751 | &length ), |
| 3752 | PSA_ERROR_BAD_STATE ); |
| 3753 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3754 | |
| 3755 | /* Call update without an IV where an IV is required. */ |
Dave Rodgman | 095dadc | 2021-06-23 12:48:52 +0100 | [diff] [blame] | 3756 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3757 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3758 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3759 | text, sizeof( text ), |
| 3760 | buffer, sizeof( buffer ), |
| 3761 | &length ), |
| 3762 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3763 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3764 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3765 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3766 | |
| 3767 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3768 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3769 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3770 | iv, sizeof( iv ) ) ); |
| 3771 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3772 | buffer, sizeof( buffer ), &length ) ); |
| 3773 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3774 | text, sizeof( text ), |
| 3775 | buffer, sizeof( buffer ), |
| 3776 | &length ), |
| 3777 | PSA_ERROR_BAD_STATE ); |
| 3778 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3779 | |
| 3780 | /* Call finish without calling setup beforehand. */ |
| 3781 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3782 | buffer, sizeof( buffer ), &length ), |
| 3783 | PSA_ERROR_BAD_STATE ); |
| 3784 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3785 | |
| 3786 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3787 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3788 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3789 | * for cipher modes with padding. */ |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3790 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3791 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3792 | buffer, sizeof( buffer ), &length ), |
| 3793 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3794 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3795 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3796 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3797 | |
| 3798 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3799 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3800 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3801 | iv, sizeof( iv ) ) ); |
| 3802 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3803 | buffer, sizeof( buffer ), &length ) ); |
| 3804 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3805 | buffer, sizeof( buffer ), &length ), |
| 3806 | PSA_ERROR_BAD_STATE ); |
| 3807 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3808 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3809 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3810 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3811 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3812 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3813 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3814 | } |
| 3815 | /* END_CASE */ |
| 3816 | |
| 3817 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3818 | void cipher_encrypt_fail( int alg_arg, |
| 3819 | int key_type_arg, |
| 3820 | data_t *key_data, |
| 3821 | data_t *input, |
| 3822 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3823 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3824 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3825 | psa_status_t status; |
| 3826 | psa_key_type_t key_type = key_type_arg; |
| 3827 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3828 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3829 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0}; |
| 3830 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3831 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3832 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3833 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3834 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3835 | size_t function_output_length; |
| 3836 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3837 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3838 | |
| 3839 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 3840 | { |
| 3841 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3842 | |
| 3843 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3844 | psa_set_key_algorithm( &attributes, alg ); |
| 3845 | psa_set_key_type( &attributes, key_type ); |
| 3846 | |
| 3847 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3848 | input->len ); |
| 3849 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3850 | |
| 3851 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3852 | &key ) ); |
| 3853 | } |
| 3854 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3855 | /* Encrypt, one-shot */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3856 | status = psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 3857 | output_buffer_size, &output_length ); |
| 3858 | |
| 3859 | TEST_EQUAL( status, expected_status ); |
| 3860 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3861 | /* Encrypt, multi-part */ |
| 3862 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 3863 | if( status == PSA_SUCCESS ) |
| 3864 | { |
| 3865 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3866 | { |
| 3867 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3868 | iv, iv_size, |
| 3869 | &iv_length ) ); |
| 3870 | } |
| 3871 | |
| 3872 | status = psa_cipher_update( &operation, input->x, input->len, |
| 3873 | output, output_buffer_size, |
| 3874 | &function_output_length ); |
| 3875 | if( status == PSA_SUCCESS ) |
| 3876 | { |
| 3877 | output_length += function_output_length; |
| 3878 | |
| 3879 | status = psa_cipher_finish( &operation, output + output_length, |
| 3880 | output_buffer_size - output_length, |
| 3881 | &function_output_length ); |
| 3882 | |
| 3883 | TEST_EQUAL( status, expected_status ); |
| 3884 | } |
| 3885 | else |
| 3886 | { |
| 3887 | TEST_EQUAL( status, expected_status ); |
| 3888 | } |
| 3889 | } |
| 3890 | else |
| 3891 | { |
| 3892 | TEST_EQUAL( status, expected_status ); |
| 3893 | } |
| 3894 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3895 | exit: |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3896 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3897 | mbedtls_free( output ); |
| 3898 | psa_destroy_key( key ); |
| 3899 | PSA_DONE( ); |
| 3900 | } |
| 3901 | /* END_CASE */ |
| 3902 | |
| 3903 | /* BEGIN_CASE */ |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3904 | void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data, |
| 3905 | data_t *input, int iv_length, |
| 3906 | int expected_result ) |
| 3907 | { |
| 3908 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3909 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3910 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3911 | size_t output_buffer_size = 0; |
| 3912 | unsigned char *output = NULL; |
| 3913 | |
| 3914 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3915 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3916 | |
| 3917 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3918 | |
| 3919 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3920 | psa_set_key_algorithm( &attributes, alg ); |
| 3921 | psa_set_key_type( &attributes, key_type ); |
| 3922 | |
| 3923 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3924 | &key ) ); |
| 3925 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3926 | TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output, |
| 3927 | iv_length ) ); |
| 3928 | |
| 3929 | exit: |
| 3930 | psa_cipher_abort( &operation ); |
| 3931 | mbedtls_free( output ); |
| 3932 | psa_destroy_key( key ); |
| 3933 | PSA_DONE( ); |
| 3934 | } |
| 3935 | /* END_CASE */ |
| 3936 | |
| 3937 | /* BEGIN_CASE */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3938 | void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data, |
| 3939 | data_t *plaintext, data_t *ciphertext ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3940 | { |
| 3941 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3942 | psa_key_type_t key_type = key_type_arg; |
| 3943 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3944 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3945 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3946 | unsigned char *output = NULL; |
| 3947 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3948 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3949 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3950 | |
| 3951 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3952 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3953 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3954 | TEST_LE_U( ciphertext->len, |
| 3955 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) ); |
| 3956 | 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] | 3957 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3958 | TEST_LE_U( plaintext->len, |
| 3959 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) ); |
| 3960 | TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ), |
| 3961 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3962 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3963 | |
| 3964 | /* Set up key and output buffer */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3965 | psa_set_key_usage_flags( &attributes, |
| 3966 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3967 | psa_set_key_algorithm( &attributes, alg ); |
| 3968 | psa_set_key_type( &attributes, key_type ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3969 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3970 | &key ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3971 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3972 | plaintext->len ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3973 | ASSERT_ALLOC( output, output_buffer_size ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3974 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3975 | /* set_iv() is not allowed */ |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3976 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3977 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
| 3978 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3979 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3980 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3981 | PSA_ERROR_BAD_STATE ); |
| 3982 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3983 | /* generate_iv() is not allowed */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3984 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3985 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3986 | &length ), |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3987 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3988 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3989 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3990 | &length ), |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3991 | PSA_ERROR_BAD_STATE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3992 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3993 | /* Multipart encryption */ |
| 3994 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3995 | output_length = 0; |
| 3996 | length = ~0; |
| 3997 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3998 | plaintext->x, plaintext->len, |
| 3999 | output, output_buffer_size, |
| 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( ciphertext->x, ciphertext->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4009 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4010 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4011 | /* Multipart encryption */ |
| 4012 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 4013 | output_length = 0; |
| 4014 | length = ~0; |
| 4015 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4016 | ciphertext->x, ciphertext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4017 | output, output_buffer_size, |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4018 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4019 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4020 | output_length += length; |
| 4021 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 4022 | output + output_length, |
| 4023 | output_buffer_size - output_length, |
| 4024 | &length ) ); |
| 4025 | output_length += length; |
| 4026 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
| 4027 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4028 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4029 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4030 | output_length = ~0; |
| 4031 | PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len, |
| 4032 | output, output_buffer_size, |
| 4033 | &output_length ) ); |
| 4034 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
| 4035 | output, output_length ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4036 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4037 | /* One-shot decryption */ |
| 4038 | output_length = ~0; |
| 4039 | PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len, |
| 4040 | output, output_buffer_size, |
| 4041 | &output_length ) ); |
| 4042 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4043 | output, output_length ); |
| 4044 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4045 | exit: |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4046 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4047 | mbedtls_free( output ); |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4048 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4049 | psa_destroy_key( key ); |
| 4050 | PSA_DONE( ); |
| 4051 | } |
| 4052 | /* END_CASE */ |
| 4053 | |
| 4054 | /* BEGIN_CASE */ |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4055 | void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data ) |
| 4056 | { |
| 4057 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4058 | psa_algorithm_t alg = alg_arg; |
| 4059 | psa_key_type_t key_type = key_type_arg; |
| 4060 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4061 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4062 | psa_status_t status; |
| 4063 | |
| 4064 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4065 | |
| 4066 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4067 | psa_set_key_algorithm( &attributes, alg ); |
| 4068 | psa_set_key_type( &attributes, key_type ); |
| 4069 | |
| 4070 | /* Usage of either of these two size macros would cause divide by zero |
| 4071 | * with incorrect key types previously. Input length should be irrelevant |
| 4072 | * here. */ |
| 4073 | TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ), |
| 4074 | 0 ); |
| 4075 | TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 ); |
| 4076 | |
| 4077 | |
| 4078 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4079 | &key ) ); |
| 4080 | |
| 4081 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4082 | * Encrypt or decrypt will end up in the same place. */ |
| 4083 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 4084 | |
| 4085 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 4086 | |
| 4087 | exit: |
| 4088 | psa_cipher_abort( &operation ); |
| 4089 | psa_destroy_key( key ); |
| 4090 | PSA_DONE( ); |
| 4091 | } |
| 4092 | /* END_CASE */ |
| 4093 | |
| 4094 | /* BEGIN_CASE */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4095 | void cipher_encrypt_validation( int alg_arg, |
| 4096 | int key_type_arg, |
| 4097 | data_t *key_data, |
| 4098 | data_t *input ) |
| 4099 | { |
| 4100 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4101 | psa_key_type_t key_type = key_type_arg; |
| 4102 | psa_algorithm_t alg = alg_arg; |
| 4103 | size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); |
| 4104 | unsigned char *output1 = NULL; |
| 4105 | size_t output1_buffer_size = 0; |
| 4106 | size_t output1_length = 0; |
| 4107 | unsigned char *output2 = NULL; |
| 4108 | size_t output2_buffer_size = 0; |
| 4109 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4110 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4111 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4112 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4113 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4114 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4115 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4116 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4117 | psa_set_key_algorithm( &attributes, alg ); |
| 4118 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4119 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4120 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 4121 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4122 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4123 | ASSERT_ALLOC( output1, output1_buffer_size ); |
| 4124 | ASSERT_ALLOC( output2, output2_buffer_size ); |
| 4125 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4126 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4127 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4128 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4129 | /* The one-shot cipher encryption uses generated iv so validating |
| 4130 | the output is not possible. Validating with multipart encryption. */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4131 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, |
| 4132 | output1_buffer_size, &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4133 | TEST_LE_U( output1_length, |
| 4134 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4135 | TEST_LE_U( output1_length, |
| 4136 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4137 | |
| 4138 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 4139 | PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4140 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4141 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4142 | input->x, input->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4143 | output2, output2_buffer_size, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4144 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4145 | TEST_LE_U( function_output_length, |
| 4146 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4147 | TEST_LE_U( function_output_length, |
| 4148 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4149 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4150 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4151 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 4152 | output2 + output2_length, |
| 4153 | output2_buffer_size - output2_length, |
| 4154 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4155 | TEST_LE_U( function_output_length, |
| 4156 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4157 | TEST_LE_U( function_output_length, |
| 4158 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4159 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4160 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4161 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4162 | ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, |
| 4163 | output2, output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4164 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4165 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4166 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4167 | mbedtls_free( output1 ); |
| 4168 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4169 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4170 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4171 | } |
| 4172 | /* END_CASE */ |
| 4173 | |
| 4174 | /* BEGIN_CASE */ |
| 4175 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4176 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4177 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4178 | int first_part_size_arg, |
| 4179 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4180 | data_t *expected_output, |
| 4181 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4182 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4183 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4184 | psa_key_type_t key_type = key_type_arg; |
| 4185 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4186 | psa_status_t status; |
| 4187 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4188 | size_t first_part_size = first_part_size_arg; |
| 4189 | size_t output1_length = output1_length_arg; |
| 4190 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4191 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4192 | size_t output_buffer_size = 0; |
| 4193 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4194 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4195 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4196 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4197 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4198 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4199 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4200 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4201 | psa_set_key_algorithm( &attributes, alg ); |
| 4202 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4203 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4204 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4205 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4206 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4207 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4208 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4209 | if( iv->len > 0 ) |
| 4210 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4211 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4212 | } |
| 4213 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4214 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4215 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4216 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4217 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4218 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4219 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 4220 | output, output_buffer_size, |
| 4221 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4222 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4223 | TEST_LE_U( function_output_length, |
| 4224 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4225 | TEST_LE_U( function_output_length, |
| 4226 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4227 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4228 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4229 | if( first_part_size < input->len ) |
| 4230 | { |
| 4231 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4232 | input->x + first_part_size, |
| 4233 | input->len - first_part_size, |
| 4234 | ( output_buffer_size == 0 ? NULL : |
| 4235 | output + total_output_length ), |
| 4236 | output_buffer_size - total_output_length, |
| 4237 | &function_output_length ) ); |
| 4238 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4239 | TEST_LE_U( function_output_length, |
| 4240 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4241 | alg, |
| 4242 | input->len - first_part_size ) ); |
| 4243 | TEST_LE_U( function_output_length, |
| 4244 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4245 | total_output_length += function_output_length; |
| 4246 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4247 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4248 | status = psa_cipher_finish( &operation, |
| 4249 | ( output_buffer_size == 0 ? NULL : |
| 4250 | output + total_output_length ), |
| 4251 | output_buffer_size - total_output_length, |
| 4252 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4253 | TEST_LE_U( function_output_length, |
| 4254 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4255 | TEST_LE_U( function_output_length, |
| 4256 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4257 | total_output_length += function_output_length; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4258 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4259 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4260 | if( expected_status == PSA_SUCCESS ) |
| 4261 | { |
| 4262 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4263 | |
| 4264 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4265 | output, total_output_length ); |
| 4266 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4267 | |
| 4268 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4269 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4270 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4271 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4272 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4273 | } |
| 4274 | /* END_CASE */ |
| 4275 | |
| 4276 | /* BEGIN_CASE */ |
| 4277 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4278 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4279 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4280 | int first_part_size_arg, |
| 4281 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4282 | data_t *expected_output, |
| 4283 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4284 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4285 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4286 | psa_key_type_t key_type = key_type_arg; |
| 4287 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4288 | psa_status_t status; |
| 4289 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4290 | size_t first_part_size = first_part_size_arg; |
| 4291 | size_t output1_length = output1_length_arg; |
| 4292 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4293 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4294 | size_t output_buffer_size = 0; |
| 4295 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4296 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4297 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4298 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4299 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4300 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4301 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4302 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4303 | psa_set_key_algorithm( &attributes, alg ); |
| 4304 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4305 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4306 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4307 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4308 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4309 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4310 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4311 | if( iv->len > 0 ) |
| 4312 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4313 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4314 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4315 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4316 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4317 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4318 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4319 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4320 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4321 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4322 | input->x, first_part_size, |
| 4323 | output, output_buffer_size, |
| 4324 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4325 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4326 | TEST_LE_U( function_output_length, |
| 4327 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4328 | TEST_LE_U( function_output_length, |
| 4329 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4330 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4331 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4332 | if( first_part_size < input->len ) |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4333 | { |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4334 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4335 | input->x + first_part_size, |
| 4336 | input->len - first_part_size, |
| 4337 | ( output_buffer_size == 0 ? NULL : |
| 4338 | output + total_output_length ), |
| 4339 | output_buffer_size - total_output_length, |
| 4340 | &function_output_length ) ); |
| 4341 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4342 | TEST_LE_U( function_output_length, |
| 4343 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4344 | alg, |
| 4345 | input->len - first_part_size ) ); |
| 4346 | TEST_LE_U( function_output_length, |
| 4347 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4348 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4349 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4350 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4351 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 4352 | ( output_buffer_size == 0 ? NULL : |
| 4353 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 4354 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4355 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4356 | TEST_LE_U( function_output_length, |
| 4357 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4358 | TEST_LE_U( function_output_length, |
| 4359 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4360 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4361 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4362 | |
| 4363 | if( expected_status == PSA_SUCCESS ) |
| 4364 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4365 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4366 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4367 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4368 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4369 | } |
| 4370 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4371 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4372 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4373 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4374 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4375 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4376 | } |
| 4377 | /* END_CASE */ |
| 4378 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4379 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 4380 | void cipher_decrypt_fail( int alg_arg, |
| 4381 | int key_type_arg, |
| 4382 | data_t *key_data, |
| 4383 | data_t *iv, |
| 4384 | data_t *input_arg, |
| 4385 | int expected_status_arg ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4386 | { |
| 4387 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4388 | psa_status_t status; |
| 4389 | psa_key_type_t key_type = key_type_arg; |
| 4390 | psa_algorithm_t alg = alg_arg; |
| 4391 | psa_status_t expected_status = expected_status_arg; |
| 4392 | unsigned char *input = NULL; |
| 4393 | size_t input_buffer_size = 0; |
| 4394 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4395 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4396 | size_t output_buffer_size = 0; |
| 4397 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4398 | size_t function_output_length; |
| 4399 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4400 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4401 | |
| 4402 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 4403 | { |
| 4404 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4405 | |
| 4406 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4407 | psa_set_key_algorithm( &attributes, alg ); |
| 4408 | psa_set_key_type( &attributes, key_type ); |
| 4409 | |
| 4410 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4411 | &key ) ); |
| 4412 | } |
| 4413 | |
| 4414 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4415 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4416 | if ( input_buffer_size > 0 ) |
| 4417 | { |
| 4418 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4419 | memcpy( input, iv->x, iv->len ); |
| 4420 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4421 | } |
| 4422 | |
| 4423 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4424 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4425 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4426 | /* Decrypt, one-short */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4427 | status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4428 | output_buffer_size, &output_length ); |
| 4429 | TEST_EQUAL( status, expected_status ); |
| 4430 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4431 | /* Decrypt, multi-part */ |
| 4432 | status = psa_cipher_decrypt_setup( &operation, key, alg ); |
| 4433 | if( status == PSA_SUCCESS ) |
| 4434 | { |
| 4435 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 4436 | input_arg->len ) + |
| 4437 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4438 | ASSERT_ALLOC( output_multi, output_buffer_size ); |
| 4439 | |
| 4440 | if( iv->len > 0 ) |
| 4441 | { |
| 4442 | status = psa_cipher_set_iv( &operation, iv->x, iv->len ); |
| 4443 | |
| 4444 | if( status != PSA_SUCCESS ) |
| 4445 | TEST_EQUAL( status, expected_status ); |
| 4446 | } |
| 4447 | |
| 4448 | if( status == PSA_SUCCESS ) |
| 4449 | { |
| 4450 | status = psa_cipher_update( &operation, |
| 4451 | input_arg->x, input_arg->len, |
| 4452 | output_multi, output_buffer_size, |
| 4453 | &function_output_length ); |
| 4454 | if( status == PSA_SUCCESS ) |
| 4455 | { |
| 4456 | output_length = function_output_length; |
| 4457 | |
| 4458 | status = psa_cipher_finish( &operation, |
| 4459 | output_multi + output_length, |
| 4460 | output_buffer_size - output_length, |
| 4461 | &function_output_length ); |
| 4462 | |
| 4463 | TEST_EQUAL( status, expected_status ); |
| 4464 | } |
| 4465 | else |
| 4466 | { |
| 4467 | TEST_EQUAL( status, expected_status ); |
| 4468 | } |
| 4469 | } |
| 4470 | else |
| 4471 | { |
| 4472 | TEST_EQUAL( status, expected_status ); |
| 4473 | } |
| 4474 | } |
| 4475 | else |
| 4476 | { |
| 4477 | TEST_EQUAL( status, expected_status ); |
| 4478 | } |
| 4479 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4480 | exit: |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4481 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4482 | mbedtls_free( input ); |
| 4483 | mbedtls_free( output ); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4484 | mbedtls_free( output_multi ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4485 | psa_destroy_key( key ); |
| 4486 | PSA_DONE( ); |
| 4487 | } |
| 4488 | /* END_CASE */ |
| 4489 | |
| 4490 | /* BEGIN_CASE */ |
| 4491 | void cipher_decrypt( int alg_arg, |
| 4492 | int key_type_arg, |
| 4493 | data_t *key_data, |
| 4494 | data_t *iv, |
| 4495 | data_t *input_arg, |
| 4496 | data_t *expected_output ) |
| 4497 | { |
| 4498 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4499 | psa_key_type_t key_type = key_type_arg; |
| 4500 | psa_algorithm_t alg = alg_arg; |
| 4501 | unsigned char *input = NULL; |
| 4502 | size_t input_buffer_size = 0; |
| 4503 | unsigned char *output = NULL; |
| 4504 | size_t output_buffer_size = 0; |
| 4505 | size_t output_length = 0; |
| 4506 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4507 | |
| 4508 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4509 | |
| 4510 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4511 | psa_set_key_algorithm( &attributes, alg ); |
| 4512 | psa_set_key_type( &attributes, key_type ); |
| 4513 | |
| 4514 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4515 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4516 | if ( input_buffer_size > 0 ) |
| 4517 | { |
| 4518 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4519 | memcpy( input, iv->x, iv->len ); |
| 4520 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4521 | } |
| 4522 | |
| 4523 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4524 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4525 | |
| 4526 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4527 | &key ) ); |
| 4528 | |
| 4529 | PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4530 | output_buffer_size, &output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4531 | TEST_LE_U( output_length, |
| 4532 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) ); |
| 4533 | TEST_LE_U( output_length, |
| 4534 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4535 | |
| 4536 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4537 | output, output_length ); |
| 4538 | exit: |
| 4539 | mbedtls_free( input ); |
| 4540 | mbedtls_free( output ); |
| 4541 | psa_destroy_key( key ); |
| 4542 | PSA_DONE( ); |
| 4543 | } |
| 4544 | /* END_CASE */ |
| 4545 | |
| 4546 | /* BEGIN_CASE */ |
| 4547 | void cipher_verify_output( int alg_arg, |
| 4548 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4549 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4550 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4551 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4552 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4553 | psa_key_type_t key_type = key_type_arg; |
| 4554 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4555 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4556 | size_t output1_size = 0; |
| 4557 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4558 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4559 | size_t output2_size = 0; |
| 4560 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4561 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4562 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4563 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4564 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4565 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4566 | psa_set_key_algorithm( &attributes, alg ); |
| 4567 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4568 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4569 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4570 | &key ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4571 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4572 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4573 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4574 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, |
| 4575 | output1, output1_size, |
| 4576 | &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4577 | TEST_LE_U( output1_length, |
| 4578 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4579 | TEST_LE_U( output1_length, |
| 4580 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4581 | |
| 4582 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4583 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4584 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4585 | PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length, |
| 4586 | output2, output2_size, |
| 4587 | &output2_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4588 | TEST_LE_U( output2_length, |
| 4589 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4590 | TEST_LE_U( output2_length, |
| 4591 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4592 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4593 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4594 | |
| 4595 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4596 | mbedtls_free( output1 ); |
| 4597 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4598 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4599 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4600 | } |
| 4601 | /* END_CASE */ |
| 4602 | |
| 4603 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4604 | void cipher_verify_output_multipart( int alg_arg, |
| 4605 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4606 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4607 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4608 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4609 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4610 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4611 | psa_key_type_t key_type = key_type_arg; |
| 4612 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4613 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4614 | unsigned char iv[16] = {0}; |
| 4615 | size_t iv_size = 16; |
| 4616 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4617 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4618 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4619 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4620 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4621 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4622 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4623 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4624 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4625 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4626 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4627 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4628 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4629 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4630 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4631 | psa_set_key_algorithm( &attributes, alg ); |
| 4632 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4633 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4634 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4635 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4636 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4637 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 4638 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4639 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4640 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 4641 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4642 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 4643 | iv, iv_size, |
| 4644 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4645 | } |
| 4646 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4647 | 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] | 4648 | TEST_LE_U( output1_buffer_size, |
| 4649 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4650 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4651 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4652 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4653 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4654 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 4655 | output1, output1_buffer_size, |
| 4656 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4657 | TEST_LE_U( function_output_length, |
| 4658 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4659 | TEST_LE_U( function_output_length, |
| 4660 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4661 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4662 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4663 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 4664 | input->x + first_part_size, |
| 4665 | input->len - first_part_size, |
| 4666 | output1, output1_buffer_size, |
| 4667 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4668 | TEST_LE_U( function_output_length, |
| 4669 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4670 | alg, |
| 4671 | input->len - first_part_size ) ); |
| 4672 | TEST_LE_U( function_output_length, |
| 4673 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4674 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4675 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4676 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 4677 | output1 + output1_length, |
| 4678 | output1_buffer_size - output1_length, |
| 4679 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4680 | TEST_LE_U( function_output_length, |
| 4681 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4682 | TEST_LE_U( function_output_length, |
| 4683 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4684 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4685 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4686 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4687 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4688 | output2_buffer_size = output1_length; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4689 | TEST_LE_U( output2_buffer_size, |
| 4690 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4691 | TEST_LE_U( output2_buffer_size, |
| 4692 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4693 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4694 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4695 | if( iv_length > 0 ) |
| 4696 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4697 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 4698 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4699 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4700 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4701 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 4702 | output2, output2_buffer_size, |
| 4703 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4704 | TEST_LE_U( function_output_length, |
| 4705 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4706 | TEST_LE_U( function_output_length, |
| 4707 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4708 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4709 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4710 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 4711 | output1 + first_part_size, |
| 4712 | output1_length - first_part_size, |
| 4713 | output2, output2_buffer_size, |
| 4714 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4715 | TEST_LE_U( function_output_length, |
| 4716 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4717 | alg, |
| 4718 | output1_length - first_part_size ) ); |
| 4719 | TEST_LE_U( function_output_length, |
| 4720 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4721 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4722 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4723 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 4724 | output2 + output2_length, |
| 4725 | output2_buffer_size - output2_length, |
| 4726 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4727 | TEST_LE_U( function_output_length, |
| 4728 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4729 | TEST_LE_U( function_output_length, |
| 4730 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4731 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4732 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4733 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4734 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4735 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4736 | |
| 4737 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4738 | psa_cipher_abort( &operation1 ); |
| 4739 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4740 | mbedtls_free( output1 ); |
| 4741 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4742 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4743 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4744 | } |
| 4745 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4746 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4747 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4748 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4749 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4750 | data_t *nonce, |
| 4751 | data_t *additional_data, |
| 4752 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4753 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4754 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4755 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4756 | psa_key_type_t key_type = key_type_arg; |
| 4757 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4758 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4759 | unsigned char *output_data = NULL; |
| 4760 | size_t output_size = 0; |
| 4761 | size_t output_length = 0; |
| 4762 | unsigned char *output_data2 = NULL; |
| 4763 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4764 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4765 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4766 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4767 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4768 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4769 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4770 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4771 | psa_set_key_algorithm( &attributes, alg ); |
| 4772 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4773 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4774 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4775 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4776 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4777 | key_bits = psa_get_key_bits( &attributes ); |
| 4778 | |
| 4779 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4780 | alg ); |
| 4781 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4782 | * should be exact. */ |
| 4783 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4784 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4785 | { |
| 4786 | TEST_EQUAL( output_size, |
| 4787 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4788 | TEST_LE_U( output_size, |
| 4789 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4790 | } |
| 4791 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4792 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4793 | status = psa_aead_encrypt( key, alg, |
| 4794 | nonce->x, nonce->len, |
| 4795 | additional_data->x, |
| 4796 | additional_data->len, |
| 4797 | input_data->x, input_data->len, |
| 4798 | output_data, output_size, |
| 4799 | &output_length ); |
| 4800 | |
| 4801 | /* If the operation is not supported, just skip and not fail in case the |
| 4802 | * encryption involves a common limitation of cryptography hardwares and |
| 4803 | * an alternative implementation. */ |
| 4804 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4805 | { |
| 4806 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4807 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4808 | } |
| 4809 | |
| 4810 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4811 | |
| 4812 | if( PSA_SUCCESS == expected_result ) |
| 4813 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4814 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4815 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4816 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4817 | * should be exact. */ |
| 4818 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4819 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4820 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4821 | TEST_LE_U( input_data->len, |
| 4822 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4823 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4824 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4825 | nonce->x, nonce->len, |
| 4826 | additional_data->x, |
| 4827 | additional_data->len, |
| 4828 | output_data, output_length, |
| 4829 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4830 | &output_length2 ), |
| 4831 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4832 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4833 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4834 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4835 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4836 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4837 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4838 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4839 | mbedtls_free( output_data ); |
| 4840 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4841 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4842 | } |
| 4843 | /* END_CASE */ |
| 4844 | |
| 4845 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4846 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 4847 | int alg_arg, |
| 4848 | data_t *nonce, |
| 4849 | data_t *additional_data, |
| 4850 | data_t *input_data, |
| 4851 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4852 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4853 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4854 | psa_key_type_t key_type = key_type_arg; |
| 4855 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4856 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4857 | unsigned char *output_data = NULL; |
| 4858 | size_t output_size = 0; |
| 4859 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4860 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4861 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4862 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4863 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4864 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4865 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4866 | psa_set_key_algorithm( &attributes, alg ); |
| 4867 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4868 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4869 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4870 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4871 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4872 | key_bits = psa_get_key_bits( &attributes ); |
| 4873 | |
| 4874 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4875 | alg ); |
| 4876 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4877 | * should be exact. */ |
| 4878 | TEST_EQUAL( output_size, |
| 4879 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4880 | TEST_LE_U( output_size, |
| 4881 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4882 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4883 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4884 | status = psa_aead_encrypt( key, alg, |
| 4885 | nonce->x, nonce->len, |
| 4886 | additional_data->x, additional_data->len, |
| 4887 | input_data->x, input_data->len, |
| 4888 | output_data, output_size, |
| 4889 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4890 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4891 | /* If the operation is not supported, just skip and not fail in case the |
| 4892 | * encryption involves a common limitation of cryptography hardwares and |
| 4893 | * an alternative implementation. */ |
| 4894 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4895 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4896 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4897 | 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] | 4898 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4899 | |
| 4900 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4901 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 4902 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4903 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4904 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4905 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4906 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4907 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4908 | } |
| 4909 | /* END_CASE */ |
| 4910 | |
| 4911 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4912 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 4913 | int alg_arg, |
| 4914 | data_t *nonce, |
| 4915 | data_t *additional_data, |
| 4916 | data_t *input_data, |
| 4917 | data_t *expected_data, |
| 4918 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4919 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4920 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4921 | psa_key_type_t key_type = key_type_arg; |
| 4922 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4923 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4924 | unsigned char *output_data = NULL; |
| 4925 | size_t output_size = 0; |
| 4926 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4927 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4928 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4929 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4930 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4931 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4932 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4933 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4934 | psa_set_key_algorithm( &attributes, alg ); |
| 4935 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4936 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4937 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4938 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4939 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4940 | key_bits = psa_get_key_bits( &attributes ); |
| 4941 | |
| 4942 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4943 | alg ); |
| 4944 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4945 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4946 | { |
| 4947 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4948 | * should be exact. */ |
| 4949 | TEST_EQUAL( output_size, |
| 4950 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4951 | TEST_LE_U( output_size, |
| 4952 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4953 | } |
| 4954 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4955 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4956 | status = psa_aead_decrypt( key, alg, |
| 4957 | nonce->x, nonce->len, |
| 4958 | additional_data->x, |
| 4959 | additional_data->len, |
| 4960 | input_data->x, input_data->len, |
| 4961 | output_data, output_size, |
| 4962 | &output_length ); |
| 4963 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4964 | /* If the operation is not supported, just skip and not fail in case the |
| 4965 | * decryption involves a common limitation of cryptography hardwares and |
| 4966 | * an alternative implementation. */ |
| 4967 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4968 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4969 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4970 | 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] | 4971 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4972 | |
| 4973 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4974 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4975 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4976 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4977 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4978 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4979 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4980 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4981 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4982 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4983 | } |
| 4984 | /* END_CASE */ |
| 4985 | |
| 4986 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4987 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 4988 | int alg_arg, |
| 4989 | data_t *nonce, |
| 4990 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4991 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 4992 | int do_set_lengths, |
| 4993 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4994 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4995 | size_t ad_part_len = 0; |
| 4996 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4997 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4998 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4999 | 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] | 5000 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5001 | mbedtls_test_set_step( ad_part_len ); |
| 5002 | |
| 5003 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5004 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5005 | if( ad_part_len & 0x01 ) |
| 5006 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5007 | else |
| 5008 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5009 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5010 | |
| 5011 | /* Split ad into length(ad_part_len) parts. */ |
| 5012 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5013 | alg_arg, nonce, |
| 5014 | additional_data, |
| 5015 | ad_part_len, |
| 5016 | input_data, -1, |
| 5017 | set_lengths_method, |
| 5018 | expected_output, |
| 5019 | 1, 0 ) ) |
| 5020 | break; |
| 5021 | |
| 5022 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5023 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 5024 | |
| 5025 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5026 | alg_arg, nonce, |
| 5027 | additional_data, |
| 5028 | ad_part_len, |
| 5029 | input_data, -1, |
| 5030 | set_lengths_method, |
| 5031 | expected_output, |
| 5032 | 1, 1 ) ) |
| 5033 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5034 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5035 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5036 | 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] | 5037 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5038 | /* Split data into length(data_part_len) parts. */ |
| 5039 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 5040 | |
| 5041 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5042 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5043 | if( data_part_len & 0x01 ) |
| 5044 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5045 | else |
| 5046 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5047 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5048 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5049 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5050 | alg_arg, nonce, |
| 5051 | additional_data, -1, |
| 5052 | input_data, data_part_len, |
| 5053 | set_lengths_method, |
| 5054 | expected_output, |
| 5055 | 1, 0 ) ) |
| 5056 | break; |
| 5057 | |
| 5058 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 5059 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 5060 | |
| 5061 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5062 | alg_arg, nonce, |
| 5063 | additional_data, -1, |
| 5064 | input_data, data_part_len, |
| 5065 | set_lengths_method, |
| 5066 | expected_output, |
| 5067 | 1, 1 ) ) |
| 5068 | break; |
| 5069 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5070 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5071 | /* Goto is required to silence warnings about unused labels, as we |
| 5072 | * don't actually do any test assertions in this function. */ |
| 5073 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5074 | } |
| 5075 | /* END_CASE */ |
| 5076 | |
| 5077 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5078 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 5079 | int alg_arg, |
| 5080 | data_t *nonce, |
| 5081 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5082 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 5083 | int do_set_lengths, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5084 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5085 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5086 | size_t ad_part_len = 0; |
| 5087 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5088 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5089 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5090 | 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] | 5091 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5092 | /* Split ad into length(ad_part_len) parts. */ |
| 5093 | mbedtls_test_set_step( ad_part_len ); |
| 5094 | |
| 5095 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5096 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5097 | if( ad_part_len & 0x01 ) |
| 5098 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5099 | else |
| 5100 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5101 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5102 | |
| 5103 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5104 | alg_arg, nonce, |
| 5105 | additional_data, |
| 5106 | ad_part_len, |
| 5107 | input_data, -1, |
| 5108 | set_lengths_method, |
| 5109 | expected_output, |
| 5110 | 0, 0 ) ) |
| 5111 | break; |
| 5112 | |
| 5113 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5114 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 5115 | |
| 5116 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5117 | alg_arg, nonce, |
| 5118 | additional_data, |
| 5119 | ad_part_len, |
| 5120 | input_data, -1, |
| 5121 | set_lengths_method, |
| 5122 | expected_output, |
| 5123 | 0, 1 ) ) |
| 5124 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5125 | } |
| 5126 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5127 | 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] | 5128 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5129 | /* Split data into length(data_part_len) parts. */ |
| 5130 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 5131 | |
| 5132 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5133 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5134 | if( data_part_len & 0x01 ) |
| 5135 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5136 | else |
| 5137 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5138 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5139 | |
| 5140 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5141 | alg_arg, nonce, |
| 5142 | additional_data, -1, |
| 5143 | input_data, data_part_len, |
| 5144 | set_lengths_method, |
| 5145 | expected_output, |
| 5146 | 0, 0 ) ) |
| 5147 | break; |
| 5148 | |
| 5149 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 5150 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 5151 | |
| 5152 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5153 | alg_arg, nonce, |
| 5154 | additional_data, -1, |
| 5155 | input_data, data_part_len, |
| 5156 | set_lengths_method, |
| 5157 | expected_output, |
| 5158 | 0, 1 ) ) |
| 5159 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5160 | } |
| 5161 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5162 | /* Goto is required to silence warnings about unused labels, as we |
| 5163 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5164 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5165 | } |
| 5166 | /* END_CASE */ |
| 5167 | |
| 5168 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5169 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 5170 | int alg_arg, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5171 | int nonce_length, |
| 5172 | int expected_nonce_length_arg, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5173 | data_t *additional_data, |
| 5174 | data_t *input_data, |
| 5175 | int expected_status_arg ) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5176 | { |
| 5177 | |
| 5178 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5179 | psa_key_type_t key_type = key_type_arg; |
| 5180 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5181 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5182 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5183 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5184 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5185 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5186 | size_t actual_nonce_length = 0; |
| 5187 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5188 | unsigned char *output = NULL; |
| 5189 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5190 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5191 | size_t ciphertext_size = 0; |
| 5192 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5193 | size_t tag_length = 0; |
| 5194 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5195 | |
| 5196 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5197 | |
| 5198 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5199 | psa_set_key_algorithm( & attributes, alg ); |
| 5200 | psa_set_key_type( & attributes, key_type ); |
| 5201 | |
| 5202 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5203 | &key ) ); |
| 5204 | |
| 5205 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5206 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5207 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5208 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5209 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5210 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5211 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5212 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5213 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5214 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5215 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5216 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5217 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5218 | |
| 5219 | /* If the operation is not supported, just skip and not fail in case the |
| 5220 | * encryption involves a common limitation of cryptography hardwares and |
| 5221 | * an alternative implementation. */ |
| 5222 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5223 | { |
| 5224 | 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] | 5225 | 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] | 5226 | } |
| 5227 | |
| 5228 | PSA_ASSERT( status ); |
| 5229 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5230 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5231 | nonce_length, |
| 5232 | &actual_nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5233 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5234 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5235 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5236 | TEST_EQUAL( actual_nonce_length, expected_nonce_length ); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5237 | |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5238 | if( expected_status == PSA_SUCCESS ) |
| 5239 | TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type, |
| 5240 | alg ) ); |
| 5241 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5242 | TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE ); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5243 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5244 | if( expected_status == PSA_SUCCESS ) |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5245 | { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5246 | /* Ensure we can still complete operation. */ |
Paul Elliott | d79c5c5 | 2021-10-06 21:49:41 +0100 | [diff] [blame] | 5247 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5248 | input_data->len ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5249 | |
| 5250 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5251 | additional_data->len ) ); |
| 5252 | |
| 5253 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5254 | output, output_size, |
| 5255 | &ciphertext_length ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5256 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5257 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5258 | &ciphertext_length, tag_buffer, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5259 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5260 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5261 | |
| 5262 | exit: |
| 5263 | psa_destroy_key( key ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5264 | mbedtls_free( output ); |
| 5265 | mbedtls_free( ciphertext ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5266 | psa_aead_abort( &operation ); |
| 5267 | PSA_DONE( ); |
| 5268 | } |
| 5269 | /* END_CASE */ |
| 5270 | |
| 5271 | /* BEGIN_CASE */ |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5272 | void aead_multipart_set_nonce( int key_type_arg, data_t *key_data, |
| 5273 | int alg_arg, |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5274 | int nonce_length_arg, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5275 | int set_lengths_method_arg, |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5276 | data_t *additional_data, |
| 5277 | data_t *input_data, |
| 5278 | int expected_status_arg ) |
| 5279 | { |
| 5280 | |
| 5281 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5282 | psa_key_type_t key_type = key_type_arg; |
| 5283 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5284 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5285 | uint8_t *nonce_buffer = NULL; |
| 5286 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5287 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5288 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5289 | unsigned char *output = NULL; |
| 5290 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5291 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5292 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5293 | size_t ciphertext_size = 0; |
| 5294 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5295 | size_t tag_length = 0; |
| 5296 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5297 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5298 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5299 | |
| 5300 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5301 | |
| 5302 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5303 | psa_set_key_algorithm( &attributes, alg ); |
| 5304 | psa_set_key_type( &attributes, key_type ); |
| 5305 | |
| 5306 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5307 | &key ) ); |
| 5308 | |
| 5309 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5310 | |
| 5311 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5312 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5313 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5314 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5315 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5316 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5317 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5318 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5319 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5320 | |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5321 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5322 | |
| 5323 | /* If the operation is not supported, just skip and not fail in case the |
| 5324 | * encryption involves a common limitation of cryptography hardwares and |
| 5325 | * an alternative implementation. */ |
| 5326 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5327 | { |
| 5328 | 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] | 5329 | 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] | 5330 | } |
| 5331 | |
| 5332 | PSA_ASSERT( status ); |
| 5333 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5334 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
| 5335 | if( nonce_length_arg == -1 ) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5336 | { |
Paul Elliott | 5e69aa5 | 2021-08-25 17:24:37 +0100 | [diff] [blame] | 5337 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5338 | ASSERT_ALLOC( nonce_buffer, 4 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5339 | nonce_length = 0; |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5340 | } |
| 5341 | else |
| 5342 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5343 | /* If length is zero, then this will return NULL. */ |
| 5344 | nonce_length = ( size_t ) nonce_length_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5345 | ASSERT_ALLOC( nonce_buffer, nonce_length ); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5346 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5347 | if( nonce_buffer ) |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5348 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5349 | for( index = 0; index < nonce_length - 1; ++index ) |
| 5350 | { |
| 5351 | nonce_buffer[index] = 'a' + index; |
| 5352 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5353 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5354 | } |
| 5355 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5356 | if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
| 5357 | { |
| 5358 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5359 | input_data->len ) ); |
| 5360 | } |
| 5361 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5362 | status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5363 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5364 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5365 | |
| 5366 | if( expected_status == PSA_SUCCESS ) |
| 5367 | { |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5368 | if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
| 5369 | { |
| 5370 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5371 | input_data->len ) ); |
| 5372 | } |
| 5373 | if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 5374 | expected_status = PSA_ERROR_BAD_STATE; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5375 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5376 | /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */ |
| 5377 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5378 | additional_data->len ), |
| 5379 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5380 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5381 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5382 | output, output_size, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5383 | &ciphertext_length ), |
| 5384 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5385 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5386 | TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5387 | &ciphertext_length, tag_buffer, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5388 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ), |
| 5389 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5390 | } |
| 5391 | |
| 5392 | exit: |
| 5393 | psa_destroy_key( key ); |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5394 | mbedtls_free( output ); |
| 5395 | mbedtls_free( ciphertext ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5396 | mbedtls_free( nonce_buffer ); |
| 5397 | psa_aead_abort( &operation ); |
| 5398 | PSA_DONE( ); |
| 5399 | } |
| 5400 | /* END_CASE */ |
| 5401 | |
| 5402 | /* BEGIN_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5403 | void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data, |
| 5404 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5405 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5406 | data_t *nonce, |
| 5407 | data_t *additional_data, |
| 5408 | data_t *input_data, |
| 5409 | int expected_status_arg ) |
| 5410 | { |
| 5411 | |
| 5412 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5413 | psa_key_type_t key_type = key_type_arg; |
| 5414 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5415 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5416 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5417 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5418 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5419 | unsigned char *output = NULL; |
| 5420 | unsigned char *ciphertext = NULL; |
| 5421 | size_t output_size = output_size_arg; |
| 5422 | size_t ciphertext_size = 0; |
| 5423 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5424 | size_t tag_length = 0; |
| 5425 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5426 | |
| 5427 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5428 | |
| 5429 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5430 | psa_set_key_algorithm( &attributes, alg ); |
| 5431 | psa_set_key_type( &attributes, key_type ); |
| 5432 | |
| 5433 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5434 | &key ) ); |
| 5435 | |
| 5436 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5437 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5438 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5439 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5440 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5441 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5442 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5443 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5444 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5445 | |
| 5446 | /* If the operation is not supported, just skip and not fail in case the |
| 5447 | * encryption involves a common limitation of cryptography hardwares and |
| 5448 | * an alternative implementation. */ |
| 5449 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5450 | { |
| 5451 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5452 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5453 | } |
| 5454 | |
| 5455 | PSA_ASSERT( status ); |
| 5456 | |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5457 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5458 | input_data->len ) ); |
| 5459 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5460 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5461 | |
| 5462 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5463 | additional_data->len ) ); |
| 5464 | |
| 5465 | status = psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5466 | output, output_size, &ciphertext_length ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5467 | |
| 5468 | TEST_EQUAL( status, expected_status ); |
| 5469 | |
| 5470 | if( expected_status == PSA_SUCCESS ) |
| 5471 | { |
| 5472 | /* Ensure we can still complete operation. */ |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5473 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5474 | &ciphertext_length, tag_buffer, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5475 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5476 | } |
| 5477 | |
| 5478 | exit: |
| 5479 | psa_destroy_key( key ); |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5480 | mbedtls_free( output ); |
| 5481 | mbedtls_free( ciphertext ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5482 | psa_aead_abort( &operation ); |
| 5483 | PSA_DONE( ); |
| 5484 | } |
| 5485 | /* END_CASE */ |
| 5486 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5487 | /* BEGIN_CASE */ |
| 5488 | void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data, |
| 5489 | int alg_arg, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5490 | int finish_ciphertext_size_arg, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5491 | int tag_size_arg, |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5492 | data_t *nonce, |
| 5493 | data_t *additional_data, |
| 5494 | data_t *input_data, |
| 5495 | int expected_status_arg ) |
| 5496 | { |
| 5497 | |
| 5498 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5499 | psa_key_type_t key_type = key_type_arg; |
| 5500 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5501 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5502 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5503 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5504 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5505 | unsigned char *ciphertext = NULL; |
| 5506 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5507 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5508 | size_t ciphertext_size = 0; |
| 5509 | size_t ciphertext_length = 0; |
| 5510 | size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5511 | size_t tag_size = ( size_t ) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5512 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5513 | |
| 5514 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5515 | |
| 5516 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5517 | psa_set_key_algorithm( &attributes, alg ); |
| 5518 | psa_set_key_type( &attributes, key_type ); |
| 5519 | |
| 5520 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5521 | &key ) ); |
| 5522 | |
| 5523 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5524 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5525 | 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] | 5526 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5527 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5528 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5529 | ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5530 | |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5531 | ASSERT_ALLOC( tag_buffer, tag_size ); |
| 5532 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5533 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5534 | |
| 5535 | /* If the operation is not supported, just skip and not fail in case the |
| 5536 | * encryption involves a common limitation of cryptography hardwares and |
| 5537 | * an alternative implementation. */ |
| 5538 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5539 | { |
| 5540 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5541 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5542 | } |
| 5543 | |
| 5544 | PSA_ASSERT( status ); |
| 5545 | |
| 5546 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5547 | |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5548 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5549 | input_data->len ) ); |
| 5550 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5551 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5552 | additional_data->len ) ); |
| 5553 | |
| 5554 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5555 | ciphertext, ciphertext_size, &ciphertext_length ) ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5556 | |
| 5557 | /* Ensure we can still complete operation. */ |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5558 | status = psa_aead_finish( &operation, finish_ciphertext, |
| 5559 | finish_ciphertext_size, |
| 5560 | &ciphertext_length, tag_buffer, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5561 | tag_size, &tag_length ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5562 | |
| 5563 | TEST_EQUAL( status, expected_status ); |
| 5564 | |
| 5565 | exit: |
| 5566 | psa_destroy_key( key ); |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5567 | mbedtls_free( ciphertext ); |
| 5568 | mbedtls_free( finish_ciphertext ); |
Paul Elliott | 4a76088 | 2021-09-20 09:42:21 +0100 | [diff] [blame] | 5569 | mbedtls_free( tag_buffer ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5570 | psa_aead_abort( &operation ); |
| 5571 | PSA_DONE( ); |
| 5572 | } |
| 5573 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5574 | |
| 5575 | /* BEGIN_CASE */ |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5576 | void aead_multipart_verify( int key_type_arg, data_t *key_data, |
| 5577 | int alg_arg, |
| 5578 | data_t *nonce, |
| 5579 | data_t *additional_data, |
| 5580 | data_t *input_data, |
| 5581 | data_t *tag, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5582 | int tag_usage_arg, |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5583 | int expected_setup_status_arg, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5584 | int expected_status_arg ) |
| 5585 | { |
| 5586 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5587 | psa_key_type_t key_type = key_type_arg; |
| 5588 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5589 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5590 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5591 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5592 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5593 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5594 | unsigned char *plaintext = NULL; |
| 5595 | unsigned char *finish_plaintext = NULL; |
| 5596 | size_t plaintext_size = 0; |
| 5597 | size_t plaintext_length = 0; |
| 5598 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5599 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5600 | unsigned char *tag_buffer = NULL; |
| 5601 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5602 | |
| 5603 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5604 | |
| 5605 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5606 | psa_set_key_algorithm( &attributes, alg ); |
| 5607 | psa_set_key_type( &attributes, key_type ); |
| 5608 | |
| 5609 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5610 | &key ) ); |
| 5611 | |
| 5612 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5613 | |
| 5614 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 5615 | input_data->len ); |
| 5616 | |
| 5617 | ASSERT_ALLOC( plaintext, plaintext_size ); |
| 5618 | |
| 5619 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 5620 | |
| 5621 | ASSERT_ALLOC( finish_plaintext, verify_plaintext_size ); |
| 5622 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5623 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5624 | |
| 5625 | /* If the operation is not supported, just skip and not fail in case the |
| 5626 | * encryption involves a common limitation of cryptography hardwares and |
| 5627 | * an alternative implementation. */ |
| 5628 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5629 | { |
| 5630 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5631 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5632 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5633 | TEST_EQUAL( status, expected_setup_status ); |
| 5634 | |
| 5635 | if( status != PSA_SUCCESS ) |
| 5636 | goto exit; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5637 | |
| 5638 | PSA_ASSERT( status ); |
| 5639 | |
| 5640 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5641 | |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5642 | status = psa_aead_set_lengths( &operation, additional_data->len, |
| 5643 | input_data->len ); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5644 | PSA_ASSERT( status ); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5645 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5646 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5647 | additional_data->len ) ); |
| 5648 | |
| 5649 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5650 | input_data->len, |
| 5651 | plaintext, plaintext_size, |
| 5652 | &plaintext_length ) ); |
| 5653 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5654 | if( tag_usage == USE_GIVEN_TAG ) |
| 5655 | { |
| 5656 | tag_buffer = tag->x; |
| 5657 | tag_size = tag->len; |
| 5658 | } |
| 5659 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5660 | status = psa_aead_verify( &operation, finish_plaintext, |
| 5661 | verify_plaintext_size, |
| 5662 | &plaintext_length, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5663 | tag_buffer, tag_size ); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5664 | |
| 5665 | TEST_EQUAL( status, expected_status ); |
| 5666 | |
| 5667 | exit: |
| 5668 | psa_destroy_key( key ); |
| 5669 | mbedtls_free( plaintext ); |
| 5670 | mbedtls_free( finish_plaintext ); |
| 5671 | psa_aead_abort( &operation ); |
| 5672 | PSA_DONE( ); |
| 5673 | } |
| 5674 | /* END_CASE */ |
| 5675 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5676 | /* BEGIN_CASE */ |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5677 | void aead_multipart_setup( int key_type_arg, data_t *key_data, |
| 5678 | int alg_arg, int expected_status_arg ) |
| 5679 | { |
| 5680 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5681 | psa_key_type_t key_type = key_type_arg; |
| 5682 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5683 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5684 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5685 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5686 | psa_status_t expected_status = expected_status_arg; |
| 5687 | |
| 5688 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5689 | |
| 5690 | psa_set_key_usage_flags( &attributes, |
| 5691 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5692 | psa_set_key_algorithm( &attributes, alg ); |
| 5693 | psa_set_key_type( &attributes, key_type ); |
| 5694 | |
| 5695 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5696 | &key ) ); |
| 5697 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5698 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5699 | |
| 5700 | TEST_EQUAL( status, expected_status ); |
| 5701 | |
| 5702 | psa_aead_abort( &operation ); |
| 5703 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5704 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5705 | |
| 5706 | TEST_EQUAL(status, expected_status ); |
| 5707 | |
| 5708 | exit: |
| 5709 | psa_destroy_key( key ); |
| 5710 | psa_aead_abort( &operation ); |
| 5711 | PSA_DONE( ); |
| 5712 | } |
| 5713 | /* END_CASE */ |
| 5714 | |
| 5715 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5716 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 5717 | int alg_arg, |
| 5718 | data_t *nonce, |
| 5719 | data_t *additional_data, |
| 5720 | data_t *input_data ) |
| 5721 | { |
| 5722 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5723 | psa_key_type_t key_type = key_type_arg; |
| 5724 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5725 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5726 | unsigned char *output_data = NULL; |
| 5727 | unsigned char *final_data = NULL; |
| 5728 | size_t output_size = 0; |
| 5729 | size_t finish_output_size = 0; |
| 5730 | size_t output_length = 0; |
| 5731 | size_t key_bits = 0; |
| 5732 | size_t tag_length = 0; |
| 5733 | size_t tag_size = 0; |
| 5734 | size_t nonce_length = 0; |
| 5735 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5736 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5737 | size_t output_part_length = 0; |
| 5738 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5739 | |
| 5740 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5741 | |
| 5742 | psa_set_key_usage_flags( & attributes, |
| 5743 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5744 | psa_set_key_algorithm( & attributes, alg ); |
| 5745 | psa_set_key_type( & attributes, key_type ); |
| 5746 | |
| 5747 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5748 | &key ) ); |
| 5749 | |
| 5750 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5751 | key_bits = psa_get_key_bits( &attributes ); |
| 5752 | |
| 5753 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 5754 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5755 | TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5756 | |
| 5757 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5758 | |
| 5759 | ASSERT_ALLOC( output_data, output_size ); |
| 5760 | |
| 5761 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 5762 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5763 | TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5764 | |
| 5765 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 5766 | |
| 5767 | /* Test all operations error without calling setup first. */ |
| 5768 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5769 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5770 | PSA_ERROR_BAD_STATE ); |
| 5771 | |
| 5772 | psa_aead_abort( &operation ); |
| 5773 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5774 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5775 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5776 | &nonce_length ), |
| 5777 | PSA_ERROR_BAD_STATE ); |
| 5778 | |
| 5779 | psa_aead_abort( &operation ); |
| 5780 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5781 | /* ------------------------------------------------------- */ |
| 5782 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5783 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5784 | input_data->len ), |
| 5785 | PSA_ERROR_BAD_STATE ); |
| 5786 | |
| 5787 | psa_aead_abort( &operation ); |
| 5788 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5789 | /* ------------------------------------------------------- */ |
| 5790 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5791 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5792 | additional_data->len ), |
| 5793 | PSA_ERROR_BAD_STATE ); |
| 5794 | |
| 5795 | psa_aead_abort( &operation ); |
| 5796 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5797 | /* ------------------------------------------------------- */ |
| 5798 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5799 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5800 | input_data->len, output_data, |
| 5801 | output_size, &output_length ), |
| 5802 | PSA_ERROR_BAD_STATE ); |
| 5803 | |
| 5804 | psa_aead_abort( &operation ); |
| 5805 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5806 | /* ------------------------------------------------------- */ |
| 5807 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5808 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5809 | finish_output_size, |
| 5810 | &output_part_length, |
| 5811 | tag_buffer, tag_length, |
| 5812 | &tag_size ), |
| 5813 | PSA_ERROR_BAD_STATE ); |
| 5814 | |
| 5815 | psa_aead_abort( &operation ); |
| 5816 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5817 | /* ------------------------------------------------------- */ |
| 5818 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5819 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5820 | finish_output_size, |
| 5821 | &output_part_length, |
| 5822 | tag_buffer, |
| 5823 | tag_length ), |
| 5824 | PSA_ERROR_BAD_STATE ); |
| 5825 | |
| 5826 | psa_aead_abort( &operation ); |
| 5827 | |
| 5828 | /* Test for double setups. */ |
| 5829 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5830 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5831 | |
| 5832 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5833 | PSA_ERROR_BAD_STATE ); |
| 5834 | |
| 5835 | psa_aead_abort( &operation ); |
| 5836 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5837 | /* ------------------------------------------------------- */ |
| 5838 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5839 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5840 | |
| 5841 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5842 | PSA_ERROR_BAD_STATE ); |
| 5843 | |
| 5844 | psa_aead_abort( &operation ); |
| 5845 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5846 | /* ------------------------------------------------------- */ |
| 5847 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5848 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5849 | |
| 5850 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5851 | PSA_ERROR_BAD_STATE ); |
| 5852 | |
| 5853 | psa_aead_abort( &operation ); |
| 5854 | |
| 5855 | /* ------------------------------------------------------- */ |
| 5856 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5857 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5858 | |
| 5859 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5860 | PSA_ERROR_BAD_STATE ); |
| 5861 | |
| 5862 | psa_aead_abort( &operation ); |
| 5863 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5864 | /* Test for not setting a nonce. */ |
| 5865 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5866 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5867 | |
| 5868 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5869 | additional_data->len ), |
| 5870 | PSA_ERROR_BAD_STATE ); |
| 5871 | |
| 5872 | psa_aead_abort( &operation ); |
| 5873 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5874 | /* ------------------------------------------------------- */ |
| 5875 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5876 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5877 | |
| 5878 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5879 | input_data->len, output_data, |
| 5880 | output_size, &output_length ), |
| 5881 | PSA_ERROR_BAD_STATE ); |
| 5882 | |
| 5883 | psa_aead_abort( &operation ); |
| 5884 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5885 | /* ------------------------------------------------------- */ |
| 5886 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5887 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5888 | |
| 5889 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5890 | finish_output_size, |
| 5891 | &output_part_length, |
| 5892 | tag_buffer, tag_length, |
| 5893 | &tag_size ), |
| 5894 | PSA_ERROR_BAD_STATE ); |
| 5895 | |
| 5896 | psa_aead_abort( &operation ); |
| 5897 | |
| 5898 | /* ------------------------------------------------------- */ |
| 5899 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5900 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5901 | |
| 5902 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5903 | finish_output_size, |
| 5904 | &output_part_length, |
| 5905 | tag_buffer, |
| 5906 | tag_length ), |
| 5907 | PSA_ERROR_BAD_STATE ); |
| 5908 | |
| 5909 | psa_aead_abort( &operation ); |
| 5910 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5911 | /* Test for double setting nonce. */ |
| 5912 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5913 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5914 | |
| 5915 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5916 | |
| 5917 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5918 | PSA_ERROR_BAD_STATE ); |
| 5919 | |
| 5920 | psa_aead_abort( &operation ); |
| 5921 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5922 | /* Test for double generating nonce. */ |
| 5923 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5924 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5925 | |
| 5926 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5927 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5928 | &nonce_length ) ); |
| 5929 | |
| 5930 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5931 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5932 | &nonce_length ), |
| 5933 | PSA_ERROR_BAD_STATE ); |
| 5934 | |
| 5935 | |
| 5936 | psa_aead_abort( &operation ); |
| 5937 | |
| 5938 | /* Test for generate nonce then set and vice versa */ |
| 5939 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5940 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5941 | |
| 5942 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5943 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5944 | &nonce_length ) ); |
| 5945 | |
| 5946 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5947 | PSA_ERROR_BAD_STATE ); |
| 5948 | |
| 5949 | psa_aead_abort( &operation ); |
| 5950 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5951 | /* Test for generating nonce after calling set lengths */ |
| 5952 | |
| 5953 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5954 | |
| 5955 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5956 | input_data->len ) ); |
| 5957 | |
| 5958 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5959 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5960 | &nonce_length ) ); |
| 5961 | |
| 5962 | psa_aead_abort( &operation ); |
| 5963 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5964 | /* 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] | 5965 | |
| 5966 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5967 | |
| 5968 | if( operation.alg == PSA_ALG_CCM ) |
| 5969 | { |
| 5970 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5971 | input_data->len ), |
| 5972 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5973 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5974 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5975 | &nonce_length ), |
| 5976 | PSA_ERROR_BAD_STATE ); |
| 5977 | } |
| 5978 | else |
| 5979 | { |
| 5980 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5981 | input_data->len ) ); |
| 5982 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5983 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5984 | &nonce_length ) ); |
| 5985 | } |
| 5986 | |
| 5987 | psa_aead_abort( &operation ); |
| 5988 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5989 | /* 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] | 5990 | #if SIZE_MAX > UINT32_MAX |
| 5991 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5992 | |
| 5993 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 5994 | { |
| 5995 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5996 | input_data->len ), |
| 5997 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5998 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5999 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6000 | &nonce_length ), |
| 6001 | PSA_ERROR_BAD_STATE ); |
| 6002 | } |
| 6003 | else |
| 6004 | { |
| 6005 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6006 | input_data->len ) ); |
| 6007 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6008 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6009 | &nonce_length ) ); |
| 6010 | } |
| 6011 | |
| 6012 | psa_aead_abort( &operation ); |
| 6013 | #endif |
| 6014 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6015 | /* 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] | 6016 | |
| 6017 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6018 | |
| 6019 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6020 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6021 | &nonce_length ) ); |
| 6022 | |
| 6023 | if( operation.alg == PSA_ALG_CCM ) |
| 6024 | { |
| 6025 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6026 | input_data->len ), |
| 6027 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6028 | } |
| 6029 | else |
| 6030 | { |
| 6031 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6032 | input_data->len ) ); |
| 6033 | } |
| 6034 | |
| 6035 | psa_aead_abort( &operation ); |
| 6036 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6037 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6038 | /* Test for setting nonce after calling set lengths */ |
| 6039 | |
| 6040 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6041 | |
| 6042 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6043 | input_data->len ) ); |
| 6044 | |
| 6045 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6046 | |
| 6047 | psa_aead_abort( &operation ); |
| 6048 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6049 | /* 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] | 6050 | |
| 6051 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6052 | |
| 6053 | if( operation.alg == PSA_ALG_CCM ) |
| 6054 | { |
| 6055 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6056 | input_data->len ), |
| 6057 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6058 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6059 | PSA_ERROR_BAD_STATE ); |
| 6060 | } |
| 6061 | else |
| 6062 | { |
| 6063 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6064 | input_data->len ) ); |
| 6065 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6066 | } |
| 6067 | |
| 6068 | psa_aead_abort( &operation ); |
| 6069 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6070 | /* 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] | 6071 | #if SIZE_MAX > UINT32_MAX |
| 6072 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6073 | |
| 6074 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 6075 | { |
| 6076 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6077 | input_data->len ), |
| 6078 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6079 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6080 | PSA_ERROR_BAD_STATE ); |
| 6081 | } |
| 6082 | else |
| 6083 | { |
| 6084 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6085 | input_data->len ) ); |
| 6086 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6087 | } |
| 6088 | |
| 6089 | psa_aead_abort( &operation ); |
| 6090 | #endif |
| 6091 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6092 | /* 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] | 6093 | |
| 6094 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6095 | |
| 6096 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6097 | |
| 6098 | if( operation.alg == PSA_ALG_CCM ) |
| 6099 | { |
| 6100 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6101 | input_data->len ), |
| 6102 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6103 | } |
| 6104 | else |
| 6105 | { |
| 6106 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6107 | input_data->len ) ); |
| 6108 | } |
| 6109 | |
| 6110 | psa_aead_abort( &operation ); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6111 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6112 | /* 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] | 6113 | #if SIZE_MAX > UINT32_MAX |
| 6114 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6115 | |
| 6116 | if( operation.alg == PSA_ALG_GCM ) |
| 6117 | { |
| 6118 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6119 | SIZE_MAX ), |
| 6120 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6121 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6122 | PSA_ERROR_BAD_STATE ); |
| 6123 | } |
| 6124 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6125 | { |
| 6126 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6127 | SIZE_MAX ) ); |
| 6128 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6129 | } |
| 6130 | |
| 6131 | psa_aead_abort( &operation ); |
| 6132 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6133 | /* 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] | 6134 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6135 | |
| 6136 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6137 | |
| 6138 | if( operation.alg == PSA_ALG_GCM ) |
| 6139 | { |
| 6140 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6141 | SIZE_MAX ), |
| 6142 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6143 | } |
| 6144 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6145 | { |
| 6146 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6147 | SIZE_MAX ) ); |
| 6148 | } |
| 6149 | |
| 6150 | psa_aead_abort( &operation ); |
| 6151 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6152 | |
| 6153 | /* ------------------------------------------------------- */ |
| 6154 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6155 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6156 | |
| 6157 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6158 | |
| 6159 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6160 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6161 | &nonce_length ), |
| 6162 | PSA_ERROR_BAD_STATE ); |
| 6163 | |
| 6164 | psa_aead_abort( &operation ); |
| 6165 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6166 | /* Test for generating nonce in decrypt setup. */ |
| 6167 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6168 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6169 | |
| 6170 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6171 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6172 | &nonce_length ), |
| 6173 | PSA_ERROR_BAD_STATE ); |
| 6174 | |
| 6175 | psa_aead_abort( &operation ); |
| 6176 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6177 | /* Test for setting lengths twice. */ |
| 6178 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6179 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6180 | |
| 6181 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6182 | |
| 6183 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6184 | input_data->len ) ); |
| 6185 | |
| 6186 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6187 | input_data->len ), |
| 6188 | PSA_ERROR_BAD_STATE ); |
| 6189 | |
| 6190 | psa_aead_abort( &operation ); |
| 6191 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6192 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6193 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6194 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6195 | |
| 6196 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6197 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6198 | if( operation.alg == PSA_ALG_CCM ) |
| 6199 | { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6200 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6201 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6202 | additional_data->len ), |
| 6203 | PSA_ERROR_BAD_STATE ); |
| 6204 | } |
| 6205 | else |
| 6206 | { |
| 6207 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6208 | additional_data->len ) ); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6209 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6210 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6211 | input_data->len ), |
| 6212 | PSA_ERROR_BAD_STATE ); |
| 6213 | } |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6214 | psa_aead_abort( &operation ); |
| 6215 | |
| 6216 | /* ------------------------------------------------------- */ |
| 6217 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6218 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6219 | |
| 6220 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6221 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6222 | if( operation.alg == PSA_ALG_CCM ) |
| 6223 | { |
| 6224 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6225 | input_data->len, output_data, |
| 6226 | output_size, &output_length ), |
| 6227 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6228 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6229 | } |
| 6230 | else |
| 6231 | { |
| 6232 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6233 | input_data->len, output_data, |
| 6234 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6235 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6236 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6237 | input_data->len ), |
| 6238 | PSA_ERROR_BAD_STATE ); |
| 6239 | } |
| 6240 | psa_aead_abort( &operation ); |
| 6241 | |
| 6242 | /* ------------------------------------------------------- */ |
| 6243 | |
| 6244 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6245 | |
| 6246 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6247 | |
| 6248 | if( operation.alg == PSA_ALG_CCM ) |
| 6249 | { |
| 6250 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6251 | finish_output_size, |
| 6252 | &output_part_length, |
| 6253 | tag_buffer, tag_length, |
| 6254 | &tag_size ) ); |
| 6255 | } |
| 6256 | else |
| 6257 | { |
| 6258 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6259 | finish_output_size, |
| 6260 | &output_part_length, |
| 6261 | tag_buffer, tag_length, |
| 6262 | &tag_size ) ); |
| 6263 | |
| 6264 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6265 | input_data->len ), |
| 6266 | PSA_ERROR_BAD_STATE ); |
| 6267 | } |
| 6268 | psa_aead_abort( &operation ); |
| 6269 | |
| 6270 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6271 | |
| 6272 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6273 | |
| 6274 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6275 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6276 | &nonce_length ) ); |
| 6277 | if( operation.alg == PSA_ALG_CCM ) |
| 6278 | { |
| 6279 | |
| 6280 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6281 | additional_data->len ), |
| 6282 | PSA_ERROR_BAD_STATE ); |
| 6283 | } |
| 6284 | else |
| 6285 | { |
| 6286 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6287 | additional_data->len ) ); |
| 6288 | |
| 6289 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6290 | input_data->len ), |
| 6291 | PSA_ERROR_BAD_STATE ); |
| 6292 | } |
| 6293 | psa_aead_abort( &operation ); |
| 6294 | |
| 6295 | /* ------------------------------------------------------- */ |
| 6296 | |
| 6297 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6298 | |
| 6299 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6300 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6301 | &nonce_length ) ); |
| 6302 | if( operation.alg == PSA_ALG_CCM ) |
| 6303 | { |
| 6304 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6305 | input_data->len, output_data, |
| 6306 | output_size, &output_length ), |
| 6307 | PSA_ERROR_BAD_STATE ); |
| 6308 | |
| 6309 | } |
| 6310 | else |
| 6311 | { |
| 6312 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6313 | input_data->len, output_data, |
| 6314 | output_size, &output_length ) ); |
| 6315 | |
| 6316 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6317 | input_data->len ), |
| 6318 | PSA_ERROR_BAD_STATE ); |
| 6319 | } |
| 6320 | psa_aead_abort( &operation ); |
| 6321 | |
| 6322 | /* ------------------------------------------------------- */ |
| 6323 | |
| 6324 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6325 | |
| 6326 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6327 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6328 | &nonce_length ) ); |
| 6329 | if( operation.alg == PSA_ALG_CCM ) |
| 6330 | { |
| 6331 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6332 | finish_output_size, |
| 6333 | &output_part_length, |
| 6334 | tag_buffer, tag_length, |
| 6335 | &tag_size ) ); |
| 6336 | } |
| 6337 | else |
| 6338 | { |
| 6339 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6340 | finish_output_size, |
| 6341 | &output_part_length, |
| 6342 | tag_buffer, tag_length, |
| 6343 | &tag_size ) ); |
| 6344 | |
| 6345 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6346 | input_data->len ), |
| 6347 | PSA_ERROR_BAD_STATE ); |
| 6348 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6349 | psa_aead_abort( &operation ); |
| 6350 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6351 | /* Test for not sending any additional data or data after setting non zero |
| 6352 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6353 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6354 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6355 | |
| 6356 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6357 | |
| 6358 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6359 | input_data->len ) ); |
| 6360 | |
| 6361 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6362 | finish_output_size, |
| 6363 | &output_part_length, |
| 6364 | tag_buffer, tag_length, |
| 6365 | &tag_size ), |
| 6366 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6367 | |
| 6368 | psa_aead_abort( &operation ); |
| 6369 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6370 | /* Test for not sending any additional data or data after setting non-zero |
| 6371 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6372 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6373 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6374 | |
| 6375 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6376 | |
| 6377 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6378 | input_data->len ) ); |
| 6379 | |
| 6380 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6381 | finish_output_size, |
| 6382 | &output_part_length, |
| 6383 | tag_buffer, |
| 6384 | tag_length ), |
| 6385 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6386 | |
| 6387 | psa_aead_abort( &operation ); |
| 6388 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6389 | /* Test for not sending any additional data after setting a non-zero length |
| 6390 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6391 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6392 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6393 | |
| 6394 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6395 | |
| 6396 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6397 | input_data->len ) ); |
| 6398 | |
| 6399 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6400 | input_data->len, output_data, |
| 6401 | output_size, &output_length ), |
| 6402 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6403 | |
| 6404 | psa_aead_abort( &operation ); |
| 6405 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6406 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6407 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6408 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6409 | |
| 6410 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6411 | |
| 6412 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6413 | input_data->len ) ); |
| 6414 | |
| 6415 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6416 | additional_data->len ) ); |
| 6417 | |
| 6418 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6419 | finish_output_size, |
| 6420 | &output_part_length, |
| 6421 | tag_buffer, tag_length, |
| 6422 | &tag_size ), |
| 6423 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6424 | |
| 6425 | psa_aead_abort( &operation ); |
| 6426 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6427 | /* Test for sending too much additional data after setting lengths. */ |
| 6428 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6429 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6430 | |
| 6431 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6432 | |
| 6433 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6434 | |
| 6435 | |
| 6436 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6437 | additional_data->len ), |
| 6438 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6439 | |
| 6440 | psa_aead_abort( &operation ); |
| 6441 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6442 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6443 | |
| 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, additional_data->len, |
| 6449 | input_data->len ) ); |
| 6450 | |
| 6451 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6452 | additional_data->len ) ); |
| 6453 | |
| 6454 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6455 | 1 ), |
| 6456 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6457 | |
| 6458 | psa_aead_abort( &operation ); |
| 6459 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6460 | /* Test for sending too much data after setting lengths. */ |
| 6461 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6462 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6463 | |
| 6464 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6465 | |
| 6466 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6467 | |
| 6468 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6469 | input_data->len, output_data, |
| 6470 | output_size, &output_length ), |
| 6471 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6472 | |
| 6473 | psa_aead_abort( &operation ); |
| 6474 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6475 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6476 | |
| 6477 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6478 | |
| 6479 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6480 | |
| 6481 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6482 | input_data->len ) ); |
| 6483 | |
| 6484 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6485 | additional_data->len ) ); |
| 6486 | |
| 6487 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6488 | input_data->len, output_data, |
| 6489 | output_size, &output_length ) ); |
| 6490 | |
| 6491 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6492 | 1, output_data, |
| 6493 | output_size, &output_length ), |
| 6494 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6495 | |
| 6496 | psa_aead_abort( &operation ); |
| 6497 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6498 | /* Test sending additional data after data. */ |
| 6499 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6500 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6501 | |
| 6502 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6503 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6504 | if( operation.alg != PSA_ALG_CCM ) |
| 6505 | { |
| 6506 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6507 | input_data->len, output_data, |
| 6508 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6509 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6510 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6511 | additional_data->len ), |
| 6512 | PSA_ERROR_BAD_STATE ); |
| 6513 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6514 | psa_aead_abort( &operation ); |
| 6515 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6516 | /* Test calling finish on decryption. */ |
| 6517 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6518 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6519 | |
| 6520 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6521 | |
| 6522 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6523 | finish_output_size, |
| 6524 | &output_part_length, |
| 6525 | tag_buffer, tag_length, |
| 6526 | &tag_size ), |
| 6527 | PSA_ERROR_BAD_STATE ); |
| 6528 | |
| 6529 | psa_aead_abort( &operation ); |
| 6530 | |
| 6531 | /* Test calling verify on encryption. */ |
| 6532 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6533 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6534 | |
| 6535 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6536 | |
| 6537 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6538 | finish_output_size, |
| 6539 | &output_part_length, |
| 6540 | tag_buffer, |
| 6541 | tag_length ), |
Paul Elliott | 5b065cb | 2021-06-23 08:33:22 +0100 | [diff] [blame] | 6542 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6543 | |
| 6544 | psa_aead_abort( &operation ); |
| 6545 | |
| 6546 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6547 | exit: |
| 6548 | psa_destroy_key( key ); |
| 6549 | psa_aead_abort( &operation ); |
| 6550 | mbedtls_free( output_data ); |
| 6551 | mbedtls_free( final_data ); |
| 6552 | PSA_DONE( ); |
| 6553 | } |
| 6554 | /* END_CASE */ |
| 6555 | |
| 6556 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6557 | void signature_size( int type_arg, |
| 6558 | int bits, |
| 6559 | int alg_arg, |
| 6560 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6561 | { |
| 6562 | psa_key_type_t type = type_arg; |
| 6563 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6564 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6565 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6566 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6567 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6568 | exit: |
| 6569 | ; |
| 6570 | } |
| 6571 | /* END_CASE */ |
| 6572 | |
| 6573 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6574 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 6575 | int alg_arg, data_t *input_data, |
| 6576 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6577 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6578 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6579 | psa_key_type_t key_type = key_type_arg; |
| 6580 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6581 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6582 | unsigned char *signature = NULL; |
| 6583 | size_t signature_size; |
| 6584 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6585 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6586 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6587 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6588 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6589 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6590 | psa_set_key_algorithm( &attributes, alg ); |
| 6591 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6592 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6593 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6594 | &key ) ); |
| 6595 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6596 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6597 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6598 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6599 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6600 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6601 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6602 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6603 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6604 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6605 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6606 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6607 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6608 | input_data->x, input_data->len, |
| 6609 | signature, signature_size, |
| 6610 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6611 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6612 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6613 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6614 | |
| 6615 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6616 | /* |
| 6617 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6618 | * thus reset them as required. |
| 6619 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6620 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6621 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6622 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 6623 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6624 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6625 | } |
| 6626 | /* END_CASE */ |
| 6627 | |
| 6628 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6629 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 6630 | int alg_arg, data_t *input_data, |
| 6631 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6632 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6633 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6634 | psa_key_type_t key_type = key_type_arg; |
| 6635 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6636 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6637 | psa_status_t actual_status; |
| 6638 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6639 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6640 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6641 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6642 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6643 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6644 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6645 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6646 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6647 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6648 | psa_set_key_algorithm( &attributes, alg ); |
| 6649 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6650 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6651 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6652 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6653 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6654 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6655 | input_data->x, input_data->len, |
| 6656 | signature, signature_size, |
| 6657 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6658 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6659 | /* The value of *signature_length is unspecified on error, but |
| 6660 | * whatever it is, it should be less than signature_size, so that |
| 6661 | * if the caller tries to read *signature_length bytes without |
| 6662 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6663 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6664 | |
| 6665 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6666 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6667 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6668 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6669 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6670 | } |
| 6671 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6672 | |
| 6673 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6674 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 6675 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6676 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6677 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6678 | psa_key_type_t key_type = key_type_arg; |
| 6679 | psa_algorithm_t alg = alg_arg; |
| 6680 | size_t key_bits; |
| 6681 | unsigned char *signature = NULL; |
| 6682 | size_t signature_size; |
| 6683 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6684 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6685 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6686 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6687 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6688 | 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] | 6689 | psa_set_key_algorithm( &attributes, alg ); |
| 6690 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6691 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6692 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6693 | &key ) ); |
| 6694 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6695 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6696 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6697 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6698 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6699 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6700 | key_bits, alg ); |
| 6701 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6702 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6703 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6704 | |
| 6705 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6706 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6707 | input_data->x, input_data->len, |
| 6708 | signature, signature_size, |
| 6709 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6710 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6711 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6712 | TEST_ASSERT( signature_length > 0 ); |
| 6713 | |
| 6714 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6715 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6716 | input_data->x, input_data->len, |
| 6717 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6718 | |
| 6719 | if( input_data->len != 0 ) |
| 6720 | { |
| 6721 | /* Flip a bit in the input and verify that the signature is now |
| 6722 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6723 | * because ECDSA may ignore the last few bits of the input. */ |
| 6724 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6725 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6726 | input_data->x, input_data->len, |
| 6727 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 6728 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6729 | } |
| 6730 | |
| 6731 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6732 | /* |
| 6733 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6734 | * thus reset them as required. |
| 6735 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6736 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6737 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6738 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6739 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6740 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6741 | } |
| 6742 | /* END_CASE */ |
| 6743 | |
| 6744 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6745 | void verify_hash( int key_type_arg, data_t *key_data, |
| 6746 | int alg_arg, data_t *hash_data, |
| 6747 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6748 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6749 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6750 | psa_key_type_t key_type = key_type_arg; |
| 6751 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6752 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6753 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6754 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 6755 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6756 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6757 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6758 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6759 | psa_set_key_algorithm( &attributes, alg ); |
| 6760 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6761 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6762 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6763 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6764 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6765 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6766 | hash_data->x, hash_data->len, |
| 6767 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 6768 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6769 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6770 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6771 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6772 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6773 | } |
| 6774 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6775 | |
| 6776 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6777 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 6778 | int alg_arg, data_t *hash_data, |
| 6779 | data_t *signature_data, |
| 6780 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6781 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6782 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6783 | psa_key_type_t key_type = key_type_arg; |
| 6784 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6785 | psa_status_t actual_status; |
| 6786 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6787 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6788 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6789 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6790 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6791 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6792 | psa_set_key_algorithm( &attributes, alg ); |
| 6793 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6794 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6795 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6796 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6797 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6798 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6799 | hash_data->x, hash_data->len, |
| 6800 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6801 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6802 | |
| 6803 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6804 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6805 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6806 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6807 | } |
| 6808 | /* END_CASE */ |
| 6809 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6810 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6811 | void sign_message_deterministic( int key_type_arg, |
| 6812 | data_t *key_data, |
| 6813 | int alg_arg, |
| 6814 | data_t *input_data, |
| 6815 | data_t *output_data ) |
| 6816 | { |
| 6817 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6818 | psa_key_type_t key_type = key_type_arg; |
| 6819 | psa_algorithm_t alg = alg_arg; |
| 6820 | size_t key_bits; |
| 6821 | unsigned char *signature = NULL; |
| 6822 | size_t signature_size; |
| 6823 | size_t signature_length = 0xdeadbeef; |
| 6824 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6825 | |
| 6826 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6827 | |
| 6828 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6829 | psa_set_key_algorithm( &attributes, alg ); |
| 6830 | psa_set_key_type( &attributes, key_type ); |
| 6831 | |
| 6832 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6833 | &key ) ); |
| 6834 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6835 | key_bits = psa_get_key_bits( &attributes ); |
| 6836 | |
| 6837 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6838 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6839 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6840 | ASSERT_ALLOC( signature, signature_size ); |
| 6841 | |
| 6842 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6843 | input_data->x, input_data->len, |
| 6844 | signature, signature_size, |
| 6845 | &signature_length ) ); |
| 6846 | |
| 6847 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6848 | signature, signature_length ); |
| 6849 | |
| 6850 | exit: |
| 6851 | psa_reset_key_attributes( &attributes ); |
| 6852 | |
| 6853 | psa_destroy_key( key ); |
| 6854 | mbedtls_free( signature ); |
| 6855 | PSA_DONE( ); |
| 6856 | |
| 6857 | } |
| 6858 | /* END_CASE */ |
| 6859 | |
| 6860 | /* BEGIN_CASE */ |
| 6861 | void sign_message_fail( int key_type_arg, |
| 6862 | data_t *key_data, |
| 6863 | int alg_arg, |
| 6864 | data_t *input_data, |
| 6865 | int signature_size_arg, |
| 6866 | int expected_status_arg ) |
| 6867 | { |
| 6868 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6869 | psa_key_type_t key_type = key_type_arg; |
| 6870 | psa_algorithm_t alg = alg_arg; |
| 6871 | size_t signature_size = signature_size_arg; |
| 6872 | psa_status_t actual_status; |
| 6873 | psa_status_t expected_status = expected_status_arg; |
| 6874 | unsigned char *signature = NULL; |
| 6875 | size_t signature_length = 0xdeadbeef; |
| 6876 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6877 | |
| 6878 | ASSERT_ALLOC( signature, signature_size ); |
| 6879 | |
| 6880 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6881 | |
| 6882 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6883 | psa_set_key_algorithm( &attributes, alg ); |
| 6884 | psa_set_key_type( &attributes, key_type ); |
| 6885 | |
| 6886 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6887 | &key ) ); |
| 6888 | |
| 6889 | actual_status = psa_sign_message( key, alg, |
| 6890 | input_data->x, input_data->len, |
| 6891 | signature, signature_size, |
| 6892 | &signature_length ); |
| 6893 | TEST_EQUAL( actual_status, expected_status ); |
| 6894 | /* The value of *signature_length is unspecified on error, but |
| 6895 | * whatever it is, it should be less than signature_size, so that |
| 6896 | * if the caller tries to read *signature_length bytes without |
| 6897 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6898 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6899 | |
| 6900 | exit: |
| 6901 | psa_reset_key_attributes( &attributes ); |
| 6902 | psa_destroy_key( key ); |
| 6903 | mbedtls_free( signature ); |
| 6904 | PSA_DONE( ); |
| 6905 | } |
| 6906 | /* END_CASE */ |
| 6907 | |
| 6908 | /* BEGIN_CASE */ |
| 6909 | void sign_verify_message( int key_type_arg, |
| 6910 | data_t *key_data, |
| 6911 | int alg_arg, |
| 6912 | data_t *input_data ) |
| 6913 | { |
| 6914 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6915 | psa_key_type_t key_type = key_type_arg; |
| 6916 | psa_algorithm_t alg = alg_arg; |
| 6917 | size_t key_bits; |
| 6918 | unsigned char *signature = NULL; |
| 6919 | size_t signature_size; |
| 6920 | size_t signature_length = 0xdeadbeef; |
| 6921 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6922 | |
| 6923 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6924 | |
| 6925 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 6926 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6927 | psa_set_key_algorithm( &attributes, alg ); |
| 6928 | psa_set_key_type( &attributes, key_type ); |
| 6929 | |
| 6930 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6931 | &key ) ); |
| 6932 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6933 | key_bits = psa_get_key_bits( &attributes ); |
| 6934 | |
| 6935 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6936 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6937 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6938 | ASSERT_ALLOC( signature, signature_size ); |
| 6939 | |
| 6940 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6941 | input_data->x, input_data->len, |
| 6942 | signature, signature_size, |
| 6943 | &signature_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6944 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6945 | TEST_ASSERT( signature_length > 0 ); |
| 6946 | |
| 6947 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6948 | input_data->x, input_data->len, |
| 6949 | signature, signature_length ) ); |
| 6950 | |
| 6951 | if( input_data->len != 0 ) |
| 6952 | { |
| 6953 | /* Flip a bit in the input and verify that the signature is now |
| 6954 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6955 | * because ECDSA may ignore the last few bits of the input. */ |
| 6956 | input_data->x[0] ^= 1; |
| 6957 | TEST_EQUAL( psa_verify_message( key, alg, |
| 6958 | input_data->x, input_data->len, |
| 6959 | signature, signature_length ), |
| 6960 | PSA_ERROR_INVALID_SIGNATURE ); |
| 6961 | } |
| 6962 | |
| 6963 | exit: |
| 6964 | psa_reset_key_attributes( &attributes ); |
| 6965 | |
| 6966 | psa_destroy_key( key ); |
| 6967 | mbedtls_free( signature ); |
| 6968 | PSA_DONE( ); |
| 6969 | } |
| 6970 | /* END_CASE */ |
| 6971 | |
| 6972 | /* BEGIN_CASE */ |
| 6973 | void verify_message( int key_type_arg, |
| 6974 | data_t *key_data, |
| 6975 | int alg_arg, |
| 6976 | data_t *input_data, |
| 6977 | data_t *signature_data ) |
| 6978 | { |
| 6979 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6980 | psa_key_type_t key_type = key_type_arg; |
| 6981 | psa_algorithm_t alg = alg_arg; |
| 6982 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6983 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6984 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6985 | |
| 6986 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6987 | |
| 6988 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6989 | psa_set_key_algorithm( &attributes, alg ); |
| 6990 | psa_set_key_type( &attributes, key_type ); |
| 6991 | |
| 6992 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6993 | &key ) ); |
| 6994 | |
| 6995 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6996 | input_data->x, input_data->len, |
| 6997 | signature_data->x, signature_data->len ) ); |
| 6998 | |
| 6999 | exit: |
| 7000 | psa_reset_key_attributes( &attributes ); |
| 7001 | psa_destroy_key( key ); |
| 7002 | PSA_DONE( ); |
| 7003 | } |
| 7004 | /* END_CASE */ |
| 7005 | |
| 7006 | /* BEGIN_CASE */ |
| 7007 | void verify_message_fail( int key_type_arg, |
| 7008 | data_t *key_data, |
| 7009 | int alg_arg, |
| 7010 | data_t *hash_data, |
| 7011 | data_t *signature_data, |
| 7012 | int expected_status_arg ) |
| 7013 | { |
| 7014 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7015 | psa_key_type_t key_type = key_type_arg; |
| 7016 | psa_algorithm_t alg = alg_arg; |
| 7017 | psa_status_t actual_status; |
| 7018 | psa_status_t expected_status = expected_status_arg; |
| 7019 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7020 | |
| 7021 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7022 | |
| 7023 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 7024 | psa_set_key_algorithm( &attributes, alg ); |
| 7025 | psa_set_key_type( &attributes, key_type ); |
| 7026 | |
| 7027 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 7028 | &key ) ); |
| 7029 | |
| 7030 | actual_status = psa_verify_message( key, alg, |
| 7031 | hash_data->x, hash_data->len, |
| 7032 | signature_data->x, |
| 7033 | signature_data->len ); |
| 7034 | TEST_EQUAL( actual_status, expected_status ); |
| 7035 | |
| 7036 | exit: |
| 7037 | psa_reset_key_attributes( &attributes ); |
| 7038 | psa_destroy_key( key ); |
| 7039 | PSA_DONE( ); |
| 7040 | } |
| 7041 | /* END_CASE */ |
| 7042 | |
| 7043 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7044 | void asymmetric_encrypt( int key_type_arg, |
| 7045 | data_t *key_data, |
| 7046 | int alg_arg, |
| 7047 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7048 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7049 | int expected_output_length_arg, |
| 7050 | int expected_status_arg ) |
| 7051 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7052 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7053 | psa_key_type_t key_type = key_type_arg; |
| 7054 | psa_algorithm_t alg = alg_arg; |
| 7055 | size_t expected_output_length = expected_output_length_arg; |
| 7056 | size_t key_bits; |
| 7057 | unsigned char *output = NULL; |
| 7058 | size_t output_size; |
| 7059 | size_t output_length = ~0; |
| 7060 | psa_status_t actual_status; |
| 7061 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7062 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7063 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7064 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 7065 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7066 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7067 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7068 | psa_set_key_algorithm( &attributes, alg ); |
| 7069 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7070 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7071 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7072 | |
| 7073 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7074 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7075 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7076 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7077 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7078 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7079 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7080 | |
| 7081 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7082 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7083 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7084 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7085 | output, output_size, |
| 7086 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7087 | TEST_EQUAL( actual_status, expected_status ); |
| 7088 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7089 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7090 | /* If the label is empty, the test framework puts a non-null pointer |
| 7091 | * in label->x. Test that a null pointer works as well. */ |
| 7092 | if( label->len == 0 ) |
| 7093 | { |
| 7094 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7095 | if( output_size != 0 ) |
| 7096 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7097 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7098 | input_data->x, input_data->len, |
| 7099 | NULL, label->len, |
| 7100 | output, output_size, |
| 7101 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7102 | TEST_EQUAL( actual_status, expected_status ); |
| 7103 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7104 | } |
| 7105 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7106 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7107 | /* |
| 7108 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7109 | * thus reset them as required. |
| 7110 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7111 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7112 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7113 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7114 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7115 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7116 | } |
| 7117 | /* END_CASE */ |
| 7118 | |
| 7119 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7120 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 7121 | data_t *key_data, |
| 7122 | int alg_arg, |
| 7123 | data_t *input_data, |
| 7124 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7125 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7126 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7127 | psa_key_type_t key_type = key_type_arg; |
| 7128 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7129 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7130 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7131 | size_t output_size; |
| 7132 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7133 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7134 | size_t output2_size; |
| 7135 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7136 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7137 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7138 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7139 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7140 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 7141 | psa_set_key_algorithm( &attributes, alg ); |
| 7142 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7143 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7144 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7145 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7146 | |
| 7147 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7148 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7149 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7150 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7151 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7152 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7153 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7154 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7155 | output2_size = input_data->len; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7156 | TEST_LE_U( output2_size, |
| 7157 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 7158 | TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7159 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7160 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 7161 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 7162 | * the original plaintext because of the non-optional random |
| 7163 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7164 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7165 | input_data->x, input_data->len, |
| 7166 | label->x, label->len, |
| 7167 | output, output_size, |
| 7168 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7169 | /* We don't know what ciphertext length to expect, but check that |
| 7170 | * it looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7171 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7172 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7173 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7174 | output, output_length, |
| 7175 | label->x, label->len, |
| 7176 | output2, output2_size, |
| 7177 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7178 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 7179 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7180 | |
| 7181 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7182 | /* |
| 7183 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7184 | * thus reset them as required. |
| 7185 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7186 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7187 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7188 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7189 | mbedtls_free( output ); |
| 7190 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7191 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7192 | } |
| 7193 | /* END_CASE */ |
| 7194 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7195 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7196 | void asymmetric_decrypt( int key_type_arg, |
| 7197 | data_t *key_data, |
| 7198 | int alg_arg, |
| 7199 | data_t *input_data, |
| 7200 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 7201 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7202 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7203 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7204 | psa_key_type_t key_type = key_type_arg; |
| 7205 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7206 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7207 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 7208 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7209 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7210 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7211 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7212 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7213 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7214 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7215 | psa_set_key_algorithm( &attributes, alg ); |
| 7216 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7217 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7218 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7219 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7220 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7221 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 7222 | key_bits = psa_get_key_bits( &attributes ); |
| 7223 | |
| 7224 | /* Determine the maximum ciphertext length */ |
| 7225 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7226 | TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7227 | ASSERT_ALLOC( output, output_size ); |
| 7228 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7229 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7230 | input_data->x, input_data->len, |
| 7231 | label->x, label->len, |
| 7232 | output, |
| 7233 | output_size, |
| 7234 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7235 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7236 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7237 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7238 | /* If the label is empty, the test framework puts a non-null pointer |
| 7239 | * in label->x. Test that a null pointer works as well. */ |
| 7240 | if( label->len == 0 ) |
| 7241 | { |
| 7242 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7243 | if( output_size != 0 ) |
| 7244 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7245 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7246 | input_data->x, input_data->len, |
| 7247 | NULL, label->len, |
| 7248 | output, |
| 7249 | output_size, |
| 7250 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7251 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7252 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7253 | } |
| 7254 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7255 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7256 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7257 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7258 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7259 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7260 | } |
| 7261 | /* END_CASE */ |
| 7262 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7263 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7264 | void asymmetric_decrypt_fail( int key_type_arg, |
| 7265 | data_t *key_data, |
| 7266 | int alg_arg, |
| 7267 | data_t *input_data, |
| 7268 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7269 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7270 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7271 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7272 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7273 | psa_key_type_t key_type = key_type_arg; |
| 7274 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7275 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7276 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7277 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7278 | psa_status_t actual_status; |
| 7279 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7280 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7281 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7282 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7283 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7284 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7285 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7286 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7287 | psa_set_key_algorithm( &attributes, alg ); |
| 7288 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7289 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7290 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7291 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7292 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7293 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7294 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7295 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7296 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7297 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7298 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7299 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7300 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7301 | /* If the label is empty, the test framework puts a non-null pointer |
| 7302 | * in label->x. Test that a null pointer works as well. */ |
| 7303 | if( label->len == 0 ) |
| 7304 | { |
| 7305 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7306 | if( output_size != 0 ) |
| 7307 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7308 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7309 | input_data->x, input_data->len, |
| 7310 | NULL, label->len, |
| 7311 | output, output_size, |
| 7312 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7313 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7314 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7315 | } |
| 7316 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7317 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7318 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7319 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7320 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7321 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7322 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7323 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7324 | |
| 7325 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 7326 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7327 | { |
| 7328 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 7329 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 7330 | * 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] | 7331 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7332 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7333 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 7334 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7335 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7336 | |
| 7337 | memset( &zero, 0, sizeof( zero ) ); |
| 7338 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7339 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7340 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7341 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7342 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7343 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7344 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7345 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7346 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7347 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7348 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 7349 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 7350 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7351 | } |
| 7352 | /* END_CASE */ |
| 7353 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7354 | /* BEGIN_CASE */ |
| 7355 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7356 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7357 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7358 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7359 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7360 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7361 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7362 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7363 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7364 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7365 | |
| 7366 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7367 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7368 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7369 | } |
| 7370 | /* END_CASE */ |
| 7371 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7372 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7373 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 7374 | int expected_status_arg ) |
| 7375 | { |
| 7376 | psa_algorithm_t alg = alg_arg; |
| 7377 | size_t capacity = capacity_arg; |
| 7378 | psa_status_t expected_status = expected_status_arg; |
| 7379 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7380 | |
| 7381 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7382 | |
| 7383 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7384 | |
| 7385 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 7386 | expected_status ); |
| 7387 | |
| 7388 | exit: |
| 7389 | psa_key_derivation_abort( &operation ); |
| 7390 | PSA_DONE( ); |
| 7391 | } |
| 7392 | /* END_CASE */ |
| 7393 | |
| 7394 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7395 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7396 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7397 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7398 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7399 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7400 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7401 | int expected_status_arg3, |
| 7402 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7403 | { |
| 7404 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7405 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 7406 | 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] | 7407 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 7408 | expected_status_arg2, |
| 7409 | expected_status_arg3}; |
| 7410 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7411 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 7412 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7413 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7414 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7415 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7416 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7417 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7418 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7419 | psa_status_t expected_output_status = expected_output_status_arg; |
| 7420 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7421 | |
| 7422 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7423 | |
| 7424 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7425 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7426 | |
| 7427 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7428 | |
| 7429 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 7430 | { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 7431 | mbedtls_test_set_step( i ); |
| 7432 | if( steps[i] == 0 ) |
| 7433 | { |
| 7434 | /* Skip this step */ |
| 7435 | } |
| 7436 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7437 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7438 | psa_set_key_type( &attributes, key_types[i] ); |
| 7439 | PSA_ASSERT( psa_import_key( &attributes, |
| 7440 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7441 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7442 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 7443 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 7444 | { |
| 7445 | // When taking a private key as secret input, use key agreement |
| 7446 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7447 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 7448 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7449 | expected_statuses[i] ); |
| 7450 | } |
| 7451 | else |
| 7452 | { |
| 7453 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7454 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7455 | expected_statuses[i] ); |
| 7456 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7457 | } |
| 7458 | else |
| 7459 | { |
| 7460 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 7461 | &operation, steps[i], |
| 7462 | inputs[i]->x, inputs[i]->len ), |
| 7463 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7464 | } |
| 7465 | } |
| 7466 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7467 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 7468 | { |
| 7469 | psa_reset_key_attributes( &attributes ); |
Dave Rodgman | 491d849 | 2021-11-16 12:12:49 +0000 | [diff] [blame] | 7470 | psa_set_key_type( &attributes, output_key_type ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7471 | psa_set_key_bits( &attributes, 8 ); |
| 7472 | actual_output_status = |
| 7473 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7474 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7475 | } |
| 7476 | else |
| 7477 | { |
| 7478 | uint8_t buffer[1]; |
| 7479 | actual_output_status = |
| 7480 | psa_key_derivation_output_bytes( &operation, |
| 7481 | buffer, sizeof( buffer ) ); |
| 7482 | } |
| 7483 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 7484 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7485 | exit: |
| 7486 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7487 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7488 | psa_destroy_key( keys[i] ); |
| 7489 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7490 | PSA_DONE( ); |
| 7491 | } |
| 7492 | /* END_CASE */ |
| 7493 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7494 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7495 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7496 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7497 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7498 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 7499 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7500 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7501 | unsigned char input1[] = "Input 1"; |
| 7502 | size_t input1_length = sizeof( input1 ); |
| 7503 | unsigned char input2[] = "Input 2"; |
| 7504 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7505 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 7506 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 7507 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7508 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7509 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7510 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7511 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7512 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7513 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7514 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7515 | psa_set_key_algorithm( &attributes, alg ); |
| 7516 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7517 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 7518 | PSA_ASSERT( psa_import_key( &attributes, |
| 7519 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7520 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7521 | |
| 7522 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7523 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7524 | input1, input1_length, |
| 7525 | input2, input2_length, |
| 7526 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7527 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7528 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7529 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7530 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7531 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7532 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7533 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7534 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7535 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7536 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7537 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7538 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7539 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7540 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7541 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7542 | } |
| 7543 | /* END_CASE */ |
| 7544 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7545 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7546 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7547 | { |
| 7548 | uint8_t output_buffer[16]; |
| 7549 | size_t buffer_size = 16; |
| 7550 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7551 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7552 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7553 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7554 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7555 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7556 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7557 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7558 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7559 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7560 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7561 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7562 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7563 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7564 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7565 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7566 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7567 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7568 | |
| 7569 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7570 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7571 | } |
| 7572 | /* END_CASE */ |
| 7573 | |
| 7574 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7575 | void derive_output( int alg_arg, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7576 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 7577 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 7578 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 7579 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 7580 | data_t *key_agreement_peer_key, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7581 | int requested_capacity_arg, |
| 7582 | data_t *expected_output1, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7583 | data_t *expected_output2, |
| 7584 | int other_key_input_type, |
| 7585 | int key_input_type, |
| 7586 | int derive_type ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7587 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7588 | psa_algorithm_t alg = alg_arg; |
Przemek Stekiel | ffbb7d3 | 2022-03-31 11:13:47 +0200 | [diff] [blame] | 7589 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg}; |
| 7590 | data_t *inputs[] = {input1, input2, input3, input4}; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7591 | mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT, |
| 7592 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7593 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7594 | MBEDTLS_SVC_KEY_ID_INIT}; |
| 7595 | psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2, |
| 7596 | expected_status_arg3, expected_status_arg4}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7597 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7598 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7599 | uint8_t *expected_outputs[2] = |
| 7600 | {expected_output1->x, expected_output2->x}; |
| 7601 | size_t output_sizes[2] = |
| 7602 | {expected_output1->len, expected_output2->len}; |
| 7603 | size_t output_buffer_size = 0; |
| 7604 | uint8_t *output_buffer = NULL; |
| 7605 | size_t expected_capacity; |
| 7606 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7607 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 7608 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 7609 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 7610 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7611 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7612 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7613 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7614 | |
| 7615 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 7616 | { |
| 7617 | if( output_sizes[i] > output_buffer_size ) |
| 7618 | output_buffer_size = output_sizes[i]; |
| 7619 | if( output_sizes[i] == 0 ) |
| 7620 | expected_outputs[i] = NULL; |
| 7621 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7622 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7623 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7624 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7625 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7626 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7627 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 7628 | requested_capacity ) ); |
| 7629 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7630 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7631 | switch( steps[i] ) |
| 7632 | { |
| 7633 | case 0: |
| 7634 | break; |
| 7635 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7636 | switch( key_input_type ) |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7637 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7638 | case 0: // input bytes |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7639 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7640 | &operation, steps[i], |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7641 | inputs[i]->x, inputs[i]->len ), |
| 7642 | statuses[i] ); |
| 7643 | |
| 7644 | if( statuses[i] != PSA_SUCCESS ) |
| 7645 | goto exit; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7646 | break; |
| 7647 | case 1: // input key |
| 7648 | psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE ); |
| 7649 | psa_set_key_algorithm( &attributes1, alg ); |
| 7650 | psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE ); |
| 7651 | |
| 7652 | PSA_ASSERT( psa_import_key( &attributes1, |
| 7653 | inputs[i]->x, inputs[i]->len, |
| 7654 | &keys[i] ) ); |
| 7655 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7656 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7657 | { |
| 7658 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7659 | TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ), |
| 7660 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7661 | } |
| 7662 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7663 | PSA_ASSERT( psa_key_derivation_input_key( &operation, |
| 7664 | steps[i], |
| 7665 | keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7666 | break; |
| 7667 | default: |
| 7668 | TEST_ASSERT( ! "default case not supported" ); |
| 7669 | break; |
| 7670 | } |
| 7671 | break; |
| 7672 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7673 | switch( other_key_input_type ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7674 | { |
| 7675 | case 0: // input bytes |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7676 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
| 7677 | steps[i], |
| 7678 | inputs[i]->x, |
| 7679 | inputs[i]->len ), |
| 7680 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7681 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7682 | case 1: // input key, type DERIVE |
| 7683 | case 11: // input key, type RAW |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7684 | psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE ); |
| 7685 | psa_set_key_algorithm( &attributes2, alg ); |
| 7686 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE ); |
| 7687 | |
| 7688 | // other secret of type RAW_DATA passed with input_key |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7689 | if( other_key_input_type == 11 ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7690 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA ); |
| 7691 | |
| 7692 | PSA_ASSERT( psa_import_key( &attributes2, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7693 | inputs[i]->x, inputs[i]->len, |
| 7694 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7695 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7696 | TEST_EQUAL( psa_key_derivation_input_key( &operation, |
| 7697 | steps[i], |
| 7698 | keys[i] ), |
| 7699 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7700 | break; |
| 7701 | case 2: // key agreement |
| 7702 | psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE ); |
| 7703 | psa_set_key_algorithm( &attributes3, alg ); |
| 7704 | psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) ); |
| 7705 | |
| 7706 | PSA_ASSERT( psa_import_key( &attributes3, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7707 | inputs[i]->x, inputs[i]->len, |
| 7708 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7709 | |
| 7710 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 7711 | &operation, |
| 7712 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 7713 | keys[i], key_agreement_peer_key->x, |
| 7714 | key_agreement_peer_key->len ), statuses[i] ); |
| 7715 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7716 | default: |
| 7717 | TEST_ASSERT( ! "default case not supported" ); |
| 7718 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7719 | } |
| 7720 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7721 | if( statuses[i] != PSA_SUCCESS ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7722 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7723 | break; |
| 7724 | default: |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7725 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7726 | &operation, steps[i], |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7727 | inputs[i]->x, inputs[i]->len ), statuses[i] ); |
| 7728 | |
| 7729 | if( statuses[i] != PSA_SUCCESS ) |
| 7730 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7731 | break; |
| 7732 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7733 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7734 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7735 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7736 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7737 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7738 | expected_capacity = requested_capacity; |
| 7739 | |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7740 | if( derive_type == 1 ) // output key |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7741 | { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7742 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 7743 | |
| 7744 | /* For output key derivation secret must be provided using |
| 7745 | input key, otherwise operation is not permitted. */ |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 7746 | if( key_input_type == 1 ) |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7747 | expected_status = PSA_SUCCESS; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7748 | |
| 7749 | psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT ); |
| 7750 | psa_set_key_algorithm( &attributes4, alg ); |
| 7751 | psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE ); |
Przemek Stekiel | 0e99391 | 2022-06-03 15:01:14 +0200 | [diff] [blame] | 7752 | psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7753 | |
| 7754 | TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation, |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7755 | &derived_key ), expected_status ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7756 | } |
| 7757 | else // output bytes |
| 7758 | { |
| 7759 | /* Expansion phase. */ |
| 7760 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7761 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7762 | /* Read some bytes. */ |
| 7763 | status = psa_key_derivation_output_bytes( &operation, |
| 7764 | output_buffer, output_sizes[i] ); |
| 7765 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 7766 | { |
| 7767 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 7768 | TEST_ASSERT( status == PSA_SUCCESS || |
| 7769 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
| 7770 | continue; |
| 7771 | } |
| 7772 | else if( expected_capacity == 0 || |
| 7773 | output_sizes[i] > expected_capacity ) |
| 7774 | { |
| 7775 | /* Capacity exceeded. */ |
| 7776 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
| 7777 | expected_capacity = 0; |
| 7778 | continue; |
| 7779 | } |
| 7780 | /* Success. Check the read data. */ |
| 7781 | PSA_ASSERT( status ); |
| 7782 | if( output_sizes[i] != 0 ) |
| 7783 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 7784 | expected_outputs[i], output_sizes[i] ); |
| 7785 | /* Check the operation status. */ |
| 7786 | expected_capacity -= output_sizes[i]; |
| 7787 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
| 7788 | ¤t_capacity ) ); |
| 7789 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7790 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7791 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7792 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7793 | |
| 7794 | exit: |
| 7795 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7796 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7797 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7798 | psa_destroy_key( keys[i] ); |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7799 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7800 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7801 | } |
| 7802 | /* END_CASE */ |
| 7803 | |
| 7804 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7805 | void derive_full( int alg_arg, |
| 7806 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7807 | data_t *input1, |
| 7808 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7809 | int requested_capacity_arg ) |
| 7810 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7811 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7812 | psa_algorithm_t alg = alg_arg; |
| 7813 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7814 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7815 | unsigned char output_buffer[16]; |
| 7816 | size_t expected_capacity = requested_capacity; |
| 7817 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7818 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7819 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7820 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7821 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7822 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7823 | psa_set_key_algorithm( &attributes, alg ); |
| 7824 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7825 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7826 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7827 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7828 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7829 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7830 | input1->x, input1->len, |
| 7831 | input2->x, input2->len, |
| 7832 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 7833 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7834 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7835 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7836 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7837 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7838 | |
| 7839 | /* Expansion phase. */ |
| 7840 | while( current_capacity > 0 ) |
| 7841 | { |
| 7842 | size_t read_size = sizeof( output_buffer ); |
| 7843 | if( read_size > current_capacity ) |
| 7844 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7845 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7846 | output_buffer, |
| 7847 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7848 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7849 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7850 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7851 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7852 | } |
| 7853 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7854 | /* Check that the operation refuses to go over capacity. */ |
| 7855 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7856 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7857 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7858 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7859 | |
| 7860 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7861 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7862 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7863 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7864 | } |
| 7865 | /* END_CASE */ |
| 7866 | |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 7867 | /* 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] | 7868 | 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] | 7869 | int derivation_step, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7870 | int capacity, int expected_capacity_status_arg, |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7871 | data_t *expected_output, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7872 | int expected_output_status_arg ) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7873 | { |
| 7874 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 7875 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 7876 | 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] | 7877 | uint8_t *output_buffer = NULL; |
| 7878 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7879 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 7880 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 7881 | 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] | 7882 | |
| 7883 | ASSERT_ALLOC( output_buffer, expected_output->len ); |
| 7884 | PSA_ASSERT( psa_crypto_init() ); |
| 7885 | |
| 7886 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7887 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7888 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7889 | |
| 7890 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7891 | step, input->x, input->len ), |
| 7892 | expected_input_status ); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7893 | |
| 7894 | if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS ) |
| 7895 | goto exit; |
| 7896 | |
| 7897 | status = psa_key_derivation_output_bytes( &operation, output_buffer, |
| 7898 | expected_output->len ); |
| 7899 | |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7900 | TEST_EQUAL( status, expected_output_status ); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7901 | if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS ) |
| 7902 | ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x, |
| 7903 | expected_output->len ); |
| 7904 | |
| 7905 | exit: |
| 7906 | mbedtls_free( output_buffer ); |
| 7907 | psa_key_derivation_abort( &operation ); |
| 7908 | PSA_DONE(); |
| 7909 | } |
| 7910 | /* END_CASE */ |
| 7911 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7912 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7913 | void derive_key_exercise( int alg_arg, |
| 7914 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7915 | data_t *input1, |
| 7916 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7917 | int derived_type_arg, |
| 7918 | int derived_bits_arg, |
| 7919 | int derived_usage_arg, |
| 7920 | int derived_alg_arg ) |
| 7921 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7922 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7923 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7924 | psa_algorithm_t alg = alg_arg; |
| 7925 | psa_key_type_t derived_type = derived_type_arg; |
| 7926 | size_t derived_bits = derived_bits_arg; |
| 7927 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 7928 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 7929 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7930 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7931 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7932 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7933 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7934 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7935 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7936 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7937 | psa_set_key_algorithm( &attributes, alg ); |
| 7938 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7939 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7940 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7941 | |
| 7942 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7943 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7944 | input1->x, input1->len, |
| 7945 | input2->x, input2->len, |
| 7946 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7947 | goto exit; |
| 7948 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7949 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 7950 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 7951 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7952 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7953 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7954 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7955 | |
| 7956 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7957 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7958 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 7959 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7960 | |
| 7961 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7962 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7963 | goto exit; |
| 7964 | |
| 7965 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7966 | /* |
| 7967 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7968 | * thus reset them as required. |
| 7969 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7970 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7971 | |
| 7972 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7973 | psa_destroy_key( base_key ); |
| 7974 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7975 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7976 | } |
| 7977 | /* END_CASE */ |
| 7978 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7979 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7980 | void derive_key_export( int alg_arg, |
| 7981 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7982 | data_t *input1, |
| 7983 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7984 | int bytes1_arg, |
| 7985 | int bytes2_arg ) |
| 7986 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7987 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7988 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7989 | psa_algorithm_t alg = alg_arg; |
| 7990 | size_t bytes1 = bytes1_arg; |
| 7991 | size_t bytes2 = bytes2_arg; |
| 7992 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7993 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7994 | uint8_t *output_buffer = NULL; |
| 7995 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7996 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7997 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7998 | size_t length; |
| 7999 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8000 | ASSERT_ALLOC( output_buffer, capacity ); |
| 8001 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8002 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8003 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8004 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8005 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8006 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8007 | 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] | 8008 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8009 | |
| 8010 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8011 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 8012 | input1->x, input1->len, |
| 8013 | input2->x, input2->len, |
| 8014 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8015 | goto exit; |
| 8016 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8017 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8018 | output_buffer, |
| 8019 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8020 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8021 | |
| 8022 | /* 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] | 8023 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 8024 | input1->x, input1->len, |
| 8025 | input2->x, input2->len, |
| 8026 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8027 | goto exit; |
| 8028 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8029 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8030 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 8031 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8032 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8033 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8034 | &derived_key ) ); |
| 8035 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8036 | export_buffer, bytes1, |
| 8037 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8038 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8039 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8040 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8041 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8042 | &derived_key ) ); |
| 8043 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8044 | export_buffer + bytes1, bytes2, |
| 8045 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8046 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8047 | |
| 8048 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8049 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 8050 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8051 | |
| 8052 | exit: |
| 8053 | mbedtls_free( output_buffer ); |
| 8054 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8055 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8056 | psa_destroy_key( base_key ); |
| 8057 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8058 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8059 | } |
| 8060 | /* END_CASE */ |
| 8061 | |
| 8062 | /* BEGIN_CASE */ |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8063 | void derive_key_type( int alg_arg, |
| 8064 | data_t *key_data, |
| 8065 | data_t *input1, |
| 8066 | data_t *input2, |
| 8067 | int key_type_arg, int bits_arg, |
| 8068 | data_t *expected_export ) |
| 8069 | { |
| 8070 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8071 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8072 | const psa_algorithm_t alg = alg_arg; |
| 8073 | const psa_key_type_t key_type = key_type_arg; |
| 8074 | const size_t bits = bits_arg; |
| 8075 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8076 | const size_t export_buffer_size = |
| 8077 | PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits ); |
| 8078 | uint8_t *export_buffer = NULL; |
| 8079 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8080 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8081 | size_t export_length; |
| 8082 | |
| 8083 | ASSERT_ALLOC( export_buffer, export_buffer_size ); |
| 8084 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8085 | |
| 8086 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8087 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8088 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8089 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 8090 | &base_key ) ); |
| 8091 | |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 8092 | if( mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8093 | &operation, base_key, alg, |
| 8094 | input1->x, input1->len, |
| 8095 | input2->x, input2->len, |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 8096 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 ) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8097 | goto exit; |
| 8098 | |
| 8099 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8100 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 8101 | psa_set_key_type( &derived_attributes, key_type ); |
| 8102 | psa_set_key_bits( &derived_attributes, bits ); |
| 8103 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 8104 | &derived_key ) ); |
| 8105 | |
| 8106 | PSA_ASSERT( psa_export_key( derived_key, |
| 8107 | export_buffer, export_buffer_size, |
| 8108 | &export_length ) ); |
| 8109 | ASSERT_COMPARE( export_buffer, export_length, |
| 8110 | expected_export->x, expected_export->len ); |
| 8111 | |
| 8112 | exit: |
| 8113 | mbedtls_free( export_buffer ); |
| 8114 | psa_key_derivation_abort( &operation ); |
| 8115 | psa_destroy_key( base_key ); |
| 8116 | psa_destroy_key( derived_key ); |
| 8117 | PSA_DONE( ); |
| 8118 | } |
| 8119 | /* END_CASE */ |
| 8120 | |
| 8121 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8122 | void derive_key( int alg_arg, |
| 8123 | data_t *key_data, data_t *input1, data_t *input2, |
| 8124 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8125 | int expected_status_arg, |
| 8126 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8127 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8128 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8129 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8130 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8131 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8132 | size_t bits = bits_arg; |
| 8133 | psa_status_t expected_status = expected_status_arg; |
| 8134 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8135 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8136 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8137 | |
| 8138 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8139 | |
| 8140 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8141 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8142 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8143 | 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] | 8144 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8145 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8146 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 8147 | input1->x, input1->len, |
| 8148 | input2->x, input2->len, |
| 8149 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8150 | goto exit; |
| 8151 | |
| 8152 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8153 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8154 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8155 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8156 | |
| 8157 | psa_status_t status = |
| 8158 | psa_key_derivation_output_key( &derived_attributes, |
| 8159 | &operation, |
| 8160 | &derived_key ); |
| 8161 | if( is_large_output > 0 ) |
| 8162 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8163 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8164 | |
| 8165 | exit: |
| 8166 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8167 | psa_destroy_key( base_key ); |
| 8168 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8169 | PSA_DONE( ); |
| 8170 | } |
| 8171 | /* END_CASE */ |
| 8172 | |
| 8173 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8174 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 8175 | int our_key_type_arg, int our_key_alg_arg, |
| 8176 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8177 | int expected_status_arg ) |
| 8178 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8179 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8180 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8181 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8182 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8183 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8184 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8185 | psa_status_t expected_status = expected_status_arg; |
| 8186 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8187 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8188 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8189 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8190 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8191 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8192 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8193 | PSA_ASSERT( psa_import_key( &attributes, |
| 8194 | our_key_data->x, our_key_data->len, |
| 8195 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8196 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8197 | /* The tests currently include inputs that should fail at either step. |
| 8198 | * Test cases that fail at the setup step should be changed to call |
| 8199 | * key_derivation_setup instead, and this function should be renamed |
| 8200 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8201 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8202 | if( status == PSA_SUCCESS ) |
| 8203 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8204 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 8205 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 8206 | our_key, |
| 8207 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8208 | expected_status ); |
| 8209 | } |
| 8210 | else |
| 8211 | { |
| 8212 | TEST_ASSERT( status == expected_status ); |
| 8213 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8214 | |
| 8215 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8216 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8217 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8218 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8219 | } |
| 8220 | /* END_CASE */ |
| 8221 | |
| 8222 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8223 | void raw_key_agreement( int alg_arg, |
| 8224 | int our_key_type_arg, data_t *our_key_data, |
| 8225 | data_t *peer_key_data, |
| 8226 | data_t *expected_output ) |
| 8227 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8228 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8229 | psa_algorithm_t alg = alg_arg; |
| 8230 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8231 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8232 | unsigned char *output = NULL; |
| 8233 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8234 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8235 | |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8236 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8237 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8238 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8239 | psa_set_key_algorithm( &attributes, alg ); |
| 8240 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8241 | PSA_ASSERT( psa_import_key( &attributes, |
| 8242 | our_key_data->x, our_key_data->len, |
| 8243 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8244 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8245 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 8246 | key_bits = psa_get_key_bits( &attributes ); |
| 8247 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8248 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8249 | TEST_LE_U( expected_output->len, |
| 8250 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 8251 | TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ), |
| 8252 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8253 | |
| 8254 | /* Good case with exact output size */ |
| 8255 | ASSERT_ALLOC( output, expected_output->len ); |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 8256 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8257 | peer_key_data->x, peer_key_data->len, |
| 8258 | output, expected_output->len, |
| 8259 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8260 | ASSERT_COMPARE( output, output_length, |
| 8261 | expected_output->x, expected_output->len ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8262 | mbedtls_free( output ); |
| 8263 | output = NULL; |
| 8264 | output_length = ~0; |
| 8265 | |
| 8266 | /* Larger buffer */ |
| 8267 | ASSERT_ALLOC( output, expected_output->len + 1 ); |
| 8268 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8269 | peer_key_data->x, peer_key_data->len, |
| 8270 | output, expected_output->len + 1, |
| 8271 | &output_length ) ); |
| 8272 | ASSERT_COMPARE( output, output_length, |
| 8273 | expected_output->x, expected_output->len ); |
| 8274 | mbedtls_free( output ); |
| 8275 | output = NULL; |
| 8276 | output_length = ~0; |
| 8277 | |
| 8278 | /* Buffer too small */ |
| 8279 | ASSERT_ALLOC( output, expected_output->len - 1 ); |
| 8280 | TEST_EQUAL( psa_raw_key_agreement( alg, our_key, |
| 8281 | peer_key_data->x, peer_key_data->len, |
| 8282 | output, expected_output->len - 1, |
| 8283 | &output_length ), |
| 8284 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8285 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8286 | TEST_LE_U( output_length, expected_output->len - 1 ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8287 | mbedtls_free( output ); |
| 8288 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8289 | |
| 8290 | exit: |
| 8291 | mbedtls_free( output ); |
| 8292 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8293 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8294 | } |
| 8295 | /* END_CASE */ |
| 8296 | |
| 8297 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8298 | void key_agreement_capacity( int alg_arg, |
| 8299 | int our_key_type_arg, data_t *our_key_data, |
| 8300 | data_t *peer_key_data, |
| 8301 | int expected_capacity_arg ) |
| 8302 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8303 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8304 | psa_algorithm_t alg = alg_arg; |
| 8305 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8306 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8307 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8308 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8309 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8310 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8311 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8312 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8313 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8314 | psa_set_key_algorithm( &attributes, alg ); |
| 8315 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8316 | PSA_ASSERT( psa_import_key( &attributes, |
| 8317 | our_key_data->x, our_key_data->len, |
| 8318 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8319 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8320 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8321 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8322 | &operation, |
| 8323 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8324 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8325 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8326 | { |
| 8327 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8328 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8329 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8330 | NULL, 0 ) ); |
| 8331 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8332 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 8333 | /* Test the advertised capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8334 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8335 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8336 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8337 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8338 | /* Test the actual capacity by reading the output. */ |
| 8339 | while( actual_capacity > sizeof( output ) ) |
| 8340 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8341 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8342 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8343 | actual_capacity -= sizeof( output ); |
| 8344 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8345 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8346 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8347 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 8348 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8349 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8350 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8351 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8352 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8353 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8354 | } |
| 8355 | /* END_CASE */ |
| 8356 | |
| 8357 | /* BEGIN_CASE */ |
| 8358 | void key_agreement_output( int alg_arg, |
| 8359 | int our_key_type_arg, data_t *our_key_data, |
| 8360 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8361 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8362 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8363 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8364 | psa_algorithm_t alg = alg_arg; |
| 8365 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8366 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8367 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8368 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8369 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8370 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 8371 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8372 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8373 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8374 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8375 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8376 | psa_set_key_algorithm( &attributes, alg ); |
| 8377 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8378 | PSA_ASSERT( psa_import_key( &attributes, |
| 8379 | our_key_data->x, our_key_data->len, |
| 8380 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8381 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8382 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8383 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8384 | &operation, |
| 8385 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8386 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8387 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8388 | { |
| 8389 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8390 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8391 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8392 | NULL, 0 ) ); |
| 8393 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8394 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8395 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8396 | actual_output, |
| 8397 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8398 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 8399 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8400 | if( expected_output2->len != 0 ) |
| 8401 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8402 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8403 | actual_output, |
| 8404 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8405 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 8406 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8407 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8408 | |
| 8409 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8410 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8411 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8412 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8413 | mbedtls_free( actual_output ); |
| 8414 | } |
| 8415 | /* END_CASE */ |
| 8416 | |
| 8417 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8418 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8419 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8420 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8421 | unsigned char *output = NULL; |
| 8422 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8423 | size_t i; |
| 8424 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8425 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 8426 | TEST_ASSERT( bytes_arg >= 0 ); |
| 8427 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 8428 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8429 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8430 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8431 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8432 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8433 | /* Run several times, to ensure that every output byte will be |
| 8434 | * nonzero at least once with overwhelming probability |
| 8435 | * (2^(-8*number_of_runs)). */ |
| 8436 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8437 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 8438 | if( bytes != 0 ) |
| 8439 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8440 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8441 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8442 | for( i = 0; i < bytes; i++ ) |
| 8443 | { |
| 8444 | if( output[i] != 0 ) |
| 8445 | ++changed[i]; |
| 8446 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8447 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8448 | |
| 8449 | /* Check that every byte was changed to nonzero at least once. This |
| 8450 | * validates that psa_generate_random is overwriting every byte of |
| 8451 | * the output buffer. */ |
| 8452 | for( i = 0; i < bytes; i++ ) |
| 8453 | { |
| 8454 | TEST_ASSERT( changed[i] != 0 ); |
| 8455 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8456 | |
| 8457 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8458 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8459 | mbedtls_free( output ); |
| 8460 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8461 | } |
| 8462 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8463 | |
| 8464 | /* BEGIN_CASE */ |
| 8465 | void generate_key( int type_arg, |
| 8466 | int bits_arg, |
| 8467 | int usage_arg, |
| 8468 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8469 | int expected_status_arg, |
| 8470 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8471 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8472 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8473 | psa_key_type_t type = type_arg; |
| 8474 | psa_key_usage_t usage = usage_arg; |
| 8475 | size_t bits = bits_arg; |
| 8476 | psa_algorithm_t alg = alg_arg; |
| 8477 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8478 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8479 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8480 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8481 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8482 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8483 | psa_set_key_usage_flags( &attributes, usage ); |
| 8484 | psa_set_key_algorithm( &attributes, alg ); |
| 8485 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8486 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8487 | |
| 8488 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8489 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 8490 | |
| 8491 | if( is_large_key > 0 ) |
| 8492 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8493 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8494 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8495 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8496 | |
| 8497 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8498 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8499 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 8500 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8501 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 8502 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8503 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 8504 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8505 | |
| 8506 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8507 | /* |
| 8508 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8509 | * thus reset them as required. |
| 8510 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8511 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8512 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8513 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8514 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8515 | } |
| 8516 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 8517 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 8518 | /* 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] | 8519 | void generate_key_rsa( int bits_arg, |
| 8520 | data_t *e_arg, |
| 8521 | int expected_status_arg ) |
| 8522 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8523 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 8524 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8525 | size_t bits = bits_arg; |
| 8526 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 8527 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 8528 | psa_status_t expected_status = expected_status_arg; |
| 8529 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8530 | uint8_t *exported = NULL; |
| 8531 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8532 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8533 | size_t exported_length = SIZE_MAX; |
| 8534 | uint8_t *e_read_buffer = NULL; |
| 8535 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 8536 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8537 | size_t e_read_length = SIZE_MAX; |
| 8538 | |
| 8539 | if( e_arg->len == 0 || |
| 8540 | ( e_arg->len == 3 && |
| 8541 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 8542 | { |
| 8543 | is_default_public_exponent = 1; |
| 8544 | e_read_size = 0; |
| 8545 | } |
| 8546 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 8547 | ASSERT_ALLOC( exported, exported_size ); |
| 8548 | |
| 8549 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8550 | |
| 8551 | psa_set_key_usage_flags( &attributes, usage ); |
| 8552 | psa_set_key_algorithm( &attributes, alg ); |
| 8553 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 8554 | e_arg->x, e_arg->len ) ); |
| 8555 | psa_set_key_bits( &attributes, bits ); |
| 8556 | |
| 8557 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8558 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8559 | if( expected_status != PSA_SUCCESS ) |
| 8560 | goto exit; |
| 8561 | |
| 8562 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8563 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8564 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8565 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 8566 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 8567 | e_read_buffer, e_read_size, |
| 8568 | &e_read_length ) ); |
| 8569 | if( is_default_public_exponent ) |
| 8570 | TEST_EQUAL( e_read_length, 0 ); |
| 8571 | else |
| 8572 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 8573 | |
| 8574 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8575 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8576 | goto exit; |
| 8577 | |
| 8578 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8579 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8580 | exported, exported_size, |
| 8581 | &exported_length ) ); |
| 8582 | { |
| 8583 | uint8_t *p = exported; |
| 8584 | uint8_t *end = exported + exported_length; |
| 8585 | size_t len; |
| 8586 | /* RSAPublicKey ::= SEQUENCE { |
| 8587 | * modulus INTEGER, -- n |
| 8588 | * publicExponent INTEGER } -- e |
| 8589 | */ |
| 8590 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8591 | MBEDTLS_ASN1_SEQUENCE | |
| 8592 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 8593 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8594 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 8595 | MBEDTLS_ASN1_INTEGER ) ); |
| 8596 | if( len >= 1 && p[0] == 0 ) |
| 8597 | { |
| 8598 | ++p; |
| 8599 | --len; |
| 8600 | } |
| 8601 | if( e_arg->len == 0 ) |
| 8602 | { |
| 8603 | TEST_EQUAL( len, 3 ); |
| 8604 | TEST_EQUAL( p[0], 1 ); |
| 8605 | TEST_EQUAL( p[1], 0 ); |
| 8606 | TEST_EQUAL( p[2], 1 ); |
| 8607 | } |
| 8608 | else |
| 8609 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 8610 | } |
| 8611 | |
| 8612 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8613 | /* |
| 8614 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 8615 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 8616 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8617 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8618 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8619 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8620 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8621 | mbedtls_free( e_read_buffer ); |
| 8622 | mbedtls_free( exported ); |
| 8623 | } |
| 8624 | /* END_CASE */ |
| 8625 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8626 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8627 | void persistent_key_load_key_from_storage( data_t *data, |
| 8628 | int type_arg, int bits_arg, |
| 8629 | int usage_flags_arg, int alg_arg, |
| 8630 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8631 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 8632 | 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] | 8633 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8634 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8635 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8636 | psa_key_type_t type = type_arg; |
| 8637 | size_t bits = bits_arg; |
| 8638 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 8639 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8640 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8641 | unsigned char *first_export = NULL; |
| 8642 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8643 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8644 | size_t first_exported_length; |
| 8645 | size_t second_exported_length; |
| 8646 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8647 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8648 | { |
| 8649 | ASSERT_ALLOC( first_export, export_size ); |
| 8650 | ASSERT_ALLOC( second_export, export_size ); |
| 8651 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8652 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8653 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8654 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 8655 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8656 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 8657 | psa_set_key_algorithm( &attributes, alg ); |
| 8658 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8659 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8660 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8661 | switch( generation_method ) |
| 8662 | { |
| 8663 | case IMPORT_KEY: |
| 8664 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8665 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8666 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8667 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8668 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8669 | case GENERATE_KEY: |
| 8670 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8671 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8672 | break; |
| 8673 | |
| 8674 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 8675 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8676 | { |
| 8677 | /* Create base key */ |
| 8678 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 8679 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8680 | psa_set_key_usage_flags( &base_attributes, |
| 8681 | PSA_KEY_USAGE_DERIVE ); |
| 8682 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 8683 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8684 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 8685 | data->x, data->len, |
| 8686 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8687 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8688 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8689 | PSA_ASSERT( psa_key_derivation_input_key( |
| 8690 | &operation, |
| 8691 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8692 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8693 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8694 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8695 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 8696 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8697 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8698 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8699 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8700 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8701 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 8702 | #else |
| 8703 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 8704 | #endif |
| 8705 | break; |
| 8706 | |
| 8707 | default: |
| 8708 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 8709 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8710 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8711 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8712 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8713 | /* Export the key if permitted by the key policy. */ |
| 8714 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8715 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8716 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8717 | first_export, export_size, |
| 8718 | &first_exported_length ) ); |
| 8719 | if( generation_method == IMPORT_KEY ) |
| 8720 | ASSERT_COMPARE( data->x, data->len, |
| 8721 | first_export, first_exported_length ); |
| 8722 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8723 | |
| 8724 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8725 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8726 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8727 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8728 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8729 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8730 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 8731 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 8732 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8733 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 8734 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 8735 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8736 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 8737 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 8738 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8739 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8740 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8741 | /* Export the key again if permitted by the key policy. */ |
| 8742 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8743 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8744 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8745 | second_export, export_size, |
| 8746 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8747 | ASSERT_COMPARE( first_export, first_exported_length, |
| 8748 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8749 | } |
| 8750 | |
| 8751 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8752 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8753 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8754 | |
| 8755 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8756 | /* |
| 8757 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8758 | * thus reset them as required. |
| 8759 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8760 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8761 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8762 | mbedtls_free( first_export ); |
| 8763 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8764 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8765 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8766 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8767 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8768 | } |
| 8769 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8770 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8771 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8772 | void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 8773 | int primitive_arg, int hash_arg, int role_arg, |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8774 | int test_input, data_t *pw_data, |
| 8775 | int inj_err_type_arg, |
| 8776 | int expected_error_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8777 | { |
| 8778 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8779 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 8780 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8781 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8782 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 8783 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8784 | psa_algorithm_t hash_alg = hash_arg; |
| 8785 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8786 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8787 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8788 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
| 8789 | psa_status_t expected_error = expected_error_arg; |
| 8790 | psa_status_t status; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8791 | unsigned char *output_buffer = NULL; |
| 8792 | size_t output_len = 0; |
| 8793 | |
| 8794 | PSA_INIT( ); |
| 8795 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8796 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
| 8797 | PSA_PAKE_STEP_KEY_SHARE); |
| 8798 | ASSERT_ALLOC( output_buffer, buf_size ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8799 | |
| 8800 | if( pw_data->len > 0 ) |
| 8801 | { |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8802 | psa_set_key_usage_flags( &attributes, key_usage_pw ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8803 | psa_set_key_algorithm( &attributes, alg ); |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8804 | psa_set_key_type( &attributes, key_type_pw ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8805 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8806 | &key ) ); |
| 8807 | } |
| 8808 | |
| 8809 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8810 | psa_pake_cs_set_primitive( &cipher_suite, primitive ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8811 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8812 | |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8813 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8814 | |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8815 | if ( inj_err_type == INJECT_ERR_UNINITIALIZED_ACCESS ) |
| 8816 | { |
| 8817 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8818 | expected_error ); |
| 8819 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8820 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8821 | expected_error ); |
| 8822 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8823 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8824 | expected_error ); |
| 8825 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8826 | TEST_EQUAL( psa_pake_set_role( &operation, role ), |
| 8827 | expected_error ); |
| 8828 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8829 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8830 | NULL, 0, NULL ), |
| 8831 | expected_error ); |
| 8832 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8833 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
| 8834 | expected_error ); |
| 8835 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8836 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8837 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8838 | |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8839 | status = psa_pake_setup( &operation, &cipher_suite ); |
| 8840 | if (status != PSA_SUCCESS) |
| 8841 | { |
| 8842 | TEST_EQUAL( status, expected_error ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8843 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8844 | } |
| 8845 | |
| 8846 | if( inj_err_type == INJECT_ERR_DUPLICATE_SETUP ) |
| 8847 | { |
| 8848 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8849 | expected_error ); |
| 8850 | goto exit; |
| 8851 | } |
| 8852 | |
| 8853 | status = psa_pake_set_role( &operation, role); |
| 8854 | if ( status != PSA_SUCCESS ) |
| 8855 | { |
| 8856 | TEST_EQUAL( status, expected_error ); |
| 8857 | goto exit; |
| 8858 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8859 | |
| 8860 | if( pw_data->len > 0 ) |
| 8861 | { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8862 | status = psa_pake_set_password_key( &operation, key ); |
| 8863 | if ( status != PSA_SUCCESS ) |
| 8864 | { |
| 8865 | TEST_EQUAL( status, expected_error ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8866 | goto exit; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8867 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8868 | } |
| 8869 | |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8870 | if ( inj_err_type == INJECT_ERR_INVALID_USER ) |
| 8871 | { |
| 8872 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8873 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8874 | goto exit; |
| 8875 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 8876 | |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8877 | if ( inj_err_type == INJECT_ERR_INVALID_PEER ) |
| 8878 | { |
| 8879 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8880 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8881 | goto exit; |
| 8882 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 8883 | |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8884 | if ( inj_err_type == INJECT_ERR_SET_USER ) |
| 8885 | { |
| 8886 | const uint8_t unsupported_id[] = "abcd"; |
| 8887 | TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ), |
| 8888 | PSA_ERROR_NOT_SUPPORTED ); |
| 8889 | goto exit; |
| 8890 | } |
| 8891 | |
| 8892 | if ( inj_err_type == INJECT_ERR_SET_PEER ) |
| 8893 | { |
| 8894 | const uint8_t unsupported_id[] = "abcd"; |
| 8895 | TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ), |
| 8896 | PSA_ERROR_NOT_SUPPORTED ); |
| 8897 | goto exit; |
| 8898 | } |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 8899 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8900 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8901 | PSA_PAKE_STEP_KEY_SHARE ); |
| 8902 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8903 | PSA_PAKE_STEP_ZK_PUBLIC ); |
| 8904 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8905 | PSA_PAKE_STEP_ZK_PROOF ); |
| 8906 | |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8907 | if ( test_input ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8908 | { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8909 | if ( inj_err_type == INJECT_EMPTY_IO_BUFFER ) |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8910 | { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8911 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, NULL, 0 ), |
| 8912 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8913 | goto exit; |
| 8914 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8915 | |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8916 | if ( inj_err_type == INJECT_UNKNOWN_STEP ) |
| 8917 | { |
| 8918 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 8919 | output_buffer, size_zk_proof ), |
| 8920 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8921 | goto exit; |
| 8922 | } |
| 8923 | |
| 8924 | if ( inj_err_type == INJECT_INVALID_FIRST_STEP ) |
| 8925 | { |
| 8926 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8927 | output_buffer, size_zk_proof ), |
| 8928 | PSA_ERROR_BAD_STATE ); |
| 8929 | goto exit; |
| 8930 | } |
| 8931 | |
| 8932 | status = psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8933 | output_buffer, size_key_share ); |
| 8934 | if ( status != PSA_SUCCESS ) |
| 8935 | { |
| 8936 | TEST_EQUAL( status, expected_error); |
| 8937 | goto exit; |
| 8938 | } |
| 8939 | |
| 8940 | if ( inj_err_type == INJECT_WRONG_BUFFER_SIZE ) |
| 8941 | { |
| 8942 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8943 | output_buffer, size_zk_public + 1 ), |
| 8944 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8945 | goto exit; |
| 8946 | } |
| 8947 | |
| 8948 | if ( inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE ) |
| 8949 | { |
| 8950 | // Just trigger any kind of error. We don't care about the result here |
| 8951 | psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 8952 | output_buffer, size_zk_public + 1 ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8953 | 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] | 8954 | output_buffer, size_zk_public ), |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8955 | PSA_ERROR_BAD_STATE ); |
| 8956 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8957 | } |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8958 | } else { |
| 8959 | if ( inj_err_type == INJECT_EMPTY_IO_BUFFER ) |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8960 | { |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8961 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8962 | NULL, 0, NULL ), |
| 8963 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8964 | goto exit; |
| 8965 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8966 | |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8967 | if ( inj_err_type == INJECT_UNKNOWN_STEP ) |
| 8968 | { |
| 8969 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
| 8970 | output_buffer, buf_size, &output_len ), |
| 8971 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8972 | goto exit; |
| 8973 | } |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8974 | |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8975 | if ( inj_err_type == INJECT_INVALID_FIRST_STEP ) |
| 8976 | { |
| 8977 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
| 8978 | output_buffer, buf_size, &output_len ), |
| 8979 | PSA_ERROR_BAD_STATE ); |
| 8980 | goto exit; |
| 8981 | } |
| 8982 | |
| 8983 | status = psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8984 | output_buffer, buf_size, &output_len ); |
| 8985 | if ( status != PSA_SUCCESS ) |
| 8986 | { |
| 8987 | TEST_EQUAL( status, expected_error); |
| 8988 | goto exit; |
| 8989 | } |
| 8990 | |
| 8991 | TEST_ASSERT( output_len > 0 ); |
| 8992 | |
| 8993 | if ( inj_err_type == INJECT_WRONG_BUFFER_SIZE ) |
| 8994 | { |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8995 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 8996 | output_buffer, size_zk_public - 1, &output_len ), |
| 8997 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8998 | goto exit; |
| 8999 | } |
| 9000 | |
| 9001 | if ( inj_err_type == INJECT_VALID_OPERATION_AFTER_FAILURE ) |
| 9002 | { |
| 9003 | // Just trigger any kind of error. We don't care about the result here |
| 9004 | psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9005 | output_buffer, size_zk_public - 1, &output_len ); |
| 9006 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
| 9007 | output_buffer, buf_size, &output_len ), |
| 9008 | PSA_ERROR_BAD_STATE ); |
| 9009 | goto exit; |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 9010 | } |
| 9011 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9012 | |
| 9013 | exit: |
| 9014 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 9015 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 9016 | mbedtls_free( output_buffer ); |
| 9017 | PSA_DONE( ); |
| 9018 | } |
| 9019 | /* END_CASE */ |
| 9020 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 9021 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9022 | void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg, |
| 9023 | int client_input_first, int inject_error, |
| 9024 | data_t *pw_data ) |
| 9025 | { |
| 9026 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9027 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 9028 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 9029 | psa_algorithm_t alg = alg_arg; |
| 9030 | psa_algorithm_t hash_alg = hash_arg; |
| 9031 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9032 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9033 | |
| 9034 | PSA_INIT( ); |
| 9035 | |
| 9036 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 9037 | psa_set_key_algorithm( &attributes, alg ); |
| 9038 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 9039 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 9040 | &key ) ); |
| 9041 | |
| 9042 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 9043 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 9044 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 9045 | |
| 9046 | |
| 9047 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 9048 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 9049 | |
| 9050 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 9051 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 9052 | |
| 9053 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 9054 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 9055 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 9056 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 9057 | client_input_first, 1, inject_error ); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9058 | |
| 9059 | if( inject_error == 1 || inject_error == 2 ) |
| 9060 | goto exit; |
| 9061 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 9062 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 9063 | client_input_first, 2, inject_error ); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 9064 | |
| 9065 | exit: |
| 9066 | psa_destroy_key( key ); |
| 9067 | psa_pake_abort( &server ); |
| 9068 | psa_pake_abort( &client ); |
| 9069 | PSA_DONE( ); |
| 9070 | } |
| 9071 | /* END_CASE */ |
| 9072 | |
| 9073 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9074 | void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg, |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9075 | int derive_alg_arg, data_t *pw_data, |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 9076 | int client_input_first, int destroy_key, |
| 9077 | int client_input_first, int inj_err_type_arg ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9078 | { |
| 9079 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9080 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 9081 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 9082 | psa_algorithm_t alg = alg_arg; |
| 9083 | psa_algorithm_t hash_alg = hash_arg; |
| 9084 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 9085 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9086 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9087 | psa_key_derivation_operation_t server_derive = |
| 9088 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9089 | psa_key_derivation_operation_t client_derive = |
| 9090 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 9091 | ecjpake_injected_failure_t inj_err_type = inj_err_type_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9092 | |
| 9093 | PSA_INIT( ); |
| 9094 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9095 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 9096 | psa_set_key_algorithm( &attributes, alg ); |
| 9097 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 9098 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 9099 | &key ) ); |
| 9100 | |
| 9101 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 9102 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 9103 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 9104 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9105 | /* Get shared key */ |
| 9106 | PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) ); |
| 9107 | PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) ); |
| 9108 | |
| 9109 | if( PSA_ALG_IS_TLS12_PRF( derive_alg ) || |
| 9110 | PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) ) |
| 9111 | { |
| 9112 | PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive, |
| 9113 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9114 | (const uint8_t*) "", 0) ); |
| 9115 | PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive, |
| 9116 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9117 | (const uint8_t*) "", 0) ); |
| 9118 | } |
| 9119 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9120 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 9121 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 9122 | |
| 9123 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 9124 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 9125 | |
| 9126 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 9127 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 9128 | |
Przemek Stekiel | cd356c3 | 2022-11-20 19:05:20 +0100 | [diff] [blame] | 9129 | if( destroy_key == 1 ) |
| 9130 | psa_destroy_key( key ); |
| 9131 | |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 9132 | if( inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_1 ) |
| 9133 | { |
| 9134 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9135 | PSA_ERROR_BAD_STATE ); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 9136 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9137 | PSA_ERROR_BAD_STATE ); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 9138 | goto exit; |
| 9139 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9140 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9141 | /* First round */ |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 9142 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 9143 | client_input_first, 1, 0 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9144 | |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 9145 | if ( inj_err_type == INJECT_ANTICIPATE_KEY_DERIVATION_2 ) |
| 9146 | { |
| 9147 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9148 | PSA_ERROR_BAD_STATE ); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 9149 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9150 | PSA_ERROR_BAD_STATE ); |
Valerio Setti | 1070aed | 2022-11-11 19:37:31 +0100 | [diff] [blame^] | 9151 | goto exit; |
| 9152 | } |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9153 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9154 | /* Second round */ |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 9155 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 9156 | client_input_first, 2, 0 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9157 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9158 | PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) ); |
| 9159 | PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) ); |
| 9160 | |
| 9161 | exit: |
| 9162 | psa_key_derivation_abort( &server_derive ); |
| 9163 | psa_key_derivation_abort( &client_derive ); |
| 9164 | psa_destroy_key( key ); |
| 9165 | psa_pake_abort( &server ); |
| 9166 | psa_pake_abort( &client ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9167 | PSA_DONE( ); |
| 9168 | } |
| 9169 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9170 | |
| 9171 | /* BEGIN_CASE */ |
| 9172 | void ecjpake_size_macros( ) |
| 9173 | { |
| 9174 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 9175 | const size_t bits = 256; |
| 9176 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
| 9177 | PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits ); |
| 9178 | const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( |
| 9179 | PSA_ECC_FAMILY_SECP_R1 ); |
| 9180 | |
| 9181 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 9182 | /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */ |
| 9183 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9184 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) ); |
| 9185 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9186 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) ); |
| 9187 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
| 9188 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9189 | PSA_BITS_TO_BYTES( bits ) ); |
| 9190 | |
| 9191 | /* Input sizes are the same as output sizes */ |
| 9192 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9193 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE) ); |
| 9194 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9195 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC) ); |
| 9196 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9197 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF) ); |
| 9198 | |
| 9199 | /* These inequalities will always hold even when other PAKEs are added */ |
| 9200 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9201 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9202 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9203 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9204 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9205 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9206 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9207 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9208 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9209 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9210 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9211 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9212 | } |
| 9213 | /* END_CASE */ |