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" |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame^] | 7 | #include "common.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 8 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 9 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() |
| 10 | * uses mbedtls_ctr_drbg internally. */ |
| 11 | #include "mbedtls/ctr_drbg.h" |
| 12 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 13 | #include "psa/crypto.h" |
Ronald Cron | 4184107 | 2020-09-17 15:28:26 +0200 | [diff] [blame] | 14 | #include "psa_crypto_slot_management.h" |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 15 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 16 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 17 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 18 | #include "test/psa_exercise_key.h" |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 19 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 20 | #include "test/drivers/test_driver.h" |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 21 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION |
| 22 | #else |
| 23 | #define TEST_DRIVER_LOCATION 0x7fffff |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 24 | #endif |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 25 | #include "mbedtls/legacy_or_psa.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 26 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 27 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 28 | #define UNUSED 0xdeadbeef |
| 29 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 30 | /* Assert that an operation is (not) active. |
| 31 | * This serves as a proxy for checking if the operation is aborted. */ |
| 32 | #define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 ) |
| 33 | #define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 34 | |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 35 | #if defined(PSA_WANT_ALG_JPAKE) |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 36 | int ecjpake_operation_setup( psa_pake_operation_t *operation, |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 37 | psa_pake_cipher_suite_t *cipher_suite, |
| 38 | psa_pake_role_t role, |
| 39 | mbedtls_svc_key_id_t key, |
| 40 | size_t key_available ) |
| 41 | { |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 42 | PSA_ASSERT( psa_pake_abort( operation ) ); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 43 | |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 44 | PSA_ASSERT( psa_pake_setup( operation, cipher_suite ) ); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 45 | |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 46 | PSA_ASSERT( psa_pake_set_role( operation, role) ); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 47 | |
| 48 | if( key_available ) |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 49 | PSA_ASSERT( psa_pake_set_password_key( operation, key ) ); |
| 50 | return 0; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 51 | exit: |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 52 | return 1; |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 53 | } |
| 54 | #endif |
| 55 | |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 56 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 57 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 58 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 59 | /** Test if a buffer contains a constant byte value. |
| 60 | * |
| 61 | * `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] | 62 | * |
| 63 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 64 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 65 | * \param size Size of the buffer in bytes. |
| 66 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 67 | * \return 1 if the buffer is all-bits-zero. |
| 68 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 69 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 70 | 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] | 71 | { |
| 72 | size_t i; |
| 73 | for( i = 0; i < size; i++ ) |
| 74 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 75 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 76 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 77 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 78 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 79 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 80 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 81 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 82 | static int asn1_write_10x( unsigned char **p, |
| 83 | unsigned char *start, |
| 84 | size_t bits, |
| 85 | unsigned char x ) |
| 86 | { |
| 87 | int ret; |
| 88 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 89 | if( bits == 0 ) |
| 90 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 91 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 92 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 93 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 94 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 95 | *p -= len; |
| 96 | ( *p )[len-1] = x; |
| 97 | if( bits % 8 == 0 ) |
| 98 | ( *p )[1] |= 1; |
| 99 | else |
| 100 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 101 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 102 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 103 | MBEDTLS_ASN1_INTEGER ) ); |
| 104 | return( len ); |
| 105 | } |
| 106 | |
| 107 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 108 | size_t buffer_size, |
| 109 | unsigned char **p, |
| 110 | size_t bits, |
| 111 | int keypair ) |
| 112 | { |
| 113 | size_t half_bits = ( bits + 1 ) / 2; |
| 114 | int ret; |
| 115 | int len = 0; |
| 116 | /* Construct something that looks like a DER encoding of |
| 117 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 118 | * RSAPrivateKey ::= SEQUENCE { |
| 119 | * version Version, |
| 120 | * modulus INTEGER, -- n |
| 121 | * publicExponent INTEGER, -- e |
| 122 | * privateExponent INTEGER, -- d |
| 123 | * prime1 INTEGER, -- p |
| 124 | * prime2 INTEGER, -- q |
| 125 | * exponent1 INTEGER, -- d mod (p-1) |
| 126 | * exponent2 INTEGER, -- d mod (q-1) |
| 127 | * coefficient INTEGER, -- (inverse of q) mod p |
| 128 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 129 | * } |
| 130 | * Or, for a public key, the same structure with only |
| 131 | * version, modulus and publicExponent. |
| 132 | */ |
| 133 | *p = buffer + buffer_size; |
| 134 | if( keypair ) |
| 135 | { |
| 136 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 137 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 138 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 139 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 140 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 141 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 142 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 143 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 144 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 145 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 146 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 147 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 148 | } |
| 149 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 150 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 151 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 152 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 153 | if( keypair ) |
| 154 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 155 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 156 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 157 | { |
| 158 | const unsigned char tag = |
| 159 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 160 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 161 | } |
| 162 | return( len ); |
| 163 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 164 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 165 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 166 | int exercise_mac_setup( psa_key_type_t key_type, |
| 167 | const unsigned char *key_bytes, |
| 168 | size_t key_length, |
| 169 | psa_algorithm_t alg, |
| 170 | psa_mac_operation_t *operation, |
| 171 | psa_status_t *status ) |
| 172 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 173 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 174 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 175 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 176 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 177 | psa_set_key_algorithm( &attributes, alg ); |
| 178 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 179 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 180 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 181 | *status = psa_mac_sign_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 182 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 183 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 184 | /* If setup failed, reproduce the failure, so that the caller can |
| 185 | * test the resulting state of the operation object. */ |
| 186 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 187 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 188 | TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 189 | } |
| 190 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 191 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 192 | return( 1 ); |
| 193 | |
| 194 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 195 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 196 | return( 0 ); |
| 197 | } |
| 198 | |
| 199 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 200 | const unsigned char *key_bytes, |
| 201 | size_t key_length, |
| 202 | psa_algorithm_t alg, |
| 203 | psa_cipher_operation_t *operation, |
| 204 | psa_status_t *status ) |
| 205 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 206 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 207 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 208 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 209 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 210 | psa_set_key_algorithm( &attributes, alg ); |
| 211 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 212 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 213 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 214 | *status = psa_cipher_encrypt_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 215 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 216 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 217 | /* If setup failed, reproduce the failure, so that the caller can |
| 218 | * test the resulting state of the operation object. */ |
| 219 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 220 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 221 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ), |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 222 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 223 | } |
| 224 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 225 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 226 | return( 1 ); |
| 227 | |
| 228 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 229 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 230 | return( 0 ); |
| 231 | } |
| 232 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 233 | 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] | 234 | { |
| 235 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 236 | 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] | 237 | uint8_t buffer[1]; |
| 238 | size_t length; |
| 239 | int ok = 0; |
| 240 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 241 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 242 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 243 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 244 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 245 | TEST_EQUAL( psa_get_key_attributes( key, &attributes ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 246 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 247 | TEST_EQUAL( |
| 248 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 249 | TEST_EQUAL( |
| 250 | 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] | 251 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 252 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 253 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 254 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 255 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 256 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 257 | TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 258 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 259 | TEST_EQUAL( psa_export_public_key( key, |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 260 | buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 261 | PSA_ERROR_INVALID_HANDLE ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 262 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 263 | ok = 1; |
| 264 | |
| 265 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 266 | /* |
| 267 | * Key attributes may have been returned by psa_get_key_attributes() |
| 268 | * thus reset them as required. |
| 269 | */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 270 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 271 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 272 | return( ok ); |
| 273 | } |
| 274 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 275 | /* Assert that a key isn't reported as having a slot number. */ |
| 276 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 277 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 278 | do \ |
| 279 | { \ |
| 280 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 281 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 282 | attributes, \ |
| 283 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 284 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 285 | } \ |
| 286 | while( 0 ) |
| 287 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 288 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 289 | ( (void) 0 ) |
| 290 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 291 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 292 | /* An overapproximation of the amount of storage needed for a key of the |
| 293 | * given type and with the given content. The API doesn't make it easy |
| 294 | * to find a good value for the size. The current implementation doesn't |
| 295 | * care about the value anyway. */ |
| 296 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 297 | ( data )->len |
| 298 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 299 | typedef enum { |
| 300 | IMPORT_KEY = 0, |
| 301 | GENERATE_KEY = 1, |
| 302 | DERIVE_KEY = 2 |
| 303 | } generate_method; |
| 304 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 305 | typedef enum |
| 306 | { |
| 307 | DO_NOT_SET_LENGTHS = 0, |
| 308 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 309 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 310 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 311 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 312 | typedef enum |
| 313 | { |
| 314 | USE_NULL_TAG = 0, |
| 315 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 316 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 317 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 318 | /*! |
| 319 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 320 | * \param key_type_arg Type of key passed in |
| 321 | * \param key_data The encryption / decryption key data |
| 322 | * \param alg_arg The type of algorithm used |
| 323 | * \param nonce Nonce data |
| 324 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 325 | * \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] | 326 | * feed additional data in to be encrypted / |
| 327 | * decrypted. If -1, no chunking. |
| 328 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 329 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 330 | * the data in to be encrypted / decrypted. If |
| 331 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 332 | * \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] | 333 | * expected here, this controls whether or not |
| 334 | * to set lengths, and in what order with |
| 335 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 336 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 337 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 338 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 339 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 340 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 341 | */ |
| 342 | static int aead_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 343 | int alg_arg, |
| 344 | data_t *nonce, |
| 345 | data_t *additional_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 346 | int ad_part_len_arg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 347 | data_t *input_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 348 | int data_part_len_arg, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 349 | set_lengths_method_t set_lengths_method, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 350 | data_t *expected_output, |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 351 | int is_encrypt, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 352 | int do_zero_parts ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 353 | { |
| 354 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 355 | psa_key_type_t key_type = key_type_arg; |
| 356 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 357 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 358 | unsigned char *output_data = NULL; |
| 359 | unsigned char *part_data = NULL; |
| 360 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 361 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 362 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 363 | size_t output_size = 0; |
| 364 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 365 | size_t output_length = 0; |
| 366 | size_t key_bits = 0; |
| 367 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 368 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 369 | size_t part_length = 0; |
| 370 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 371 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 372 | size_t ad_part_len = 0; |
| 373 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 374 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 375 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 376 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 377 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 378 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 379 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 380 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 381 | PSA_ASSERT( psa_crypto_init( ) ); |
| 382 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 383 | if( is_encrypt ) |
| 384 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 385 | else |
| 386 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 387 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 388 | psa_set_key_algorithm( &attributes, alg ); |
| 389 | psa_set_key_type( &attributes, key_type ); |
| 390 | |
| 391 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 392 | &key ) ); |
| 393 | |
| 394 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 395 | key_bits = psa_get_key_bits( &attributes ); |
| 396 | |
| 397 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 398 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 399 | if( is_encrypt ) |
| 400 | { |
| 401 | /* Tag gets written at end of buffer. */ |
| 402 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 403 | ( input_data->len + |
| 404 | tag_length ) ); |
| 405 | data_true_size = input_data->len; |
| 406 | } |
| 407 | else |
| 408 | { |
| 409 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 410 | ( input_data->len - |
| 411 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 412 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 413 | /* Do not want to attempt to decrypt tag. */ |
| 414 | data_true_size = input_data->len - tag_length; |
| 415 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 416 | |
| 417 | ASSERT_ALLOC( output_data, output_size ); |
| 418 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 419 | if( is_encrypt ) |
| 420 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 421 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 422 | TEST_LE_U( final_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 423 | } |
| 424 | else |
| 425 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 426 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 427 | TEST_LE_U( final_output_size, PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 428 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 429 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 430 | ASSERT_ALLOC( final_data, final_output_size ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 431 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 432 | if( is_encrypt ) |
| 433 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 434 | else |
| 435 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 436 | |
| 437 | /* If the operation is not supported, just skip and not fail in case the |
| 438 | * encryption involves a common limitation of cryptography hardwares and |
| 439 | * an alternative implementation. */ |
| 440 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 441 | { |
| 442 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 443 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 444 | } |
| 445 | |
| 446 | PSA_ASSERT( status ); |
| 447 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 448 | if( set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 449 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 450 | else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 451 | { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 452 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 453 | data_true_size ) ); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 454 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 455 | } |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 456 | else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 457 | { |
| 458 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 459 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 460 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 461 | data_true_size ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 462 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 463 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 464 | if( ad_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 465 | { |
| 466 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 467 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 468 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 469 | for( part_offset = 0, part_count = 0; |
| 470 | part_offset < additional_data->len; |
| 471 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 472 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 473 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 474 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 475 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 476 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 477 | else if( additional_data->len - part_offset < ad_part_len ) |
| 478 | { |
| 479 | part_length = additional_data->len - part_offset; |
| 480 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 481 | else |
| 482 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 483 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | PSA_ASSERT( psa_aead_update_ad( &operation, |
| 487 | additional_data->x + part_offset, |
| 488 | part_length ) ); |
| 489 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | else |
| 493 | { |
| 494 | /* Pass additional data in one go. */ |
| 495 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 496 | additional_data->len ) ); |
| 497 | } |
| 498 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 499 | if( data_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 500 | { |
| 501 | /* Pass data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 502 | data_part_len = ( size_t ) data_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 503 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 504 | ( size_t ) data_part_len ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 505 | |
| 506 | ASSERT_ALLOC( part_data, part_data_size ); |
| 507 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 508 | for( part_offset = 0, part_count = 0; |
| 509 | part_offset < data_true_size; |
| 510 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 511 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 512 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 513 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 514 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 515 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 516 | else if( ( data_true_size - part_offset ) < data_part_len ) |
| 517 | { |
| 518 | part_length = ( data_true_size - part_offset ); |
| 519 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 520 | else |
| 521 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 522 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | PSA_ASSERT( psa_aead_update( &operation, |
| 526 | ( input_data->x + part_offset ), |
| 527 | part_length, part_data, |
| 528 | part_data_size, |
| 529 | &output_part_length ) ); |
| 530 | |
| 531 | if( output_data && output_part_length ) |
| 532 | { |
Mircea Udrea | 657ff4f | 2022-01-31 13:51:56 +0100 | [diff] [blame] | 533 | memcpy( ( output_data + output_length ), part_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 534 | output_part_length ); |
| 535 | } |
| 536 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 537 | output_length += output_part_length; |
| 538 | } |
| 539 | } |
| 540 | else |
| 541 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 542 | /* Pass all data in one go. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 543 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 544 | data_true_size, output_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 545 | output_size, &output_length ) ); |
| 546 | } |
| 547 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 548 | if( is_encrypt ) |
| 549 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 550 | final_output_size, |
| 551 | &output_part_length, |
| 552 | tag_buffer, tag_length, |
| 553 | &tag_size ) ); |
| 554 | else |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 555 | { |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 556 | PSA_ASSERT( psa_aead_verify( &operation, final_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 557 | final_output_size, |
| 558 | &output_part_length, |
| 559 | ( input_data->x + data_true_size ), |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 560 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 561 | } |
| 562 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 563 | if( output_data && output_part_length ) |
| 564 | memcpy( ( output_data + output_length ), final_data, |
| 565 | output_part_length ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 566 | |
| 567 | output_length += output_part_length; |
| 568 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 569 | |
| 570 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 571 | * should be exact.*/ |
| 572 | if( is_encrypt ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 573 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 574 | TEST_EQUAL( tag_length, tag_size ); |
| 575 | |
| 576 | if( output_data && tag_length ) |
| 577 | memcpy( ( output_data + output_length ), tag_buffer, |
| 578 | tag_length ); |
| 579 | |
| 580 | output_length += tag_length; |
| 581 | |
| 582 | TEST_EQUAL( output_length, |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 583 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 584 | input_data->len ) ); |
| 585 | TEST_LE_U( output_length, |
| 586 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 587 | } |
| 588 | else |
| 589 | { |
| 590 | TEST_EQUAL( output_length, |
| 591 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, |
| 592 | input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 593 | TEST_LE_U( output_length, |
| 594 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 597 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 598 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 599 | output_data, output_length ); |
| 600 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 601 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 602 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 603 | |
| 604 | exit: |
| 605 | psa_destroy_key( key ); |
| 606 | psa_aead_abort( &operation ); |
| 607 | mbedtls_free( output_data ); |
| 608 | mbedtls_free( part_data ); |
| 609 | mbedtls_free( final_data ); |
| 610 | PSA_DONE( ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 611 | |
| 612 | return( test_ok ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 613 | } |
| 614 | |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 615 | /*! |
| 616 | * \brief Internal Function for MAC multipart tests. |
| 617 | * \param key_type_arg Type of key passed in |
| 618 | * \param key_data The encryption / decryption key data |
| 619 | * \param alg_arg The type of algorithm used |
| 620 | * \param input_data Data to encrypt / decrypt |
| 621 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 622 | * the data in to be encrypted / decrypted. If |
| 623 | * -1, no chunking |
| 624 | * \param expected_output Expected output |
| 625 | * \param is_verify If non-zero this is an verify operation. |
| 626 | * \param do_zero_parts If non-zero, interleave zero length chunks |
| 627 | * with normal length chunks. |
| 628 | * \return int Zero on failure, non-zero on success. |
| 629 | */ |
| 630 | static int mac_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 631 | int alg_arg, |
| 632 | data_t *input_data, |
| 633 | int data_part_len_arg, |
| 634 | data_t *expected_output, |
| 635 | int is_verify, |
| 636 | int do_zero_parts ) |
| 637 | { |
| 638 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 639 | psa_key_type_t key_type = key_type_arg; |
| 640 | psa_algorithm_t alg = alg_arg; |
| 641 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 642 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
| 643 | size_t part_offset = 0; |
| 644 | size_t part_length = 0; |
| 645 | size_t data_part_len = 0; |
| 646 | size_t mac_len = 0; |
| 647 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 648 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 649 | |
| 650 | int test_ok = 0; |
| 651 | size_t part_count = 0; |
| 652 | |
Neil Armstrong | fd4c259 | 2022-03-07 10:11:11 +0100 | [diff] [blame] | 653 | PSA_INIT( ); |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 654 | |
| 655 | if( is_verify ) |
| 656 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
| 657 | else |
| 658 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
| 659 | |
| 660 | psa_set_key_algorithm( &attributes, alg ); |
| 661 | psa_set_key_type( &attributes, key_type ); |
| 662 | |
| 663 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 664 | &key ) ); |
| 665 | |
| 666 | if( is_verify ) |
| 667 | status = psa_mac_verify_setup( &operation, key, alg ); |
| 668 | else |
| 669 | status = psa_mac_sign_setup( &operation, key, alg ); |
| 670 | |
| 671 | PSA_ASSERT( status ); |
| 672 | |
| 673 | if( data_part_len_arg != -1 ) |
| 674 | { |
| 675 | /* Pass data in parts */ |
| 676 | data_part_len = ( size_t ) data_part_len_arg; |
| 677 | |
| 678 | for( part_offset = 0, part_count = 0; |
| 679 | part_offset < input_data->len; |
| 680 | part_offset += part_length, part_count++ ) |
| 681 | { |
| 682 | if( do_zero_parts && ( part_count & 0x01 ) ) |
| 683 | { |
| 684 | part_length = 0; |
| 685 | } |
| 686 | else if( ( input_data->len - part_offset ) < data_part_len ) |
| 687 | { |
| 688 | part_length = ( input_data->len - part_offset ); |
| 689 | } |
| 690 | else |
| 691 | { |
| 692 | part_length = data_part_len; |
| 693 | } |
| 694 | |
| 695 | PSA_ASSERT( psa_mac_update( &operation, |
| 696 | ( input_data->x + part_offset ), |
| 697 | part_length ) ); |
| 698 | } |
| 699 | } |
| 700 | else |
| 701 | { |
| 702 | /* Pass all data in one go. */ |
| 703 | PSA_ASSERT( psa_mac_update( &operation, input_data->x, |
| 704 | input_data->len ) ); |
| 705 | } |
| 706 | |
| 707 | if( is_verify ) |
| 708 | { |
| 709 | PSA_ASSERT( psa_mac_verify_finish( &operation, expected_output->x, |
| 710 | expected_output->len ) ); |
| 711 | } |
| 712 | else |
| 713 | { |
| 714 | PSA_ASSERT( psa_mac_sign_finish( &operation, mac, |
| 715 | PSA_MAC_MAX_SIZE, &mac_len ) ); |
| 716 | |
| 717 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 718 | mac, mac_len ); |
| 719 | } |
| 720 | |
| 721 | test_ok = 1; |
| 722 | |
| 723 | exit: |
| 724 | psa_destroy_key( key ); |
| 725 | psa_mac_abort( &operation ); |
| 726 | PSA_DONE( ); |
| 727 | |
| 728 | return( test_ok ); |
| 729 | } |
| 730 | |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 731 | #if defined(PSA_WANT_ALG_JPAKE) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 732 | static void ecjpake_do_round( psa_algorithm_t alg, unsigned int primitive, |
| 733 | psa_pake_operation_t *server, |
| 734 | psa_pake_operation_t *client, |
| 735 | int client_input_first, |
| 736 | int round, int inject_error ) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 737 | { |
| 738 | unsigned char *buffer0 = NULL, *buffer1 = NULL; |
| 739 | size_t buffer_length = ( |
| 740 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE) + |
| 741 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC) + |
| 742 | 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] | 743 | /* The output should be exactly this size according to the spec */ |
| 744 | const size_t expected_size_key_share = |
| 745 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_KEY_SHARE); |
| 746 | /* The output should be exactly this size according to the spec */ |
| 747 | const size_t expected_size_zk_public = |
| 748 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PUBLIC); |
| 749 | /* The output can be smaller: the spec allows stripping leading zeroes */ |
| 750 | const size_t max_expected_size_zk_proof = |
| 751 | PSA_PAKE_OUTPUT_SIZE(alg, primitive, PSA_PAKE_STEP_ZK_PROOF); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 752 | size_t buffer0_off = 0; |
| 753 | size_t buffer1_off = 0; |
| 754 | size_t s_g1_len, s_g2_len, s_a_len; |
| 755 | size_t s_g1_off, s_g2_off, s_a_off; |
| 756 | size_t s_x1_pk_len, s_x2_pk_len, s_x2s_pk_len; |
| 757 | size_t s_x1_pk_off, s_x2_pk_off, s_x2s_pk_off; |
| 758 | size_t s_x1_pr_len, s_x2_pr_len, s_x2s_pr_len; |
| 759 | size_t s_x1_pr_off, s_x2_pr_off, s_x2s_pr_off; |
| 760 | size_t c_g1_len, c_g2_len, c_a_len; |
| 761 | size_t c_g1_off, c_g2_off, c_a_off; |
| 762 | size_t c_x1_pk_len, c_x2_pk_len, c_x2s_pk_len; |
| 763 | size_t c_x1_pk_off, c_x2_pk_off, c_x2s_pk_off; |
| 764 | size_t c_x1_pr_len, c_x2_pr_len, c_x2s_pr_len; |
| 765 | size_t c_x1_pr_off, c_x2_pr_off, c_x2s_pr_off; |
| 766 | psa_status_t expected_status = PSA_SUCCESS; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 767 | psa_status_t status; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 768 | |
| 769 | ASSERT_ALLOC( buffer0, buffer_length ); |
| 770 | ASSERT_ALLOC( buffer1, buffer_length ); |
| 771 | |
| 772 | switch( round ) |
| 773 | { |
| 774 | case 1: |
| 775 | /* Server first round Output */ |
| 776 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 777 | buffer0 + buffer0_off, |
| 778 | 512 - buffer0_off, &s_g1_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 779 | TEST_EQUAL( s_g1_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 780 | s_g1_off = buffer0_off; |
| 781 | buffer0_off += s_g1_len; |
| 782 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 783 | buffer0 + buffer0_off, |
| 784 | 512 - buffer0_off, &s_x1_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 785 | TEST_EQUAL( s_x1_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 786 | s_x1_pk_off = buffer0_off; |
| 787 | buffer0_off += s_x1_pk_len; |
| 788 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 789 | buffer0 + buffer0_off, |
| 790 | 512 - buffer0_off, &s_x1_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 791 | TEST_LE_U( s_x1_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 792 | s_x1_pr_off = buffer0_off; |
| 793 | buffer0_off += s_x1_pr_len; |
| 794 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 795 | buffer0 + buffer0_off, |
| 796 | 512 - buffer0_off, &s_g2_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 797 | TEST_EQUAL( s_g2_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 798 | s_g2_off = buffer0_off; |
| 799 | buffer0_off += s_g2_len; |
| 800 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 801 | buffer0 + buffer0_off, |
| 802 | 512 - buffer0_off, &s_x2_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 803 | TEST_EQUAL( s_x2_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 804 | s_x2_pk_off = buffer0_off; |
| 805 | buffer0_off += s_x2_pk_len; |
| 806 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 807 | buffer0 + buffer0_off, |
| 808 | 512 - buffer0_off, &s_x2_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 809 | TEST_LE_U( s_x2_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 810 | s_x2_pr_off = buffer0_off; |
| 811 | buffer0_off += s_x2_pr_len; |
| 812 | |
| 813 | if( inject_error == 1 ) |
| 814 | { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 815 | buffer0[s_x1_pr_off + 8] ^= 1; |
| 816 | buffer0[s_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 817 | expected_status = PSA_ERROR_DATA_INVALID; |
| 818 | } |
| 819 | |
Neil Armstrong | 51009d7 | 2022-09-05 17:59:54 +0200 | [diff] [blame] | 820 | /* |
| 821 | * When injecting errors in inputs, the implementation is |
| 822 | * free to detect it right away of with a delay. |
| 823 | * This permits delaying the error until the end of the input |
| 824 | * sequence, if no error appears then, this will be treated |
| 825 | * as an error. |
| 826 | */ |
| 827 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 828 | if( client_input_first == 1 ) |
| 829 | { |
| 830 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 831 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 832 | buffer0 + s_g1_off, s_g1_len ); |
| 833 | if( inject_error == 1 && status != PSA_SUCCESS ) |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 834 | { |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 835 | TEST_EQUAL( status, expected_status ); |
| 836 | break; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 837 | } |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 838 | else |
| 839 | { |
| 840 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 841 | } |
| 842 | |
| 843 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 844 | buffer0 + s_x1_pk_off, |
| 845 | s_x1_pk_len ); |
| 846 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 847 | { |
| 848 | TEST_EQUAL( status, expected_status ); |
| 849 | break; |
| 850 | } |
| 851 | else |
| 852 | { |
| 853 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 854 | } |
| 855 | |
| 856 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 857 | buffer0 + s_x1_pr_off, |
| 858 | s_x1_pr_len ); |
| 859 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 860 | { |
| 861 | TEST_EQUAL( status, expected_status ); |
| 862 | break; |
| 863 | } |
| 864 | else |
| 865 | { |
| 866 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 867 | } |
| 868 | |
| 869 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 870 | buffer0 + s_g2_off, |
| 871 | s_g2_len ); |
| 872 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 873 | { |
| 874 | TEST_EQUAL( status, expected_status ); |
| 875 | break; |
| 876 | } |
| 877 | else |
| 878 | { |
| 879 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 880 | } |
| 881 | |
| 882 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 883 | buffer0 + s_x2_pk_off, |
| 884 | s_x2_pk_len ); |
| 885 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 886 | { |
| 887 | TEST_EQUAL( status, expected_status ); |
| 888 | break; |
| 889 | } |
| 890 | else |
| 891 | { |
| 892 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 893 | } |
| 894 | |
| 895 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 896 | buffer0 + s_x2_pr_off, |
| 897 | s_x2_pr_len ); |
| 898 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 899 | { |
| 900 | TEST_EQUAL( status, expected_status ); |
| 901 | break; |
| 902 | } |
| 903 | else |
| 904 | { |
| 905 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 906 | } |
| 907 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 908 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 909 | if( inject_error == 1 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 910 | 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] | 911 | } |
| 912 | |
| 913 | /* Client first round Output */ |
| 914 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 915 | buffer1 + buffer1_off, |
| 916 | 512 - buffer1_off, &c_g1_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 917 | TEST_EQUAL( c_g1_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 918 | c_g1_off = buffer1_off; |
| 919 | buffer1_off += c_g1_len; |
| 920 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 921 | buffer1 + buffer1_off, |
| 922 | 512 - buffer1_off, &c_x1_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 923 | TEST_EQUAL( c_x1_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 924 | c_x1_pk_off = buffer1_off; |
| 925 | buffer1_off += c_x1_pk_len; |
| 926 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 927 | buffer1 + buffer1_off, |
| 928 | 512 - buffer1_off, &c_x1_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 929 | TEST_LE_U( c_x1_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 930 | c_x1_pr_off = buffer1_off; |
| 931 | buffer1_off += c_x1_pr_len; |
| 932 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 933 | buffer1 + buffer1_off, |
| 934 | 512 - buffer1_off, &c_g2_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 935 | TEST_EQUAL( c_g2_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 936 | c_g2_off = buffer1_off; |
| 937 | buffer1_off += c_g2_len; |
| 938 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 939 | buffer1 + buffer1_off, |
| 940 | 512 - buffer1_off, &c_x2_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 941 | TEST_EQUAL( c_x2_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 942 | c_x2_pk_off = buffer1_off; |
| 943 | buffer1_off += c_x2_pk_len; |
| 944 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 945 | buffer1 + buffer1_off, |
| 946 | 512 - buffer1_off, &c_x2_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 947 | TEST_LE_U( c_x2_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 948 | c_x2_pr_off = buffer1_off; |
| 949 | buffer1_off += c_x2_pr_len; |
| 950 | |
| 951 | if( client_input_first == 0 ) |
| 952 | { |
| 953 | /* Client first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 954 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 955 | buffer0 + s_g1_off, s_g1_len ); |
| 956 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 957 | { |
| 958 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 959 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 960 | } |
| 961 | else |
| 962 | { |
| 963 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 964 | } |
| 965 | |
| 966 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 967 | buffer0 + s_x1_pk_off, |
| 968 | s_x1_pk_len ); |
| 969 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 970 | { |
| 971 | TEST_EQUAL( status, expected_status ); |
| 972 | break; |
| 973 | } |
| 974 | else |
| 975 | { |
| 976 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 977 | } |
| 978 | |
| 979 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 980 | buffer0 + s_x1_pr_off, |
| 981 | s_x1_pr_len ); |
| 982 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 983 | { |
| 984 | TEST_EQUAL( status, expected_status ); |
| 985 | break; |
| 986 | } |
| 987 | else |
| 988 | { |
| 989 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 990 | } |
| 991 | |
| 992 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 993 | buffer0 + s_g2_off, |
| 994 | s_g2_len ); |
| 995 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 996 | { |
| 997 | TEST_EQUAL( status, expected_status ); |
| 998 | break; |
| 999 | } |
| 1000 | else |
| 1001 | { |
| 1002 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1003 | } |
| 1004 | |
| 1005 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1006 | buffer0 + s_x2_pk_off, |
| 1007 | s_x2_pk_len ); |
| 1008 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 1009 | { |
| 1010 | TEST_EQUAL( status, expected_status ); |
| 1011 | break; |
| 1012 | } |
| 1013 | else |
| 1014 | { |
| 1015 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1016 | } |
| 1017 | |
| 1018 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1019 | buffer0 + s_x2_pr_off, |
| 1020 | s_x2_pr_len ); |
| 1021 | if( inject_error == 1 && status != PSA_SUCCESS ) |
| 1022 | { |
| 1023 | TEST_EQUAL( status, expected_status ); |
| 1024 | break; |
| 1025 | } |
| 1026 | else |
| 1027 | { |
| 1028 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1029 | } |
| 1030 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1031 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1032 | if( inject_error == 1 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1033 | 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] | 1034 | } |
| 1035 | |
| 1036 | if( inject_error == 2 ) |
| 1037 | { |
Andrzej Kurek | c018204 | 2022-11-08 08:12:56 -0500 | [diff] [blame] | 1038 | buffer1[c_x1_pr_off + 12] ^= 1; |
| 1039 | buffer1[c_x2_pr_off + 7] ^= 1; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1040 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1041 | } |
| 1042 | |
| 1043 | /* Server first round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1044 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1045 | buffer1 + c_g1_off, c_g1_len ); |
| 1046 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1047 | { |
| 1048 | TEST_EQUAL( status, expected_status ); |
| 1049 | break; |
| 1050 | } |
| 1051 | else |
| 1052 | { |
| 1053 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1054 | } |
| 1055 | |
| 1056 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1057 | buffer1 + c_x1_pk_off, c_x1_pk_len ); |
| 1058 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1059 | { |
| 1060 | TEST_EQUAL( status, expected_status ); |
| 1061 | break; |
| 1062 | } |
| 1063 | else |
| 1064 | { |
| 1065 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1066 | } |
| 1067 | |
| 1068 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1069 | buffer1 + c_x1_pr_off, c_x1_pr_len ); |
| 1070 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1071 | { |
| 1072 | TEST_EQUAL( status, expected_status ); |
| 1073 | break; |
| 1074 | } |
| 1075 | else |
| 1076 | { |
| 1077 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1078 | } |
| 1079 | |
| 1080 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1081 | buffer1 + c_g2_off, c_g2_len ); |
| 1082 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1083 | { |
| 1084 | TEST_EQUAL( status, expected_status ); |
| 1085 | break; |
| 1086 | } |
| 1087 | else |
| 1088 | { |
| 1089 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1090 | } |
| 1091 | |
| 1092 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1093 | buffer1 + c_x2_pk_off, c_x2_pk_len ); |
| 1094 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1095 | { |
| 1096 | TEST_EQUAL( status, expected_status ); |
| 1097 | break; |
| 1098 | } |
| 1099 | else |
| 1100 | { |
| 1101 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1102 | } |
| 1103 | |
| 1104 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1105 | buffer1 + c_x2_pr_off, c_x2_pr_len ); |
| 1106 | if( inject_error == 2 && status != PSA_SUCCESS ) |
| 1107 | { |
| 1108 | TEST_EQUAL( status, expected_status ); |
| 1109 | break; |
| 1110 | } |
| 1111 | else |
| 1112 | { |
| 1113 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1114 | } |
| 1115 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1116 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1117 | if( inject_error == 2 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1118 | 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] | 1119 | |
| 1120 | break; |
| 1121 | |
| 1122 | case 2: |
| 1123 | /* Server second round Output */ |
| 1124 | buffer0_off = 0; |
| 1125 | |
| 1126 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1127 | buffer0 + buffer0_off, |
| 1128 | 512 - buffer0_off, &s_a_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1129 | TEST_EQUAL( s_a_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1130 | s_a_off = buffer0_off; |
| 1131 | buffer0_off += s_a_len; |
| 1132 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1133 | buffer0 + buffer0_off, |
| 1134 | 512 - buffer0_off, &s_x2s_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1135 | TEST_EQUAL( s_x2s_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1136 | s_x2s_pk_off = buffer0_off; |
| 1137 | buffer0_off += s_x2s_pk_len; |
| 1138 | PSA_ASSERT( psa_pake_output( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1139 | buffer0 + buffer0_off, |
| 1140 | 512 - buffer0_off, &s_x2s_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1141 | TEST_LE_U( s_x2s_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1142 | s_x2s_pr_off = buffer0_off; |
| 1143 | buffer0_off += s_x2s_pr_len; |
| 1144 | |
| 1145 | if( inject_error == 3 ) |
| 1146 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1147 | buffer0[s_x2s_pk_off + 12] += 0x33; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1148 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1149 | } |
| 1150 | |
| 1151 | if( client_input_first == 1 ) |
| 1152 | { |
| 1153 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1154 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1155 | buffer0 + s_a_off, s_a_len ); |
| 1156 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1157 | { |
| 1158 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1159 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1160 | } |
| 1161 | else |
| 1162 | { |
| 1163 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1164 | } |
| 1165 | |
| 1166 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1167 | buffer0 + s_x2s_pk_off, |
| 1168 | s_x2s_pk_len ); |
| 1169 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1170 | { |
| 1171 | TEST_EQUAL( status, expected_status ); |
| 1172 | break; |
| 1173 | } |
| 1174 | else |
| 1175 | { |
| 1176 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1177 | } |
| 1178 | |
| 1179 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1180 | buffer0 + s_x2s_pr_off, |
| 1181 | s_x2s_pr_len ); |
| 1182 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1183 | { |
| 1184 | TEST_EQUAL( status, expected_status ); |
| 1185 | break; |
| 1186 | } |
| 1187 | else |
| 1188 | { |
| 1189 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1190 | } |
| 1191 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1192 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1193 | if( inject_error == 3 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1194 | 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] | 1195 | } |
| 1196 | |
| 1197 | /* Client second round Output */ |
| 1198 | buffer1_off = 0; |
| 1199 | |
| 1200 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1201 | buffer1 + buffer1_off, |
| 1202 | 512 - buffer1_off, &c_a_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1203 | TEST_EQUAL( c_a_len, expected_size_key_share ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1204 | c_a_off = buffer1_off; |
| 1205 | buffer1_off += c_a_len; |
| 1206 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1207 | buffer1 + buffer1_off, |
| 1208 | 512 - buffer1_off, &c_x2s_pk_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1209 | TEST_EQUAL( c_x2s_pk_len, expected_size_zk_public ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1210 | c_x2s_pk_off = buffer1_off; |
| 1211 | buffer1_off += c_x2s_pk_len; |
| 1212 | PSA_ASSERT( psa_pake_output( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1213 | buffer1 + buffer1_off, |
| 1214 | 512 - buffer1_off, &c_x2s_pr_len ) ); |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 1215 | TEST_LE_U( c_x2s_pr_len, max_expected_size_zk_proof ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1216 | c_x2s_pr_off = buffer1_off; |
| 1217 | buffer1_off += c_x2s_pr_len; |
| 1218 | |
| 1219 | if( client_input_first == 0 ) |
| 1220 | { |
| 1221 | /* Client second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1222 | status = psa_pake_input( client, PSA_PAKE_STEP_KEY_SHARE, |
| 1223 | buffer0 + s_a_off, s_a_len ); |
| 1224 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1225 | { |
| 1226 | TEST_EQUAL( status, expected_status ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1227 | break; |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1228 | } |
| 1229 | else |
| 1230 | { |
| 1231 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1232 | } |
| 1233 | |
| 1234 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1235 | buffer0 + s_x2s_pk_off, |
| 1236 | s_x2s_pk_len ); |
| 1237 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1238 | { |
| 1239 | TEST_EQUAL( status, expected_status ); |
| 1240 | break; |
| 1241 | } |
| 1242 | else |
| 1243 | { |
| 1244 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1245 | } |
| 1246 | |
| 1247 | status = psa_pake_input( client, PSA_PAKE_STEP_ZK_PROOF, |
| 1248 | buffer0 + s_x2s_pr_off, |
| 1249 | s_x2s_pr_len ); |
| 1250 | if( inject_error == 3 && status != PSA_SUCCESS ) |
| 1251 | { |
| 1252 | TEST_EQUAL( status, expected_status ); |
| 1253 | break; |
| 1254 | } |
| 1255 | else |
| 1256 | { |
| 1257 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1258 | } |
| 1259 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1260 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1261 | if( inject_error == 3 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1262 | 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] | 1263 | } |
| 1264 | |
| 1265 | if( inject_error == 4 ) |
| 1266 | { |
Neil Armstrong | eae1dfc | 2022-06-21 13:37:06 +0200 | [diff] [blame] | 1267 | buffer1[c_x2s_pk_off + 7] += 0x28; |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1268 | expected_status = PSA_ERROR_DATA_INVALID; |
| 1269 | } |
| 1270 | |
| 1271 | /* Server second round Input */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1272 | status = psa_pake_input( server, PSA_PAKE_STEP_KEY_SHARE, |
| 1273 | buffer1 + c_a_off, c_a_len ); |
| 1274 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1275 | { |
| 1276 | TEST_EQUAL( status, expected_status ); |
| 1277 | break; |
| 1278 | } |
| 1279 | else |
| 1280 | { |
| 1281 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1282 | } |
| 1283 | |
| 1284 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PUBLIC, |
| 1285 | buffer1 + c_x2s_pk_off, c_x2s_pk_len ); |
| 1286 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1287 | { |
| 1288 | TEST_EQUAL( status, expected_status ); |
| 1289 | break; |
| 1290 | } |
| 1291 | else |
| 1292 | { |
| 1293 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1294 | } |
| 1295 | |
| 1296 | status = psa_pake_input( server, PSA_PAKE_STEP_ZK_PROOF, |
| 1297 | buffer1 + c_x2s_pr_off, c_x2s_pr_len ); |
| 1298 | if( inject_error == 4 && status != PSA_SUCCESS ) |
| 1299 | { |
| 1300 | TEST_EQUAL( status, expected_status ); |
| 1301 | break; |
| 1302 | } |
| 1303 | else |
| 1304 | { |
| 1305 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 1306 | } |
| 1307 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1308 | /* Error didn't trigger, make test fail */ |
Neil Armstrong | db5b960 | 2022-06-20 14:56:50 +0200 | [diff] [blame] | 1309 | if( inject_error == 4 ) |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 1310 | 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] | 1311 | |
| 1312 | break; |
| 1313 | |
| 1314 | } |
| 1315 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1316 | exit: |
| 1317 | mbedtls_free( buffer0 ); |
| 1318 | mbedtls_free( buffer1 ); |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1319 | } |
Neil Armstrong | 75673ab | 2022-06-15 17:39:01 +0200 | [diff] [blame] | 1320 | #endif /* PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 1321 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1322 | /* END_HEADER */ |
| 1323 | |
| 1324 | /* BEGIN_DEPENDENCIES |
| 1325 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 1326 | * END_DEPENDENCIES |
| 1327 | */ |
| 1328 | |
| 1329 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1330 | void static_checks( ) |
| 1331 | { |
| 1332 | size_t max_truncated_mac_size = |
| 1333 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 1334 | |
| 1335 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 1336 | * encoding. The shifted mask is the maximum truncated value. The |
| 1337 | * untruncated algorithm may be one byte larger. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1338 | TEST_LE_U( PSA_MAC_MAX_SIZE, 1 + max_truncated_mac_size ); |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 1339 | } |
| 1340 | /* END_CASE */ |
| 1341 | |
| 1342 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1343 | void import_with_policy( int type_arg, |
| 1344 | int usage_arg, int alg_arg, |
| 1345 | int expected_status_arg ) |
| 1346 | { |
| 1347 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1348 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1349 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1350 | psa_key_type_t type = type_arg; |
| 1351 | psa_key_usage_t usage = usage_arg; |
| 1352 | psa_algorithm_t alg = alg_arg; |
| 1353 | psa_status_t expected_status = expected_status_arg; |
| 1354 | const uint8_t key_material[16] = {0}; |
| 1355 | psa_status_t status; |
| 1356 | |
| 1357 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1358 | |
| 1359 | psa_set_key_type( &attributes, type ); |
| 1360 | psa_set_key_usage_flags( &attributes, usage ); |
| 1361 | psa_set_key_algorithm( &attributes, alg ); |
| 1362 | |
| 1363 | status = psa_import_key( &attributes, |
| 1364 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1365 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1366 | TEST_EQUAL( status, expected_status ); |
| 1367 | if( status != PSA_SUCCESS ) |
| 1368 | goto exit; |
| 1369 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1370 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1371 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 1372 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1373 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1374 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1375 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1376 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1377 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1378 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1379 | |
| 1380 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1381 | /* |
| 1382 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1383 | * thus reset them as required. |
| 1384 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1385 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1386 | |
| 1387 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1388 | PSA_DONE( ); |
| 1389 | } |
| 1390 | /* END_CASE */ |
| 1391 | |
| 1392 | /* BEGIN_CASE */ |
| 1393 | void import_with_data( data_t *data, int type_arg, |
| 1394 | int attr_bits_arg, |
| 1395 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1396 | { |
| 1397 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1398 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1399 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1400 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1401 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1402 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1403 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1404 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1405 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1406 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1407 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1408 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 1409 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1410 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1411 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1412 | if( status != PSA_SUCCESS ) |
| 1413 | goto exit; |
| 1414 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1415 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1416 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 1417 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 1418 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1419 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1420 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1421 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1422 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1423 | |
| 1424 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1425 | /* |
| 1426 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1427 | * thus reset them as required. |
| 1428 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1429 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1430 | |
| 1431 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1432 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1433 | } |
| 1434 | /* END_CASE */ |
| 1435 | |
| 1436 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1437 | void import_large_key( int type_arg, int byte_size_arg, |
| 1438 | int expected_status_arg ) |
| 1439 | { |
| 1440 | psa_key_type_t type = type_arg; |
| 1441 | size_t byte_size = byte_size_arg; |
| 1442 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1443 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1444 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1445 | psa_status_t status; |
| 1446 | uint8_t *buffer = NULL; |
| 1447 | size_t buffer_size = byte_size + 1; |
| 1448 | size_t n; |
| 1449 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1450 | /* Skip the test case if the target running the test cannot |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 1451 | * accommodate large keys due to heap size constraints */ |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 1452 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1453 | memset( buffer, 'K', byte_size ); |
| 1454 | |
| 1455 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1456 | |
| 1457 | /* Try importing the key */ |
| 1458 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1459 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1460 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 1461 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1462 | TEST_EQUAL( status, expected_status ); |
| 1463 | |
| 1464 | if( status == PSA_SUCCESS ) |
| 1465 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1466 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1467 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 1468 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 1469 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1470 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1471 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1472 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1473 | for( n = 0; n < byte_size; n++ ) |
| 1474 | TEST_EQUAL( buffer[n], 'K' ); |
| 1475 | for( n = byte_size; n < buffer_size; n++ ) |
| 1476 | TEST_EQUAL( buffer[n], 0 ); |
| 1477 | } |
| 1478 | |
| 1479 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1480 | /* |
| 1481 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1482 | * thus reset them as required. |
| 1483 | */ |
| 1484 | psa_reset_key_attributes( &attributes ); |
| 1485 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1486 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 1487 | PSA_DONE( ); |
| 1488 | mbedtls_free( buffer ); |
| 1489 | } |
| 1490 | /* END_CASE */ |
| 1491 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 1492 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1493 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 1494 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1495 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1496 | size_t bits = bits_arg; |
| 1497 | psa_status_t expected_status = expected_status_arg; |
| 1498 | psa_status_t status; |
| 1499 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1500 | 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] | 1501 | size_t buffer_size = /* Slight overapproximations */ |
| 1502 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1503 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1504 | unsigned char *p; |
| 1505 | int ret; |
| 1506 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1507 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1508 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1509 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1510 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1511 | |
| 1512 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 1513 | bits, keypair ) ) >= 0 ); |
| 1514 | length = ret; |
| 1515 | |
| 1516 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1517 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1518 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1519 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1520 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1521 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1522 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1523 | |
| 1524 | exit: |
| 1525 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1526 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 1527 | } |
| 1528 | /* END_CASE */ |
| 1529 | |
| 1530 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1531 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 1532 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 1533 | int usage_arg, int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1534 | int lifetime_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1535 | int expected_bits, |
| 1536 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1537 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1538 | int canonical_input ) |
| 1539 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1540 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1541 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1542 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1543 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1544 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1545 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1546 | unsigned char *exported = NULL; |
| 1547 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1548 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1549 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1550 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1551 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1552 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1553 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 1554 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1555 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1556 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1557 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1558 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1559 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1560 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1561 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 1562 | psa_set_key_algorithm( &attributes, alg ); |
| 1563 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 1564 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1565 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1566 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1567 | |
| 1568 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1569 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1570 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1571 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 1572 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1573 | |
| 1574 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1575 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1576 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1577 | |
| 1578 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 1579 | * and export_size. On errors, the exported length must be 0. */ |
| 1580 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 1581 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1582 | TEST_LE_U( exported_length, export_size ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 1583 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 1584 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 1585 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1586 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1587 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1588 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1589 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 1590 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1591 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 1592 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 1593 | * this validates the canonical representations. For canonical inputs, |
| 1594 | * this doesn't directly validate the implementation, but it still helps |
| 1595 | * by cross-validating the test data with the sanity check code. */ |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1596 | if( !psa_key_lifetime_is_external( lifetime ) ) |
| 1597 | { |
| 1598 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
| 1599 | goto exit; |
| 1600 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 1601 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1602 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1603 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1604 | else |
| 1605 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1606 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1607 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1608 | &key2 ) ); |
| 1609 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1610 | reexported, |
| 1611 | export_size, |
| 1612 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 1613 | ASSERT_COMPARE( exported, exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1614 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1615 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1616 | } |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1617 | TEST_LE_U( exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1618 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 1619 | psa_get_key_bits( &got_attributes ) ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1620 | TEST_LE_U( exported_length, PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1621 | |
| 1622 | destroy: |
| 1623 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1624 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1625 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1626 | |
| 1627 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1628 | /* |
| 1629 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1630 | * thus reset them as required. |
| 1631 | */ |
| 1632 | psa_reset_key_attributes( &got_attributes ); |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1633 | psa_destroy_key( key ) ; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1634 | mbedtls_free( exported ); |
| 1635 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1636 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 1637 | } |
| 1638 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1639 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1640 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1641 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1642 | int type_arg, |
| 1643 | int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1644 | int lifetime_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1645 | int export_size_delta, |
| 1646 | int expected_export_status_arg, |
| 1647 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1648 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1649 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1650 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 1651 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1652 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1653 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1654 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1655 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1656 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 1657 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1658 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1659 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1660 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1661 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 1662 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1663 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 1664 | psa_set_key_algorithm( &attributes, alg ); |
| 1665 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1666 | |
| 1667 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1668 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1669 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1670 | /* Export the public key */ |
| 1671 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1672 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 1673 | exported, export_size, |
| 1674 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1675 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1676 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1677 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 1678 | 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] | 1679 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1680 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1681 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 1682 | TEST_LE_U( expected_public_key->len, |
| 1683 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1684 | TEST_LE_U( expected_public_key->len, |
| 1685 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 1686 | TEST_LE_U( expected_public_key->len, |
| 1687 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 1688 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 1689 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 1690 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1691 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1692 | /* |
| 1693 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1694 | * thus reset them as required. |
| 1695 | */ |
| 1696 | psa_reset_key_attributes( &attributes ); |
| 1697 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 1698 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1699 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1700 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 1701 | } |
| 1702 | /* END_CASE */ |
| 1703 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 1704 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1705 | void import_and_exercise_key( data_t *data, |
| 1706 | int type_arg, |
| 1707 | int bits_arg, |
| 1708 | int alg_arg ) |
| 1709 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1710 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1711 | psa_key_type_t type = type_arg; |
| 1712 | size_t bits = bits_arg; |
| 1713 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1714 | 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] | 1715 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1716 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1717 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1718 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1719 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 1720 | psa_set_key_usage_flags( &attributes, usage ); |
| 1721 | psa_set_key_algorithm( &attributes, alg ); |
| 1722 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1723 | |
| 1724 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1725 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1726 | |
| 1727 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1728 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1729 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1730 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1731 | |
| 1732 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1733 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1734 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1735 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1736 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1737 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1738 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1739 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1740 | /* |
| 1741 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1742 | * thus reset them as required. |
| 1743 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1744 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1745 | |
| 1746 | psa_reset_key_attributes( &attributes ); |
| 1747 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1748 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1749 | } |
| 1750 | /* END_CASE */ |
| 1751 | |
| 1752 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1753 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1754 | int bits_arg, int expected_bits_arg, |
| 1755 | int usage_arg, int expected_usage_arg, |
| 1756 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1757 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1758 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1759 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1760 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1761 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1762 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1763 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1764 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1765 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1766 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1767 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1768 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1769 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1770 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1771 | psa_set_key_usage_flags( &attributes, usage ); |
| 1772 | psa_set_key_algorithm( &attributes, alg ); |
| 1773 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1774 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1775 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1776 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1777 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1778 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1779 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1780 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1781 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1782 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1783 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1784 | |
| 1785 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1786 | /* |
| 1787 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1788 | * thus reset them as required. |
| 1789 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1790 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1791 | |
| 1792 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1793 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1794 | } |
| 1795 | /* END_CASE */ |
| 1796 | |
| 1797 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1798 | void check_key_policy( int type_arg, int bits_arg, |
| 1799 | int usage_arg, int alg_arg ) |
| 1800 | { |
| 1801 | 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] | 1802 | usage_arg, |
| 1803 | mbedtls_test_update_key_usage_flags( usage_arg ), |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1804 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1805 | goto exit; |
| 1806 | } |
| 1807 | /* END_CASE */ |
| 1808 | |
| 1809 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1810 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1811 | { |
| 1812 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1813 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1814 | * 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] | 1815 | * to suppress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1816 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1817 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1818 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1819 | |
| 1820 | memset( &zero, 0, sizeof( zero ) ); |
| 1821 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1822 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1823 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1824 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1825 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1826 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1827 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1828 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1829 | |
| 1830 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1831 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1832 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1833 | |
| 1834 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1835 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1836 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1837 | |
| 1838 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1839 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1840 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1841 | } |
| 1842 | /* END_CASE */ |
| 1843 | |
| 1844 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1845 | void mac_key_policy( int policy_usage_arg, |
| 1846 | int policy_alg_arg, |
| 1847 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1848 | data_t *key_data, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1849 | int exercise_alg_arg, |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1850 | int expected_status_sign_arg, |
| 1851 | int expected_status_verify_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1852 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1853 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1854 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1855 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1856 | psa_key_type_t key_type = key_type_arg; |
| 1857 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1858 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1859 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1860 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1861 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1862 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1863 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1864 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1865 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1866 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1867 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1868 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1869 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1870 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1871 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1872 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1873 | |
gabor-mezei-arm | 40d5cd8 | 2021-06-29 11:06:16 +0200 | [diff] [blame] | 1874 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
| 1875 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1876 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1877 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1878 | TEST_EQUAL( status, expected_status_sign ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1879 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1880 | /* Calculate the MAC, one-shot case. */ |
| 1881 | uint8_t input[128] = {0}; |
| 1882 | size_t mac_len; |
| 1883 | TEST_EQUAL( psa_mac_compute( key, exercise_alg, |
| 1884 | input, 128, |
| 1885 | mac, PSA_MAC_MAX_SIZE, &mac_len ), |
| 1886 | expected_status_sign ); |
| 1887 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1888 | /* Calculate the MAC, multi-part case. */ |
| 1889 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1890 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
| 1891 | if( status == PSA_SUCCESS ) |
| 1892 | { |
| 1893 | status = psa_mac_update( &operation, input, 128 ); |
| 1894 | if( status == PSA_SUCCESS ) |
| 1895 | TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE, |
| 1896 | &mac_len ), |
| 1897 | expected_status_sign ); |
| 1898 | else |
| 1899 | TEST_EQUAL( status, expected_status_sign ); |
| 1900 | } |
| 1901 | else |
| 1902 | { |
| 1903 | TEST_EQUAL( status, expected_status_sign ); |
| 1904 | } |
| 1905 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1906 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1907 | /* Verify correct MAC, one-shot case. */ |
| 1908 | status = psa_mac_verify( key, exercise_alg, input, 128, |
| 1909 | mac, mac_len ); |
| 1910 | |
| 1911 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1912 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1913 | else |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1914 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1915 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1916 | /* Verify correct MAC, multi-part case. */ |
| 1917 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
| 1918 | if( status == PSA_SUCCESS ) |
| 1919 | { |
| 1920 | status = psa_mac_update( &operation, input, 128 ); |
| 1921 | if( status == PSA_SUCCESS ) |
| 1922 | { |
| 1923 | status = psa_mac_verify_finish( &operation, mac, mac_len ); |
| 1924 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1925 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1926 | else |
| 1927 | TEST_EQUAL( status, expected_status_verify ); |
| 1928 | } |
| 1929 | else |
| 1930 | { |
| 1931 | TEST_EQUAL( status, expected_status_verify ); |
| 1932 | } |
| 1933 | } |
| 1934 | else |
| 1935 | { |
| 1936 | TEST_EQUAL( status, expected_status_verify ); |
| 1937 | } |
| 1938 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1939 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1940 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1941 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1942 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1943 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1944 | |
| 1945 | exit: |
| 1946 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1947 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1948 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1949 | } |
| 1950 | /* END_CASE */ |
| 1951 | |
| 1952 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1953 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1954 | int policy_alg, |
| 1955 | int key_type, |
| 1956 | data_t *key_data, |
| 1957 | int exercise_alg ) |
| 1958 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1959 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1960 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1961 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1962 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1963 | size_t output_buffer_size = 0; |
| 1964 | size_t input_buffer_size = 0; |
| 1965 | size_t output_length = 0; |
| 1966 | uint8_t *output = NULL; |
| 1967 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1968 | psa_status_t status; |
| 1969 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1970 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg ); |
| 1971 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg, |
| 1972 | input_buffer_size ); |
| 1973 | |
| 1974 | ASSERT_ALLOC( input, input_buffer_size ); |
| 1975 | ASSERT_ALLOC( output, output_buffer_size ); |
| 1976 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1977 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1978 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1979 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1980 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1981 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1982 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1983 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1984 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1985 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1986 | /* Check if no key usage flag implication is done */ |
| 1987 | TEST_EQUAL( policy_usage, |
| 1988 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1989 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 1990 | /* Encrypt check, one-shot */ |
| 1991 | status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size, |
| 1992 | output, output_buffer_size, |
| 1993 | &output_length); |
| 1994 | if( policy_alg == exercise_alg && |
| 1995 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1996 | PSA_ASSERT( status ); |
| 1997 | else |
| 1998 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1999 | |
| 2000 | /* Encrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2001 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2002 | if( policy_alg == exercise_alg && |
| 2003 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2004 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2005 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2006 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2007 | psa_cipher_abort( &operation ); |
| 2008 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2009 | /* Decrypt check, one-shot */ |
| 2010 | status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size, |
| 2011 | input, input_buffer_size, |
| 2012 | &output_length); |
| 2013 | if( policy_alg == exercise_alg && |
| 2014 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 2015 | PSA_ASSERT( status ); |
| 2016 | else |
| 2017 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2018 | |
| 2019 | /* Decrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2020 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2021 | if( policy_alg == exercise_alg && |
| 2022 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2023 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2024 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2025 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2026 | |
| 2027 | exit: |
| 2028 | psa_cipher_abort( &operation ); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame] | 2029 | mbedtls_free( input ); |
| 2030 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2031 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2032 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2033 | } |
| 2034 | /* END_CASE */ |
| 2035 | |
| 2036 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2037 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2038 | int policy_alg, |
| 2039 | int key_type, |
| 2040 | data_t *key_data, |
| 2041 | int nonce_length_arg, |
| 2042 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2043 | int exercise_alg, |
| 2044 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2045 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2046 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2047 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2048 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2049 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2050 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2051 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2052 | unsigned char nonce[16] = {0}; |
| 2053 | size_t nonce_length = nonce_length_arg; |
| 2054 | unsigned char tag[16]; |
| 2055 | size_t tag_length = tag_length_arg; |
| 2056 | size_t output_length; |
| 2057 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2058 | TEST_LE_U( nonce_length, sizeof( nonce ) ); |
| 2059 | TEST_LE_U( tag_length, sizeof( tag ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2060 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2061 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2062 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2063 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2064 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2065 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2066 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2067 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2068 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2069 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2070 | /* Check if no key usage implication is done */ |
| 2071 | TEST_EQUAL( policy_usage, |
| 2072 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2073 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2074 | /* Encrypt check, one-shot */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2075 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2076 | nonce, nonce_length, |
| 2077 | NULL, 0, |
| 2078 | NULL, 0, |
| 2079 | tag, tag_length, |
| 2080 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2081 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2082 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2083 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2084 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2085 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2086 | /* Encrypt check, multi-part */ |
| 2087 | status = psa_aead_encrypt_setup( &operation, key, exercise_alg ); |
| 2088 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 2089 | TEST_EQUAL( status, expected_status ); |
| 2090 | else |
| 2091 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2092 | |
| 2093 | /* Decrypt check, one-shot */ |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2094 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2095 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2096 | nonce, nonce_length, |
| 2097 | NULL, 0, |
| 2098 | tag, tag_length, |
| 2099 | NULL, 0, |
| 2100 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2101 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2102 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2103 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2104 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2105 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2106 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2107 | |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2108 | /* Decrypt check, multi-part */ |
| 2109 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
| 2110 | status = psa_aead_decrypt_setup( &operation, key, exercise_alg ); |
| 2111 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 2112 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2113 | else |
| 2114 | TEST_EQUAL( status, expected_status ); |
| 2115 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2116 | exit: |
Neil Armstrong | 752d811 | 2022-02-07 14:51:11 +0100 | [diff] [blame] | 2117 | PSA_ASSERT( psa_aead_abort( &operation ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2118 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2119 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2120 | } |
| 2121 | /* END_CASE */ |
| 2122 | |
| 2123 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2124 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2125 | int policy_alg, |
| 2126 | int key_type, |
| 2127 | data_t *key_data, |
| 2128 | int exercise_alg ) |
| 2129 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2130 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2131 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2132 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2133 | psa_status_t status; |
| 2134 | size_t key_bits; |
| 2135 | size_t buffer_length; |
| 2136 | unsigned char *buffer = NULL; |
| 2137 | size_t output_length; |
| 2138 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2139 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2140 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2141 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2142 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2143 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2144 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2145 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2146 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2147 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2148 | /* Check if no key usage implication is done */ |
| 2149 | TEST_EQUAL( policy_usage, |
| 2150 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2151 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2152 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 2153 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2154 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 2155 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 2156 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2157 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2158 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2159 | NULL, 0, |
| 2160 | NULL, 0, |
| 2161 | buffer, buffer_length, |
| 2162 | &output_length ); |
| 2163 | if( policy_alg == exercise_alg && |
| 2164 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2165 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2166 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2167 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2168 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 2169 | if( buffer_length != 0 ) |
| 2170 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2171 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2172 | buffer, buffer_length, |
| 2173 | NULL, 0, |
| 2174 | buffer, buffer_length, |
| 2175 | &output_length ); |
| 2176 | if( policy_alg == exercise_alg && |
| 2177 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2178 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2179 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2180 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2181 | |
| 2182 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2183 | /* |
| 2184 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2185 | * thus reset them as required. |
| 2186 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2187 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2188 | |
| 2189 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2190 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2191 | mbedtls_free( buffer ); |
| 2192 | } |
| 2193 | /* END_CASE */ |
| 2194 | |
| 2195 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2196 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2197 | int policy_alg, |
| 2198 | int key_type, |
| 2199 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2200 | int exercise_alg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2201 | int payload_length_arg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2202 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2203 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2204 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2205 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2206 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 2207 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2208 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 2209 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 2210 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 2211 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 2212 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 2213 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 2214 | int compatible_alg = payload_length_arg > 0; |
| 2215 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2216 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2217 | size_t signature_length; |
| 2218 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2219 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2220 | in the expected usage flags. */ |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2221 | TEST_EQUAL( expected_usage, |
| 2222 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2223 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2224 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2225 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2226 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2227 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2228 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2229 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2230 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2231 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2232 | |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2233 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 2234 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2235 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2236 | payload, payload_length, |
| 2237 | signature, sizeof( signature ), |
| 2238 | &signature_length ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2239 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2240 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2241 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2242 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2243 | |
| 2244 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2245 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2246 | payload, payload_length, |
| 2247 | signature, sizeof( signature ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2248 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2249 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 2250 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2251 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2252 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 2253 | if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) && |
gabor-mezei-arm | d851d68 | 2021-06-28 14:53:49 +0200 | [diff] [blame] | 2254 | PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) ) |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 2255 | { |
| 2256 | status = psa_sign_message( key, exercise_alg, |
| 2257 | payload, payload_length, |
| 2258 | signature, sizeof( signature ), |
| 2259 | &signature_length ); |
| 2260 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 2261 | PSA_ASSERT( status ); |
| 2262 | else |
| 2263 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2264 | |
| 2265 | memset( signature, 0, sizeof( signature ) ); |
| 2266 | status = psa_verify_message( key, exercise_alg, |
| 2267 | payload, payload_length, |
| 2268 | signature, sizeof( signature ) ); |
| 2269 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 2270 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 2271 | else |
| 2272 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 2273 | } |
| 2274 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2275 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2276 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2277 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 2278 | } |
| 2279 | /* END_CASE */ |
| 2280 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2281 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2282 | void derive_key_policy( int policy_usage, |
| 2283 | int policy_alg, |
| 2284 | int key_type, |
| 2285 | data_t *key_data, |
| 2286 | int exercise_alg ) |
| 2287 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2288 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2289 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2290 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2291 | psa_status_t status; |
| 2292 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2293 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2294 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2295 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2296 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2297 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2298 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2299 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2300 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2301 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2302 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 2303 | |
| 2304 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 2305 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2306 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2307 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 2308 | &operation, |
| 2309 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 2310 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 2311 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2312 | |
| 2313 | status = psa_key_derivation_input_key( &operation, |
| 2314 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2315 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 2316 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2317 | if( policy_alg == exercise_alg && |
| 2318 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2319 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2320 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2321 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2322 | |
| 2323 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2324 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2325 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2326 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 2327 | } |
| 2328 | /* END_CASE */ |
| 2329 | |
| 2330 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2331 | void agreement_key_policy( int policy_usage, |
| 2332 | int policy_alg, |
| 2333 | int key_type_arg, |
| 2334 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2335 | int exercise_alg, |
| 2336 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2337 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2338 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2339 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2340 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2341 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2342 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2343 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2344 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2345 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2346 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2347 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2348 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2349 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2350 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2351 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2352 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2353 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2354 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2355 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2356 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2357 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2358 | |
| 2359 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2360 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2361 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2362 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 2363 | } |
| 2364 | /* END_CASE */ |
| 2365 | |
| 2366 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2367 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 2368 | int usage_arg, int alg_arg, int alg2_arg ) |
| 2369 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2370 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2371 | psa_key_type_t key_type = key_type_arg; |
| 2372 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2373 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2374 | psa_key_usage_t usage = usage_arg; |
| 2375 | psa_algorithm_t alg = alg_arg; |
| 2376 | psa_algorithm_t alg2 = alg2_arg; |
| 2377 | |
| 2378 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2379 | |
| 2380 | psa_set_key_usage_flags( &attributes, usage ); |
| 2381 | psa_set_key_algorithm( &attributes, alg ); |
| 2382 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 2383 | psa_set_key_type( &attributes, key_type ); |
| 2384 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2385 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2386 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 2387 | /* Update the usage flags to obtain implicit usage flags */ |
| 2388 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2389 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2390 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 2391 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 2392 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 2393 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2394 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2395 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2396 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2397 | goto exit; |
| 2398 | |
| 2399 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2400 | /* |
| 2401 | * Key attributes may have been returned by psa_get_key_attributes() |
| 2402 | * thus reset them as required. |
| 2403 | */ |
| 2404 | psa_reset_key_attributes( &got_attributes ); |
| 2405 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2406 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2407 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 2408 | } |
| 2409 | /* END_CASE */ |
| 2410 | |
| 2411 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2412 | void raw_agreement_key_policy( int policy_usage, |
| 2413 | int policy_alg, |
| 2414 | int key_type_arg, |
| 2415 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2416 | int exercise_alg, |
| 2417 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2418 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2419 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2420 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2421 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2422 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2423 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2424 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2425 | |
| 2426 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2427 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2428 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 2429 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 2430 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2431 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2432 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2433 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2434 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 2435 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2436 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 2437 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2438 | |
| 2439 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 2440 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2441 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2442 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 2443 | } |
| 2444 | /* END_CASE */ |
| 2445 | |
| 2446 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2447 | void copy_success( int source_usage_arg, |
| 2448 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2449 | unsigned int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2450 | int type_arg, data_t *material, |
| 2451 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2452 | int target_usage_arg, |
| 2453 | int target_alg_arg, int target_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2454 | unsigned int target_lifetime_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2455 | int expected_usage_arg, |
| 2456 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2457 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2458 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2459 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2460 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 2461 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2462 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2463 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 2464 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2465 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2466 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2467 | uint8_t *export_buffer = NULL; |
| 2468 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2469 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2470 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2471 | /* Prepare the source key. */ |
| 2472 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2473 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2474 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2475 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2476 | psa_set_key_lifetime( &source_attributes, source_lifetime); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2477 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2478 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2479 | &source_key ) ); |
| 2480 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2481 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2482 | /* Prepare the target attributes. */ |
| 2483 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2484 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2485 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2486 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2487 | psa_set_key_lifetime( &target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 2488 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2489 | if( target_usage_arg != -1 ) |
| 2490 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2491 | if( target_alg_arg != -1 ) |
| 2492 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2493 | if( target_alg2_arg != -1 ) |
| 2494 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2495 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2496 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2497 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2498 | PSA_ASSERT( psa_copy_key( source_key, |
| 2499 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2500 | |
| 2501 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2502 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2503 | |
| 2504 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2505 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 2506 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 2507 | psa_get_key_type( &target_attributes ) ); |
| 2508 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 2509 | psa_get_key_bits( &target_attributes ) ); |
| 2510 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 2511 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2512 | TEST_EQUAL( expected_alg2, |
| 2513 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2514 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 2515 | { |
| 2516 | size_t length; |
| 2517 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2518 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2519 | material->len, &length ) ); |
| 2520 | ASSERT_COMPARE( material->x, material->len, |
| 2521 | export_buffer, length ); |
| 2522 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 2523 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2524 | if( !psa_key_lifetime_is_external( target_lifetime ) ) |
| 2525 | { |
| 2526 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
| 2527 | goto exit; |
| 2528 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
| 2529 | goto exit; |
| 2530 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2531 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2532 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2533 | |
| 2534 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2535 | /* |
| 2536 | * Source and target key attributes may have been returned by |
| 2537 | * psa_get_key_attributes() thus reset them as required. |
| 2538 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 2539 | psa_reset_key_attributes( &source_attributes ); |
| 2540 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 2541 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2542 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 2543 | mbedtls_free( export_buffer ); |
| 2544 | } |
| 2545 | /* END_CASE */ |
| 2546 | |
| 2547 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2548 | void copy_fail( int source_usage_arg, |
| 2549 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2550 | int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2551 | int type_arg, data_t *material, |
| 2552 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2553 | int target_usage_arg, |
| 2554 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2555 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2556 | int expected_status_arg ) |
| 2557 | { |
| 2558 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2559 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2560 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2561 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2562 | 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] | 2563 | |
| 2564 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2565 | |
| 2566 | /* Prepare the source key. */ |
| 2567 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 2568 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2569 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2570 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 2571 | psa_set_key_lifetime( &source_attributes, source_lifetime_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 2572 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 2573 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2574 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2575 | |
| 2576 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 2577 | psa_set_key_id( &target_attributes, key_id ); |
| 2578 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2579 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 2580 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 2581 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 2582 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 2583 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2584 | |
| 2585 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2586 | TEST_EQUAL( psa_copy_key( source_key, |
| 2587 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2588 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2589 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2590 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2591 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2592 | exit: |
| 2593 | psa_reset_key_attributes( &source_attributes ); |
| 2594 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2595 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 2596 | } |
| 2597 | /* END_CASE */ |
| 2598 | |
| 2599 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2600 | void hash_operation_init( ) |
| 2601 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2602 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2603 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2604 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2605 | * 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] | 2606 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2607 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 2608 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 2609 | psa_hash_operation_t zero; |
| 2610 | |
| 2611 | memset( &zero, 0, sizeof( zero ) ); |
| 2612 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2613 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2614 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 2615 | PSA_ERROR_BAD_STATE ); |
| 2616 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 2617 | PSA_ERROR_BAD_STATE ); |
| 2618 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 2619 | PSA_ERROR_BAD_STATE ); |
| 2620 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2621 | /* A default hash operation should be abortable without error. */ |
| 2622 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 2623 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 2624 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2625 | } |
| 2626 | /* END_CASE */ |
| 2627 | |
| 2628 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2629 | void hash_setup( int alg_arg, |
| 2630 | int expected_status_arg ) |
| 2631 | { |
| 2632 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2633 | uint8_t *output = NULL; |
| 2634 | size_t output_size = 0; |
| 2635 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2636 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2637 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2638 | psa_status_t status; |
| 2639 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2640 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2641 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2642 | /* Hash Setup, one-shot */ |
Neil Armstrong | ee9686b | 2022-02-25 15:47:34 +0100 | [diff] [blame] | 2643 | output_size = PSA_HASH_LENGTH( alg ); |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2644 | ASSERT_ALLOC( output, output_size ); |
| 2645 | |
| 2646 | status = psa_hash_compute( alg, NULL, 0, |
| 2647 | output, output_size, &output_length ); |
| 2648 | TEST_EQUAL( status, expected_status ); |
| 2649 | |
| 2650 | /* Hash Setup, multi-part */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 2651 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2652 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2653 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 2654 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 2655 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2656 | |
| 2657 | /* If setup failed, reproduce the failure, so as to |
| 2658 | * test the resulting state of the operation object. */ |
| 2659 | if( status != PSA_SUCCESS ) |
| 2660 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 2661 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2662 | /* Now the operation object should be reusable. */ |
| 2663 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 2664 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 2665 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2666 | #endif |
| 2667 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2668 | exit: |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 2669 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2670 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2671 | } |
| 2672 | /* END_CASE */ |
| 2673 | |
| 2674 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2675 | void hash_compute_fail( int alg_arg, data_t *input, |
| 2676 | int output_size_arg, int expected_status_arg ) |
| 2677 | { |
| 2678 | psa_algorithm_t alg = alg_arg; |
| 2679 | uint8_t *output = NULL; |
| 2680 | size_t output_size = output_size_arg; |
| 2681 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2682 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2683 | psa_status_t expected_status = expected_status_arg; |
| 2684 | psa_status_t status; |
| 2685 | |
| 2686 | ASSERT_ALLOC( output, output_size ); |
| 2687 | |
| 2688 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2689 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2690 | /* Hash Compute, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2691 | status = psa_hash_compute( alg, input->x, input->len, |
| 2692 | output, output_size, &output_length ); |
| 2693 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2694 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2695 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2696 | /* Hash Compute, multi-part */ |
| 2697 | status = psa_hash_setup( &operation, alg ); |
| 2698 | if( status == PSA_SUCCESS ) |
| 2699 | { |
| 2700 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2701 | if( status == PSA_SUCCESS ) |
| 2702 | { |
| 2703 | status = psa_hash_finish( &operation, output, output_size, |
| 2704 | &output_length ); |
| 2705 | if( status == PSA_SUCCESS ) |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 2706 | TEST_LE_U( output_length, output_size ); |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2707 | else |
| 2708 | TEST_EQUAL( status, expected_status ); |
| 2709 | } |
| 2710 | else |
| 2711 | { |
| 2712 | TEST_EQUAL( status, expected_status ); |
| 2713 | } |
| 2714 | } |
| 2715 | else |
| 2716 | { |
| 2717 | TEST_EQUAL( status, expected_status ); |
| 2718 | } |
| 2719 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2720 | exit: |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 2721 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2722 | mbedtls_free( output ); |
| 2723 | PSA_DONE( ); |
| 2724 | } |
| 2725 | /* END_CASE */ |
| 2726 | |
| 2727 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2728 | void hash_compare_fail( int alg_arg, data_t *input, |
| 2729 | data_t *reference_hash, |
| 2730 | int expected_status_arg ) |
| 2731 | { |
| 2732 | psa_algorithm_t alg = alg_arg; |
| 2733 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2734 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2735 | psa_status_t status; |
| 2736 | |
| 2737 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2738 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2739 | /* Hash Compare, one-shot */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2740 | status = psa_hash_compare( alg, input->x, input->len, |
| 2741 | reference_hash->x, reference_hash->len ); |
| 2742 | TEST_EQUAL( status, expected_status ); |
| 2743 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2744 | /* Hash Compare, multi-part */ |
| 2745 | status = psa_hash_setup( &operation, alg ); |
| 2746 | if( status == PSA_SUCCESS ) |
| 2747 | { |
| 2748 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2749 | if( status == PSA_SUCCESS ) |
| 2750 | { |
| 2751 | status = psa_hash_verify( &operation, reference_hash->x, |
| 2752 | reference_hash->len ); |
| 2753 | TEST_EQUAL( status, expected_status ); |
| 2754 | } |
| 2755 | else |
| 2756 | { |
| 2757 | TEST_EQUAL( status, expected_status ); |
| 2758 | } |
| 2759 | } |
| 2760 | else |
| 2761 | { |
| 2762 | TEST_EQUAL( status, expected_status ); |
| 2763 | } |
| 2764 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2765 | exit: |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2766 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2767 | PSA_DONE( ); |
| 2768 | } |
| 2769 | /* END_CASE */ |
| 2770 | |
| 2771 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2772 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2773 | data_t *expected_output ) |
| 2774 | { |
| 2775 | psa_algorithm_t alg = alg_arg; |
| 2776 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2777 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2778 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2779 | size_t i; |
| 2780 | |
| 2781 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2782 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2783 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2784 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2785 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2786 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2787 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2788 | ASSERT_COMPARE( output, output_length, |
| 2789 | expected_output->x, expected_output->len ); |
| 2790 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2791 | /* Compute with tight buffer, multi-part */ |
| 2792 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2793 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2794 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2795 | PSA_HASH_LENGTH( alg ), |
| 2796 | &output_length ) ); |
| 2797 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2798 | ASSERT_COMPARE( output, output_length, |
| 2799 | expected_output->x, expected_output->len ); |
| 2800 | |
| 2801 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2802 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2803 | output, sizeof( output ), |
| 2804 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2805 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2806 | ASSERT_COMPARE( output, output_length, |
| 2807 | expected_output->x, expected_output->len ); |
| 2808 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2809 | /* Compute with larger buffer, multi-part */ |
| 2810 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2811 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2812 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2813 | sizeof( output ), &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 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2819 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2820 | output, output_length ) ); |
| 2821 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2822 | /* Compare with correct hash, multi-part */ |
| 2823 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2824 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2825 | PSA_ASSERT( psa_hash_verify( &operation, output, |
| 2826 | output_length ) ); |
| 2827 | |
| 2828 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2829 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2830 | output, output_length + 1 ), |
| 2831 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2832 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2833 | /* Compare with trailing garbage, multi-part */ |
| 2834 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2835 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2836 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ), |
| 2837 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2838 | |
| 2839 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2840 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2841 | output, output_length - 1 ), |
| 2842 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2843 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2844 | /* Compare with truncated hash, multi-part */ |
| 2845 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2846 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2847 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ), |
| 2848 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2849 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2850 | /* Compare with corrupted value */ |
| 2851 | for( i = 0; i < output_length; i++ ) |
| 2852 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2853 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2854 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2855 | |
| 2856 | /* 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 ), |
| 2859 | PSA_ERROR_INVALID_SIGNATURE ); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2860 | |
| 2861 | /* 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 ), |
| 2865 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2866 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2867 | output[i] ^= 1; |
| 2868 | } |
| 2869 | |
| 2870 | exit: |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2871 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2872 | PSA_DONE( ); |
| 2873 | } |
| 2874 | /* END_CASE */ |
| 2875 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2876 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2877 | void hash_bad_order( ) |
| 2878 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2879 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2880 | unsigned char input[] = ""; |
| 2881 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2882 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2883 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2884 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2885 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2886 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2887 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2888 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2889 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2890 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2891 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2892 | /* Call setup twice in a row. */ |
| 2893 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2894 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2895 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2896 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2897 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2898 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2899 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2900 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2901 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2902 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2903 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2904 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2905 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2906 | /* Check that update calls abort on error. */ |
| 2907 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2908 | operation.id = UINT_MAX; |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2909 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
| 2910 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
| 2911 | PSA_ERROR_BAD_STATE ); |
| 2912 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2913 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2914 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2915 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2916 | /* Call update after finish. */ |
| 2917 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2918 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2919 | hash, sizeof( hash ), &hash_len ) ); |
| 2920 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2921 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2922 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2923 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2924 | /* Call verify without calling setup beforehand. */ |
| 2925 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2926 | valid_hash, sizeof( valid_hash ) ), |
| 2927 | PSA_ERROR_BAD_STATE ); |
| 2928 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2929 | |
| 2930 | /* Call verify after finish. */ |
| 2931 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2932 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2933 | hash, sizeof( hash ), &hash_len ) ); |
| 2934 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2935 | valid_hash, sizeof( valid_hash ) ), |
| 2936 | PSA_ERROR_BAD_STATE ); |
| 2937 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2938 | |
| 2939 | /* Call verify twice in a row. */ |
| 2940 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2941 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2942 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2943 | valid_hash, sizeof( valid_hash ) ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2944 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2945 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2946 | valid_hash, sizeof( valid_hash ) ), |
| 2947 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2948 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2949 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2950 | |
| 2951 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2952 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2953 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2954 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2955 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2956 | |
| 2957 | /* Call finish twice in a row. */ |
| 2958 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2959 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2960 | hash, sizeof( hash ), &hash_len ) ); |
| 2961 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2962 | hash, sizeof( hash ), &hash_len ), |
| 2963 | PSA_ERROR_BAD_STATE ); |
| 2964 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2965 | |
| 2966 | /* Call finish after calling verify. */ |
| 2967 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2968 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2969 | valid_hash, sizeof( valid_hash ) ) ); |
| 2970 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2971 | hash, sizeof( hash ), &hash_len ), |
| 2972 | PSA_ERROR_BAD_STATE ); |
| 2973 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2974 | |
| 2975 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2976 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2977 | } |
| 2978 | /* END_CASE */ |
| 2979 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2980 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2981 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2982 | { |
| 2983 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2984 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2985 | * appended to it */ |
| 2986 | unsigned char hash[] = { |
| 2987 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2988 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2989 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2990 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2991 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2992 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2993 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2994 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2995 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2996 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2997 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2998 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2999 | PSA_ERROR_INVALID_SIGNATURE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3000 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 3001 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 3002 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3003 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3004 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3005 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3006 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3007 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3008 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 3009 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3010 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3011 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3012 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 3013 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3014 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3015 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 3016 | } |
| 3017 | /* END_CASE */ |
| 3018 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3019 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3020 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3021 | { |
| 3022 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 3023 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3024 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 3025 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3026 | size_t hash_len; |
| 3027 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3028 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3029 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3030 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3031 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3032 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3033 | hash, expected_size - 1, &hash_len ), |
| 3034 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3035 | |
| 3036 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3037 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3038 | } |
| 3039 | /* END_CASE */ |
| 3040 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3041 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3042 | void hash_clone_source_state( ) |
| 3043 | { |
| 3044 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3045 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3046 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 3047 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3048 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3049 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3050 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3051 | size_t hash_len; |
| 3052 | |
| 3053 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3054 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 3055 | |
| 3056 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3057 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3058 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3059 | hash, sizeof( hash ), &hash_len ) ); |
| 3060 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3061 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3062 | |
| 3063 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 3064 | PSA_ERROR_BAD_STATE ); |
| 3065 | |
| 3066 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 3067 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 3068 | hash, sizeof( hash ), &hash_len ) ); |
| 3069 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 3070 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3071 | hash, sizeof( hash ), &hash_len ) ); |
| 3072 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 3073 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 3074 | hash, sizeof( hash ), &hash_len ) ); |
| 3075 | |
| 3076 | exit: |
| 3077 | psa_hash_abort( &op_source ); |
| 3078 | psa_hash_abort( &op_init ); |
| 3079 | psa_hash_abort( &op_setup ); |
| 3080 | psa_hash_abort( &op_finished ); |
| 3081 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3082 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3083 | } |
| 3084 | /* END_CASE */ |
| 3085 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3086 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3087 | void hash_clone_target_state( ) |
| 3088 | { |
| 3089 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 3090 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 3091 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 3092 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 3093 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 3094 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 3095 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 3096 | size_t hash_len; |
| 3097 | |
| 3098 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3099 | |
| 3100 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 3101 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 3102 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 3103 | hash, sizeof( hash ), &hash_len ) ); |
| 3104 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 3105 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 3106 | |
| 3107 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 3108 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 3109 | hash, sizeof( hash ), &hash_len ) ); |
| 3110 | |
| 3111 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 3112 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 3113 | PSA_ERROR_BAD_STATE ); |
| 3114 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 3115 | PSA_ERROR_BAD_STATE ); |
| 3116 | |
| 3117 | exit: |
| 3118 | psa_hash_abort( &op_target ); |
| 3119 | psa_hash_abort( &op_init ); |
| 3120 | psa_hash_abort( &op_setup ); |
| 3121 | psa_hash_abort( &op_finished ); |
| 3122 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3123 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 3124 | } |
| 3125 | /* END_CASE */ |
| 3126 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 3127 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3128 | void mac_operation_init( ) |
| 3129 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3130 | const uint8_t input[1] = { 0 }; |
| 3131 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3132 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3133 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3134 | * 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] | 3135 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3136 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 3137 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 3138 | psa_mac_operation_t zero; |
| 3139 | |
| 3140 | memset( &zero, 0, sizeof( zero ) ); |
| 3141 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3142 | /* A freshly-initialized MAC operation should not be usable. */ |
| 3143 | TEST_EQUAL( psa_mac_update( &func, |
| 3144 | input, sizeof( input ) ), |
| 3145 | PSA_ERROR_BAD_STATE ); |
| 3146 | TEST_EQUAL( psa_mac_update( &init, |
| 3147 | input, sizeof( input ) ), |
| 3148 | PSA_ERROR_BAD_STATE ); |
| 3149 | TEST_EQUAL( psa_mac_update( &zero, |
| 3150 | input, sizeof( input ) ), |
| 3151 | PSA_ERROR_BAD_STATE ); |
| 3152 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3153 | /* A default MAC operation should be abortable without error. */ |
| 3154 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 3155 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 3156 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3157 | } |
| 3158 | /* END_CASE */ |
| 3159 | |
| 3160 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3161 | void mac_setup( int key_type_arg, |
| 3162 | data_t *key, |
| 3163 | int alg_arg, |
| 3164 | int expected_status_arg ) |
| 3165 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3166 | psa_key_type_t key_type = key_type_arg; |
| 3167 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3168 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3169 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3170 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 3171 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3172 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3173 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3174 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3175 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3176 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3177 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 3178 | &operation, &status ) ) |
| 3179 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3180 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3181 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3182 | /* The operation object should be reusable. */ |
| 3183 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 3184 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 3185 | smoke_test_key_data, |
| 3186 | sizeof( smoke_test_key_data ), |
| 3187 | KNOWN_SUPPORTED_MAC_ALG, |
| 3188 | &operation, &status ) ) |
| 3189 | goto exit; |
| 3190 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3191 | #endif |
| 3192 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3193 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3194 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3195 | } |
| 3196 | /* END_CASE */ |
| 3197 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 3198 | /* 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] | 3199 | void mac_bad_order( ) |
| 3200 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3201 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3202 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 3203 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3204 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3205 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3206 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3207 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3208 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3209 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 3210 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 3211 | size_t sign_mac_length = 0; |
| 3212 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 3213 | const uint8_t verify_mac[] = { |
| 3214 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 3215 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 3216 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 3217 | |
| 3218 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3219 | 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] | 3220 | psa_set_key_algorithm( &attributes, alg ); |
| 3221 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3222 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3223 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3224 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3225 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3226 | /* Call update without calling setup beforehand. */ |
| 3227 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3228 | PSA_ERROR_BAD_STATE ); |
| 3229 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3230 | |
| 3231 | /* Call sign finish without calling setup beforehand. */ |
| 3232 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 3233 | &sign_mac_length), |
| 3234 | PSA_ERROR_BAD_STATE ); |
| 3235 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3236 | |
| 3237 | /* Call verify finish without calling setup beforehand. */ |
| 3238 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3239 | verify_mac, sizeof( verify_mac ) ), |
| 3240 | PSA_ERROR_BAD_STATE ); |
| 3241 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3242 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3243 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3244 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3245 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3246 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3247 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3248 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3249 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3250 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3251 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3252 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3253 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3254 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3255 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3256 | sign_mac, sizeof( sign_mac ), |
| 3257 | &sign_mac_length ) ); |
| 3258 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3259 | PSA_ERROR_BAD_STATE ); |
| 3260 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3261 | |
| 3262 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3263 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3264 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3265 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3266 | verify_mac, sizeof( verify_mac ) ) ); |
| 3267 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 3268 | PSA_ERROR_BAD_STATE ); |
| 3269 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3270 | |
| 3271 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3272 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3273 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3274 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 3275 | sign_mac, sizeof( sign_mac ), |
| 3276 | &sign_mac_length ) ); |
| 3277 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3278 | sign_mac, sizeof( sign_mac ), |
| 3279 | &sign_mac_length ), |
| 3280 | PSA_ERROR_BAD_STATE ); |
| 3281 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3282 | |
| 3283 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3284 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3285 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 3286 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3287 | verify_mac, sizeof( verify_mac ) ) ); |
| 3288 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3289 | verify_mac, sizeof( verify_mac ) ), |
| 3290 | PSA_ERROR_BAD_STATE ); |
| 3291 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3292 | |
| 3293 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3294 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3295 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3296 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3297 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3298 | verify_mac, sizeof( verify_mac ) ), |
| 3299 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3300 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3301 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3302 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3303 | |
| 3304 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3305 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3306 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3307 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3308 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3309 | sign_mac, sizeof( sign_mac ), |
| 3310 | &sign_mac_length ), |
| 3311 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3312 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 3313 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3314 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3315 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3316 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3317 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3318 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3319 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3320 | } |
| 3321 | /* END_CASE */ |
| 3322 | |
| 3323 | /* BEGIN_CASE */ |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3324 | void mac_sign_verify_multi( int key_type_arg, |
| 3325 | data_t *key_data, |
| 3326 | int alg_arg, |
| 3327 | data_t *input, |
| 3328 | int is_verify, |
| 3329 | data_t *expected_mac ) |
| 3330 | { |
| 3331 | size_t data_part_len = 0; |
| 3332 | |
| 3333 | for( data_part_len = 1; data_part_len <= input->len; data_part_len++ ) |
| 3334 | { |
| 3335 | /* Split data into length(data_part_len) parts. */ |
| 3336 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 3337 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3338 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3339 | alg_arg, |
| 3340 | input, data_part_len, |
| 3341 | expected_mac, |
| 3342 | is_verify, 0 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3343 | break; |
| 3344 | |
| 3345 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3346 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3347 | |
Neil Armstrong | fe6da1c | 2022-03-03 16:29:14 +0100 | [diff] [blame] | 3348 | if( mac_multipart_internal_func( key_type_arg, key_data, |
| 3349 | alg_arg, |
| 3350 | input, data_part_len, |
| 3351 | expected_mac, |
| 3352 | is_verify, 1 ) == 0 ) |
Neil Armstrong | 4766f99 | 2022-02-28 16:23:59 +0100 | [diff] [blame] | 3353 | break; |
| 3354 | } |
| 3355 | |
| 3356 | /* Goto is required to silence warnings about unused labels, as we |
| 3357 | * don't actually do any test assertions in this function. */ |
| 3358 | goto exit; |
| 3359 | } |
| 3360 | /* END_CASE */ |
| 3361 | |
| 3362 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3363 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3364 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3365 | int alg_arg, |
| 3366 | data_t *input, |
| 3367 | data_t *expected_mac ) |
| 3368 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3369 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3370 | psa_key_type_t key_type = key_type_arg; |
| 3371 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3372 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3373 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3374 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3375 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3376 | 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] | 3377 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3378 | const size_t output_sizes_to_test[] = { |
| 3379 | 0, |
| 3380 | 1, |
| 3381 | expected_mac->len - 1, |
| 3382 | expected_mac->len, |
| 3383 | expected_mac->len + 1, |
| 3384 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3385 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3386 | TEST_LE_U( mac_buffer_size, PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3387 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 3388 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3389 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3390 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3391 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3392 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3393 | psa_set_key_algorithm( &attributes, alg ); |
| 3394 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3395 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3396 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3397 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3398 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3399 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 3400 | { |
| 3401 | const size_t output_size = output_sizes_to_test[i]; |
| 3402 | psa_status_t expected_status = |
| 3403 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 3404 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3405 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3406 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3407 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3408 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3409 | /* Calculate the MAC, one-shot case. */ |
| 3410 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 3411 | input->x, input->len, |
| 3412 | actual_mac, output_size, &mac_length ), |
| 3413 | expected_status ); |
| 3414 | if( expected_status == PSA_SUCCESS ) |
| 3415 | { |
| 3416 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3417 | actual_mac, mac_length ); |
| 3418 | } |
| 3419 | |
| 3420 | if( output_size > 0 ) |
| 3421 | memset( actual_mac, 0, output_size ); |
| 3422 | |
| 3423 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3424 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 3425 | PSA_ASSERT( psa_mac_update( &operation, |
| 3426 | input->x, input->len ) ); |
| 3427 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 3428 | actual_mac, output_size, |
| 3429 | &mac_length ), |
| 3430 | expected_status ); |
| 3431 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 3432 | |
| 3433 | if( expected_status == PSA_SUCCESS ) |
| 3434 | { |
| 3435 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 3436 | actual_mac, mac_length ); |
| 3437 | } |
| 3438 | mbedtls_free( actual_mac ); |
| 3439 | actual_mac = NULL; |
| 3440 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3441 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3442 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3443 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3444 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3445 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 3446 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 3447 | } |
| 3448 | /* END_CASE */ |
| 3449 | |
| 3450 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3451 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3452 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3453 | int alg_arg, |
| 3454 | data_t *input, |
| 3455 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3456 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3457 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3458 | psa_key_type_t key_type = key_type_arg; |
| 3459 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 3460 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3461 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3462 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3463 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3464 | TEST_LE_U( expected_mac->len, PSA_MAC_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 3465 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3466 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3467 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 3468 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3469 | psa_set_key_algorithm( &attributes, alg ); |
| 3470 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 3471 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3472 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3473 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 3474 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3475 | /* Verify correct MAC, one-shot case. */ |
| 3476 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 3477 | expected_mac->x, expected_mac->len ) ); |
| 3478 | |
| 3479 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3480 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3481 | PSA_ASSERT( psa_mac_update( &operation, |
| 3482 | input->x, input->len ) ); |
| 3483 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 3484 | expected_mac->x, |
| 3485 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3486 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3487 | /* Test a MAC that's too short, one-shot case. */ |
| 3488 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3489 | input->x, input->len, |
| 3490 | expected_mac->x, |
| 3491 | expected_mac->len - 1 ), |
| 3492 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3493 | |
| 3494 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3495 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3496 | PSA_ASSERT( psa_mac_update( &operation, |
| 3497 | input->x, input->len ) ); |
| 3498 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3499 | expected_mac->x, |
| 3500 | expected_mac->len - 1 ), |
| 3501 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3502 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3503 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3504 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 3505 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3506 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3507 | input->x, input->len, |
| 3508 | perturbed_mac, expected_mac->len + 1 ), |
| 3509 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3510 | |
| 3511 | /* Test a MAC that's too long, 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 | perturbed_mac, |
| 3517 | expected_mac->len + 1 ), |
| 3518 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3519 | |
| 3520 | /* Test changing one byte. */ |
| 3521 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 3522 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 3523 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3524 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 3525 | |
| 3526 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 3527 | input->x, input->len, |
| 3528 | perturbed_mac, expected_mac->len ), |
| 3529 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3530 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3531 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3532 | PSA_ASSERT( psa_mac_update( &operation, |
| 3533 | input->x, input->len ) ); |
| 3534 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 3535 | perturbed_mac, |
| 3536 | expected_mac->len ), |
| 3537 | PSA_ERROR_INVALID_SIGNATURE ); |
| 3538 | perturbed_mac[i] ^= 1; |
| 3539 | } |
| 3540 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3541 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3542 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3543 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3544 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 3545 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 3546 | } |
| 3547 | /* END_CASE */ |
| 3548 | |
| 3549 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3550 | void cipher_operation_init( ) |
| 3551 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3552 | const uint8_t input[1] = { 0 }; |
| 3553 | unsigned char output[1] = { 0 }; |
| 3554 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3555 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 3556 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 3557 | * 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] | 3558 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3559 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 3560 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 3561 | psa_cipher_operation_t zero; |
| 3562 | |
| 3563 | memset( &zero, 0, sizeof( zero ) ); |
| 3564 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3565 | /* A freshly-initialized cipher operation should not be usable. */ |
| 3566 | TEST_EQUAL( psa_cipher_update( &func, |
| 3567 | input, sizeof( input ), |
| 3568 | output, sizeof( output ), |
| 3569 | &output_length ), |
| 3570 | PSA_ERROR_BAD_STATE ); |
| 3571 | TEST_EQUAL( psa_cipher_update( &init, |
| 3572 | input, sizeof( input ), |
| 3573 | output, sizeof( output ), |
| 3574 | &output_length ), |
| 3575 | PSA_ERROR_BAD_STATE ); |
| 3576 | TEST_EQUAL( psa_cipher_update( &zero, |
| 3577 | input, sizeof( input ), |
| 3578 | output, sizeof( output ), |
| 3579 | &output_length ), |
| 3580 | PSA_ERROR_BAD_STATE ); |
| 3581 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 3582 | /* A default cipher operation should be abortable without error. */ |
| 3583 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 3584 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 3585 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3586 | } |
| 3587 | /* END_CASE */ |
| 3588 | |
| 3589 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3590 | void cipher_setup( int key_type_arg, |
| 3591 | data_t *key, |
| 3592 | int alg_arg, |
| 3593 | int expected_status_arg ) |
| 3594 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3595 | psa_key_type_t key_type = key_type_arg; |
| 3596 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3597 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3598 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3599 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 3600 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3601 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 3602 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3603 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3604 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3605 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3606 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 3607 | &operation, &status ) ) |
| 3608 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3609 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3610 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 3611 | /* The operation object should be reusable. */ |
| 3612 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 3613 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 3614 | smoke_test_key_data, |
| 3615 | sizeof( smoke_test_key_data ), |
| 3616 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 3617 | &operation, &status ) ) |
| 3618 | goto exit; |
| 3619 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 3620 | #endif |
| 3621 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3622 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3623 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3624 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3625 | } |
| 3626 | /* END_CASE */ |
| 3627 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 3628 | /* 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] | 3629 | void cipher_bad_order( ) |
| 3630 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3631 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3632 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 3633 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3634 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3635 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3636 | 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] | 3637 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3638 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 3639 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 3640 | const uint8_t text[] = { |
| 3641 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 3642 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 3643 | 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] | 3644 | size_t length = 0; |
| 3645 | |
| 3646 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3647 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3648 | psa_set_key_algorithm( &attributes, alg ); |
| 3649 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3650 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 3651 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3652 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3653 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3654 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3655 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3656 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3657 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3658 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3659 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3660 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3661 | |
| 3662 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3663 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3664 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3665 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3666 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3667 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3668 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 3669 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 3670 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3671 | /* Generate an IV without calling setup beforehand. */ |
| 3672 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3673 | buffer, sizeof( buffer ), |
| 3674 | &length ), |
| 3675 | PSA_ERROR_BAD_STATE ); |
| 3676 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3677 | |
| 3678 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3679 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3680 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3681 | buffer, sizeof( buffer ), |
| 3682 | &length ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3683 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3684 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3685 | buffer, sizeof( buffer ), |
| 3686 | &length ), |
| 3687 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3688 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3689 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3690 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3691 | |
| 3692 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3693 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3694 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3695 | iv, sizeof( iv ) ) ); |
| 3696 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 3697 | buffer, sizeof( buffer ), |
| 3698 | &length ), |
| 3699 | PSA_ERROR_BAD_STATE ); |
| 3700 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3701 | |
| 3702 | /* Set an IV without calling setup beforehand. */ |
| 3703 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3704 | iv, sizeof( iv ) ), |
| 3705 | PSA_ERROR_BAD_STATE ); |
| 3706 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3707 | |
| 3708 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3709 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3710 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3711 | iv, sizeof( iv ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3712 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3713 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3714 | iv, sizeof( iv ) ), |
| 3715 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3716 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3717 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3718 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3719 | |
| 3720 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3721 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3722 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3723 | buffer, sizeof( buffer ), |
| 3724 | &length ) ); |
| 3725 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 3726 | iv, sizeof( iv ) ), |
| 3727 | PSA_ERROR_BAD_STATE ); |
| 3728 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3729 | |
| 3730 | /* Call update without calling setup beforehand. */ |
| 3731 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3732 | text, sizeof( text ), |
| 3733 | buffer, sizeof( buffer ), |
| 3734 | &length ), |
| 3735 | PSA_ERROR_BAD_STATE ); |
| 3736 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3737 | |
| 3738 | /* Call update without an IV where an IV is required. */ |
Dave Rodgman | 095dadc | 2021-06-23 12:48:52 +0100 | [diff] [blame] | 3739 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3740 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3741 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3742 | text, sizeof( text ), |
| 3743 | buffer, sizeof( buffer ), |
| 3744 | &length ), |
| 3745 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3746 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3747 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3748 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3749 | |
| 3750 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3751 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3752 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3753 | iv, sizeof( iv ) ) ); |
| 3754 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3755 | buffer, sizeof( buffer ), &length ) ); |
| 3756 | TEST_EQUAL( psa_cipher_update( &operation, |
| 3757 | text, sizeof( text ), |
| 3758 | buffer, sizeof( buffer ), |
| 3759 | &length ), |
| 3760 | PSA_ERROR_BAD_STATE ); |
| 3761 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3762 | |
| 3763 | /* Call finish without calling setup beforehand. */ |
| 3764 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3765 | buffer, sizeof( buffer ), &length ), |
| 3766 | PSA_ERROR_BAD_STATE ); |
| 3767 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3768 | |
| 3769 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3770 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3771 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 3772 | * for cipher modes with padding. */ |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3773 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3774 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3775 | buffer, sizeof( buffer ), &length ), |
| 3776 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3777 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3778 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 3779 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3780 | |
| 3781 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3782 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3783 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 3784 | iv, sizeof( iv ) ) ); |
| 3785 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3786 | buffer, sizeof( buffer ), &length ) ); |
| 3787 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3788 | buffer, sizeof( buffer ), &length ), |
| 3789 | PSA_ERROR_BAD_STATE ); |
| 3790 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3791 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3792 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3793 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3794 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3795 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3796 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3797 | } |
| 3798 | /* END_CASE */ |
| 3799 | |
| 3800 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3801 | void cipher_encrypt_fail( int alg_arg, |
| 3802 | int key_type_arg, |
| 3803 | data_t *key_data, |
| 3804 | data_t *input, |
| 3805 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3806 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3807 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3808 | psa_status_t status; |
| 3809 | psa_key_type_t key_type = key_type_arg; |
| 3810 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3811 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3812 | unsigned char iv[PSA_CIPHER_IV_MAX_SIZE] = {0}; |
| 3813 | size_t iv_size = PSA_CIPHER_IV_MAX_SIZE; |
| 3814 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3815 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3816 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3817 | size_t output_length = 0; |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3818 | size_t function_output_length; |
| 3819 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3820 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3821 | |
| 3822 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 3823 | { |
| 3824 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3825 | |
| 3826 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3827 | psa_set_key_algorithm( &attributes, alg ); |
| 3828 | psa_set_key_type( &attributes, key_type ); |
| 3829 | |
| 3830 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3831 | input->len ); |
| 3832 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3833 | |
| 3834 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3835 | &key ) ); |
| 3836 | } |
| 3837 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3838 | /* Encrypt, one-shot */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3839 | status = psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 3840 | output_buffer_size, &output_length ); |
| 3841 | |
| 3842 | TEST_EQUAL( status, expected_status ); |
| 3843 | |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3844 | /* Encrypt, multi-part */ |
| 3845 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 3846 | if( status == PSA_SUCCESS ) |
| 3847 | { |
| 3848 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3849 | { |
| 3850 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 3851 | iv, iv_size, |
| 3852 | &iv_length ) ); |
| 3853 | } |
| 3854 | |
| 3855 | status = psa_cipher_update( &operation, input->x, input->len, |
| 3856 | output, output_buffer_size, |
| 3857 | &function_output_length ); |
| 3858 | if( status == PSA_SUCCESS ) |
| 3859 | { |
| 3860 | output_length += function_output_length; |
| 3861 | |
| 3862 | status = psa_cipher_finish( &operation, output + output_length, |
| 3863 | output_buffer_size - output_length, |
| 3864 | &function_output_length ); |
| 3865 | |
| 3866 | TEST_EQUAL( status, expected_status ); |
| 3867 | } |
| 3868 | else |
| 3869 | { |
| 3870 | TEST_EQUAL( status, expected_status ); |
| 3871 | } |
| 3872 | } |
| 3873 | else |
| 3874 | { |
| 3875 | TEST_EQUAL( status, expected_status ); |
| 3876 | } |
| 3877 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3878 | exit: |
Neil Armstrong | d8dba4e | 2022-02-07 15:19:29 +0100 | [diff] [blame] | 3879 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3880 | mbedtls_free( output ); |
| 3881 | psa_destroy_key( key ); |
| 3882 | PSA_DONE( ); |
| 3883 | } |
| 3884 | /* END_CASE */ |
| 3885 | |
| 3886 | /* BEGIN_CASE */ |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3887 | void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data, |
| 3888 | data_t *input, int iv_length, |
| 3889 | int expected_result ) |
| 3890 | { |
| 3891 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3892 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3893 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3894 | size_t output_buffer_size = 0; |
| 3895 | unsigned char *output = NULL; |
| 3896 | |
| 3897 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3898 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3899 | |
| 3900 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3901 | |
| 3902 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3903 | psa_set_key_algorithm( &attributes, alg ); |
| 3904 | psa_set_key_type( &attributes, key_type ); |
| 3905 | |
| 3906 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3907 | &key ) ); |
| 3908 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3909 | TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output, |
| 3910 | iv_length ) ); |
| 3911 | |
| 3912 | exit: |
| 3913 | psa_cipher_abort( &operation ); |
| 3914 | mbedtls_free( output ); |
| 3915 | psa_destroy_key( key ); |
| 3916 | PSA_DONE( ); |
| 3917 | } |
| 3918 | /* END_CASE */ |
| 3919 | |
| 3920 | /* BEGIN_CASE */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3921 | void cipher_alg_without_iv( int alg_arg, int key_type_arg, data_t *key_data, |
| 3922 | data_t *plaintext, data_t *ciphertext ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3923 | { |
| 3924 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3925 | psa_key_type_t key_type = key_type_arg; |
| 3926 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3927 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3928 | uint8_t iv[1] = { 0x5a }; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3929 | unsigned char *output = NULL; |
| 3930 | size_t output_buffer_size = 0; |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3931 | size_t output_length, length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3932 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3933 | |
| 3934 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3935 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3936 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3937 | TEST_LE_U( ciphertext->len, |
| 3938 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, plaintext->len ) ); |
| 3939 | 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] | 3940 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( plaintext->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3941 | TEST_LE_U( plaintext->len, |
| 3942 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ) ); |
| 3943 | TEST_LE_U( PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, ciphertext->len ), |
| 3944 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( ciphertext->len ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3945 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3946 | |
| 3947 | /* Set up key and output buffer */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3948 | psa_set_key_usage_flags( &attributes, |
| 3949 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3950 | psa_set_key_algorithm( &attributes, alg ); |
| 3951 | psa_set_key_type( &attributes, key_type ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3952 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3953 | &key ) ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3954 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3955 | plaintext->len ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3956 | ASSERT_ALLOC( output, output_buffer_size ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3957 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3958 | /* set_iv() is not allowed */ |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3959 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3960 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
| 3961 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3962 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3963 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3964 | PSA_ERROR_BAD_STATE ); |
| 3965 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 3966 | /* generate_iv() is not allowed */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3967 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3968 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3969 | &length ), |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3970 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3971 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3972 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3973 | &length ), |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 3974 | PSA_ERROR_BAD_STATE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3975 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3976 | /* Multipart encryption */ |
| 3977 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3978 | output_length = 0; |
| 3979 | length = ~0; |
| 3980 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3981 | plaintext->x, plaintext->len, |
| 3982 | output, output_buffer_size, |
| 3983 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 3984 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3985 | output_length += length; |
| 3986 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame^] | 3987 | mbedtls_buffer_offset( output, output_length ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3988 | output_buffer_size - output_length, |
| 3989 | &length ) ); |
| 3990 | output_length += length; |
| 3991 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3992 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 3993 | |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 3994 | /* Multipart encryption */ |
| 3995 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
| 3996 | output_length = 0; |
| 3997 | length = ~0; |
| 3998 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3999 | ciphertext->x, ciphertext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4000 | output, output_buffer_size, |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4001 | &length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4002 | TEST_LE_U( length, output_buffer_size ); |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4003 | output_length += length; |
| 4004 | PSA_ASSERT( psa_cipher_finish( &operation, |
Gilles Peskine | 42649d9 | 2022-11-23 14:15:57 +0100 | [diff] [blame^] | 4005 | mbedtls_buffer_offset( output, output_length ), |
Gilles Peskine | 286c314 | 2022-04-20 17:09:38 +0200 | [diff] [blame] | 4006 | output_buffer_size - output_length, |
| 4007 | &length ) ); |
| 4008 | output_length += length; |
| 4009 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
| 4010 | output, output_length ); |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4011 | |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4012 | /* One-shot encryption */ |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4013 | output_length = ~0; |
| 4014 | PSA_ASSERT( psa_cipher_encrypt( key, alg, plaintext->x, plaintext->len, |
| 4015 | output, output_buffer_size, |
| 4016 | &output_length ) ); |
| 4017 | ASSERT_COMPARE( ciphertext->x, ciphertext->len, |
| 4018 | output, output_length ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4019 | |
Gilles Peskine | 9e38f2c | 2022-04-20 17:07:52 +0200 | [diff] [blame] | 4020 | /* One-shot decryption */ |
| 4021 | output_length = ~0; |
| 4022 | PSA_ASSERT( psa_cipher_decrypt( key, alg, ciphertext->x, ciphertext->len, |
| 4023 | output, output_buffer_size, |
| 4024 | &output_length ) ); |
| 4025 | ASSERT_COMPARE( plaintext->x, plaintext->len, |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4026 | output, output_length ); |
| 4027 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4028 | exit: |
Neil Armstrong | 3ee335d | 2022-02-07 14:51:37 +0100 | [diff] [blame] | 4029 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4030 | mbedtls_free( output ); |
Gilles Peskine | 9b9b614 | 2022-04-20 16:55:03 +0200 | [diff] [blame] | 4031 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4032 | psa_destroy_key( key ); |
| 4033 | PSA_DONE( ); |
| 4034 | } |
| 4035 | /* END_CASE */ |
| 4036 | |
| 4037 | /* BEGIN_CASE */ |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 4038 | void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data ) |
| 4039 | { |
| 4040 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4041 | psa_algorithm_t alg = alg_arg; |
| 4042 | psa_key_type_t key_type = key_type_arg; |
| 4043 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4044 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 4045 | psa_status_t status; |
| 4046 | |
| 4047 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4048 | |
| 4049 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4050 | psa_set_key_algorithm( &attributes, alg ); |
| 4051 | psa_set_key_type( &attributes, key_type ); |
| 4052 | |
| 4053 | /* Usage of either of these two size macros would cause divide by zero |
| 4054 | * with incorrect key types previously. Input length should be irrelevant |
| 4055 | * here. */ |
| 4056 | TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ), |
| 4057 | 0 ); |
| 4058 | TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 ); |
| 4059 | |
| 4060 | |
| 4061 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4062 | &key ) ); |
| 4063 | |
| 4064 | /* Should fail due to invalid alg type (to support invalid key type). |
| 4065 | * Encrypt or decrypt will end up in the same place. */ |
| 4066 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 4067 | |
| 4068 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 4069 | |
| 4070 | exit: |
| 4071 | psa_cipher_abort( &operation ); |
| 4072 | psa_destroy_key( key ); |
| 4073 | PSA_DONE( ); |
| 4074 | } |
| 4075 | /* END_CASE */ |
| 4076 | |
| 4077 | /* BEGIN_CASE */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4078 | void cipher_encrypt_validation( int alg_arg, |
| 4079 | int key_type_arg, |
| 4080 | data_t *key_data, |
| 4081 | data_t *input ) |
| 4082 | { |
| 4083 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4084 | psa_key_type_t key_type = key_type_arg; |
| 4085 | psa_algorithm_t alg = alg_arg; |
| 4086 | size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); |
| 4087 | unsigned char *output1 = NULL; |
| 4088 | size_t output1_buffer_size = 0; |
| 4089 | size_t output1_length = 0; |
| 4090 | unsigned char *output2 = NULL; |
| 4091 | size_t output2_buffer_size = 0; |
| 4092 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4093 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4094 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4095 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4096 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4097 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4098 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4099 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4100 | psa_set_key_algorithm( &attributes, alg ); |
| 4101 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4102 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4103 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 4104 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4105 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4106 | ASSERT_ALLOC( output1, output1_buffer_size ); |
| 4107 | ASSERT_ALLOC( output2, output2_buffer_size ); |
| 4108 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4109 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4110 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4111 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 4112 | /* The one-shot cipher encryption uses generated iv so validating |
| 4113 | the output is not possible. Validating with multipart encryption. */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4114 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, |
| 4115 | output1_buffer_size, &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4116 | TEST_LE_U( output1_length, |
| 4117 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4118 | TEST_LE_U( output1_length, |
| 4119 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4120 | |
| 4121 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 4122 | PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4123 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4124 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4125 | input->x, input->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4126 | output2, output2_buffer_size, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4127 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4128 | TEST_LE_U( function_output_length, |
| 4129 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4130 | TEST_LE_U( function_output_length, |
| 4131 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4132 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4133 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4134 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 4135 | output2 + output2_length, |
| 4136 | output2_buffer_size - output2_length, |
| 4137 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4138 | TEST_LE_U( function_output_length, |
| 4139 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4140 | TEST_LE_U( function_output_length, |
| 4141 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4142 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4143 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4144 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4145 | ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, |
| 4146 | output2, output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4147 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4148 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4149 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4150 | mbedtls_free( output1 ); |
| 4151 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4152 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4153 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4154 | } |
| 4155 | /* END_CASE */ |
| 4156 | |
| 4157 | /* BEGIN_CASE */ |
| 4158 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4159 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4160 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4161 | int first_part_size_arg, |
| 4162 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4163 | data_t *expected_output, |
| 4164 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4165 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4166 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4167 | psa_key_type_t key_type = key_type_arg; |
| 4168 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4169 | psa_status_t status; |
| 4170 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4171 | size_t first_part_size = first_part_size_arg; |
| 4172 | size_t output1_length = output1_length_arg; |
| 4173 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4174 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4175 | size_t output_buffer_size = 0; |
| 4176 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4177 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4178 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4179 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4180 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4181 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4182 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4183 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4184 | psa_set_key_algorithm( &attributes, alg ); |
| 4185 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4186 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4187 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4188 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4189 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4190 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4191 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4192 | if( iv->len > 0 ) |
| 4193 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4194 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4195 | } |
| 4196 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4197 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4198 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4199 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4200 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4201 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4202 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 4203 | output, output_buffer_size, |
| 4204 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4205 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4206 | TEST_LE_U( function_output_length, |
| 4207 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4208 | TEST_LE_U( function_output_length, |
| 4209 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4210 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4211 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4212 | if( first_part_size < input->len ) |
| 4213 | { |
| 4214 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4215 | input->x + first_part_size, |
| 4216 | input->len - first_part_size, |
| 4217 | ( output_buffer_size == 0 ? NULL : |
| 4218 | output + total_output_length ), |
| 4219 | output_buffer_size - total_output_length, |
| 4220 | &function_output_length ) ); |
| 4221 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4222 | TEST_LE_U( function_output_length, |
| 4223 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4224 | alg, |
| 4225 | input->len - first_part_size ) ); |
| 4226 | TEST_LE_U( function_output_length, |
| 4227 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4228 | total_output_length += function_output_length; |
| 4229 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4230 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4231 | status = psa_cipher_finish( &operation, |
| 4232 | ( output_buffer_size == 0 ? NULL : |
| 4233 | output + total_output_length ), |
| 4234 | output_buffer_size - total_output_length, |
| 4235 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4236 | TEST_LE_U( function_output_length, |
| 4237 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4238 | TEST_LE_U( function_output_length, |
| 4239 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4240 | total_output_length += function_output_length; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4241 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4242 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4243 | if( expected_status == PSA_SUCCESS ) |
| 4244 | { |
| 4245 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 4246 | |
| 4247 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4248 | output, total_output_length ); |
| 4249 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4250 | |
| 4251 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4252 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4253 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4254 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4255 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4256 | } |
| 4257 | /* END_CASE */ |
| 4258 | |
| 4259 | /* BEGIN_CASE */ |
| 4260 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4261 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4262 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4263 | int first_part_size_arg, |
| 4264 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4265 | data_t *expected_output, |
| 4266 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4267 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4268 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4269 | psa_key_type_t key_type = key_type_arg; |
| 4270 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4271 | psa_status_t status; |
| 4272 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4273 | size_t first_part_size = first_part_size_arg; |
| 4274 | size_t output1_length = output1_length_arg; |
| 4275 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4276 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4277 | size_t output_buffer_size = 0; |
| 4278 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4279 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4280 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4281 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4282 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4283 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4284 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4285 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4286 | psa_set_key_algorithm( &attributes, alg ); |
| 4287 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4288 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4289 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4290 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4291 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4292 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4293 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4294 | if( iv->len > 0 ) |
| 4295 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4296 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4297 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4298 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4299 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 4300 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4301 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4302 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4303 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4304 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4305 | input->x, first_part_size, |
| 4306 | output, output_buffer_size, |
| 4307 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4308 | TEST_ASSERT( function_output_length == output1_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4309 | TEST_LE_U( function_output_length, |
| 4310 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4311 | TEST_LE_U( function_output_length, |
| 4312 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4313 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4314 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4315 | if( first_part_size < input->len ) |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4316 | { |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4317 | PSA_ASSERT( psa_cipher_update( &operation, |
| 4318 | input->x + first_part_size, |
| 4319 | input->len - first_part_size, |
| 4320 | ( output_buffer_size == 0 ? NULL : |
| 4321 | output + total_output_length ), |
| 4322 | output_buffer_size - total_output_length, |
| 4323 | &function_output_length ) ); |
| 4324 | TEST_ASSERT( function_output_length == output2_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4325 | TEST_LE_U( function_output_length, |
| 4326 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4327 | alg, |
| 4328 | input->len - first_part_size ) ); |
| 4329 | TEST_LE_U( function_output_length, |
| 4330 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4331 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4332 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4333 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4334 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 4335 | ( output_buffer_size == 0 ? NULL : |
| 4336 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 4337 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4338 | &function_output_length ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4339 | TEST_LE_U( function_output_length, |
| 4340 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4341 | TEST_LE_U( function_output_length, |
| 4342 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 4343 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4344 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4345 | |
| 4346 | if( expected_status == PSA_SUCCESS ) |
| 4347 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4348 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 4349 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4350 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4351 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4352 | } |
| 4353 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4354 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4355 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4356 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4357 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4358 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4359 | } |
| 4360 | /* END_CASE */ |
| 4361 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4362 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 4363 | void cipher_decrypt_fail( int alg_arg, |
| 4364 | int key_type_arg, |
| 4365 | data_t *key_data, |
| 4366 | data_t *iv, |
| 4367 | data_t *input_arg, |
| 4368 | int expected_status_arg ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4369 | { |
| 4370 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4371 | psa_status_t status; |
| 4372 | psa_key_type_t key_type = key_type_arg; |
| 4373 | psa_algorithm_t alg = alg_arg; |
| 4374 | psa_status_t expected_status = expected_status_arg; |
| 4375 | unsigned char *input = NULL; |
| 4376 | size_t input_buffer_size = 0; |
| 4377 | unsigned char *output = NULL; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4378 | unsigned char *output_multi = NULL; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4379 | size_t output_buffer_size = 0; |
| 4380 | size_t output_length = 0; |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4381 | size_t function_output_length; |
| 4382 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4383 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4384 | |
| 4385 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 4386 | { |
| 4387 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4388 | |
| 4389 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4390 | psa_set_key_algorithm( &attributes, alg ); |
| 4391 | psa_set_key_type( &attributes, key_type ); |
| 4392 | |
| 4393 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4394 | &key ) ); |
| 4395 | } |
| 4396 | |
| 4397 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4398 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4399 | if ( input_buffer_size > 0 ) |
| 4400 | { |
| 4401 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4402 | memcpy( input, iv->x, iv->len ); |
| 4403 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4404 | } |
| 4405 | |
| 4406 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4407 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4408 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4409 | /* Decrypt, one-short */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4410 | status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4411 | output_buffer_size, &output_length ); |
| 4412 | TEST_EQUAL( status, expected_status ); |
| 4413 | |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4414 | /* Decrypt, multi-part */ |
| 4415 | status = psa_cipher_decrypt_setup( &operation, key, alg ); |
| 4416 | if( status == PSA_SUCCESS ) |
| 4417 | { |
| 4418 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 4419 | input_arg->len ) + |
| 4420 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4421 | ASSERT_ALLOC( output_multi, output_buffer_size ); |
| 4422 | |
| 4423 | if( iv->len > 0 ) |
| 4424 | { |
| 4425 | status = psa_cipher_set_iv( &operation, iv->x, iv->len ); |
| 4426 | |
| 4427 | if( status != PSA_SUCCESS ) |
| 4428 | TEST_EQUAL( status, expected_status ); |
| 4429 | } |
| 4430 | |
| 4431 | if( status == PSA_SUCCESS ) |
| 4432 | { |
| 4433 | status = psa_cipher_update( &operation, |
| 4434 | input_arg->x, input_arg->len, |
| 4435 | output_multi, output_buffer_size, |
| 4436 | &function_output_length ); |
| 4437 | if( status == PSA_SUCCESS ) |
| 4438 | { |
| 4439 | output_length = function_output_length; |
| 4440 | |
| 4441 | status = psa_cipher_finish( &operation, |
| 4442 | output_multi + output_length, |
| 4443 | output_buffer_size - output_length, |
| 4444 | &function_output_length ); |
| 4445 | |
| 4446 | TEST_EQUAL( status, expected_status ); |
| 4447 | } |
| 4448 | else |
| 4449 | { |
| 4450 | TEST_EQUAL( status, expected_status ); |
| 4451 | } |
| 4452 | } |
| 4453 | else |
| 4454 | { |
| 4455 | TEST_EQUAL( status, expected_status ); |
| 4456 | } |
| 4457 | } |
| 4458 | else |
| 4459 | { |
| 4460 | TEST_EQUAL( status, expected_status ); |
| 4461 | } |
| 4462 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4463 | exit: |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4464 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4465 | mbedtls_free( input ); |
| 4466 | mbedtls_free( output ); |
Neil Armstrong | 66a479f | 2022-02-07 15:41:19 +0100 | [diff] [blame] | 4467 | mbedtls_free( output_multi ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4468 | psa_destroy_key( key ); |
| 4469 | PSA_DONE( ); |
| 4470 | } |
| 4471 | /* END_CASE */ |
| 4472 | |
| 4473 | /* BEGIN_CASE */ |
| 4474 | void cipher_decrypt( int alg_arg, |
| 4475 | int key_type_arg, |
| 4476 | data_t *key_data, |
| 4477 | data_t *iv, |
| 4478 | data_t *input_arg, |
| 4479 | data_t *expected_output ) |
| 4480 | { |
| 4481 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4482 | psa_key_type_t key_type = key_type_arg; |
| 4483 | psa_algorithm_t alg = alg_arg; |
| 4484 | unsigned char *input = NULL; |
| 4485 | size_t input_buffer_size = 0; |
| 4486 | unsigned char *output = NULL; |
| 4487 | size_t output_buffer_size = 0; |
| 4488 | size_t output_length = 0; |
| 4489 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4490 | |
| 4491 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4492 | |
| 4493 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4494 | psa_set_key_algorithm( &attributes, alg ); |
| 4495 | psa_set_key_type( &attributes, key_type ); |
| 4496 | |
| 4497 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 4498 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 4499 | if ( input_buffer_size > 0 ) |
| 4500 | { |
| 4501 | ASSERT_ALLOC( input, input_buffer_size ); |
| 4502 | memcpy( input, iv->x, iv->len ); |
| 4503 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 4504 | } |
| 4505 | |
| 4506 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 4507 | ASSERT_ALLOC( output, output_buffer_size ); |
| 4508 | |
| 4509 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4510 | &key ) ); |
| 4511 | |
| 4512 | PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 4513 | output_buffer_size, &output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4514 | TEST_LE_U( output_length, |
| 4515 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) ); |
| 4516 | TEST_LE_U( output_length, |
| 4517 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4518 | |
| 4519 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 4520 | output, output_length ); |
| 4521 | exit: |
| 4522 | mbedtls_free( input ); |
| 4523 | mbedtls_free( output ); |
| 4524 | psa_destroy_key( key ); |
| 4525 | PSA_DONE( ); |
| 4526 | } |
| 4527 | /* END_CASE */ |
| 4528 | |
| 4529 | /* BEGIN_CASE */ |
| 4530 | void cipher_verify_output( int alg_arg, |
| 4531 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4532 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4533 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4534 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4535 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4536 | psa_key_type_t key_type = key_type_arg; |
| 4537 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4538 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4539 | size_t output1_size = 0; |
| 4540 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4541 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4542 | size_t output2_size = 0; |
| 4543 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4544 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4545 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4546 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4547 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4548 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4549 | psa_set_key_algorithm( &attributes, alg ); |
| 4550 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4551 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4552 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4553 | &key ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4554 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4555 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4556 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4557 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, |
| 4558 | output1, output1_size, |
| 4559 | &output1_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4560 | TEST_LE_U( output1_length, |
| 4561 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 4562 | TEST_LE_U( output1_length, |
| 4563 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4564 | |
| 4565 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4566 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4567 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 4568 | PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length, |
| 4569 | output2, output2_size, |
| 4570 | &output2_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4571 | TEST_LE_U( output2_length, |
| 4572 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4573 | TEST_LE_U( output2_length, |
| 4574 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4575 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4576 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4577 | |
| 4578 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4579 | mbedtls_free( output1 ); |
| 4580 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4581 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4582 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4583 | } |
| 4584 | /* END_CASE */ |
| 4585 | |
| 4586 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 4587 | void cipher_verify_output_multipart( int alg_arg, |
| 4588 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4589 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4590 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4591 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4592 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4593 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4594 | psa_key_type_t key_type = key_type_arg; |
| 4595 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 4596 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4597 | unsigned char iv[16] = {0}; |
| 4598 | size_t iv_size = 16; |
| 4599 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4600 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4601 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4602 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4603 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4604 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4605 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4606 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 4607 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 4608 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4609 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4610 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4611 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4612 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4613 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4614 | psa_set_key_algorithm( &attributes, alg ); |
| 4615 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 4616 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4617 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4618 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4619 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4620 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 4621 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4622 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4623 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 4624 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4625 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 4626 | iv, iv_size, |
| 4627 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4628 | } |
| 4629 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4630 | 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] | 4631 | TEST_LE_U( output1_buffer_size, |
| 4632 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4633 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4634 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4635 | TEST_LE_U( first_part_size, input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4636 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4637 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 4638 | output1, output1_buffer_size, |
| 4639 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4640 | TEST_LE_U( function_output_length, |
| 4641 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4642 | TEST_LE_U( function_output_length, |
| 4643 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4644 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4645 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4646 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 4647 | input->x + first_part_size, |
| 4648 | input->len - first_part_size, |
| 4649 | output1, output1_buffer_size, |
| 4650 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4651 | TEST_LE_U( function_output_length, |
| 4652 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4653 | alg, |
| 4654 | input->len - first_part_size ) ); |
| 4655 | TEST_LE_U( function_output_length, |
| 4656 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4657 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4658 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4659 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 4660 | output1 + output1_length, |
| 4661 | output1_buffer_size - output1_length, |
| 4662 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4663 | TEST_LE_U( function_output_length, |
| 4664 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4665 | TEST_LE_U( function_output_length, |
| 4666 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4667 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4668 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4669 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4670 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4671 | output2_buffer_size = output1_length; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4672 | TEST_LE_U( output2_buffer_size, |
| 4673 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 4674 | TEST_LE_U( output2_buffer_size, |
| 4675 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4676 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4677 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 4678 | if( iv_length > 0 ) |
| 4679 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 4680 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 4681 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 4682 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4683 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4684 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 4685 | output2, output2_buffer_size, |
| 4686 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4687 | TEST_LE_U( function_output_length, |
| 4688 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 4689 | TEST_LE_U( function_output_length, |
| 4690 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4691 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4692 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4693 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 4694 | output1 + first_part_size, |
| 4695 | output1_length - first_part_size, |
| 4696 | output2, output2_buffer_size, |
| 4697 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4698 | TEST_LE_U( function_output_length, |
| 4699 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 4700 | alg, |
| 4701 | output1_length - first_part_size ) ); |
| 4702 | TEST_LE_U( function_output_length, |
| 4703 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4704 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 4705 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4706 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 4707 | output2 + output2_length, |
| 4708 | output2_buffer_size - output2_length, |
| 4709 | &function_output_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4710 | TEST_LE_U( function_output_length, |
| 4711 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 4712 | TEST_LE_U( function_output_length, |
| 4713 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 4714 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 4715 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4716 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4717 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4718 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4719 | |
| 4720 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 4721 | psa_cipher_abort( &operation1 ); |
| 4722 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 4723 | mbedtls_free( output1 ); |
| 4724 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4725 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4726 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 4727 | } |
| 4728 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 4729 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 4730 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4731 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4732 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4733 | data_t *nonce, |
| 4734 | data_t *additional_data, |
| 4735 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 4736 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4737 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4738 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4739 | psa_key_type_t key_type = key_type_arg; |
| 4740 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4741 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4742 | unsigned char *output_data = NULL; |
| 4743 | size_t output_size = 0; |
| 4744 | size_t output_length = 0; |
| 4745 | unsigned char *output_data2 = NULL; |
| 4746 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4747 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4748 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4749 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4750 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4751 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4752 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4753 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4754 | psa_set_key_algorithm( &attributes, alg ); |
| 4755 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4756 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4757 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4758 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4759 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4760 | key_bits = psa_get_key_bits( &attributes ); |
| 4761 | |
| 4762 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4763 | alg ); |
| 4764 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4765 | * should be exact. */ |
| 4766 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4767 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4768 | { |
| 4769 | TEST_EQUAL( output_size, |
| 4770 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4771 | TEST_LE_U( output_size, |
| 4772 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4773 | } |
| 4774 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4775 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 4776 | status = psa_aead_encrypt( key, alg, |
| 4777 | nonce->x, nonce->len, |
| 4778 | additional_data->x, |
| 4779 | additional_data->len, |
| 4780 | input_data->x, input_data->len, |
| 4781 | output_data, output_size, |
| 4782 | &output_length ); |
| 4783 | |
| 4784 | /* If the operation is not supported, just skip and not fail in case the |
| 4785 | * encryption involves a common limitation of cryptography hardwares and |
| 4786 | * an alternative implementation. */ |
| 4787 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4788 | { |
| 4789 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4790 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4791 | } |
| 4792 | |
| 4793 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4794 | |
| 4795 | if( PSA_SUCCESS == expected_result ) |
| 4796 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 4797 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4798 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4799 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4800 | * should be exact. */ |
| 4801 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4802 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 4803 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4804 | TEST_LE_U( input_data->len, |
| 4805 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 4806 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4807 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 4808 | nonce->x, nonce->len, |
| 4809 | additional_data->x, |
| 4810 | additional_data->len, |
| 4811 | output_data, output_length, |
| 4812 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 4813 | &output_length2 ), |
| 4814 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4815 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4816 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 4817 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4818 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4819 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4820 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4821 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4822 | mbedtls_free( output_data ); |
| 4823 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4824 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4825 | } |
| 4826 | /* END_CASE */ |
| 4827 | |
| 4828 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4829 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 4830 | int alg_arg, |
| 4831 | data_t *nonce, |
| 4832 | data_t *additional_data, |
| 4833 | data_t *input_data, |
| 4834 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4835 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4836 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4837 | psa_key_type_t key_type = key_type_arg; |
| 4838 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4839 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4840 | unsigned char *output_data = NULL; |
| 4841 | size_t output_size = 0; |
| 4842 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4843 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4844 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4845 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4846 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4847 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4848 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4849 | psa_set_key_algorithm( &attributes, alg ); |
| 4850 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4851 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4852 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4853 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4854 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4855 | key_bits = psa_get_key_bits( &attributes ); |
| 4856 | |
| 4857 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4858 | alg ); |
| 4859 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 4860 | * should be exact. */ |
| 4861 | TEST_EQUAL( output_size, |
| 4862 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4863 | TEST_LE_U( output_size, |
| 4864 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4865 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4866 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4867 | status = psa_aead_encrypt( key, alg, |
| 4868 | nonce->x, nonce->len, |
| 4869 | additional_data->x, additional_data->len, |
| 4870 | input_data->x, input_data->len, |
| 4871 | output_data, output_size, |
| 4872 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4873 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4874 | /* If the operation is not supported, just skip and not fail in case the |
| 4875 | * encryption involves a common limitation of cryptography hardwares and |
| 4876 | * an alternative implementation. */ |
| 4877 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4878 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4879 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4880 | 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] | 4881 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4882 | |
| 4883 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4884 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 4885 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4886 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4887 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4888 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4889 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4890 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4891 | } |
| 4892 | /* END_CASE */ |
| 4893 | |
| 4894 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 4895 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 4896 | int alg_arg, |
| 4897 | data_t *nonce, |
| 4898 | data_t *additional_data, |
| 4899 | data_t *input_data, |
| 4900 | data_t *expected_data, |
| 4901 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4902 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4903 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4904 | psa_key_type_t key_type = key_type_arg; |
| 4905 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4906 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4907 | unsigned char *output_data = NULL; |
| 4908 | size_t output_size = 0; |
| 4909 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4910 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 4911 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4912 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4913 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 4914 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4915 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 4916 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4917 | psa_set_key_algorithm( &attributes, alg ); |
| 4918 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4919 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 4920 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4921 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4922 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4923 | key_bits = psa_get_key_bits( &attributes ); |
| 4924 | |
| 4925 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 4926 | alg ); |
| 4927 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 4928 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 4929 | { |
| 4930 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 4931 | * should be exact. */ |
| 4932 | TEST_EQUAL( output_size, |
| 4933 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 4934 | TEST_LE_U( output_size, |
| 4935 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 4936 | } |
| 4937 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4938 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4939 | status = psa_aead_decrypt( key, alg, |
| 4940 | nonce->x, nonce->len, |
| 4941 | additional_data->x, |
| 4942 | additional_data->len, |
| 4943 | input_data->x, input_data->len, |
| 4944 | output_data, output_size, |
| 4945 | &output_length ); |
| 4946 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4947 | /* If the operation is not supported, just skip and not fail in case the |
| 4948 | * decryption involves a common limitation of cryptography hardwares and |
| 4949 | * an alternative implementation. */ |
| 4950 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4951 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4952 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4953 | 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] | 4954 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4955 | |
| 4956 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4957 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4958 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4959 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4960 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4961 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4962 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4963 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4964 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4965 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4966 | } |
| 4967 | /* END_CASE */ |
| 4968 | |
| 4969 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4970 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 4971 | int alg_arg, |
| 4972 | data_t *nonce, |
| 4973 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4974 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 4975 | int do_set_lengths, |
| 4976 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4977 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4978 | size_t ad_part_len = 0; |
| 4979 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4980 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4981 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4982 | 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] | 4983 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4984 | mbedtls_test_set_step( ad_part_len ); |
| 4985 | |
| 4986 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4987 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4988 | if( ad_part_len & 0x01 ) |
| 4989 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4990 | else |
| 4991 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4992 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4993 | |
| 4994 | /* Split ad into length(ad_part_len) parts. */ |
| 4995 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4996 | alg_arg, nonce, |
| 4997 | additional_data, |
| 4998 | ad_part_len, |
| 4999 | input_data, -1, |
| 5000 | set_lengths_method, |
| 5001 | expected_output, |
| 5002 | 1, 0 ) ) |
| 5003 | break; |
| 5004 | |
| 5005 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5006 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 5007 | |
| 5008 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5009 | alg_arg, nonce, |
| 5010 | additional_data, |
| 5011 | ad_part_len, |
| 5012 | input_data, -1, |
| 5013 | set_lengths_method, |
| 5014 | expected_output, |
| 5015 | 1, 1 ) ) |
| 5016 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5017 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5018 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5019 | 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] | 5020 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5021 | /* Split data into length(data_part_len) parts. */ |
| 5022 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 5023 | |
| 5024 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5025 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5026 | if( data_part_len & 0x01 ) |
| 5027 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5028 | else |
| 5029 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5030 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5031 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5032 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5033 | alg_arg, nonce, |
| 5034 | additional_data, -1, |
| 5035 | input_data, data_part_len, |
| 5036 | set_lengths_method, |
| 5037 | expected_output, |
| 5038 | 1, 0 ) ) |
| 5039 | break; |
| 5040 | |
| 5041 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 5042 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 5043 | |
| 5044 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5045 | alg_arg, nonce, |
| 5046 | additional_data, -1, |
| 5047 | input_data, data_part_len, |
| 5048 | set_lengths_method, |
| 5049 | expected_output, |
| 5050 | 1, 1 ) ) |
| 5051 | break; |
| 5052 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5053 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5054 | /* Goto is required to silence warnings about unused labels, as we |
| 5055 | * don't actually do any test assertions in this function. */ |
| 5056 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5057 | } |
| 5058 | /* END_CASE */ |
| 5059 | |
| 5060 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5061 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 5062 | int alg_arg, |
| 5063 | data_t *nonce, |
| 5064 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5065 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 5066 | int do_set_lengths, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5067 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5068 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5069 | size_t ad_part_len = 0; |
| 5070 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5071 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5072 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5073 | 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] | 5074 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5075 | /* Split ad into length(ad_part_len) parts. */ |
| 5076 | mbedtls_test_set_step( ad_part_len ); |
| 5077 | |
| 5078 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5079 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5080 | if( ad_part_len & 0x01 ) |
| 5081 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5082 | else |
| 5083 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5084 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5085 | |
| 5086 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5087 | alg_arg, nonce, |
| 5088 | additional_data, |
| 5089 | ad_part_len, |
| 5090 | input_data, -1, |
| 5091 | set_lengths_method, |
| 5092 | expected_output, |
| 5093 | 0, 0 ) ) |
| 5094 | break; |
| 5095 | |
| 5096 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 5097 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 5098 | |
| 5099 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5100 | alg_arg, nonce, |
| 5101 | additional_data, |
| 5102 | ad_part_len, |
| 5103 | input_data, -1, |
| 5104 | set_lengths_method, |
| 5105 | expected_output, |
| 5106 | 0, 1 ) ) |
| 5107 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5108 | } |
| 5109 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5110 | 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] | 5111 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5112 | /* Split data into length(data_part_len) parts. */ |
| 5113 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 5114 | |
| 5115 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5116 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5117 | if( data_part_len & 0x01 ) |
| 5118 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 5119 | else |
| 5120 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5121 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 5122 | |
| 5123 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5124 | alg_arg, nonce, |
| 5125 | additional_data, -1, |
| 5126 | input_data, data_part_len, |
| 5127 | set_lengths_method, |
| 5128 | expected_output, |
| 5129 | 0, 0 ) ) |
| 5130 | break; |
| 5131 | |
| 5132 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 5133 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 5134 | |
| 5135 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 5136 | alg_arg, nonce, |
| 5137 | additional_data, -1, |
| 5138 | input_data, data_part_len, |
| 5139 | set_lengths_method, |
| 5140 | expected_output, |
| 5141 | 0, 1 ) ) |
| 5142 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5143 | } |
| 5144 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 5145 | /* Goto is required to silence warnings about unused labels, as we |
| 5146 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 5147 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 5148 | } |
| 5149 | /* END_CASE */ |
| 5150 | |
| 5151 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5152 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 5153 | int alg_arg, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5154 | int nonce_length, |
| 5155 | int expected_nonce_length_arg, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5156 | data_t *additional_data, |
| 5157 | data_t *input_data, |
| 5158 | int expected_status_arg ) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5159 | { |
| 5160 | |
| 5161 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5162 | psa_key_type_t key_type = key_type_arg; |
| 5163 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5164 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5165 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5166 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5167 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5168 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5169 | size_t actual_nonce_length = 0; |
| 5170 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 5171 | unsigned char *output = NULL; |
| 5172 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5173 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5174 | size_t ciphertext_size = 0; |
| 5175 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5176 | size_t tag_length = 0; |
| 5177 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5178 | |
| 5179 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5180 | |
| 5181 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5182 | psa_set_key_algorithm( & attributes, alg ); |
| 5183 | psa_set_key_type( & attributes, key_type ); |
| 5184 | |
| 5185 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5186 | &key ) ); |
| 5187 | |
| 5188 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5189 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5190 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5191 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5192 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5193 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5194 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5195 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5196 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5197 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5198 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5199 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5200 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5201 | |
| 5202 | /* If the operation is not supported, just skip and not fail in case the |
| 5203 | * encryption involves a common limitation of cryptography hardwares and |
| 5204 | * an alternative implementation. */ |
| 5205 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5206 | { |
| 5207 | 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] | 5208 | 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] | 5209 | } |
| 5210 | |
| 5211 | PSA_ASSERT( status ); |
| 5212 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5213 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5214 | nonce_length, |
| 5215 | &actual_nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5216 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5217 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5218 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5219 | TEST_EQUAL( actual_nonce_length, expected_nonce_length ); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 5220 | |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 5221 | if( expected_status == PSA_SUCCESS ) |
| 5222 | TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type, |
| 5223 | alg ) ); |
| 5224 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5225 | TEST_LE_U( actual_nonce_length, PSA_AEAD_NONCE_MAX_SIZE ); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 5226 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5227 | if( expected_status == PSA_SUCCESS ) |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5228 | { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5229 | /* Ensure we can still complete operation. */ |
Paul Elliott | d79c5c5 | 2021-10-06 21:49:41 +0100 | [diff] [blame] | 5230 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5231 | input_data->len ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5232 | |
| 5233 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5234 | additional_data->len ) ); |
| 5235 | |
| 5236 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5237 | output, output_size, |
| 5238 | &ciphertext_length ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5239 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5240 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5241 | &ciphertext_length, tag_buffer, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 5242 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5243 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5244 | |
| 5245 | exit: |
| 5246 | psa_destroy_key( key ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 5247 | mbedtls_free( output ); |
| 5248 | mbedtls_free( ciphertext ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 5249 | psa_aead_abort( &operation ); |
| 5250 | PSA_DONE( ); |
| 5251 | } |
| 5252 | /* END_CASE */ |
| 5253 | |
| 5254 | /* BEGIN_CASE */ |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5255 | void aead_multipart_set_nonce( int key_type_arg, data_t *key_data, |
| 5256 | int alg_arg, |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5257 | int nonce_length_arg, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5258 | int set_lengths_method_arg, |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5259 | data_t *additional_data, |
| 5260 | data_t *input_data, |
| 5261 | int expected_status_arg ) |
| 5262 | { |
| 5263 | |
| 5264 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5265 | psa_key_type_t key_type = key_type_arg; |
| 5266 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5267 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5268 | uint8_t *nonce_buffer = NULL; |
| 5269 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5270 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5271 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5272 | unsigned char *output = NULL; |
| 5273 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5274 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5275 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5276 | size_t ciphertext_size = 0; |
| 5277 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5278 | size_t tag_length = 0; |
| 5279 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5280 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5281 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5282 | |
| 5283 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5284 | |
| 5285 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5286 | psa_set_key_algorithm( &attributes, alg ); |
| 5287 | psa_set_key_type( &attributes, key_type ); |
| 5288 | |
| 5289 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5290 | &key ) ); |
| 5291 | |
| 5292 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5293 | |
| 5294 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5295 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5296 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5297 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5298 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5299 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5300 | TEST_LE_U( ciphertext_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5301 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5302 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5303 | |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5304 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5305 | |
| 5306 | /* If the operation is not supported, just skip and not fail in case the |
| 5307 | * encryption involves a common limitation of cryptography hardwares and |
| 5308 | * an alternative implementation. */ |
| 5309 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5310 | { |
| 5311 | 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] | 5312 | 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] | 5313 | } |
| 5314 | |
| 5315 | PSA_ASSERT( status ); |
| 5316 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5317 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
| 5318 | if( nonce_length_arg == -1 ) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5319 | { |
Paul Elliott | 5e69aa5 | 2021-08-25 17:24:37 +0100 | [diff] [blame] | 5320 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 5321 | ASSERT_ALLOC( nonce_buffer, 4 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5322 | nonce_length = 0; |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5323 | } |
| 5324 | else |
| 5325 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5326 | /* If length is zero, then this will return NULL. */ |
| 5327 | nonce_length = ( size_t ) nonce_length_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5328 | ASSERT_ALLOC( nonce_buffer, nonce_length ); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5329 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5330 | if( nonce_buffer ) |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5331 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 5332 | for( index = 0; index < nonce_length - 1; ++index ) |
| 5333 | { |
| 5334 | nonce_buffer[index] = 'a' + index; |
| 5335 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 5336 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5337 | } |
| 5338 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5339 | if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
| 5340 | { |
| 5341 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5342 | input_data->len ) ); |
| 5343 | } |
| 5344 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5345 | status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5346 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 5347 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5348 | |
| 5349 | if( expected_status == PSA_SUCCESS ) |
| 5350 | { |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5351 | if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
| 5352 | { |
| 5353 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5354 | input_data->len ) ); |
| 5355 | } |
| 5356 | if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 5357 | expected_status = PSA_ERROR_BAD_STATE; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5358 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5359 | /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */ |
| 5360 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5361 | additional_data->len ), |
| 5362 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5363 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5364 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5365 | output, output_size, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5366 | &ciphertext_length ), |
| 5367 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5368 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5369 | TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5370 | &ciphertext_length, tag_buffer, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 5371 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ), |
| 5372 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5373 | } |
| 5374 | |
| 5375 | exit: |
| 5376 | psa_destroy_key( key ); |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 5377 | mbedtls_free( output ); |
| 5378 | mbedtls_free( ciphertext ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 5379 | mbedtls_free( nonce_buffer ); |
| 5380 | psa_aead_abort( &operation ); |
| 5381 | PSA_DONE( ); |
| 5382 | } |
| 5383 | /* END_CASE */ |
| 5384 | |
| 5385 | /* BEGIN_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5386 | void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data, |
| 5387 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5388 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5389 | data_t *nonce, |
| 5390 | data_t *additional_data, |
| 5391 | data_t *input_data, |
| 5392 | int expected_status_arg ) |
| 5393 | { |
| 5394 | |
| 5395 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5396 | psa_key_type_t key_type = key_type_arg; |
| 5397 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5398 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5399 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5400 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5401 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5402 | unsigned char *output = NULL; |
| 5403 | unsigned char *ciphertext = NULL; |
| 5404 | size_t output_size = output_size_arg; |
| 5405 | size_t ciphertext_size = 0; |
| 5406 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5407 | size_t tag_length = 0; |
| 5408 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5409 | |
| 5410 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5411 | |
| 5412 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5413 | psa_set_key_algorithm( &attributes, alg ); |
| 5414 | psa_set_key_type( &attributes, key_type ); |
| 5415 | |
| 5416 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5417 | &key ) ); |
| 5418 | |
| 5419 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5420 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5421 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5422 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5423 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5424 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5425 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5426 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5427 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5428 | |
| 5429 | /* If the operation is not supported, just skip and not fail in case the |
| 5430 | * encryption involves a common limitation of cryptography hardwares and |
| 5431 | * an alternative implementation. */ |
| 5432 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5433 | { |
| 5434 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5435 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5436 | } |
| 5437 | |
| 5438 | PSA_ASSERT( status ); |
| 5439 | |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 5440 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5441 | input_data->len ) ); |
| 5442 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5443 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5444 | |
| 5445 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5446 | additional_data->len ) ); |
| 5447 | |
| 5448 | status = psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5449 | output, output_size, &ciphertext_length ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5450 | |
| 5451 | TEST_EQUAL( status, expected_status ); |
| 5452 | |
| 5453 | if( expected_status == PSA_SUCCESS ) |
| 5454 | { |
| 5455 | /* Ensure we can still complete operation. */ |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5456 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 5457 | &ciphertext_length, tag_buffer, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5458 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 5459 | } |
| 5460 | |
| 5461 | exit: |
| 5462 | psa_destroy_key( key ); |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 5463 | mbedtls_free( output ); |
| 5464 | mbedtls_free( ciphertext ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5465 | psa_aead_abort( &operation ); |
| 5466 | PSA_DONE( ); |
| 5467 | } |
| 5468 | /* END_CASE */ |
| 5469 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5470 | /* BEGIN_CASE */ |
| 5471 | void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data, |
| 5472 | int alg_arg, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5473 | int finish_ciphertext_size_arg, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5474 | int tag_size_arg, |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5475 | data_t *nonce, |
| 5476 | data_t *additional_data, |
| 5477 | data_t *input_data, |
| 5478 | int expected_status_arg ) |
| 5479 | { |
| 5480 | |
| 5481 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5482 | psa_key_type_t key_type = key_type_arg; |
| 5483 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5484 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5485 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5486 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5487 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5488 | unsigned char *ciphertext = NULL; |
| 5489 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5490 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5491 | size_t ciphertext_size = 0; |
| 5492 | size_t ciphertext_length = 0; |
| 5493 | size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5494 | size_t tag_size = ( size_t ) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5495 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5496 | |
| 5497 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5498 | |
| 5499 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5500 | psa_set_key_algorithm( &attributes, alg ); |
| 5501 | psa_set_key_type( &attributes, key_type ); |
| 5502 | |
| 5503 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5504 | &key ) ); |
| 5505 | |
| 5506 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5507 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5508 | 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] | 5509 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5510 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5511 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5512 | ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5513 | |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5514 | ASSERT_ALLOC( tag_buffer, tag_size ); |
| 5515 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5516 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5517 | |
| 5518 | /* If the operation is not supported, just skip and not fail in case the |
| 5519 | * encryption involves a common limitation of cryptography hardwares and |
| 5520 | * an alternative implementation. */ |
| 5521 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5522 | { |
| 5523 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5524 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5525 | } |
| 5526 | |
| 5527 | PSA_ASSERT( status ); |
| 5528 | |
| 5529 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5530 | |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 5531 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5532 | input_data->len ) ); |
| 5533 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5534 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5535 | additional_data->len ) ); |
| 5536 | |
| 5537 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5538 | ciphertext, ciphertext_size, &ciphertext_length ) ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5539 | |
| 5540 | /* Ensure we can still complete operation. */ |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5541 | status = psa_aead_finish( &operation, finish_ciphertext, |
| 5542 | finish_ciphertext_size, |
| 5543 | &ciphertext_length, tag_buffer, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 5544 | tag_size, &tag_length ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5545 | |
| 5546 | TEST_EQUAL( status, expected_status ); |
| 5547 | |
| 5548 | exit: |
| 5549 | psa_destroy_key( key ); |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 5550 | mbedtls_free( ciphertext ); |
| 5551 | mbedtls_free( finish_ciphertext ); |
Paul Elliott | 4a76088 | 2021-09-20 09:42:21 +0100 | [diff] [blame] | 5552 | mbedtls_free( tag_buffer ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 5553 | psa_aead_abort( &operation ); |
| 5554 | PSA_DONE( ); |
| 5555 | } |
| 5556 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 5557 | |
| 5558 | /* BEGIN_CASE */ |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5559 | void aead_multipart_verify( int key_type_arg, data_t *key_data, |
| 5560 | int alg_arg, |
| 5561 | data_t *nonce, |
| 5562 | data_t *additional_data, |
| 5563 | data_t *input_data, |
| 5564 | data_t *tag, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5565 | int tag_usage_arg, |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5566 | int expected_setup_status_arg, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5567 | int expected_status_arg ) |
| 5568 | { |
| 5569 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5570 | psa_key_type_t key_type = key_type_arg; |
| 5571 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5572 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5573 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5574 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5575 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5576 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5577 | unsigned char *plaintext = NULL; |
| 5578 | unsigned char *finish_plaintext = NULL; |
| 5579 | size_t plaintext_size = 0; |
| 5580 | size_t plaintext_length = 0; |
| 5581 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 5582 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5583 | unsigned char *tag_buffer = NULL; |
| 5584 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5585 | |
| 5586 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5587 | |
| 5588 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5589 | psa_set_key_algorithm( &attributes, alg ); |
| 5590 | psa_set_key_type( &attributes, key_type ); |
| 5591 | |
| 5592 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5593 | &key ) ); |
| 5594 | |
| 5595 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5596 | |
| 5597 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 5598 | input_data->len ); |
| 5599 | |
| 5600 | ASSERT_ALLOC( plaintext, plaintext_size ); |
| 5601 | |
| 5602 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 5603 | |
| 5604 | ASSERT_ALLOC( finish_plaintext, verify_plaintext_size ); |
| 5605 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5606 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5607 | |
| 5608 | /* If the operation is not supported, just skip and not fail in case the |
| 5609 | * encryption involves a common limitation of cryptography hardwares and |
| 5610 | * an alternative implementation. */ |
| 5611 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 5612 | { |
| 5613 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 5614 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 5615 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5616 | TEST_EQUAL( status, expected_setup_status ); |
| 5617 | |
| 5618 | if( status != PSA_SUCCESS ) |
| 5619 | goto exit; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5620 | |
| 5621 | PSA_ASSERT( status ); |
| 5622 | |
| 5623 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5624 | |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5625 | status = psa_aead_set_lengths( &operation, additional_data->len, |
| 5626 | input_data->len ); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 5627 | PSA_ASSERT( status ); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 5628 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5629 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5630 | additional_data->len ) ); |
| 5631 | |
| 5632 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5633 | input_data->len, |
| 5634 | plaintext, plaintext_size, |
| 5635 | &plaintext_length ) ); |
| 5636 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5637 | if( tag_usage == USE_GIVEN_TAG ) |
| 5638 | { |
| 5639 | tag_buffer = tag->x; |
| 5640 | tag_size = tag->len; |
| 5641 | } |
| 5642 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5643 | status = psa_aead_verify( &operation, finish_plaintext, |
| 5644 | verify_plaintext_size, |
| 5645 | &plaintext_length, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 5646 | tag_buffer, tag_size ); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5647 | |
| 5648 | TEST_EQUAL( status, expected_status ); |
| 5649 | |
| 5650 | exit: |
| 5651 | psa_destroy_key( key ); |
| 5652 | mbedtls_free( plaintext ); |
| 5653 | mbedtls_free( finish_plaintext ); |
| 5654 | psa_aead_abort( &operation ); |
| 5655 | PSA_DONE( ); |
| 5656 | } |
| 5657 | /* END_CASE */ |
| 5658 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 5659 | /* BEGIN_CASE */ |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5660 | void aead_multipart_setup( int key_type_arg, data_t *key_data, |
| 5661 | int alg_arg, int expected_status_arg ) |
| 5662 | { |
| 5663 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5664 | psa_key_type_t key_type = key_type_arg; |
| 5665 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5666 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5667 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5668 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 5669 | psa_status_t expected_status = expected_status_arg; |
| 5670 | |
| 5671 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5672 | |
| 5673 | psa_set_key_usage_flags( &attributes, |
| 5674 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5675 | psa_set_key_algorithm( &attributes, alg ); |
| 5676 | psa_set_key_type( &attributes, key_type ); |
| 5677 | |
| 5678 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5679 | &key ) ); |
| 5680 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5681 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 5682 | |
| 5683 | TEST_EQUAL( status, expected_status ); |
| 5684 | |
| 5685 | psa_aead_abort( &operation ); |
| 5686 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 5687 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 5688 | |
| 5689 | TEST_EQUAL(status, expected_status ); |
| 5690 | |
| 5691 | exit: |
| 5692 | psa_destroy_key( key ); |
| 5693 | psa_aead_abort( &operation ); |
| 5694 | PSA_DONE( ); |
| 5695 | } |
| 5696 | /* END_CASE */ |
| 5697 | |
| 5698 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5699 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 5700 | int alg_arg, |
| 5701 | data_t *nonce, |
| 5702 | data_t *additional_data, |
| 5703 | data_t *input_data ) |
| 5704 | { |
| 5705 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5706 | psa_key_type_t key_type = key_type_arg; |
| 5707 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 5708 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5709 | unsigned char *output_data = NULL; |
| 5710 | unsigned char *final_data = NULL; |
| 5711 | size_t output_size = 0; |
| 5712 | size_t finish_output_size = 0; |
| 5713 | size_t output_length = 0; |
| 5714 | size_t key_bits = 0; |
| 5715 | size_t tag_length = 0; |
| 5716 | size_t tag_size = 0; |
| 5717 | size_t nonce_length = 0; |
| 5718 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 5719 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 5720 | size_t output_part_length = 0; |
| 5721 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5722 | |
| 5723 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5724 | |
| 5725 | psa_set_key_usage_flags( & attributes, |
| 5726 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5727 | psa_set_key_algorithm( & attributes, alg ); |
| 5728 | psa_set_key_type( & attributes, key_type ); |
| 5729 | |
| 5730 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5731 | &key ) ); |
| 5732 | |
| 5733 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5734 | key_bits = psa_get_key_bits( &attributes ); |
| 5735 | |
| 5736 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 5737 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5738 | TEST_LE_U( tag_length, PSA_AEAD_TAG_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5739 | |
| 5740 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 5741 | |
| 5742 | ASSERT_ALLOC( output_data, output_size ); |
| 5743 | |
| 5744 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 5745 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 5746 | TEST_LE_U( finish_output_size, PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5747 | |
| 5748 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 5749 | |
| 5750 | /* Test all operations error without calling setup first. */ |
| 5751 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5752 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5753 | PSA_ERROR_BAD_STATE ); |
| 5754 | |
| 5755 | psa_aead_abort( &operation ); |
| 5756 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5757 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5758 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5759 | &nonce_length ), |
| 5760 | PSA_ERROR_BAD_STATE ); |
| 5761 | |
| 5762 | psa_aead_abort( &operation ); |
| 5763 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5764 | /* ------------------------------------------------------- */ |
| 5765 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5766 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5767 | input_data->len ), |
| 5768 | PSA_ERROR_BAD_STATE ); |
| 5769 | |
| 5770 | psa_aead_abort( &operation ); |
| 5771 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5772 | /* ------------------------------------------------------- */ |
| 5773 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5774 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5775 | additional_data->len ), |
| 5776 | PSA_ERROR_BAD_STATE ); |
| 5777 | |
| 5778 | psa_aead_abort( &operation ); |
| 5779 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5780 | /* ------------------------------------------------------- */ |
| 5781 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5782 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5783 | input_data->len, output_data, |
| 5784 | output_size, &output_length ), |
| 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_finish( &operation, final_data, |
| 5792 | finish_output_size, |
| 5793 | &output_part_length, |
| 5794 | tag_buffer, tag_length, |
| 5795 | &tag_size ), |
| 5796 | PSA_ERROR_BAD_STATE ); |
| 5797 | |
| 5798 | psa_aead_abort( &operation ); |
| 5799 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5800 | /* ------------------------------------------------------- */ |
| 5801 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5802 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5803 | finish_output_size, |
| 5804 | &output_part_length, |
| 5805 | tag_buffer, |
| 5806 | tag_length ), |
| 5807 | PSA_ERROR_BAD_STATE ); |
| 5808 | |
| 5809 | psa_aead_abort( &operation ); |
| 5810 | |
| 5811 | /* Test for double setups. */ |
| 5812 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5813 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5814 | |
| 5815 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5816 | PSA_ERROR_BAD_STATE ); |
| 5817 | |
| 5818 | psa_aead_abort( &operation ); |
| 5819 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 5820 | /* ------------------------------------------------------- */ |
| 5821 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5822 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5823 | |
| 5824 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5825 | PSA_ERROR_BAD_STATE ); |
| 5826 | |
| 5827 | psa_aead_abort( &operation ); |
| 5828 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5829 | /* ------------------------------------------------------- */ |
| 5830 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5831 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5832 | |
| 5833 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 5834 | PSA_ERROR_BAD_STATE ); |
| 5835 | |
| 5836 | psa_aead_abort( &operation ); |
| 5837 | |
| 5838 | /* ------------------------------------------------------- */ |
| 5839 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5840 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5841 | |
| 5842 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 5843 | PSA_ERROR_BAD_STATE ); |
| 5844 | |
| 5845 | psa_aead_abort( &operation ); |
| 5846 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5847 | /* Test for not setting a nonce. */ |
| 5848 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5849 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5850 | |
| 5851 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5852 | additional_data->len ), |
| 5853 | PSA_ERROR_BAD_STATE ); |
| 5854 | |
| 5855 | psa_aead_abort( &operation ); |
| 5856 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5857 | /* ------------------------------------------------------- */ |
| 5858 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 5859 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5860 | |
| 5861 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5862 | input_data->len, output_data, |
| 5863 | output_size, &output_length ), |
| 5864 | PSA_ERROR_BAD_STATE ); |
| 5865 | |
| 5866 | psa_aead_abort( &operation ); |
| 5867 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5868 | /* ------------------------------------------------------- */ |
| 5869 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5870 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5871 | |
| 5872 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5873 | finish_output_size, |
| 5874 | &output_part_length, |
| 5875 | tag_buffer, tag_length, |
| 5876 | &tag_size ), |
| 5877 | PSA_ERROR_BAD_STATE ); |
| 5878 | |
| 5879 | psa_aead_abort( &operation ); |
| 5880 | |
| 5881 | /* ------------------------------------------------------- */ |
| 5882 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 5883 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5884 | |
| 5885 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5886 | finish_output_size, |
| 5887 | &output_part_length, |
| 5888 | tag_buffer, |
| 5889 | tag_length ), |
| 5890 | PSA_ERROR_BAD_STATE ); |
| 5891 | |
| 5892 | psa_aead_abort( &operation ); |
| 5893 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5894 | /* Test for double setting nonce. */ |
| 5895 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5896 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5897 | |
| 5898 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5899 | |
| 5900 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5901 | PSA_ERROR_BAD_STATE ); |
| 5902 | |
| 5903 | psa_aead_abort( &operation ); |
| 5904 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5905 | /* Test for double generating nonce. */ |
| 5906 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5907 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5908 | |
| 5909 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5910 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5911 | &nonce_length ) ); |
| 5912 | |
| 5913 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5914 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5915 | &nonce_length ), |
| 5916 | PSA_ERROR_BAD_STATE ); |
| 5917 | |
| 5918 | |
| 5919 | psa_aead_abort( &operation ); |
| 5920 | |
| 5921 | /* Test for generate nonce then set and vice versa */ |
| 5922 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5923 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5924 | |
| 5925 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5926 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5927 | &nonce_length ) ); |
| 5928 | |
| 5929 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5930 | PSA_ERROR_BAD_STATE ); |
| 5931 | |
| 5932 | psa_aead_abort( &operation ); |
| 5933 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5934 | /* Test for generating nonce after calling set lengths */ |
| 5935 | |
| 5936 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5937 | |
| 5938 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5939 | input_data->len ) ); |
| 5940 | |
| 5941 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5942 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5943 | &nonce_length ) ); |
| 5944 | |
| 5945 | psa_aead_abort( &operation ); |
| 5946 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5947 | /* 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] | 5948 | |
| 5949 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5950 | |
| 5951 | if( operation.alg == PSA_ALG_CCM ) |
| 5952 | { |
| 5953 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5954 | input_data->len ), |
| 5955 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5956 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5957 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5958 | &nonce_length ), |
| 5959 | PSA_ERROR_BAD_STATE ); |
| 5960 | } |
| 5961 | else |
| 5962 | { |
| 5963 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5964 | input_data->len ) ); |
| 5965 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5966 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5967 | &nonce_length ) ); |
| 5968 | } |
| 5969 | |
| 5970 | psa_aead_abort( &operation ); |
| 5971 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5972 | /* 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] | 5973 | #if SIZE_MAX > UINT32_MAX |
| 5974 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5975 | |
| 5976 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 5977 | { |
| 5978 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5979 | input_data->len ), |
| 5980 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5981 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5982 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5983 | &nonce_length ), |
| 5984 | PSA_ERROR_BAD_STATE ); |
| 5985 | } |
| 5986 | else |
| 5987 | { |
| 5988 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5989 | input_data->len ) ); |
| 5990 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5991 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5992 | &nonce_length ) ); |
| 5993 | } |
| 5994 | |
| 5995 | psa_aead_abort( &operation ); |
| 5996 | #endif |
| 5997 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5998 | /* 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] | 5999 | |
| 6000 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6001 | |
| 6002 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6003 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6004 | &nonce_length ) ); |
| 6005 | |
| 6006 | if( operation.alg == PSA_ALG_CCM ) |
| 6007 | { |
| 6008 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6009 | input_data->len ), |
| 6010 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6011 | } |
| 6012 | else |
| 6013 | { |
| 6014 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6015 | input_data->len ) ); |
| 6016 | } |
| 6017 | |
| 6018 | psa_aead_abort( &operation ); |
| 6019 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6020 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 6021 | /* Test for setting nonce after calling set lengths */ |
| 6022 | |
| 6023 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6024 | |
| 6025 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6026 | input_data->len ) ); |
| 6027 | |
| 6028 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6029 | |
| 6030 | psa_aead_abort( &operation ); |
| 6031 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6032 | /* 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] | 6033 | |
| 6034 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6035 | |
| 6036 | if( operation.alg == PSA_ALG_CCM ) |
| 6037 | { |
| 6038 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6039 | input_data->len ), |
| 6040 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6041 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6042 | PSA_ERROR_BAD_STATE ); |
| 6043 | } |
| 6044 | else |
| 6045 | { |
| 6046 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6047 | input_data->len ) ); |
| 6048 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6049 | } |
| 6050 | |
| 6051 | psa_aead_abort( &operation ); |
| 6052 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 6053 | /* 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] | 6054 | #if SIZE_MAX > UINT32_MAX |
| 6055 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6056 | |
| 6057 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 6058 | { |
| 6059 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6060 | input_data->len ), |
| 6061 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6062 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6063 | PSA_ERROR_BAD_STATE ); |
| 6064 | } |
| 6065 | else |
| 6066 | { |
| 6067 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 6068 | input_data->len ) ); |
| 6069 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6070 | } |
| 6071 | |
| 6072 | psa_aead_abort( &operation ); |
| 6073 | #endif |
| 6074 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6075 | /* 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] | 6076 | |
| 6077 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6078 | |
| 6079 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6080 | |
| 6081 | if( operation.alg == PSA_ALG_CCM ) |
| 6082 | { |
| 6083 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6084 | input_data->len ), |
| 6085 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6086 | } |
| 6087 | else |
| 6088 | { |
| 6089 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 6090 | input_data->len ) ); |
| 6091 | } |
| 6092 | |
| 6093 | psa_aead_abort( &operation ); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6094 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6095 | /* 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] | 6096 | #if SIZE_MAX > UINT32_MAX |
| 6097 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6098 | |
| 6099 | if( operation.alg == PSA_ALG_GCM ) |
| 6100 | { |
| 6101 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6102 | SIZE_MAX ), |
| 6103 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6104 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 6105 | PSA_ERROR_BAD_STATE ); |
| 6106 | } |
| 6107 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6108 | { |
| 6109 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6110 | SIZE_MAX ) ); |
| 6111 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6112 | } |
| 6113 | |
| 6114 | psa_aead_abort( &operation ); |
| 6115 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 6116 | /* 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] | 6117 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6118 | |
| 6119 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6120 | |
| 6121 | if( operation.alg == PSA_ALG_GCM ) |
| 6122 | { |
| 6123 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6124 | SIZE_MAX ), |
| 6125 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6126 | } |
| 6127 | else if ( operation.alg != PSA_ALG_CCM ) |
| 6128 | { |
| 6129 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6130 | SIZE_MAX ) ); |
| 6131 | } |
| 6132 | |
| 6133 | psa_aead_abort( &operation ); |
| 6134 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6135 | |
| 6136 | /* ------------------------------------------------------- */ |
| 6137 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 6138 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6139 | |
| 6140 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6141 | |
| 6142 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6143 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6144 | &nonce_length ), |
| 6145 | PSA_ERROR_BAD_STATE ); |
| 6146 | |
| 6147 | psa_aead_abort( &operation ); |
| 6148 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6149 | /* Test for generating nonce in decrypt setup. */ |
| 6150 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 6151 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6152 | |
| 6153 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6154 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6155 | &nonce_length ), |
| 6156 | PSA_ERROR_BAD_STATE ); |
| 6157 | |
| 6158 | psa_aead_abort( &operation ); |
| 6159 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6160 | /* Test for setting lengths twice. */ |
| 6161 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6162 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6163 | |
| 6164 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6165 | |
| 6166 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6167 | input_data->len ) ); |
| 6168 | |
| 6169 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6170 | input_data->len ), |
| 6171 | PSA_ERROR_BAD_STATE ); |
| 6172 | |
| 6173 | psa_aead_abort( &operation ); |
| 6174 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6175 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6176 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6177 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6178 | |
| 6179 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6180 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6181 | if( operation.alg == PSA_ALG_CCM ) |
| 6182 | { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6183 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6184 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6185 | additional_data->len ), |
| 6186 | PSA_ERROR_BAD_STATE ); |
| 6187 | } |
| 6188 | else |
| 6189 | { |
| 6190 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6191 | additional_data->len ) ); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6192 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6193 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6194 | input_data->len ), |
| 6195 | PSA_ERROR_BAD_STATE ); |
| 6196 | } |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6197 | psa_aead_abort( &operation ); |
| 6198 | |
| 6199 | /* ------------------------------------------------------- */ |
| 6200 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6201 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6202 | |
| 6203 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6204 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6205 | if( operation.alg == PSA_ALG_CCM ) |
| 6206 | { |
| 6207 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6208 | input_data->len, output_data, |
| 6209 | output_size, &output_length ), |
| 6210 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6211 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6212 | } |
| 6213 | else |
| 6214 | { |
| 6215 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6216 | input_data->len, output_data, |
| 6217 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6218 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6219 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6220 | input_data->len ), |
| 6221 | PSA_ERROR_BAD_STATE ); |
| 6222 | } |
| 6223 | psa_aead_abort( &operation ); |
| 6224 | |
| 6225 | /* ------------------------------------------------------- */ |
| 6226 | |
| 6227 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6228 | |
| 6229 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6230 | |
| 6231 | if( operation.alg == PSA_ALG_CCM ) |
| 6232 | { |
| 6233 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6234 | finish_output_size, |
| 6235 | &output_part_length, |
| 6236 | tag_buffer, tag_length, |
| 6237 | &tag_size ) ); |
| 6238 | } |
| 6239 | else |
| 6240 | { |
| 6241 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6242 | finish_output_size, |
| 6243 | &output_part_length, |
| 6244 | tag_buffer, tag_length, |
| 6245 | &tag_size ) ); |
| 6246 | |
| 6247 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6248 | input_data->len ), |
| 6249 | PSA_ERROR_BAD_STATE ); |
| 6250 | } |
| 6251 | psa_aead_abort( &operation ); |
| 6252 | |
| 6253 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 6254 | |
| 6255 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6256 | |
| 6257 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6258 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6259 | &nonce_length ) ); |
| 6260 | if( operation.alg == PSA_ALG_CCM ) |
| 6261 | { |
| 6262 | |
| 6263 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6264 | additional_data->len ), |
| 6265 | PSA_ERROR_BAD_STATE ); |
| 6266 | } |
| 6267 | else |
| 6268 | { |
| 6269 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6270 | additional_data->len ) ); |
| 6271 | |
| 6272 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6273 | input_data->len ), |
| 6274 | PSA_ERROR_BAD_STATE ); |
| 6275 | } |
| 6276 | psa_aead_abort( &operation ); |
| 6277 | |
| 6278 | /* ------------------------------------------------------- */ |
| 6279 | |
| 6280 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6281 | |
| 6282 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6283 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6284 | &nonce_length ) ); |
| 6285 | if( operation.alg == PSA_ALG_CCM ) |
| 6286 | { |
| 6287 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6288 | input_data->len, output_data, |
| 6289 | output_size, &output_length ), |
| 6290 | PSA_ERROR_BAD_STATE ); |
| 6291 | |
| 6292 | } |
| 6293 | else |
| 6294 | { |
| 6295 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6296 | input_data->len, output_data, |
| 6297 | output_size, &output_length ) ); |
| 6298 | |
| 6299 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6300 | input_data->len ), |
| 6301 | PSA_ERROR_BAD_STATE ); |
| 6302 | } |
| 6303 | psa_aead_abort( &operation ); |
| 6304 | |
| 6305 | /* ------------------------------------------------------- */ |
| 6306 | |
| 6307 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6308 | |
| 6309 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 6310 | PSA_AEAD_NONCE_MAX_SIZE, |
| 6311 | &nonce_length ) ); |
| 6312 | if( operation.alg == PSA_ALG_CCM ) |
| 6313 | { |
| 6314 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6315 | finish_output_size, |
| 6316 | &output_part_length, |
| 6317 | tag_buffer, tag_length, |
| 6318 | &tag_size ) ); |
| 6319 | } |
| 6320 | else |
| 6321 | { |
| 6322 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 6323 | finish_output_size, |
| 6324 | &output_part_length, |
| 6325 | tag_buffer, tag_length, |
| 6326 | &tag_size ) ); |
| 6327 | |
| 6328 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 6329 | input_data->len ), |
| 6330 | PSA_ERROR_BAD_STATE ); |
| 6331 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6332 | psa_aead_abort( &operation ); |
| 6333 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6334 | /* Test for not sending any additional data or data after setting non zero |
| 6335 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6336 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6337 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6338 | |
| 6339 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6340 | |
| 6341 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6342 | input_data->len ) ); |
| 6343 | |
| 6344 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6345 | finish_output_size, |
| 6346 | &output_part_length, |
| 6347 | tag_buffer, tag_length, |
| 6348 | &tag_size ), |
| 6349 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6350 | |
| 6351 | psa_aead_abort( &operation ); |
| 6352 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6353 | /* Test for not sending any additional data or data after setting non-zero |
| 6354 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6355 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6356 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6357 | |
| 6358 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6359 | |
| 6360 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6361 | input_data->len ) ); |
| 6362 | |
| 6363 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6364 | finish_output_size, |
| 6365 | &output_part_length, |
| 6366 | tag_buffer, |
| 6367 | tag_length ), |
| 6368 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6369 | |
| 6370 | psa_aead_abort( &operation ); |
| 6371 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 6372 | /* Test for not sending any additional data after setting a non-zero length |
| 6373 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6374 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6375 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6376 | |
| 6377 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6378 | |
| 6379 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6380 | input_data->len ) ); |
| 6381 | |
| 6382 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6383 | input_data->len, output_data, |
| 6384 | output_size, &output_length ), |
| 6385 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6386 | |
| 6387 | psa_aead_abort( &operation ); |
| 6388 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6389 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 6390 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 6391 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6392 | |
| 6393 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6394 | |
| 6395 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6396 | input_data->len ) ); |
| 6397 | |
| 6398 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6399 | additional_data->len ) ); |
| 6400 | |
| 6401 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6402 | finish_output_size, |
| 6403 | &output_part_length, |
| 6404 | tag_buffer, tag_length, |
| 6405 | &tag_size ), |
| 6406 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6407 | |
| 6408 | psa_aead_abort( &operation ); |
| 6409 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6410 | /* Test for sending too much additional data after setting lengths. */ |
| 6411 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6412 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6413 | |
| 6414 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6415 | |
| 6416 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6417 | |
| 6418 | |
| 6419 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6420 | additional_data->len ), |
| 6421 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6422 | |
| 6423 | psa_aead_abort( &operation ); |
| 6424 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6425 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6426 | |
| 6427 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6428 | |
| 6429 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6430 | |
| 6431 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6432 | input_data->len ) ); |
| 6433 | |
| 6434 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6435 | additional_data->len ) ); |
| 6436 | |
| 6437 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6438 | 1 ), |
| 6439 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6440 | |
| 6441 | psa_aead_abort( &operation ); |
| 6442 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6443 | /* Test for sending too much data after setting lengths. */ |
| 6444 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 6445 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6446 | |
| 6447 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6448 | |
| 6449 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 6450 | |
| 6451 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6452 | input_data->len, output_data, |
| 6453 | output_size, &output_length ), |
| 6454 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6455 | |
| 6456 | psa_aead_abort( &operation ); |
| 6457 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 6458 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 6459 | |
| 6460 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6461 | |
| 6462 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6463 | |
| 6464 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 6465 | input_data->len ) ); |
| 6466 | |
| 6467 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 6468 | additional_data->len ) ); |
| 6469 | |
| 6470 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6471 | input_data->len, output_data, |
| 6472 | output_size, &output_length ) ); |
| 6473 | |
| 6474 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 6475 | 1, output_data, |
| 6476 | output_size, &output_length ), |
| 6477 | PSA_ERROR_INVALID_ARGUMENT ); |
| 6478 | |
| 6479 | psa_aead_abort( &operation ); |
| 6480 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6481 | /* Test sending additional data after data. */ |
| 6482 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6483 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6484 | |
| 6485 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6486 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6487 | if( operation.alg != PSA_ALG_CCM ) |
| 6488 | { |
| 6489 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 6490 | input_data->len, output_data, |
| 6491 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6492 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 6493 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 6494 | additional_data->len ), |
| 6495 | PSA_ERROR_BAD_STATE ); |
| 6496 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6497 | psa_aead_abort( &operation ); |
| 6498 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6499 | /* Test calling finish on decryption. */ |
| 6500 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6501 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 6502 | |
| 6503 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6504 | |
| 6505 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 6506 | finish_output_size, |
| 6507 | &output_part_length, |
| 6508 | tag_buffer, tag_length, |
| 6509 | &tag_size ), |
| 6510 | PSA_ERROR_BAD_STATE ); |
| 6511 | |
| 6512 | psa_aead_abort( &operation ); |
| 6513 | |
| 6514 | /* Test calling verify on encryption. */ |
| 6515 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6516 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 6517 | |
| 6518 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 6519 | |
| 6520 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 6521 | finish_output_size, |
| 6522 | &output_part_length, |
| 6523 | tag_buffer, |
| 6524 | tag_length ), |
Paul Elliott | 5b065cb | 2021-06-23 08:33:22 +0100 | [diff] [blame] | 6525 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 6526 | |
| 6527 | psa_aead_abort( &operation ); |
| 6528 | |
| 6529 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 6530 | exit: |
| 6531 | psa_destroy_key( key ); |
| 6532 | psa_aead_abort( &operation ); |
| 6533 | mbedtls_free( output_data ); |
| 6534 | mbedtls_free( final_data ); |
| 6535 | PSA_DONE( ); |
| 6536 | } |
| 6537 | /* END_CASE */ |
| 6538 | |
| 6539 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6540 | void signature_size( int type_arg, |
| 6541 | int bits, |
| 6542 | int alg_arg, |
| 6543 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6544 | { |
| 6545 | psa_key_type_t type = type_arg; |
| 6546 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6547 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6548 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6549 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 6550 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6551 | exit: |
| 6552 | ; |
| 6553 | } |
| 6554 | /* END_CASE */ |
| 6555 | |
| 6556 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6557 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 6558 | int alg_arg, data_t *input_data, |
| 6559 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6560 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6561 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 6562 | psa_key_type_t key_type = key_type_arg; |
| 6563 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6564 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6565 | unsigned char *signature = NULL; |
| 6566 | size_t signature_size; |
| 6567 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6568 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6569 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6570 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6571 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6572 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6573 | psa_set_key_algorithm( &attributes, alg ); |
| 6574 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6575 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6576 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6577 | &key ) ); |
| 6578 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6579 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6580 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6581 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6582 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6583 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 6584 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6585 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6586 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6587 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6588 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6589 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6590 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6591 | input_data->x, input_data->len, |
| 6592 | signature, signature_size, |
| 6593 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6594 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6595 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6596 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6597 | |
| 6598 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6599 | /* |
| 6600 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6601 | * thus reset them as required. |
| 6602 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6603 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6604 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6605 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 6606 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6607 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6608 | } |
| 6609 | /* END_CASE */ |
| 6610 | |
| 6611 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6612 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 6613 | int alg_arg, data_t *input_data, |
| 6614 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6615 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6616 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6617 | psa_key_type_t key_type = key_type_arg; |
| 6618 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6619 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6620 | psa_status_t actual_status; |
| 6621 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 6622 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 6623 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6624 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6625 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6626 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6627 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6628 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6629 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6630 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6631 | psa_set_key_algorithm( &attributes, alg ); |
| 6632 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 6633 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6634 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6635 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6636 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6637 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6638 | input_data->x, input_data->len, |
| 6639 | signature, signature_size, |
| 6640 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6641 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 6642 | /* The value of *signature_length is unspecified on error, but |
| 6643 | * whatever it is, it should be less than signature_size, so that |
| 6644 | * if the caller tries to read *signature_length bytes without |
| 6645 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6646 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6647 | |
| 6648 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6649 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6650 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6651 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6652 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 6653 | } |
| 6654 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 6655 | |
| 6656 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6657 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 6658 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6659 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6660 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6661 | psa_key_type_t key_type = key_type_arg; |
| 6662 | psa_algorithm_t alg = alg_arg; |
| 6663 | size_t key_bits; |
| 6664 | unsigned char *signature = NULL; |
| 6665 | size_t signature_size; |
| 6666 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6667 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6668 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6669 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6670 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6671 | 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] | 6672 | psa_set_key_algorithm( &attributes, alg ); |
| 6673 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6674 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6675 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6676 | &key ) ); |
| 6677 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6678 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6679 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 6680 | /* Allocate a buffer which has the size advertised by the |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6681 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6682 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6683 | key_bits, alg ); |
| 6684 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6685 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6686 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6687 | |
| 6688 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6689 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6690 | input_data->x, input_data->len, |
| 6691 | signature, signature_size, |
| 6692 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6693 | /* Check that the signature length looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6694 | TEST_LE_U( signature_length, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6695 | TEST_ASSERT( signature_length > 0 ); |
| 6696 | |
| 6697 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6698 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6699 | input_data->x, input_data->len, |
| 6700 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6701 | |
| 6702 | if( input_data->len != 0 ) |
| 6703 | { |
| 6704 | /* Flip a bit in the input and verify that the signature is now |
| 6705 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6706 | * because ECDSA may ignore the last few bits of the input. */ |
| 6707 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6708 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6709 | input_data->x, input_data->len, |
| 6710 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 6711 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6712 | } |
| 6713 | |
| 6714 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6715 | /* |
| 6716 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6717 | * thus reset them as required. |
| 6718 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6719 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6720 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6721 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6722 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6723 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 6724 | } |
| 6725 | /* END_CASE */ |
| 6726 | |
| 6727 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6728 | void verify_hash( int key_type_arg, data_t *key_data, |
| 6729 | int alg_arg, data_t *hash_data, |
| 6730 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6731 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6732 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6733 | psa_key_type_t key_type = key_type_arg; |
| 6734 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6735 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6736 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6737 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 6738 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6739 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6740 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6741 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6742 | psa_set_key_algorithm( &attributes, alg ); |
| 6743 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6744 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6745 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6746 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6747 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6748 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6749 | hash_data->x, hash_data->len, |
| 6750 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 6751 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6752 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6753 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6754 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6755 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 6756 | } |
| 6757 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6758 | |
| 6759 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 6760 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 6761 | int alg_arg, data_t *hash_data, |
| 6762 | data_t *signature_data, |
| 6763 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6764 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6765 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6766 | psa_key_type_t key_type = key_type_arg; |
| 6767 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6768 | psa_status_t actual_status; |
| 6769 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6770 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6771 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6772 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6773 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6774 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6775 | psa_set_key_algorithm( &attributes, alg ); |
| 6776 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6777 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6778 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6779 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6780 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6781 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 6782 | hash_data->x, hash_data->len, |
| 6783 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6784 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6785 | |
| 6786 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6787 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6788 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6789 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6790 | } |
| 6791 | /* END_CASE */ |
| 6792 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6793 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6794 | void sign_message_deterministic( int key_type_arg, |
| 6795 | data_t *key_data, |
| 6796 | int alg_arg, |
| 6797 | data_t *input_data, |
| 6798 | data_t *output_data ) |
| 6799 | { |
| 6800 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6801 | psa_key_type_t key_type = key_type_arg; |
| 6802 | psa_algorithm_t alg = alg_arg; |
| 6803 | size_t key_bits; |
| 6804 | unsigned char *signature = NULL; |
| 6805 | size_t signature_size; |
| 6806 | size_t signature_length = 0xdeadbeef; |
| 6807 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6808 | |
| 6809 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6810 | |
| 6811 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6812 | psa_set_key_algorithm( &attributes, alg ); |
| 6813 | psa_set_key_type( &attributes, key_type ); |
| 6814 | |
| 6815 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6816 | &key ) ); |
| 6817 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6818 | key_bits = psa_get_key_bits( &attributes ); |
| 6819 | |
| 6820 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6821 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6822 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6823 | ASSERT_ALLOC( signature, signature_size ); |
| 6824 | |
| 6825 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6826 | input_data->x, input_data->len, |
| 6827 | signature, signature_size, |
| 6828 | &signature_length ) ); |
| 6829 | |
| 6830 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 6831 | signature, signature_length ); |
| 6832 | |
| 6833 | exit: |
| 6834 | psa_reset_key_attributes( &attributes ); |
| 6835 | |
| 6836 | psa_destroy_key( key ); |
| 6837 | mbedtls_free( signature ); |
| 6838 | PSA_DONE( ); |
| 6839 | |
| 6840 | } |
| 6841 | /* END_CASE */ |
| 6842 | |
| 6843 | /* BEGIN_CASE */ |
| 6844 | void sign_message_fail( int key_type_arg, |
| 6845 | data_t *key_data, |
| 6846 | int alg_arg, |
| 6847 | data_t *input_data, |
| 6848 | int signature_size_arg, |
| 6849 | int expected_status_arg ) |
| 6850 | { |
| 6851 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6852 | psa_key_type_t key_type = key_type_arg; |
| 6853 | psa_algorithm_t alg = alg_arg; |
| 6854 | size_t signature_size = signature_size_arg; |
| 6855 | psa_status_t actual_status; |
| 6856 | psa_status_t expected_status = expected_status_arg; |
| 6857 | unsigned char *signature = NULL; |
| 6858 | size_t signature_length = 0xdeadbeef; |
| 6859 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6860 | |
| 6861 | ASSERT_ALLOC( signature, signature_size ); |
| 6862 | |
| 6863 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6864 | |
| 6865 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 6866 | psa_set_key_algorithm( &attributes, alg ); |
| 6867 | psa_set_key_type( &attributes, key_type ); |
| 6868 | |
| 6869 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6870 | &key ) ); |
| 6871 | |
| 6872 | actual_status = psa_sign_message( key, alg, |
| 6873 | input_data->x, input_data->len, |
| 6874 | signature, signature_size, |
| 6875 | &signature_length ); |
| 6876 | TEST_EQUAL( actual_status, expected_status ); |
| 6877 | /* The value of *signature_length is unspecified on error, but |
| 6878 | * whatever it is, it should be less than signature_size, so that |
| 6879 | * if the caller tries to read *signature_length bytes without |
| 6880 | * checking the error code then they don't overflow a buffer. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6881 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6882 | |
| 6883 | exit: |
| 6884 | psa_reset_key_attributes( &attributes ); |
| 6885 | psa_destroy_key( key ); |
| 6886 | mbedtls_free( signature ); |
| 6887 | PSA_DONE( ); |
| 6888 | } |
| 6889 | /* END_CASE */ |
| 6890 | |
| 6891 | /* BEGIN_CASE */ |
| 6892 | void sign_verify_message( int key_type_arg, |
| 6893 | data_t *key_data, |
| 6894 | int alg_arg, |
| 6895 | data_t *input_data ) |
| 6896 | { |
| 6897 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6898 | psa_key_type_t key_type = key_type_arg; |
| 6899 | psa_algorithm_t alg = alg_arg; |
| 6900 | size_t key_bits; |
| 6901 | unsigned char *signature = NULL; |
| 6902 | size_t signature_size; |
| 6903 | size_t signature_length = 0xdeadbeef; |
| 6904 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6905 | |
| 6906 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6907 | |
| 6908 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 6909 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6910 | psa_set_key_algorithm( &attributes, alg ); |
| 6911 | psa_set_key_type( &attributes, key_type ); |
| 6912 | |
| 6913 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6914 | &key ) ); |
| 6915 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6916 | key_bits = psa_get_key_bits( &attributes ); |
| 6917 | |
| 6918 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6919 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6920 | TEST_LE_U( signature_size, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6921 | ASSERT_ALLOC( signature, signature_size ); |
| 6922 | |
| 6923 | PSA_ASSERT( psa_sign_message( key, alg, |
| 6924 | input_data->x, input_data->len, |
| 6925 | signature, signature_size, |
| 6926 | &signature_length ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6927 | TEST_LE_U( signature_length, signature_size ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6928 | TEST_ASSERT( signature_length > 0 ); |
| 6929 | |
| 6930 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6931 | input_data->x, input_data->len, |
| 6932 | signature, signature_length ) ); |
| 6933 | |
| 6934 | if( input_data->len != 0 ) |
| 6935 | { |
| 6936 | /* Flip a bit in the input and verify that the signature is now |
| 6937 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 6938 | * because ECDSA may ignore the last few bits of the input. */ |
| 6939 | input_data->x[0] ^= 1; |
| 6940 | TEST_EQUAL( psa_verify_message( key, alg, |
| 6941 | input_data->x, input_data->len, |
| 6942 | signature, signature_length ), |
| 6943 | PSA_ERROR_INVALID_SIGNATURE ); |
| 6944 | } |
| 6945 | |
| 6946 | exit: |
| 6947 | psa_reset_key_attributes( &attributes ); |
| 6948 | |
| 6949 | psa_destroy_key( key ); |
| 6950 | mbedtls_free( signature ); |
| 6951 | PSA_DONE( ); |
| 6952 | } |
| 6953 | /* END_CASE */ |
| 6954 | |
| 6955 | /* BEGIN_CASE */ |
| 6956 | void verify_message( int key_type_arg, |
| 6957 | data_t *key_data, |
| 6958 | int alg_arg, |
| 6959 | data_t *input_data, |
| 6960 | data_t *signature_data ) |
| 6961 | { |
| 6962 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6963 | psa_key_type_t key_type = key_type_arg; |
| 6964 | psa_algorithm_t alg = alg_arg; |
| 6965 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6966 | |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 6967 | TEST_LE_U( signature_data->len, PSA_SIGNATURE_MAX_SIZE ); |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 6968 | |
| 6969 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6970 | |
| 6971 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6972 | psa_set_key_algorithm( &attributes, alg ); |
| 6973 | psa_set_key_type( &attributes, key_type ); |
| 6974 | |
| 6975 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6976 | &key ) ); |
| 6977 | |
| 6978 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6979 | input_data->x, input_data->len, |
| 6980 | signature_data->x, signature_data->len ) ); |
| 6981 | |
| 6982 | exit: |
| 6983 | psa_reset_key_attributes( &attributes ); |
| 6984 | psa_destroy_key( key ); |
| 6985 | PSA_DONE( ); |
| 6986 | } |
| 6987 | /* END_CASE */ |
| 6988 | |
| 6989 | /* BEGIN_CASE */ |
| 6990 | void verify_message_fail( int key_type_arg, |
| 6991 | data_t *key_data, |
| 6992 | int alg_arg, |
| 6993 | data_t *hash_data, |
| 6994 | data_t *signature_data, |
| 6995 | int expected_status_arg ) |
| 6996 | { |
| 6997 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6998 | psa_key_type_t key_type = key_type_arg; |
| 6999 | psa_algorithm_t alg = alg_arg; |
| 7000 | psa_status_t actual_status; |
| 7001 | psa_status_t expected_status = expected_status_arg; |
| 7002 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7003 | |
| 7004 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7005 | |
| 7006 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 7007 | psa_set_key_algorithm( &attributes, alg ); |
| 7008 | psa_set_key_type( &attributes, key_type ); |
| 7009 | |
| 7010 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 7011 | &key ) ); |
| 7012 | |
| 7013 | actual_status = psa_verify_message( key, alg, |
| 7014 | hash_data->x, hash_data->len, |
| 7015 | signature_data->x, |
| 7016 | signature_data->len ); |
| 7017 | TEST_EQUAL( actual_status, expected_status ); |
| 7018 | |
| 7019 | exit: |
| 7020 | psa_reset_key_attributes( &attributes ); |
| 7021 | psa_destroy_key( key ); |
| 7022 | PSA_DONE( ); |
| 7023 | } |
| 7024 | /* END_CASE */ |
| 7025 | |
| 7026 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7027 | void asymmetric_encrypt( int key_type_arg, |
| 7028 | data_t *key_data, |
| 7029 | int alg_arg, |
| 7030 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7031 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7032 | int expected_output_length_arg, |
| 7033 | int expected_status_arg ) |
| 7034 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7035 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7036 | psa_key_type_t key_type = key_type_arg; |
| 7037 | psa_algorithm_t alg = alg_arg; |
| 7038 | size_t expected_output_length = expected_output_length_arg; |
| 7039 | size_t key_bits; |
| 7040 | unsigned char *output = NULL; |
| 7041 | size_t output_size; |
| 7042 | size_t output_length = ~0; |
| 7043 | psa_status_t actual_status; |
| 7044 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7045 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7046 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7047 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 7048 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7049 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7050 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 7051 | psa_set_key_algorithm( &attributes, alg ); |
| 7052 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7053 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7054 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7055 | |
| 7056 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7057 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7058 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7059 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7060 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7061 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7062 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7063 | |
| 7064 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7065 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7066 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7067 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7068 | output, output_size, |
| 7069 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7070 | TEST_EQUAL( actual_status, expected_status ); |
| 7071 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7072 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7073 | /* If the label is empty, the test framework puts a non-null pointer |
| 7074 | * in label->x. Test that a null pointer works as well. */ |
| 7075 | if( label->len == 0 ) |
| 7076 | { |
| 7077 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7078 | if( output_size != 0 ) |
| 7079 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7080 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7081 | input_data->x, input_data->len, |
| 7082 | NULL, label->len, |
| 7083 | output, output_size, |
| 7084 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7085 | TEST_EQUAL( actual_status, expected_status ); |
| 7086 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7087 | } |
| 7088 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7089 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7090 | /* |
| 7091 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7092 | * thus reset them as required. |
| 7093 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7094 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7095 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7096 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7097 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7098 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 7099 | } |
| 7100 | /* END_CASE */ |
| 7101 | |
| 7102 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7103 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 7104 | data_t *key_data, |
| 7105 | int alg_arg, |
| 7106 | data_t *input_data, |
| 7107 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7108 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7109 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7110 | psa_key_type_t key_type = key_type_arg; |
| 7111 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7112 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7113 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7114 | size_t output_size; |
| 7115 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7116 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7117 | size_t output2_size; |
| 7118 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7119 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7120 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7121 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7122 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7123 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 7124 | psa_set_key_algorithm( &attributes, alg ); |
| 7125 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7126 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7127 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7128 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7129 | |
| 7130 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7131 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7132 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7133 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7134 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7135 | TEST_LE_U( output_size, PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7136 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7137 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7138 | output2_size = input_data->len; |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7139 | TEST_LE_U( output2_size, |
| 7140 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 7141 | TEST_LE_U( output2_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7142 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7143 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 7144 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 7145 | * the original plaintext because of the non-optional random |
| 7146 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7147 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7148 | input_data->x, input_data->len, |
| 7149 | label->x, label->len, |
| 7150 | output, output_size, |
| 7151 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7152 | /* We don't know what ciphertext length to expect, but check that |
| 7153 | * it looks sensible. */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7154 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 7155 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7156 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7157 | output, output_length, |
| 7158 | label->x, label->len, |
| 7159 | output2, output2_size, |
| 7160 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7161 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 7162 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7163 | |
| 7164 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7165 | /* |
| 7166 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7167 | * thus reset them as required. |
| 7168 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7169 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7170 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7171 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7172 | mbedtls_free( output ); |
| 7173 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7174 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7175 | } |
| 7176 | /* END_CASE */ |
| 7177 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7178 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7179 | void asymmetric_decrypt( int key_type_arg, |
| 7180 | data_t *key_data, |
| 7181 | int alg_arg, |
| 7182 | data_t *input_data, |
| 7183 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 7184 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7185 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7186 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7187 | psa_key_type_t key_type = key_type_arg; |
| 7188 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7189 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7190 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 7191 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7192 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7193 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7194 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7195 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7196 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7197 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7198 | psa_set_key_algorithm( &attributes, alg ); |
| 7199 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7200 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7201 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7202 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7203 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7204 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 7205 | key_bits = psa_get_key_bits( &attributes ); |
| 7206 | |
| 7207 | /* Determine the maximum ciphertext length */ |
| 7208 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7209 | TEST_LE_U( output_size, PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7210 | ASSERT_ALLOC( output, output_size ); |
| 7211 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7212 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7213 | input_data->x, input_data->len, |
| 7214 | label->x, label->len, |
| 7215 | output, |
| 7216 | output_size, |
| 7217 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7218 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7219 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7220 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7221 | /* If the label is empty, the test framework puts a non-null pointer |
| 7222 | * in label->x. Test that a null pointer works as well. */ |
| 7223 | if( label->len == 0 ) |
| 7224 | { |
| 7225 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7226 | if( output_size != 0 ) |
| 7227 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7228 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7229 | input_data->x, input_data->len, |
| 7230 | NULL, label->len, |
| 7231 | output, |
| 7232 | output_size, |
| 7233 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 7234 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 7235 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7236 | } |
| 7237 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7238 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7239 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7240 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 7241 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7242 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7243 | } |
| 7244 | /* END_CASE */ |
| 7245 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7246 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7247 | void asymmetric_decrypt_fail( int key_type_arg, |
| 7248 | data_t *key_data, |
| 7249 | int alg_arg, |
| 7250 | data_t *input_data, |
| 7251 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7252 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7253 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7254 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7255 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7256 | psa_key_type_t key_type = key_type_arg; |
| 7257 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7258 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 7259 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 7260 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7261 | psa_status_t actual_status; |
| 7262 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7263 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7264 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7265 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7266 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7267 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7268 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7269 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 7270 | psa_set_key_algorithm( &attributes, alg ); |
| 7271 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 7272 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7273 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7274 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7275 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7276 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7277 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7278 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 7279 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7280 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7281 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7282 | TEST_LE_U( output_length, output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7283 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7284 | /* If the label is empty, the test framework puts a non-null pointer |
| 7285 | * in label->x. Test that a null pointer works as well. */ |
| 7286 | if( label->len == 0 ) |
| 7287 | { |
| 7288 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7289 | if( output_size != 0 ) |
| 7290 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7291 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7292 | input_data->x, input_data->len, |
| 7293 | NULL, label->len, |
| 7294 | output, output_size, |
| 7295 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7296 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7297 | TEST_LE_U( output_length, output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 7298 | } |
| 7299 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7300 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7301 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7302 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 7303 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7304 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 7305 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 7306 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7307 | |
| 7308 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 7309 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7310 | { |
| 7311 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 7312 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 7313 | * 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] | 7314 | * to suppress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7315 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7316 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 7317 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7318 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7319 | |
| 7320 | memset( &zero, 0, sizeof( zero ) ); |
| 7321 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7322 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7323 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7324 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7325 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7326 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7327 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7328 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 7329 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7330 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7331 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 7332 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 7333 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 7334 | } |
| 7335 | /* END_CASE */ |
| 7336 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7337 | /* BEGIN_CASE */ |
| 7338 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7339 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7340 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7341 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7342 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7343 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7344 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7345 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 7346 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7347 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7348 | |
| 7349 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7350 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7351 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 7352 | } |
| 7353 | /* END_CASE */ |
| 7354 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7355 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 7356 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 7357 | int expected_status_arg ) |
| 7358 | { |
| 7359 | psa_algorithm_t alg = alg_arg; |
| 7360 | size_t capacity = capacity_arg; |
| 7361 | psa_status_t expected_status = expected_status_arg; |
| 7362 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7363 | |
| 7364 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7365 | |
| 7366 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7367 | |
| 7368 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 7369 | expected_status ); |
| 7370 | |
| 7371 | exit: |
| 7372 | psa_key_derivation_abort( &operation ); |
| 7373 | PSA_DONE( ); |
| 7374 | } |
| 7375 | /* END_CASE */ |
| 7376 | |
| 7377 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7378 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7379 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7380 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7381 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7382 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 7383 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7384 | int expected_status_arg3, |
| 7385 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7386 | { |
| 7387 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7388 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 7389 | 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] | 7390 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 7391 | expected_status_arg2, |
| 7392 | expected_status_arg3}; |
| 7393 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7394 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 7395 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7396 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7397 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 7398 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7399 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7400 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7401 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7402 | psa_status_t expected_output_status = expected_output_status_arg; |
| 7403 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7404 | |
| 7405 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7406 | |
| 7407 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7408 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7409 | |
| 7410 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7411 | |
| 7412 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 7413 | { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 7414 | mbedtls_test_set_step( i ); |
| 7415 | if( steps[i] == 0 ) |
| 7416 | { |
| 7417 | /* Skip this step */ |
| 7418 | } |
| 7419 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7420 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7421 | psa_set_key_type( &attributes, key_types[i] ); |
| 7422 | PSA_ASSERT( psa_import_key( &attributes, |
| 7423 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7424 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7425 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 7426 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 7427 | { |
| 7428 | // When taking a private key as secret input, use key agreement |
| 7429 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7430 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 7431 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7432 | expected_statuses[i] ); |
| 7433 | } |
| 7434 | else |
| 7435 | { |
| 7436 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7437 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 7438 | expected_statuses[i] ); |
| 7439 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 7440 | } |
| 7441 | else |
| 7442 | { |
| 7443 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 7444 | &operation, steps[i], |
| 7445 | inputs[i]->x, inputs[i]->len ), |
| 7446 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7447 | } |
| 7448 | } |
| 7449 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7450 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 7451 | { |
| 7452 | psa_reset_key_attributes( &attributes ); |
Dave Rodgman | 491d849 | 2021-11-16 12:12:49 +0000 | [diff] [blame] | 7453 | psa_set_key_type( &attributes, output_key_type ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7454 | psa_set_key_bits( &attributes, 8 ); |
| 7455 | actual_output_status = |
| 7456 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7457 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 7458 | } |
| 7459 | else |
| 7460 | { |
| 7461 | uint8_t buffer[1]; |
| 7462 | actual_output_status = |
| 7463 | psa_key_derivation_output_bytes( &operation, |
| 7464 | buffer, sizeof( buffer ) ); |
| 7465 | } |
| 7466 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 7467 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7468 | exit: |
| 7469 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7470 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7471 | psa_destroy_key( keys[i] ); |
| 7472 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 7473 | PSA_DONE( ); |
| 7474 | } |
| 7475 | /* END_CASE */ |
| 7476 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7477 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7478 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7479 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7480 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7481 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 7482 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7483 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7484 | unsigned char input1[] = "Input 1"; |
| 7485 | size_t input1_length = sizeof( input1 ); |
| 7486 | unsigned char input2[] = "Input 2"; |
| 7487 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7488 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 7489 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 7490 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7491 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 7492 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7493 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7494 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7495 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7496 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 7497 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7498 | psa_set_key_algorithm( &attributes, alg ); |
| 7499 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7500 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 7501 | PSA_ASSERT( psa_import_key( &attributes, |
| 7502 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7503 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7504 | |
| 7505 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7506 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7507 | input1, input1_length, |
| 7508 | input2, input2_length, |
| 7509 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7510 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7511 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7512 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 7513 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 7514 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7515 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7516 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7517 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7518 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7519 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7520 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7521 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7522 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7523 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7524 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7525 | } |
| 7526 | /* END_CASE */ |
| 7527 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7528 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 7529 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7530 | { |
| 7531 | uint8_t output_buffer[16]; |
| 7532 | size_t buffer_size = 16; |
| 7533 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7534 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7535 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7536 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7537 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7538 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7539 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7540 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7541 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7542 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7543 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7544 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7545 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 7546 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7547 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7548 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7549 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 7550 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 7551 | |
| 7552 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7553 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 7554 | } |
| 7555 | /* END_CASE */ |
| 7556 | |
| 7557 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7558 | void derive_output( int alg_arg, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7559 | int step1_arg, data_t *input1, int expected_status_arg1, |
| 7560 | int step2_arg, data_t *input2, int expected_status_arg2, |
| 7561 | int step3_arg, data_t *input3, int expected_status_arg3, |
| 7562 | int step4_arg, data_t *input4, int expected_status_arg4, |
| 7563 | data_t *key_agreement_peer_key, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7564 | int requested_capacity_arg, |
| 7565 | data_t *expected_output1, |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7566 | data_t *expected_output2, |
| 7567 | int other_key_input_type, |
| 7568 | int key_input_type, |
| 7569 | int derive_type ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7570 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7571 | psa_algorithm_t alg = alg_arg; |
Przemek Stekiel | ffbb7d3 | 2022-03-31 11:13:47 +0200 | [diff] [blame] | 7572 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg, step4_arg}; |
| 7573 | data_t *inputs[] = {input1, input2, input3, input4}; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7574 | mbedtls_svc_key_id_t keys[] = {MBEDTLS_SVC_KEY_ID_INIT, |
| 7575 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7576 | MBEDTLS_SVC_KEY_ID_INIT, |
| 7577 | MBEDTLS_SVC_KEY_ID_INIT}; |
| 7578 | psa_status_t statuses[] = {expected_status_arg1, expected_status_arg2, |
| 7579 | expected_status_arg3, expected_status_arg4}; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7580 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7581 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7582 | uint8_t *expected_outputs[2] = |
| 7583 | {expected_output1->x, expected_output2->x}; |
| 7584 | size_t output_sizes[2] = |
| 7585 | {expected_output1->len, expected_output2->len}; |
| 7586 | size_t output_buffer_size = 0; |
| 7587 | uint8_t *output_buffer = NULL; |
| 7588 | size_t expected_capacity; |
| 7589 | size_t current_capacity; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7590 | psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT; |
| 7591 | psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT; |
| 7592 | psa_key_attributes_t attributes3 = PSA_KEY_ATTRIBUTES_INIT; |
| 7593 | psa_key_attributes_t attributes4 = PSA_KEY_ATTRIBUTES_INIT; |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7594 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7595 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7596 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7597 | |
| 7598 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 7599 | { |
| 7600 | if( output_sizes[i] > output_buffer_size ) |
| 7601 | output_buffer_size = output_sizes[i]; |
| 7602 | if( output_sizes[i] == 0 ) |
| 7603 | expected_outputs[i] = NULL; |
| 7604 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7605 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7606 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7607 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7608 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7609 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 7610 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 7611 | requested_capacity ) ); |
| 7612 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7613 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7614 | switch( steps[i] ) |
| 7615 | { |
| 7616 | case 0: |
| 7617 | break; |
| 7618 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7619 | switch( key_input_type ) |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7620 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7621 | case 0: // input bytes |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7622 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7623 | &operation, steps[i], |
Przemek Stekiel | fcdd023 | 2022-05-19 10:28:58 +0200 | [diff] [blame] | 7624 | inputs[i]->x, inputs[i]->len ), |
| 7625 | statuses[i] ); |
| 7626 | |
| 7627 | if( statuses[i] != PSA_SUCCESS ) |
| 7628 | goto exit; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7629 | break; |
| 7630 | case 1: // input key |
| 7631 | psa_set_key_usage_flags( &attributes1, PSA_KEY_USAGE_DERIVE ); |
| 7632 | psa_set_key_algorithm( &attributes1, alg ); |
| 7633 | psa_set_key_type( &attributes1, PSA_KEY_TYPE_DERIVE ); |
| 7634 | |
| 7635 | PSA_ASSERT( psa_import_key( &attributes1, |
| 7636 | inputs[i]->x, inputs[i]->len, |
| 7637 | &keys[i] ) ); |
| 7638 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7639 | if( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7640 | { |
| 7641 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes1 ) ); |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 7642 | TEST_LE_U( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes1 ) ), |
| 7643 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7644 | } |
| 7645 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7646 | PSA_ASSERT( psa_key_derivation_input_key( &operation, |
| 7647 | steps[i], |
| 7648 | keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7649 | break; |
| 7650 | default: |
| 7651 | TEST_ASSERT( ! "default case not supported" ); |
| 7652 | break; |
| 7653 | } |
| 7654 | break; |
| 7655 | case PSA_KEY_DERIVATION_INPUT_OTHER_SECRET: |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7656 | switch( other_key_input_type ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7657 | { |
| 7658 | case 0: // input bytes |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7659 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
| 7660 | steps[i], |
| 7661 | inputs[i]->x, |
| 7662 | inputs[i]->len ), |
| 7663 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7664 | break; |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7665 | case 1: // input key, type DERIVE |
| 7666 | case 11: // input key, type RAW |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7667 | psa_set_key_usage_flags( &attributes2, PSA_KEY_USAGE_DERIVE ); |
| 7668 | psa_set_key_algorithm( &attributes2, alg ); |
| 7669 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_DERIVE ); |
| 7670 | |
| 7671 | // other secret of type RAW_DATA passed with input_key |
Przemek Stekiel | e665466 | 2022-04-20 09:14:51 +0200 | [diff] [blame] | 7672 | if( other_key_input_type == 11 ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7673 | psa_set_key_type( &attributes2, PSA_KEY_TYPE_RAW_DATA ); |
| 7674 | |
| 7675 | PSA_ASSERT( psa_import_key( &attributes2, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7676 | inputs[i]->x, inputs[i]->len, |
| 7677 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7678 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7679 | TEST_EQUAL( psa_key_derivation_input_key( &operation, |
| 7680 | steps[i], |
| 7681 | keys[i] ), |
| 7682 | statuses[i] ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7683 | break; |
| 7684 | case 2: // key agreement |
| 7685 | psa_set_key_usage_flags( &attributes3, PSA_KEY_USAGE_DERIVE ); |
| 7686 | psa_set_key_algorithm( &attributes3, alg ); |
| 7687 | psa_set_key_type( &attributes3, PSA_KEY_TYPE_ECC_KEY_PAIR(PSA_ECC_FAMILY_SECP_R1) ); |
| 7688 | |
| 7689 | PSA_ASSERT( psa_import_key( &attributes3, |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7690 | inputs[i]->x, inputs[i]->len, |
| 7691 | &keys[i] ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7692 | |
| 7693 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 7694 | &operation, |
| 7695 | PSA_KEY_DERIVATION_INPUT_OTHER_SECRET, |
| 7696 | keys[i], key_agreement_peer_key->x, |
| 7697 | key_agreement_peer_key->len ), statuses[i] ); |
| 7698 | break; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7699 | default: |
| 7700 | TEST_ASSERT( ! "default case not supported" ); |
| 7701 | break; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7702 | } |
| 7703 | |
Przemek Stekiel | 38647de | 2022-04-19 13:27:47 +0200 | [diff] [blame] | 7704 | if( statuses[i] != PSA_SUCCESS ) |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7705 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7706 | break; |
| 7707 | default: |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7708 | TEST_EQUAL( psa_key_derivation_input_bytes( |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7709 | &operation, steps[i], |
Przemek Stekiel | ead1bb9 | 2022-05-11 12:22:57 +0200 | [diff] [blame] | 7710 | inputs[i]->x, inputs[i]->len ), statuses[i] ); |
| 7711 | |
| 7712 | if( statuses[i] != PSA_SUCCESS ) |
| 7713 | goto exit; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7714 | break; |
| 7715 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 7716 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 7717 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7718 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7719 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7720 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7721 | expected_capacity = requested_capacity; |
| 7722 | |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7723 | if( derive_type == 1 ) // output key |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7724 | { |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7725 | psa_status_t expected_status = PSA_ERROR_NOT_PERMITTED; |
| 7726 | |
| 7727 | /* For output key derivation secret must be provided using |
| 7728 | input key, otherwise operation is not permitted. */ |
Przemek Stekiel | 4e47a91 | 2022-04-21 11:40:18 +0200 | [diff] [blame] | 7729 | if( key_input_type == 1 ) |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7730 | expected_status = PSA_SUCCESS; |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7731 | |
| 7732 | psa_set_key_usage_flags( &attributes4, PSA_KEY_USAGE_EXPORT ); |
| 7733 | psa_set_key_algorithm( &attributes4, alg ); |
| 7734 | psa_set_key_type( &attributes4, PSA_KEY_TYPE_DERIVE ); |
Przemek Stekiel | 0e99391 | 2022-06-03 15:01:14 +0200 | [diff] [blame] | 7735 | psa_set_key_bits( &attributes4, PSA_BYTES_TO_BITS( requested_capacity ) ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7736 | |
| 7737 | TEST_EQUAL( psa_key_derivation_output_key( &attributes4, &operation, |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7738 | &derived_key ), expected_status ); |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7739 | } |
| 7740 | else // output bytes |
| 7741 | { |
| 7742 | /* Expansion phase. */ |
| 7743 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7744 | { |
Przemek Stekiel | cd00d7f | 2022-04-01 13:40:48 +0200 | [diff] [blame] | 7745 | /* Read some bytes. */ |
| 7746 | status = psa_key_derivation_output_bytes( &operation, |
| 7747 | output_buffer, output_sizes[i] ); |
| 7748 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 7749 | { |
| 7750 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 7751 | TEST_ASSERT( status == PSA_SUCCESS || |
| 7752 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
| 7753 | continue; |
| 7754 | } |
| 7755 | else if( expected_capacity == 0 || |
| 7756 | output_sizes[i] > expected_capacity ) |
| 7757 | { |
| 7758 | /* Capacity exceeded. */ |
| 7759 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
| 7760 | expected_capacity = 0; |
| 7761 | continue; |
| 7762 | } |
| 7763 | /* Success. Check the read data. */ |
| 7764 | PSA_ASSERT( status ); |
| 7765 | if( output_sizes[i] != 0 ) |
| 7766 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 7767 | expected_outputs[i], output_sizes[i] ); |
| 7768 | /* Check the operation status. */ |
| 7769 | expected_capacity -= output_sizes[i]; |
| 7770 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
| 7771 | ¤t_capacity ) ); |
| 7772 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7773 | } |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7774 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7775 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7776 | |
| 7777 | exit: |
| 7778 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7779 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7780 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 7781 | psa_destroy_key( keys[i] ); |
Przemek Stekiel | 4daaa2b | 2022-04-20 10:06:38 +0200 | [diff] [blame] | 7782 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7783 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 7784 | } |
| 7785 | /* END_CASE */ |
| 7786 | |
| 7787 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7788 | void derive_full( int alg_arg, |
| 7789 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7790 | data_t *input1, |
| 7791 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7792 | int requested_capacity_arg ) |
| 7793 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7794 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7795 | psa_algorithm_t alg = alg_arg; |
| 7796 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7797 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7798 | unsigned char output_buffer[16]; |
| 7799 | size_t expected_capacity = requested_capacity; |
| 7800 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7801 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7802 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7803 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7804 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7805 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7806 | psa_set_key_algorithm( &attributes, alg ); |
| 7807 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7808 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7809 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7810 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7811 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7812 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 7813 | input1->x, input1->len, |
| 7814 | input2->x, input2->len, |
| 7815 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 7816 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 7817 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7818 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7819 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7820 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7821 | |
| 7822 | /* Expansion phase. */ |
| 7823 | while( current_capacity > 0 ) |
| 7824 | { |
| 7825 | size_t read_size = sizeof( output_buffer ); |
| 7826 | if( read_size > current_capacity ) |
| 7827 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7828 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7829 | output_buffer, |
| 7830 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7831 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7832 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7833 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7834 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7835 | } |
| 7836 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7837 | /* Check that the operation refuses to go over capacity. */ |
| 7838 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7839 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7840 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7841 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7842 | |
| 7843 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7844 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7845 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7846 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 7847 | } |
| 7848 | /* END_CASE */ |
| 7849 | |
Przemek Stekiel | 8258ea7 | 2022-10-19 12:17:19 +0200 | [diff] [blame] | 7850 | /* 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] | 7851 | 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] | 7852 | int derivation_step, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7853 | int capacity, int expected_capacity_status_arg, |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7854 | data_t *expected_output, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7855 | int expected_output_status_arg ) |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7856 | { |
| 7857 | psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS; |
| 7858 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Andrzej Kurek | d378504 | 2022-09-16 06:45:44 -0400 | [diff] [blame] | 7859 | 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] | 7860 | uint8_t *output_buffer = NULL; |
| 7861 | psa_status_t status; |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7862 | psa_status_t expected_input_status = (psa_status_t) expected_input_status_arg; |
| 7863 | psa_status_t expected_capacity_status = (psa_status_t) expected_capacity_status_arg; |
| 7864 | 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] | 7865 | |
| 7866 | ASSERT_ALLOC( output_buffer, expected_output->len ); |
| 7867 | PSA_ASSERT( psa_crypto_init() ); |
| 7868 | |
| 7869 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Andrzej Kurek | 2be1689 | 2022-09-16 07:14:04 -0400 | [diff] [blame] | 7870 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7871 | expected_capacity_status); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7872 | |
| 7873 | TEST_EQUAL( psa_key_derivation_input_bytes( &operation, |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7874 | step, input->x, input->len ), |
| 7875 | expected_input_status ); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7876 | |
| 7877 | if( ( (psa_status_t) expected_input_status ) != PSA_SUCCESS ) |
| 7878 | goto exit; |
| 7879 | |
| 7880 | status = psa_key_derivation_output_bytes( &operation, output_buffer, |
| 7881 | expected_output->len ); |
| 7882 | |
Andrzej Kurek | 3539f2c | 2022-09-26 10:56:02 -0400 | [diff] [blame] | 7883 | TEST_EQUAL( status, expected_output_status ); |
Andrzej Kurek | d8705bc | 2022-07-29 10:02:05 -0400 | [diff] [blame] | 7884 | if( expected_output->len != 0 && expected_output_status == PSA_SUCCESS ) |
| 7885 | ASSERT_COMPARE( output_buffer, expected_output->len, expected_output->x, |
| 7886 | expected_output->len ); |
| 7887 | |
| 7888 | exit: |
| 7889 | mbedtls_free( output_buffer ); |
| 7890 | psa_key_derivation_abort( &operation ); |
| 7891 | PSA_DONE(); |
| 7892 | } |
| 7893 | /* END_CASE */ |
| 7894 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7895 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7896 | void derive_key_exercise( int alg_arg, |
| 7897 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7898 | data_t *input1, |
| 7899 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7900 | int derived_type_arg, |
| 7901 | int derived_bits_arg, |
| 7902 | int derived_usage_arg, |
| 7903 | int derived_alg_arg ) |
| 7904 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7905 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7906 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7907 | psa_algorithm_t alg = alg_arg; |
| 7908 | psa_key_type_t derived_type = derived_type_arg; |
| 7909 | size_t derived_bits = derived_bits_arg; |
| 7910 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 7911 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 7912 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7913 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7914 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7915 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7916 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7917 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7918 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7919 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7920 | psa_set_key_algorithm( &attributes, alg ); |
| 7921 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7922 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7923 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7924 | |
| 7925 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7926 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7927 | input1->x, input1->len, |
| 7928 | input2->x, input2->len, |
| 7929 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 7930 | goto exit; |
| 7931 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7932 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 7933 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 7934 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7935 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7936 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7937 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7938 | |
| 7939 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7940 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7941 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 7942 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7943 | |
| 7944 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7945 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7946 | goto exit; |
| 7947 | |
| 7948 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7949 | /* |
| 7950 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7951 | * thus reset them as required. |
| 7952 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7953 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7954 | |
| 7955 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7956 | psa_destroy_key( base_key ); |
| 7957 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7958 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7959 | } |
| 7960 | /* END_CASE */ |
| 7961 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7962 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7963 | void derive_key_export( int alg_arg, |
| 7964 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7965 | data_t *input1, |
| 7966 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7967 | int bytes1_arg, |
| 7968 | int bytes2_arg ) |
| 7969 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7970 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7971 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7972 | psa_algorithm_t alg = alg_arg; |
| 7973 | size_t bytes1 = bytes1_arg; |
| 7974 | size_t bytes2 = bytes2_arg; |
| 7975 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7976 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7977 | uint8_t *output_buffer = NULL; |
| 7978 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7979 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7980 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7981 | size_t length; |
| 7982 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7983 | ASSERT_ALLOC( output_buffer, capacity ); |
| 7984 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7985 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7986 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7987 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 7988 | psa_set_key_algorithm( &base_attributes, alg ); |
| 7989 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7990 | 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] | 7991 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 7992 | |
| 7993 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7994 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 7995 | input1->x, input1->len, |
| 7996 | input2->x, input2->len, |
| 7997 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 7998 | goto exit; |
| 7999 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8000 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8001 | output_buffer, |
| 8002 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8003 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8004 | |
| 8005 | /* 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] | 8006 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 8007 | input1->x, input1->len, |
| 8008 | input2->x, input2->len, |
| 8009 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 8010 | goto exit; |
| 8011 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8012 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8013 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 8014 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8015 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8016 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8017 | &derived_key ) ); |
| 8018 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8019 | export_buffer, bytes1, |
| 8020 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8021 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8022 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8023 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8024 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8025 | &derived_key ) ); |
| 8026 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8027 | export_buffer + bytes1, bytes2, |
| 8028 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8029 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8030 | |
| 8031 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8032 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 8033 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8034 | |
| 8035 | exit: |
| 8036 | mbedtls_free( output_buffer ); |
| 8037 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8038 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8039 | psa_destroy_key( base_key ); |
| 8040 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8041 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 8042 | } |
| 8043 | /* END_CASE */ |
| 8044 | |
| 8045 | /* BEGIN_CASE */ |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8046 | void derive_key_type( int alg_arg, |
| 8047 | data_t *key_data, |
| 8048 | data_t *input1, |
| 8049 | data_t *input2, |
| 8050 | int key_type_arg, int bits_arg, |
| 8051 | data_t *expected_export ) |
| 8052 | { |
| 8053 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8054 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8055 | const psa_algorithm_t alg = alg_arg; |
| 8056 | const psa_key_type_t key_type = key_type_arg; |
| 8057 | const size_t bits = bits_arg; |
| 8058 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8059 | const size_t export_buffer_size = |
| 8060 | PSA_EXPORT_KEY_OUTPUT_SIZE( key_type, bits ); |
| 8061 | uint8_t *export_buffer = NULL; |
| 8062 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8063 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8064 | size_t export_length; |
| 8065 | |
| 8066 | ASSERT_ALLOC( export_buffer, export_buffer_size ); |
| 8067 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8068 | |
| 8069 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8070 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8071 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8072 | PSA_ASSERT( psa_import_key( &base_attributes, key_data->x, key_data->len, |
| 8073 | &base_key ) ); |
| 8074 | |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 8075 | if( mbedtls_test_psa_setup_key_derivation_wrap( |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8076 | &operation, base_key, alg, |
| 8077 | input1->x, input1->len, |
| 8078 | input2->x, input2->len, |
Przemek Stekiel | c85f091 | 2022-03-08 11:37:54 +0100 | [diff] [blame] | 8079 | PSA_KEY_DERIVATION_UNLIMITED_CAPACITY ) == 0 ) |
Przemyslaw Stekiel | 696b120 | 2021-11-24 16:29:10 +0100 | [diff] [blame] | 8080 | goto exit; |
| 8081 | |
| 8082 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8083 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 8084 | psa_set_key_type( &derived_attributes, key_type ); |
| 8085 | psa_set_key_bits( &derived_attributes, bits ); |
| 8086 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
| 8087 | &derived_key ) ); |
| 8088 | |
| 8089 | PSA_ASSERT( psa_export_key( derived_key, |
| 8090 | export_buffer, export_buffer_size, |
| 8091 | &export_length ) ); |
| 8092 | ASSERT_COMPARE( export_buffer, export_length, |
| 8093 | expected_export->x, expected_export->len ); |
| 8094 | |
| 8095 | exit: |
| 8096 | mbedtls_free( export_buffer ); |
| 8097 | psa_key_derivation_abort( &operation ); |
| 8098 | psa_destroy_key( base_key ); |
| 8099 | psa_destroy_key( derived_key ); |
| 8100 | PSA_DONE( ); |
| 8101 | } |
| 8102 | /* END_CASE */ |
| 8103 | |
| 8104 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8105 | void derive_key( int alg_arg, |
| 8106 | data_t *key_data, data_t *input1, data_t *input2, |
| 8107 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8108 | int expected_status_arg, |
| 8109 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8110 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8111 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8112 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8113 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8114 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8115 | size_t bits = bits_arg; |
| 8116 | psa_status_t expected_status = expected_status_arg; |
| 8117 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 8118 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8119 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8120 | |
| 8121 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8122 | |
| 8123 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 8124 | psa_set_key_algorithm( &base_attributes, alg ); |
| 8125 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 8126 | 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] | 8127 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8128 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8129 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 8130 | input1->x, input1->len, |
| 8131 | input2->x, input2->len, |
| 8132 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8133 | goto exit; |
| 8134 | |
| 8135 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 8136 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 8137 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8138 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8139 | |
| 8140 | psa_status_t status = |
| 8141 | psa_key_derivation_output_key( &derived_attributes, |
| 8142 | &operation, |
| 8143 | &derived_key ); |
| 8144 | if( is_large_output > 0 ) |
| 8145 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8146 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8147 | |
| 8148 | exit: |
| 8149 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8150 | psa_destroy_key( base_key ); |
| 8151 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 8152 | PSA_DONE( ); |
| 8153 | } |
| 8154 | /* END_CASE */ |
| 8155 | |
| 8156 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8157 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 8158 | int our_key_type_arg, int our_key_alg_arg, |
| 8159 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8160 | int expected_status_arg ) |
| 8161 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8162 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8163 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8164 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8165 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8166 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8167 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8168 | psa_status_t expected_status = expected_status_arg; |
| 8169 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8170 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8171 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8172 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8173 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 8174 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8175 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8176 | PSA_ASSERT( psa_import_key( &attributes, |
| 8177 | our_key_data->x, our_key_data->len, |
| 8178 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8179 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8180 | /* The tests currently include inputs that should fail at either step. |
| 8181 | * Test cases that fail at the setup step should be changed to call |
| 8182 | * key_derivation_setup instead, and this function should be renamed |
| 8183 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8184 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8185 | if( status == PSA_SUCCESS ) |
| 8186 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8187 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 8188 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 8189 | our_key, |
| 8190 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 8191 | expected_status ); |
| 8192 | } |
| 8193 | else |
| 8194 | { |
| 8195 | TEST_ASSERT( status == expected_status ); |
| 8196 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8197 | |
| 8198 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8199 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8200 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8201 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 8202 | } |
| 8203 | /* END_CASE */ |
| 8204 | |
| 8205 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8206 | void raw_key_agreement( int alg_arg, |
| 8207 | int our_key_type_arg, data_t *our_key_data, |
| 8208 | data_t *peer_key_data, |
| 8209 | data_t *expected_output ) |
| 8210 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8211 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8212 | psa_algorithm_t alg = alg_arg; |
| 8213 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8214 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8215 | unsigned char *output = NULL; |
| 8216 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8217 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8218 | |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8219 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8220 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8221 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8222 | psa_set_key_algorithm( &attributes, alg ); |
| 8223 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8224 | PSA_ASSERT( psa_import_key( &attributes, |
| 8225 | our_key_data->x, our_key_data->len, |
| 8226 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8227 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 8228 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 8229 | key_bits = psa_get_key_bits( &attributes ); |
| 8230 | |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8231 | /* Validate size macros */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8232 | TEST_LE_U( expected_output->len, |
| 8233 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 8234 | TEST_LE_U( PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ), |
| 8235 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8236 | |
| 8237 | /* Good case with exact output size */ |
| 8238 | ASSERT_ALLOC( output, expected_output->len ); |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 8239 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8240 | peer_key_data->x, peer_key_data->len, |
| 8241 | output, expected_output->len, |
| 8242 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8243 | ASSERT_COMPARE( output, output_length, |
| 8244 | expected_output->x, expected_output->len ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8245 | mbedtls_free( output ); |
| 8246 | output = NULL; |
| 8247 | output_length = ~0; |
| 8248 | |
| 8249 | /* Larger buffer */ |
| 8250 | ASSERT_ALLOC( output, expected_output->len + 1 ); |
| 8251 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 8252 | peer_key_data->x, peer_key_data->len, |
| 8253 | output, expected_output->len + 1, |
| 8254 | &output_length ) ); |
| 8255 | ASSERT_COMPARE( output, output_length, |
| 8256 | expected_output->x, expected_output->len ); |
| 8257 | mbedtls_free( output ); |
| 8258 | output = NULL; |
| 8259 | output_length = ~0; |
| 8260 | |
| 8261 | /* Buffer too small */ |
| 8262 | ASSERT_ALLOC( output, expected_output->len - 1 ); |
| 8263 | TEST_EQUAL( psa_raw_key_agreement( alg, our_key, |
| 8264 | peer_key_data->x, peer_key_data->len, |
| 8265 | output, expected_output->len - 1, |
| 8266 | &output_length ), |
| 8267 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8268 | /* Not required by the spec, but good robustness */ |
Gilles Peskine | 7be11a7 | 2022-04-14 00:12:57 +0200 | [diff] [blame] | 8269 | TEST_LE_U( output_length, expected_output->len - 1 ); |
Gilles Peskine | 992bee8 | 2022-04-13 23:25:52 +0200 | [diff] [blame] | 8270 | mbedtls_free( output ); |
| 8271 | output = NULL; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8272 | |
| 8273 | exit: |
| 8274 | mbedtls_free( output ); |
| 8275 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8276 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 8277 | } |
| 8278 | /* END_CASE */ |
| 8279 | |
| 8280 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8281 | void key_agreement_capacity( int alg_arg, |
| 8282 | int our_key_type_arg, data_t *our_key_data, |
| 8283 | data_t *peer_key_data, |
| 8284 | int expected_capacity_arg ) |
| 8285 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8286 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8287 | psa_algorithm_t alg = alg_arg; |
| 8288 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8289 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8290 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8291 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8292 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8293 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8294 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8295 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8296 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8297 | psa_set_key_algorithm( &attributes, alg ); |
| 8298 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8299 | PSA_ASSERT( psa_import_key( &attributes, |
| 8300 | our_key_data->x, our_key_data->len, |
| 8301 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8302 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8303 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8304 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8305 | &operation, |
| 8306 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8307 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8308 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8309 | { |
| 8310 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8311 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8312 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8313 | NULL, 0 ) ); |
| 8314 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8315 | |
Shaun Case | 8b0ecbc | 2021-12-20 21:14:10 -0800 | [diff] [blame] | 8316 | /* Test the advertised capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 8317 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8318 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 8319 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8320 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8321 | /* Test the actual capacity by reading the output. */ |
| 8322 | while( actual_capacity > sizeof( output ) ) |
| 8323 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8324 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8325 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8326 | actual_capacity -= sizeof( output ); |
| 8327 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8328 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8329 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8330 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 8331 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 8332 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8333 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8334 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8335 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8336 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8337 | } |
| 8338 | /* END_CASE */ |
| 8339 | |
| 8340 | /* BEGIN_CASE */ |
| 8341 | void key_agreement_output( int alg_arg, |
| 8342 | int our_key_type_arg, data_t *our_key_data, |
| 8343 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8344 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8345 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8346 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8347 | psa_algorithm_t alg = alg_arg; |
| 8348 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8349 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8350 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8351 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8352 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8353 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 8354 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8355 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8356 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8357 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8358 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8359 | psa_set_key_algorithm( &attributes, alg ); |
| 8360 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8361 | PSA_ASSERT( psa_import_key( &attributes, |
| 8362 | our_key_data->x, our_key_data->len, |
| 8363 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8364 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8365 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8366 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 8367 | &operation, |
| 8368 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 8369 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8370 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 8371 | { |
| 8372 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8373 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 8374 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 8375 | NULL, 0 ) ); |
| 8376 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8377 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8378 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8379 | actual_output, |
| 8380 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8381 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 8382 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8383 | if( expected_output2->len != 0 ) |
| 8384 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8385 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8386 | actual_output, |
| 8387 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 8388 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 8389 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 8390 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8391 | |
| 8392 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8393 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8394 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8395 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 8396 | mbedtls_free( actual_output ); |
| 8397 | } |
| 8398 | /* END_CASE */ |
| 8399 | |
| 8400 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8401 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8402 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8403 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8404 | unsigned char *output = NULL; |
| 8405 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8406 | size_t i; |
| 8407 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8408 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 8409 | TEST_ASSERT( bytes_arg >= 0 ); |
| 8410 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 8411 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 8412 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8413 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8414 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8415 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8416 | /* Run several times, to ensure that every output byte will be |
| 8417 | * nonzero at least once with overwhelming probability |
| 8418 | * (2^(-8*number_of_runs)). */ |
| 8419 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8420 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 8421 | if( bytes != 0 ) |
| 8422 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8423 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8424 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8425 | for( i = 0; i < bytes; i++ ) |
| 8426 | { |
| 8427 | if( output[i] != 0 ) |
| 8428 | ++changed[i]; |
| 8429 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8430 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8431 | |
| 8432 | /* Check that every byte was changed to nonzero at least once. This |
| 8433 | * validates that psa_generate_random is overwriting every byte of |
| 8434 | * the output buffer. */ |
| 8435 | for( i = 0; i < bytes; i++ ) |
| 8436 | { |
| 8437 | TEST_ASSERT( changed[i] != 0 ); |
| 8438 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8439 | |
| 8440 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8441 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 8442 | mbedtls_free( output ); |
| 8443 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 8444 | } |
| 8445 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8446 | |
| 8447 | /* BEGIN_CASE */ |
| 8448 | void generate_key( int type_arg, |
| 8449 | int bits_arg, |
| 8450 | int usage_arg, |
| 8451 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8452 | int expected_status_arg, |
| 8453 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8454 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8455 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8456 | psa_key_type_t type = type_arg; |
| 8457 | psa_key_usage_t usage = usage_arg; |
| 8458 | size_t bits = bits_arg; |
| 8459 | psa_algorithm_t alg = alg_arg; |
| 8460 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8461 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8462 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8463 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8464 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8465 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8466 | psa_set_key_usage_flags( &attributes, usage ); |
| 8467 | psa_set_key_algorithm( &attributes, alg ); |
| 8468 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8469 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8470 | |
| 8471 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 8472 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 8473 | |
| 8474 | if( is_large_key > 0 ) |
| 8475 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 8476 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8477 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 8478 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8479 | |
| 8480 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8481 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 8482 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 8483 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8484 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 8485 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8486 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 8487 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8488 | |
| 8489 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8490 | /* |
| 8491 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8492 | * thus reset them as required. |
| 8493 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8494 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8495 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8496 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8497 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 8498 | } |
| 8499 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 8500 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 8501 | /* 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] | 8502 | void generate_key_rsa( int bits_arg, |
| 8503 | data_t *e_arg, |
| 8504 | int expected_status_arg ) |
| 8505 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8506 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 8507 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8508 | size_t bits = bits_arg; |
| 8509 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 8510 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 8511 | psa_status_t expected_status = expected_status_arg; |
| 8512 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8513 | uint8_t *exported = NULL; |
| 8514 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8515 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8516 | size_t exported_length = SIZE_MAX; |
| 8517 | uint8_t *e_read_buffer = NULL; |
| 8518 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 8519 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8520 | size_t e_read_length = SIZE_MAX; |
| 8521 | |
| 8522 | if( e_arg->len == 0 || |
| 8523 | ( e_arg->len == 3 && |
| 8524 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 8525 | { |
| 8526 | is_default_public_exponent = 1; |
| 8527 | e_read_size = 0; |
| 8528 | } |
| 8529 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 8530 | ASSERT_ALLOC( exported, exported_size ); |
| 8531 | |
| 8532 | PSA_ASSERT( psa_crypto_init( ) ); |
| 8533 | |
| 8534 | psa_set_key_usage_flags( &attributes, usage ); |
| 8535 | psa_set_key_algorithm( &attributes, alg ); |
| 8536 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 8537 | e_arg->x, e_arg->len ) ); |
| 8538 | psa_set_key_bits( &attributes, bits ); |
| 8539 | |
| 8540 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8541 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8542 | if( expected_status != PSA_SUCCESS ) |
| 8543 | goto exit; |
| 8544 | |
| 8545 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8546 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8547 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8548 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 8549 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 8550 | e_read_buffer, e_read_size, |
| 8551 | &e_read_length ) ); |
| 8552 | if( is_default_public_exponent ) |
| 8553 | TEST_EQUAL( e_read_length, 0 ); |
| 8554 | else |
| 8555 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 8556 | |
| 8557 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8558 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8559 | goto exit; |
| 8560 | |
| 8561 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8562 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8563 | exported, exported_size, |
| 8564 | &exported_length ) ); |
| 8565 | { |
| 8566 | uint8_t *p = exported; |
| 8567 | uint8_t *end = exported + exported_length; |
| 8568 | size_t len; |
| 8569 | /* RSAPublicKey ::= SEQUENCE { |
| 8570 | * modulus INTEGER, -- n |
| 8571 | * publicExponent INTEGER } -- e |
| 8572 | */ |
| 8573 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8574 | MBEDTLS_ASN1_SEQUENCE | |
| 8575 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 8576 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8577 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 8578 | MBEDTLS_ASN1_INTEGER ) ); |
| 8579 | if( len >= 1 && p[0] == 0 ) |
| 8580 | { |
| 8581 | ++p; |
| 8582 | --len; |
| 8583 | } |
| 8584 | if( e_arg->len == 0 ) |
| 8585 | { |
| 8586 | TEST_EQUAL( len, 3 ); |
| 8587 | TEST_EQUAL( p[0], 1 ); |
| 8588 | TEST_EQUAL( p[1], 0 ); |
| 8589 | TEST_EQUAL( p[2], 1 ); |
| 8590 | } |
| 8591 | else |
| 8592 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 8593 | } |
| 8594 | |
| 8595 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8596 | /* |
| 8597 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 8598 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 8599 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8600 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8601 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8602 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8603 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 8604 | mbedtls_free( e_read_buffer ); |
| 8605 | mbedtls_free( exported ); |
| 8606 | } |
| 8607 | /* END_CASE */ |
| 8608 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8609 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8610 | void persistent_key_load_key_from_storage( data_t *data, |
| 8611 | int type_arg, int bits_arg, |
| 8612 | int usage_flags_arg, int alg_arg, |
| 8613 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8614 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 8615 | 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] | 8616 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8617 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8618 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8619 | psa_key_type_t type = type_arg; |
| 8620 | size_t bits = bits_arg; |
| 8621 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 8622 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8623 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8624 | unsigned char *first_export = NULL; |
| 8625 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 8626 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8627 | size_t first_exported_length; |
| 8628 | size_t second_exported_length; |
| 8629 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8630 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8631 | { |
| 8632 | ASSERT_ALLOC( first_export, export_size ); |
| 8633 | ASSERT_ALLOC( second_export, export_size ); |
| 8634 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8635 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8636 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8637 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 8638 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8639 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 8640 | psa_set_key_algorithm( &attributes, alg ); |
| 8641 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 8642 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8643 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8644 | switch( generation_method ) |
| 8645 | { |
| 8646 | case IMPORT_KEY: |
| 8647 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8648 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8649 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8650 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8651 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8652 | case GENERATE_KEY: |
| 8653 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8654 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8655 | break; |
| 8656 | |
| 8657 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 8658 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8659 | { |
| 8660 | /* Create base key */ |
| 8661 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 8662 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8663 | psa_set_key_usage_flags( &base_attributes, |
| 8664 | PSA_KEY_USAGE_DERIVE ); |
| 8665 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 8666 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 8667 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 8668 | data->x, data->len, |
| 8669 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8670 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8671 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8672 | PSA_ASSERT( psa_key_derivation_input_key( |
| 8673 | &operation, |
| 8674 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8675 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8676 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8677 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 8678 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 8679 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8680 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8681 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8682 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8683 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8684 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 8685 | #else |
| 8686 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 8687 | #endif |
| 8688 | break; |
| 8689 | |
| 8690 | default: |
| 8691 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 8692 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8693 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8694 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8695 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8696 | /* Export the key if permitted by the key policy. */ |
| 8697 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 8698 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8699 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8700 | first_export, export_size, |
| 8701 | &first_exported_length ) ); |
| 8702 | if( generation_method == IMPORT_KEY ) |
| 8703 | ASSERT_COMPARE( data->x, data->len, |
| 8704 | first_export, first_exported_length ); |
| 8705 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8706 | |
| 8707 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8708 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8709 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 8710 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8711 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8712 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8713 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 8714 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 8715 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8716 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 8717 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 8718 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 8719 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 8720 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 8721 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8722 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8723 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8724 | /* Export the key again if permitted by the key policy. */ |
| 8725 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8726 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8727 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8728 | second_export, export_size, |
| 8729 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8730 | ASSERT_COMPARE( first_export, first_exported_length, |
| 8731 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8732 | } |
| 8733 | |
| 8734 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 8735 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 8736 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8737 | |
| 8738 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8739 | /* |
| 8740 | * Key attributes may have been returned by psa_get_key_attributes() |
| 8741 | * thus reset them as required. |
| 8742 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 8743 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 8744 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8745 | mbedtls_free( first_export ); |
| 8746 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 8747 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 8748 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 8749 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 8750 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 8751 | } |
| 8752 | /* END_CASE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8753 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8754 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8755 | void ecjpake_setup( int alg_arg, int key_type_pw_arg, int key_usage_pw_arg, |
| 8756 | int primitive_arg, int hash_arg, int role_arg, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8757 | int input_first, data_t *pw_data, |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8758 | int expected_status_setup_arg, |
| 8759 | int expected_status_set_role_arg, |
| 8760 | int expected_status_set_password_key_arg, |
| 8761 | int expected_status_input_output_arg) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8762 | { |
| 8763 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8764 | psa_pake_operation_t operation = psa_pake_operation_init(); |
| 8765 | psa_algorithm_t alg = alg_arg; |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8766 | psa_pake_primitive_t primitive = primitive_arg; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8767 | psa_key_type_t key_type_pw = key_type_pw_arg; |
| 8768 | psa_key_usage_t key_usage_pw = key_usage_pw_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8769 | psa_algorithm_t hash_alg = hash_arg; |
| 8770 | psa_pake_role_t role = role_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8771 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8772 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8773 | psa_status_t expected_status_setup = expected_status_setup_arg; |
| 8774 | psa_status_t expected_status_set_role = expected_status_set_role_arg; |
| 8775 | psa_status_t expected_status_set_password_key = |
| 8776 | expected_status_set_password_key_arg; |
| 8777 | psa_status_t expected_status_input_output = |
| 8778 | expected_status_input_output_arg; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8779 | unsigned char *output_buffer = NULL; |
| 8780 | size_t output_len = 0; |
| 8781 | |
| 8782 | PSA_INIT( ); |
| 8783 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8784 | size_t buf_size = PSA_PAKE_OUTPUT_SIZE(alg, primitive_arg, |
| 8785 | PSA_PAKE_STEP_KEY_SHARE); |
| 8786 | ASSERT_ALLOC( output_buffer, buf_size ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8787 | |
| 8788 | if( pw_data->len > 0 ) |
| 8789 | { |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8790 | psa_set_key_usage_flags( &attributes, key_usage_pw ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8791 | psa_set_key_algorithm( &attributes, alg ); |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8792 | psa_set_key_type( &attributes, key_type_pw ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8793 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8794 | &key ) ); |
| 8795 | } |
| 8796 | |
| 8797 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8798 | psa_pake_cs_set_primitive( &cipher_suite, primitive ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8799 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8800 | |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8801 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8802 | |
| 8803 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8804 | PSA_ERROR_BAD_STATE ); |
| 8805 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8806 | PSA_ERROR_BAD_STATE ); |
| 8807 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8808 | PSA_ERROR_BAD_STATE ); |
| 8809 | TEST_EQUAL( psa_pake_set_role( &operation, role ), |
| 8810 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8811 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
| 8812 | NULL, 0, NULL ), |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8813 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8814 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, NULL, 0), |
Neil Armstrong | 645cccd | 2022-06-08 17:36:23 +0200 | [diff] [blame] | 8815 | PSA_ERROR_BAD_STATE ); |
| 8816 | |
| 8817 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8818 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8819 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8820 | expected_status_setup ); |
| 8821 | if( expected_status_setup != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8822 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8823 | |
Neil Armstrong | 50de0ae | 2022-06-08 17:46:24 +0200 | [diff] [blame] | 8824 | TEST_EQUAL( psa_pake_setup( &operation, &cipher_suite ), |
| 8825 | PSA_ERROR_BAD_STATE ); |
| 8826 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8827 | TEST_EQUAL( psa_pake_set_role( &operation, role), |
| 8828 | expected_status_set_role ); |
| 8829 | if( expected_status_set_role != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8830 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8831 | |
| 8832 | if( pw_data->len > 0 ) |
| 8833 | { |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8834 | TEST_EQUAL( psa_pake_set_password_key( &operation, key ), |
| 8835 | expected_status_set_password_key ); |
| 8836 | if( expected_status_set_password_key != PSA_SUCCESS ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8837 | goto exit; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8838 | } |
| 8839 | |
Neil Armstrong | 707d957 | 2022-06-08 17:31:49 +0200 | [diff] [blame] | 8840 | TEST_EQUAL( psa_pake_set_user( &operation, NULL, 0 ), |
| 8841 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8842 | TEST_EQUAL( psa_pake_set_peer( &operation, NULL, 0 ), |
| 8843 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8844 | |
| 8845 | const uint8_t unsupported_id[] = "abcd"; |
| 8846 | |
| 8847 | TEST_EQUAL( psa_pake_set_user( &operation, unsupported_id, 4 ), |
| 8848 | PSA_ERROR_NOT_SUPPORTED ); |
| 8849 | TEST_EQUAL( psa_pake_set_peer( &operation, unsupported_id, 4 ), |
| 8850 | PSA_ERROR_NOT_SUPPORTED ); |
| 8851 | |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8852 | const size_t size_key_share = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8853 | PSA_PAKE_STEP_KEY_SHARE ); |
| 8854 | const size_t size_zk_public = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8855 | PSA_PAKE_STEP_ZK_PUBLIC ); |
| 8856 | const size_t size_zk_proof = PSA_PAKE_INPUT_SIZE( alg, primitive, |
| 8857 | PSA_PAKE_STEP_ZK_PROOF ); |
| 8858 | |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8859 | /* First round */ |
| 8860 | if( input_first ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8861 | { |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8862 | /* Invalid parameters (input) */ |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8863 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8864 | NULL, 0 ), |
| 8865 | PSA_ERROR_INVALID_ARGUMENT ); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8866 | /* Invalid parameters (step) */ |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 8867 | TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8868 | key, pw_data->len ) , 0 ); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8869 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8870 | output_buffer, size_zk_proof ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8871 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8872 | /* Invalid first step */ |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 8873 | TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8874 | key, pw_data->len ), 0 ); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8875 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PROOF, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8876 | output_buffer, size_zk_proof ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8877 | PSA_ERROR_BAD_STATE ); |
| 8878 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8879 | /* Possibly valid */ |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 8880 | TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8881 | key, pw_data->len ), 0 ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8882 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_KEY_SHARE, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8883 | output_buffer, size_key_share ), |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8884 | expected_status_input_output); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8885 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8886 | if( expected_status_input_output == PSA_SUCCESS ) |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8887 | { |
| 8888 | /* Buffer too large */ |
| 8889 | TEST_EQUAL( psa_pake_input( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8890 | output_buffer, size_zk_public + 1 ), |
| 8891 | PSA_ERROR_INVALID_ARGUMENT ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8892 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8893 | /* The operation's state should be invalidated at this point */ |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8894 | 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] | 8895 | output_buffer, size_zk_public ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8896 | PSA_ERROR_BAD_STATE ); |
| 8897 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8898 | } |
| 8899 | else |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8900 | { |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8901 | /* Invalid parameters (output) */ |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8902 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8903 | NULL, 0, NULL ), |
| 8904 | PSA_ERROR_INVALID_ARGUMENT ); |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8905 | /* Invalid parameters (step) */ |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 8906 | TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8907 | key, pw_data->len ), 0 ); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8908 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF + 10, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8909 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8910 | PSA_ERROR_INVALID_ARGUMENT ); |
| 8911 | /* Invalid first step */ |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 8912 | TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8913 | key, pw_data->len ), 0 ); |
Przemek Stekiel | 7c79548 | 2022-11-15 22:26:12 +0100 | [diff] [blame] | 8914 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PROOF, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8915 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8916 | PSA_ERROR_BAD_STATE ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8917 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8918 | /* Possibly valid */ |
Przemek Stekiel | f82effa | 2022-11-21 15:10:32 +0100 | [diff] [blame] | 8919 | TEST_EQUAL( ecjpake_operation_setup( &operation, &cipher_suite, role, |
| 8920 | key, pw_data->len ), 0 ); |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8921 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_KEY_SHARE, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8922 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8923 | expected_status_input_output ); |
Neil Armstrong | 98506ab | 2022-06-08 17:43:20 +0200 | [diff] [blame] | 8924 | |
Neil Armstrong | 2a73f21 | 2022-09-06 11:34:54 +0200 | [diff] [blame] | 8925 | if( expected_status_input_output == PSA_SUCCESS ) |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8926 | { |
| 8927 | TEST_ASSERT( output_len > 0 ); |
| 8928 | |
| 8929 | /* Buffer too small */ |
| 8930 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
Manuel Pégourié-Gonnard | b63a9ef | 2022-10-06 10:55:19 +0200 | [diff] [blame] | 8931 | output_buffer, size_zk_public - 1, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8932 | PSA_ERROR_BUFFER_TOO_SMALL ); |
| 8933 | |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8934 | /* The operation's state should be invalidated at this point */ |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8935 | TEST_EQUAL( psa_pake_output( &operation, PSA_PAKE_STEP_ZK_PUBLIC, |
Manuel Pégourié-Gonnard | f155ab9 | 2022-10-13 13:11:52 +0200 | [diff] [blame] | 8936 | output_buffer, buf_size, &output_len ), |
Neil Armstrong | 9c8b492 | 2022-06-08 17:59:07 +0200 | [diff] [blame] | 8937 | PSA_ERROR_BAD_STATE ); |
| 8938 | } |
| 8939 | } |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 8940 | |
| 8941 | exit: |
| 8942 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 8943 | PSA_ASSERT( psa_pake_abort( &operation ) ); |
| 8944 | mbedtls_free( output_buffer ); |
| 8945 | PSA_DONE( ); |
| 8946 | } |
| 8947 | /* END_CASE */ |
| 8948 | |
Neil Armstrong | a557cb8 | 2022-06-10 08:58:32 +0200 | [diff] [blame] | 8949 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8950 | void ecjpake_rounds_inject( int alg_arg, int primitive_arg, int hash_arg, |
| 8951 | int client_input_first, int inject_error, |
| 8952 | data_t *pw_data ) |
| 8953 | { |
| 8954 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 8955 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 8956 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 8957 | psa_algorithm_t alg = alg_arg; |
| 8958 | psa_algorithm_t hash_alg = hash_arg; |
| 8959 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 8960 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 8961 | |
| 8962 | PSA_INIT( ); |
| 8963 | |
| 8964 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 8965 | psa_set_key_algorithm( &attributes, alg ); |
| 8966 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 8967 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 8968 | &key ) ); |
| 8969 | |
| 8970 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 8971 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 8972 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 8973 | |
| 8974 | |
| 8975 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 8976 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 8977 | |
| 8978 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 8979 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 8980 | |
| 8981 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 8982 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 8983 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8984 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8985 | client_input_first, 1, inject_error ); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8986 | |
| 8987 | if( inject_error == 1 || inject_error == 2 ) |
| 8988 | goto exit; |
| 8989 | |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 8990 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 8991 | client_input_first, 2, inject_error ); |
Neil Armstrong | 8c2e8a6 | 2022-06-15 15:28:32 +0200 | [diff] [blame] | 8992 | |
| 8993 | exit: |
| 8994 | psa_destroy_key( key ); |
| 8995 | psa_pake_abort( &server ); |
| 8996 | psa_pake_abort( &client ); |
| 8997 | PSA_DONE( ); |
| 8998 | } |
| 8999 | /* END_CASE */ |
| 9000 | |
| 9001 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_JPAKE */ |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9002 | void ecjpake_rounds( int alg_arg, int primitive_arg, int hash_arg, |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9003 | int derive_alg_arg, data_t *pw_data, |
Przemek Stekiel | cd356c3 | 2022-11-20 19:05:20 +0100 | [diff] [blame] | 9004 | int client_input_first, int destroy_key ) |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9005 | { |
| 9006 | psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init(); |
| 9007 | psa_pake_operation_t server = psa_pake_operation_init(); |
| 9008 | psa_pake_operation_t client = psa_pake_operation_init(); |
| 9009 | psa_algorithm_t alg = alg_arg; |
| 9010 | psa_algorithm_t hash_alg = hash_arg; |
| 9011 | psa_algorithm_t derive_alg = derive_alg_arg; |
| 9012 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 9013 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 9014 | psa_key_derivation_operation_t server_derive = |
| 9015 | PSA_KEY_DERIVATION_OPERATION_INIT; |
| 9016 | psa_key_derivation_operation_t client_derive = |
| 9017 | PSA_KEY_DERIVATION_OPERATION_INIT; |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9018 | |
| 9019 | PSA_INIT( ); |
| 9020 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9021 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 9022 | psa_set_key_algorithm( &attributes, alg ); |
| 9023 | psa_set_key_type( &attributes, PSA_KEY_TYPE_PASSWORD ); |
| 9024 | PSA_ASSERT( psa_import_key( &attributes, pw_data->x, pw_data->len, |
| 9025 | &key ) ); |
| 9026 | |
| 9027 | psa_pake_cs_set_algorithm( &cipher_suite, alg ); |
| 9028 | psa_pake_cs_set_primitive( &cipher_suite, primitive_arg ); |
| 9029 | psa_pake_cs_set_hash( &cipher_suite, hash_alg ); |
| 9030 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9031 | /* Get shared key */ |
| 9032 | PSA_ASSERT( psa_key_derivation_setup( &server_derive, derive_alg ) ); |
| 9033 | PSA_ASSERT( psa_key_derivation_setup( &client_derive, derive_alg ) ); |
| 9034 | |
| 9035 | if( PSA_ALG_IS_TLS12_PRF( derive_alg ) || |
| 9036 | PSA_ALG_IS_TLS12_PSK_TO_MS( derive_alg ) ) |
| 9037 | { |
| 9038 | PSA_ASSERT( psa_key_derivation_input_bytes( &server_derive, |
| 9039 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9040 | (const uint8_t*) "", 0) ); |
| 9041 | PSA_ASSERT( psa_key_derivation_input_bytes( &client_derive, |
| 9042 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 9043 | (const uint8_t*) "", 0) ); |
| 9044 | } |
| 9045 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9046 | PSA_ASSERT( psa_pake_setup( &server, &cipher_suite ) ); |
| 9047 | PSA_ASSERT( psa_pake_setup( &client, &cipher_suite ) ); |
| 9048 | |
| 9049 | PSA_ASSERT( psa_pake_set_role( &server, PSA_PAKE_ROLE_SERVER ) ); |
| 9050 | PSA_ASSERT( psa_pake_set_role( &client, PSA_PAKE_ROLE_CLIENT ) ); |
| 9051 | |
| 9052 | PSA_ASSERT( psa_pake_set_password_key( &server, key ) ); |
| 9053 | PSA_ASSERT( psa_pake_set_password_key( &client, key ) ); |
| 9054 | |
Przemek Stekiel | cd356c3 | 2022-11-20 19:05:20 +0100 | [diff] [blame] | 9055 | if( destroy_key == 1 ) |
| 9056 | psa_destroy_key( key ); |
| 9057 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9058 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 9059 | PSA_ERROR_BAD_STATE ); |
| 9060 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 9061 | PSA_ERROR_BAD_STATE ); |
| 9062 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9063 | /* First round */ |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 9064 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 9065 | client_input_first, 1, 0 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9066 | |
Neil Armstrong | 1e85560 | 2022-06-15 11:32:11 +0200 | [diff] [blame] | 9067 | TEST_EQUAL( psa_pake_get_implicit_key( &server, &server_derive ), |
| 9068 | PSA_ERROR_BAD_STATE ); |
| 9069 | TEST_EQUAL( psa_pake_get_implicit_key( &client, &client_derive ), |
| 9070 | PSA_ERROR_BAD_STATE ); |
| 9071 | |
Neil Armstrong | f983caf | 2022-06-15 15:27:48 +0200 | [diff] [blame] | 9072 | /* Second round */ |
Neil Armstrong | 78c4e8e | 2022-09-05 18:08:13 +0200 | [diff] [blame] | 9073 | ecjpake_do_round( alg, primitive_arg, &server, &client, |
| 9074 | client_input_first, 2, 0 ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9075 | |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9076 | PSA_ASSERT( psa_pake_get_implicit_key( &server, &server_derive ) ); |
| 9077 | PSA_ASSERT( psa_pake_get_implicit_key( &client, &client_derive ) ); |
| 9078 | |
| 9079 | exit: |
| 9080 | psa_key_derivation_abort( &server_derive ); |
| 9081 | psa_key_derivation_abort( &client_derive ); |
| 9082 | psa_destroy_key( key ); |
| 9083 | psa_pake_abort( &server ); |
| 9084 | psa_pake_abort( &client ); |
Neil Armstrong | d597bc7 | 2022-05-25 11:28:39 +0200 | [diff] [blame] | 9085 | PSA_DONE( ); |
| 9086 | } |
| 9087 | /* END_CASE */ |
Manuel Pégourié-Gonnard | ec7012d | 2022-10-05 12:17:34 +0200 | [diff] [blame] | 9088 | |
| 9089 | /* BEGIN_CASE */ |
| 9090 | void ecjpake_size_macros( ) |
| 9091 | { |
| 9092 | const psa_algorithm_t alg = PSA_ALG_JPAKE; |
| 9093 | const size_t bits = 256; |
| 9094 | const psa_pake_primitive_t prim = PSA_PAKE_PRIMITIVE( |
| 9095 | PSA_PAKE_PRIMITIVE_TYPE_ECC, PSA_ECC_FAMILY_SECP_R1, bits ); |
| 9096 | const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEY_PAIR( |
| 9097 | PSA_ECC_FAMILY_SECP_R1 ); |
| 9098 | |
| 9099 | // https://armmbed.github.io/mbed-crypto/1.1_PAKE_Extension.0-bet.0/html/pake.html#pake-step-types |
| 9100 | /* The output for KEY_SHARE and ZK_PUBLIC is the same as a public key */ |
| 9101 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9102 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) ); |
| 9103 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9104 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( key_type, bits ) ); |
| 9105 | /* The output for ZK_PROOF is the same bitsize as the curve */ |
| 9106 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9107 | PSA_BITS_TO_BYTES( bits ) ); |
| 9108 | |
| 9109 | /* Input sizes are the same as output sizes */ |
| 9110 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9111 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE) ); |
| 9112 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9113 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC) ); |
| 9114 | TEST_EQUAL( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9115 | PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF) ); |
| 9116 | |
| 9117 | /* These inequalities will always hold even when other PAKEs are added */ |
| 9118 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9119 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9120 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9121 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9122 | TEST_LE_U( PSA_PAKE_OUTPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9123 | PSA_PAKE_OUTPUT_MAX_SIZE ); |
| 9124 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_KEY_SHARE), |
| 9125 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9126 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PUBLIC), |
| 9127 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9128 | TEST_LE_U( PSA_PAKE_INPUT_SIZE(alg, prim, PSA_PAKE_STEP_ZK_PROOF), |
| 9129 | PSA_PAKE_INPUT_MAX_SIZE ); |
| 9130 | } |
| 9131 | /* END_CASE */ |