Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2 | #include <stdint.h> |
mohammad1603 | 2701005 | 2018-07-03 13:16:15 +0300 | [diff] [blame] | 3 | |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 4 | #include "mbedtls/asn1.h" |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 5 | #include "mbedtls/asn1write.h" |
Gilles Peskine | dd2f95b | 2018-08-11 01:22:42 +0200 | [diff] [blame] | 6 | #include "mbedtls/oid.h" |
| 7 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 8 | /* For MBEDTLS_CTR_DRBG_MAX_REQUEST, knowing that psa_generate_random() |
| 9 | * uses mbedtls_ctr_drbg internally. */ |
| 10 | #include "mbedtls/ctr_drbg.h" |
| 11 | |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 12 | #include "psa/crypto.h" |
Ronald Cron | 4184107 | 2020-09-17 15:28:26 +0200 | [diff] [blame] | 13 | #include "psa_crypto_slot_management.h" |
Gilles Peskine | bdc96fd | 2019-08-07 12:08:04 +0200 | [diff] [blame] | 14 | |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 15 | #include "test/asn1_helpers.h" |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 16 | #include "test/psa_crypto_helpers.h" |
Gilles Peskine | e78b002 | 2021-02-13 00:41:11 +0100 | [diff] [blame] | 17 | #include "test/psa_exercise_key.h" |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 18 | #if defined(PSA_CRYPTO_DRIVER_TEST) |
| 19 | #include "test/drivers/test_driver.h" |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 20 | #define TEST_DRIVER_LOCATION PSA_CRYPTO_TEST_DRIVER_LOCATION |
| 21 | #else |
| 22 | #define TEST_DRIVER_LOCATION 0x7fffff |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 23 | #endif |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 24 | |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 25 | /* If this comes up, it's a bug in the test code or in the test data. */ |
| 26 | #define UNUSED 0xdeadbeef |
| 27 | |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 28 | /* Assert that an operation is (not) active. |
| 29 | * This serves as a proxy for checking if the operation is aborted. */ |
| 30 | #define ASSERT_OPERATION_IS_ACTIVE( operation ) TEST_ASSERT( operation.id != 0 ) |
| 31 | #define ASSERT_OPERATION_IS_INACTIVE( operation ) TEST_ASSERT( operation.id == 0 ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 32 | |
| 33 | /** An invalid export length that will never be set by psa_export_key(). */ |
| 34 | static const size_t INVALID_EXPORT_LENGTH = ~0U; |
| 35 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 36 | /** Test if a buffer contains a constant byte value. |
| 37 | * |
| 38 | * `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] | 39 | * |
| 40 | * \param buffer Pointer to the beginning of the buffer. |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 41 | * \param c Expected value of every byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 42 | * \param size Size of the buffer in bytes. |
| 43 | * |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 44 | * \return 1 if the buffer is all-bits-zero. |
| 45 | * \return 0 if there is at least one nonzero byte. |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 46 | */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 47 | 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] | 48 | { |
| 49 | size_t i; |
| 50 | for( i = 0; i < size; i++ ) |
| 51 | { |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 52 | if( ( (unsigned char *) buffer )[i] != c ) |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 53 | return( 0 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 54 | } |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 55 | return( 1 ); |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 56 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 57 | #if defined(MBEDTLS_ASN1_WRITE_C) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 58 | /* Write the ASN.1 INTEGER with the value 2^(bits-1)+x backwards from *p. */ |
| 59 | static int asn1_write_10x( unsigned char **p, |
| 60 | unsigned char *start, |
| 61 | size_t bits, |
| 62 | unsigned char x ) |
| 63 | { |
| 64 | int ret; |
| 65 | int len = bits / 8 + 1; |
Gilles Peskine | 480416a | 2018-06-28 19:04:07 +0200 | [diff] [blame] | 66 | if( bits == 0 ) |
| 67 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
| 68 | if( bits <= 8 && x >= 1 << ( bits - 1 ) ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 69 | return( MBEDTLS_ERR_ASN1_INVALID_DATA ); |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 70 | if( *p < start || *p - start < (ptrdiff_t) len ) |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 71 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); |
| 72 | *p -= len; |
| 73 | ( *p )[len-1] = x; |
| 74 | if( bits % 8 == 0 ) |
| 75 | ( *p )[1] |= 1; |
| 76 | else |
| 77 | ( *p )[0] |= 1 << ( bits % 8 ); |
| 78 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
| 79 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 80 | MBEDTLS_ASN1_INTEGER ) ); |
| 81 | return( len ); |
| 82 | } |
| 83 | |
| 84 | static int construct_fake_rsa_key( unsigned char *buffer, |
| 85 | size_t buffer_size, |
| 86 | unsigned char **p, |
| 87 | size_t bits, |
| 88 | int keypair ) |
| 89 | { |
| 90 | size_t half_bits = ( bits + 1 ) / 2; |
| 91 | int ret; |
| 92 | int len = 0; |
| 93 | /* Construct something that looks like a DER encoding of |
| 94 | * as defined by PKCS#1 v2.2 (RFC 8017) section A.1.2: |
| 95 | * RSAPrivateKey ::= SEQUENCE { |
| 96 | * version Version, |
| 97 | * modulus INTEGER, -- n |
| 98 | * publicExponent INTEGER, -- e |
| 99 | * privateExponent INTEGER, -- d |
| 100 | * prime1 INTEGER, -- p |
| 101 | * prime2 INTEGER, -- q |
| 102 | * exponent1 INTEGER, -- d mod (p-1) |
| 103 | * exponent2 INTEGER, -- d mod (q-1) |
| 104 | * coefficient INTEGER, -- (inverse of q) mod p |
| 105 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 106 | * } |
| 107 | * Or, for a public key, the same structure with only |
| 108 | * version, modulus and publicExponent. |
| 109 | */ |
| 110 | *p = buffer + buffer_size; |
| 111 | if( keypair ) |
| 112 | { |
| 113 | MBEDTLS_ASN1_CHK_ADD( len, /* pq */ |
| 114 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 115 | MBEDTLS_ASN1_CHK_ADD( len, /* dq */ |
| 116 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 117 | MBEDTLS_ASN1_CHK_ADD( len, /* dp */ |
| 118 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 119 | MBEDTLS_ASN1_CHK_ADD( len, /* q */ |
| 120 | asn1_write_10x( p, buffer, half_bits, 1 ) ); |
| 121 | MBEDTLS_ASN1_CHK_ADD( len, /* p != q to pass mbedtls sanity checks */ |
| 122 | asn1_write_10x( p, buffer, half_bits, 3 ) ); |
| 123 | MBEDTLS_ASN1_CHK_ADD( len, /* d */ |
| 124 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 125 | } |
| 126 | MBEDTLS_ASN1_CHK_ADD( len, /* e = 65537 */ |
| 127 | asn1_write_10x( p, buffer, 17, 1 ) ); |
| 128 | MBEDTLS_ASN1_CHK_ADD( len, /* n */ |
| 129 | asn1_write_10x( p, buffer, bits, 1 ) ); |
| 130 | if( keypair ) |
| 131 | MBEDTLS_ASN1_CHK_ADD( len, /* version = 0 */ |
| 132 | mbedtls_asn1_write_int( p, buffer, 0 ) ); |
| 133 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, buffer, len ) ); |
| 134 | { |
| 135 | const unsigned char tag = |
| 136 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE; |
| 137 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, buffer, tag ) ); |
| 138 | } |
| 139 | return( len ); |
| 140 | } |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 141 | #endif /* MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 142 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 143 | int exercise_mac_setup( psa_key_type_t key_type, |
| 144 | const unsigned char *key_bytes, |
| 145 | size_t key_length, |
| 146 | psa_algorithm_t alg, |
| 147 | psa_mac_operation_t *operation, |
| 148 | psa_status_t *status ) |
| 149 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 150 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 151 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 152 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 153 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 154 | psa_set_key_algorithm( &attributes, alg ); |
| 155 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 156 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 157 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 158 | *status = psa_mac_sign_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 159 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 160 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 161 | /* If setup failed, reproduce the failure, so that the caller can |
| 162 | * test the resulting state of the operation object. */ |
| 163 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 164 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 165 | TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 168 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 169 | return( 1 ); |
| 170 | |
| 171 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 172 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 173 | return( 0 ); |
| 174 | } |
| 175 | |
| 176 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 177 | const unsigned char *key_bytes, |
| 178 | size_t key_length, |
| 179 | psa_algorithm_t alg, |
| 180 | psa_cipher_operation_t *operation, |
| 181 | psa_status_t *status ) |
| 182 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 183 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 184 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 185 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 186 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 187 | psa_set_key_algorithm( &attributes, alg ); |
| 188 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 189 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 190 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 191 | *status = psa_cipher_encrypt_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 192 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 193 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 194 | /* If setup failed, reproduce the failure, so that the caller can |
| 195 | * test the resulting state of the operation object. */ |
| 196 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 197 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 198 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ), |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 199 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 202 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 203 | return( 1 ); |
| 204 | |
| 205 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 206 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 207 | return( 0 ); |
| 208 | } |
| 209 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 210 | 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] | 211 | { |
| 212 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 213 | 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] | 214 | uint8_t buffer[1]; |
| 215 | size_t length; |
| 216 | int ok = 0; |
| 217 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 218 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 219 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 220 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 221 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 222 | TEST_EQUAL( psa_get_key_attributes( key, &attributes ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 223 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 224 | TEST_EQUAL( |
| 225 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 226 | TEST_EQUAL( |
| 227 | 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] | 228 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 229 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 230 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 231 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 232 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 233 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 234 | TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 235 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 236 | TEST_EQUAL( psa_export_public_key( key, |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 237 | buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 238 | PSA_ERROR_INVALID_HANDLE ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 239 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 240 | ok = 1; |
| 241 | |
| 242 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 243 | /* |
| 244 | * Key attributes may have been returned by psa_get_key_attributes() |
| 245 | * thus reset them as required. |
| 246 | */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 247 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 248 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 249 | return( ok ); |
| 250 | } |
| 251 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 252 | /* Assert that a key isn't reported as having a slot number. */ |
| 253 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 254 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 255 | do \ |
| 256 | { \ |
| 257 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 258 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 259 | attributes, \ |
| 260 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 261 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 262 | } \ |
| 263 | while( 0 ) |
| 264 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 265 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 266 | ( (void) 0 ) |
| 267 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 268 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 269 | /* An overapproximation of the amount of storage needed for a key of the |
| 270 | * given type and with the given content. The API doesn't make it easy |
| 271 | * to find a good value for the size. The current implementation doesn't |
| 272 | * care about the value anyway. */ |
| 273 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 274 | ( data )->len |
| 275 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 276 | typedef enum { |
| 277 | IMPORT_KEY = 0, |
| 278 | GENERATE_KEY = 1, |
| 279 | DERIVE_KEY = 2 |
| 280 | } generate_method; |
| 281 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 282 | typedef enum |
| 283 | { |
| 284 | DO_NOT_SET_LENGTHS = 0, |
| 285 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 286 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 287 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 288 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 289 | typedef enum |
| 290 | { |
| 291 | USE_NULL_TAG = 0, |
| 292 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 293 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 294 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 295 | /*! |
| 296 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 297 | * \param key_type_arg Type of key passed in |
| 298 | * \param key_data The encryption / decryption key data |
| 299 | * \param alg_arg The type of algorithm used |
| 300 | * \param nonce Nonce data |
| 301 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 302 | * \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] | 303 | * feed additional data in to be encrypted / |
| 304 | * decrypted. If -1, no chunking. |
| 305 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 306 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 307 | * the data in to be encrypted / decrypted. If |
| 308 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 309 | * \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] | 310 | * expected here, this controls whether or not |
| 311 | * to set lengths, and in what order with |
| 312 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 313 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 314 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 315 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 316 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 317 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 318 | */ |
| 319 | static int aead_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 320 | int alg_arg, |
| 321 | data_t *nonce, |
| 322 | data_t *additional_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 323 | int ad_part_len_arg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 324 | data_t *input_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 325 | int data_part_len_arg, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 326 | set_lengths_method_t set_lengths_method, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 327 | data_t *expected_output, |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 328 | int is_encrypt, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 329 | int do_zero_parts ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 330 | { |
| 331 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 332 | psa_key_type_t key_type = key_type_arg; |
| 333 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 334 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 335 | unsigned char *output_data = NULL; |
| 336 | unsigned char *part_data = NULL; |
| 337 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 338 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 339 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 340 | size_t output_size = 0; |
| 341 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 342 | size_t output_length = 0; |
| 343 | size_t key_bits = 0; |
| 344 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 345 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 346 | size_t part_length = 0; |
| 347 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 348 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 349 | size_t ad_part_len = 0; |
| 350 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 351 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 352 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 353 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 354 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 355 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 356 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 357 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 358 | PSA_ASSERT( psa_crypto_init( ) ); |
| 359 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 360 | if( is_encrypt ) |
| 361 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 362 | else |
| 363 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 364 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 365 | psa_set_key_algorithm( &attributes, alg ); |
| 366 | psa_set_key_type( &attributes, key_type ); |
| 367 | |
| 368 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 369 | &key ) ); |
| 370 | |
| 371 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 372 | key_bits = psa_get_key_bits( &attributes ); |
| 373 | |
| 374 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 375 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 376 | if( is_encrypt ) |
| 377 | { |
| 378 | /* Tag gets written at end of buffer. */ |
| 379 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 380 | ( input_data->len + |
| 381 | tag_length ) ); |
| 382 | data_true_size = input_data->len; |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 387 | ( input_data->len - |
| 388 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 389 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 390 | /* Do not want to attempt to decrypt tag. */ |
| 391 | data_true_size = input_data->len - tag_length; |
| 392 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 393 | |
| 394 | ASSERT_ALLOC( output_data, output_size ); |
| 395 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 396 | if( is_encrypt ) |
| 397 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 398 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 399 | TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 400 | } |
| 401 | else |
| 402 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 403 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 404 | TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 405 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 406 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 407 | ASSERT_ALLOC( final_data, final_output_size ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 408 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 409 | if( is_encrypt ) |
| 410 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 411 | else |
| 412 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 413 | |
| 414 | /* If the operation is not supported, just skip and not fail in case the |
| 415 | * encryption involves a common limitation of cryptography hardwares and |
| 416 | * an alternative implementation. */ |
| 417 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 418 | { |
| 419 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 420 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 421 | } |
| 422 | |
| 423 | PSA_ASSERT( status ); |
| 424 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 425 | if( set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 426 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 427 | else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 428 | { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 429 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 430 | data_true_size ) ); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 431 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 432 | } |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 433 | else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 434 | { |
| 435 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 436 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 437 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 438 | data_true_size ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 439 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 440 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 441 | if( ad_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 442 | { |
| 443 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 444 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 445 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 446 | for( part_offset = 0, part_count = 0; |
| 447 | part_offset < additional_data->len; |
| 448 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 449 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 450 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 451 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 452 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 453 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 454 | else if( additional_data->len - part_offset < ad_part_len ) |
| 455 | { |
| 456 | part_length = additional_data->len - part_offset; |
| 457 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 458 | else |
| 459 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 460 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | PSA_ASSERT( psa_aead_update_ad( &operation, |
| 464 | additional_data->x + part_offset, |
| 465 | part_length ) ); |
| 466 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 467 | } |
| 468 | } |
| 469 | else |
| 470 | { |
| 471 | /* Pass additional data in one go. */ |
| 472 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 473 | additional_data->len ) ); |
| 474 | } |
| 475 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 476 | if( data_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 477 | { |
| 478 | /* Pass data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 479 | data_part_len = ( size_t ) data_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 480 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 481 | ( size_t ) data_part_len ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 482 | |
| 483 | ASSERT_ALLOC( part_data, part_data_size ); |
| 484 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 485 | for( part_offset = 0, part_count = 0; |
| 486 | part_offset < data_true_size; |
| 487 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 488 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 489 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 490 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 491 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 492 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 493 | else if( ( data_true_size - part_offset ) < data_part_len ) |
| 494 | { |
| 495 | part_length = ( data_true_size - part_offset ); |
| 496 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 497 | else |
| 498 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 499 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | PSA_ASSERT( psa_aead_update( &operation, |
| 503 | ( input_data->x + part_offset ), |
| 504 | part_length, part_data, |
| 505 | part_data_size, |
| 506 | &output_part_length ) ); |
| 507 | |
| 508 | if( output_data && output_part_length ) |
| 509 | { |
| 510 | memcpy( ( output_data + part_offset ), part_data, |
| 511 | output_part_length ); |
| 512 | } |
| 513 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 514 | output_length += output_part_length; |
| 515 | } |
| 516 | } |
| 517 | else |
| 518 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 519 | /* Pass all data in one go. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 520 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 521 | data_true_size, output_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 522 | output_size, &output_length ) ); |
| 523 | } |
| 524 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 525 | if( is_encrypt ) |
| 526 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 527 | final_output_size, |
| 528 | &output_part_length, |
| 529 | tag_buffer, tag_length, |
| 530 | &tag_size ) ); |
| 531 | else |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 532 | { |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 533 | PSA_ASSERT( psa_aead_verify( &operation, final_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 534 | final_output_size, |
| 535 | &output_part_length, |
| 536 | ( input_data->x + data_true_size ), |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 537 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 538 | } |
| 539 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 540 | if( output_data && output_part_length ) |
| 541 | memcpy( ( output_data + output_length ), final_data, |
| 542 | output_part_length ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 543 | |
| 544 | output_length += output_part_length; |
| 545 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 546 | |
| 547 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 548 | * should be exact.*/ |
| 549 | if( is_encrypt ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 550 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 551 | TEST_EQUAL( tag_length, tag_size ); |
| 552 | |
| 553 | if( output_data && tag_length ) |
| 554 | memcpy( ( output_data + output_length ), tag_buffer, |
| 555 | tag_length ); |
| 556 | |
| 557 | output_length += tag_length; |
| 558 | |
| 559 | TEST_EQUAL( output_length, |
| 560 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 561 | input_data->len ) ); |
| 562 | TEST_ASSERT( output_length <= |
| 563 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 564 | } |
| 565 | else |
| 566 | { |
| 567 | TEST_EQUAL( output_length, |
| 568 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, |
| 569 | input_data->len ) ); |
| 570 | TEST_ASSERT( output_length <= |
| 571 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 572 | } |
| 573 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 574 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 575 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 576 | output_data, output_length ); |
| 577 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 578 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 579 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 580 | |
| 581 | exit: |
| 582 | psa_destroy_key( key ); |
| 583 | psa_aead_abort( &operation ); |
| 584 | mbedtls_free( output_data ); |
| 585 | mbedtls_free( part_data ); |
| 586 | mbedtls_free( final_data ); |
| 587 | PSA_DONE( ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 588 | |
| 589 | return( test_ok ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 590 | } |
| 591 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 592 | /* END_HEADER */ |
| 593 | |
| 594 | /* BEGIN_DEPENDENCIES |
| 595 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 596 | * END_DEPENDENCIES |
| 597 | */ |
| 598 | |
| 599 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 600 | void static_checks( ) |
| 601 | { |
| 602 | size_t max_truncated_mac_size = |
| 603 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 604 | |
| 605 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 606 | * encoding. The shifted mask is the maximum truncated value. The |
| 607 | * untruncated algorithm may be one byte larger. */ |
| 608 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
| 609 | } |
| 610 | /* END_CASE */ |
| 611 | |
| 612 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 613 | void import_with_policy( int type_arg, |
| 614 | int usage_arg, int alg_arg, |
| 615 | int expected_status_arg ) |
| 616 | { |
| 617 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 618 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 619 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 620 | psa_key_type_t type = type_arg; |
| 621 | psa_key_usage_t usage = usage_arg; |
| 622 | psa_algorithm_t alg = alg_arg; |
| 623 | psa_status_t expected_status = expected_status_arg; |
| 624 | const uint8_t key_material[16] = {0}; |
| 625 | psa_status_t status; |
| 626 | |
| 627 | PSA_ASSERT( psa_crypto_init( ) ); |
| 628 | |
| 629 | psa_set_key_type( &attributes, type ); |
| 630 | psa_set_key_usage_flags( &attributes, usage ); |
| 631 | psa_set_key_algorithm( &attributes, alg ); |
| 632 | |
| 633 | status = psa_import_key( &attributes, |
| 634 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 635 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 636 | TEST_EQUAL( status, expected_status ); |
| 637 | if( status != PSA_SUCCESS ) |
| 638 | goto exit; |
| 639 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 640 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 641 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 642 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 643 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 644 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 645 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 646 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 647 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 648 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 649 | |
| 650 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 651 | /* |
| 652 | * Key attributes may have been returned by psa_get_key_attributes() |
| 653 | * thus reset them as required. |
| 654 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 655 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 656 | |
| 657 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 658 | PSA_DONE( ); |
| 659 | } |
| 660 | /* END_CASE */ |
| 661 | |
| 662 | /* BEGIN_CASE */ |
| 663 | void import_with_data( data_t *data, int type_arg, |
| 664 | int attr_bits_arg, |
| 665 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 666 | { |
| 667 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 668 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 669 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 670 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 671 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 672 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 673 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 674 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 675 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 676 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 677 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 678 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 679 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 680 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 681 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 682 | if( status != PSA_SUCCESS ) |
| 683 | goto exit; |
| 684 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 685 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 686 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 687 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 688 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 689 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 690 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 691 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 692 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 693 | |
| 694 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 695 | /* |
| 696 | * Key attributes may have been returned by psa_get_key_attributes() |
| 697 | * thus reset them as required. |
| 698 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 699 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 700 | |
| 701 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 702 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 703 | } |
| 704 | /* END_CASE */ |
| 705 | |
| 706 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 707 | void import_large_key( int type_arg, int byte_size_arg, |
| 708 | int expected_status_arg ) |
| 709 | { |
| 710 | psa_key_type_t type = type_arg; |
| 711 | size_t byte_size = byte_size_arg; |
| 712 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 713 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 714 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 715 | psa_status_t status; |
| 716 | uint8_t *buffer = NULL; |
| 717 | size_t buffer_size = byte_size + 1; |
| 718 | size_t n; |
| 719 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 720 | /* Skip the test case if the target running the test cannot |
| 721 | * accomodate large keys due to heap size constraints */ |
| 722 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 723 | memset( buffer, 'K', byte_size ); |
| 724 | |
| 725 | PSA_ASSERT( psa_crypto_init( ) ); |
| 726 | |
| 727 | /* Try importing the key */ |
| 728 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 729 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 730 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 731 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 732 | TEST_EQUAL( status, expected_status ); |
| 733 | |
| 734 | if( status == PSA_SUCCESS ) |
| 735 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 736 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 737 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 738 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 739 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 740 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 741 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 742 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 743 | for( n = 0; n < byte_size; n++ ) |
| 744 | TEST_EQUAL( buffer[n], 'K' ); |
| 745 | for( n = byte_size; n < buffer_size; n++ ) |
| 746 | TEST_EQUAL( buffer[n], 0 ); |
| 747 | } |
| 748 | |
| 749 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 750 | /* |
| 751 | * Key attributes may have been returned by psa_get_key_attributes() |
| 752 | * thus reset them as required. |
| 753 | */ |
| 754 | psa_reset_key_attributes( &attributes ); |
| 755 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 756 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 757 | PSA_DONE( ); |
| 758 | mbedtls_free( buffer ); |
| 759 | } |
| 760 | /* END_CASE */ |
| 761 | |
Andrzej Kurek | 77b8e09 | 2022-01-17 15:29:38 +0100 | [diff] [blame] | 762 | /* BEGIN_CASE depends_on:MBEDTLS_ASN1_WRITE_C */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 763 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 764 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 765 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 766 | size_t bits = bits_arg; |
| 767 | psa_status_t expected_status = expected_status_arg; |
| 768 | psa_status_t status; |
| 769 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 770 | 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] | 771 | size_t buffer_size = /* Slight overapproximations */ |
| 772 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 773 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 774 | unsigned char *p; |
| 775 | int ret; |
| 776 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 777 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 778 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 779 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 780 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 781 | |
| 782 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 783 | bits, keypair ) ) >= 0 ); |
| 784 | length = ret; |
| 785 | |
| 786 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 787 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 788 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 789 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 790 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 791 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 792 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 793 | |
| 794 | exit: |
| 795 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 796 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 797 | } |
| 798 | /* END_CASE */ |
| 799 | |
| 800 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 801 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 802 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 803 | int usage_arg, int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 804 | int lifetime_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 805 | int expected_bits, |
| 806 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 807 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 808 | int canonical_input ) |
| 809 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 810 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 811 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 812 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 813 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 814 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 815 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 816 | unsigned char *exported = NULL; |
| 817 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 818 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 819 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 820 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 821 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 822 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 823 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 824 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 825 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 826 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 827 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 828 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 829 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 830 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 831 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 832 | psa_set_key_algorithm( &attributes, alg ); |
| 833 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 834 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 835 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 836 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 837 | |
| 838 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 839 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 840 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 841 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 842 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 843 | |
| 844 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 845 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 846 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 847 | |
| 848 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 849 | * and export_size. On errors, the exported length must be 0. */ |
| 850 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 851 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 852 | TEST_ASSERT( exported_length <= export_size ); |
| 853 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 854 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 855 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 856 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 857 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 858 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 859 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 860 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 861 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 862 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 863 | * this validates the canonical representations. For canonical inputs, |
| 864 | * this doesn't directly validate the implementation, but it still helps |
| 865 | * by cross-validating the test data with the sanity check code. */ |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 866 | if( !psa_key_lifetime_is_external( lifetime ) ) |
| 867 | { |
| 868 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
| 869 | goto exit; |
| 870 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 871 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 872 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 873 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 874 | else |
| 875 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 876 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 877 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 878 | &key2 ) ); |
| 879 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 880 | reexported, |
| 881 | export_size, |
| 882 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 883 | ASSERT_COMPARE( exported, exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 884 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 885 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 886 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 887 | TEST_ASSERT( exported_length <= |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 888 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 889 | psa_get_key_bits( &got_attributes ) ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 890 | TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 891 | |
| 892 | destroy: |
| 893 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 894 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 895 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 896 | |
| 897 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 898 | /* |
| 899 | * Key attributes may have been returned by psa_get_key_attributes() |
| 900 | * thus reset them as required. |
| 901 | */ |
| 902 | psa_reset_key_attributes( &got_attributes ); |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 903 | psa_destroy_key( key ) ; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 904 | mbedtls_free( exported ); |
| 905 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 906 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 907 | } |
| 908 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 909 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 910 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 911 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 912 | int type_arg, |
| 913 | int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 914 | int lifetime_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 915 | int export_size_delta, |
| 916 | int expected_export_status_arg, |
| 917 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 918 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 919 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 920 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 921 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 922 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 923 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 924 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 925 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 926 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 927 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 928 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 929 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 930 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 931 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 932 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 933 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 934 | psa_set_key_algorithm( &attributes, alg ); |
| 935 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 936 | |
| 937 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 938 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 939 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 940 | /* Export the public key */ |
| 941 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 942 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 943 | exported, export_size, |
| 944 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 945 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 946 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 947 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 948 | 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] | 949 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 950 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 951 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 952 | TEST_ASSERT( expected_public_key->len <= |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 953 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 954 | TEST_ASSERT( expected_public_key->len <= |
| 955 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 956 | TEST_ASSERT( expected_public_key->len <= |
| 957 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 958 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 959 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 960 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 961 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 962 | /* |
| 963 | * Key attributes may have been returned by psa_get_key_attributes() |
| 964 | * thus reset them as required. |
| 965 | */ |
| 966 | psa_reset_key_attributes( &attributes ); |
| 967 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 968 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 969 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 970 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 971 | } |
| 972 | /* END_CASE */ |
| 973 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 974 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 975 | void import_and_exercise_key( data_t *data, |
| 976 | int type_arg, |
| 977 | int bits_arg, |
| 978 | int alg_arg ) |
| 979 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 980 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 981 | psa_key_type_t type = type_arg; |
| 982 | size_t bits = bits_arg; |
| 983 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 984 | 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] | 985 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 986 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 987 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 988 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 989 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 990 | psa_set_key_usage_flags( &attributes, usage ); |
| 991 | psa_set_key_algorithm( &attributes, alg ); |
| 992 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 993 | |
| 994 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 995 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 996 | |
| 997 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 998 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 999 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 1000 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1001 | |
| 1002 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1003 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1004 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1005 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1006 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1007 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1008 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1009 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1010 | /* |
| 1011 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1012 | * thus reset them as required. |
| 1013 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1014 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1015 | |
| 1016 | psa_reset_key_attributes( &attributes ); |
| 1017 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1018 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1019 | } |
| 1020 | /* END_CASE */ |
| 1021 | |
| 1022 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1023 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1024 | int bits_arg, int expected_bits_arg, |
| 1025 | int usage_arg, int expected_usage_arg, |
| 1026 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1027 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1028 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1029 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1030 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1031 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1032 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1033 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1034 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1035 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1036 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1037 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1038 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1039 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1040 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1041 | psa_set_key_usage_flags( &attributes, usage ); |
| 1042 | psa_set_key_algorithm( &attributes, alg ); |
| 1043 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1044 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1045 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1046 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1047 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1048 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1049 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1050 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1051 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1052 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1053 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1054 | |
| 1055 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1056 | /* |
| 1057 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1058 | * thus reset them as required. |
| 1059 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1060 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1061 | |
| 1062 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1063 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1064 | } |
| 1065 | /* END_CASE */ |
| 1066 | |
| 1067 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1068 | void check_key_policy( int type_arg, int bits_arg, |
| 1069 | int usage_arg, int alg_arg ) |
| 1070 | { |
| 1071 | 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] | 1072 | usage_arg, |
| 1073 | mbedtls_test_update_key_usage_flags( usage_arg ), |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1074 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1075 | goto exit; |
| 1076 | } |
| 1077 | /* END_CASE */ |
| 1078 | |
| 1079 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1080 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1081 | { |
| 1082 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1083 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1084 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1085 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1086 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1087 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1088 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1089 | |
| 1090 | memset( &zero, 0, sizeof( zero ) ); |
| 1091 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1092 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1093 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1094 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1095 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1096 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1097 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1098 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1099 | |
| 1100 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1101 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1102 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1103 | |
| 1104 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1105 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1106 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1107 | |
| 1108 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1109 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1110 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1111 | } |
| 1112 | /* END_CASE */ |
| 1113 | |
| 1114 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1115 | void mac_key_policy( int policy_usage_arg, |
| 1116 | int policy_alg_arg, |
| 1117 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1118 | data_t *key_data, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1119 | int exercise_alg_arg, |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1120 | int expected_status_sign_arg, |
| 1121 | int expected_status_verify_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1122 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1123 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1124 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1125 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1126 | psa_key_type_t key_type = key_type_arg; |
| 1127 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1128 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1129 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1130 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1131 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1132 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1133 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1134 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1135 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1136 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1137 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1138 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1139 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1140 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1141 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1142 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1143 | |
gabor-mezei-arm | 40d5cd8 | 2021-06-29 11:06:16 +0200 | [diff] [blame] | 1144 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
| 1145 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1146 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1147 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1148 | TEST_EQUAL( status, expected_status_sign ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1149 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1150 | /* Calculate the MAC, one-shot case. */ |
| 1151 | uint8_t input[128] = {0}; |
| 1152 | size_t mac_len; |
| 1153 | TEST_EQUAL( psa_mac_compute( key, exercise_alg, |
| 1154 | input, 128, |
| 1155 | mac, PSA_MAC_MAX_SIZE, &mac_len ), |
| 1156 | expected_status_sign ); |
| 1157 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1158 | /* Calculate the MAC, multi-part case. */ |
| 1159 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1160 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
| 1161 | if( status == PSA_SUCCESS ) |
| 1162 | { |
| 1163 | status = psa_mac_update( &operation, input, 128 ); |
| 1164 | if( status == PSA_SUCCESS ) |
| 1165 | TEST_EQUAL( psa_mac_sign_finish( &operation, mac, PSA_MAC_MAX_SIZE, |
| 1166 | &mac_len ), |
| 1167 | expected_status_sign ); |
| 1168 | else |
| 1169 | TEST_EQUAL( status, expected_status_sign ); |
| 1170 | } |
| 1171 | else |
| 1172 | { |
| 1173 | TEST_EQUAL( status, expected_status_sign ); |
| 1174 | } |
| 1175 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 1176 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1177 | /* Verify correct MAC, one-shot case. */ |
| 1178 | status = psa_mac_verify( key, exercise_alg, input, 128, |
| 1179 | mac, mac_len ); |
| 1180 | |
| 1181 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1182 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1183 | else |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1184 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1185 | |
Neil Armstrong | 3af9b97 | 2022-02-07 12:20:21 +0100 | [diff] [blame] | 1186 | /* Verify correct MAC, multi-part case. */ |
| 1187 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
| 1188 | if( status == PSA_SUCCESS ) |
| 1189 | { |
| 1190 | status = psa_mac_update( &operation, input, 128 ); |
| 1191 | if( status == PSA_SUCCESS ) |
| 1192 | { |
| 1193 | status = psa_mac_verify_finish( &operation, mac, mac_len ); |
| 1194 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1195 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1196 | else |
| 1197 | TEST_EQUAL( status, expected_status_verify ); |
| 1198 | } |
| 1199 | else |
| 1200 | { |
| 1201 | TEST_EQUAL( status, expected_status_verify ); |
| 1202 | } |
| 1203 | } |
| 1204 | else |
| 1205 | { |
| 1206 | TEST_EQUAL( status, expected_status_verify ); |
| 1207 | } |
| 1208 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1209 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1210 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1211 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1212 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1213 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1214 | |
| 1215 | exit: |
| 1216 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1217 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1218 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1219 | } |
| 1220 | /* END_CASE */ |
| 1221 | |
| 1222 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1223 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1224 | int policy_alg, |
| 1225 | int key_type, |
| 1226 | data_t *key_data, |
| 1227 | int exercise_alg ) |
| 1228 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1229 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1230 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1231 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1232 | psa_key_usage_t policy_usage = policy_usage_arg; |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame^] | 1233 | size_t output_buffer_size = 0; |
| 1234 | size_t input_buffer_size = 0; |
| 1235 | size_t output_length = 0; |
| 1236 | uint8_t *output = NULL; |
| 1237 | uint8_t *input = NULL; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1238 | psa_status_t status; |
| 1239 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame^] | 1240 | input_buffer_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( exercise_alg ); |
| 1241 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, exercise_alg, |
| 1242 | input_buffer_size ); |
| 1243 | |
| 1244 | ASSERT_ALLOC( input, input_buffer_size ); |
| 1245 | ASSERT_ALLOC( output, output_buffer_size ); |
| 1246 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1247 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1248 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1249 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1250 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1251 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1252 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1253 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1254 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1255 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1256 | /* Check if no key usage flag implication is done */ |
| 1257 | TEST_EQUAL( policy_usage, |
| 1258 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1259 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame^] | 1260 | /* Encrypt check, one-shot */ |
| 1261 | status = psa_cipher_encrypt( key, exercise_alg, input, input_buffer_size, |
| 1262 | output, output_buffer_size, |
| 1263 | &output_length); |
| 1264 | if( policy_alg == exercise_alg && |
| 1265 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1266 | PSA_ASSERT( status ); |
| 1267 | else |
| 1268 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1269 | |
| 1270 | /* Encrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1271 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1272 | if( policy_alg == exercise_alg && |
| 1273 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1274 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1275 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1276 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1277 | psa_cipher_abort( &operation ); |
| 1278 | |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame^] | 1279 | /* Decrypt check, one-shot */ |
| 1280 | status = psa_cipher_decrypt( key, exercise_alg, output, output_buffer_size, |
| 1281 | input, input_buffer_size, |
| 1282 | &output_length); |
| 1283 | if( policy_alg == exercise_alg && |
| 1284 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
| 1285 | PSA_ASSERT( status ); |
| 1286 | else |
| 1287 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1288 | |
| 1289 | /* Decrypt check, multi-part */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1290 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1291 | if( policy_alg == exercise_alg && |
| 1292 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1293 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1294 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1295 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1296 | |
| 1297 | exit: |
| 1298 | psa_cipher_abort( &operation ); |
Neil Armstrong | 78aeaf8 | 2022-02-07 14:50:35 +0100 | [diff] [blame^] | 1299 | mbedtls_free( input ); |
| 1300 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1301 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1302 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1303 | } |
| 1304 | /* END_CASE */ |
| 1305 | |
| 1306 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1307 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1308 | int policy_alg, |
| 1309 | int key_type, |
| 1310 | data_t *key_data, |
| 1311 | int nonce_length_arg, |
| 1312 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1313 | int exercise_alg, |
| 1314 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1315 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1316 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1317 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1318 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1319 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1320 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1321 | unsigned char nonce[16] = {0}; |
| 1322 | size_t nonce_length = nonce_length_arg; |
| 1323 | unsigned char tag[16]; |
| 1324 | size_t tag_length = tag_length_arg; |
| 1325 | size_t output_length; |
| 1326 | |
| 1327 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 1328 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 1329 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1330 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1331 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1332 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1333 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1334 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1335 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1336 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1337 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1338 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1339 | /* Check if no key usage implication is done */ |
| 1340 | TEST_EQUAL( policy_usage, |
| 1341 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1342 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1343 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1344 | nonce, nonce_length, |
| 1345 | NULL, 0, |
| 1346 | NULL, 0, |
| 1347 | tag, tag_length, |
| 1348 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1349 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1350 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1351 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1352 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1353 | |
| 1354 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1355 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1356 | nonce, nonce_length, |
| 1357 | NULL, 0, |
| 1358 | tag, tag_length, |
| 1359 | NULL, 0, |
| 1360 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1361 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 1362 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1363 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1364 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1365 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1366 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1367 | |
| 1368 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1369 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1370 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1371 | } |
| 1372 | /* END_CASE */ |
| 1373 | |
| 1374 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1375 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1376 | int policy_alg, |
| 1377 | int key_type, |
| 1378 | data_t *key_data, |
| 1379 | int exercise_alg ) |
| 1380 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1381 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1382 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1383 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1384 | psa_status_t status; |
| 1385 | size_t key_bits; |
| 1386 | size_t buffer_length; |
| 1387 | unsigned char *buffer = NULL; |
| 1388 | size_t output_length; |
| 1389 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1390 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1391 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1392 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1393 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1394 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1395 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1396 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1397 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1398 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1399 | /* Check if no key usage implication is done */ |
| 1400 | TEST_EQUAL( policy_usage, |
| 1401 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1402 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1403 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1404 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1405 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1406 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1407 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1408 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1409 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1410 | NULL, 0, |
| 1411 | NULL, 0, |
| 1412 | buffer, buffer_length, |
| 1413 | &output_length ); |
| 1414 | if( policy_alg == exercise_alg && |
| 1415 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1416 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1417 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1418 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1419 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1420 | if( buffer_length != 0 ) |
| 1421 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1422 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1423 | buffer, buffer_length, |
| 1424 | NULL, 0, |
| 1425 | buffer, buffer_length, |
| 1426 | &output_length ); |
| 1427 | if( policy_alg == exercise_alg && |
| 1428 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1429 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1430 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1431 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1432 | |
| 1433 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1434 | /* |
| 1435 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1436 | * thus reset them as required. |
| 1437 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1438 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1439 | |
| 1440 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1441 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1442 | mbedtls_free( buffer ); |
| 1443 | } |
| 1444 | /* END_CASE */ |
| 1445 | |
| 1446 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1447 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1448 | int policy_alg, |
| 1449 | int key_type, |
| 1450 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1451 | int exercise_alg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1452 | int payload_length_arg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1453 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1454 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1455 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1456 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1457 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 1458 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1459 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1460 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1461 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1462 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1463 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1464 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1465 | int compatible_alg = payload_length_arg > 0; |
| 1466 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1467 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1468 | size_t signature_length; |
| 1469 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1470 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1471 | in the expected usage flags. */ |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1472 | TEST_EQUAL( expected_usage, |
| 1473 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1474 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1475 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1476 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1477 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1478 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1479 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1480 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1481 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1482 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1483 | |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1484 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1485 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1486 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1487 | payload, payload_length, |
| 1488 | signature, sizeof( signature ), |
| 1489 | &signature_length ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1490 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1491 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1492 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1493 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1494 | |
| 1495 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1496 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1497 | payload, payload_length, |
| 1498 | signature, sizeof( signature ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1499 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1500 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1501 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1502 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1503 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 1504 | if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) && |
gabor-mezei-arm | d851d68 | 2021-06-28 14:53:49 +0200 | [diff] [blame] | 1505 | PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) ) |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1506 | { |
| 1507 | status = psa_sign_message( key, exercise_alg, |
| 1508 | payload, payload_length, |
| 1509 | signature, sizeof( signature ), |
| 1510 | &signature_length ); |
| 1511 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 1512 | PSA_ASSERT( status ); |
| 1513 | else |
| 1514 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1515 | |
| 1516 | memset( signature, 0, sizeof( signature ) ); |
| 1517 | status = psa_verify_message( key, exercise_alg, |
| 1518 | payload, payload_length, |
| 1519 | signature, sizeof( signature ) ); |
| 1520 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 1521 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1522 | else |
| 1523 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1524 | } |
| 1525 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1526 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1527 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1528 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1529 | } |
| 1530 | /* END_CASE */ |
| 1531 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1532 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1533 | void derive_key_policy( int policy_usage, |
| 1534 | int policy_alg, |
| 1535 | int key_type, |
| 1536 | data_t *key_data, |
| 1537 | int exercise_alg ) |
| 1538 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1539 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1540 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1541 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1542 | psa_status_t status; |
| 1543 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1544 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1545 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1546 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1547 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1548 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1549 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1550 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1551 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1552 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1553 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 1554 | |
| 1555 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 1556 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1557 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1558 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 1559 | &operation, |
| 1560 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 1561 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1562 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1563 | |
| 1564 | status = psa_key_derivation_input_key( &operation, |
| 1565 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1566 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1567 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1568 | if( policy_alg == exercise_alg && |
| 1569 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1570 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1571 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1572 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1573 | |
| 1574 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1575 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1576 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1577 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1578 | } |
| 1579 | /* END_CASE */ |
| 1580 | |
| 1581 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1582 | void agreement_key_policy( int policy_usage, |
| 1583 | int policy_alg, |
| 1584 | int key_type_arg, |
| 1585 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1586 | int exercise_alg, |
| 1587 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1588 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1589 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1590 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1591 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1592 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1593 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1594 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1595 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1596 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1597 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1598 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1599 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1600 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1601 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1602 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1603 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1604 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1605 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1606 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1607 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1608 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1609 | |
| 1610 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1611 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1612 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1613 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1614 | } |
| 1615 | /* END_CASE */ |
| 1616 | |
| 1617 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1618 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 1619 | int usage_arg, int alg_arg, int alg2_arg ) |
| 1620 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1621 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1622 | psa_key_type_t key_type = key_type_arg; |
| 1623 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1624 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1625 | psa_key_usage_t usage = usage_arg; |
| 1626 | psa_algorithm_t alg = alg_arg; |
| 1627 | psa_algorithm_t alg2 = alg2_arg; |
| 1628 | |
| 1629 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1630 | |
| 1631 | psa_set_key_usage_flags( &attributes, usage ); |
| 1632 | psa_set_key_algorithm( &attributes, alg ); |
| 1633 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 1634 | psa_set_key_type( &attributes, key_type ); |
| 1635 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1636 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1637 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1638 | /* Update the usage flags to obtain implicit usage flags */ |
| 1639 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1640 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1641 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1642 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 1643 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 1644 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1645 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1646 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1647 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1648 | goto exit; |
| 1649 | |
| 1650 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1651 | /* |
| 1652 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1653 | * thus reset them as required. |
| 1654 | */ |
| 1655 | psa_reset_key_attributes( &got_attributes ); |
| 1656 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1657 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1658 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1659 | } |
| 1660 | /* END_CASE */ |
| 1661 | |
| 1662 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1663 | void raw_agreement_key_policy( int policy_usage, |
| 1664 | int policy_alg, |
| 1665 | int key_type_arg, |
| 1666 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1667 | int exercise_alg, |
| 1668 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1669 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1670 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1671 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1672 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1673 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1674 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1675 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1676 | |
| 1677 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1678 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1679 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1680 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1681 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1682 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1683 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1684 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1685 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1686 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1687 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1688 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1689 | |
| 1690 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1691 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1692 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1693 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1694 | } |
| 1695 | /* END_CASE */ |
| 1696 | |
| 1697 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1698 | void copy_success( int source_usage_arg, |
| 1699 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1700 | unsigned int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1701 | int type_arg, data_t *material, |
| 1702 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1703 | int target_usage_arg, |
| 1704 | int target_alg_arg, int target_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1705 | unsigned int target_lifetime_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1706 | int expected_usage_arg, |
| 1707 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1708 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1709 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1710 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1711 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 1712 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1713 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1714 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 1715 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1716 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1717 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1718 | uint8_t *export_buffer = NULL; |
| 1719 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1720 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1721 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1722 | /* Prepare the source key. */ |
| 1723 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1724 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1725 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1726 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1727 | psa_set_key_lifetime( &source_attributes, source_lifetime); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1728 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1729 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1730 | &source_key ) ); |
| 1731 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1732 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1733 | /* Prepare the target attributes. */ |
| 1734 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1735 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1736 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1737 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1738 | psa_set_key_lifetime( &target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1739 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1740 | if( target_usage_arg != -1 ) |
| 1741 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1742 | if( target_alg_arg != -1 ) |
| 1743 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1744 | if( target_alg2_arg != -1 ) |
| 1745 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1746 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1747 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1748 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1749 | PSA_ASSERT( psa_copy_key( source_key, |
| 1750 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1751 | |
| 1752 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1753 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1754 | |
| 1755 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1756 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1757 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 1758 | psa_get_key_type( &target_attributes ) ); |
| 1759 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 1760 | psa_get_key_bits( &target_attributes ) ); |
| 1761 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 1762 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1763 | TEST_EQUAL( expected_alg2, |
| 1764 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1765 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 1766 | { |
| 1767 | size_t length; |
| 1768 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1769 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1770 | material->len, &length ) ); |
| 1771 | ASSERT_COMPARE( material->x, material->len, |
| 1772 | export_buffer, length ); |
| 1773 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1774 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1775 | if( !psa_key_lifetime_is_external( target_lifetime ) ) |
| 1776 | { |
| 1777 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
| 1778 | goto exit; |
| 1779 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
| 1780 | goto exit; |
| 1781 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1782 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1783 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1784 | |
| 1785 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1786 | /* |
| 1787 | * Source and target key attributes may have been returned by |
| 1788 | * psa_get_key_attributes() thus reset them as required. |
| 1789 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1790 | psa_reset_key_attributes( &source_attributes ); |
| 1791 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1792 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1793 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1794 | mbedtls_free( export_buffer ); |
| 1795 | } |
| 1796 | /* END_CASE */ |
| 1797 | |
| 1798 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1799 | void copy_fail( int source_usage_arg, |
| 1800 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1801 | int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1802 | int type_arg, data_t *material, |
| 1803 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1804 | int target_usage_arg, |
| 1805 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1806 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1807 | int expected_status_arg ) |
| 1808 | { |
| 1809 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1810 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1811 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1812 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1813 | 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] | 1814 | |
| 1815 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1816 | |
| 1817 | /* Prepare the source key. */ |
| 1818 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1819 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1820 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1821 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1822 | psa_set_key_lifetime( &source_attributes, source_lifetime_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1823 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1824 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1825 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1826 | |
| 1827 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1828 | psa_set_key_id( &target_attributes, key_id ); |
| 1829 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1830 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 1831 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 1832 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1833 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1834 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1835 | |
| 1836 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1837 | TEST_EQUAL( psa_copy_key( source_key, |
| 1838 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1839 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1840 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1841 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1842 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1843 | exit: |
| 1844 | psa_reset_key_attributes( &source_attributes ); |
| 1845 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1846 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1847 | } |
| 1848 | /* END_CASE */ |
| 1849 | |
| 1850 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1851 | void hash_operation_init( ) |
| 1852 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1853 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1854 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1855 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1856 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1857 | * to supress the Clang warning for the test. */ |
| 1858 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 1859 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 1860 | psa_hash_operation_t zero; |
| 1861 | |
| 1862 | memset( &zero, 0, sizeof( zero ) ); |
| 1863 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1864 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1865 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 1866 | PSA_ERROR_BAD_STATE ); |
| 1867 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 1868 | PSA_ERROR_BAD_STATE ); |
| 1869 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 1870 | PSA_ERROR_BAD_STATE ); |
| 1871 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1872 | /* A default hash operation should be abortable without error. */ |
| 1873 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 1874 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 1875 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1876 | } |
| 1877 | /* END_CASE */ |
| 1878 | |
| 1879 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1880 | void hash_setup( int alg_arg, |
| 1881 | int expected_status_arg ) |
| 1882 | { |
| 1883 | psa_algorithm_t alg = alg_arg; |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 1884 | uint8_t *output = NULL; |
| 1885 | size_t output_size = 0; |
| 1886 | size_t output_length = 0; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1887 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1888 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1889 | psa_status_t status; |
| 1890 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1891 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1892 | |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 1893 | /* Hash Setup, one-shot */ |
| 1894 | output_size = PSA_HASH_LENGTH(alg); |
| 1895 | ASSERT_ALLOC( output, output_size ); |
| 1896 | |
| 1897 | status = psa_hash_compute( alg, NULL, 0, |
| 1898 | output, output_size, &output_length ); |
| 1899 | TEST_EQUAL( status, expected_status ); |
| 1900 | |
| 1901 | /* Hash Setup, multi-part */ |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1902 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1903 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1904 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1905 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 1906 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1907 | |
| 1908 | /* If setup failed, reproduce the failure, so as to |
| 1909 | * test the resulting state of the operation object. */ |
| 1910 | if( status != PSA_SUCCESS ) |
| 1911 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 1912 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1913 | /* Now the operation object should be reusable. */ |
| 1914 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 1915 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 1916 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1917 | #endif |
| 1918 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1919 | exit: |
Neil Armstrong | edb2086 | 2022-02-07 15:47:44 +0100 | [diff] [blame] | 1920 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1921 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1922 | } |
| 1923 | /* END_CASE */ |
| 1924 | |
| 1925 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1926 | void hash_compute_fail( int alg_arg, data_t *input, |
| 1927 | int output_size_arg, int expected_status_arg ) |
| 1928 | { |
| 1929 | psa_algorithm_t alg = alg_arg; |
| 1930 | uint8_t *output = NULL; |
| 1931 | size_t output_size = output_size_arg; |
| 1932 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 1933 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1934 | psa_status_t expected_status = expected_status_arg; |
| 1935 | psa_status_t status; |
| 1936 | |
| 1937 | ASSERT_ALLOC( output, output_size ); |
| 1938 | |
| 1939 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1940 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 1941 | /* Hash Compute, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1942 | status = psa_hash_compute( alg, input->x, input->len, |
| 1943 | output, output_size, &output_length ); |
| 1944 | TEST_EQUAL( status, expected_status ); |
| 1945 | TEST_ASSERT( output_length <= output_size ); |
| 1946 | |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 1947 | /* Hash Compute, multi-part */ |
| 1948 | status = psa_hash_setup( &operation, alg ); |
| 1949 | if( status == PSA_SUCCESS ) |
| 1950 | { |
| 1951 | status = psa_hash_update( &operation, input->x, input->len ); |
| 1952 | if( status == PSA_SUCCESS ) |
| 1953 | { |
| 1954 | status = psa_hash_finish( &operation, output, output_size, |
| 1955 | &output_length ); |
| 1956 | if( status == PSA_SUCCESS ) |
| 1957 | TEST_ASSERT( output_length <= output_size ); |
| 1958 | else |
| 1959 | TEST_EQUAL( status, expected_status ); |
| 1960 | } |
| 1961 | else |
| 1962 | { |
| 1963 | TEST_EQUAL( status, expected_status ); |
| 1964 | } |
| 1965 | } |
| 1966 | else |
| 1967 | { |
| 1968 | TEST_EQUAL( status, expected_status ); |
| 1969 | } |
| 1970 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1971 | exit: |
Neil Armstrong | 161ec5c | 2022-02-07 11:18:45 +0100 | [diff] [blame] | 1972 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1973 | mbedtls_free( output ); |
| 1974 | PSA_DONE( ); |
| 1975 | } |
| 1976 | /* END_CASE */ |
| 1977 | |
| 1978 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1979 | void hash_compare_fail( int alg_arg, data_t *input, |
| 1980 | data_t *reference_hash, |
| 1981 | int expected_status_arg ) |
| 1982 | { |
| 1983 | psa_algorithm_t alg = alg_arg; |
| 1984 | psa_status_t expected_status = expected_status_arg; |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 1985 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1986 | psa_status_t status; |
| 1987 | |
| 1988 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1989 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 1990 | /* Hash Compare, one-shot */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1991 | status = psa_hash_compare( alg, input->x, input->len, |
| 1992 | reference_hash->x, reference_hash->len ); |
| 1993 | TEST_EQUAL( status, expected_status ); |
| 1994 | |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 1995 | /* Hash Compare, multi-part */ |
| 1996 | status = psa_hash_setup( &operation, alg ); |
| 1997 | if( status == PSA_SUCCESS ) |
| 1998 | { |
| 1999 | status = psa_hash_update( &operation, input->x, input->len ); |
| 2000 | if( status == PSA_SUCCESS ) |
| 2001 | { |
| 2002 | status = psa_hash_verify( &operation, reference_hash->x, |
| 2003 | reference_hash->len ); |
| 2004 | TEST_EQUAL( status, expected_status ); |
| 2005 | } |
| 2006 | else |
| 2007 | { |
| 2008 | TEST_EQUAL( status, expected_status ); |
| 2009 | } |
| 2010 | } |
| 2011 | else |
| 2012 | { |
| 2013 | TEST_EQUAL( status, expected_status ); |
| 2014 | } |
| 2015 | |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2016 | exit: |
Neil Armstrong | 55a1be1 | 2022-02-07 11:23:20 +0100 | [diff] [blame] | 2017 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 2018 | PSA_DONE( ); |
| 2019 | } |
| 2020 | /* END_CASE */ |
| 2021 | |
| 2022 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2023 | void hash_compute_compare( int alg_arg, data_t *input, |
| 2024 | data_t *expected_output ) |
| 2025 | { |
| 2026 | psa_algorithm_t alg = alg_arg; |
| 2027 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 2028 | size_t output_length = INVALID_EXPORT_LENGTH; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2029 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2030 | size_t i; |
| 2031 | |
| 2032 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2033 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2034 | /* Compute with tight buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2035 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2036 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2037 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2038 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2039 | ASSERT_COMPARE( output, output_length, |
| 2040 | expected_output->x, expected_output->len ); |
| 2041 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2042 | /* Compute with tight buffer, multi-part */ |
| 2043 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2044 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2045 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2046 | PSA_HASH_LENGTH( alg ), |
| 2047 | &output_length ) ); |
| 2048 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2049 | ASSERT_COMPARE( output, output_length, |
| 2050 | expected_output->x, expected_output->len ); |
| 2051 | |
| 2052 | /* Compute with larger buffer, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2053 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 2054 | output, sizeof( output ), |
| 2055 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2056 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2057 | ASSERT_COMPARE( output, output_length, |
| 2058 | expected_output->x, expected_output->len ); |
| 2059 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2060 | /* Compute with larger buffer, multi-part */ |
| 2061 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2062 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2063 | PSA_ASSERT( psa_hash_finish( &operation, output, |
| 2064 | sizeof( output ), &output_length ) ); |
| 2065 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
| 2066 | ASSERT_COMPARE( output, output_length, |
| 2067 | expected_output->x, expected_output->len ); |
| 2068 | |
| 2069 | /* Compare with correct hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2070 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 2071 | output, output_length ) ); |
| 2072 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2073 | /* Compare with correct hash, multi-part */ |
| 2074 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2075 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2076 | PSA_ASSERT( psa_hash_verify( &operation, output, |
| 2077 | output_length ) ); |
| 2078 | |
| 2079 | /* Compare with trailing garbage, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2080 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2081 | output, output_length + 1 ), |
| 2082 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2083 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2084 | /* Compare with trailing garbage, multi-part */ |
| 2085 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2086 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2087 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length + 1 ), |
| 2088 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2089 | |
| 2090 | /* Compare with truncated hash, one-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2091 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2092 | output, output_length - 1 ), |
| 2093 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2094 | |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2095 | /* Compare with truncated hash, multi-part */ |
| 2096 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2097 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2098 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length - 1 ), |
| 2099 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2100 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2101 | /* Compare with corrupted value */ |
| 2102 | for( i = 0; i < output_length; i++ ) |
| 2103 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2104 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2105 | output[i] ^= 1; |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2106 | |
| 2107 | /* One-shot */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2108 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 2109 | output, output_length ), |
| 2110 | PSA_ERROR_INVALID_SIGNATURE ); |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2111 | |
| 2112 | /* Multi-Part */ |
| 2113 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2114 | PSA_ASSERT( psa_hash_update( &operation, input->x, input->len ) ); |
| 2115 | TEST_EQUAL( psa_hash_verify( &operation, output, output_length ), |
| 2116 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2117 | |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2118 | output[i] ^= 1; |
| 2119 | } |
| 2120 | |
| 2121 | exit: |
Neil Armstrong | ca30a00 | 2022-02-07 11:40:23 +0100 | [diff] [blame] | 2122 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 2123 | PSA_DONE( ); |
| 2124 | } |
| 2125 | /* END_CASE */ |
| 2126 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2127 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2128 | void hash_bad_order( ) |
| 2129 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2130 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2131 | unsigned char input[] = ""; |
| 2132 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2133 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2134 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2135 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2136 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2137 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2138 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2139 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2140 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2141 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2142 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2143 | /* Call setup twice in a row. */ |
| 2144 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2145 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2146 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 2147 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2148 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2149 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2150 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2151 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2152 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2153 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2154 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2155 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2156 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2157 | /* Check that update calls abort on error. */ |
| 2158 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 2159 | operation.id = UINT_MAX; |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2160 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
| 2161 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
| 2162 | PSA_ERROR_BAD_STATE ); |
| 2163 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2164 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2165 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2166 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2167 | /* Call update after finish. */ |
| 2168 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2169 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2170 | hash, sizeof( hash ), &hash_len ) ); |
| 2171 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2172 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2173 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2174 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2175 | /* Call verify without calling setup beforehand. */ |
| 2176 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2177 | valid_hash, sizeof( valid_hash ) ), |
| 2178 | PSA_ERROR_BAD_STATE ); |
| 2179 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2180 | |
| 2181 | /* Call verify after finish. */ |
| 2182 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2183 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2184 | hash, sizeof( hash ), &hash_len ) ); |
| 2185 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2186 | valid_hash, sizeof( valid_hash ) ), |
| 2187 | PSA_ERROR_BAD_STATE ); |
| 2188 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2189 | |
| 2190 | /* Call verify twice in a row. */ |
| 2191 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2192 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2193 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2194 | valid_hash, sizeof( valid_hash ) ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2195 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2196 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2197 | valid_hash, sizeof( valid_hash ) ), |
| 2198 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2199 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2200 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2201 | |
| 2202 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2203 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2204 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2205 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2206 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2207 | |
| 2208 | /* Call finish twice in a row. */ |
| 2209 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2210 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2211 | hash, sizeof( hash ), &hash_len ) ); |
| 2212 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2213 | hash, sizeof( hash ), &hash_len ), |
| 2214 | PSA_ERROR_BAD_STATE ); |
| 2215 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2216 | |
| 2217 | /* Call finish after calling verify. */ |
| 2218 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2219 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2220 | valid_hash, sizeof( valid_hash ) ) ); |
| 2221 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2222 | hash, sizeof( hash ), &hash_len ), |
| 2223 | PSA_ERROR_BAD_STATE ); |
| 2224 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2225 | |
| 2226 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2227 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2228 | } |
| 2229 | /* END_CASE */ |
| 2230 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2231 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2232 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2233 | { |
| 2234 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2235 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2236 | * appended to it */ |
| 2237 | unsigned char hash[] = { |
| 2238 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2239 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2240 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2241 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2242 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2243 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2244 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2245 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2246 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2247 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2248 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2249 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2250 | PSA_ERROR_INVALID_SIGNATURE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2251 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2252 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2253 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2254 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2255 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2256 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2257 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2258 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2259 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2260 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2261 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2262 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2263 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2264 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2265 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2266 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2267 | } |
| 2268 | /* END_CASE */ |
| 2269 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2270 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2271 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2272 | { |
| 2273 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2274 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2275 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2276 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2277 | size_t hash_len; |
| 2278 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2279 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2280 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2281 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2282 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2283 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2284 | hash, expected_size - 1, &hash_len ), |
| 2285 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2286 | |
| 2287 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2288 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2289 | } |
| 2290 | /* END_CASE */ |
| 2291 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2292 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2293 | void hash_clone_source_state( ) |
| 2294 | { |
| 2295 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2296 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2297 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2298 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2299 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2300 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2301 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2302 | size_t hash_len; |
| 2303 | |
| 2304 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2305 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 2306 | |
| 2307 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2308 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2309 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2310 | hash, sizeof( hash ), &hash_len ) ); |
| 2311 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2312 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2313 | |
| 2314 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2315 | PSA_ERROR_BAD_STATE ); |
| 2316 | |
| 2317 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2318 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2319 | hash, sizeof( hash ), &hash_len ) ); |
| 2320 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2321 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2322 | hash, sizeof( hash ), &hash_len ) ); |
| 2323 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2324 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2325 | hash, sizeof( hash ), &hash_len ) ); |
| 2326 | |
| 2327 | exit: |
| 2328 | psa_hash_abort( &op_source ); |
| 2329 | psa_hash_abort( &op_init ); |
| 2330 | psa_hash_abort( &op_setup ); |
| 2331 | psa_hash_abort( &op_finished ); |
| 2332 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2333 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2334 | } |
| 2335 | /* END_CASE */ |
| 2336 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2337 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2338 | void hash_clone_target_state( ) |
| 2339 | { |
| 2340 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2341 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2342 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2343 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2344 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2345 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2346 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2347 | size_t hash_len; |
| 2348 | |
| 2349 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2350 | |
| 2351 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2352 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2353 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2354 | hash, sizeof( hash ), &hash_len ) ); |
| 2355 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2356 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2357 | |
| 2358 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2359 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2360 | hash, sizeof( hash ), &hash_len ) ); |
| 2361 | |
| 2362 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2363 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2364 | PSA_ERROR_BAD_STATE ); |
| 2365 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2366 | PSA_ERROR_BAD_STATE ); |
| 2367 | |
| 2368 | exit: |
| 2369 | psa_hash_abort( &op_target ); |
| 2370 | psa_hash_abort( &op_init ); |
| 2371 | psa_hash_abort( &op_setup ); |
| 2372 | psa_hash_abort( &op_finished ); |
| 2373 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2374 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2375 | } |
| 2376 | /* END_CASE */ |
| 2377 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2378 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2379 | void mac_operation_init( ) |
| 2380 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2381 | const uint8_t input[1] = { 0 }; |
| 2382 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2383 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2384 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2385 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2386 | * to supress the Clang warning for the test. */ |
| 2387 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2388 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2389 | psa_mac_operation_t zero; |
| 2390 | |
| 2391 | memset( &zero, 0, sizeof( zero ) ); |
| 2392 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2393 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2394 | TEST_EQUAL( psa_mac_update( &func, |
| 2395 | input, sizeof( input ) ), |
| 2396 | PSA_ERROR_BAD_STATE ); |
| 2397 | TEST_EQUAL( psa_mac_update( &init, |
| 2398 | input, sizeof( input ) ), |
| 2399 | PSA_ERROR_BAD_STATE ); |
| 2400 | TEST_EQUAL( psa_mac_update( &zero, |
| 2401 | input, sizeof( input ) ), |
| 2402 | PSA_ERROR_BAD_STATE ); |
| 2403 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2404 | /* A default MAC operation should be abortable without error. */ |
| 2405 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2406 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2407 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2408 | } |
| 2409 | /* END_CASE */ |
| 2410 | |
| 2411 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2412 | void mac_setup( int key_type_arg, |
| 2413 | data_t *key, |
| 2414 | int alg_arg, |
| 2415 | int expected_status_arg ) |
| 2416 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2417 | psa_key_type_t key_type = key_type_arg; |
| 2418 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2419 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2420 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2421 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 2422 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2423 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2424 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2425 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2426 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2427 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2428 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 2429 | &operation, &status ) ) |
| 2430 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2431 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2432 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2433 | /* The operation object should be reusable. */ |
| 2434 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2435 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2436 | smoke_test_key_data, |
| 2437 | sizeof( smoke_test_key_data ), |
| 2438 | KNOWN_SUPPORTED_MAC_ALG, |
| 2439 | &operation, &status ) ) |
| 2440 | goto exit; |
| 2441 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2442 | #endif |
| 2443 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2444 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2445 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2446 | } |
| 2447 | /* END_CASE */ |
| 2448 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2449 | /* 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] | 2450 | void mac_bad_order( ) |
| 2451 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2452 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2453 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2454 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2455 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2456 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2457 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2458 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2459 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2460 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2461 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2462 | size_t sign_mac_length = 0; |
| 2463 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2464 | const uint8_t verify_mac[] = { |
| 2465 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2466 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2467 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2468 | |
| 2469 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2470 | 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] | 2471 | psa_set_key_algorithm( &attributes, alg ); |
| 2472 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2473 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2474 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2475 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2476 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2477 | /* Call update without calling setup beforehand. */ |
| 2478 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2479 | PSA_ERROR_BAD_STATE ); |
| 2480 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2481 | |
| 2482 | /* Call sign finish without calling setup beforehand. */ |
| 2483 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2484 | &sign_mac_length), |
| 2485 | PSA_ERROR_BAD_STATE ); |
| 2486 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2487 | |
| 2488 | /* Call verify finish without calling setup beforehand. */ |
| 2489 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2490 | verify_mac, sizeof( verify_mac ) ), |
| 2491 | PSA_ERROR_BAD_STATE ); |
| 2492 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2493 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2494 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2495 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2496 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2497 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2498 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2499 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2500 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2501 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2502 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2503 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2504 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2505 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2506 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2507 | sign_mac, sizeof( sign_mac ), |
| 2508 | &sign_mac_length ) ); |
| 2509 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2510 | PSA_ERROR_BAD_STATE ); |
| 2511 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2512 | |
| 2513 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2514 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2515 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2516 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2517 | verify_mac, sizeof( verify_mac ) ) ); |
| 2518 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2519 | PSA_ERROR_BAD_STATE ); |
| 2520 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2521 | |
| 2522 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2523 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2524 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2525 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2526 | sign_mac, sizeof( sign_mac ), |
| 2527 | &sign_mac_length ) ); |
| 2528 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2529 | sign_mac, sizeof( sign_mac ), |
| 2530 | &sign_mac_length ), |
| 2531 | PSA_ERROR_BAD_STATE ); |
| 2532 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2533 | |
| 2534 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2535 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2536 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2537 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2538 | verify_mac, sizeof( verify_mac ) ) ); |
| 2539 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2540 | verify_mac, sizeof( verify_mac ) ), |
| 2541 | PSA_ERROR_BAD_STATE ); |
| 2542 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2543 | |
| 2544 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2545 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2546 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2547 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2548 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2549 | verify_mac, sizeof( verify_mac ) ), |
| 2550 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2551 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2552 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2553 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2554 | |
| 2555 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2556 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2557 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2558 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2559 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2560 | sign_mac, sizeof( sign_mac ), |
| 2561 | &sign_mac_length ), |
| 2562 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2563 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2564 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2565 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2566 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2567 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2568 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2569 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2570 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2571 | } |
| 2572 | /* END_CASE */ |
| 2573 | |
| 2574 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2575 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2576 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2577 | int alg_arg, |
| 2578 | data_t *input, |
| 2579 | data_t *expected_mac ) |
| 2580 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2581 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2582 | psa_key_type_t key_type = key_type_arg; |
| 2583 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2584 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2585 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2586 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2587 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2588 | 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] | 2589 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2590 | const size_t output_sizes_to_test[] = { |
| 2591 | 0, |
| 2592 | 1, |
| 2593 | expected_mac->len - 1, |
| 2594 | expected_mac->len, |
| 2595 | expected_mac->len + 1, |
| 2596 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2597 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2598 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2599 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 2600 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2601 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2602 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2603 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2604 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2605 | psa_set_key_algorithm( &attributes, alg ); |
| 2606 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2607 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2608 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2609 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2610 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2611 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 2612 | { |
| 2613 | const size_t output_size = output_sizes_to_test[i]; |
| 2614 | psa_status_t expected_status = |
| 2615 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 2616 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2617 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2618 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2619 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2620 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2621 | /* Calculate the MAC, one-shot case. */ |
| 2622 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 2623 | input->x, input->len, |
| 2624 | actual_mac, output_size, &mac_length ), |
| 2625 | expected_status ); |
| 2626 | if( expected_status == PSA_SUCCESS ) |
| 2627 | { |
| 2628 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2629 | actual_mac, mac_length ); |
| 2630 | } |
| 2631 | |
| 2632 | if( output_size > 0 ) |
| 2633 | memset( actual_mac, 0, output_size ); |
| 2634 | |
| 2635 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2636 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2637 | PSA_ASSERT( psa_mac_update( &operation, |
| 2638 | input->x, input->len ) ); |
| 2639 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2640 | actual_mac, output_size, |
| 2641 | &mac_length ), |
| 2642 | expected_status ); |
| 2643 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2644 | |
| 2645 | if( expected_status == PSA_SUCCESS ) |
| 2646 | { |
| 2647 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2648 | actual_mac, mac_length ); |
| 2649 | } |
| 2650 | mbedtls_free( actual_mac ); |
| 2651 | actual_mac = NULL; |
| 2652 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2653 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2654 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2655 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2656 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2657 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2658 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2659 | } |
| 2660 | /* END_CASE */ |
| 2661 | |
| 2662 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2663 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2664 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2665 | int alg_arg, |
| 2666 | data_t *input, |
| 2667 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2668 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2669 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2670 | psa_key_type_t key_type = key_type_arg; |
| 2671 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2672 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2673 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2674 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2675 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2676 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 2677 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2678 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2679 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2680 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2681 | psa_set_key_algorithm( &attributes, alg ); |
| 2682 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2683 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2684 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2685 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2686 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2687 | /* Verify correct MAC, one-shot case. */ |
| 2688 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 2689 | expected_mac->x, expected_mac->len ) ); |
| 2690 | |
| 2691 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2692 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2693 | PSA_ASSERT( psa_mac_update( &operation, |
| 2694 | input->x, input->len ) ); |
| 2695 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2696 | expected_mac->x, |
| 2697 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2698 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2699 | /* Test a MAC that's too short, one-shot case. */ |
| 2700 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2701 | input->x, input->len, |
| 2702 | expected_mac->x, |
| 2703 | expected_mac->len - 1 ), |
| 2704 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2705 | |
| 2706 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2707 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2708 | PSA_ASSERT( psa_mac_update( &operation, |
| 2709 | input->x, input->len ) ); |
| 2710 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2711 | expected_mac->x, |
| 2712 | expected_mac->len - 1 ), |
| 2713 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2714 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2715 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2716 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 2717 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2718 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2719 | input->x, input->len, |
| 2720 | perturbed_mac, expected_mac->len + 1 ), |
| 2721 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2722 | |
| 2723 | /* Test a MAC that's too long, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2724 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2725 | PSA_ASSERT( psa_mac_update( &operation, |
| 2726 | input->x, input->len ) ); |
| 2727 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2728 | perturbed_mac, |
| 2729 | expected_mac->len + 1 ), |
| 2730 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2731 | |
| 2732 | /* Test changing one byte. */ |
| 2733 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 2734 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2735 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2736 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2737 | |
| 2738 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2739 | input->x, input->len, |
| 2740 | perturbed_mac, expected_mac->len ), |
| 2741 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2742 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2743 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2744 | PSA_ASSERT( psa_mac_update( &operation, |
| 2745 | input->x, input->len ) ); |
| 2746 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2747 | perturbed_mac, |
| 2748 | expected_mac->len ), |
| 2749 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2750 | perturbed_mac[i] ^= 1; |
| 2751 | } |
| 2752 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2753 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2754 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2755 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2756 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2757 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2758 | } |
| 2759 | /* END_CASE */ |
| 2760 | |
| 2761 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2762 | void cipher_operation_init( ) |
| 2763 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2764 | const uint8_t input[1] = { 0 }; |
| 2765 | unsigned char output[1] = { 0 }; |
| 2766 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2767 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2768 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2769 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2770 | * to supress the Clang warning for the test. */ |
| 2771 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 2772 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2773 | psa_cipher_operation_t zero; |
| 2774 | |
| 2775 | memset( &zero, 0, sizeof( zero ) ); |
| 2776 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2777 | /* A freshly-initialized cipher operation should not be usable. */ |
| 2778 | TEST_EQUAL( psa_cipher_update( &func, |
| 2779 | input, sizeof( input ), |
| 2780 | output, sizeof( output ), |
| 2781 | &output_length ), |
| 2782 | PSA_ERROR_BAD_STATE ); |
| 2783 | TEST_EQUAL( psa_cipher_update( &init, |
| 2784 | input, sizeof( input ), |
| 2785 | output, sizeof( output ), |
| 2786 | &output_length ), |
| 2787 | PSA_ERROR_BAD_STATE ); |
| 2788 | TEST_EQUAL( psa_cipher_update( &zero, |
| 2789 | input, sizeof( input ), |
| 2790 | output, sizeof( output ), |
| 2791 | &output_length ), |
| 2792 | PSA_ERROR_BAD_STATE ); |
| 2793 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2794 | /* A default cipher operation should be abortable without error. */ |
| 2795 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 2796 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 2797 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2798 | } |
| 2799 | /* END_CASE */ |
| 2800 | |
| 2801 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2802 | void cipher_setup( int key_type_arg, |
| 2803 | data_t *key, |
| 2804 | int alg_arg, |
| 2805 | int expected_status_arg ) |
| 2806 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2807 | psa_key_type_t key_type = key_type_arg; |
| 2808 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2809 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2810 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2811 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 2812 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2813 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2814 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2815 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2816 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2817 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2818 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 2819 | &operation, &status ) ) |
| 2820 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2821 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2822 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2823 | /* The operation object should be reusable. */ |
| 2824 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 2825 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2826 | smoke_test_key_data, |
| 2827 | sizeof( smoke_test_key_data ), |
| 2828 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2829 | &operation, &status ) ) |
| 2830 | goto exit; |
| 2831 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2832 | #endif |
| 2833 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2834 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2835 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2836 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2837 | } |
| 2838 | /* END_CASE */ |
| 2839 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2840 | /* 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] | 2841 | void cipher_bad_order( ) |
| 2842 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2843 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2844 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2845 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2846 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2847 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2848 | 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] | 2849 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2850 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2851 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 2852 | const uint8_t text[] = { |
| 2853 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 2854 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2855 | 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] | 2856 | size_t length = 0; |
| 2857 | |
| 2858 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2859 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 2860 | psa_set_key_algorithm( &attributes, alg ); |
| 2861 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2862 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2863 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2864 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2865 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2866 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2867 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2868 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2869 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2870 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2871 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2872 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2873 | |
| 2874 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2875 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2876 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2877 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2878 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2879 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2880 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2881 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2882 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2883 | /* Generate an IV without calling setup beforehand. */ |
| 2884 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2885 | buffer, sizeof( buffer ), |
| 2886 | &length ), |
| 2887 | PSA_ERROR_BAD_STATE ); |
| 2888 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2889 | |
| 2890 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2891 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2892 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2893 | buffer, sizeof( buffer ), |
| 2894 | &length ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2895 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2896 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2897 | buffer, sizeof( buffer ), |
| 2898 | &length ), |
| 2899 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2900 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2901 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2902 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2903 | |
| 2904 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2905 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2906 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2907 | iv, sizeof( iv ) ) ); |
| 2908 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2909 | buffer, sizeof( buffer ), |
| 2910 | &length ), |
| 2911 | PSA_ERROR_BAD_STATE ); |
| 2912 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2913 | |
| 2914 | /* Set an IV without calling setup beforehand. */ |
| 2915 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2916 | iv, sizeof( iv ) ), |
| 2917 | PSA_ERROR_BAD_STATE ); |
| 2918 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2919 | |
| 2920 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2921 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2922 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2923 | iv, sizeof( iv ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2924 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2925 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2926 | iv, sizeof( iv ) ), |
| 2927 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2928 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2929 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2930 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2931 | |
| 2932 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2933 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2934 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2935 | buffer, sizeof( buffer ), |
| 2936 | &length ) ); |
| 2937 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2938 | iv, sizeof( iv ) ), |
| 2939 | PSA_ERROR_BAD_STATE ); |
| 2940 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2941 | |
| 2942 | /* Call update without calling setup beforehand. */ |
| 2943 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2944 | text, sizeof( text ), |
| 2945 | buffer, sizeof( buffer ), |
| 2946 | &length ), |
| 2947 | PSA_ERROR_BAD_STATE ); |
| 2948 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2949 | |
| 2950 | /* Call update without an IV where an IV is required. */ |
Dave Rodgman | 095dadc | 2021-06-23 12:48:52 +0100 | [diff] [blame] | 2951 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2952 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2953 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2954 | text, sizeof( text ), |
| 2955 | buffer, sizeof( buffer ), |
| 2956 | &length ), |
| 2957 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2958 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2959 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2960 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2961 | |
| 2962 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2963 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2964 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2965 | iv, sizeof( iv ) ) ); |
| 2966 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2967 | buffer, sizeof( buffer ), &length ) ); |
| 2968 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2969 | text, sizeof( text ), |
| 2970 | buffer, sizeof( buffer ), |
| 2971 | &length ), |
| 2972 | PSA_ERROR_BAD_STATE ); |
| 2973 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2974 | |
| 2975 | /* Call finish without calling setup beforehand. */ |
| 2976 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2977 | buffer, sizeof( buffer ), &length ), |
| 2978 | PSA_ERROR_BAD_STATE ); |
| 2979 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2980 | |
| 2981 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2982 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2983 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 2984 | * for cipher modes with padding. */ |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2985 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2986 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2987 | buffer, sizeof( buffer ), &length ), |
| 2988 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2989 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2990 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2991 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2992 | |
| 2993 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2994 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2995 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2996 | iv, sizeof( iv ) ) ); |
| 2997 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2998 | buffer, sizeof( buffer ), &length ) ); |
| 2999 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 3000 | buffer, sizeof( buffer ), &length ), |
| 3001 | PSA_ERROR_BAD_STATE ); |
| 3002 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3003 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3004 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 3005 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 3006 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3007 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3008 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 3009 | } |
| 3010 | /* END_CASE */ |
| 3011 | |
| 3012 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3013 | void cipher_encrypt_fail( int alg_arg, |
| 3014 | int key_type_arg, |
| 3015 | data_t *key_data, |
| 3016 | data_t *input, |
| 3017 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3018 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3019 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3020 | psa_status_t status; |
| 3021 | psa_key_type_t key_type = key_type_arg; |
| 3022 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 3023 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3024 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3025 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3026 | size_t output_length = 0; |
| 3027 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3028 | |
| 3029 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 3030 | { |
| 3031 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3032 | |
| 3033 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3034 | psa_set_key_algorithm( &attributes, alg ); |
| 3035 | psa_set_key_type( &attributes, key_type ); |
| 3036 | |
| 3037 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 3038 | input->len ); |
| 3039 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3040 | |
| 3041 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3042 | &key ) ); |
| 3043 | } |
| 3044 | |
| 3045 | status = psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 3046 | output_buffer_size, &output_length ); |
| 3047 | |
| 3048 | TEST_EQUAL( status, expected_status ); |
| 3049 | |
| 3050 | exit: |
| 3051 | mbedtls_free( output ); |
| 3052 | psa_destroy_key( key ); |
| 3053 | PSA_DONE( ); |
| 3054 | } |
| 3055 | /* END_CASE */ |
| 3056 | |
| 3057 | /* BEGIN_CASE */ |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 3058 | void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data, |
| 3059 | data_t *input, int iv_length, |
| 3060 | int expected_result ) |
| 3061 | { |
| 3062 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3063 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3064 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3065 | size_t output_buffer_size = 0; |
| 3066 | unsigned char *output = NULL; |
| 3067 | |
| 3068 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3069 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3070 | |
| 3071 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3072 | |
| 3073 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3074 | psa_set_key_algorithm( &attributes, alg ); |
| 3075 | psa_set_key_type( &attributes, key_type ); |
| 3076 | |
| 3077 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3078 | &key ) ); |
| 3079 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3080 | TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output, |
| 3081 | iv_length ) ); |
| 3082 | |
| 3083 | exit: |
| 3084 | psa_cipher_abort( &operation ); |
| 3085 | mbedtls_free( output ); |
| 3086 | psa_destroy_key( key ); |
| 3087 | PSA_DONE( ); |
| 3088 | } |
| 3089 | /* END_CASE */ |
| 3090 | |
| 3091 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3092 | void cipher_encrypt_alg_without_iv( int alg_arg, |
| 3093 | int key_type_arg, |
| 3094 | data_t *key_data, |
| 3095 | data_t *input, |
| 3096 | data_t *expected_output ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3097 | { |
| 3098 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3099 | psa_key_type_t key_type = key_type_arg; |
| 3100 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3101 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3102 | uint8_t iv[1] = { 0x5a }; |
| 3103 | size_t iv_length; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3104 | unsigned char *output = NULL; |
| 3105 | size_t output_buffer_size = 0; |
| 3106 | size_t output_length = 0; |
| 3107 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3108 | |
| 3109 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3110 | |
| 3111 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3112 | psa_set_key_algorithm( &attributes, alg ); |
| 3113 | psa_set_key_type( &attributes, key_type ); |
| 3114 | |
| 3115 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3116 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3117 | |
| 3118 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3119 | &key ) ); |
| 3120 | |
Ronald Cron | 6c9bb0f | 2021-07-15 09:38:11 +0200 | [diff] [blame] | 3121 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3122 | TEST_EQUAL( psa_cipher_set_iv( &operation, iv, sizeof( iv ) ), |
| 3123 | PSA_ERROR_BAD_STATE ); |
| 3124 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3125 | TEST_EQUAL( psa_cipher_generate_iv( &operation, iv, sizeof( iv ), |
| 3126 | &iv_length ), |
| 3127 | PSA_ERROR_BAD_STATE ); |
| 3128 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3129 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 3130 | output_buffer_size, &output_length ) ); |
| 3131 | TEST_ASSERT( output_length <= |
| 3132 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 3133 | TEST_ASSERT( output_length <= |
| 3134 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
| 3135 | |
| 3136 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3137 | output, output_length ); |
| 3138 | exit: |
| 3139 | mbedtls_free( output ); |
| 3140 | psa_destroy_key( key ); |
| 3141 | PSA_DONE( ); |
| 3142 | } |
| 3143 | /* END_CASE */ |
| 3144 | |
| 3145 | /* BEGIN_CASE */ |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 3146 | void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data ) |
| 3147 | { |
| 3148 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3149 | psa_algorithm_t alg = alg_arg; |
| 3150 | psa_key_type_t key_type = key_type_arg; |
| 3151 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3152 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 3153 | psa_status_t status; |
| 3154 | |
| 3155 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3156 | |
| 3157 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3158 | psa_set_key_algorithm( &attributes, alg ); |
| 3159 | psa_set_key_type( &attributes, key_type ); |
| 3160 | |
| 3161 | /* Usage of either of these two size macros would cause divide by zero |
| 3162 | * with incorrect key types previously. Input length should be irrelevant |
| 3163 | * here. */ |
| 3164 | TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ), |
| 3165 | 0 ); |
| 3166 | TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 ); |
| 3167 | |
| 3168 | |
| 3169 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3170 | &key ) ); |
| 3171 | |
| 3172 | /* Should fail due to invalid alg type (to support invalid key type). |
| 3173 | * Encrypt or decrypt will end up in the same place. */ |
| 3174 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 3175 | |
| 3176 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 3177 | |
| 3178 | exit: |
| 3179 | psa_cipher_abort( &operation ); |
| 3180 | psa_destroy_key( key ); |
| 3181 | PSA_DONE( ); |
| 3182 | } |
| 3183 | /* END_CASE */ |
| 3184 | |
| 3185 | /* BEGIN_CASE */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3186 | void cipher_encrypt_validation( int alg_arg, |
| 3187 | int key_type_arg, |
| 3188 | data_t *key_data, |
| 3189 | data_t *input ) |
| 3190 | { |
| 3191 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3192 | psa_key_type_t key_type = key_type_arg; |
| 3193 | psa_algorithm_t alg = alg_arg; |
| 3194 | size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); |
| 3195 | unsigned char *output1 = NULL; |
| 3196 | size_t output1_buffer_size = 0; |
| 3197 | size_t output1_length = 0; |
| 3198 | unsigned char *output2 = NULL; |
| 3199 | size_t output2_buffer_size = 0; |
| 3200 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3201 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3202 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3203 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3204 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3205 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3206 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3207 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3208 | psa_set_key_algorithm( &attributes, alg ); |
| 3209 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3210 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3211 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3212 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 3213 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 3214 | ASSERT_ALLOC( output1, output1_buffer_size ); |
| 3215 | ASSERT_ALLOC( output2, output2_buffer_size ); |
| 3216 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3217 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3218 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3219 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 3220 | /* The one-shot cipher encryption uses generated iv so validating |
| 3221 | the output is not possible. Validating with multipart encryption. */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3222 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, |
| 3223 | output1_buffer_size, &output1_length ) ); |
| 3224 | TEST_ASSERT( output1_length <= |
| 3225 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 3226 | TEST_ASSERT( output1_length <= |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3227 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3228 | |
| 3229 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3230 | PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3231 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3232 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3233 | input->x, input->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3234 | output2, output2_buffer_size, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3235 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3236 | TEST_ASSERT( function_output_length <= |
| 3237 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 3238 | TEST_ASSERT( function_output_length <= |
| 3239 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3240 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3241 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3242 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3243 | output2 + output2_length, |
| 3244 | output2_buffer_size - output2_length, |
| 3245 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3246 | TEST_ASSERT( function_output_length <= |
| 3247 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3248 | TEST_ASSERT( function_output_length <= |
| 3249 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3250 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3251 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3252 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3253 | ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, |
| 3254 | output2, output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3255 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3256 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3257 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3258 | mbedtls_free( output1 ); |
| 3259 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3260 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3261 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3262 | } |
| 3263 | /* END_CASE */ |
| 3264 | |
| 3265 | /* BEGIN_CASE */ |
| 3266 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3267 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3268 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3269 | int first_part_size_arg, |
| 3270 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3271 | data_t *expected_output, |
| 3272 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3273 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3274 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3275 | psa_key_type_t key_type = key_type_arg; |
| 3276 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3277 | psa_status_t status; |
| 3278 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3279 | size_t first_part_size = first_part_size_arg; |
| 3280 | size_t output1_length = output1_length_arg; |
| 3281 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3282 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3283 | size_t output_buffer_size = 0; |
| 3284 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3285 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3286 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3287 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3288 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3289 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3290 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3291 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3292 | psa_set_key_algorithm( &attributes, alg ); |
| 3293 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3294 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3295 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3296 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3297 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3298 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3299 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3300 | if( iv->len > 0 ) |
| 3301 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3302 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3303 | } |
| 3304 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3305 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 3306 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3307 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3308 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3309 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3310 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 3311 | output, output_buffer_size, |
| 3312 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3313 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3314 | TEST_ASSERT( function_output_length <= |
| 3315 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3316 | TEST_ASSERT( function_output_length <= |
| 3317 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3318 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3319 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3320 | if( first_part_size < input->len ) |
| 3321 | { |
| 3322 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3323 | input->x + first_part_size, |
| 3324 | input->len - first_part_size, |
| 3325 | ( output_buffer_size == 0 ? NULL : |
| 3326 | output + total_output_length ), |
| 3327 | output_buffer_size - total_output_length, |
| 3328 | &function_output_length ) ); |
| 3329 | TEST_ASSERT( function_output_length == output2_length ); |
| 3330 | TEST_ASSERT( function_output_length <= |
| 3331 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3332 | alg, |
| 3333 | input->len - first_part_size ) ); |
| 3334 | TEST_ASSERT( function_output_length <= |
| 3335 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
| 3336 | total_output_length += function_output_length; |
| 3337 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3338 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3339 | status = psa_cipher_finish( &operation, |
| 3340 | ( output_buffer_size == 0 ? NULL : |
| 3341 | output + total_output_length ), |
| 3342 | output_buffer_size - total_output_length, |
| 3343 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3344 | TEST_ASSERT( function_output_length <= |
| 3345 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3346 | TEST_ASSERT( function_output_length <= |
| 3347 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3348 | total_output_length += function_output_length; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3349 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3350 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3351 | if( expected_status == PSA_SUCCESS ) |
| 3352 | { |
| 3353 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3354 | |
| 3355 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3356 | output, total_output_length ); |
| 3357 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3358 | |
| 3359 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3360 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3361 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3362 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3363 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3364 | } |
| 3365 | /* END_CASE */ |
| 3366 | |
| 3367 | /* BEGIN_CASE */ |
| 3368 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3369 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3370 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3371 | int first_part_size_arg, |
| 3372 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3373 | data_t *expected_output, |
| 3374 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3375 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3376 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3377 | psa_key_type_t key_type = key_type_arg; |
| 3378 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3379 | psa_status_t status; |
| 3380 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3381 | size_t first_part_size = first_part_size_arg; |
| 3382 | size_t output1_length = output1_length_arg; |
| 3383 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3384 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3385 | size_t output_buffer_size = 0; |
| 3386 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3387 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3388 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3389 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3390 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3391 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3392 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3393 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3394 | psa_set_key_algorithm( &attributes, alg ); |
| 3395 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3396 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3397 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3398 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3399 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3400 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3401 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3402 | if( iv->len > 0 ) |
| 3403 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3404 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3405 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3406 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3407 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 3408 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3409 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3410 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3411 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3412 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3413 | input->x, first_part_size, |
| 3414 | output, output_buffer_size, |
| 3415 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3416 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3417 | TEST_ASSERT( function_output_length <= |
| 3418 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3419 | TEST_ASSERT( function_output_length <= |
| 3420 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3421 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3422 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3423 | if( first_part_size < input->len ) |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3424 | { |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3425 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3426 | input->x + first_part_size, |
| 3427 | input->len - first_part_size, |
| 3428 | ( output_buffer_size == 0 ? NULL : |
| 3429 | output + total_output_length ), |
| 3430 | output_buffer_size - total_output_length, |
| 3431 | &function_output_length ) ); |
| 3432 | TEST_ASSERT( function_output_length == output2_length ); |
| 3433 | TEST_ASSERT( function_output_length <= |
| 3434 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3435 | alg, |
| 3436 | input->len - first_part_size ) ); |
| 3437 | TEST_ASSERT( function_output_length <= |
| 3438 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
| 3439 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3440 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3441 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3442 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 3443 | ( output_buffer_size == 0 ? NULL : |
| 3444 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3445 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3446 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3447 | TEST_ASSERT( function_output_length <= |
| 3448 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3449 | TEST_ASSERT( function_output_length <= |
| 3450 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3451 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3452 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3453 | |
| 3454 | if( expected_status == PSA_SUCCESS ) |
| 3455 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3456 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3457 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3458 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3459 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3460 | } |
| 3461 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3462 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3463 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3464 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3465 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3466 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3467 | } |
| 3468 | /* END_CASE */ |
| 3469 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3470 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3471 | void cipher_decrypt_fail( int alg_arg, |
| 3472 | int key_type_arg, |
| 3473 | data_t *key_data, |
| 3474 | data_t *iv, |
| 3475 | data_t *input_arg, |
| 3476 | int expected_status_arg ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3477 | { |
| 3478 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3479 | psa_status_t status; |
| 3480 | psa_key_type_t key_type = key_type_arg; |
| 3481 | psa_algorithm_t alg = alg_arg; |
| 3482 | psa_status_t expected_status = expected_status_arg; |
| 3483 | unsigned char *input = NULL; |
| 3484 | size_t input_buffer_size = 0; |
| 3485 | unsigned char *output = NULL; |
| 3486 | size_t output_buffer_size = 0; |
| 3487 | size_t output_length = 0; |
| 3488 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3489 | |
| 3490 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 3491 | { |
| 3492 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3493 | |
| 3494 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3495 | psa_set_key_algorithm( &attributes, alg ); |
| 3496 | psa_set_key_type( &attributes, key_type ); |
| 3497 | |
| 3498 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3499 | &key ) ); |
| 3500 | } |
| 3501 | |
| 3502 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 3503 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 3504 | if ( input_buffer_size > 0 ) |
| 3505 | { |
| 3506 | ASSERT_ALLOC( input, input_buffer_size ); |
| 3507 | memcpy( input, iv->x, iv->len ); |
| 3508 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 3509 | } |
| 3510 | |
| 3511 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 3512 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3513 | |
| 3514 | status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 3515 | output_buffer_size, &output_length ); |
| 3516 | TEST_EQUAL( status, expected_status ); |
| 3517 | |
| 3518 | exit: |
| 3519 | mbedtls_free( input ); |
| 3520 | mbedtls_free( output ); |
| 3521 | psa_destroy_key( key ); |
| 3522 | PSA_DONE( ); |
| 3523 | } |
| 3524 | /* END_CASE */ |
| 3525 | |
| 3526 | /* BEGIN_CASE */ |
| 3527 | void cipher_decrypt( int alg_arg, |
| 3528 | int key_type_arg, |
| 3529 | data_t *key_data, |
| 3530 | data_t *iv, |
| 3531 | data_t *input_arg, |
| 3532 | data_t *expected_output ) |
| 3533 | { |
| 3534 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3535 | psa_key_type_t key_type = key_type_arg; |
| 3536 | psa_algorithm_t alg = alg_arg; |
| 3537 | unsigned char *input = NULL; |
| 3538 | size_t input_buffer_size = 0; |
| 3539 | unsigned char *output = NULL; |
| 3540 | size_t output_buffer_size = 0; |
| 3541 | size_t output_length = 0; |
| 3542 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3543 | |
| 3544 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3545 | |
| 3546 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3547 | psa_set_key_algorithm( &attributes, alg ); |
| 3548 | psa_set_key_type( &attributes, key_type ); |
| 3549 | |
| 3550 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 3551 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 3552 | if ( input_buffer_size > 0 ) |
| 3553 | { |
| 3554 | ASSERT_ALLOC( input, input_buffer_size ); |
| 3555 | memcpy( input, iv->x, iv->len ); |
| 3556 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 3557 | } |
| 3558 | |
| 3559 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 3560 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3561 | |
| 3562 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3563 | &key ) ); |
| 3564 | |
| 3565 | PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 3566 | output_buffer_size, &output_length ) ); |
| 3567 | TEST_ASSERT( output_length <= |
| 3568 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) ); |
| 3569 | TEST_ASSERT( output_length <= |
| 3570 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) ); |
| 3571 | |
| 3572 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3573 | output, output_length ); |
| 3574 | exit: |
| 3575 | mbedtls_free( input ); |
| 3576 | mbedtls_free( output ); |
| 3577 | psa_destroy_key( key ); |
| 3578 | PSA_DONE( ); |
| 3579 | } |
| 3580 | /* END_CASE */ |
| 3581 | |
| 3582 | /* BEGIN_CASE */ |
| 3583 | void cipher_verify_output( int alg_arg, |
| 3584 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3585 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3586 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3587 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3588 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3589 | psa_key_type_t key_type = key_type_arg; |
| 3590 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3591 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3592 | size_t output1_size = 0; |
| 3593 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3594 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3595 | size_t output2_size = 0; |
| 3596 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3597 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3598 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3599 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3600 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3601 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3602 | psa_set_key_algorithm( &attributes, alg ); |
| 3603 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3604 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3605 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3606 | &key ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3607 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3608 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3609 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3610 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, |
| 3611 | output1, output1_size, |
| 3612 | &output1_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3613 | TEST_ASSERT( output1_length <= |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3614 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3615 | TEST_ASSERT( output1_length <= |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3616 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3617 | |
| 3618 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3619 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3620 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3621 | PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length, |
| 3622 | output2, output2_size, |
| 3623 | &output2_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3624 | TEST_ASSERT( output2_length <= |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3625 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3626 | TEST_ASSERT( output2_length <= |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3627 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3628 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3629 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3630 | |
| 3631 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3632 | mbedtls_free( output1 ); |
| 3633 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3634 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3635 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3636 | } |
| 3637 | /* END_CASE */ |
| 3638 | |
| 3639 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3640 | void cipher_verify_output_multipart( int alg_arg, |
| 3641 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3642 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3643 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3644 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3645 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3646 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3647 | psa_key_type_t key_type = key_type_arg; |
| 3648 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3649 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3650 | unsigned char iv[16] = {0}; |
| 3651 | size_t iv_size = 16; |
| 3652 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3653 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3654 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3655 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3656 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3657 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3658 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3659 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3660 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3661 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3662 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3663 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3664 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3665 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3666 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3667 | psa_set_key_algorithm( &attributes, alg ); |
| 3668 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3669 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3670 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3671 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3672 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3673 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 3674 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3675 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3676 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3677 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3678 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3679 | iv, iv_size, |
| 3680 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3681 | } |
| 3682 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3683 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3684 | TEST_ASSERT( output1_buffer_size <= |
| 3685 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3686 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3687 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3688 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3689 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3690 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3691 | output1, output1_buffer_size, |
| 3692 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3693 | TEST_ASSERT( function_output_length <= |
| 3694 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3695 | TEST_ASSERT( function_output_length <= |
| 3696 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3697 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3698 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3699 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3700 | input->x + first_part_size, |
| 3701 | input->len - first_part_size, |
| 3702 | output1, output1_buffer_size, |
| 3703 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3704 | TEST_ASSERT( function_output_length <= |
| 3705 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3706 | alg, |
| 3707 | input->len - first_part_size ) ); |
| 3708 | TEST_ASSERT( function_output_length <= |
| 3709 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3710 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3711 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3712 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3713 | output1 + output1_length, |
| 3714 | output1_buffer_size - output1_length, |
| 3715 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3716 | TEST_ASSERT( function_output_length <= |
| 3717 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3718 | TEST_ASSERT( function_output_length <= |
| 3719 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3720 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3721 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3722 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3723 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3724 | output2_buffer_size = output1_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3725 | TEST_ASSERT( output2_buffer_size <= |
| 3726 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3727 | TEST_ASSERT( output2_buffer_size <= |
| 3728 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3729 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3730 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3731 | if( iv_length > 0 ) |
| 3732 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3733 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3734 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3735 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3736 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3737 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3738 | output2, output2_buffer_size, |
| 3739 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3740 | TEST_ASSERT( function_output_length <= |
| 3741 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3742 | TEST_ASSERT( function_output_length <= |
| 3743 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3744 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3745 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3746 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3747 | output1 + first_part_size, |
| 3748 | output1_length - first_part_size, |
| 3749 | output2, output2_buffer_size, |
| 3750 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3751 | TEST_ASSERT( function_output_length <= |
| 3752 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3753 | alg, |
| 3754 | output1_length - first_part_size ) ); |
| 3755 | TEST_ASSERT( function_output_length <= |
| 3756 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3757 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3758 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3759 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3760 | output2 + output2_length, |
| 3761 | output2_buffer_size - output2_length, |
| 3762 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3763 | TEST_ASSERT( function_output_length <= |
| 3764 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3765 | TEST_ASSERT( function_output_length <= |
| 3766 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3767 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3768 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3769 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3770 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3771 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3772 | |
| 3773 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3774 | psa_cipher_abort( &operation1 ); |
| 3775 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3776 | mbedtls_free( output1 ); |
| 3777 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3778 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3779 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3780 | } |
| 3781 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3782 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3783 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3784 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3785 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3786 | data_t *nonce, |
| 3787 | data_t *additional_data, |
| 3788 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3789 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3790 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3791 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3792 | psa_key_type_t key_type = key_type_arg; |
| 3793 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3794 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3795 | unsigned char *output_data = NULL; |
| 3796 | size_t output_size = 0; |
| 3797 | size_t output_length = 0; |
| 3798 | unsigned char *output_data2 = NULL; |
| 3799 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3800 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3801 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3802 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3803 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3804 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3805 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3806 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3807 | psa_set_key_algorithm( &attributes, alg ); |
| 3808 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3809 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3810 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3811 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3812 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3813 | key_bits = psa_get_key_bits( &attributes ); |
| 3814 | |
| 3815 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3816 | alg ); |
| 3817 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3818 | * should be exact. */ |
| 3819 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3820 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3821 | { |
| 3822 | TEST_EQUAL( output_size, |
| 3823 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3824 | TEST_ASSERT( output_size <= |
| 3825 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3826 | } |
| 3827 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3828 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3829 | status = psa_aead_encrypt( key, alg, |
| 3830 | nonce->x, nonce->len, |
| 3831 | additional_data->x, |
| 3832 | additional_data->len, |
| 3833 | input_data->x, input_data->len, |
| 3834 | output_data, output_size, |
| 3835 | &output_length ); |
| 3836 | |
| 3837 | /* If the operation is not supported, just skip and not fail in case the |
| 3838 | * encryption involves a common limitation of cryptography hardwares and |
| 3839 | * an alternative implementation. */ |
| 3840 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 3841 | { |
| 3842 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3843 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 3844 | } |
| 3845 | |
| 3846 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3847 | |
| 3848 | if( PSA_SUCCESS == expected_result ) |
| 3849 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3850 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3851 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3852 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3853 | * should be exact. */ |
| 3854 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3855 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3856 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3857 | TEST_ASSERT( input_data->len <= |
| 3858 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
| 3859 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3860 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3861 | nonce->x, nonce->len, |
| 3862 | additional_data->x, |
| 3863 | additional_data->len, |
| 3864 | output_data, output_length, |
| 3865 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3866 | &output_length2 ), |
| 3867 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3868 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3869 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3870 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3871 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3872 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3873 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3874 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3875 | mbedtls_free( output_data ); |
| 3876 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3877 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3878 | } |
| 3879 | /* END_CASE */ |
| 3880 | |
| 3881 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3882 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3883 | int alg_arg, |
| 3884 | data_t *nonce, |
| 3885 | data_t *additional_data, |
| 3886 | data_t *input_data, |
| 3887 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3888 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3889 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3890 | psa_key_type_t key_type = key_type_arg; |
| 3891 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3892 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3893 | unsigned char *output_data = NULL; |
| 3894 | size_t output_size = 0; |
| 3895 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3896 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3897 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3898 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3899 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3900 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3901 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3902 | psa_set_key_algorithm( &attributes, alg ); |
| 3903 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3904 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3905 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3906 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3907 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3908 | key_bits = psa_get_key_bits( &attributes ); |
| 3909 | |
| 3910 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3911 | alg ); |
| 3912 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3913 | * should be exact. */ |
| 3914 | TEST_EQUAL( output_size, |
| 3915 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3916 | TEST_ASSERT( output_size <= |
| 3917 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3918 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3919 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3920 | status = psa_aead_encrypt( key, alg, |
| 3921 | nonce->x, nonce->len, |
| 3922 | additional_data->x, additional_data->len, |
| 3923 | input_data->x, input_data->len, |
| 3924 | output_data, output_size, |
| 3925 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3926 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3927 | /* If the operation is not supported, just skip and not fail in case the |
| 3928 | * encryption involves a common limitation of cryptography hardwares and |
| 3929 | * an alternative implementation. */ |
| 3930 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3931 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3932 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3933 | 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] | 3934 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3935 | |
| 3936 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3937 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3938 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3939 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3940 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3941 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3942 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3943 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3944 | } |
| 3945 | /* END_CASE */ |
| 3946 | |
| 3947 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3948 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3949 | int alg_arg, |
| 3950 | data_t *nonce, |
| 3951 | data_t *additional_data, |
| 3952 | data_t *input_data, |
| 3953 | data_t *expected_data, |
| 3954 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3955 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3956 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3957 | psa_key_type_t key_type = key_type_arg; |
| 3958 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3959 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3960 | unsigned char *output_data = NULL; |
| 3961 | size_t output_size = 0; |
| 3962 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3963 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3964 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3965 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3966 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3967 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3968 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3969 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3970 | psa_set_key_algorithm( &attributes, alg ); |
| 3971 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3972 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3973 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3974 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3975 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3976 | key_bits = psa_get_key_bits( &attributes ); |
| 3977 | |
| 3978 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3979 | alg ); |
| 3980 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3981 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3982 | { |
| 3983 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3984 | * should be exact. */ |
| 3985 | TEST_EQUAL( output_size, |
| 3986 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3987 | TEST_ASSERT( output_size <= |
| 3988 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3989 | } |
| 3990 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3991 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3992 | status = psa_aead_decrypt( key, alg, |
| 3993 | nonce->x, nonce->len, |
| 3994 | additional_data->x, |
| 3995 | additional_data->len, |
| 3996 | input_data->x, input_data->len, |
| 3997 | output_data, output_size, |
| 3998 | &output_length ); |
| 3999 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4000 | /* If the operation is not supported, just skip and not fail in case the |
| 4001 | * decryption involves a common limitation of cryptography hardwares and |
| 4002 | * an alternative implementation. */ |
| 4003 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4004 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 4005 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4006 | 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] | 4007 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 4008 | |
| 4009 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4010 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 4011 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 4012 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 4013 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4014 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4015 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 4016 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4017 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 4018 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 4019 | } |
| 4020 | /* END_CASE */ |
| 4021 | |
| 4022 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4023 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 4024 | int alg_arg, |
| 4025 | data_t *nonce, |
| 4026 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4027 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 4028 | int do_set_lengths, |
| 4029 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4030 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4031 | size_t ad_part_len = 0; |
| 4032 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4033 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4034 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4035 | 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] | 4036 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4037 | mbedtls_test_set_step( ad_part_len ); |
| 4038 | |
| 4039 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4040 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4041 | if( ad_part_len & 0x01 ) |
| 4042 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4043 | else |
| 4044 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4045 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4046 | |
| 4047 | /* Split ad into length(ad_part_len) parts. */ |
| 4048 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4049 | alg_arg, nonce, |
| 4050 | additional_data, |
| 4051 | ad_part_len, |
| 4052 | input_data, -1, |
| 4053 | set_lengths_method, |
| 4054 | expected_output, |
| 4055 | 1, 0 ) ) |
| 4056 | break; |
| 4057 | |
| 4058 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4059 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 4060 | |
| 4061 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4062 | alg_arg, nonce, |
| 4063 | additional_data, |
| 4064 | ad_part_len, |
| 4065 | input_data, -1, |
| 4066 | set_lengths_method, |
| 4067 | expected_output, |
| 4068 | 1, 1 ) ) |
| 4069 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4070 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4071 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4072 | 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] | 4073 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4074 | /* Split data into length(data_part_len) parts. */ |
| 4075 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 4076 | |
| 4077 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4078 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4079 | if( data_part_len & 0x01 ) |
| 4080 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4081 | else |
| 4082 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4083 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4084 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4085 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4086 | alg_arg, nonce, |
| 4087 | additional_data, -1, |
| 4088 | input_data, data_part_len, |
| 4089 | set_lengths_method, |
| 4090 | expected_output, |
| 4091 | 1, 0 ) ) |
| 4092 | break; |
| 4093 | |
| 4094 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 4095 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 4096 | |
| 4097 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4098 | alg_arg, nonce, |
| 4099 | additional_data, -1, |
| 4100 | input_data, data_part_len, |
| 4101 | set_lengths_method, |
| 4102 | expected_output, |
| 4103 | 1, 1 ) ) |
| 4104 | break; |
| 4105 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4106 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 4107 | /* Goto is required to silence warnings about unused labels, as we |
| 4108 | * don't actually do any test assertions in this function. */ |
| 4109 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4110 | } |
| 4111 | /* END_CASE */ |
| 4112 | |
| 4113 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4114 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 4115 | int alg_arg, |
| 4116 | data_t *nonce, |
| 4117 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4118 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 4119 | int do_set_lengths, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4120 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4121 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4122 | size_t ad_part_len = 0; |
| 4123 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4124 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4125 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4126 | 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] | 4127 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4128 | /* Split ad into length(ad_part_len) parts. */ |
| 4129 | mbedtls_test_set_step( ad_part_len ); |
| 4130 | |
| 4131 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4132 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4133 | if( ad_part_len & 0x01 ) |
| 4134 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4135 | else |
| 4136 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4137 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4138 | |
| 4139 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4140 | alg_arg, nonce, |
| 4141 | additional_data, |
| 4142 | ad_part_len, |
| 4143 | input_data, -1, |
| 4144 | set_lengths_method, |
| 4145 | expected_output, |
| 4146 | 0, 0 ) ) |
| 4147 | break; |
| 4148 | |
| 4149 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 4150 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 4151 | |
| 4152 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4153 | alg_arg, nonce, |
| 4154 | additional_data, |
| 4155 | ad_part_len, |
| 4156 | input_data, -1, |
| 4157 | set_lengths_method, |
| 4158 | expected_output, |
| 4159 | 0, 1 ) ) |
| 4160 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4161 | } |
| 4162 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4163 | 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] | 4164 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4165 | /* Split data into length(data_part_len) parts. */ |
| 4166 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 4167 | |
| 4168 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4169 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4170 | if( data_part_len & 0x01 ) |
| 4171 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 4172 | else |
| 4173 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4174 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 4175 | |
| 4176 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4177 | alg_arg, nonce, |
| 4178 | additional_data, -1, |
| 4179 | input_data, data_part_len, |
| 4180 | set_lengths_method, |
| 4181 | expected_output, |
| 4182 | 0, 0 ) ) |
| 4183 | break; |
| 4184 | |
| 4185 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 4186 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 4187 | |
| 4188 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 4189 | alg_arg, nonce, |
| 4190 | additional_data, -1, |
| 4191 | input_data, data_part_len, |
| 4192 | set_lengths_method, |
| 4193 | expected_output, |
| 4194 | 0, 1 ) ) |
| 4195 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4196 | } |
| 4197 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 4198 | /* Goto is required to silence warnings about unused labels, as we |
| 4199 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 4200 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 4201 | } |
| 4202 | /* END_CASE */ |
| 4203 | |
| 4204 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4205 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 4206 | int alg_arg, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4207 | int nonce_length, |
| 4208 | int expected_nonce_length_arg, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4209 | data_t *additional_data, |
| 4210 | data_t *input_data, |
| 4211 | int expected_status_arg ) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4212 | { |
| 4213 | |
| 4214 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4215 | psa_key_type_t key_type = key_type_arg; |
| 4216 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4217 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4218 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 4219 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4220 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 4221 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4222 | size_t actual_nonce_length = 0; |
| 4223 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 4224 | unsigned char *output = NULL; |
| 4225 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4226 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4227 | size_t ciphertext_size = 0; |
| 4228 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4229 | size_t tag_length = 0; |
| 4230 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4231 | |
| 4232 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4233 | |
| 4234 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4235 | psa_set_key_algorithm( & attributes, alg ); |
| 4236 | psa_set_key_type( & attributes, key_type ); |
| 4237 | |
| 4238 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4239 | &key ) ); |
| 4240 | |
| 4241 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4242 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4243 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 4244 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4245 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4246 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4247 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4248 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4249 | TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4250 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4251 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4252 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4253 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 4254 | |
| 4255 | /* If the operation is not supported, just skip and not fail in case the |
| 4256 | * encryption involves a common limitation of cryptography hardwares and |
| 4257 | * an alternative implementation. */ |
| 4258 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4259 | { |
| 4260 | 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] | 4261 | 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] | 4262 | } |
| 4263 | |
| 4264 | PSA_ASSERT( status ); |
| 4265 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4266 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4267 | nonce_length, |
| 4268 | &actual_nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4269 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 4270 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4271 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4272 | TEST_EQUAL( actual_nonce_length, expected_nonce_length ); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 4273 | |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 4274 | if( expected_status == PSA_SUCCESS ) |
| 4275 | TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type, |
| 4276 | alg ) ); |
| 4277 | |
Paul Elliott | d79c5c5 | 2021-10-06 21:49:41 +0100 | [diff] [blame] | 4278 | TEST_ASSERT( actual_nonce_length <= PSA_AEAD_NONCE_MAX_SIZE ); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 4279 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 4280 | if( expected_status == PSA_SUCCESS ) |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4281 | { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4282 | /* Ensure we can still complete operation. */ |
Paul Elliott | d79c5c5 | 2021-10-06 21:49:41 +0100 | [diff] [blame] | 4283 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4284 | input_data->len ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4285 | |
| 4286 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 4287 | additional_data->len ) ); |
| 4288 | |
| 4289 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4290 | output, output_size, |
| 4291 | &ciphertext_length ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4292 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4293 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 4294 | &ciphertext_length, tag_buffer, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4295 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 4296 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4297 | |
| 4298 | exit: |
| 4299 | psa_destroy_key( key ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4300 | mbedtls_free( output ); |
| 4301 | mbedtls_free( ciphertext ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4302 | psa_aead_abort( &operation ); |
| 4303 | PSA_DONE( ); |
| 4304 | } |
| 4305 | /* END_CASE */ |
| 4306 | |
| 4307 | /* BEGIN_CASE */ |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4308 | void aead_multipart_set_nonce( int key_type_arg, data_t *key_data, |
| 4309 | int alg_arg, |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4310 | int nonce_length_arg, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 4311 | int set_lengths_method_arg, |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4312 | data_t *additional_data, |
| 4313 | data_t *input_data, |
| 4314 | int expected_status_arg ) |
| 4315 | { |
| 4316 | |
| 4317 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4318 | psa_key_type_t key_type = key_type_arg; |
| 4319 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4320 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4321 | uint8_t *nonce_buffer = NULL; |
| 4322 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4323 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 4324 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4325 | unsigned char *output = NULL; |
| 4326 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4327 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4328 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4329 | size_t ciphertext_size = 0; |
| 4330 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4331 | size_t tag_length = 0; |
| 4332 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4333 | size_t index = 0; |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 4334 | set_lengths_method_t set_lengths_method = set_lengths_method_arg; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4335 | |
| 4336 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4337 | |
| 4338 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4339 | psa_set_key_algorithm( &attributes, alg ); |
| 4340 | psa_set_key_type( &attributes, key_type ); |
| 4341 | |
| 4342 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4343 | &key ) ); |
| 4344 | |
| 4345 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4346 | |
| 4347 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 4348 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4349 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4350 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4351 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4352 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4353 | TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4354 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4355 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4356 | |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4357 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 4358 | |
| 4359 | /* If the operation is not supported, just skip and not fail in case the |
| 4360 | * encryption involves a common limitation of cryptography hardwares and |
| 4361 | * an alternative implementation. */ |
| 4362 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4363 | { |
| 4364 | 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] | 4365 | 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] | 4366 | } |
| 4367 | |
| 4368 | PSA_ASSERT( status ); |
| 4369 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4370 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
| 4371 | if( nonce_length_arg == -1 ) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4372 | { |
Paul Elliott | 5e69aa5 | 2021-08-25 17:24:37 +0100 | [diff] [blame] | 4373 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 4374 | ASSERT_ALLOC( nonce_buffer, 4 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4375 | nonce_length = 0; |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 4376 | } |
| 4377 | else |
| 4378 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4379 | /* If length is zero, then this will return NULL. */ |
| 4380 | nonce_length = ( size_t ) nonce_length_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4381 | ASSERT_ALLOC( nonce_buffer, nonce_length ); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 4382 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4383 | if( nonce_buffer ) |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 4384 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4385 | for( index = 0; index < nonce_length - 1; ++index ) |
| 4386 | { |
| 4387 | nonce_buffer[index] = 'a' + index; |
| 4388 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 4389 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4390 | } |
| 4391 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 4392 | if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
| 4393 | { |
| 4394 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4395 | input_data->len ) ); |
| 4396 | } |
| 4397 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4398 | status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4399 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 4400 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4401 | |
| 4402 | if( expected_status == PSA_SUCCESS ) |
| 4403 | { |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 4404 | if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
| 4405 | { |
| 4406 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4407 | input_data->len ) ); |
| 4408 | } |
| 4409 | if( operation.alg == PSA_ALG_CCM && set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 4410 | expected_status = PSA_ERROR_BAD_STATE; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4411 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 4412 | /* Ensure we can still complete operation, unless it's CCM and we didn't set lengths. */ |
| 4413 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4414 | additional_data->len ), |
| 4415 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4416 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 4417 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4418 | output, output_size, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 4419 | &ciphertext_length ), |
| 4420 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4421 | |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 4422 | TEST_EQUAL( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4423 | &ciphertext_length, tag_buffer, |
Andrzej Kurek | a2ce72e | 2021-12-25 17:21:47 +0100 | [diff] [blame] | 4424 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ), |
| 4425 | expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | exit: |
| 4429 | psa_destroy_key( key ); |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4430 | mbedtls_free( output ); |
| 4431 | mbedtls_free( ciphertext ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4432 | mbedtls_free( nonce_buffer ); |
| 4433 | psa_aead_abort( &operation ); |
| 4434 | PSA_DONE( ); |
| 4435 | } |
| 4436 | /* END_CASE */ |
| 4437 | |
| 4438 | /* BEGIN_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4439 | void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data, |
| 4440 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 4441 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4442 | data_t *nonce, |
| 4443 | data_t *additional_data, |
| 4444 | data_t *input_data, |
| 4445 | int expected_status_arg ) |
| 4446 | { |
| 4447 | |
| 4448 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4449 | psa_key_type_t key_type = key_type_arg; |
| 4450 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4451 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4452 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4453 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 4454 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 4455 | unsigned char *output = NULL; |
| 4456 | unsigned char *ciphertext = NULL; |
| 4457 | size_t output_size = output_size_arg; |
| 4458 | size_t ciphertext_size = 0; |
| 4459 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4460 | size_t tag_length = 0; |
| 4461 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 4462 | |
| 4463 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4464 | |
| 4465 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4466 | psa_set_key_algorithm( &attributes, alg ); |
| 4467 | psa_set_key_type( &attributes, key_type ); |
| 4468 | |
| 4469 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4470 | &key ) ); |
| 4471 | |
| 4472 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4473 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 4474 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4475 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 4476 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4477 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 4478 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4479 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4480 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 4481 | |
| 4482 | /* If the operation is not supported, just skip and not fail in case the |
| 4483 | * encryption involves a common limitation of cryptography hardwares and |
| 4484 | * an alternative implementation. */ |
| 4485 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4486 | { |
| 4487 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4488 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4489 | } |
| 4490 | |
| 4491 | PSA_ASSERT( status ); |
| 4492 | |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 4493 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4494 | input_data->len ) ); |
| 4495 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4496 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4497 | |
| 4498 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 4499 | additional_data->len ) ); |
| 4500 | |
| 4501 | status = psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 4502 | output, output_size, &ciphertext_length ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4503 | |
| 4504 | TEST_EQUAL( status, expected_status ); |
| 4505 | |
| 4506 | if( expected_status == PSA_SUCCESS ) |
| 4507 | { |
| 4508 | /* Ensure we can still complete operation. */ |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 4509 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 4510 | &ciphertext_length, tag_buffer, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4511 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 4512 | } |
| 4513 | |
| 4514 | exit: |
| 4515 | psa_destroy_key( key ); |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 4516 | mbedtls_free( output ); |
| 4517 | mbedtls_free( ciphertext ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4518 | psa_aead_abort( &operation ); |
| 4519 | PSA_DONE( ); |
| 4520 | } |
| 4521 | /* END_CASE */ |
| 4522 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4523 | /* BEGIN_CASE */ |
| 4524 | void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data, |
| 4525 | int alg_arg, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4526 | int finish_ciphertext_size_arg, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 4527 | int tag_size_arg, |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4528 | data_t *nonce, |
| 4529 | data_t *additional_data, |
| 4530 | data_t *input_data, |
| 4531 | int expected_status_arg ) |
| 4532 | { |
| 4533 | |
| 4534 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4535 | psa_key_type_t key_type = key_type_arg; |
| 4536 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4537 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4538 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4539 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 4540 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4541 | unsigned char *ciphertext = NULL; |
| 4542 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 4543 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4544 | size_t ciphertext_size = 0; |
| 4545 | size_t ciphertext_length = 0; |
| 4546 | size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 4547 | size_t tag_size = ( size_t ) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4548 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4549 | |
| 4550 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4551 | |
| 4552 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4553 | psa_set_key_algorithm( &attributes, alg ); |
| 4554 | psa_set_key_type( &attributes, key_type ); |
| 4555 | |
| 4556 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4557 | &key ) ); |
| 4558 | |
| 4559 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4560 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4561 | 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] | 4562 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4563 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4564 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4565 | ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4566 | |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 4567 | ASSERT_ALLOC( tag_buffer, tag_size ); |
| 4568 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4569 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 4570 | |
| 4571 | /* If the operation is not supported, just skip and not fail in case the |
| 4572 | * encryption involves a common limitation of cryptography hardwares and |
| 4573 | * an alternative implementation. */ |
| 4574 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4575 | { |
| 4576 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4577 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4578 | } |
| 4579 | |
| 4580 | PSA_ASSERT( status ); |
| 4581 | |
| 4582 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4583 | |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 4584 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4585 | input_data->len ) ); |
| 4586 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4587 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 4588 | additional_data->len ) ); |
| 4589 | |
| 4590 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4591 | ciphertext, ciphertext_size, &ciphertext_length ) ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4592 | |
| 4593 | /* Ensure we can still complete operation. */ |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4594 | status = psa_aead_finish( &operation, finish_ciphertext, |
| 4595 | finish_ciphertext_size, |
| 4596 | &ciphertext_length, tag_buffer, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 4597 | tag_size, &tag_length ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4598 | |
| 4599 | TEST_EQUAL( status, expected_status ); |
| 4600 | |
| 4601 | exit: |
| 4602 | psa_destroy_key( key ); |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4603 | mbedtls_free( ciphertext ); |
| 4604 | mbedtls_free( finish_ciphertext ); |
Paul Elliott | 4a76088 | 2021-09-20 09:42:21 +0100 | [diff] [blame] | 4605 | mbedtls_free( tag_buffer ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4606 | psa_aead_abort( &operation ); |
| 4607 | PSA_DONE( ); |
| 4608 | } |
| 4609 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4610 | |
| 4611 | /* BEGIN_CASE */ |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4612 | void aead_multipart_verify( int key_type_arg, data_t *key_data, |
| 4613 | int alg_arg, |
| 4614 | data_t *nonce, |
| 4615 | data_t *additional_data, |
| 4616 | data_t *input_data, |
| 4617 | data_t *tag, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 4618 | int tag_usage_arg, |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4619 | int expected_setup_status_arg, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4620 | int expected_status_arg ) |
| 4621 | { |
| 4622 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4623 | psa_key_type_t key_type = key_type_arg; |
| 4624 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4625 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4626 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4627 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 4628 | psa_status_t expected_status = expected_status_arg; |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4629 | psa_status_t expected_setup_status = expected_setup_status_arg; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4630 | unsigned char *plaintext = NULL; |
| 4631 | unsigned char *finish_plaintext = NULL; |
| 4632 | size_t plaintext_size = 0; |
| 4633 | size_t plaintext_length = 0; |
| 4634 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4635 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 4636 | unsigned char *tag_buffer = NULL; |
| 4637 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4638 | |
| 4639 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4640 | |
| 4641 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4642 | psa_set_key_algorithm( &attributes, alg ); |
| 4643 | psa_set_key_type( &attributes, key_type ); |
| 4644 | |
| 4645 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4646 | &key ) ); |
| 4647 | |
| 4648 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4649 | |
| 4650 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 4651 | input_data->len ); |
| 4652 | |
| 4653 | ASSERT_ALLOC( plaintext, plaintext_size ); |
| 4654 | |
| 4655 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 4656 | |
| 4657 | ASSERT_ALLOC( finish_plaintext, verify_plaintext_size ); |
| 4658 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4659 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 4660 | |
| 4661 | /* If the operation is not supported, just skip and not fail in case the |
| 4662 | * encryption involves a common limitation of cryptography hardwares and |
| 4663 | * an alternative implementation. */ |
| 4664 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4665 | { |
| 4666 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4667 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4668 | } |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4669 | TEST_EQUAL( status, expected_setup_status ); |
| 4670 | |
| 4671 | if( status != PSA_SUCCESS ) |
| 4672 | goto exit; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4673 | |
| 4674 | PSA_ASSERT( status ); |
| 4675 | |
| 4676 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4677 | |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 4678 | status = psa_aead_set_lengths( &operation, additional_data->len, |
| 4679 | input_data->len ); |
Andrzej Kurek | f881601 | 2021-12-19 17:00:12 +0100 | [diff] [blame] | 4680 | PSA_ASSERT( status ); |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 4681 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4682 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 4683 | additional_data->len ) ); |
| 4684 | |
| 4685 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 4686 | input_data->len, |
| 4687 | plaintext, plaintext_size, |
| 4688 | &plaintext_length ) ); |
| 4689 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 4690 | if( tag_usage == USE_GIVEN_TAG ) |
| 4691 | { |
| 4692 | tag_buffer = tag->x; |
| 4693 | tag_size = tag->len; |
| 4694 | } |
| 4695 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4696 | status = psa_aead_verify( &operation, finish_plaintext, |
| 4697 | verify_plaintext_size, |
| 4698 | &plaintext_length, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 4699 | tag_buffer, tag_size ); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4700 | |
| 4701 | TEST_EQUAL( status, expected_status ); |
| 4702 | |
| 4703 | exit: |
| 4704 | psa_destroy_key( key ); |
| 4705 | mbedtls_free( plaintext ); |
| 4706 | mbedtls_free( finish_plaintext ); |
| 4707 | psa_aead_abort( &operation ); |
| 4708 | PSA_DONE( ); |
| 4709 | } |
| 4710 | /* END_CASE */ |
| 4711 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4712 | /* BEGIN_CASE */ |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 4713 | void aead_multipart_setup( int key_type_arg, data_t *key_data, |
| 4714 | int alg_arg, int expected_status_arg ) |
| 4715 | { |
| 4716 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4717 | psa_key_type_t key_type = key_type_arg; |
| 4718 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4719 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 4720 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4721 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 4722 | psa_status_t expected_status = expected_status_arg; |
| 4723 | |
| 4724 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4725 | |
| 4726 | psa_set_key_usage_flags( &attributes, |
| 4727 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4728 | psa_set_key_algorithm( &attributes, alg ); |
| 4729 | psa_set_key_type( &attributes, key_type ); |
| 4730 | |
| 4731 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4732 | &key ) ); |
| 4733 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 4734 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 4735 | |
| 4736 | TEST_EQUAL( status, expected_status ); |
| 4737 | |
| 4738 | psa_aead_abort( &operation ); |
| 4739 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 4740 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 4741 | |
| 4742 | TEST_EQUAL(status, expected_status ); |
| 4743 | |
| 4744 | exit: |
| 4745 | psa_destroy_key( key ); |
| 4746 | psa_aead_abort( &operation ); |
| 4747 | PSA_DONE( ); |
| 4748 | } |
| 4749 | /* END_CASE */ |
| 4750 | |
| 4751 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4752 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 4753 | int alg_arg, |
| 4754 | data_t *nonce, |
| 4755 | data_t *additional_data, |
| 4756 | data_t *input_data ) |
| 4757 | { |
| 4758 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4759 | psa_key_type_t key_type = key_type_arg; |
| 4760 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4761 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4762 | unsigned char *output_data = NULL; |
| 4763 | unsigned char *final_data = NULL; |
| 4764 | size_t output_size = 0; |
| 4765 | size_t finish_output_size = 0; |
| 4766 | size_t output_length = 0; |
| 4767 | size_t key_bits = 0; |
| 4768 | size_t tag_length = 0; |
| 4769 | size_t tag_size = 0; |
| 4770 | size_t nonce_length = 0; |
| 4771 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 4772 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 4773 | size_t output_part_length = 0; |
| 4774 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4775 | |
| 4776 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4777 | |
| 4778 | psa_set_key_usage_flags( & attributes, |
| 4779 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4780 | psa_set_key_algorithm( & attributes, alg ); |
| 4781 | psa_set_key_type( & attributes, key_type ); |
| 4782 | |
| 4783 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4784 | &key ) ); |
| 4785 | |
| 4786 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4787 | key_bits = psa_get_key_bits( &attributes ); |
| 4788 | |
| 4789 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 4790 | |
| 4791 | TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE ); |
| 4792 | |
| 4793 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 4794 | |
| 4795 | ASSERT_ALLOC( output_data, output_size ); |
| 4796 | |
| 4797 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4798 | |
| 4799 | TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
| 4800 | |
| 4801 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 4802 | |
| 4803 | /* Test all operations error without calling setup first. */ |
| 4804 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4805 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 4806 | PSA_ERROR_BAD_STATE ); |
| 4807 | |
| 4808 | psa_aead_abort( &operation ); |
| 4809 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4810 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4811 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4812 | &nonce_length ), |
| 4813 | PSA_ERROR_BAD_STATE ); |
| 4814 | |
| 4815 | psa_aead_abort( &operation ); |
| 4816 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4817 | /* ------------------------------------------------------- */ |
| 4818 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4819 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4820 | input_data->len ), |
| 4821 | PSA_ERROR_BAD_STATE ); |
| 4822 | |
| 4823 | psa_aead_abort( &operation ); |
| 4824 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4825 | /* ------------------------------------------------------- */ |
| 4826 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4827 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4828 | additional_data->len ), |
| 4829 | PSA_ERROR_BAD_STATE ); |
| 4830 | |
| 4831 | psa_aead_abort( &operation ); |
| 4832 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4833 | /* ------------------------------------------------------- */ |
| 4834 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4835 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 4836 | input_data->len, output_data, |
| 4837 | output_size, &output_length ), |
| 4838 | PSA_ERROR_BAD_STATE ); |
| 4839 | |
| 4840 | psa_aead_abort( &operation ); |
| 4841 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4842 | /* ------------------------------------------------------- */ |
| 4843 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4844 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 4845 | finish_output_size, |
| 4846 | &output_part_length, |
| 4847 | tag_buffer, tag_length, |
| 4848 | &tag_size ), |
| 4849 | PSA_ERROR_BAD_STATE ); |
| 4850 | |
| 4851 | psa_aead_abort( &operation ); |
| 4852 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4853 | /* ------------------------------------------------------- */ |
| 4854 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4855 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 4856 | finish_output_size, |
| 4857 | &output_part_length, |
| 4858 | tag_buffer, |
| 4859 | tag_length ), |
| 4860 | PSA_ERROR_BAD_STATE ); |
| 4861 | |
| 4862 | psa_aead_abort( &operation ); |
| 4863 | |
| 4864 | /* Test for double setups. */ |
| 4865 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4866 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4867 | |
| 4868 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 4869 | PSA_ERROR_BAD_STATE ); |
| 4870 | |
| 4871 | psa_aead_abort( &operation ); |
| 4872 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4873 | /* ------------------------------------------------------- */ |
| 4874 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4875 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4876 | |
| 4877 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 4878 | PSA_ERROR_BAD_STATE ); |
| 4879 | |
| 4880 | psa_aead_abort( &operation ); |
| 4881 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4882 | /* ------------------------------------------------------- */ |
| 4883 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4884 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4885 | |
| 4886 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 4887 | PSA_ERROR_BAD_STATE ); |
| 4888 | |
| 4889 | psa_aead_abort( &operation ); |
| 4890 | |
| 4891 | /* ------------------------------------------------------- */ |
| 4892 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4893 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4894 | |
| 4895 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 4896 | PSA_ERROR_BAD_STATE ); |
| 4897 | |
| 4898 | psa_aead_abort( &operation ); |
| 4899 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4900 | /* Test for not setting a nonce. */ |
| 4901 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4902 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4903 | |
| 4904 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4905 | additional_data->len ), |
| 4906 | PSA_ERROR_BAD_STATE ); |
| 4907 | |
| 4908 | psa_aead_abort( &operation ); |
| 4909 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 4910 | /* ------------------------------------------------------- */ |
| 4911 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 4912 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4913 | |
| 4914 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 4915 | input_data->len, output_data, |
| 4916 | output_size, &output_length ), |
| 4917 | PSA_ERROR_BAD_STATE ); |
| 4918 | |
| 4919 | psa_aead_abort( &operation ); |
| 4920 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 4921 | /* ------------------------------------------------------- */ |
| 4922 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 4923 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4924 | |
| 4925 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 4926 | finish_output_size, |
| 4927 | &output_part_length, |
| 4928 | tag_buffer, tag_length, |
| 4929 | &tag_size ), |
| 4930 | PSA_ERROR_BAD_STATE ); |
| 4931 | |
| 4932 | psa_aead_abort( &operation ); |
| 4933 | |
| 4934 | /* ------------------------------------------------------- */ |
| 4935 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 4936 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4937 | |
| 4938 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 4939 | finish_output_size, |
| 4940 | &output_part_length, |
| 4941 | tag_buffer, |
| 4942 | tag_length ), |
| 4943 | PSA_ERROR_BAD_STATE ); |
| 4944 | |
| 4945 | psa_aead_abort( &operation ); |
| 4946 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4947 | /* Test for double setting nonce. */ |
| 4948 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4949 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4950 | |
| 4951 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4952 | |
| 4953 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 4954 | PSA_ERROR_BAD_STATE ); |
| 4955 | |
| 4956 | psa_aead_abort( &operation ); |
| 4957 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4958 | /* Test for double generating nonce. */ |
| 4959 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4960 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4961 | |
| 4962 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4963 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4964 | &nonce_length ) ); |
| 4965 | |
| 4966 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4967 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4968 | &nonce_length ), |
| 4969 | PSA_ERROR_BAD_STATE ); |
| 4970 | |
| 4971 | |
| 4972 | psa_aead_abort( &operation ); |
| 4973 | |
| 4974 | /* Test for generate nonce then set and vice versa */ |
| 4975 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4976 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4977 | |
| 4978 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4979 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4980 | &nonce_length ) ); |
| 4981 | |
| 4982 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 4983 | PSA_ERROR_BAD_STATE ); |
| 4984 | |
| 4985 | psa_aead_abort( &operation ); |
| 4986 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 4987 | /* Test for generating nonce after calling set lengths */ |
| 4988 | |
| 4989 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4990 | |
| 4991 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4992 | input_data->len ) ); |
| 4993 | |
| 4994 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4995 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4996 | &nonce_length ) ); |
| 4997 | |
| 4998 | psa_aead_abort( &operation ); |
| 4999 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5000 | /* 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] | 5001 | |
| 5002 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5003 | |
| 5004 | if( operation.alg == PSA_ALG_CCM ) |
| 5005 | { |
| 5006 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5007 | input_data->len ), |
| 5008 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5009 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5010 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5011 | &nonce_length ), |
| 5012 | PSA_ERROR_BAD_STATE ); |
| 5013 | } |
| 5014 | else |
| 5015 | { |
| 5016 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5017 | input_data->len ) ); |
| 5018 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5019 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5020 | &nonce_length ) ); |
| 5021 | } |
| 5022 | |
| 5023 | psa_aead_abort( &operation ); |
| 5024 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5025 | /* 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] | 5026 | #if SIZE_MAX > UINT32_MAX |
| 5027 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5028 | |
| 5029 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 5030 | { |
| 5031 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5032 | input_data->len ), |
| 5033 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5034 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5035 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5036 | &nonce_length ), |
| 5037 | PSA_ERROR_BAD_STATE ); |
| 5038 | } |
| 5039 | else |
| 5040 | { |
| 5041 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5042 | input_data->len ) ); |
| 5043 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5044 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5045 | &nonce_length ) ); |
| 5046 | } |
| 5047 | |
| 5048 | psa_aead_abort( &operation ); |
| 5049 | #endif |
| 5050 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5051 | /* 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] | 5052 | |
| 5053 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5054 | |
| 5055 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5056 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5057 | &nonce_length ) ); |
| 5058 | |
| 5059 | if( operation.alg == PSA_ALG_CCM ) |
| 5060 | { |
| 5061 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5062 | input_data->len ), |
| 5063 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5064 | } |
| 5065 | else |
| 5066 | { |
| 5067 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5068 | input_data->len ) ); |
| 5069 | } |
| 5070 | |
| 5071 | psa_aead_abort( &operation ); |
| 5072 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5073 | /* ------------------------------------------------------- */ |
Andrzej Kurek | 1e8e174 | 2021-12-25 23:50:53 +0100 | [diff] [blame] | 5074 | /* Test for setting nonce after calling set lengths */ |
| 5075 | |
| 5076 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5077 | |
| 5078 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5079 | input_data->len ) ); |
| 5080 | |
| 5081 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5082 | |
| 5083 | psa_aead_abort( &operation ); |
| 5084 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5085 | /* 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] | 5086 | |
| 5087 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5088 | |
| 5089 | if( operation.alg == PSA_ALG_CCM ) |
| 5090 | { |
| 5091 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5092 | input_data->len ), |
| 5093 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5094 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5095 | PSA_ERROR_BAD_STATE ); |
| 5096 | } |
| 5097 | else |
| 5098 | { |
| 5099 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5100 | input_data->len ) ); |
| 5101 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5102 | } |
| 5103 | |
| 5104 | psa_aead_abort( &operation ); |
| 5105 | |
Andrzej Kurek | e5f94fb | 2021-12-26 01:00:20 +0100 | [diff] [blame] | 5106 | /* 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] | 5107 | #if SIZE_MAX > UINT32_MAX |
| 5108 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5109 | |
| 5110 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 5111 | { |
| 5112 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5113 | input_data->len ), |
| 5114 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5115 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5116 | PSA_ERROR_BAD_STATE ); |
| 5117 | } |
| 5118 | else |
| 5119 | { |
| 5120 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 5121 | input_data->len ) ); |
| 5122 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5123 | } |
| 5124 | |
| 5125 | psa_aead_abort( &operation ); |
| 5126 | #endif |
| 5127 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5128 | /* 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] | 5129 | |
| 5130 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5131 | |
| 5132 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5133 | |
| 5134 | if( operation.alg == PSA_ALG_CCM ) |
| 5135 | { |
| 5136 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5137 | input_data->len ), |
| 5138 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5139 | } |
| 5140 | else |
| 5141 | { |
| 5142 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 5143 | input_data->len ) ); |
| 5144 | } |
| 5145 | |
| 5146 | psa_aead_abort( &operation ); |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5147 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5148 | /* 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] | 5149 | #if SIZE_MAX > UINT32_MAX |
| 5150 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5151 | |
| 5152 | if( operation.alg == PSA_ALG_GCM ) |
| 5153 | { |
| 5154 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5155 | SIZE_MAX ), |
| 5156 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5157 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 5158 | PSA_ERROR_BAD_STATE ); |
| 5159 | } |
| 5160 | else if ( operation.alg != PSA_ALG_CCM ) |
| 5161 | { |
| 5162 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5163 | SIZE_MAX ) ); |
| 5164 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5165 | } |
| 5166 | |
| 5167 | psa_aead_abort( &operation ); |
| 5168 | |
Andrzej Kurek | 031df4a | 2022-01-19 12:44:49 -0500 | [diff] [blame] | 5169 | /* 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] | 5170 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5171 | |
| 5172 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5173 | |
| 5174 | if( operation.alg == PSA_ALG_GCM ) |
| 5175 | { |
| 5176 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5177 | SIZE_MAX ), |
| 5178 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5179 | } |
| 5180 | else if ( operation.alg != PSA_ALG_CCM ) |
| 5181 | { |
| 5182 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5183 | SIZE_MAX ) ); |
| 5184 | } |
| 5185 | |
| 5186 | psa_aead_abort( &operation ); |
| 5187 | #endif |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5188 | |
| 5189 | /* ------------------------------------------------------- */ |
| 5190 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 5191 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5192 | |
| 5193 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5194 | |
| 5195 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5196 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5197 | &nonce_length ), |
| 5198 | PSA_ERROR_BAD_STATE ); |
| 5199 | |
| 5200 | psa_aead_abort( &operation ); |
| 5201 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 5202 | /* Test for generating nonce in decrypt setup. */ |
| 5203 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 5204 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5205 | |
| 5206 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5207 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5208 | &nonce_length ), |
| 5209 | PSA_ERROR_BAD_STATE ); |
| 5210 | |
| 5211 | psa_aead_abort( &operation ); |
| 5212 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5213 | /* Test for setting lengths twice. */ |
| 5214 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5215 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5216 | |
| 5217 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5218 | |
| 5219 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5220 | input_data->len ) ); |
| 5221 | |
| 5222 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5223 | input_data->len ), |
| 5224 | PSA_ERROR_BAD_STATE ); |
| 5225 | |
| 5226 | psa_aead_abort( &operation ); |
| 5227 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5228 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5229 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5230 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5231 | |
| 5232 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5233 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5234 | if( operation.alg == PSA_ALG_CCM ) |
| 5235 | { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 5236 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5237 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5238 | additional_data->len ), |
| 5239 | PSA_ERROR_BAD_STATE ); |
| 5240 | } |
| 5241 | else |
| 5242 | { |
| 5243 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5244 | additional_data->len ) ); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 5245 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5246 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5247 | input_data->len ), |
| 5248 | PSA_ERROR_BAD_STATE ); |
| 5249 | } |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 5250 | psa_aead_abort( &operation ); |
| 5251 | |
| 5252 | /* ------------------------------------------------------- */ |
| 5253 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 5254 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5255 | |
| 5256 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5257 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5258 | if( operation.alg == PSA_ALG_CCM ) |
| 5259 | { |
| 5260 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5261 | input_data->len, output_data, |
| 5262 | output_size, &output_length ), |
| 5263 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5264 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5265 | } |
| 5266 | else |
| 5267 | { |
| 5268 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5269 | input_data->len, output_data, |
| 5270 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5271 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5272 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5273 | input_data->len ), |
| 5274 | PSA_ERROR_BAD_STATE ); |
| 5275 | } |
| 5276 | psa_aead_abort( &operation ); |
| 5277 | |
| 5278 | /* ------------------------------------------------------- */ |
| 5279 | |
| 5280 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5281 | |
| 5282 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5283 | |
| 5284 | if( operation.alg == PSA_ALG_CCM ) |
| 5285 | { |
| 5286 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 5287 | finish_output_size, |
| 5288 | &output_part_length, |
| 5289 | tag_buffer, tag_length, |
| 5290 | &tag_size ) ); |
| 5291 | } |
| 5292 | else |
| 5293 | { |
| 5294 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 5295 | finish_output_size, |
| 5296 | &output_part_length, |
| 5297 | tag_buffer, tag_length, |
| 5298 | &tag_size ) ); |
| 5299 | |
| 5300 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5301 | input_data->len ), |
| 5302 | PSA_ERROR_BAD_STATE ); |
| 5303 | } |
| 5304 | psa_aead_abort( &operation ); |
| 5305 | |
| 5306 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 5307 | |
| 5308 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5309 | |
| 5310 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5311 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5312 | &nonce_length ) ); |
| 5313 | if( operation.alg == PSA_ALG_CCM ) |
| 5314 | { |
| 5315 | |
| 5316 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5317 | additional_data->len ), |
| 5318 | PSA_ERROR_BAD_STATE ); |
| 5319 | } |
| 5320 | else |
| 5321 | { |
| 5322 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5323 | additional_data->len ) ); |
| 5324 | |
| 5325 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5326 | input_data->len ), |
| 5327 | PSA_ERROR_BAD_STATE ); |
| 5328 | } |
| 5329 | psa_aead_abort( &operation ); |
| 5330 | |
| 5331 | /* ------------------------------------------------------- */ |
| 5332 | |
| 5333 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5334 | |
| 5335 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5336 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5337 | &nonce_length ) ); |
| 5338 | if( operation.alg == PSA_ALG_CCM ) |
| 5339 | { |
| 5340 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5341 | input_data->len, output_data, |
| 5342 | output_size, &output_length ), |
| 5343 | PSA_ERROR_BAD_STATE ); |
| 5344 | |
| 5345 | } |
| 5346 | else |
| 5347 | { |
| 5348 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5349 | input_data->len, output_data, |
| 5350 | output_size, &output_length ) ); |
| 5351 | |
| 5352 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5353 | input_data->len ), |
| 5354 | PSA_ERROR_BAD_STATE ); |
| 5355 | } |
| 5356 | psa_aead_abort( &operation ); |
| 5357 | |
| 5358 | /* ------------------------------------------------------- */ |
| 5359 | |
| 5360 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5361 | |
| 5362 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5363 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5364 | &nonce_length ) ); |
| 5365 | if( operation.alg == PSA_ALG_CCM ) |
| 5366 | { |
| 5367 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 5368 | finish_output_size, |
| 5369 | &output_part_length, |
| 5370 | tag_buffer, tag_length, |
| 5371 | &tag_size ) ); |
| 5372 | } |
| 5373 | else |
| 5374 | { |
| 5375 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 5376 | finish_output_size, |
| 5377 | &output_part_length, |
| 5378 | tag_buffer, tag_length, |
| 5379 | &tag_size ) ); |
| 5380 | |
| 5381 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5382 | input_data->len ), |
| 5383 | PSA_ERROR_BAD_STATE ); |
| 5384 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5385 | psa_aead_abort( &operation ); |
| 5386 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 5387 | /* Test for not sending any additional data or data after setting non zero |
| 5388 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5389 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5390 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5391 | |
| 5392 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5393 | |
| 5394 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5395 | input_data->len ) ); |
| 5396 | |
| 5397 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5398 | finish_output_size, |
| 5399 | &output_part_length, |
| 5400 | tag_buffer, tag_length, |
| 5401 | &tag_size ), |
| 5402 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5403 | |
| 5404 | psa_aead_abort( &operation ); |
| 5405 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 5406 | /* Test for not sending any additional data or data after setting non-zero |
| 5407 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5408 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5409 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5410 | |
| 5411 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5412 | |
| 5413 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5414 | input_data->len ) ); |
| 5415 | |
| 5416 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5417 | finish_output_size, |
| 5418 | &output_part_length, |
| 5419 | tag_buffer, |
| 5420 | tag_length ), |
| 5421 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5422 | |
| 5423 | psa_aead_abort( &operation ); |
| 5424 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 5425 | /* Test for not sending any additional data after setting a non-zero length |
| 5426 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5427 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5428 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5429 | |
| 5430 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5431 | |
| 5432 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5433 | input_data->len ) ); |
| 5434 | |
| 5435 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5436 | input_data->len, output_data, |
| 5437 | output_size, &output_length ), |
| 5438 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5439 | |
| 5440 | psa_aead_abort( &operation ); |
| 5441 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 5442 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 5443 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 5444 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5445 | |
| 5446 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5447 | |
| 5448 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5449 | input_data->len ) ); |
| 5450 | |
| 5451 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5452 | additional_data->len ) ); |
| 5453 | |
| 5454 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5455 | finish_output_size, |
| 5456 | &output_part_length, |
| 5457 | tag_buffer, tag_length, |
| 5458 | &tag_size ), |
| 5459 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5460 | |
| 5461 | psa_aead_abort( &operation ); |
| 5462 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 5463 | /* Test for sending too much additional data after setting lengths. */ |
| 5464 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 5465 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5466 | |
| 5467 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5468 | |
| 5469 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 5470 | |
| 5471 | |
| 5472 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5473 | additional_data->len ), |
| 5474 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5475 | |
| 5476 | psa_aead_abort( &operation ); |
| 5477 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 5478 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 5479 | |
| 5480 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5481 | |
| 5482 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5483 | |
| 5484 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5485 | input_data->len ) ); |
| 5486 | |
| 5487 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5488 | additional_data->len ) ); |
| 5489 | |
| 5490 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5491 | 1 ), |
| 5492 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5493 | |
| 5494 | psa_aead_abort( &operation ); |
| 5495 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 5496 | /* Test for sending too much data after setting lengths. */ |
| 5497 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 5498 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5499 | |
| 5500 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5501 | |
| 5502 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 5503 | |
| 5504 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5505 | input_data->len, output_data, |
| 5506 | output_size, &output_length ), |
| 5507 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5508 | |
| 5509 | psa_aead_abort( &operation ); |
| 5510 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 5511 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 5512 | |
| 5513 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5514 | |
| 5515 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5516 | |
| 5517 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5518 | input_data->len ) ); |
| 5519 | |
| 5520 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5521 | additional_data->len ) ); |
| 5522 | |
| 5523 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5524 | input_data->len, output_data, |
| 5525 | output_size, &output_length ) ); |
| 5526 | |
| 5527 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5528 | 1, output_data, |
| 5529 | output_size, &output_length ), |
| 5530 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5531 | |
| 5532 | psa_aead_abort( &operation ); |
| 5533 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5534 | /* Test sending additional data after data. */ |
| 5535 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5536 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5537 | |
| 5538 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5539 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5540 | if( operation.alg != PSA_ALG_CCM ) |
| 5541 | { |
| 5542 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5543 | input_data->len, output_data, |
| 5544 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5545 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame] | 5546 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5547 | additional_data->len ), |
| 5548 | PSA_ERROR_BAD_STATE ); |
| 5549 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5550 | psa_aead_abort( &operation ); |
| 5551 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 5552 | /* Test calling finish on decryption. */ |
| 5553 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 5554 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5555 | |
| 5556 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5557 | |
| 5558 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5559 | finish_output_size, |
| 5560 | &output_part_length, |
| 5561 | tag_buffer, tag_length, |
| 5562 | &tag_size ), |
| 5563 | PSA_ERROR_BAD_STATE ); |
| 5564 | |
| 5565 | psa_aead_abort( &operation ); |
| 5566 | |
| 5567 | /* Test calling verify on encryption. */ |
| 5568 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 5569 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5570 | |
| 5571 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5572 | |
| 5573 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5574 | finish_output_size, |
| 5575 | &output_part_length, |
| 5576 | tag_buffer, |
| 5577 | tag_length ), |
Paul Elliott | 5b065cb | 2021-06-23 08:33:22 +0100 | [diff] [blame] | 5578 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 5579 | |
| 5580 | psa_aead_abort( &operation ); |
| 5581 | |
| 5582 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5583 | exit: |
| 5584 | psa_destroy_key( key ); |
| 5585 | psa_aead_abort( &operation ); |
| 5586 | mbedtls_free( output_data ); |
| 5587 | mbedtls_free( final_data ); |
| 5588 | PSA_DONE( ); |
| 5589 | } |
| 5590 | /* END_CASE */ |
| 5591 | |
| 5592 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 5593 | void signature_size( int type_arg, |
| 5594 | int bits, |
| 5595 | int alg_arg, |
| 5596 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5597 | { |
| 5598 | psa_key_type_t type = type_arg; |
| 5599 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5600 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 5601 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5602 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 5603 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5604 | exit: |
| 5605 | ; |
| 5606 | } |
| 5607 | /* END_CASE */ |
| 5608 | |
| 5609 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 5610 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 5611 | int alg_arg, data_t *input_data, |
| 5612 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5613 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5614 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5615 | psa_key_type_t key_type = key_type_arg; |
| 5616 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5617 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5618 | unsigned char *signature = NULL; |
| 5619 | size_t signature_size; |
| 5620 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5621 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5622 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5623 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5624 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5625 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5626 | psa_set_key_algorithm( &attributes, alg ); |
| 5627 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 5628 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5629 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5630 | &key ) ); |
| 5631 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5632 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5633 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 5634 | /* Allocate a buffer which has the size advertized by the |
| 5635 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5636 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 5637 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5638 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5639 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5640 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5641 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 5642 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5643 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5644 | input_data->x, input_data->len, |
| 5645 | signature, signature_size, |
| 5646 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5647 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 5648 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 5649 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5650 | |
| 5651 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5652 | /* |
| 5653 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5654 | * thus reset them as required. |
| 5655 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5656 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5657 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5658 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 5659 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5660 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5661 | } |
| 5662 | /* END_CASE */ |
| 5663 | |
| 5664 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 5665 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 5666 | int alg_arg, data_t *input_data, |
| 5667 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5668 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5669 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5670 | psa_key_type_t key_type = key_type_arg; |
| 5671 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 5672 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5673 | psa_status_t actual_status; |
| 5674 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 5675 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 5676 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5677 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5678 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5679 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5680 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5681 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5682 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5683 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5684 | psa_set_key_algorithm( &attributes, alg ); |
| 5685 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 5686 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5687 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5688 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5689 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5690 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5691 | input_data->x, input_data->len, |
| 5692 | signature, signature_size, |
| 5693 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5694 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 5695 | /* The value of *signature_length is unspecified on error, but |
| 5696 | * whatever it is, it should be less than signature_size, so that |
| 5697 | * if the caller tries to read *signature_length bytes without |
| 5698 | * checking the error code then they don't overflow a buffer. */ |
| 5699 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5700 | |
| 5701 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5702 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5703 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5704 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5705 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5706 | } |
| 5707 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 5708 | |
| 5709 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 5710 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 5711 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5712 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5713 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5714 | psa_key_type_t key_type = key_type_arg; |
| 5715 | psa_algorithm_t alg = alg_arg; |
| 5716 | size_t key_bits; |
| 5717 | unsigned char *signature = NULL; |
| 5718 | size_t signature_size; |
| 5719 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5720 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5721 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5722 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5723 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5724 | 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] | 5725 | psa_set_key_algorithm( &attributes, alg ); |
| 5726 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5727 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5728 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5729 | &key ) ); |
| 5730 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5731 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5732 | |
| 5733 | /* Allocate a buffer which has the size advertized by the |
| 5734 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5735 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5736 | key_bits, alg ); |
| 5737 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5738 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5739 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5740 | |
| 5741 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5742 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5743 | input_data->x, input_data->len, |
| 5744 | signature, signature_size, |
| 5745 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5746 | /* Check that the signature length looks sensible. */ |
| 5747 | TEST_ASSERT( signature_length <= signature_size ); |
| 5748 | TEST_ASSERT( signature_length > 0 ); |
| 5749 | |
| 5750 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5751 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5752 | input_data->x, input_data->len, |
| 5753 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5754 | |
| 5755 | if( input_data->len != 0 ) |
| 5756 | { |
| 5757 | /* Flip a bit in the input and verify that the signature is now |
| 5758 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 5759 | * because ECDSA may ignore the last few bits of the input. */ |
| 5760 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5761 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5762 | input_data->x, input_data->len, |
| 5763 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 5764 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5765 | } |
| 5766 | |
| 5767 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5768 | /* |
| 5769 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5770 | * thus reset them as required. |
| 5771 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5772 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5773 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5774 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5775 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5776 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5777 | } |
| 5778 | /* END_CASE */ |
| 5779 | |
| 5780 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 5781 | void verify_hash( int key_type_arg, data_t *key_data, |
| 5782 | int alg_arg, data_t *hash_data, |
| 5783 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 5784 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5785 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 5786 | psa_key_type_t key_type = key_type_arg; |
| 5787 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5788 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 5789 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5790 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 5791 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5792 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 5793 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5794 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5795 | psa_set_key_algorithm( &attributes, alg ); |
| 5796 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 5797 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5798 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5799 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 5800 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5801 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5802 | hash_data->x, hash_data->len, |
| 5803 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 5804 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 5805 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5806 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5807 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5808 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 5809 | } |
| 5810 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5811 | |
| 5812 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 5813 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 5814 | int alg_arg, data_t *hash_data, |
| 5815 | data_t *signature_data, |
| 5816 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5817 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5818 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5819 | psa_key_type_t key_type = key_type_arg; |
| 5820 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5821 | psa_status_t actual_status; |
| 5822 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5823 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5824 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5825 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5826 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5827 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5828 | psa_set_key_algorithm( &attributes, alg ); |
| 5829 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 5830 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5831 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5832 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5833 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5834 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5835 | hash_data->x, hash_data->len, |
| 5836 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5837 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5838 | |
| 5839 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5840 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5841 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5842 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5843 | } |
| 5844 | /* END_CASE */ |
| 5845 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5846 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 5847 | void sign_message_deterministic( int key_type_arg, |
| 5848 | data_t *key_data, |
| 5849 | int alg_arg, |
| 5850 | data_t *input_data, |
| 5851 | data_t *output_data ) |
| 5852 | { |
| 5853 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5854 | psa_key_type_t key_type = key_type_arg; |
| 5855 | psa_algorithm_t alg = alg_arg; |
| 5856 | size_t key_bits; |
| 5857 | unsigned char *signature = NULL; |
| 5858 | size_t signature_size; |
| 5859 | size_t signature_length = 0xdeadbeef; |
| 5860 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5861 | |
| 5862 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5863 | |
| 5864 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 5865 | psa_set_key_algorithm( &attributes, alg ); |
| 5866 | psa_set_key_type( &attributes, key_type ); |
| 5867 | |
| 5868 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5869 | &key ) ); |
| 5870 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5871 | key_bits = psa_get_key_bits( &attributes ); |
| 5872 | |
| 5873 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 5874 | TEST_ASSERT( signature_size != 0 ); |
| 5875 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 5876 | ASSERT_ALLOC( signature, signature_size ); |
| 5877 | |
| 5878 | PSA_ASSERT( psa_sign_message( key, alg, |
| 5879 | input_data->x, input_data->len, |
| 5880 | signature, signature_size, |
| 5881 | &signature_length ) ); |
| 5882 | |
| 5883 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 5884 | signature, signature_length ); |
| 5885 | |
| 5886 | exit: |
| 5887 | psa_reset_key_attributes( &attributes ); |
| 5888 | |
| 5889 | psa_destroy_key( key ); |
| 5890 | mbedtls_free( signature ); |
| 5891 | PSA_DONE( ); |
| 5892 | |
| 5893 | } |
| 5894 | /* END_CASE */ |
| 5895 | |
| 5896 | /* BEGIN_CASE */ |
| 5897 | void sign_message_fail( int key_type_arg, |
| 5898 | data_t *key_data, |
| 5899 | int alg_arg, |
| 5900 | data_t *input_data, |
| 5901 | int signature_size_arg, |
| 5902 | int expected_status_arg ) |
| 5903 | { |
| 5904 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5905 | psa_key_type_t key_type = key_type_arg; |
| 5906 | psa_algorithm_t alg = alg_arg; |
| 5907 | size_t signature_size = signature_size_arg; |
| 5908 | psa_status_t actual_status; |
| 5909 | psa_status_t expected_status = expected_status_arg; |
| 5910 | unsigned char *signature = NULL; |
| 5911 | size_t signature_length = 0xdeadbeef; |
| 5912 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5913 | |
| 5914 | ASSERT_ALLOC( signature, signature_size ); |
| 5915 | |
| 5916 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5917 | |
| 5918 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 5919 | psa_set_key_algorithm( &attributes, alg ); |
| 5920 | psa_set_key_type( &attributes, key_type ); |
| 5921 | |
| 5922 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5923 | &key ) ); |
| 5924 | |
| 5925 | actual_status = psa_sign_message( key, alg, |
| 5926 | input_data->x, input_data->len, |
| 5927 | signature, signature_size, |
| 5928 | &signature_length ); |
| 5929 | TEST_EQUAL( actual_status, expected_status ); |
| 5930 | /* The value of *signature_length is unspecified on error, but |
| 5931 | * whatever it is, it should be less than signature_size, so that |
| 5932 | * if the caller tries to read *signature_length bytes without |
| 5933 | * checking the error code then they don't overflow a buffer. */ |
| 5934 | TEST_ASSERT( signature_length <= signature_size ); |
| 5935 | |
| 5936 | exit: |
| 5937 | psa_reset_key_attributes( &attributes ); |
| 5938 | psa_destroy_key( key ); |
| 5939 | mbedtls_free( signature ); |
| 5940 | PSA_DONE( ); |
| 5941 | } |
| 5942 | /* END_CASE */ |
| 5943 | |
| 5944 | /* BEGIN_CASE */ |
| 5945 | void sign_verify_message( int key_type_arg, |
| 5946 | data_t *key_data, |
| 5947 | int alg_arg, |
| 5948 | data_t *input_data ) |
| 5949 | { |
| 5950 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5951 | psa_key_type_t key_type = key_type_arg; |
| 5952 | psa_algorithm_t alg = alg_arg; |
| 5953 | size_t key_bits; |
| 5954 | unsigned char *signature = NULL; |
| 5955 | size_t signature_size; |
| 5956 | size_t signature_length = 0xdeadbeef; |
| 5957 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5958 | |
| 5959 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5960 | |
| 5961 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 5962 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 5963 | psa_set_key_algorithm( &attributes, alg ); |
| 5964 | psa_set_key_type( &attributes, key_type ); |
| 5965 | |
| 5966 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5967 | &key ) ); |
| 5968 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5969 | key_bits = psa_get_key_bits( &attributes ); |
| 5970 | |
| 5971 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 5972 | TEST_ASSERT( signature_size != 0 ); |
| 5973 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 5974 | ASSERT_ALLOC( signature, signature_size ); |
| 5975 | |
| 5976 | PSA_ASSERT( psa_sign_message( key, alg, |
| 5977 | input_data->x, input_data->len, |
| 5978 | signature, signature_size, |
| 5979 | &signature_length ) ); |
| 5980 | TEST_ASSERT( signature_length <= signature_size ); |
| 5981 | TEST_ASSERT( signature_length > 0 ); |
| 5982 | |
| 5983 | PSA_ASSERT( psa_verify_message( key, alg, |
| 5984 | input_data->x, input_data->len, |
| 5985 | signature, signature_length ) ); |
| 5986 | |
| 5987 | if( input_data->len != 0 ) |
| 5988 | { |
| 5989 | /* Flip a bit in the input and verify that the signature is now |
| 5990 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 5991 | * because ECDSA may ignore the last few bits of the input. */ |
| 5992 | input_data->x[0] ^= 1; |
| 5993 | TEST_EQUAL( psa_verify_message( key, alg, |
| 5994 | input_data->x, input_data->len, |
| 5995 | signature, signature_length ), |
| 5996 | PSA_ERROR_INVALID_SIGNATURE ); |
| 5997 | } |
| 5998 | |
| 5999 | exit: |
| 6000 | psa_reset_key_attributes( &attributes ); |
| 6001 | |
| 6002 | psa_destroy_key( key ); |
| 6003 | mbedtls_free( signature ); |
| 6004 | PSA_DONE( ); |
| 6005 | } |
| 6006 | /* END_CASE */ |
| 6007 | |
| 6008 | /* BEGIN_CASE */ |
| 6009 | void verify_message( int key_type_arg, |
| 6010 | data_t *key_data, |
| 6011 | int alg_arg, |
| 6012 | data_t *input_data, |
| 6013 | data_t *signature_data ) |
| 6014 | { |
| 6015 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6016 | psa_key_type_t key_type = key_type_arg; |
| 6017 | psa_algorithm_t alg = alg_arg; |
| 6018 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6019 | |
| 6020 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
| 6021 | |
| 6022 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6023 | |
| 6024 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6025 | psa_set_key_algorithm( &attributes, alg ); |
| 6026 | psa_set_key_type( &attributes, key_type ); |
| 6027 | |
| 6028 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6029 | &key ) ); |
| 6030 | |
| 6031 | PSA_ASSERT( psa_verify_message( key, alg, |
| 6032 | input_data->x, input_data->len, |
| 6033 | signature_data->x, signature_data->len ) ); |
| 6034 | |
| 6035 | exit: |
| 6036 | psa_reset_key_attributes( &attributes ); |
| 6037 | psa_destroy_key( key ); |
| 6038 | PSA_DONE( ); |
| 6039 | } |
| 6040 | /* END_CASE */ |
| 6041 | |
| 6042 | /* BEGIN_CASE */ |
| 6043 | void verify_message_fail( int key_type_arg, |
| 6044 | data_t *key_data, |
| 6045 | int alg_arg, |
| 6046 | data_t *hash_data, |
| 6047 | data_t *signature_data, |
| 6048 | int expected_status_arg ) |
| 6049 | { |
| 6050 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6051 | psa_key_type_t key_type = key_type_arg; |
| 6052 | psa_algorithm_t alg = alg_arg; |
| 6053 | psa_status_t actual_status; |
| 6054 | psa_status_t expected_status = expected_status_arg; |
| 6055 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6056 | |
| 6057 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6058 | |
| 6059 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 6060 | psa_set_key_algorithm( &attributes, alg ); |
| 6061 | psa_set_key_type( &attributes, key_type ); |
| 6062 | |
| 6063 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 6064 | &key ) ); |
| 6065 | |
| 6066 | actual_status = psa_verify_message( key, alg, |
| 6067 | hash_data->x, hash_data->len, |
| 6068 | signature_data->x, |
| 6069 | signature_data->len ); |
| 6070 | TEST_EQUAL( actual_status, expected_status ); |
| 6071 | |
| 6072 | exit: |
| 6073 | psa_reset_key_attributes( &attributes ); |
| 6074 | psa_destroy_key( key ); |
| 6075 | PSA_DONE( ); |
| 6076 | } |
| 6077 | /* END_CASE */ |
| 6078 | |
| 6079 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6080 | void asymmetric_encrypt( int key_type_arg, |
| 6081 | data_t *key_data, |
| 6082 | int alg_arg, |
| 6083 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6084 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6085 | int expected_output_length_arg, |
| 6086 | int expected_status_arg ) |
| 6087 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6088 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6089 | psa_key_type_t key_type = key_type_arg; |
| 6090 | psa_algorithm_t alg = alg_arg; |
| 6091 | size_t expected_output_length = expected_output_length_arg; |
| 6092 | size_t key_bits; |
| 6093 | unsigned char *output = NULL; |
| 6094 | size_t output_size; |
| 6095 | size_t output_length = ~0; |
| 6096 | psa_status_t actual_status; |
| 6097 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6098 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6099 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6100 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 6101 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6102 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6103 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 6104 | psa_set_key_algorithm( &attributes, alg ); |
| 6105 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6106 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6107 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6108 | |
| 6109 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6110 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6111 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6112 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6113 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6114 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6115 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6116 | |
| 6117 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6118 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6119 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6120 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6121 | output, output_size, |
| 6122 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6123 | TEST_EQUAL( actual_status, expected_status ); |
| 6124 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6125 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6126 | /* If the label is empty, the test framework puts a non-null pointer |
| 6127 | * in label->x. Test that a null pointer works as well. */ |
| 6128 | if( label->len == 0 ) |
| 6129 | { |
| 6130 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 6131 | if( output_size != 0 ) |
| 6132 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6133 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6134 | input_data->x, input_data->len, |
| 6135 | NULL, label->len, |
| 6136 | output, output_size, |
| 6137 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6138 | TEST_EQUAL( actual_status, expected_status ); |
| 6139 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6140 | } |
| 6141 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6142 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6143 | /* |
| 6144 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6145 | * thus reset them as required. |
| 6146 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6147 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6148 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6149 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6150 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6151 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 6152 | } |
| 6153 | /* END_CASE */ |
| 6154 | |
| 6155 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6156 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 6157 | data_t *key_data, |
| 6158 | int alg_arg, |
| 6159 | data_t *input_data, |
| 6160 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6161 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6162 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6163 | psa_key_type_t key_type = key_type_arg; |
| 6164 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6165 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6166 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6167 | size_t output_size; |
| 6168 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 6169 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6170 | size_t output2_size; |
| 6171 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6172 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6173 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6174 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6175 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6176 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 6177 | psa_set_key_algorithm( &attributes, alg ); |
| 6178 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6179 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6180 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6181 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6182 | |
| 6183 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6184 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6185 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6186 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6187 | output_size = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6188 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6189 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6190 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6191 | output2_size = input_data->len; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6192 | TEST_ASSERT( output2_size <= |
| 6193 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 6194 | TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6195 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6196 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 6197 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 6198 | * the original plaintext because of the non-optional random |
| 6199 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6200 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6201 | input_data->x, input_data->len, |
| 6202 | label->x, label->len, |
| 6203 | output, output_size, |
| 6204 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6205 | /* We don't know what ciphertext length to expect, but check that |
| 6206 | * it looks sensible. */ |
| 6207 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 6208 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6209 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6210 | output, output_length, |
| 6211 | label->x, label->len, |
| 6212 | output2, output2_size, |
| 6213 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6214 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 6215 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6216 | |
| 6217 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6218 | /* |
| 6219 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6220 | * thus reset them as required. |
| 6221 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6222 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6223 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6224 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 6225 | mbedtls_free( output ); |
| 6226 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6227 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6228 | } |
| 6229 | /* END_CASE */ |
| 6230 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6231 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6232 | void asymmetric_decrypt( int key_type_arg, |
| 6233 | data_t *key_data, |
| 6234 | int alg_arg, |
| 6235 | data_t *input_data, |
| 6236 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 6237 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6238 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6239 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6240 | psa_key_type_t key_type = key_type_arg; |
| 6241 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6242 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6243 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 6244 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6245 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6246 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6247 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6248 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6249 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6250 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 6251 | psa_set_key_algorithm( &attributes, alg ); |
| 6252 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6253 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6254 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6255 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6256 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6257 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 6258 | key_bits = psa_get_key_bits( &attributes ); |
| 6259 | |
| 6260 | /* Determine the maximum ciphertext length */ |
| 6261 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 6262 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
| 6263 | ASSERT_ALLOC( output, output_size ); |
| 6264 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6265 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6266 | input_data->x, input_data->len, |
| 6267 | label->x, label->len, |
| 6268 | output, |
| 6269 | output_size, |
| 6270 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6271 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 6272 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6273 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6274 | /* If the label is empty, the test framework puts a non-null pointer |
| 6275 | * in label->x. Test that a null pointer works as well. */ |
| 6276 | if( label->len == 0 ) |
| 6277 | { |
| 6278 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 6279 | if( output_size != 0 ) |
| 6280 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6281 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6282 | input_data->x, input_data->len, |
| 6283 | NULL, label->len, |
| 6284 | output, |
| 6285 | output_size, |
| 6286 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 6287 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 6288 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6289 | } |
| 6290 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6291 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6292 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6293 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 6294 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6295 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6296 | } |
| 6297 | /* END_CASE */ |
| 6298 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6299 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6300 | void asymmetric_decrypt_fail( int key_type_arg, |
| 6301 | data_t *key_data, |
| 6302 | int alg_arg, |
| 6303 | data_t *input_data, |
| 6304 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 6305 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 6306 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6307 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6308 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6309 | psa_key_type_t key_type = key_type_arg; |
| 6310 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6311 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 6312 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6313 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6314 | psa_status_t actual_status; |
| 6315 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6316 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6317 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6318 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 6319 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6320 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6321 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6322 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 6323 | psa_set_key_algorithm( &attributes, alg ); |
| 6324 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 6325 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6326 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6327 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6328 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6329 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 6330 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6331 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 6332 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 6333 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6334 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6335 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6336 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6337 | /* If the label is empty, the test framework puts a non-null pointer |
| 6338 | * in label->x. Test that a null pointer works as well. */ |
| 6339 | if( label->len == 0 ) |
| 6340 | { |
| 6341 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 6342 | if( output_size != 0 ) |
| 6343 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6344 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6345 | input_data->x, input_data->len, |
| 6346 | NULL, label->len, |
| 6347 | output, output_size, |
| 6348 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6349 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6350 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6351 | } |
| 6352 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6353 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6354 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6355 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 6356 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6357 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6358 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 6359 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6360 | |
| 6361 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 6362 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 6363 | { |
| 6364 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 6365 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 6366 | * though it's OK by the C standard. We could test for this, but we'd need |
| 6367 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 6368 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6369 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 6370 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6371 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 6372 | |
| 6373 | memset( &zero, 0, sizeof( zero ) ); |
| 6374 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6375 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6376 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 6377 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6378 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 6379 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6380 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 6381 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 6382 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6383 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6384 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 6385 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 6386 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 6387 | } |
| 6388 | /* END_CASE */ |
| 6389 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 6390 | /* BEGIN_CASE */ |
| 6391 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6392 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6393 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6394 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6395 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6396 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6397 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6398 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 6399 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 6400 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6401 | |
| 6402 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6403 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6404 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6405 | } |
| 6406 | /* END_CASE */ |
| 6407 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6408 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 6409 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 6410 | int expected_status_arg ) |
| 6411 | { |
| 6412 | psa_algorithm_t alg = alg_arg; |
| 6413 | size_t capacity = capacity_arg; |
| 6414 | psa_status_t expected_status = expected_status_arg; |
| 6415 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6416 | |
| 6417 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6418 | |
| 6419 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 6420 | |
| 6421 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 6422 | expected_status ); |
| 6423 | |
| 6424 | exit: |
| 6425 | psa_key_derivation_abort( &operation ); |
| 6426 | PSA_DONE( ); |
| 6427 | } |
| 6428 | /* END_CASE */ |
| 6429 | |
| 6430 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6431 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 6432 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6433 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 6434 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6435 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 6436 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 6437 | int expected_status_arg3, |
| 6438 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6439 | { |
| 6440 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 6441 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 6442 | 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] | 6443 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 6444 | expected_status_arg2, |
| 6445 | expected_status_arg3}; |
| 6446 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6447 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 6448 | MBEDTLS_SVC_KEY_ID_INIT, |
| 6449 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6450 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6451 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6452 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 6453 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6454 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 6455 | psa_status_t expected_output_status = expected_output_status_arg; |
| 6456 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6457 | |
| 6458 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6459 | |
| 6460 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 6461 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6462 | |
| 6463 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 6464 | |
| 6465 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 6466 | { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 6467 | mbedtls_test_set_step( i ); |
| 6468 | if( steps[i] == 0 ) |
| 6469 | { |
| 6470 | /* Skip this step */ |
| 6471 | } |
| 6472 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6473 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 6474 | psa_set_key_type( &attributes, key_types[i] ); |
| 6475 | PSA_ASSERT( psa_import_key( &attributes, |
| 6476 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6477 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 6478 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 6479 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 6480 | { |
| 6481 | // When taking a private key as secret input, use key agreement |
| 6482 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6483 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 6484 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 6485 | expected_statuses[i] ); |
| 6486 | } |
| 6487 | else |
| 6488 | { |
| 6489 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6490 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 6491 | expected_statuses[i] ); |
| 6492 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 6493 | } |
| 6494 | else |
| 6495 | { |
| 6496 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 6497 | &operation, steps[i], |
| 6498 | inputs[i]->x, inputs[i]->len ), |
| 6499 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6500 | } |
| 6501 | } |
| 6502 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 6503 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 6504 | { |
| 6505 | psa_reset_key_attributes( &attributes ); |
Dave Rodgman | 491d849 | 2021-11-16 12:12:49 +0000 | [diff] [blame] | 6506 | psa_set_key_type( &attributes, output_key_type ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 6507 | psa_set_key_bits( &attributes, 8 ); |
| 6508 | actual_output_status = |
| 6509 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6510 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 6511 | } |
| 6512 | else |
| 6513 | { |
| 6514 | uint8_t buffer[1]; |
| 6515 | actual_output_status = |
| 6516 | psa_key_derivation_output_bytes( &operation, |
| 6517 | buffer, sizeof( buffer ) ); |
| 6518 | } |
| 6519 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 6520 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6521 | exit: |
| 6522 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6523 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 6524 | psa_destroy_key( keys[i] ); |
| 6525 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6526 | PSA_DONE( ); |
| 6527 | } |
| 6528 | /* END_CASE */ |
| 6529 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 6530 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 6531 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 6532 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 6533 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6534 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 6535 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6536 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 6537 | unsigned char input1[] = "Input 1"; |
| 6538 | size_t input1_length = sizeof( input1 ); |
| 6539 | unsigned char input2[] = "Input 2"; |
| 6540 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6541 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 6542 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 6543 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 6544 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 6545 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6546 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 6547 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6548 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6549 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6550 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 6551 | psa_set_key_algorithm( &attributes, alg ); |
| 6552 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6553 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 6554 | PSA_ASSERT( psa_import_key( &attributes, |
| 6555 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6556 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6557 | |
| 6558 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6559 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 6560 | input1, input1_length, |
| 6561 | input2, input2_length, |
| 6562 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 6563 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6564 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6565 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 6566 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 6567 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6568 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6569 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6570 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6571 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 6572 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6573 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6574 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6575 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6576 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6577 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6578 | } |
| 6579 | /* END_CASE */ |
| 6580 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6581 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 6582 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6583 | { |
| 6584 | uint8_t output_buffer[16]; |
| 6585 | size_t buffer_size = 16; |
| 6586 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6587 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6588 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6589 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 6590 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 6591 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6592 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6593 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 6594 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6595 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6596 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6597 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6598 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 6599 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 6600 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6601 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6602 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 6603 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6604 | |
| 6605 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6606 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 6607 | } |
| 6608 | /* END_CASE */ |
| 6609 | |
| 6610 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6611 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 6612 | int step1_arg, data_t *input1, |
| 6613 | int step2_arg, data_t *input2, |
| 6614 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6615 | int requested_capacity_arg, |
| 6616 | data_t *expected_output1, |
| 6617 | data_t *expected_output2 ) |
| 6618 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6619 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 6620 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 6621 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6622 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 6623 | MBEDTLS_SVC_KEY_ID_INIT, |
| 6624 | MBEDTLS_SVC_KEY_ID_INIT }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6625 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6626 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6627 | uint8_t *expected_outputs[2] = |
| 6628 | {expected_output1->x, expected_output2->x}; |
| 6629 | size_t output_sizes[2] = |
| 6630 | {expected_output1->len, expected_output2->len}; |
| 6631 | size_t output_buffer_size = 0; |
| 6632 | uint8_t *output_buffer = NULL; |
| 6633 | size_t expected_capacity; |
| 6634 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6635 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6636 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 6637 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6638 | |
| 6639 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 6640 | { |
| 6641 | if( output_sizes[i] > output_buffer_size ) |
| 6642 | output_buffer_size = output_sizes[i]; |
| 6643 | if( output_sizes[i] == 0 ) |
| 6644 | expected_outputs[i] = NULL; |
| 6645 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6646 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6647 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6648 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6649 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 6650 | psa_set_key_algorithm( &attributes, alg ); |
| 6651 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6652 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6653 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 6654 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 6655 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 6656 | requested_capacity ) ); |
| 6657 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6658 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 6659 | switch( steps[i] ) |
| 6660 | { |
| 6661 | case 0: |
| 6662 | break; |
| 6663 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 6664 | PSA_ASSERT( psa_import_key( &attributes, |
| 6665 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6666 | &keys[i] ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6667 | |
| 6668 | if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 6669 | { |
| 6670 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) ); |
| 6671 | TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <= |
| 6672 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
| 6673 | } |
| 6674 | |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 6675 | PSA_ASSERT( psa_key_derivation_input_key( |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6676 | &operation, steps[i], keys[i] ) ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 6677 | break; |
| 6678 | default: |
| 6679 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 6680 | &operation, steps[i], |
| 6681 | inputs[i]->x, inputs[i]->len ) ); |
| 6682 | break; |
| 6683 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6684 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 6685 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6686 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6687 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6688 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6689 | expected_capacity = requested_capacity; |
| 6690 | |
| 6691 | /* Expansion phase. */ |
| 6692 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 6693 | { |
| 6694 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6695 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6696 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6697 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 6698 | { |
| 6699 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 6700 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 6701 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6702 | continue; |
| 6703 | } |
| 6704 | else if( expected_capacity == 0 || |
| 6705 | output_sizes[i] > expected_capacity ) |
| 6706 | { |
| 6707 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 6708 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6709 | expected_capacity = 0; |
| 6710 | continue; |
| 6711 | } |
| 6712 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6713 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6714 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 6715 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 6716 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6717 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6718 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6719 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6720 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6721 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6722 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6723 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6724 | |
| 6725 | exit: |
| 6726 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6727 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6728 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 6729 | psa_destroy_key( keys[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6730 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6731 | } |
| 6732 | /* END_CASE */ |
| 6733 | |
| 6734 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6735 | void derive_full( int alg_arg, |
| 6736 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 6737 | data_t *input1, |
| 6738 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6739 | int requested_capacity_arg ) |
| 6740 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6741 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6742 | psa_algorithm_t alg = alg_arg; |
| 6743 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6744 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6745 | unsigned char output_buffer[16]; |
| 6746 | size_t expected_capacity = requested_capacity; |
| 6747 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6748 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6749 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6750 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6751 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6752 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 6753 | psa_set_key_algorithm( &attributes, alg ); |
| 6754 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6755 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6756 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6757 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6758 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6759 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 6760 | input1->x, input1->len, |
| 6761 | input2->x, input2->len, |
| 6762 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 6763 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 6764 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6765 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6766 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6767 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6768 | |
| 6769 | /* Expansion phase. */ |
| 6770 | while( current_capacity > 0 ) |
| 6771 | { |
| 6772 | size_t read_size = sizeof( output_buffer ); |
| 6773 | if( read_size > current_capacity ) |
| 6774 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6775 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6776 | output_buffer, |
| 6777 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6778 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6779 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6780 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6781 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6782 | } |
| 6783 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6784 | /* Check that the operation refuses to go over capacity. */ |
| 6785 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 6786 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6787 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6788 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6789 | |
| 6790 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6791 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6792 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6793 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6794 | } |
| 6795 | /* END_CASE */ |
| 6796 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 6797 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6798 | void derive_key_exercise( int alg_arg, |
| 6799 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 6800 | data_t *input1, |
| 6801 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6802 | int derived_type_arg, |
| 6803 | int derived_bits_arg, |
| 6804 | int derived_usage_arg, |
| 6805 | int derived_alg_arg ) |
| 6806 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6807 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6808 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6809 | psa_algorithm_t alg = alg_arg; |
| 6810 | psa_key_type_t derived_type = derived_type_arg; |
| 6811 | size_t derived_bits = derived_bits_arg; |
| 6812 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 6813 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 6814 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6815 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6816 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6817 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6818 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6819 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6820 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6821 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 6822 | psa_set_key_algorithm( &attributes, alg ); |
| 6823 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6824 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6825 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6826 | |
| 6827 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6828 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 6829 | input1->x, input1->len, |
| 6830 | input2->x, input2->len, |
| 6831 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 6832 | goto exit; |
| 6833 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6834 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 6835 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 6836 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 6837 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6838 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6839 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6840 | |
| 6841 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6842 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6843 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 6844 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6845 | |
| 6846 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6847 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6848 | goto exit; |
| 6849 | |
| 6850 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6851 | /* |
| 6852 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6853 | * thus reset them as required. |
| 6854 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6855 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6856 | |
| 6857 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6858 | psa_destroy_key( base_key ); |
| 6859 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6860 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6861 | } |
| 6862 | /* END_CASE */ |
| 6863 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 6864 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6865 | void derive_key_export( int alg_arg, |
| 6866 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 6867 | data_t *input1, |
| 6868 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6869 | int bytes1_arg, |
| 6870 | int bytes2_arg ) |
| 6871 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6872 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6873 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6874 | psa_algorithm_t alg = alg_arg; |
| 6875 | size_t bytes1 = bytes1_arg; |
| 6876 | size_t bytes2 = bytes2_arg; |
| 6877 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6878 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6879 | uint8_t *output_buffer = NULL; |
| 6880 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6881 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6882 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6883 | size_t length; |
| 6884 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6885 | ASSERT_ALLOC( output_buffer, capacity ); |
| 6886 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6887 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6888 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6889 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 6890 | psa_set_key_algorithm( &base_attributes, alg ); |
| 6891 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6892 | 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] | 6893 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6894 | |
| 6895 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6896 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 6897 | input1->x, input1->len, |
| 6898 | input2->x, input2->len, |
| 6899 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 6900 | goto exit; |
| 6901 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6902 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6903 | output_buffer, |
| 6904 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6905 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6906 | |
| 6907 | /* 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] | 6908 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 6909 | input1->x, input1->len, |
| 6910 | input2->x, input2->len, |
| 6911 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 6912 | goto exit; |
| 6913 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6914 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 6915 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 6916 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 6917 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6918 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6919 | &derived_key ) ); |
| 6920 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6921 | export_buffer, bytes1, |
| 6922 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6923 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6924 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 6925 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6926 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6927 | &derived_key ) ); |
| 6928 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6929 | export_buffer + bytes1, bytes2, |
| 6930 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6931 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6932 | |
| 6933 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 6934 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 6935 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6936 | |
| 6937 | exit: |
| 6938 | mbedtls_free( output_buffer ); |
| 6939 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6940 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6941 | psa_destroy_key( base_key ); |
| 6942 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6943 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6944 | } |
| 6945 | /* END_CASE */ |
| 6946 | |
| 6947 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 6948 | void derive_key( int alg_arg, |
| 6949 | data_t *key_data, data_t *input1, data_t *input2, |
| 6950 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 6951 | int expected_status_arg, |
| 6952 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6953 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6954 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6955 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6956 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 6957 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6958 | size_t bits = bits_arg; |
| 6959 | psa_status_t expected_status = expected_status_arg; |
| 6960 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6961 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6962 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6963 | |
| 6964 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6965 | |
| 6966 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 6967 | psa_set_key_algorithm( &base_attributes, alg ); |
| 6968 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 6969 | 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] | 6970 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6971 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6972 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 6973 | input1->x, input1->len, |
| 6974 | input2->x, input2->len, |
| 6975 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6976 | goto exit; |
| 6977 | |
| 6978 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 6979 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 6980 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6981 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 6982 | |
| 6983 | psa_status_t status = |
| 6984 | psa_key_derivation_output_key( &derived_attributes, |
| 6985 | &operation, |
| 6986 | &derived_key ); |
| 6987 | if( is_large_output > 0 ) |
| 6988 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 6989 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6990 | |
| 6991 | exit: |
| 6992 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6993 | psa_destroy_key( base_key ); |
| 6994 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6995 | PSA_DONE( ); |
| 6996 | } |
| 6997 | /* END_CASE */ |
| 6998 | |
| 6999 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7000 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 7001 | int our_key_type_arg, int our_key_alg_arg, |
| 7002 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7003 | int expected_status_arg ) |
| 7004 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7005 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7006 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 7007 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7008 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7009 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7010 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 7011 | psa_status_t expected_status = expected_status_arg; |
| 7012 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7013 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7014 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7015 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7016 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 7017 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7018 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7019 | PSA_ASSERT( psa_import_key( &attributes, |
| 7020 | our_key_data->x, our_key_data->len, |
| 7021 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7022 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 7023 | /* The tests currently include inputs that should fail at either step. |
| 7024 | * Test cases that fail at the setup step should be changed to call |
| 7025 | * key_derivation_setup instead, and this function should be renamed |
| 7026 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7027 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 7028 | if( status == PSA_SUCCESS ) |
| 7029 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7030 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 7031 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 7032 | our_key, |
| 7033 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 7034 | expected_status ); |
| 7035 | } |
| 7036 | else |
| 7037 | { |
| 7038 | TEST_ASSERT( status == expected_status ); |
| 7039 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7040 | |
| 7041 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7042 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7043 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7044 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 7045 | } |
| 7046 | /* END_CASE */ |
| 7047 | |
| 7048 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7049 | void raw_key_agreement( int alg_arg, |
| 7050 | int our_key_type_arg, data_t *our_key_data, |
| 7051 | data_t *peer_key_data, |
| 7052 | data_t *expected_output ) |
| 7053 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7054 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7055 | psa_algorithm_t alg = alg_arg; |
| 7056 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7057 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7058 | unsigned char *output = NULL; |
| 7059 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7060 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7061 | |
| 7062 | ASSERT_ALLOC( output, expected_output->len ); |
| 7063 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7064 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7065 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7066 | psa_set_key_algorithm( &attributes, alg ); |
| 7067 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7068 | PSA_ASSERT( psa_import_key( &attributes, |
| 7069 | our_key_data->x, our_key_data->len, |
| 7070 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7071 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7072 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 7073 | key_bits = psa_get_key_bits( &attributes ); |
| 7074 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 7075 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 7076 | peer_key_data->x, peer_key_data->len, |
| 7077 | output, expected_output->len, |
| 7078 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7079 | ASSERT_COMPARE( output, output_length, |
| 7080 | expected_output->x, expected_output->len ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 7081 | TEST_ASSERT( output_length <= |
| 7082 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 7083 | TEST_ASSERT( output_length <= |
| 7084 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7085 | |
| 7086 | exit: |
| 7087 | mbedtls_free( output ); |
| 7088 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7089 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 7090 | } |
| 7091 | /* END_CASE */ |
| 7092 | |
| 7093 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7094 | void key_agreement_capacity( int alg_arg, |
| 7095 | int our_key_type_arg, data_t *our_key_data, |
| 7096 | data_t *peer_key_data, |
| 7097 | int expected_capacity_arg ) |
| 7098 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7099 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7100 | psa_algorithm_t alg = alg_arg; |
| 7101 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7102 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7103 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7104 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 7105 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7106 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7107 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7108 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7109 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7110 | psa_set_key_algorithm( &attributes, alg ); |
| 7111 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7112 | PSA_ASSERT( psa_import_key( &attributes, |
| 7113 | our_key_data->x, our_key_data->len, |
| 7114 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7115 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7116 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7117 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 7118 | &operation, |
| 7119 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 7120 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 7121 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 7122 | { |
| 7123 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7124 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 7125 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 7126 | NULL, 0 ) ); |
| 7127 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7128 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 7129 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 7130 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7131 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 7132 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7133 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 7134 | /* Test the actual capacity by reading the output. */ |
| 7135 | while( actual_capacity > sizeof( output ) ) |
| 7136 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7137 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7138 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 7139 | actual_capacity -= sizeof( output ); |
| 7140 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7141 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7142 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7143 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 7144 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 7145 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7146 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7147 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7148 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7149 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7150 | } |
| 7151 | /* END_CASE */ |
| 7152 | |
| 7153 | /* BEGIN_CASE */ |
| 7154 | void key_agreement_output( int alg_arg, |
| 7155 | int our_key_type_arg, data_t *our_key_data, |
| 7156 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 7157 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7158 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7159 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7160 | psa_algorithm_t alg = alg_arg; |
| 7161 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7162 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7163 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 7164 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7165 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 7166 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 7167 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7168 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7169 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7170 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7171 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 7172 | psa_set_key_algorithm( &attributes, alg ); |
| 7173 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7174 | PSA_ASSERT( psa_import_key( &attributes, |
| 7175 | our_key_data->x, our_key_data->len, |
| 7176 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7177 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7178 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7179 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 7180 | &operation, |
| 7181 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 7182 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 7183 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 7184 | { |
| 7185 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7186 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 7187 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 7188 | NULL, 0 ) ); |
| 7189 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7190 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7191 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7192 | actual_output, |
| 7193 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 7194 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 7195 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 7196 | if( expected_output2->len != 0 ) |
| 7197 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7198 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7199 | actual_output, |
| 7200 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 7201 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 7202 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 7203 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7204 | |
| 7205 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7206 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7207 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7208 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 7209 | mbedtls_free( actual_output ); |
| 7210 | } |
| 7211 | /* END_CASE */ |
| 7212 | |
| 7213 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 7214 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7215 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 7216 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7217 | unsigned char *output = NULL; |
| 7218 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 7219 | size_t i; |
| 7220 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7221 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 7222 | TEST_ASSERT( bytes_arg >= 0 ); |
| 7223 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 7224 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 7225 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7226 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7227 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7228 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 7229 | /* Run several times, to ensure that every output byte will be |
| 7230 | * nonzero at least once with overwhelming probability |
| 7231 | * (2^(-8*number_of_runs)). */ |
| 7232 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7233 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 7234 | if( bytes != 0 ) |
| 7235 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7236 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 7237 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 7238 | for( i = 0; i < bytes; i++ ) |
| 7239 | { |
| 7240 | if( output[i] != 0 ) |
| 7241 | ++changed[i]; |
| 7242 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7243 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 7244 | |
| 7245 | /* Check that every byte was changed to nonzero at least once. This |
| 7246 | * validates that psa_generate_random is overwriting every byte of |
| 7247 | * the output buffer. */ |
| 7248 | for( i = 0; i < bytes; i++ ) |
| 7249 | { |
| 7250 | TEST_ASSERT( changed[i] != 0 ); |
| 7251 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7252 | |
| 7253 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7254 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 7255 | mbedtls_free( output ); |
| 7256 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 7257 | } |
| 7258 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7259 | |
| 7260 | /* BEGIN_CASE */ |
| 7261 | void generate_key( int type_arg, |
| 7262 | int bits_arg, |
| 7263 | int usage_arg, |
| 7264 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 7265 | int expected_status_arg, |
| 7266 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7267 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7268 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7269 | psa_key_type_t type = type_arg; |
| 7270 | psa_key_usage_t usage = usage_arg; |
| 7271 | size_t bits = bits_arg; |
| 7272 | psa_algorithm_t alg = alg_arg; |
| 7273 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7274 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7275 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7276 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7277 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7278 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7279 | psa_set_key_usage_flags( &attributes, usage ); |
| 7280 | psa_set_key_algorithm( &attributes, alg ); |
| 7281 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7282 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7283 | |
| 7284 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 7285 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 7286 | |
| 7287 | if( is_large_key > 0 ) |
| 7288 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 7289 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7290 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 7291 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7292 | |
| 7293 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7294 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 7295 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 7296 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7297 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 7298 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7299 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 7300 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7301 | |
| 7302 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7303 | /* |
| 7304 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7305 | * thus reset them as required. |
| 7306 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7307 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7308 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7309 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7310 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 7311 | } |
| 7312 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 7313 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 7314 | /* 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] | 7315 | void generate_key_rsa( int bits_arg, |
| 7316 | data_t *e_arg, |
| 7317 | int expected_status_arg ) |
| 7318 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7319 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 7320 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7321 | size_t bits = bits_arg; |
| 7322 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 7323 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 7324 | psa_status_t expected_status = expected_status_arg; |
| 7325 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7326 | uint8_t *exported = NULL; |
| 7327 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 7328 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7329 | size_t exported_length = SIZE_MAX; |
| 7330 | uint8_t *e_read_buffer = NULL; |
| 7331 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 7332 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7333 | size_t e_read_length = SIZE_MAX; |
| 7334 | |
| 7335 | if( e_arg->len == 0 || |
| 7336 | ( e_arg->len == 3 && |
| 7337 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 7338 | { |
| 7339 | is_default_public_exponent = 1; |
| 7340 | e_read_size = 0; |
| 7341 | } |
| 7342 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 7343 | ASSERT_ALLOC( exported, exported_size ); |
| 7344 | |
| 7345 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7346 | |
| 7347 | psa_set_key_usage_flags( &attributes, usage ); |
| 7348 | psa_set_key_algorithm( &attributes, alg ); |
| 7349 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 7350 | e_arg->x, e_arg->len ) ); |
| 7351 | psa_set_key_bits( &attributes, bits ); |
| 7352 | |
| 7353 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7354 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7355 | if( expected_status != PSA_SUCCESS ) |
| 7356 | goto exit; |
| 7357 | |
| 7358 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7359 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7360 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 7361 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 7362 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 7363 | e_read_buffer, e_read_size, |
| 7364 | &e_read_length ) ); |
| 7365 | if( is_default_public_exponent ) |
| 7366 | TEST_EQUAL( e_read_length, 0 ); |
| 7367 | else |
| 7368 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 7369 | |
| 7370 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7371 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7372 | goto exit; |
| 7373 | |
| 7374 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7375 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7376 | exported, exported_size, |
| 7377 | &exported_length ) ); |
| 7378 | { |
| 7379 | uint8_t *p = exported; |
| 7380 | uint8_t *end = exported + exported_length; |
| 7381 | size_t len; |
| 7382 | /* RSAPublicKey ::= SEQUENCE { |
| 7383 | * modulus INTEGER, -- n |
| 7384 | * publicExponent INTEGER } -- e |
| 7385 | */ |
| 7386 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7387 | MBEDTLS_ASN1_SEQUENCE | |
| 7388 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 7389 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7390 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 7391 | MBEDTLS_ASN1_INTEGER ) ); |
| 7392 | if( len >= 1 && p[0] == 0 ) |
| 7393 | { |
| 7394 | ++p; |
| 7395 | --len; |
| 7396 | } |
| 7397 | if( e_arg->len == 0 ) |
| 7398 | { |
| 7399 | TEST_EQUAL( len, 3 ); |
| 7400 | TEST_EQUAL( p[0], 1 ); |
| 7401 | TEST_EQUAL( p[1], 0 ); |
| 7402 | TEST_EQUAL( p[2], 1 ); |
| 7403 | } |
| 7404 | else |
| 7405 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 7406 | } |
| 7407 | |
| 7408 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7409 | /* |
| 7410 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 7411 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 7412 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7413 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7414 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7415 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7416 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7417 | mbedtls_free( e_read_buffer ); |
| 7418 | mbedtls_free( exported ); |
| 7419 | } |
| 7420 | /* END_CASE */ |
| 7421 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7422 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7423 | void persistent_key_load_key_from_storage( data_t *data, |
| 7424 | int type_arg, int bits_arg, |
| 7425 | int usage_flags_arg, int alg_arg, |
| 7426 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7427 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 7428 | 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] | 7429 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7430 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7431 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7432 | psa_key_type_t type = type_arg; |
| 7433 | size_t bits = bits_arg; |
| 7434 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 7435 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7436 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7437 | unsigned char *first_export = NULL; |
| 7438 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 7439 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7440 | size_t first_exported_length; |
| 7441 | size_t second_exported_length; |
| 7442 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7443 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 7444 | { |
| 7445 | ASSERT_ALLOC( first_export, export_size ); |
| 7446 | ASSERT_ALLOC( second_export, export_size ); |
| 7447 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7448 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7449 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7450 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 7451 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7452 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 7453 | psa_set_key_algorithm( &attributes, alg ); |
| 7454 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7455 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7456 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7457 | switch( generation_method ) |
| 7458 | { |
| 7459 | case IMPORT_KEY: |
| 7460 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7461 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7462 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7463 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7464 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7465 | case GENERATE_KEY: |
| 7466 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7467 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7468 | break; |
| 7469 | |
| 7470 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 7471 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7472 | { |
| 7473 | /* Create base key */ |
| 7474 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 7475 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7476 | psa_set_key_usage_flags( &base_attributes, |
| 7477 | PSA_KEY_USAGE_DERIVE ); |
| 7478 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 7479 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7480 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 7481 | data->x, data->len, |
| 7482 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7483 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7484 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7485 | PSA_ASSERT( psa_key_derivation_input_key( |
| 7486 | &operation, |
| 7487 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7488 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7489 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7490 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7491 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 7492 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7493 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7494 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7495 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7496 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7497 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 7498 | #else |
| 7499 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 7500 | #endif |
| 7501 | break; |
| 7502 | |
| 7503 | default: |
| 7504 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 7505 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7506 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7507 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7508 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7509 | /* Export the key if permitted by the key policy. */ |
| 7510 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 7511 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7512 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7513 | first_export, export_size, |
| 7514 | &first_exported_length ) ); |
| 7515 | if( generation_method == IMPORT_KEY ) |
| 7516 | ASSERT_COMPARE( data->x, data->len, |
| 7517 | first_export, first_exported_length ); |
| 7518 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7519 | |
| 7520 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7521 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7522 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7523 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7524 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7525 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7526 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 7527 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 7528 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7529 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 7530 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 7531 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 7532 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 7533 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 7534 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7535 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7536 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7537 | /* Export the key again if permitted by the key policy. */ |
| 7538 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7539 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7540 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7541 | second_export, export_size, |
| 7542 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7543 | ASSERT_COMPARE( first_export, first_exported_length, |
| 7544 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7545 | } |
| 7546 | |
| 7547 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7548 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7549 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7550 | |
| 7551 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7552 | /* |
| 7553 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7554 | * thus reset them as required. |
| 7555 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7556 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7557 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7558 | mbedtls_free( first_export ); |
| 7559 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7560 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7561 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7562 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7563 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7564 | } |
| 7565 | /* END_CASE */ |