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 | } |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 57 | |
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 | } |
| 141 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 142 | int exercise_mac_setup( psa_key_type_t key_type, |
| 143 | const unsigned char *key_bytes, |
| 144 | size_t key_length, |
| 145 | psa_algorithm_t alg, |
| 146 | psa_mac_operation_t *operation, |
| 147 | psa_status_t *status ) |
| 148 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 149 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 150 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 151 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 152 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 153 | psa_set_key_algorithm( &attributes, alg ); |
| 154 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 155 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 156 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 157 | *status = psa_mac_sign_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 158 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 159 | PSA_ASSERT( psa_mac_abort( operation ) ); |
| 160 | /* If setup failed, reproduce the failure, so that the caller can |
| 161 | * test the resulting state of the operation object. */ |
| 162 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 163 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 164 | TEST_EQUAL( psa_mac_sign_setup( operation, key, alg ), *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 167 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 168 | return( 1 ); |
| 169 | |
| 170 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 171 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 172 | return( 0 ); |
| 173 | } |
| 174 | |
| 175 | int exercise_cipher_setup( psa_key_type_t key_type, |
| 176 | const unsigned char *key_bytes, |
| 177 | size_t key_length, |
| 178 | psa_algorithm_t alg, |
| 179 | psa_cipher_operation_t *operation, |
| 180 | psa_status_t *status ) |
| 181 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 182 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 183 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 184 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 185 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 186 | psa_set_key_algorithm( &attributes, alg ); |
| 187 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 188 | PSA_ASSERT( psa_import_key( &attributes, key_bytes, key_length, &key ) ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 189 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 190 | *status = psa_cipher_encrypt_setup( operation, key, alg ); |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 191 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 192 | PSA_ASSERT( psa_cipher_abort( operation ) ); |
| 193 | /* If setup failed, reproduce the failure, so that the caller can |
| 194 | * test the resulting state of the operation object. */ |
| 195 | if( *status != PSA_SUCCESS ) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 196 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 197 | TEST_EQUAL( psa_cipher_encrypt_setup( operation, key, alg ), |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 198 | *status ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 201 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 202 | return( 1 ); |
| 203 | |
| 204 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 205 | psa_destroy_key( key ); |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 206 | return( 0 ); |
| 207 | } |
| 208 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 209 | 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] | 210 | { |
| 211 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 212 | 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] | 213 | uint8_t buffer[1]; |
| 214 | size_t length; |
| 215 | int ok = 0; |
| 216 | |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 217 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 218 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 219 | psa_set_key_algorithm( &attributes, PSA_ALG_CTR ); |
| 220 | psa_set_key_type( &attributes, PSA_KEY_TYPE_AES ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 221 | TEST_EQUAL( psa_get_key_attributes( key, &attributes ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 222 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 223 | TEST_EQUAL( |
| 224 | MBEDTLS_SVC_KEY_ID_GET_KEY_ID( psa_get_key_id( &attributes ) ), 0 ); |
| 225 | TEST_EQUAL( |
| 226 | 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] | 227 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), 0 ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 228 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 ); |
| 229 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 ); |
| 230 | TEST_EQUAL( psa_get_key_type( &attributes ), 0 ); |
| 231 | TEST_EQUAL( psa_get_key_bits( &attributes ), 0 ); |
| 232 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 233 | TEST_EQUAL( psa_export_key( key, buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 234 | PSA_ERROR_INVALID_HANDLE ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 235 | TEST_EQUAL( psa_export_public_key( key, |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 236 | buffer, sizeof( buffer ), &length ), |
Maulik Patel | 3240c9d | 2021-03-17 16:11:05 +0000 | [diff] [blame] | 237 | PSA_ERROR_INVALID_HANDLE ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 238 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 239 | ok = 1; |
| 240 | |
| 241 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 242 | /* |
| 243 | * Key attributes may have been returned by psa_get_key_attributes() |
| 244 | * thus reset them as required. |
| 245 | */ |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 246 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 247 | |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 248 | return( ok ); |
| 249 | } |
| 250 | |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 251 | /* Assert that a key isn't reported as having a slot number. */ |
| 252 | #if defined(MBEDTLS_PSA_CRYPTO_SE_C) |
| 253 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 254 | do \ |
| 255 | { \ |
| 256 | psa_key_slot_number_t ASSERT_NO_SLOT_NUMBER_slot_number; \ |
| 257 | TEST_EQUAL( psa_get_key_slot_number( \ |
| 258 | attributes, \ |
| 259 | &ASSERT_NO_SLOT_NUMBER_slot_number ), \ |
| 260 | PSA_ERROR_INVALID_ARGUMENT ); \ |
| 261 | } \ |
| 262 | while( 0 ) |
| 263 | #else /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 264 | #define ASSERT_NO_SLOT_NUMBER( attributes ) \ |
| 265 | ( (void) 0 ) |
| 266 | #endif /* MBEDTLS_PSA_CRYPTO_SE_C */ |
| 267 | |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 268 | /* An overapproximation of the amount of storage needed for a key of the |
| 269 | * given type and with the given content. The API doesn't make it easy |
| 270 | * to find a good value for the size. The current implementation doesn't |
| 271 | * care about the value anyway. */ |
| 272 | #define KEY_BITS_FROM_DATA( type, data ) \ |
| 273 | ( data )->len |
| 274 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 275 | typedef enum { |
| 276 | IMPORT_KEY = 0, |
| 277 | GENERATE_KEY = 1, |
| 278 | DERIVE_KEY = 2 |
| 279 | } generate_method; |
| 280 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 281 | typedef enum |
| 282 | { |
| 283 | DO_NOT_SET_LENGTHS = 0, |
| 284 | SET_LENGTHS_BEFORE_NONCE = 1, |
| 285 | SET_LENGTHS_AFTER_NONCE = 2 |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 286 | } set_lengths_method_t; |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 287 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 288 | typedef enum |
| 289 | { |
| 290 | USE_NULL_TAG = 0, |
| 291 | USE_GIVEN_TAG = 1, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 292 | } tag_usage_method_t; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 293 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 294 | /*! |
| 295 | * \brief Internal Function for AEAD multipart tests. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 296 | * \param key_type_arg Type of key passed in |
| 297 | * \param key_data The encryption / decryption key data |
| 298 | * \param alg_arg The type of algorithm used |
| 299 | * \param nonce Nonce data |
| 300 | * \param additional_data Additional data |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 301 | * \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] | 302 | * feed additional data in to be encrypted / |
| 303 | * decrypted. If -1, no chunking. |
| 304 | * \param input_data Data to encrypt / decrypt |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 305 | * \param data_part_len_arg If not -1, the length of chunks to feed |
| 306 | * the data in to be encrypted / decrypted. If |
| 307 | * -1, no chunking |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 308 | * \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] | 309 | * expected here, this controls whether or not |
| 310 | * to set lengths, and in what order with |
| 311 | * respect to set nonce. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 312 | * \param expected_output Expected output |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 313 | * \param is_encrypt If non-zero this is an encryption operation. |
Paul Elliott | 41ffae1 | 2021-07-22 21:52:01 +0100 | [diff] [blame] | 314 | * \param do_zero_parts If non-zero, interleave zero length chunks |
Paul Elliott | 8eec8d4 | 2021-09-19 22:38:27 +0100 | [diff] [blame] | 315 | * with normal length chunks. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 316 | * \return int Zero on failure, non-zero on success. |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 317 | */ |
| 318 | static int aead_multipart_internal_func( int key_type_arg, data_t *key_data, |
| 319 | int alg_arg, |
| 320 | data_t *nonce, |
| 321 | data_t *additional_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 322 | int ad_part_len_arg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 323 | data_t *input_data, |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 324 | int data_part_len_arg, |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 325 | set_lengths_method_t set_lengths_method, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 326 | data_t *expected_output, |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 327 | int is_encrypt, |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 328 | int do_zero_parts ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 329 | { |
| 330 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 331 | psa_key_type_t key_type = key_type_arg; |
| 332 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 333 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 334 | unsigned char *output_data = NULL; |
| 335 | unsigned char *part_data = NULL; |
| 336 | unsigned char *final_data = NULL; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 337 | size_t data_true_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 338 | size_t part_data_size = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 339 | size_t output_size = 0; |
| 340 | size_t final_output_size = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 341 | size_t output_length = 0; |
| 342 | size_t key_bits = 0; |
| 343 | size_t tag_length = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 344 | size_t part_offset = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 345 | size_t part_length = 0; |
| 346 | size_t output_part_length = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 347 | size_t tag_size = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 348 | size_t ad_part_len = 0; |
| 349 | size_t data_part_len = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 350 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 351 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 352 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 353 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 354 | int test_ok = 0; |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 355 | size_t part_count = 0; |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 356 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 357 | PSA_ASSERT( psa_crypto_init( ) ); |
| 358 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 359 | if( is_encrypt ) |
| 360 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 361 | else |
| 362 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 363 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 364 | psa_set_key_algorithm( &attributes, alg ); |
| 365 | psa_set_key_type( &attributes, key_type ); |
| 366 | |
| 367 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 368 | &key ) ); |
| 369 | |
| 370 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 371 | key_bits = psa_get_key_bits( &attributes ); |
| 372 | |
| 373 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 374 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 375 | if( is_encrypt ) |
| 376 | { |
| 377 | /* Tag gets written at end of buffer. */ |
| 378 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 379 | ( input_data->len + |
| 380 | tag_length ) ); |
| 381 | data_true_size = input_data->len; |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 386 | ( input_data->len - |
| 387 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 388 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 389 | /* Do not want to attempt to decrypt tag. */ |
| 390 | data_true_size = input_data->len - tag_length; |
| 391 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 392 | |
| 393 | ASSERT_ALLOC( output_data, output_size ); |
| 394 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 395 | if( is_encrypt ) |
| 396 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 397 | final_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 398 | TEST_ASSERT( final_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 399 | } |
| 400 | else |
| 401 | { |
Paul Elliott | 5a9642f | 2021-09-13 19:13:22 +0100 | [diff] [blame] | 402 | final_output_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 403 | TEST_ASSERT( final_output_size <= PSA_AEAD_VERIFY_OUTPUT_MAX_SIZE ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 404 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 405 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 406 | ASSERT_ALLOC( final_data, final_output_size ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 407 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 408 | if( is_encrypt ) |
| 409 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 410 | else |
| 411 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 412 | |
| 413 | /* If the operation is not supported, just skip and not fail in case the |
| 414 | * encryption involves a common limitation of cryptography hardwares and |
| 415 | * an alternative implementation. */ |
| 416 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 417 | { |
| 418 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 419 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 420 | } |
| 421 | |
| 422 | PSA_ASSERT( status ); |
| 423 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 424 | if( set_lengths_method == DO_NOT_SET_LENGTHS ) |
| 425 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 426 | else if( set_lengths_method == SET_LENGTHS_BEFORE_NONCE ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 427 | { |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 428 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 429 | data_true_size ) ); |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 430 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 431 | } |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 432 | else if( set_lengths_method == SET_LENGTHS_AFTER_NONCE ) |
Paul Elliott | ebf9163 | 2021-07-22 17:54:42 +0100 | [diff] [blame] | 433 | { |
| 434 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 435 | |
Paul Elliott | 33746aa | 2021-09-15 16:40:40 +0100 | [diff] [blame] | 436 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 437 | data_true_size ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 438 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 439 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 440 | if( ad_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 441 | { |
| 442 | /* Pass additional data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 443 | ad_part_len = (size_t) ad_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 444 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 445 | for( part_offset = 0, part_count = 0; |
| 446 | part_offset < additional_data->len; |
| 447 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 448 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 449 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 450 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 451 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 452 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 453 | else if( additional_data->len - part_offset < ad_part_len ) |
| 454 | { |
| 455 | part_length = additional_data->len - part_offset; |
| 456 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 457 | else |
| 458 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 459 | part_length = ad_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | PSA_ASSERT( psa_aead_update_ad( &operation, |
| 463 | additional_data->x + part_offset, |
| 464 | part_length ) ); |
| 465 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | else |
| 469 | { |
| 470 | /* Pass additional data in one go. */ |
| 471 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 472 | additional_data->len ) ); |
| 473 | } |
| 474 | |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 475 | if( data_part_len_arg != -1 ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 476 | { |
| 477 | /* Pass data in parts */ |
Paul Elliott | 6bfd0fb | 2021-09-15 14:15:55 +0100 | [diff] [blame] | 478 | data_part_len = ( size_t ) data_part_len_arg; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 479 | part_data_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 480 | ( size_t ) data_part_len ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 481 | |
| 482 | ASSERT_ALLOC( part_data, part_data_size ); |
| 483 | |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 484 | for( part_offset = 0, part_count = 0; |
| 485 | part_offset < data_true_size; |
| 486 | part_offset += part_length, part_count++ ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 487 | { |
Paul Elliott | 4e4d71a | 2021-09-15 16:50:01 +0100 | [diff] [blame] | 488 | if( do_zero_parts && ( part_count & 0x01 ) ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 489 | { |
Paul Elliott | 329d538 | 2021-07-22 17:10:45 +0100 | [diff] [blame] | 490 | part_length = 0; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 491 | } |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 492 | else if( ( data_true_size - part_offset ) < data_part_len ) |
| 493 | { |
| 494 | part_length = ( data_true_size - part_offset ); |
| 495 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 496 | else |
| 497 | { |
Paul Elliott | e49fe45 | 2021-09-15 16:52:11 +0100 | [diff] [blame] | 498 | part_length = data_part_len; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | PSA_ASSERT( psa_aead_update( &operation, |
| 502 | ( input_data->x + part_offset ), |
| 503 | part_length, part_data, |
| 504 | part_data_size, |
| 505 | &output_part_length ) ); |
| 506 | |
| 507 | if( output_data && output_part_length ) |
| 508 | { |
| 509 | memcpy( ( output_data + part_offset ), part_data, |
| 510 | output_part_length ); |
| 511 | } |
| 512 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 513 | output_length += output_part_length; |
| 514 | } |
| 515 | } |
| 516 | else |
| 517 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 518 | /* Pass all data in one go. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 519 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 520 | data_true_size, output_data, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 521 | output_size, &output_length ) ); |
| 522 | } |
| 523 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 524 | if( is_encrypt ) |
| 525 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 526 | final_output_size, |
| 527 | &output_part_length, |
| 528 | tag_buffer, tag_length, |
| 529 | &tag_size ) ); |
| 530 | else |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 531 | { |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 532 | PSA_ASSERT( psa_aead_verify( &operation, final_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 533 | final_output_size, |
| 534 | &output_part_length, |
| 535 | ( input_data->x + data_true_size ), |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 536 | tag_length ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 539 | if( output_data && output_part_length ) |
| 540 | memcpy( ( output_data + output_length ), final_data, |
| 541 | output_part_length ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 542 | |
| 543 | output_length += output_part_length; |
| 544 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 545 | |
| 546 | /* For all currently defined algorithms, PSA_AEAD_xxx_OUTPUT_SIZE |
| 547 | * should be exact.*/ |
| 548 | if( is_encrypt ) |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 549 | { |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 550 | TEST_EQUAL( tag_length, tag_size ); |
| 551 | |
| 552 | if( output_data && tag_length ) |
| 553 | memcpy( ( output_data + output_length ), tag_buffer, |
| 554 | tag_length ); |
| 555 | |
| 556 | output_length += tag_length; |
| 557 | |
| 558 | TEST_EQUAL( output_length, |
| 559 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 560 | input_data->len ) ); |
| 561 | TEST_ASSERT( output_length <= |
| 562 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 563 | } |
| 564 | else |
| 565 | { |
| 566 | TEST_EQUAL( output_length, |
| 567 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, |
| 568 | input_data->len ) ); |
| 569 | TEST_ASSERT( output_length <= |
| 570 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 571 | } |
| 572 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 573 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 574 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 575 | output_data, output_length ); |
| 576 | |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 577 | |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 578 | test_ok = 1; |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 579 | |
| 580 | exit: |
| 581 | psa_destroy_key( key ); |
| 582 | psa_aead_abort( &operation ); |
| 583 | mbedtls_free( output_data ); |
| 584 | mbedtls_free( part_data ); |
| 585 | mbedtls_free( final_data ); |
| 586 | PSA_DONE( ); |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 587 | |
| 588 | return( test_ok ); |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 589 | } |
| 590 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 591 | /* END_HEADER */ |
| 592 | |
| 593 | /* BEGIN_DEPENDENCIES |
| 594 | * depends_on:MBEDTLS_PSA_CRYPTO_C |
| 595 | * END_DEPENDENCIES |
| 596 | */ |
| 597 | |
| 598 | /* BEGIN_CASE */ |
Gilles Peskine | e1f2d7d | 2018-08-21 14:54:54 +0200 | [diff] [blame] | 599 | void static_checks( ) |
| 600 | { |
| 601 | size_t max_truncated_mac_size = |
| 602 | PSA_ALG_MAC_TRUNCATION_MASK >> PSA_MAC_TRUNCATION_OFFSET; |
| 603 | |
| 604 | /* Check that the length for a truncated MAC always fits in the algorithm |
| 605 | * encoding. The shifted mask is the maximum truncated value. The |
| 606 | * untruncated algorithm may be one byte larger. */ |
| 607 | TEST_ASSERT( PSA_MAC_MAX_SIZE <= 1 + max_truncated_mac_size ); |
| 608 | } |
| 609 | /* END_CASE */ |
| 610 | |
| 611 | /* BEGIN_CASE */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 612 | void import_with_policy( int type_arg, |
| 613 | int usage_arg, int alg_arg, |
| 614 | int expected_status_arg ) |
| 615 | { |
| 616 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 617 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 618 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 619 | psa_key_type_t type = type_arg; |
| 620 | psa_key_usage_t usage = usage_arg; |
| 621 | psa_algorithm_t alg = alg_arg; |
| 622 | psa_status_t expected_status = expected_status_arg; |
| 623 | const uint8_t key_material[16] = {0}; |
| 624 | psa_status_t status; |
| 625 | |
| 626 | PSA_ASSERT( psa_crypto_init( ) ); |
| 627 | |
| 628 | psa_set_key_type( &attributes, type ); |
| 629 | psa_set_key_usage_flags( &attributes, usage ); |
| 630 | psa_set_key_algorithm( &attributes, alg ); |
| 631 | |
| 632 | status = psa_import_key( &attributes, |
| 633 | key_material, sizeof( key_material ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 634 | &key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 635 | TEST_EQUAL( status, expected_status ); |
| 636 | if( status != PSA_SUCCESS ) |
| 637 | goto exit; |
| 638 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 639 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 640 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 641 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 642 | mbedtls_test_update_key_usage_flags( usage ) ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 643 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 644 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 645 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 646 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 647 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 648 | |
| 649 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 650 | /* |
| 651 | * Key attributes may have been returned by psa_get_key_attributes() |
| 652 | * thus reset them as required. |
| 653 | */ |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 654 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 655 | |
| 656 | psa_destroy_key( key ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 657 | PSA_DONE( ); |
| 658 | } |
| 659 | /* END_CASE */ |
| 660 | |
| 661 | /* BEGIN_CASE */ |
| 662 | void import_with_data( data_t *data, int type_arg, |
| 663 | int attr_bits_arg, |
| 664 | int expected_status_arg ) |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 665 | { |
| 666 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 667 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 668 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 669 | psa_key_type_t type = type_arg; |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 670 | size_t attr_bits = attr_bits_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 671 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 672 | psa_status_t status; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 673 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 674 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 675 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 676 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 677 | psa_set_key_bits( &attributes, attr_bits ); |
Gilles Peskine | 6edfa29 | 2019-07-31 15:53:45 +0200 | [diff] [blame] | 678 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 679 | status = psa_import_key( &attributes, data->x, data->len, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 680 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 681 | if( status != PSA_SUCCESS ) |
| 682 | goto exit; |
| 683 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 684 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 685 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
Gilles Peskine | 8fb3a9e | 2019-05-03 16:59:21 +0200 | [diff] [blame] | 686 | if( attr_bits != 0 ) |
Gilles Peskine | 7e0cff9 | 2019-07-30 13:48:52 +0200 | [diff] [blame] | 687 | TEST_EQUAL( attr_bits, psa_get_key_bits( &got_attributes ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 688 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 689 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 690 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 691 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 692 | |
| 693 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 694 | /* |
| 695 | * Key attributes may have been returned by psa_get_key_attributes() |
| 696 | * thus reset them as required. |
| 697 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 698 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 699 | |
| 700 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 701 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 702 | } |
| 703 | /* END_CASE */ |
| 704 | |
| 705 | /* BEGIN_CASE */ |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 706 | void import_large_key( int type_arg, int byte_size_arg, |
| 707 | int expected_status_arg ) |
| 708 | { |
| 709 | psa_key_type_t type = type_arg; |
| 710 | size_t byte_size = byte_size_arg; |
| 711 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 712 | psa_status_t expected_status = expected_status_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 713 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 714 | psa_status_t status; |
| 715 | uint8_t *buffer = NULL; |
| 716 | size_t buffer_size = byte_size + 1; |
| 717 | size_t n; |
| 718 | |
Steven Cooreman | 69967ce | 2021-01-18 18:01:08 +0100 | [diff] [blame] | 719 | /* Skip the test case if the target running the test cannot |
| 720 | * accomodate large keys due to heap size constraints */ |
| 721 | ASSERT_ALLOC_WEAK( buffer, buffer_size ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 722 | memset( buffer, 'K', byte_size ); |
| 723 | |
| 724 | PSA_ASSERT( psa_crypto_init( ) ); |
| 725 | |
| 726 | /* Try importing the key */ |
| 727 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 728 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 729 | status = psa_import_key( &attributes, buffer, byte_size, &key ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 730 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 731 | TEST_EQUAL( status, expected_status ); |
| 732 | |
| 733 | if( status == PSA_SUCCESS ) |
| 734 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 735 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 736 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 737 | TEST_EQUAL( psa_get_key_bits( &attributes ), |
| 738 | PSA_BYTES_TO_BITS( byte_size ) ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 739 | ASSERT_NO_SLOT_NUMBER( &attributes ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 740 | memset( buffer, 0, byte_size + 1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 741 | PSA_ASSERT( psa_export_key( key, buffer, byte_size, &n ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 742 | for( n = 0; n < byte_size; n++ ) |
| 743 | TEST_EQUAL( buffer[n], 'K' ); |
| 744 | for( n = byte_size; n < buffer_size; n++ ) |
| 745 | TEST_EQUAL( buffer[n], 0 ); |
| 746 | } |
| 747 | |
| 748 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 749 | /* |
| 750 | * Key attributes may have been returned by psa_get_key_attributes() |
| 751 | * thus reset them as required. |
| 752 | */ |
| 753 | psa_reset_key_attributes( &attributes ); |
| 754 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 755 | psa_destroy_key( key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 756 | PSA_DONE( ); |
| 757 | mbedtls_free( buffer ); |
| 758 | } |
| 759 | /* END_CASE */ |
| 760 | |
| 761 | /* BEGIN_CASE */ |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 762 | void import_rsa_made_up( int bits_arg, int keypair, int expected_status_arg ) |
| 763 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 764 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 765 | size_t bits = bits_arg; |
| 766 | psa_status_t expected_status = expected_status_arg; |
| 767 | psa_status_t status; |
| 768 | psa_key_type_t type = |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 769 | 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] | 770 | size_t buffer_size = /* Slight overapproximations */ |
| 771 | keypair ? bits * 9 / 16 + 80 : bits / 8 + 20; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 772 | unsigned char *buffer = NULL; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 773 | unsigned char *p; |
| 774 | int ret; |
| 775 | size_t length; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 776 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 777 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 778 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 779 | ASSERT_ALLOC( buffer, buffer_size ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 780 | |
| 781 | TEST_ASSERT( ( ret = construct_fake_rsa_key( buffer, buffer_size, &p, |
| 782 | bits, keypair ) ) >= 0 ); |
| 783 | length = ret; |
| 784 | |
| 785 | /* Try importing the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 786 | psa_set_key_type( &attributes, type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 787 | status = psa_import_key( &attributes, p, length, &key ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 788 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 789 | |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 790 | if( status == PSA_SUCCESS ) |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 791 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 792 | |
| 793 | exit: |
| 794 | mbedtls_free( buffer ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 795 | PSA_DONE( ); |
Gilles Peskine | 0b352bc | 2018-06-28 00:16:11 +0200 | [diff] [blame] | 796 | } |
| 797 | /* END_CASE */ |
| 798 | |
| 799 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 800 | void import_export( data_t *data, |
Moran Peker | a964a8f | 2018-06-04 18:42:36 +0300 | [diff] [blame] | 801 | int type_arg, |
Gilles Peskine | 1ecf92c2 | 2019-05-24 15:00:06 +0200 | [diff] [blame] | 802 | int usage_arg, int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 803 | int lifetime_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 804 | int expected_bits, |
| 805 | int export_size_delta, |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 806 | int expected_export_status_arg, |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 807 | int canonical_input ) |
| 808 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 809 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 810 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 811 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 812 | psa_status_t expected_export_status = expected_export_status_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 813 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 814 | psa_key_lifetime_t lifetime = lifetime_arg; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 815 | unsigned char *exported = NULL; |
| 816 | unsigned char *reexported = NULL; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 817 | size_t export_size; |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 818 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 819 | size_t reexported_length; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 820 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 821 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 822 | |
Moran Peker | cb088e7 | 2018-07-17 17:36:59 +0300 | [diff] [blame] | 823 | export_size = (ptrdiff_t) data->len + export_size_delta; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 824 | ASSERT_ALLOC( exported, export_size ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 825 | if( ! canonical_input ) |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 826 | ASSERT_ALLOC( reexported, export_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 827 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 828 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 829 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 830 | psa_set_key_usage_flags( &attributes, usage_arg ); |
| 831 | psa_set_key_algorithm( &attributes, alg ); |
| 832 | psa_set_key_type( &attributes, type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 833 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 834 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 835 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 836 | |
| 837 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 838 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 839 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 840 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), (size_t) expected_bits ); |
Gilles Peskine | 5fe5e27 | 2019-08-02 20:30:01 +0200 | [diff] [blame] | 841 | ASSERT_NO_SLOT_NUMBER( &got_attributes ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 842 | |
| 843 | /* Export the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 844 | status = psa_export_key( key, exported, export_size, &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 845 | TEST_EQUAL( status, expected_export_status ); |
Jaeden Amero | f24c7f8 | 2018-06-27 17:20:43 +0100 | [diff] [blame] | 846 | |
| 847 | /* The exported length must be set by psa_export_key() to a value between 0 |
| 848 | * and export_size. On errors, the exported length must be 0. */ |
| 849 | TEST_ASSERT( exported_length != INVALID_EXPORT_LENGTH ); |
| 850 | TEST_ASSERT( status == PSA_SUCCESS || exported_length == 0 ); |
| 851 | TEST_ASSERT( exported_length <= export_size ); |
| 852 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 853 | TEST_ASSERT( mem_is_char( exported + exported_length, 0, |
Gilles Peskine | 3f669c3 | 2018-06-21 09:21:51 +0200 | [diff] [blame] | 854 | export_size - exported_length ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 855 | if( status != PSA_SUCCESS ) |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 856 | { |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 857 | TEST_EQUAL( exported_length, 0 ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 858 | goto destroy; |
Gilles Peskine | e66ca3b | 2018-06-20 00:11:45 +0200 | [diff] [blame] | 859 | } |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 860 | |
Gilles Peskine | ea38a92 | 2021-02-13 00:05:16 +0100 | [diff] [blame] | 861 | /* Run sanity checks on the exported key. For non-canonical inputs, |
| 862 | * this validates the canonical representations. For canonical inputs, |
| 863 | * this doesn't directly validate the implementation, but it still helps |
| 864 | * by cross-validating the test data with the sanity check code. */ |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 865 | if( !psa_key_lifetime_is_external( lifetime ) ) |
| 866 | { |
| 867 | if( ! mbedtls_test_psa_exercise_key( key, usage_arg, 0 ) ) |
| 868 | goto exit; |
| 869 | } |
Gilles Peskine | 8f60923 | 2018-08-11 01:24:55 +0200 | [diff] [blame] | 870 | |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 871 | if( canonical_input ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 872 | ASSERT_COMPARE( data->x, data->len, exported, exported_length ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 873 | else |
| 874 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 875 | mbedtls_svc_key_id_t key2 = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 876 | PSA_ASSERT( psa_import_key( &attributes, exported, exported_length, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 877 | &key2 ) ); |
| 878 | PSA_ASSERT( psa_export_key( key2, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 879 | reexported, |
| 880 | export_size, |
| 881 | &reexported_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 882 | ASSERT_COMPARE( exported, exported_length, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 883 | reexported, reexported_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 884 | PSA_ASSERT( psa_destroy_key( key2 ) ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 885 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 886 | TEST_ASSERT( exported_length <= |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 887 | PSA_EXPORT_KEY_OUTPUT_SIZE( type, |
Archana | 9d17bf4 | 2021-09-10 06:22:44 +0530 | [diff] [blame] | 888 | psa_get_key_bits( &got_attributes ) ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 889 | TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 890 | |
| 891 | destroy: |
| 892 | /* Destroy the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 893 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 894 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 895 | |
| 896 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 897 | /* |
| 898 | * Key attributes may have been returned by psa_get_key_attributes() |
| 899 | * thus reset them as required. |
| 900 | */ |
| 901 | psa_reset_key_attributes( &got_attributes ); |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 902 | psa_destroy_key( key ) ; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 903 | mbedtls_free( exported ); |
| 904 | mbedtls_free( reexported ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 905 | PSA_DONE( ); |
Gilles Peskine | 2f9c4dc | 2018-01-28 13:16:24 +0100 | [diff] [blame] | 906 | } |
| 907 | /* END_CASE */ |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 908 | |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 909 | /* BEGIN_CASE */ |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 910 | void import_export_public_key( data_t *data, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 911 | int type_arg, |
| 912 | int alg_arg, |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 913 | int lifetime_arg, |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 914 | int export_size_delta, |
| 915 | int expected_export_status_arg, |
| 916 | data_t *expected_public_key ) |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 917 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 918 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 919 | psa_key_type_t type = type_arg; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 920 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 921 | psa_status_t expected_export_status = expected_export_status_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 922 | psa_status_t status; |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 923 | psa_key_lifetime_t lifetime = lifetime_arg; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 924 | unsigned char *exported = NULL; |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 925 | size_t export_size = expected_public_key->len + export_size_delta; |
Jaeden Amero | 2a671e9 | 2018-06-27 17:47:40 +0100 | [diff] [blame] | 926 | size_t exported_length = INVALID_EXPORT_LENGTH; |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 927 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 928 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 929 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 930 | |
Archana | 4d7ae1d | 2021-07-07 02:50:22 +0530 | [diff] [blame] | 931 | psa_set_key_lifetime( &attributes, lifetime ); |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 932 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT ); |
| 933 | psa_set_key_algorithm( &attributes, alg ); |
| 934 | psa_set_key_type( &attributes, type ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 935 | |
| 936 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 937 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 938 | |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 939 | /* Export the public key */ |
| 940 | ASSERT_ALLOC( exported, export_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 941 | status = psa_export_public_key( key, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 942 | exported, export_size, |
| 943 | &exported_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 944 | TEST_EQUAL( status, expected_export_status ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 945 | if( status == PSA_SUCCESS ) |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 946 | { |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 947 | 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] | 948 | size_t bits; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 949 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 950 | bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 951 | TEST_ASSERT( expected_public_key->len <= |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 952 | PSA_EXPORT_KEY_OUTPUT_SIZE( public_type, bits ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 953 | TEST_ASSERT( expected_public_key->len <= |
| 954 | PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type, bits ) ); |
| 955 | TEST_ASSERT( expected_public_key->len <= |
| 956 | PSA_EXPORT_PUBLIC_KEY_MAX_SIZE ); |
Gilles Peskine | 49c2591 | 2018-10-29 15:15:31 +0100 | [diff] [blame] | 957 | ASSERT_COMPARE( expected_public_key->x, expected_public_key->len, |
| 958 | exported, exported_length ); |
Gilles Peskine | d8b7d4f | 2018-10-29 15:18:41 +0100 | [diff] [blame] | 959 | } |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 960 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 961 | /* |
| 962 | * Key attributes may have been returned by psa_get_key_attributes() |
| 963 | * thus reset them as required. |
| 964 | */ |
| 965 | psa_reset_key_attributes( &attributes ); |
| 966 | |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 967 | mbedtls_free( exported ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 968 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 969 | PSA_DONE( ); |
Moran Peker | f709f4a | 2018-06-06 17:26:04 +0300 | [diff] [blame] | 970 | } |
| 971 | /* END_CASE */ |
| 972 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 973 | /* BEGIN_CASE */ |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 974 | void import_and_exercise_key( data_t *data, |
| 975 | int type_arg, |
| 976 | int bits_arg, |
| 977 | int alg_arg ) |
| 978 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 979 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 980 | psa_key_type_t type = type_arg; |
| 981 | size_t bits = bits_arg; |
| 982 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 983 | 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] | 984 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 985 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 986 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 987 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 988 | |
Gilles Peskine | 4747d19 | 2019-04-17 15:05:45 +0200 | [diff] [blame] | 989 | psa_set_key_usage_flags( &attributes, usage ); |
| 990 | psa_set_key_algorithm( &attributes, alg ); |
| 991 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 992 | |
| 993 | /* Import the key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 994 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &key ) ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 995 | |
| 996 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 997 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 998 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 999 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1000 | |
| 1001 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1002 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 1003 | goto exit; |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1004 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1005 | PSA_ASSERT( psa_destroy_key( key ) ); |
| 1006 | test_operations_on_invalid_key( key ); |
Gilles Peskine | 4cf3a43 | 2019-04-18 22:28:52 +0200 | [diff] [blame] | 1007 | |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1008 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1009 | /* |
| 1010 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1011 | * thus reset them as required. |
| 1012 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1013 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1014 | |
| 1015 | psa_reset_key_attributes( &attributes ); |
| 1016 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1017 | PSA_DONE( ); |
Gilles Peskine | a680c7a | 2018-06-26 16:12:43 +0200 | [diff] [blame] | 1018 | } |
| 1019 | /* END_CASE */ |
| 1020 | |
| 1021 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1022 | void effective_key_attributes( int type_arg, int expected_type_arg, |
| 1023 | int bits_arg, int expected_bits_arg, |
| 1024 | int usage_arg, int expected_usage_arg, |
| 1025 | int alg_arg, int expected_alg_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1026 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1027 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1028 | psa_key_type_t key_type = type_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1029 | psa_key_type_t expected_key_type = expected_type_arg; |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1030 | size_t bits = bits_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1031 | size_t expected_bits = expected_bits_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1032 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1033 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1034 | psa_key_usage_t usage = usage_arg; |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1035 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1036 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1037 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1038 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1039 | |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1040 | psa_set_key_usage_flags( &attributes, usage ); |
| 1041 | psa_set_key_algorithm( &attributes, alg ); |
| 1042 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1043 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1044 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1045 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Gilles Peskine | 1a96049 | 2019-11-26 17:12:21 +0100 | [diff] [blame] | 1046 | psa_reset_key_attributes( &attributes ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1047 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1048 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1049 | TEST_EQUAL( psa_get_key_type( &attributes ), expected_key_type ); |
| 1050 | TEST_EQUAL( psa_get_key_bits( &attributes ), expected_bits ); |
| 1051 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1052 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), expected_alg ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1053 | |
| 1054 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1055 | /* |
| 1056 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1057 | * thus reset them as required. |
| 1058 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1059 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1060 | |
| 1061 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1062 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1063 | } |
| 1064 | /* END_CASE */ |
| 1065 | |
| 1066 | /* BEGIN_CASE */ |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1067 | void check_key_policy( int type_arg, int bits_arg, |
| 1068 | int usage_arg, int alg_arg ) |
| 1069 | { |
| 1070 | 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] | 1071 | usage_arg, |
| 1072 | mbedtls_test_update_key_usage_flags( usage_arg ), |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1073 | alg_arg, alg_arg ); |
Gilles Peskine | 06c2889 | 2019-11-26 18:07:46 +0100 | [diff] [blame] | 1074 | goto exit; |
| 1075 | } |
| 1076 | /* END_CASE */ |
| 1077 | |
| 1078 | /* BEGIN_CASE */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1079 | void key_attributes_init( ) |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1080 | { |
| 1081 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1082 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1083 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1084 | * to supress the Clang warning for the test. */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1085 | psa_key_attributes_t func = psa_key_attributes_init( ); |
| 1086 | psa_key_attributes_t init = PSA_KEY_ATTRIBUTES_INIT; |
| 1087 | psa_key_attributes_t zero; |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1088 | |
| 1089 | memset( &zero, 0, sizeof( zero ) ); |
| 1090 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1091 | TEST_EQUAL( psa_get_key_lifetime( &func ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1092 | TEST_EQUAL( psa_get_key_lifetime( &init ), PSA_KEY_LIFETIME_VOLATILE ); |
| 1093 | TEST_EQUAL( psa_get_key_lifetime( &zero ), PSA_KEY_LIFETIME_VOLATILE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1094 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1095 | TEST_EQUAL( psa_get_key_type( &func ), 0 ); |
| 1096 | TEST_EQUAL( psa_get_key_type( &init ), 0 ); |
| 1097 | TEST_EQUAL( psa_get_key_type( &zero ), 0 ); |
| 1098 | |
| 1099 | TEST_EQUAL( psa_get_key_bits( &func ), 0 ); |
| 1100 | TEST_EQUAL( psa_get_key_bits( &init ), 0 ); |
| 1101 | TEST_EQUAL( psa_get_key_bits( &zero ), 0 ); |
| 1102 | |
| 1103 | TEST_EQUAL( psa_get_key_usage_flags( &func ), 0 ); |
| 1104 | TEST_EQUAL( psa_get_key_usage_flags( &init ), 0 ); |
| 1105 | TEST_EQUAL( psa_get_key_usage_flags( &zero ), 0 ); |
| 1106 | |
| 1107 | TEST_EQUAL( psa_get_key_algorithm( &func ), 0 ); |
| 1108 | TEST_EQUAL( psa_get_key_algorithm( &init ), 0 ); |
| 1109 | TEST_EQUAL( psa_get_key_algorithm( &zero ), 0 ); |
Jaeden Amero | 70261c5 | 2019-01-04 11:47:20 +0000 | [diff] [blame] | 1110 | } |
| 1111 | /* END_CASE */ |
| 1112 | |
| 1113 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1114 | void mac_key_policy( int policy_usage_arg, |
| 1115 | int policy_alg_arg, |
| 1116 | int key_type_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1117 | data_t *key_data, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1118 | int exercise_alg_arg, |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1119 | int expected_status_sign_arg, |
| 1120 | int expected_status_verify_arg ) |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1121 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1122 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1123 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 1124 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1125 | psa_key_type_t key_type = key_type_arg; |
| 1126 | psa_algorithm_t policy_alg = policy_alg_arg; |
| 1127 | psa_algorithm_t exercise_alg = exercise_alg_arg; |
| 1128 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1129 | psa_status_t status; |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1130 | psa_status_t expected_status_sign = expected_status_sign_arg; |
| 1131 | psa_status_t expected_status_verify = expected_status_verify_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1132 | unsigned char mac[PSA_MAC_MAX_SIZE]; |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1133 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1134 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1135 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1136 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1137 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1138 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1139 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1140 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1141 | &key ) ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1142 | |
gabor-mezei-arm | 40d5cd8 | 2021-06-29 11:06:16 +0200 | [diff] [blame] | 1143 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
| 1144 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1145 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1146 | status = psa_mac_sign_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1147 | TEST_EQUAL( status, expected_status_sign ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1148 | |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1149 | /* Calculate the MAC, one-shot case. */ |
| 1150 | uint8_t input[128] = {0}; |
| 1151 | size_t mac_len; |
| 1152 | TEST_EQUAL( psa_mac_compute( key, exercise_alg, |
| 1153 | input, 128, |
| 1154 | mac, PSA_MAC_MAX_SIZE, &mac_len ), |
| 1155 | expected_status_sign ); |
| 1156 | |
| 1157 | /* Verify correct MAC, one-shot case. */ |
| 1158 | status = psa_mac_verify( key, exercise_alg, input, 128, |
| 1159 | mac, mac_len ); |
| 1160 | |
| 1161 | if( expected_status_sign != PSA_SUCCESS && expected_status_verify == PSA_SUCCESS ) |
| 1162 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1163 | else |
Mateusz Starzyk | 1ebcd55 | 2021-08-30 17:09:03 +0200 | [diff] [blame] | 1164 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1165 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1166 | psa_mac_abort( &operation ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1167 | |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1168 | memset( mac, 0, sizeof( mac ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1169 | status = psa_mac_verify_setup( &operation, key, exercise_alg ); |
Mateusz Starzyk | d07f4fc | 2021-08-24 11:01:23 +0200 | [diff] [blame] | 1170 | TEST_EQUAL( status, expected_status_verify ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1171 | |
| 1172 | exit: |
| 1173 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1174 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1175 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1176 | } |
| 1177 | /* END_CASE */ |
| 1178 | |
| 1179 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1180 | void cipher_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1181 | int policy_alg, |
| 1182 | int key_type, |
| 1183 | data_t *key_data, |
| 1184 | int exercise_alg ) |
| 1185 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1186 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1187 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 1188 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1189 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1190 | psa_status_t status; |
| 1191 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1192 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1193 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1194 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1195 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1196 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1197 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1198 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1199 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1200 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1201 | /* Check if no key usage flag implication is done */ |
| 1202 | TEST_EQUAL( policy_usage, |
| 1203 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1204 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1205 | status = psa_cipher_encrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1206 | if( policy_alg == exercise_alg && |
| 1207 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1208 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1209 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1210 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1211 | psa_cipher_abort( &operation ); |
| 1212 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1213 | status = psa_cipher_decrypt_setup( &operation, key, exercise_alg ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1214 | if( policy_alg == exercise_alg && |
| 1215 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1216 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1217 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1218 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1219 | |
| 1220 | exit: |
| 1221 | psa_cipher_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1222 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1223 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1224 | } |
| 1225 | /* END_CASE */ |
| 1226 | |
| 1227 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1228 | void aead_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1229 | int policy_alg, |
| 1230 | int key_type, |
| 1231 | data_t *key_data, |
| 1232 | int nonce_length_arg, |
| 1233 | int tag_length_arg, |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1234 | int exercise_alg, |
| 1235 | int expected_status_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1236 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1237 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1238 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1239 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1240 | psa_status_t status; |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1241 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1242 | unsigned char nonce[16] = {0}; |
| 1243 | size_t nonce_length = nonce_length_arg; |
| 1244 | unsigned char tag[16]; |
| 1245 | size_t tag_length = tag_length_arg; |
| 1246 | size_t output_length; |
| 1247 | |
| 1248 | TEST_ASSERT( nonce_length <= sizeof( nonce ) ); |
| 1249 | TEST_ASSERT( tag_length <= sizeof( tag ) ); |
| 1250 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1251 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1252 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1253 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1254 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1255 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1256 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1257 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1258 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1259 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1260 | /* Check if no key usage implication is done */ |
| 1261 | TEST_EQUAL( policy_usage, |
| 1262 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1263 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1264 | status = psa_aead_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1265 | nonce, nonce_length, |
| 1266 | NULL, 0, |
| 1267 | NULL, 0, |
| 1268 | tag, tag_length, |
| 1269 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1270 | if( ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
| 1271 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1272 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1273 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1274 | |
| 1275 | memset( tag, 0, sizeof( tag ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1276 | status = psa_aead_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1277 | nonce, nonce_length, |
| 1278 | NULL, 0, |
| 1279 | tag, tag_length, |
| 1280 | NULL, 0, |
| 1281 | &output_length ); |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1282 | if( ( policy_usage & PSA_KEY_USAGE_DECRYPT ) == 0 ) |
| 1283 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1284 | else if( expected_status == PSA_SUCCESS ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1285 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1286 | else |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1287 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1288 | |
| 1289 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1290 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1291 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1292 | } |
| 1293 | /* END_CASE */ |
| 1294 | |
| 1295 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1296 | void asymmetric_encryption_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1297 | int policy_alg, |
| 1298 | int key_type, |
| 1299 | data_t *key_data, |
| 1300 | int exercise_alg ) |
| 1301 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1302 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1303 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1304 | psa_key_usage_t policy_usage = policy_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1305 | psa_status_t status; |
| 1306 | size_t key_bits; |
| 1307 | size_t buffer_length; |
| 1308 | unsigned char *buffer = NULL; |
| 1309 | size_t output_length; |
| 1310 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1311 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1312 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1313 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1314 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1315 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1316 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1317 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1318 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1319 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1320 | /* Check if no key usage implication is done */ |
| 1321 | TEST_EQUAL( policy_usage, |
| 1322 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1323 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1324 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 1325 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1326 | buffer_length = PSA_ASYMMETRIC_ENCRYPT_OUTPUT_SIZE( key_type, key_bits, |
| 1327 | exercise_alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 1328 | ASSERT_ALLOC( buffer, buffer_length ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1329 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1330 | status = psa_asymmetric_encrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1331 | NULL, 0, |
| 1332 | NULL, 0, |
| 1333 | buffer, buffer_length, |
| 1334 | &output_length ); |
| 1335 | if( policy_alg == exercise_alg && |
| 1336 | ( policy_usage & PSA_KEY_USAGE_ENCRYPT ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1337 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1338 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1339 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1340 | |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 1341 | if( buffer_length != 0 ) |
| 1342 | memset( buffer, 0, buffer_length ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1343 | status = psa_asymmetric_decrypt( key, exercise_alg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1344 | buffer, buffer_length, |
| 1345 | NULL, 0, |
| 1346 | buffer, buffer_length, |
| 1347 | &output_length ); |
| 1348 | if( policy_alg == exercise_alg && |
| 1349 | ( policy_usage & PSA_KEY_USAGE_DECRYPT ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1350 | TEST_EQUAL( status, PSA_ERROR_INVALID_PADDING ); |
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 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1355 | /* |
| 1356 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1357 | * thus reset them as required. |
| 1358 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1359 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1360 | |
| 1361 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1362 | PSA_DONE( ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1363 | mbedtls_free( buffer ); |
| 1364 | } |
| 1365 | /* END_CASE */ |
| 1366 | |
| 1367 | /* BEGIN_CASE */ |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1368 | void asymmetric_signature_key_policy( int policy_usage_arg, |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1369 | int policy_alg, |
| 1370 | int key_type, |
| 1371 | data_t *key_data, |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1372 | int exercise_alg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1373 | int payload_length_arg, |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1374 | int expected_usage_arg ) |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1375 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1376 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1377 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1378 | psa_key_usage_t policy_usage = policy_usage_arg; |
| 1379 | psa_key_usage_t expected_usage = expected_usage_arg; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1380 | psa_status_t status; |
Gilles Peskine | 30f77cd | 2019-01-14 16:06:39 +0100 | [diff] [blame] | 1381 | unsigned char payload[PSA_HASH_MAX_SIZE] = {1}; |
| 1382 | /* If `payload_length_arg > 0`, `exercise_alg` is supposed to be |
| 1383 | * compatible with the policy and `payload_length_arg` is supposed to be |
| 1384 | * a valid input length to sign. If `payload_length_arg <= 0`, |
| 1385 | * `exercise_alg` is supposed to be forbidden by the policy. */ |
| 1386 | int compatible_alg = payload_length_arg > 0; |
| 1387 | size_t payload_length = compatible_alg ? payload_length_arg : 0; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1388 | unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0}; |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1389 | size_t signature_length; |
| 1390 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1391 | /* Check if all implicit usage flags are deployed |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1392 | in the expected usage flags. */ |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1393 | TEST_EQUAL( expected_usage, |
| 1394 | mbedtls_test_update_key_usage_flags( policy_usage ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1395 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1396 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1397 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1398 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1399 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1400 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1401 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1402 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1403 | &key ) ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1404 | |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1405 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), expected_usage ); |
| 1406 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1407 | status = psa_sign_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1408 | payload, payload_length, |
| 1409 | signature, sizeof( signature ), |
| 1410 | &signature_length ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1411 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_HASH ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1412 | PSA_ASSERT( status ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1413 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1414 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1415 | |
| 1416 | memset( signature, 0, sizeof( signature ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1417 | status = psa_verify_hash( key, exercise_alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 1418 | payload, payload_length, |
| 1419 | signature, sizeof( signature ) ); |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1420 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_HASH ) != 0 ) |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1421 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 76f5c7b | 2018-07-06 16:53:09 +0200 | [diff] [blame] | 1422 | else |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1423 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1424 | |
Gilles Peskine | f7b4137 | 2021-09-22 16:15:05 +0200 | [diff] [blame] | 1425 | if( PSA_ALG_IS_SIGN_HASH( exercise_alg ) && |
gabor-mezei-arm | d851d68 | 2021-06-28 14:53:49 +0200 | [diff] [blame] | 1426 | PSA_ALG_IS_HASH( PSA_ALG_SIGN_GET_HASH( exercise_alg ) ) ) |
gabor-mezei-arm | edf2df8 | 2021-05-13 16:17:16 +0200 | [diff] [blame] | 1427 | { |
| 1428 | status = psa_sign_message( key, exercise_alg, |
| 1429 | payload, payload_length, |
| 1430 | signature, sizeof( signature ), |
| 1431 | &signature_length ); |
| 1432 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_SIGN_MESSAGE ) != 0 ) |
| 1433 | PSA_ASSERT( status ); |
| 1434 | else |
| 1435 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1436 | |
| 1437 | memset( signature, 0, sizeof( signature ) ); |
| 1438 | status = psa_verify_message( key, exercise_alg, |
| 1439 | payload, payload_length, |
| 1440 | signature, sizeof( signature ) ); |
| 1441 | if( compatible_alg && ( expected_usage & PSA_KEY_USAGE_VERIFY_MESSAGE ) != 0 ) |
| 1442 | TEST_EQUAL( status, PSA_ERROR_INVALID_SIGNATURE ); |
| 1443 | else |
| 1444 | TEST_EQUAL( status, PSA_ERROR_NOT_PERMITTED ); |
| 1445 | } |
| 1446 | |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1447 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1448 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1449 | PSA_DONE( ); |
Gilles Peskine | d5b3322 | 2018-06-18 22:20:03 +0200 | [diff] [blame] | 1450 | } |
| 1451 | /* END_CASE */ |
| 1452 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1453 | /* BEGIN_CASE */ |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1454 | void derive_key_policy( int policy_usage, |
| 1455 | int policy_alg, |
| 1456 | int key_type, |
| 1457 | data_t *key_data, |
| 1458 | int exercise_alg ) |
| 1459 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1460 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1461 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1462 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1463 | psa_status_t status; |
| 1464 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1465 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1466 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1467 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1468 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1469 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1470 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1471 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1472 | &key ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1473 | |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1474 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
| 1475 | |
| 1476 | if( PSA_ALG_IS_TLS12_PRF( exercise_alg ) || |
| 1477 | PSA_ALG_IS_TLS12_PSK_TO_MS( exercise_alg ) ) |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1478 | { |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1479 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 1480 | &operation, |
| 1481 | PSA_KEY_DERIVATION_INPUT_SEED, |
| 1482 | (const uint8_t*) "", 0) ); |
Janos Follath | 0c1ed84 | 2019-06-28 13:35:36 +0100 | [diff] [blame] | 1483 | } |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1484 | |
| 1485 | status = psa_key_derivation_input_key( &operation, |
| 1486 | PSA_KEY_DERIVATION_INPUT_SECRET, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1487 | key ); |
Janos Follath | ba3fab9 | 2019-06-11 14:50:16 +0100 | [diff] [blame] | 1488 | |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1489 | if( policy_alg == exercise_alg && |
| 1490 | ( policy_usage & PSA_KEY_USAGE_DERIVE ) != 0 ) |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1491 | PSA_ASSERT( status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +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 | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1494 | |
| 1495 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1496 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1497 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1498 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 1499 | } |
| 1500 | /* END_CASE */ |
| 1501 | |
| 1502 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1503 | void agreement_key_policy( int policy_usage, |
| 1504 | int policy_alg, |
| 1505 | int key_type_arg, |
| 1506 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1507 | int exercise_alg, |
| 1508 | int expected_status_arg ) |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1509 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1510 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1511 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1512 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1513 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1514 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1515 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1516 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1517 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1518 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1519 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1520 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1521 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1522 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1523 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1524 | &key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1525 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1526 | PSA_ASSERT( psa_key_derivation_setup( &operation, exercise_alg ) ); |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1527 | status = mbedtls_test_psa_key_agreement_with_self( &operation, key ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1528 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1529 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1530 | |
| 1531 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1532 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1533 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1534 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 1535 | } |
| 1536 | /* END_CASE */ |
| 1537 | |
| 1538 | /* BEGIN_CASE */ |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1539 | void key_policy_alg2( int key_type_arg, data_t *key_data, |
| 1540 | int usage_arg, int alg_arg, int alg2_arg ) |
| 1541 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1542 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1543 | psa_key_type_t key_type = key_type_arg; |
| 1544 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1545 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1546 | psa_key_usage_t usage = usage_arg; |
| 1547 | psa_algorithm_t alg = alg_arg; |
| 1548 | psa_algorithm_t alg2 = alg2_arg; |
| 1549 | |
| 1550 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1551 | |
| 1552 | psa_set_key_usage_flags( &attributes, usage ); |
| 1553 | psa_set_key_algorithm( &attributes, alg ); |
| 1554 | psa_set_key_enrollment_algorithm( &attributes, alg2 ); |
| 1555 | psa_set_key_type( &attributes, key_type ); |
| 1556 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1557 | &key ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1558 | |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 1559 | /* Update the usage flags to obtain implicit usage flags */ |
| 1560 | usage = mbedtls_test_update_key_usage_flags( usage ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1561 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1562 | TEST_EQUAL( psa_get_key_usage_flags( &got_attributes ), usage ); |
| 1563 | TEST_EQUAL( psa_get_key_algorithm( &got_attributes ), alg ); |
| 1564 | TEST_EQUAL( psa_get_key_enrollment_algorithm( &got_attributes ), alg2 ); |
| 1565 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1566 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1567 | goto exit; |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1568 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg2 ) ) |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1569 | goto exit; |
| 1570 | |
| 1571 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1572 | /* |
| 1573 | * Key attributes may have been returned by psa_get_key_attributes() |
| 1574 | * thus reset them as required. |
| 1575 | */ |
| 1576 | psa_reset_key_attributes( &got_attributes ); |
| 1577 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1578 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1579 | PSA_DONE( ); |
Gilles Peskine | 96f0b3b | 2019-05-10 19:33:38 +0200 | [diff] [blame] | 1580 | } |
| 1581 | /* END_CASE */ |
| 1582 | |
| 1583 | /* BEGIN_CASE */ |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1584 | void raw_agreement_key_policy( int policy_usage, |
| 1585 | int policy_alg, |
| 1586 | int key_type_arg, |
| 1587 | data_t *key_data, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1588 | int exercise_alg, |
| 1589 | int expected_status_arg ) |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1590 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1591 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1592 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1593 | psa_key_type_t key_type = key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1594 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1595 | psa_status_t status; |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1596 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1597 | |
| 1598 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1599 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 1600 | psa_set_key_usage_flags( &attributes, policy_usage ); |
| 1601 | psa_set_key_algorithm( &attributes, policy_alg ); |
| 1602 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1603 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1604 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1605 | &key ) ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1606 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 1607 | status = mbedtls_test_psa_raw_key_agreement_with_self( exercise_alg, key ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1608 | |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 1609 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1610 | |
| 1611 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 1612 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1613 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1614 | PSA_DONE( ); |
Gilles Peskine | 04ee2d2 | 2019-04-11 21:25:46 +0200 | [diff] [blame] | 1615 | } |
| 1616 | /* END_CASE */ |
| 1617 | |
| 1618 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1619 | void copy_success( int source_usage_arg, |
| 1620 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1621 | unsigned int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1622 | int type_arg, data_t *material, |
| 1623 | int copy_attributes, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1624 | int target_usage_arg, |
| 1625 | int target_alg_arg, int target_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1626 | unsigned int target_lifetime_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1627 | int expected_usage_arg, |
| 1628 | int expected_alg_arg, int expected_alg2_arg ) |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1629 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1630 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1631 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1632 | psa_key_usage_t expected_usage = expected_usage_arg; |
| 1633 | psa_algorithm_t expected_alg = expected_alg_arg; |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1634 | psa_algorithm_t expected_alg2 = expected_alg2_arg; |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1635 | psa_key_lifetime_t source_lifetime = source_lifetime_arg; |
| 1636 | psa_key_lifetime_t target_lifetime = target_lifetime_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1637 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1638 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1639 | uint8_t *export_buffer = NULL; |
| 1640 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1641 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1642 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1643 | /* Prepare the source key. */ |
| 1644 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1645 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1646 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1647 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1648 | psa_set_key_lifetime( &source_attributes, source_lifetime); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1649 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1650 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1651 | &source_key ) ); |
| 1652 | PSA_ASSERT( psa_get_key_attributes( source_key, &source_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1653 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1654 | /* Prepare the target attributes. */ |
| 1655 | if( copy_attributes ) |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1656 | { |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1657 | target_attributes = source_attributes; |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1658 | } |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1659 | psa_set_key_lifetime( &target_attributes, target_lifetime); |
Ronald Cron | 65f38a3 | 2020-10-23 17:11:13 +0200 | [diff] [blame] | 1660 | |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1661 | if( target_usage_arg != -1 ) |
| 1662 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1663 | if( target_alg_arg != -1 ) |
| 1664 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1665 | if( target_alg2_arg != -1 ) |
| 1666 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1667 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1668 | |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1669 | /* Copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1670 | PSA_ASSERT( psa_copy_key( source_key, |
| 1671 | &target_attributes, &target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1672 | |
| 1673 | /* Destroy the source to ensure that this doesn't affect the target. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1674 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1675 | |
| 1676 | /* Test that the target slot has the expected content and policy. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1677 | PSA_ASSERT( psa_get_key_attributes( target_key, &target_attributes ) ); |
Gilles Peskine | ca25db9 | 2019-04-19 11:43:08 +0200 | [diff] [blame] | 1678 | TEST_EQUAL( psa_get_key_type( &source_attributes ), |
| 1679 | psa_get_key_type( &target_attributes ) ); |
| 1680 | TEST_EQUAL( psa_get_key_bits( &source_attributes ), |
| 1681 | psa_get_key_bits( &target_attributes ) ); |
| 1682 | TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) ); |
| 1683 | TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1684 | TEST_EQUAL( expected_alg2, |
| 1685 | psa_get_key_enrollment_algorithm( &target_attributes ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1686 | if( expected_usage & PSA_KEY_USAGE_EXPORT ) |
| 1687 | { |
| 1688 | size_t length; |
| 1689 | ASSERT_ALLOC( export_buffer, material->len ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1690 | PSA_ASSERT( psa_export_key( target_key, export_buffer, |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1691 | material->len, &length ) ); |
| 1692 | ASSERT_COMPARE( material->x, material->len, |
| 1693 | export_buffer, length ); |
| 1694 | } |
Steven Cooreman | b3ce815 | 2021-02-18 12:03:50 +0100 | [diff] [blame] | 1695 | |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1696 | if( !psa_key_lifetime_is_external( target_lifetime ) ) |
| 1697 | { |
| 1698 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg ) ) |
| 1699 | goto exit; |
| 1700 | if( ! mbedtls_test_psa_exercise_key( target_key, expected_usage, expected_alg2 ) ) |
| 1701 | goto exit; |
| 1702 | } |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1703 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1704 | PSA_ASSERT( psa_destroy_key( target_key ) ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1705 | |
| 1706 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1707 | /* |
| 1708 | * Source and target key attributes may have been returned by |
| 1709 | * psa_get_key_attributes() thus reset them as required. |
| 1710 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 1711 | psa_reset_key_attributes( &source_attributes ); |
| 1712 | psa_reset_key_attributes( &target_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 1713 | |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1714 | PSA_DONE( ); |
Gilles Peskine | 57ab721 | 2019-01-28 13:03:09 +0100 | [diff] [blame] | 1715 | mbedtls_free( export_buffer ); |
| 1716 | } |
| 1717 | /* END_CASE */ |
| 1718 | |
| 1719 | /* BEGIN_CASE */ |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1720 | void copy_fail( int source_usage_arg, |
| 1721 | int source_alg_arg, int source_alg2_arg, |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1722 | int source_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1723 | int type_arg, data_t *material, |
| 1724 | int target_type_arg, int target_bits_arg, |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1725 | int target_usage_arg, |
| 1726 | int target_alg_arg, int target_alg2_arg, |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1727 | int target_id_arg, int target_lifetime_arg, |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1728 | int expected_status_arg ) |
| 1729 | { |
| 1730 | psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 1731 | psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1732 | mbedtls_svc_key_id_t source_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 1733 | mbedtls_svc_key_id_t target_key = MBEDTLS_SVC_KEY_ID_INIT; |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1734 | 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] | 1735 | |
| 1736 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1737 | |
| 1738 | /* Prepare the source key. */ |
| 1739 | psa_set_key_usage_flags( &source_attributes, source_usage_arg ); |
| 1740 | psa_set_key_algorithm( &source_attributes, source_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1741 | psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1742 | psa_set_key_type( &source_attributes, type_arg ); |
Archana | 8a18036 | 2021-07-05 02:18:48 +0530 | [diff] [blame] | 1743 | psa_set_key_lifetime( &source_attributes, source_lifetime_arg ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 1744 | PSA_ASSERT( psa_import_key( &source_attributes, |
| 1745 | material->x, material->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1746 | &source_key ) ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1747 | |
| 1748 | /* Prepare the target attributes. */ |
Ronald Cron | 88a5546 | 2021-03-31 09:39:07 +0200 | [diff] [blame] | 1749 | psa_set_key_id( &target_attributes, key_id ); |
| 1750 | psa_set_key_lifetime( &target_attributes, target_lifetime_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1751 | psa_set_key_type( &target_attributes, target_type_arg ); |
| 1752 | psa_set_key_bits( &target_attributes, target_bits_arg ); |
| 1753 | psa_set_key_usage_flags( &target_attributes, target_usage_arg ); |
| 1754 | psa_set_key_algorithm( &target_attributes, target_alg_arg ); |
Gilles Peskine | bcdd44b | 2019-05-20 17:28:11 +0200 | [diff] [blame] | 1755 | psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1756 | |
| 1757 | /* Try to copy the key. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1758 | TEST_EQUAL( psa_copy_key( source_key, |
| 1759 | &target_attributes, &target_key ), |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1760 | expected_status_arg ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1761 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 1762 | PSA_ASSERT( psa_destroy_key( source_key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 1763 | |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1764 | exit: |
| 1765 | psa_reset_key_attributes( &source_attributes ); |
| 1766 | psa_reset_key_attributes( &target_attributes ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1767 | PSA_DONE( ); |
Gilles Peskine | 4a64464 | 2019-05-03 17:14:08 +0200 | [diff] [blame] | 1768 | } |
| 1769 | /* END_CASE */ |
| 1770 | |
| 1771 | /* BEGIN_CASE */ |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1772 | void hash_operation_init( ) |
| 1773 | { |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1774 | const uint8_t input[1] = { 0 }; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1775 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 1776 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 1777 | * though it's OK by the C standard. We could test for this, but we'd need |
| 1778 | * to supress the Clang warning for the test. */ |
| 1779 | psa_hash_operation_t func = psa_hash_operation_init( ); |
| 1780 | psa_hash_operation_t init = PSA_HASH_OPERATION_INIT; |
| 1781 | psa_hash_operation_t zero; |
| 1782 | |
| 1783 | memset( &zero, 0, sizeof( zero ) ); |
| 1784 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1785 | /* A freshly-initialized hash operation should not be usable. */ |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1786 | TEST_EQUAL( psa_hash_update( &func, input, sizeof( input ) ), |
| 1787 | PSA_ERROR_BAD_STATE ); |
| 1788 | TEST_EQUAL( psa_hash_update( &init, input, sizeof( input ) ), |
| 1789 | PSA_ERROR_BAD_STATE ); |
| 1790 | TEST_EQUAL( psa_hash_update( &zero, input, sizeof( input ) ), |
| 1791 | PSA_ERROR_BAD_STATE ); |
| 1792 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 1793 | /* A default hash operation should be abortable without error. */ |
| 1794 | PSA_ASSERT( psa_hash_abort( &func ) ); |
| 1795 | PSA_ASSERT( psa_hash_abort( &init ) ); |
| 1796 | PSA_ASSERT( psa_hash_abort( &zero ) ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1797 | } |
| 1798 | /* END_CASE */ |
| 1799 | |
| 1800 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1801 | void hash_setup( int alg_arg, |
| 1802 | int expected_status_arg ) |
| 1803 | { |
| 1804 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 1805 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1806 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1807 | psa_status_t status; |
| 1808 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1809 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1810 | |
Gilles Peskine | da8191d1c | 2018-07-08 19:46:38 +0200 | [diff] [blame] | 1811 | status = psa_hash_setup( &operation, alg ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 1812 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1813 | |
Gilles Peskine | 9e0a4a5 | 2019-02-25 22:11:18 +0100 | [diff] [blame] | 1814 | /* Whether setup succeeded or failed, abort must succeed. */ |
| 1815 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1816 | |
| 1817 | /* If setup failed, reproduce the failure, so as to |
| 1818 | * test the resulting state of the operation object. */ |
| 1819 | if( status != PSA_SUCCESS ) |
| 1820 | TEST_EQUAL( psa_hash_setup( &operation, alg ), status ); |
| 1821 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 1822 | /* Now the operation object should be reusable. */ |
| 1823 | #if defined(KNOWN_SUPPORTED_HASH_ALG) |
| 1824 | PSA_ASSERT( psa_hash_setup( &operation, KNOWN_SUPPORTED_HASH_ALG ) ); |
| 1825 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1826 | #endif |
| 1827 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1828 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 1829 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 1830 | } |
| 1831 | /* END_CASE */ |
| 1832 | |
| 1833 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1834 | void hash_compute_fail( int alg_arg, data_t *input, |
| 1835 | int output_size_arg, int expected_status_arg ) |
| 1836 | { |
| 1837 | psa_algorithm_t alg = alg_arg; |
| 1838 | uint8_t *output = NULL; |
| 1839 | size_t output_size = output_size_arg; |
| 1840 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1841 | psa_status_t expected_status = expected_status_arg; |
| 1842 | psa_status_t status; |
| 1843 | |
| 1844 | ASSERT_ALLOC( output, output_size ); |
| 1845 | |
| 1846 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1847 | |
| 1848 | status = psa_hash_compute( alg, input->x, input->len, |
| 1849 | output, output_size, &output_length ); |
| 1850 | TEST_EQUAL( status, expected_status ); |
| 1851 | TEST_ASSERT( output_length <= output_size ); |
| 1852 | |
| 1853 | exit: |
| 1854 | mbedtls_free( output ); |
| 1855 | PSA_DONE( ); |
| 1856 | } |
| 1857 | /* END_CASE */ |
| 1858 | |
| 1859 | /* BEGIN_CASE */ |
Gilles Peskine | 88e0846 | 2020-01-28 20:43:00 +0100 | [diff] [blame] | 1860 | void hash_compare_fail( int alg_arg, data_t *input, |
| 1861 | data_t *reference_hash, |
| 1862 | int expected_status_arg ) |
| 1863 | { |
| 1864 | psa_algorithm_t alg = alg_arg; |
| 1865 | psa_status_t expected_status = expected_status_arg; |
| 1866 | psa_status_t status; |
| 1867 | |
| 1868 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1869 | |
| 1870 | status = psa_hash_compare( alg, input->x, input->len, |
| 1871 | reference_hash->x, reference_hash->len ); |
| 1872 | TEST_EQUAL( status, expected_status ); |
| 1873 | |
| 1874 | exit: |
| 1875 | PSA_DONE( ); |
| 1876 | } |
| 1877 | /* END_CASE */ |
| 1878 | |
| 1879 | /* BEGIN_CASE */ |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1880 | void hash_compute_compare( int alg_arg, data_t *input, |
| 1881 | data_t *expected_output ) |
| 1882 | { |
| 1883 | psa_algorithm_t alg = alg_arg; |
| 1884 | uint8_t output[PSA_HASH_MAX_SIZE + 1]; |
| 1885 | size_t output_length = INVALID_EXPORT_LENGTH; |
| 1886 | size_t i; |
| 1887 | |
| 1888 | PSA_ASSERT( psa_crypto_init( ) ); |
| 1889 | |
| 1890 | /* Compute with tight buffer */ |
| 1891 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1892 | output, PSA_HASH_LENGTH( alg ), |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1893 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1894 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1895 | ASSERT_COMPARE( output, output_length, |
| 1896 | expected_output->x, expected_output->len ); |
| 1897 | |
| 1898 | /* Compute with larger buffer */ |
| 1899 | PSA_ASSERT( psa_hash_compute( alg, input->x, input->len, |
| 1900 | output, sizeof( output ), |
| 1901 | &output_length ) ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 1902 | TEST_EQUAL( output_length, PSA_HASH_LENGTH( alg ) ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1903 | ASSERT_COMPARE( output, output_length, |
| 1904 | expected_output->x, expected_output->len ); |
| 1905 | |
| 1906 | /* Compare with correct hash */ |
| 1907 | PSA_ASSERT( psa_hash_compare( alg, input->x, input->len, |
| 1908 | output, output_length ) ); |
| 1909 | |
| 1910 | /* Compare with trailing garbage */ |
| 1911 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1912 | output, output_length + 1 ), |
| 1913 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1914 | |
| 1915 | /* Compare with truncated hash */ |
| 1916 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1917 | output, output_length - 1 ), |
| 1918 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1919 | |
| 1920 | /* Compare with corrupted value */ |
| 1921 | for( i = 0; i < output_length; i++ ) |
| 1922 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 1923 | mbedtls_test_set_step( i ); |
Gilles Peskine | 0a749c8 | 2019-11-28 19:33:58 +0100 | [diff] [blame] | 1924 | output[i] ^= 1; |
| 1925 | TEST_EQUAL( psa_hash_compare( alg, input->x, input->len, |
| 1926 | output, output_length ), |
| 1927 | PSA_ERROR_INVALID_SIGNATURE ); |
| 1928 | output[i] ^= 1; |
| 1929 | } |
| 1930 | |
| 1931 | exit: |
| 1932 | PSA_DONE( ); |
| 1933 | } |
| 1934 | /* END_CASE */ |
| 1935 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 1936 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1937 | void hash_bad_order( ) |
| 1938 | { |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1939 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1940 | unsigned char input[] = ""; |
| 1941 | /* SHA-256 hash of an empty string */ |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1942 | const unsigned char valid_hash[] = { |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1943 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 1944 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 1945 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }; |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1946 | unsigned char hash[sizeof(valid_hash)] = { 0 }; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1947 | size_t hash_len; |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 1948 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1949 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 1950 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1951 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1952 | /* Call setup twice in a row. */ |
| 1953 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1954 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1955 | TEST_EQUAL( psa_hash_setup( &operation, alg ), |
| 1956 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1957 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1958 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1959 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 1960 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1961 | /* Call update without calling setup beforehand. */ |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 1962 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1963 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1964 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1965 | |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1966 | /* Check that update calls abort on error. */ |
| 1967 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 6f71058 | 2021-06-24 18:14:52 +0100 | [diff] [blame] | 1968 | operation.id = UINT_MAX; |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 1969 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
| 1970 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
| 1971 | PSA_ERROR_BAD_STATE ); |
| 1972 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 1973 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1974 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 1975 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1976 | /* Call update after finish. */ |
| 1977 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1978 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1979 | hash, sizeof( hash ), &hash_len ) ); |
| 1980 | TEST_EQUAL( psa_hash_update( &operation, input, sizeof( input ) ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 1981 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1982 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 1983 | |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 1984 | /* Call verify without calling setup beforehand. */ |
| 1985 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1986 | valid_hash, sizeof( valid_hash ) ), |
| 1987 | PSA_ERROR_BAD_STATE ); |
| 1988 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1989 | |
| 1990 | /* Call verify after finish. */ |
| 1991 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 1992 | PSA_ASSERT( psa_hash_finish( &operation, |
| 1993 | hash, sizeof( hash ), &hash_len ) ); |
| 1994 | TEST_EQUAL( psa_hash_verify( &operation, |
| 1995 | valid_hash, sizeof( valid_hash ) ), |
| 1996 | PSA_ERROR_BAD_STATE ); |
| 1997 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 1998 | |
| 1999 | /* Call verify twice in a row. */ |
| 2000 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2001 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2002 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2003 | valid_hash, sizeof( valid_hash ) ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2004 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2005 | TEST_EQUAL( psa_hash_verify( &operation, |
| 2006 | valid_hash, sizeof( valid_hash ) ), |
| 2007 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2008 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2009 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2010 | |
| 2011 | /* Call finish without calling setup beforehand. */ |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2012 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2013 | hash, sizeof( hash ), &hash_len ), |
Jaeden Amero | a0f625a | 2019-02-15 13:52:25 +0000 | [diff] [blame] | 2014 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 11aa7ee | 2019-02-19 11:44:55 +0000 | [diff] [blame] | 2015 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2016 | |
| 2017 | /* Call finish twice in a row. */ |
| 2018 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2019 | PSA_ASSERT( psa_hash_finish( &operation, |
| 2020 | hash, sizeof( hash ), &hash_len ) ); |
| 2021 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2022 | hash, sizeof( hash ), &hash_len ), |
| 2023 | PSA_ERROR_BAD_STATE ); |
| 2024 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2025 | |
| 2026 | /* Call finish after calling verify. */ |
| 2027 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
| 2028 | PSA_ASSERT( psa_hash_verify( &operation, |
| 2029 | valid_hash, sizeof( valid_hash ) ) ); |
| 2030 | TEST_EQUAL( psa_hash_finish( &operation, |
| 2031 | hash, sizeof( hash ), &hash_len ), |
| 2032 | PSA_ERROR_BAD_STATE ); |
| 2033 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2034 | |
| 2035 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2036 | PSA_DONE( ); |
itayzafrir | f86548d | 2018-11-01 10:44:32 +0200 | [diff] [blame] | 2037 | } |
| 2038 | /* END_CASE */ |
| 2039 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2040 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2041 | void hash_verify_bad_args( ) |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2042 | { |
| 2043 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2044 | /* SHA-256 hash of an empty string with 2 extra bytes (0xaa and 0xbb) |
| 2045 | * appended to it */ |
| 2046 | unsigned char hash[] = { |
| 2047 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, |
| 2048 | 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 2049 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55, 0xaa, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2050 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2051 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2052 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2053 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2054 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2055 | /* psa_hash_verify with a smaller hash than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2056 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2057 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2058 | TEST_EQUAL( psa_hash_verify( &operation, hash, expected_size - 1 ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2059 | PSA_ERROR_INVALID_SIGNATURE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2060 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
| 2061 | PSA_ASSERT( psa_hash_abort( &operation ) ); |
| 2062 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2063 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2064 | /* psa_hash_verify with a non-matching hash */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2065 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2066 | TEST_EQUAL( psa_hash_verify( &operation, hash + 1, expected_size ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2067 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2068 | |
itayzafrir | 27e6945 | 2018-11-01 14:26:34 +0200 | [diff] [blame] | 2069 | /* psa_hash_verify with a hash longer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2070 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2071 | TEST_EQUAL( psa_hash_verify( &operation, hash, sizeof( hash ) ), |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2072 | PSA_ERROR_INVALID_SIGNATURE ); |
itayzafrir | 4271df9 | 2018-10-24 18:16:19 +0300 | [diff] [blame] | 2073 | |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2074 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2075 | PSA_DONE( ); |
itayzafrir | ec93d30 | 2018-10-18 18:01:10 +0300 | [diff] [blame] | 2076 | } |
| 2077 | /* END_CASE */ |
| 2078 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2079 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2080 | void hash_finish_bad_args( ) |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2081 | { |
| 2082 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
itayzafrir | b2dd5ed | 2018-11-01 11:58:59 +0200 | [diff] [blame] | 2083 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2084 | size_t expected_size = PSA_HASH_LENGTH( alg ); |
Jaeden Amero | 6a25b41 | 2019-01-04 11:47:44 +0000 | [diff] [blame] | 2085 | psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT; |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2086 | size_t hash_len; |
| 2087 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2088 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2089 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2090 | /* psa_hash_finish with a smaller hash buffer than expected */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2091 | PSA_ASSERT( psa_hash_setup( &operation, alg ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2092 | TEST_EQUAL( psa_hash_finish( &operation, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 2093 | hash, expected_size - 1, &hash_len ), |
| 2094 | PSA_ERROR_BUFFER_TOO_SMALL ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2095 | |
| 2096 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2097 | PSA_DONE( ); |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2098 | } |
| 2099 | /* END_CASE */ |
| 2100 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2101 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2102 | void hash_clone_source_state( ) |
| 2103 | { |
| 2104 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2105 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2106 | psa_hash_operation_t op_source = PSA_HASH_OPERATION_INIT; |
| 2107 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2108 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2109 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2110 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2111 | size_t hash_len; |
| 2112 | |
| 2113 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2114 | PSA_ASSERT( psa_hash_setup( &op_source, alg ) ); |
| 2115 | |
| 2116 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2117 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2118 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2119 | hash, sizeof( hash ), &hash_len ) ); |
| 2120 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2121 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2122 | |
| 2123 | TEST_EQUAL( psa_hash_clone( &op_source, &op_setup ), |
| 2124 | PSA_ERROR_BAD_STATE ); |
| 2125 | |
| 2126 | PSA_ASSERT( psa_hash_clone( &op_source, &op_init ) ); |
| 2127 | PSA_ASSERT( psa_hash_finish( &op_init, |
| 2128 | hash, sizeof( hash ), &hash_len ) ); |
| 2129 | PSA_ASSERT( psa_hash_clone( &op_source, &op_finished ) ); |
| 2130 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2131 | hash, sizeof( hash ), &hash_len ) ); |
| 2132 | PSA_ASSERT( psa_hash_clone( &op_source, &op_aborted ) ); |
| 2133 | PSA_ASSERT( psa_hash_finish( &op_aborted, |
| 2134 | hash, sizeof( hash ), &hash_len ) ); |
| 2135 | |
| 2136 | exit: |
| 2137 | psa_hash_abort( &op_source ); |
| 2138 | psa_hash_abort( &op_init ); |
| 2139 | psa_hash_abort( &op_setup ); |
| 2140 | psa_hash_abort( &op_finished ); |
| 2141 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2142 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2143 | } |
| 2144 | /* END_CASE */ |
| 2145 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2146 | /* BEGIN_CASE depends_on:PSA_WANT_ALG_SHA_256 */ |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2147 | void hash_clone_target_state( ) |
| 2148 | { |
| 2149 | psa_algorithm_t alg = PSA_ALG_SHA_256; |
| 2150 | unsigned char hash[PSA_HASH_MAX_SIZE]; |
| 2151 | psa_hash_operation_t op_init = PSA_HASH_OPERATION_INIT; |
| 2152 | psa_hash_operation_t op_setup = PSA_HASH_OPERATION_INIT; |
| 2153 | psa_hash_operation_t op_finished = PSA_HASH_OPERATION_INIT; |
| 2154 | psa_hash_operation_t op_aborted = PSA_HASH_OPERATION_INIT; |
| 2155 | psa_hash_operation_t op_target = PSA_HASH_OPERATION_INIT; |
| 2156 | size_t hash_len; |
| 2157 | |
| 2158 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2159 | |
| 2160 | PSA_ASSERT( psa_hash_setup( &op_setup, alg ) ); |
| 2161 | PSA_ASSERT( psa_hash_setup( &op_finished, alg ) ); |
| 2162 | PSA_ASSERT( psa_hash_finish( &op_finished, |
| 2163 | hash, sizeof( hash ), &hash_len ) ); |
| 2164 | PSA_ASSERT( psa_hash_setup( &op_aborted, alg ) ); |
| 2165 | PSA_ASSERT( psa_hash_abort( &op_aborted ) ); |
| 2166 | |
| 2167 | PSA_ASSERT( psa_hash_clone( &op_setup, &op_target ) ); |
| 2168 | PSA_ASSERT( psa_hash_finish( &op_target, |
| 2169 | hash, sizeof( hash ), &hash_len ) ); |
| 2170 | |
| 2171 | TEST_EQUAL( psa_hash_clone( &op_init, &op_target ), PSA_ERROR_BAD_STATE ); |
| 2172 | TEST_EQUAL( psa_hash_clone( &op_finished, &op_target ), |
| 2173 | PSA_ERROR_BAD_STATE ); |
| 2174 | TEST_EQUAL( psa_hash_clone( &op_aborted, &op_target ), |
| 2175 | PSA_ERROR_BAD_STATE ); |
| 2176 | |
| 2177 | exit: |
| 2178 | psa_hash_abort( &op_target ); |
| 2179 | psa_hash_abort( &op_init ); |
| 2180 | psa_hash_abort( &op_setup ); |
| 2181 | psa_hash_abort( &op_finished ); |
| 2182 | psa_hash_abort( &op_aborted ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2183 | PSA_DONE( ); |
Gilles Peskine | ebb2c3e | 2019-01-19 12:03:41 +0100 | [diff] [blame] | 2184 | } |
| 2185 | /* END_CASE */ |
| 2186 | |
itayzafrir | 5802832 | 2018-10-25 10:22:01 +0300 | [diff] [blame] | 2187 | /* BEGIN_CASE */ |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2188 | void mac_operation_init( ) |
| 2189 | { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2190 | const uint8_t input[1] = { 0 }; |
| 2191 | |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2192 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2193 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2194 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2195 | * to supress the Clang warning for the test. */ |
| 2196 | psa_mac_operation_t func = psa_mac_operation_init( ); |
| 2197 | psa_mac_operation_t init = PSA_MAC_OPERATION_INIT; |
| 2198 | psa_mac_operation_t zero; |
| 2199 | |
| 2200 | memset( &zero, 0, sizeof( zero ) ); |
| 2201 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2202 | /* A freshly-initialized MAC operation should not be usable. */ |
| 2203 | TEST_EQUAL( psa_mac_update( &func, |
| 2204 | input, sizeof( input ) ), |
| 2205 | PSA_ERROR_BAD_STATE ); |
| 2206 | TEST_EQUAL( psa_mac_update( &init, |
| 2207 | input, sizeof( input ) ), |
| 2208 | PSA_ERROR_BAD_STATE ); |
| 2209 | TEST_EQUAL( psa_mac_update( &zero, |
| 2210 | input, sizeof( input ) ), |
| 2211 | PSA_ERROR_BAD_STATE ); |
| 2212 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2213 | /* A default MAC operation should be abortable without error. */ |
| 2214 | PSA_ASSERT( psa_mac_abort( &func ) ); |
| 2215 | PSA_ASSERT( psa_mac_abort( &init ) ); |
| 2216 | PSA_ASSERT( psa_mac_abort( &zero ) ); |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2217 | } |
| 2218 | /* END_CASE */ |
| 2219 | |
| 2220 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2221 | void mac_setup( int key_type_arg, |
| 2222 | data_t *key, |
| 2223 | int alg_arg, |
| 2224 | int expected_status_arg ) |
| 2225 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2226 | psa_key_type_t key_type = key_type_arg; |
| 2227 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2228 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2229 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2230 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 2231 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2232 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2233 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2234 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2235 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2236 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2237 | if( ! exercise_mac_setup( key_type, key->x, key->len, alg, |
| 2238 | &operation, &status ) ) |
| 2239 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2240 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2241 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2242 | /* The operation object should be reusable. */ |
| 2243 | #if defined(KNOWN_SUPPORTED_MAC_ALG) |
| 2244 | if( ! exercise_mac_setup( KNOWN_SUPPORTED_MAC_KEY_TYPE, |
| 2245 | smoke_test_key_data, |
| 2246 | sizeof( smoke_test_key_data ), |
| 2247 | KNOWN_SUPPORTED_MAC_ALG, |
| 2248 | &operation, &status ) ) |
| 2249 | goto exit; |
| 2250 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2251 | #endif |
| 2252 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2253 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2254 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2255 | } |
| 2256 | /* END_CASE */ |
| 2257 | |
Gilles Peskine | d6dc40c | 2021-01-12 12:55:31 +0100 | [diff] [blame] | 2258 | /* 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] | 2259 | void mac_bad_order( ) |
| 2260 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2261 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2262 | psa_key_type_t key_type = PSA_KEY_TYPE_HMAC; |
| 2263 | psa_algorithm_t alg = PSA_ALG_HMAC(PSA_ALG_SHA_256); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2264 | const uint8_t key_data[] = { |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2265 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2266 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2267 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa }; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2268 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2269 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
| 2270 | uint8_t sign_mac[PSA_MAC_MAX_SIZE + 10] = { 0 }; |
| 2271 | size_t sign_mac_length = 0; |
| 2272 | const uint8_t input[] = { 0xbb, 0xbb, 0xbb, 0xbb }; |
| 2273 | const uint8_t verify_mac[] = { |
| 2274 | 0x74, 0x65, 0x93, 0x8c, 0xeb, 0x1d, 0xb3, 0x76, 0x5a, 0x38, 0xe7, 0xdd, |
| 2275 | 0x85, 0xc5, 0xad, 0x4f, 0x07, 0xe7, 0xd5, 0xb2, 0x64, 0xf0, 0x1a, 0x1a, |
| 2276 | 0x2c, 0xf9, 0x18, 0xca, 0x59, 0x7e, 0x5d, 0xf6 }; |
| 2277 | |
| 2278 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2279 | 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] | 2280 | psa_set_key_algorithm( &attributes, alg ); |
| 2281 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2282 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2283 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2284 | &key ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2285 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2286 | /* Call update without calling setup beforehand. */ |
| 2287 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2288 | PSA_ERROR_BAD_STATE ); |
| 2289 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2290 | |
| 2291 | /* Call sign finish without calling setup beforehand. */ |
| 2292 | TEST_EQUAL( psa_mac_sign_finish( &operation, sign_mac, sizeof( sign_mac ), |
| 2293 | &sign_mac_length), |
| 2294 | PSA_ERROR_BAD_STATE ); |
| 2295 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2296 | |
| 2297 | /* Call verify finish without calling setup beforehand. */ |
| 2298 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2299 | verify_mac, sizeof( verify_mac ) ), |
| 2300 | PSA_ERROR_BAD_STATE ); |
| 2301 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2302 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2303 | /* Call setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2304 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2305 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2306 | TEST_EQUAL( psa_mac_sign_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2307 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2308 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2309 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2310 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2311 | |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2312 | /* Call update after sign finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2313 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2314 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2315 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2316 | sign_mac, sizeof( sign_mac ), |
| 2317 | &sign_mac_length ) ); |
| 2318 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2319 | PSA_ERROR_BAD_STATE ); |
| 2320 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2321 | |
| 2322 | /* Call update after verify finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2323 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2324 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2325 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2326 | verify_mac, sizeof( verify_mac ) ) ); |
| 2327 | TEST_EQUAL( psa_mac_update( &operation, input, sizeof( input ) ), |
| 2328 | PSA_ERROR_BAD_STATE ); |
| 2329 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2330 | |
| 2331 | /* Call sign finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2332 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2333 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2334 | PSA_ASSERT( psa_mac_sign_finish( &operation, |
| 2335 | sign_mac, sizeof( sign_mac ), |
| 2336 | &sign_mac_length ) ); |
| 2337 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2338 | sign_mac, sizeof( sign_mac ), |
| 2339 | &sign_mac_length ), |
| 2340 | PSA_ERROR_BAD_STATE ); |
| 2341 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2342 | |
| 2343 | /* Call verify finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2344 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2345 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
| 2346 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2347 | verify_mac, sizeof( verify_mac ) ) ); |
| 2348 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2349 | verify_mac, sizeof( verify_mac ) ), |
| 2350 | PSA_ERROR_BAD_STATE ); |
| 2351 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2352 | |
| 2353 | /* Setup sign but try verify. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2354 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2355 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2356 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2357 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2358 | verify_mac, sizeof( verify_mac ) ), |
| 2359 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2360 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2361 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2362 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2363 | |
| 2364 | /* Setup verify but try sign. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2365 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2366 | PSA_ASSERT( psa_mac_update( &operation, input, sizeof( input ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2367 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2368 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2369 | sign_mac, sizeof( sign_mac ), |
| 2370 | &sign_mac_length ), |
| 2371 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2372 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 252ef28 | 2019-02-15 14:05:35 +0000 | [diff] [blame] | 2373 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2374 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2375 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2376 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2377 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2378 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2379 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2380 | } |
| 2381 | /* END_CASE */ |
| 2382 | |
| 2383 | /* BEGIN_CASE */ |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2384 | void mac_sign( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2385 | data_t *key_data, |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2386 | int alg_arg, |
| 2387 | data_t *input, |
| 2388 | data_t *expected_mac ) |
| 2389 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2390 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2391 | psa_key_type_t key_type = key_type_arg; |
| 2392 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2393 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2394 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2395 | uint8_t *actual_mac = NULL; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2396 | size_t mac_buffer_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2397 | 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] | 2398 | size_t mac_length = 0; |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2399 | const size_t output_sizes_to_test[] = { |
| 2400 | 0, |
| 2401 | 1, |
| 2402 | expected_mac->len - 1, |
| 2403 | expected_mac->len, |
| 2404 | expected_mac->len + 1, |
| 2405 | }; |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2406 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2407 | TEST_ASSERT( mac_buffer_size <= PSA_MAC_MAX_SIZE ); |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2408 | /* We expect PSA_MAC_LENGTH to be exact. */ |
Gilles Peskine | 3d404d6 | 2020-08-25 23:47:36 +0200 | [diff] [blame] | 2409 | TEST_ASSERT( expected_mac->len == mac_buffer_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2410 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2411 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2412 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2413 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2414 | psa_set_key_algorithm( &attributes, alg ); |
| 2415 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2416 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2417 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2418 | &key ) ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2419 | |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2420 | for( size_t i = 0; i < ARRAY_LENGTH( output_sizes_to_test ); i++ ) |
| 2421 | { |
| 2422 | const size_t output_size = output_sizes_to_test[i]; |
| 2423 | psa_status_t expected_status = |
| 2424 | ( output_size >= expected_mac->len ? PSA_SUCCESS : |
| 2425 | PSA_ERROR_BUFFER_TOO_SMALL ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2426 | |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2427 | mbedtls_test_set_step( output_size ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2428 | ASSERT_ALLOC( actual_mac, output_size ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2429 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2430 | /* Calculate the MAC, one-shot case. */ |
| 2431 | TEST_EQUAL( psa_mac_compute( key, alg, |
| 2432 | input->x, input->len, |
| 2433 | actual_mac, output_size, &mac_length ), |
| 2434 | expected_status ); |
| 2435 | if( expected_status == PSA_SUCCESS ) |
| 2436 | { |
| 2437 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2438 | actual_mac, mac_length ); |
| 2439 | } |
| 2440 | |
| 2441 | if( output_size > 0 ) |
| 2442 | memset( actual_mac, 0, output_size ); |
| 2443 | |
| 2444 | /* Calculate the MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2445 | PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) ); |
Gilles Peskine | 8b356b5 | 2020-08-25 23:44:59 +0200 | [diff] [blame] | 2446 | PSA_ASSERT( psa_mac_update( &operation, |
| 2447 | input->x, input->len ) ); |
| 2448 | TEST_EQUAL( psa_mac_sign_finish( &operation, |
| 2449 | actual_mac, output_size, |
| 2450 | &mac_length ), |
| 2451 | expected_status ); |
| 2452 | PSA_ASSERT( psa_mac_abort( &operation ) ); |
| 2453 | |
| 2454 | if( expected_status == PSA_SUCCESS ) |
| 2455 | { |
| 2456 | ASSERT_COMPARE( expected_mac->x, expected_mac->len, |
| 2457 | actual_mac, mac_length ); |
| 2458 | } |
| 2459 | mbedtls_free( actual_mac ); |
| 2460 | actual_mac = NULL; |
| 2461 | } |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2462 | |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2463 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2464 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2465 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2466 | PSA_DONE( ); |
Gilles Peskine | 5e65cec | 2020-08-25 23:38:39 +0200 | [diff] [blame] | 2467 | mbedtls_free( actual_mac ); |
Gilles Peskine | a7aa442 | 2018-08-14 15:17:54 +0200 | [diff] [blame] | 2468 | } |
| 2469 | /* END_CASE */ |
| 2470 | |
| 2471 | /* BEGIN_CASE */ |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2472 | void mac_verify( int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2473 | data_t *key_data, |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2474 | int alg_arg, |
| 2475 | data_t *input, |
| 2476 | data_t *expected_mac ) |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2477 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2478 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2479 | psa_key_type_t key_type = key_type_arg; |
| 2480 | psa_algorithm_t alg = alg_arg; |
Jaeden Amero | 769ce27 | 2019-01-04 11:48:03 +0000 | [diff] [blame] | 2481 | psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2482 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2483 | uint8_t *perturbed_mac = NULL; |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2484 | |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 2485 | TEST_ASSERT( expected_mac->len <= PSA_MAC_MAX_SIZE ); |
| 2486 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2487 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2488 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 2489 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2490 | psa_set_key_algorithm( &attributes, alg ); |
| 2491 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | 6df908f | 2018-04-02 08:34:15 -0700 | [diff] [blame] | 2492 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2493 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2494 | &key ) ); |
Gilles Peskine | c0ec972 | 2018-06-18 17:03:37 +0200 | [diff] [blame] | 2495 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2496 | /* Verify correct MAC, one-shot case. */ |
| 2497 | PSA_ASSERT( psa_mac_verify( key, alg, input->x, input->len, |
| 2498 | expected_mac->x, expected_mac->len ) ); |
| 2499 | |
| 2500 | /* Verify correct MAC, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2501 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2502 | PSA_ASSERT( psa_mac_update( &operation, |
| 2503 | input->x, input->len ) ); |
| 2504 | PSA_ASSERT( psa_mac_verify_finish( &operation, |
| 2505 | expected_mac->x, |
| 2506 | expected_mac->len ) ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2507 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2508 | /* Test a MAC that's too short, one-shot case. */ |
| 2509 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2510 | input->x, input->len, |
| 2511 | expected_mac->x, |
| 2512 | expected_mac->len - 1 ), |
| 2513 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2514 | |
| 2515 | /* Test a MAC that's too short, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2516 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2517 | PSA_ASSERT( psa_mac_update( &operation, |
| 2518 | input->x, input->len ) ); |
| 2519 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2520 | expected_mac->x, |
| 2521 | expected_mac->len - 1 ), |
| 2522 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2523 | |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2524 | /* Test a MAC that's too long, one-shot case. */ |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2525 | ASSERT_ALLOC( perturbed_mac, expected_mac->len + 1 ); |
| 2526 | memcpy( perturbed_mac, expected_mac->x, expected_mac->len ); |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2527 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2528 | input->x, input->len, |
| 2529 | perturbed_mac, expected_mac->len + 1 ), |
| 2530 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2531 | |
| 2532 | /* Test a MAC that's too long, multi-part case. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2533 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2534 | PSA_ASSERT( psa_mac_update( &operation, |
| 2535 | input->x, input->len ) ); |
| 2536 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2537 | perturbed_mac, |
| 2538 | expected_mac->len + 1 ), |
| 2539 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2540 | |
| 2541 | /* Test changing one byte. */ |
| 2542 | for( size_t i = 0; i < expected_mac->len; i++ ) |
| 2543 | { |
Chris Jones | 9634bb1 | 2021-01-20 15:56:42 +0000 | [diff] [blame] | 2544 | mbedtls_test_set_step( i ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2545 | perturbed_mac[i] ^= 1; |
gabor-mezei-arm | 534bb99 | 2021-03-01 15:35:48 +0100 | [diff] [blame] | 2546 | |
| 2547 | TEST_EQUAL( psa_mac_verify( key, alg, |
| 2548 | input->x, input->len, |
| 2549 | perturbed_mac, expected_mac->len ), |
| 2550 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2551 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2552 | PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2553 | PSA_ASSERT( psa_mac_update( &operation, |
| 2554 | input->x, input->len ) ); |
| 2555 | TEST_EQUAL( psa_mac_verify_finish( &operation, |
| 2556 | perturbed_mac, |
| 2557 | expected_mac->len ), |
| 2558 | PSA_ERROR_INVALID_SIGNATURE ); |
| 2559 | perturbed_mac[i] ^= 1; |
| 2560 | } |
| 2561 | |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2562 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2563 | psa_mac_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2564 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2565 | PSA_DONE( ); |
Gilles Peskine | 29c4a6c | 2020-08-26 00:01:39 +0200 | [diff] [blame] | 2566 | mbedtls_free( perturbed_mac ); |
Gilles Peskine | 8c9def3 | 2018-02-08 10:02:12 +0100 | [diff] [blame] | 2567 | } |
| 2568 | /* END_CASE */ |
| 2569 | |
| 2570 | /* BEGIN_CASE */ |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2571 | void cipher_operation_init( ) |
| 2572 | { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2573 | const uint8_t input[1] = { 0 }; |
| 2574 | unsigned char output[1] = { 0 }; |
| 2575 | size_t output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2576 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 2577 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 2578 | * though it's OK by the C standard. We could test for this, but we'd need |
| 2579 | * to supress the Clang warning for the test. */ |
| 2580 | psa_cipher_operation_t func = psa_cipher_operation_init( ); |
| 2581 | psa_cipher_operation_t init = PSA_CIPHER_OPERATION_INIT; |
| 2582 | psa_cipher_operation_t zero; |
| 2583 | |
| 2584 | memset( &zero, 0, sizeof( zero ) ); |
| 2585 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2586 | /* A freshly-initialized cipher operation should not be usable. */ |
| 2587 | TEST_EQUAL( psa_cipher_update( &func, |
| 2588 | input, sizeof( input ), |
| 2589 | output, sizeof( output ), |
| 2590 | &output_length ), |
| 2591 | PSA_ERROR_BAD_STATE ); |
| 2592 | TEST_EQUAL( psa_cipher_update( &init, |
| 2593 | input, sizeof( input ), |
| 2594 | output, sizeof( output ), |
| 2595 | &output_length ), |
| 2596 | PSA_ERROR_BAD_STATE ); |
| 2597 | TEST_EQUAL( psa_cipher_update( &zero, |
| 2598 | input, sizeof( input ), |
| 2599 | output, sizeof( output ), |
| 2600 | &output_length ), |
| 2601 | PSA_ERROR_BAD_STATE ); |
| 2602 | |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 2603 | /* A default cipher operation should be abortable without error. */ |
| 2604 | PSA_ASSERT( psa_cipher_abort( &func ) ); |
| 2605 | PSA_ASSERT( psa_cipher_abort( &init ) ); |
| 2606 | PSA_ASSERT( psa_cipher_abort( &zero ) ); |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2607 | } |
| 2608 | /* END_CASE */ |
| 2609 | |
| 2610 | /* BEGIN_CASE */ |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2611 | void cipher_setup( int key_type_arg, |
| 2612 | data_t *key, |
| 2613 | int alg_arg, |
| 2614 | int expected_status_arg ) |
| 2615 | { |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2616 | psa_key_type_t key_type = key_type_arg; |
| 2617 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2618 | psa_status_t expected_status = expected_status_arg; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 2619 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2620 | psa_status_t status; |
Gilles Peskine | 612ffd2 | 2021-01-20 18:51:00 +0100 | [diff] [blame] | 2621 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2622 | const uint8_t smoke_test_key_data[16] = "kkkkkkkkkkkkkkkk"; |
| 2623 | #endif |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2624 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 2625 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2626 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2627 | if( ! exercise_cipher_setup( key_type, key->x, key->len, alg, |
| 2628 | &operation, &status ) ) |
| 2629 | goto exit; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 2630 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2631 | |
Gilles Peskine | f426e0f | 2019-02-25 17:42:03 +0100 | [diff] [blame] | 2632 | /* The operation object should be reusable. */ |
| 2633 | #if defined(KNOWN_SUPPORTED_CIPHER_ALG) |
| 2634 | if( ! exercise_cipher_setup( KNOWN_SUPPORTED_CIPHER_KEY_TYPE, |
| 2635 | smoke_test_key_data, |
| 2636 | sizeof( smoke_test_key_data ), |
| 2637 | KNOWN_SUPPORTED_CIPHER_ALG, |
| 2638 | &operation, &status ) ) |
| 2639 | goto exit; |
| 2640 | TEST_EQUAL( status, PSA_SUCCESS ); |
| 2641 | #endif |
| 2642 | |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2643 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2644 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2645 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2646 | } |
| 2647 | /* END_CASE */ |
| 2648 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 2649 | /* 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] | 2650 | void cipher_bad_order( ) |
| 2651 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2652 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2653 | psa_key_type_t key_type = PSA_KEY_TYPE_AES; |
| 2654 | psa_algorithm_t alg = PSA_ALG_CBC_PKCS7; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2655 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2656 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2657 | 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] | 2658 | const uint8_t key_data[] = { |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2659 | 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, |
| 2660 | 0xaa, 0xaa, 0xaa, 0xaa }; |
| 2661 | const uint8_t text[] = { |
| 2662 | 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, |
| 2663 | 0xbb, 0xbb, 0xbb, 0xbb }; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 2664 | 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] | 2665 | size_t length = 0; |
| 2666 | |
| 2667 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 2668 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 2669 | psa_set_key_algorithm( &attributes, alg ); |
| 2670 | psa_set_key_type( &attributes, key_type ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2671 | PSA_ASSERT( psa_import_key( &attributes, key_data, sizeof( key_data ), |
| 2672 | &key ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2673 | |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2674 | /* Call encrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2675 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2676 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2677 | TEST_EQUAL( psa_cipher_encrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2678 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2679 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2680 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2681 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2682 | |
| 2683 | /* Call decrypt setup twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2684 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2685 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2686 | TEST_EQUAL( psa_cipher_decrypt_setup( &operation, key, alg ), |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2687 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2688 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2689 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 5ae6f75 | 2021-06-24 11:36:14 +0100 | [diff] [blame] | 2690 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | 36ee5d0 | 2019-02-19 09:25:10 +0000 | [diff] [blame] | 2691 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2692 | /* Generate an IV without calling setup beforehand. */ |
| 2693 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2694 | buffer, sizeof( buffer ), |
| 2695 | &length ), |
| 2696 | PSA_ERROR_BAD_STATE ); |
| 2697 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2698 | |
| 2699 | /* Generate an IV twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2700 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2701 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2702 | buffer, sizeof( buffer ), |
| 2703 | &length ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2704 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2705 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2706 | buffer, sizeof( buffer ), |
| 2707 | &length ), |
| 2708 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2709 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2710 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2711 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2712 | |
| 2713 | /* Generate an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2714 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2715 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2716 | iv, sizeof( iv ) ) ); |
| 2717 | TEST_EQUAL( psa_cipher_generate_iv( &operation, |
| 2718 | buffer, sizeof( buffer ), |
| 2719 | &length ), |
| 2720 | PSA_ERROR_BAD_STATE ); |
| 2721 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2722 | |
| 2723 | /* Set an IV without calling setup beforehand. */ |
| 2724 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2725 | iv, sizeof( iv ) ), |
| 2726 | PSA_ERROR_BAD_STATE ); |
| 2727 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2728 | |
| 2729 | /* Set an IV after it's already set. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2730 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2731 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2732 | iv, sizeof( iv ) ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2733 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2734 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2735 | iv, sizeof( iv ) ), |
| 2736 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2737 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2738 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2739 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2740 | |
| 2741 | /* Set an IV after it's already generated. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2742 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2743 | PSA_ASSERT( psa_cipher_generate_iv( &operation, |
| 2744 | buffer, sizeof( buffer ), |
| 2745 | &length ) ); |
| 2746 | TEST_EQUAL( psa_cipher_set_iv( &operation, |
| 2747 | iv, sizeof( iv ) ), |
| 2748 | PSA_ERROR_BAD_STATE ); |
| 2749 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2750 | |
| 2751 | /* Call update without calling setup beforehand. */ |
| 2752 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2753 | text, sizeof( text ), |
| 2754 | buffer, sizeof( buffer ), |
| 2755 | &length ), |
| 2756 | PSA_ERROR_BAD_STATE ); |
| 2757 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2758 | |
| 2759 | /* Call update without an IV where an IV is required. */ |
Dave Rodgman | 095dadc | 2021-06-23 12:48:52 +0100 | [diff] [blame] | 2760 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2761 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2762 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2763 | text, sizeof( text ), |
| 2764 | buffer, sizeof( buffer ), |
| 2765 | &length ), |
| 2766 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2767 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2768 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2769 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2770 | |
| 2771 | /* Call update after finish. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2772 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2773 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2774 | iv, sizeof( iv ) ) ); |
| 2775 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2776 | buffer, sizeof( buffer ), &length ) ); |
| 2777 | TEST_EQUAL( psa_cipher_update( &operation, |
| 2778 | text, sizeof( text ), |
| 2779 | buffer, sizeof( buffer ), |
| 2780 | &length ), |
| 2781 | PSA_ERROR_BAD_STATE ); |
| 2782 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2783 | |
| 2784 | /* Call finish without calling setup beforehand. */ |
| 2785 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2786 | buffer, sizeof( buffer ), &length ), |
| 2787 | PSA_ERROR_BAD_STATE ); |
| 2788 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2789 | |
| 2790 | /* Call finish without an IV where an IV is required. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2791 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2792 | /* Not calling update means we are encrypting an empty buffer, which is OK |
| 2793 | * for cipher modes with padding. */ |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2794 | ASSERT_OPERATION_IS_ACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2795 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2796 | buffer, sizeof( buffer ), &length ), |
| 2797 | PSA_ERROR_BAD_STATE ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2798 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2799 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
Dave Rodgman | 647791d | 2021-06-23 12:49:59 +0100 | [diff] [blame] | 2800 | ASSERT_OPERATION_IS_INACTIVE( operation ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2801 | |
| 2802 | /* Call finish twice in a row. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2803 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2804 | PSA_ASSERT( psa_cipher_set_iv( &operation, |
| 2805 | iv, sizeof( iv ) ) ); |
| 2806 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 2807 | buffer, sizeof( buffer ), &length ) ); |
| 2808 | TEST_EQUAL( psa_cipher_finish( &operation, |
| 2809 | buffer, sizeof( buffer ), &length ), |
| 2810 | PSA_ERROR_BAD_STATE ); |
| 2811 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 2812 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2813 | PSA_ASSERT( psa_destroy_key( key ) ); |
Gilles Peskine | 76b29a7 | 2019-05-28 14:08:50 +0200 | [diff] [blame] | 2814 | |
Jaeden Amero | ab43997 | 2019-02-15 14:12:05 +0000 | [diff] [blame] | 2815 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 2816 | psa_cipher_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 2817 | PSA_DONE( ); |
Gilles Peskine | 16c0f4f | 2018-06-20 16:05:20 +0200 | [diff] [blame] | 2818 | } |
| 2819 | /* END_CASE */ |
| 2820 | |
| 2821 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 2822 | void cipher_encrypt_fail( int alg_arg, |
| 2823 | int key_type_arg, |
| 2824 | data_t *key_data, |
| 2825 | data_t *input, |
| 2826 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2827 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 2828 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2829 | psa_status_t status; |
| 2830 | psa_key_type_t key_type = key_type_arg; |
| 2831 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | b866e2b | 2018-06-21 09:25:10 +0200 | [diff] [blame] | 2832 | psa_status_t expected_status = expected_status_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 2833 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2834 | size_t output_buffer_size = 0; |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2835 | size_t output_length = 0; |
| 2836 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2837 | |
| 2838 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 2839 | { |
| 2840 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2841 | |
| 2842 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2843 | psa_set_key_algorithm( &attributes, alg ); |
| 2844 | psa_set_key_type( &attributes, key_type ); |
| 2845 | |
| 2846 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, |
| 2847 | input->len ); |
| 2848 | ASSERT_ALLOC( output, output_buffer_size ); |
| 2849 | |
| 2850 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2851 | &key ) ); |
| 2852 | } |
| 2853 | |
| 2854 | status = psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 2855 | output_buffer_size, &output_length ); |
| 2856 | |
| 2857 | TEST_EQUAL( status, expected_status ); |
| 2858 | |
| 2859 | exit: |
| 2860 | mbedtls_free( output ); |
| 2861 | psa_destroy_key( key ); |
| 2862 | PSA_DONE( ); |
| 2863 | } |
| 2864 | /* END_CASE */ |
| 2865 | |
| 2866 | /* BEGIN_CASE */ |
Mateusz Starzyk | ed71e92 | 2021-10-21 10:04:57 +0200 | [diff] [blame] | 2867 | void cipher_encrypt_validate_iv_length( int alg, int key_type, data_t* key_data, |
| 2868 | data_t *input, int iv_length, |
| 2869 | int expected_result ) |
| 2870 | { |
| 2871 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2872 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2873 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2874 | size_t output_buffer_size = 0; |
| 2875 | unsigned char *output = NULL; |
| 2876 | |
| 2877 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2878 | ASSERT_ALLOC( output, output_buffer_size ); |
| 2879 | |
| 2880 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2881 | |
| 2882 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2883 | psa_set_key_algorithm( &attributes, alg ); |
| 2884 | psa_set_key_type( &attributes, key_type ); |
| 2885 | |
| 2886 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2887 | &key ) ); |
| 2888 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 2889 | TEST_EQUAL( expected_result, psa_cipher_set_iv( &operation, output, |
| 2890 | iv_length ) ); |
| 2891 | |
| 2892 | exit: |
| 2893 | psa_cipher_abort( &operation ); |
| 2894 | mbedtls_free( output ); |
| 2895 | psa_destroy_key( key ); |
| 2896 | PSA_DONE( ); |
| 2897 | } |
| 2898 | /* END_CASE */ |
| 2899 | |
| 2900 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 2901 | void cipher_encrypt_alg_without_iv( int alg_arg, |
| 2902 | int key_type_arg, |
| 2903 | data_t *key_data, |
| 2904 | data_t *input, |
| 2905 | data_t *expected_output ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2906 | { |
| 2907 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2908 | psa_key_type_t key_type = key_type_arg; |
| 2909 | psa_algorithm_t alg = alg_arg; |
| 2910 | unsigned char *output = NULL; |
| 2911 | size_t output_buffer_size = 0; |
| 2912 | size_t output_length = 0; |
| 2913 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2914 | |
| 2915 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2916 | |
| 2917 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2918 | psa_set_key_algorithm( &attributes, alg ); |
| 2919 | psa_set_key_type( &attributes, key_type ); |
| 2920 | |
| 2921 | output_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 2922 | ASSERT_ALLOC( output, output_buffer_size ); |
| 2923 | |
| 2924 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2925 | &key ) ); |
| 2926 | |
| 2927 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output, |
| 2928 | output_buffer_size, &output_length ) ); |
| 2929 | TEST_ASSERT( output_length <= |
| 2930 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 2931 | TEST_ASSERT( output_length <= |
| 2932 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
| 2933 | |
| 2934 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 2935 | output, output_length ); |
| 2936 | exit: |
| 2937 | mbedtls_free( output ); |
| 2938 | psa_destroy_key( key ); |
| 2939 | PSA_DONE( ); |
| 2940 | } |
| 2941 | /* END_CASE */ |
| 2942 | |
| 2943 | /* BEGIN_CASE */ |
Paul Elliott | a417f56 | 2021-07-14 12:31:21 +0100 | [diff] [blame] | 2944 | void cipher_bad_key( int alg_arg, int key_type_arg, data_t *key_data ) |
| 2945 | { |
| 2946 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2947 | psa_algorithm_t alg = alg_arg; |
| 2948 | psa_key_type_t key_type = key_type_arg; |
| 2949 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 2950 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
| 2951 | psa_status_t status; |
| 2952 | |
| 2953 | PSA_ASSERT( psa_crypto_init( ) ); |
| 2954 | |
| 2955 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 2956 | psa_set_key_algorithm( &attributes, alg ); |
| 2957 | psa_set_key_type( &attributes, key_type ); |
| 2958 | |
| 2959 | /* Usage of either of these two size macros would cause divide by zero |
| 2960 | * with incorrect key types previously. Input length should be irrelevant |
| 2961 | * here. */ |
| 2962 | TEST_EQUAL( PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, 16 ), |
| 2963 | 0 ); |
| 2964 | TEST_EQUAL( PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, 16 ), 0 ); |
| 2965 | |
| 2966 | |
| 2967 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 2968 | &key ) ); |
| 2969 | |
| 2970 | /* Should fail due to invalid alg type (to support invalid key type). |
| 2971 | * Encrypt or decrypt will end up in the same place. */ |
| 2972 | status = psa_cipher_encrypt_setup( &operation, key, alg ); |
| 2973 | |
| 2974 | TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT ); |
| 2975 | |
| 2976 | exit: |
| 2977 | psa_cipher_abort( &operation ); |
| 2978 | psa_destroy_key( key ); |
| 2979 | PSA_DONE( ); |
| 2980 | } |
| 2981 | /* END_CASE */ |
| 2982 | |
| 2983 | /* BEGIN_CASE */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 2984 | void cipher_encrypt_validation( int alg_arg, |
| 2985 | int key_type_arg, |
| 2986 | data_t *key_data, |
| 2987 | data_t *input ) |
| 2988 | { |
| 2989 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 2990 | psa_key_type_t key_type = key_type_arg; |
| 2991 | psa_algorithm_t alg = alg_arg; |
| 2992 | size_t iv_size = PSA_CIPHER_IV_LENGTH ( key_type, alg ); |
| 2993 | unsigned char *output1 = NULL; |
| 2994 | size_t output1_buffer_size = 0; |
| 2995 | size_t output1_length = 0; |
| 2996 | unsigned char *output2 = NULL; |
| 2997 | size_t output2_buffer_size = 0; |
| 2998 | size_t output2_length = 0; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 2999 | size_t function_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3000 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3001 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3002 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3003 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3004 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3005 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3006 | psa_set_key_algorithm( &attributes, alg ); |
| 3007 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3008 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3009 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3010 | output2_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 3011 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 3012 | ASSERT_ALLOC( output1, output1_buffer_size ); |
| 3013 | ASSERT_ALLOC( output2, output2_buffer_size ); |
| 3014 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3015 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3016 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3017 | |
gabor-mezei-arm | 50c86cf | 2021-06-25 15:47:50 +0200 | [diff] [blame] | 3018 | /* The one-shot cipher encryption uses generated iv so validating |
| 3019 | the output is not possible. Validating with multipart encryption. */ |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3020 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, output1, |
| 3021 | output1_buffer_size, &output1_length ) ); |
| 3022 | TEST_ASSERT( output1_length <= |
| 3023 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 3024 | TEST_ASSERT( output1_length <= |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3025 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3026 | |
| 3027 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
| 3028 | PSA_ASSERT( psa_cipher_set_iv( &operation, output1, iv_size ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3029 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3030 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3031 | input->x, input->len, |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3032 | output2, output2_buffer_size, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3033 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3034 | TEST_ASSERT( function_output_length <= |
| 3035 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) ); |
| 3036 | TEST_ASSERT( function_output_length <= |
| 3037 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3038 | output2_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3039 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3040 | PSA_ASSERT( psa_cipher_finish( &operation, |
| 3041 | output2 + output2_length, |
| 3042 | output2_buffer_size - output2_length, |
| 3043 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3044 | TEST_ASSERT( function_output_length <= |
| 3045 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3046 | TEST_ASSERT( function_output_length <= |
| 3047 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3048 | output2_length += function_output_length; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3049 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3050 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3051 | ASSERT_COMPARE( output1 + iv_size, output1_length - iv_size, |
| 3052 | output2, output2_length ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3053 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3054 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3055 | psa_cipher_abort( &operation ); |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3056 | mbedtls_free( output1 ); |
| 3057 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3058 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3059 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3060 | } |
| 3061 | /* END_CASE */ |
| 3062 | |
| 3063 | /* BEGIN_CASE */ |
| 3064 | void cipher_encrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3065 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3066 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3067 | int first_part_size_arg, |
| 3068 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3069 | data_t *expected_output, |
| 3070 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3071 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3072 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3073 | psa_key_type_t key_type = key_type_arg; |
| 3074 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3075 | psa_status_t status; |
| 3076 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3077 | size_t first_part_size = first_part_size_arg; |
| 3078 | size_t output1_length = output1_length_arg; |
| 3079 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3080 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3081 | size_t output_buffer_size = 0; |
| 3082 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3083 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3084 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3085 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3086 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3087 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3088 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3089 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3090 | psa_set_key_algorithm( &attributes, alg ); |
| 3091 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3092 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3093 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3094 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3095 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3096 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3097 | |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3098 | if( iv->len > 0 ) |
| 3099 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3100 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3101 | } |
| 3102 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3103 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 3104 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3105 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3106 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3107 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3108 | PSA_ASSERT( psa_cipher_update( &operation, input->x, first_part_size, |
| 3109 | output, output_buffer_size, |
| 3110 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3111 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3112 | TEST_ASSERT( function_output_length <= |
| 3113 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3114 | TEST_ASSERT( function_output_length <= |
| 3115 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3116 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3117 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3118 | if( first_part_size < input->len ) |
| 3119 | { |
| 3120 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3121 | input->x + first_part_size, |
| 3122 | input->len - first_part_size, |
| 3123 | ( output_buffer_size == 0 ? NULL : |
| 3124 | output + total_output_length ), |
| 3125 | output_buffer_size - total_output_length, |
| 3126 | &function_output_length ) ); |
| 3127 | TEST_ASSERT( function_output_length == output2_length ); |
| 3128 | TEST_ASSERT( function_output_length <= |
| 3129 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3130 | alg, |
| 3131 | input->len - first_part_size ) ); |
| 3132 | TEST_ASSERT( function_output_length <= |
| 3133 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
| 3134 | total_output_length += function_output_length; |
| 3135 | } |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3136 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3137 | status = psa_cipher_finish( &operation, |
| 3138 | ( output_buffer_size == 0 ? NULL : |
| 3139 | output + total_output_length ), |
| 3140 | output_buffer_size - total_output_length, |
| 3141 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3142 | TEST_ASSERT( function_output_length <= |
| 3143 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3144 | TEST_ASSERT( function_output_length <= |
| 3145 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3146 | total_output_length += function_output_length; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3147 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3148 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3149 | if( expected_status == PSA_SUCCESS ) |
| 3150 | { |
| 3151 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
| 3152 | |
| 3153 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3154 | output, total_output_length ); |
| 3155 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3156 | |
| 3157 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3158 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3159 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3160 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3161 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3162 | } |
| 3163 | /* END_CASE */ |
| 3164 | |
| 3165 | /* BEGIN_CASE */ |
| 3166 | void cipher_decrypt_multipart( int alg_arg, int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3167 | data_t *key_data, data_t *iv, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3168 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3169 | int first_part_size_arg, |
| 3170 | int output1_length_arg, int output2_length_arg, |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3171 | data_t *expected_output, |
| 3172 | int expected_status_arg ) |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3173 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3174 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3175 | psa_key_type_t key_type = key_type_arg; |
| 3176 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3177 | psa_status_t status; |
| 3178 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3179 | size_t first_part_size = first_part_size_arg; |
| 3180 | size_t output1_length = output1_length_arg; |
| 3181 | size_t output2_length = output2_length_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3182 | unsigned char *output = NULL; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3183 | size_t output_buffer_size = 0; |
| 3184 | size_t function_output_length = 0; |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3185 | size_t total_output_length = 0; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3186 | psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3187 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3188 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3189 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3190 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3191 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3192 | psa_set_key_algorithm( &attributes, alg ); |
| 3193 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3194 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3195 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3196 | &key ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3197 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3198 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3199 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3200 | if( iv->len > 0 ) |
| 3201 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3202 | PSA_ASSERT( psa_cipher_set_iv( &operation, iv->x, iv->len ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3203 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3204 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3205 | output_buffer_size = PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, input->len ) + |
| 3206 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3207 | ASSERT_ALLOC( output, output_buffer_size ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3208 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3209 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3210 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3211 | input->x, first_part_size, |
| 3212 | output, output_buffer_size, |
| 3213 | &function_output_length ) ); |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3214 | TEST_ASSERT( function_output_length == output1_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3215 | TEST_ASSERT( function_output_length <= |
| 3216 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3217 | TEST_ASSERT( function_output_length <= |
| 3218 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3219 | total_output_length += function_output_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3220 | |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3221 | if( first_part_size < input->len ) |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3222 | { |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3223 | PSA_ASSERT( psa_cipher_update( &operation, |
| 3224 | input->x + first_part_size, |
| 3225 | input->len - first_part_size, |
| 3226 | ( output_buffer_size == 0 ? NULL : |
| 3227 | output + total_output_length ), |
| 3228 | output_buffer_size - total_output_length, |
| 3229 | &function_output_length ) ); |
| 3230 | TEST_ASSERT( function_output_length == output2_length ); |
| 3231 | TEST_ASSERT( function_output_length <= |
| 3232 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3233 | alg, |
| 3234 | input->len - first_part_size ) ); |
| 3235 | TEST_ASSERT( function_output_length <= |
| 3236 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len ) ); |
| 3237 | total_output_length += function_output_length; |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3238 | } |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3239 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3240 | status = psa_cipher_finish( &operation, |
Gilles Peskine | 0c510f3 | 2021-03-24 00:41:51 +0100 | [diff] [blame] | 3241 | ( output_buffer_size == 0 ? NULL : |
| 3242 | output + total_output_length ), |
Gilles Peskine | ee46fe7 | 2019-02-19 19:05:33 +0100 | [diff] [blame] | 3243 | output_buffer_size - total_output_length, |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3244 | &function_output_length ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3245 | TEST_ASSERT( function_output_length <= |
| 3246 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3247 | TEST_ASSERT( function_output_length <= |
| 3248 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | a7ec95f | 2018-06-08 14:40:59 +0200 | [diff] [blame] | 3249 | total_output_length += function_output_length; |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3250 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3251 | |
| 3252 | if( expected_status == PSA_SUCCESS ) |
| 3253 | { |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3254 | PSA_ASSERT( psa_cipher_abort( &operation ) ); |
gabor-mezei-arm | 95aad83 | 2021-06-25 18:21:33 +0200 | [diff] [blame] | 3255 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3256 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3257 | output, total_output_length ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3258 | } |
| 3259 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3260 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3261 | psa_cipher_abort( &operation ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3262 | mbedtls_free( output ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3263 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3264 | PSA_DONE( ); |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3265 | } |
| 3266 | /* END_CASE */ |
| 3267 | |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3268 | /* BEGIN_CASE */ |
gabor-mezei-arm | a56756e | 2021-06-25 15:49:14 +0200 | [diff] [blame] | 3269 | void cipher_decrypt_fail( int alg_arg, |
| 3270 | int key_type_arg, |
| 3271 | data_t *key_data, |
| 3272 | data_t *iv, |
| 3273 | data_t *input_arg, |
| 3274 | int expected_status_arg ) |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3275 | { |
| 3276 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3277 | psa_status_t status; |
| 3278 | psa_key_type_t key_type = key_type_arg; |
| 3279 | psa_algorithm_t alg = alg_arg; |
| 3280 | psa_status_t expected_status = expected_status_arg; |
| 3281 | unsigned char *input = NULL; |
| 3282 | size_t input_buffer_size = 0; |
| 3283 | unsigned char *output = NULL; |
| 3284 | size_t output_buffer_size = 0; |
| 3285 | size_t output_length = 0; |
| 3286 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3287 | |
| 3288 | if ( PSA_ERROR_BAD_STATE != expected_status ) |
| 3289 | { |
| 3290 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3291 | |
| 3292 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3293 | psa_set_key_algorithm( &attributes, alg ); |
| 3294 | psa_set_key_type( &attributes, key_type ); |
| 3295 | |
| 3296 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3297 | &key ) ); |
| 3298 | } |
| 3299 | |
| 3300 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 3301 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 3302 | if ( input_buffer_size > 0 ) |
| 3303 | { |
| 3304 | ASSERT_ALLOC( input, input_buffer_size ); |
| 3305 | memcpy( input, iv->x, iv->len ); |
| 3306 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 3307 | } |
| 3308 | |
| 3309 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 3310 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3311 | |
| 3312 | status = psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 3313 | output_buffer_size, &output_length ); |
| 3314 | TEST_EQUAL( status, expected_status ); |
| 3315 | |
| 3316 | exit: |
| 3317 | mbedtls_free( input ); |
| 3318 | mbedtls_free( output ); |
| 3319 | psa_destroy_key( key ); |
| 3320 | PSA_DONE( ); |
| 3321 | } |
| 3322 | /* END_CASE */ |
| 3323 | |
| 3324 | /* BEGIN_CASE */ |
| 3325 | void cipher_decrypt( int alg_arg, |
| 3326 | int key_type_arg, |
| 3327 | data_t *key_data, |
| 3328 | data_t *iv, |
| 3329 | data_t *input_arg, |
| 3330 | data_t *expected_output ) |
| 3331 | { |
| 3332 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 3333 | psa_key_type_t key_type = key_type_arg; |
| 3334 | psa_algorithm_t alg = alg_arg; |
| 3335 | unsigned char *input = NULL; |
| 3336 | size_t input_buffer_size = 0; |
| 3337 | unsigned char *output = NULL; |
| 3338 | size_t output_buffer_size = 0; |
| 3339 | size_t output_length = 0; |
| 3340 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 3341 | |
| 3342 | PSA_ASSERT( psa_crypto_init( ) ); |
| 3343 | |
| 3344 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3345 | psa_set_key_algorithm( &attributes, alg ); |
| 3346 | psa_set_key_type( &attributes, key_type ); |
| 3347 | |
| 3348 | /* Allocate input buffer and copy the iv and the plaintext */ |
| 3349 | input_buffer_size = ( (size_t) input_arg->len + (size_t) iv->len ); |
| 3350 | if ( input_buffer_size > 0 ) |
| 3351 | { |
| 3352 | ASSERT_ALLOC( input, input_buffer_size ); |
| 3353 | memcpy( input, iv->x, iv->len ); |
| 3354 | memcpy( input + iv->len, input_arg->x, input_arg->len ); |
| 3355 | } |
| 3356 | |
| 3357 | output_buffer_size = PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ); |
| 3358 | ASSERT_ALLOC( output, output_buffer_size ); |
| 3359 | |
| 3360 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3361 | &key ) ); |
| 3362 | |
| 3363 | PSA_ASSERT( psa_cipher_decrypt( key, alg, input, input_buffer_size, output, |
| 3364 | output_buffer_size, &output_length ) ); |
| 3365 | TEST_ASSERT( output_length <= |
| 3366 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, input_buffer_size ) ); |
| 3367 | TEST_ASSERT( output_length <= |
| 3368 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( input_buffer_size ) ); |
| 3369 | |
| 3370 | ASSERT_COMPARE( expected_output->x, expected_output->len, |
| 3371 | output, output_length ); |
| 3372 | exit: |
| 3373 | mbedtls_free( input ); |
| 3374 | mbedtls_free( output ); |
| 3375 | psa_destroy_key( key ); |
| 3376 | PSA_DONE( ); |
| 3377 | } |
| 3378 | /* END_CASE */ |
| 3379 | |
| 3380 | /* BEGIN_CASE */ |
| 3381 | void cipher_verify_output( int alg_arg, |
| 3382 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3383 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3384 | data_t *input ) |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3385 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3386 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3387 | psa_key_type_t key_type = key_type_arg; |
| 3388 | psa_algorithm_t alg = alg_arg; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3389 | unsigned char *output1 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3390 | size_t output1_size = 0; |
| 3391 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3392 | unsigned char *output2 = NULL; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3393 | size_t output2_size = 0; |
| 3394 | size_t output2_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3395 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3396 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3397 | PSA_ASSERT( psa_crypto_init( ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3398 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3399 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3400 | psa_set_key_algorithm( &attributes, alg ); |
| 3401 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3402 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3403 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3404 | &key ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3405 | output1_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3406 | ASSERT_ALLOC( output1, output1_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3407 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3408 | PSA_ASSERT( psa_cipher_encrypt( key, alg, input->x, input->len, |
| 3409 | output1, output1_size, |
| 3410 | &output1_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3411 | TEST_ASSERT( output1_length <= |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3412 | PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3413 | TEST_ASSERT( output1_length <= |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3414 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3415 | |
| 3416 | output2_size = output1_length; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3417 | ASSERT_ALLOC( output2, output2_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3418 | |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3419 | PSA_ASSERT( psa_cipher_decrypt( key, alg, output1, output1_length, |
| 3420 | output2, output2_size, |
| 3421 | &output2_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3422 | TEST_ASSERT( output2_length <= |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3423 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3424 | TEST_ASSERT( output2_length <= |
gabor-mezei-arm | f494bcd | 2021-03-01 15:11:46 +0100 | [diff] [blame] | 3425 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3426 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3427 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3428 | |
| 3429 | exit: |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3430 | mbedtls_free( output1 ); |
| 3431 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3432 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3433 | PSA_DONE( ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3434 | } |
| 3435 | /* END_CASE */ |
| 3436 | |
| 3437 | /* BEGIN_CASE */ |
Gilles Peskine | 50e586b | 2018-06-08 14:28:46 +0200 | [diff] [blame] | 3438 | void cipher_verify_output_multipart( int alg_arg, |
| 3439 | int key_type_arg, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3440 | data_t *key_data, |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3441 | data_t *input, |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3442 | int first_part_size_arg ) |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3443 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3444 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3445 | psa_key_type_t key_type = key_type_arg; |
| 3446 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3447 | size_t first_part_size = first_part_size_arg; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3448 | unsigned char iv[16] = {0}; |
| 3449 | size_t iv_size = 16; |
| 3450 | size_t iv_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3451 | unsigned char *output1 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3452 | size_t output1_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3453 | size_t output1_length = 0; |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3454 | unsigned char *output2 = NULL; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3455 | size_t output2_buffer_size = 0; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3456 | size_t output2_length = 0; |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3457 | size_t function_output_length; |
Jaeden Amero | 5bae227 | 2019-01-04 11:48:27 +0000 | [diff] [blame] | 3458 | psa_cipher_operation_t operation1 = PSA_CIPHER_OPERATION_INIT; |
| 3459 | psa_cipher_operation_t operation2 = PSA_CIPHER_OPERATION_INIT; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3460 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3461 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3462 | PSA_ASSERT( psa_crypto_init( ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3463 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3464 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3465 | psa_set_key_algorithm( &attributes, alg ); |
| 3466 | psa_set_key_type( &attributes, key_type ); |
Moran Peker | ed34695 | 2018-07-05 15:22:45 +0300 | [diff] [blame] | 3467 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3468 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 3469 | &key ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3470 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3471 | PSA_ASSERT( psa_cipher_encrypt_setup( &operation1, key, alg ) ); |
| 3472 | PSA_ASSERT( psa_cipher_decrypt_setup( &operation2, key, alg ) ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3473 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3474 | if( alg != PSA_ALG_ECB_NO_PADDING ) |
| 3475 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3476 | PSA_ASSERT( psa_cipher_generate_iv( &operation1, |
| 3477 | iv, iv_size, |
| 3478 | &iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3479 | } |
| 3480 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3481 | output1_buffer_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE( key_type, alg, input->len ); |
| 3482 | TEST_ASSERT( output1_buffer_size <= |
| 3483 | PSA_CIPHER_ENCRYPT_OUTPUT_MAX_SIZE( input->len ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3484 | ASSERT_ALLOC( output1, output1_buffer_size ); |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3485 | |
Gilles Peskine | e086652 | 2019-02-19 19:44:00 +0100 | [diff] [blame] | 3486 | TEST_ASSERT( first_part_size <= input->len ); |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3487 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3488 | PSA_ASSERT( psa_cipher_update( &operation1, input->x, first_part_size, |
| 3489 | output1, output1_buffer_size, |
| 3490 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3491 | TEST_ASSERT( function_output_length <= |
| 3492 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3493 | TEST_ASSERT( function_output_length <= |
| 3494 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3495 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3496 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3497 | PSA_ASSERT( psa_cipher_update( &operation1, |
| 3498 | input->x + first_part_size, |
| 3499 | input->len - first_part_size, |
| 3500 | output1, output1_buffer_size, |
| 3501 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3502 | TEST_ASSERT( function_output_length <= |
| 3503 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3504 | alg, |
| 3505 | input->len - first_part_size ) ); |
| 3506 | TEST_ASSERT( function_output_length <= |
| 3507 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( input->len - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3508 | output1_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3509 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3510 | PSA_ASSERT( psa_cipher_finish( &operation1, |
| 3511 | output1 + output1_length, |
| 3512 | output1_buffer_size - output1_length, |
| 3513 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3514 | TEST_ASSERT( function_output_length <= |
| 3515 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3516 | TEST_ASSERT( function_output_length <= |
| 3517 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3518 | output1_length += function_output_length; |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3519 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3520 | PSA_ASSERT( psa_cipher_abort( &operation1 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3521 | |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3522 | output2_buffer_size = output1_length; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3523 | TEST_ASSERT( output2_buffer_size <= |
| 3524 | PSA_CIPHER_DECRYPT_OUTPUT_SIZE( key_type, alg, output1_length ) ); |
| 3525 | TEST_ASSERT( output2_buffer_size <= |
| 3526 | PSA_CIPHER_DECRYPT_OUTPUT_MAX_SIZE( output1_length ) ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3527 | ASSERT_ALLOC( output2, output2_buffer_size ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3528 | |
Steven Cooreman | 177deba | 2020-09-07 17:14:14 +0200 | [diff] [blame] | 3529 | if( iv_length > 0 ) |
| 3530 | { |
Steven Cooreman | a6033e9 | 2020-08-25 11:47:50 +0200 | [diff] [blame] | 3531 | PSA_ASSERT( psa_cipher_set_iv( &operation2, |
| 3532 | iv, iv_length ) ); |
Steven Cooreman | ed3c9ec | 2020-07-06 14:08:59 +0200 | [diff] [blame] | 3533 | } |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3534 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3535 | PSA_ASSERT( psa_cipher_update( &operation2, output1, first_part_size, |
| 3536 | output2, output2_buffer_size, |
| 3537 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3538 | TEST_ASSERT( function_output_length <= |
| 3539 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, alg, first_part_size ) ); |
| 3540 | TEST_ASSERT( function_output_length <= |
| 3541 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3542 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3543 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3544 | PSA_ASSERT( psa_cipher_update( &operation2, |
| 3545 | output1 + first_part_size, |
| 3546 | output1_length - first_part_size, |
| 3547 | output2, output2_buffer_size, |
| 3548 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3549 | TEST_ASSERT( function_output_length <= |
| 3550 | PSA_CIPHER_UPDATE_OUTPUT_SIZE( key_type, |
| 3551 | alg, |
| 3552 | output1_length - first_part_size ) ); |
| 3553 | TEST_ASSERT( function_output_length <= |
| 3554 | PSA_CIPHER_UPDATE_OUTPUT_MAX_SIZE( output1_length - first_part_size ) ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3555 | output2_length += function_output_length; |
Moran Peker | ded8440 | 2018-06-06 16:36:50 +0300 | [diff] [blame] | 3556 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3557 | PSA_ASSERT( psa_cipher_finish( &operation2, |
| 3558 | output2 + output2_length, |
| 3559 | output2_buffer_size - output2_length, |
| 3560 | &function_output_length ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3561 | TEST_ASSERT( function_output_length <= |
| 3562 | PSA_CIPHER_FINISH_OUTPUT_SIZE( key_type, alg ) ); |
| 3563 | TEST_ASSERT( function_output_length <= |
| 3564 | PSA_CIPHER_FINISH_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 048b7f0 | 2018-06-08 14:20:49 +0200 | [diff] [blame] | 3565 | output2_length += function_output_length; |
Gilles Peskine | 4ca9c3f | 2018-06-06 18:44:09 +0200 | [diff] [blame] | 3566 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3567 | PSA_ASSERT( psa_cipher_abort( &operation2 ) ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3568 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3569 | ASSERT_COMPARE( input->x, input->len, output2, output2_length ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3570 | |
| 3571 | exit: |
Gilles Peskine | 64f13ef | 2020-08-25 23:15:20 +0200 | [diff] [blame] | 3572 | psa_cipher_abort( &operation1 ); |
| 3573 | psa_cipher_abort( &operation2 ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 3574 | mbedtls_free( output1 ); |
| 3575 | mbedtls_free( output2 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3576 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3577 | PSA_DONE( ); |
mohammad1603 | d7d7ba5 | 2018-03-12 18:51:53 +0200 | [diff] [blame] | 3578 | } |
| 3579 | /* END_CASE */ |
Gilles Peskine | 7268afc | 2018-06-06 15:19:24 +0200 | [diff] [blame] | 3580 | |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 3581 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3582 | void aead_encrypt_decrypt( int key_type_arg, data_t *key_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3583 | int alg_arg, |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3584 | data_t *nonce, |
| 3585 | data_t *additional_data, |
| 3586 | data_t *input_data, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 3587 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3588 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3589 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3590 | psa_key_type_t key_type = key_type_arg; |
| 3591 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3592 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3593 | unsigned char *output_data = NULL; |
| 3594 | size_t output_size = 0; |
| 3595 | size_t output_length = 0; |
| 3596 | unsigned char *output_data2 = NULL; |
| 3597 | size_t output_length2 = 0; |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3598 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3599 | psa_status_t expected_result = expected_result_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3600 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3601 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3602 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3603 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3604 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 3605 | psa_set_key_algorithm( &attributes, alg ); |
| 3606 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3607 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3608 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3609 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3610 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3611 | key_bits = psa_get_key_bits( &attributes ); |
| 3612 | |
| 3613 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3614 | alg ); |
| 3615 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3616 | * should be exact. */ |
| 3617 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3618 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3619 | { |
| 3620 | TEST_EQUAL( output_size, |
| 3621 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3622 | TEST_ASSERT( output_size <= |
| 3623 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3624 | } |
| 3625 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3626 | |
Steven Cooreman | f49478b | 2021-02-15 15:19:25 +0100 | [diff] [blame] | 3627 | status = psa_aead_encrypt( key, alg, |
| 3628 | nonce->x, nonce->len, |
| 3629 | additional_data->x, |
| 3630 | additional_data->len, |
| 3631 | input_data->x, input_data->len, |
| 3632 | output_data, output_size, |
| 3633 | &output_length ); |
| 3634 | |
| 3635 | /* If the operation is not supported, just skip and not fail in case the |
| 3636 | * encryption involves a common limitation of cryptography hardwares and |
| 3637 | * an alternative implementation. */ |
| 3638 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 3639 | { |
| 3640 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3641 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 3642 | } |
| 3643 | |
| 3644 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3645 | |
| 3646 | if( PSA_SUCCESS == expected_result ) |
| 3647 | { |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 3648 | ASSERT_ALLOC( output_data2, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3649 | |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3650 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3651 | * should be exact. */ |
| 3652 | TEST_EQUAL( input_data->len, |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3653 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, output_length ) ); |
Gilles Peskine | 003a4a9 | 2019-05-14 16:09:40 +0200 | [diff] [blame] | 3654 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 3655 | TEST_ASSERT( input_data->len <= |
| 3656 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( output_length ) ); |
| 3657 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3658 | TEST_EQUAL( psa_aead_decrypt( key, alg, |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 3659 | nonce->x, nonce->len, |
| 3660 | additional_data->x, |
| 3661 | additional_data->len, |
| 3662 | output_data, output_length, |
| 3663 | output_data2, output_length, |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 3664 | &output_length2 ), |
| 3665 | expected_result ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3666 | |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3667 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 3668 | output_data2, output_length2 ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3669 | } |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3670 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3671 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3672 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3673 | mbedtls_free( output_data ); |
| 3674 | mbedtls_free( output_data2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3675 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3676 | } |
| 3677 | /* END_CASE */ |
| 3678 | |
| 3679 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3680 | void aead_encrypt( int key_type_arg, data_t *key_data, |
| 3681 | int alg_arg, |
| 3682 | data_t *nonce, |
| 3683 | data_t *additional_data, |
| 3684 | data_t *input_data, |
| 3685 | data_t *expected_result ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3686 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3687 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3688 | psa_key_type_t key_type = key_type_arg; |
| 3689 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3690 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3691 | unsigned char *output_data = NULL; |
| 3692 | size_t output_size = 0; |
| 3693 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3694 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3695 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3696 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3697 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3698 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3699 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 3700 | psa_set_key_algorithm( &attributes, alg ); |
| 3701 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3702 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3703 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3704 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3705 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3706 | key_bits = psa_get_key_bits( &attributes ); |
| 3707 | |
| 3708 | output_size = input_data->len + PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3709 | alg ); |
| 3710 | /* For all currently defined algorithms, PSA_AEAD_ENCRYPT_OUTPUT_SIZE |
| 3711 | * should be exact. */ |
| 3712 | TEST_EQUAL( output_size, |
| 3713 | PSA_AEAD_ENCRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3714 | TEST_ASSERT( output_size <= |
| 3715 | PSA_AEAD_ENCRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3716 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3717 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3718 | status = psa_aead_encrypt( key, alg, |
| 3719 | nonce->x, nonce->len, |
| 3720 | additional_data->x, additional_data->len, |
| 3721 | input_data->x, input_data->len, |
| 3722 | output_data, output_size, |
| 3723 | &output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3724 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3725 | /* If the operation is not supported, just skip and not fail in case the |
| 3726 | * encryption involves a common limitation of cryptography hardwares and |
| 3727 | * an alternative implementation. */ |
| 3728 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3729 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3730 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3731 | 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] | 3732 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3733 | |
| 3734 | PSA_ASSERT( status ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3735 | ASSERT_COMPARE( expected_result->x, expected_result->len, |
| 3736 | output_data, output_length ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3737 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3738 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3739 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3740 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3741 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3742 | } |
| 3743 | /* END_CASE */ |
| 3744 | |
| 3745 | /* BEGIN_CASE */ |
Gilles Peskine | 7da96b0 | 2018-08-17 18:45:42 +0200 | [diff] [blame] | 3746 | void aead_decrypt( int key_type_arg, data_t *key_data, |
| 3747 | int alg_arg, |
| 3748 | data_t *nonce, |
| 3749 | data_t *additional_data, |
| 3750 | data_t *input_data, |
| 3751 | data_t *expected_data, |
| 3752 | int expected_result_arg ) |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3753 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3754 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3755 | psa_key_type_t key_type = key_type_arg; |
| 3756 | psa_algorithm_t alg = alg_arg; |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3757 | size_t key_bits; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3758 | unsigned char *output_data = NULL; |
| 3759 | size_t output_size = 0; |
| 3760 | size_t output_length = 0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3761 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 3762 | psa_status_t expected_result = expected_result_arg; |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3763 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3764 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 3765 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3766 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 3767 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 3768 | psa_set_key_algorithm( &attributes, alg ); |
| 3769 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3770 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 3771 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3772 | &key ) ); |
Bence Szépkúti | ec174e2 | 2021-03-19 18:46:15 +0100 | [diff] [blame] | 3773 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 3774 | key_bits = psa_get_key_bits( &attributes ); |
| 3775 | |
| 3776 | output_size = input_data->len - PSA_AEAD_TAG_LENGTH( key_type, key_bits, |
| 3777 | alg ); |
| 3778 | if( expected_result != PSA_ERROR_INVALID_ARGUMENT && |
| 3779 | expected_result != PSA_ERROR_NOT_SUPPORTED ) |
| 3780 | { |
| 3781 | /* For all currently defined algorithms, PSA_AEAD_DECRYPT_OUTPUT_SIZE |
| 3782 | * should be exact. */ |
| 3783 | TEST_EQUAL( output_size, |
| 3784 | PSA_AEAD_DECRYPT_OUTPUT_SIZE( key_type, alg, input_data->len ) ); |
| 3785 | TEST_ASSERT( output_size <= |
| 3786 | PSA_AEAD_DECRYPT_OUTPUT_MAX_SIZE( input_data->len ) ); |
| 3787 | } |
| 3788 | ASSERT_ALLOC( output_data, output_size ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3789 | |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3790 | status = psa_aead_decrypt( key, alg, |
| 3791 | nonce->x, nonce->len, |
| 3792 | additional_data->x, |
| 3793 | additional_data->len, |
| 3794 | input_data->x, input_data->len, |
| 3795 | output_data, output_size, |
| 3796 | &output_length ); |
| 3797 | |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3798 | /* If the operation is not supported, just skip and not fail in case the |
| 3799 | * decryption involves a common limitation of cryptography hardwares and |
| 3800 | * an alternative implementation. */ |
| 3801 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3802 | { |
Ronald Cron | 28a45ed | 2021-02-09 20:35:42 +0100 | [diff] [blame] | 3803 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 3804 | 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] | 3805 | } |
Steven Cooreman | d588ea1 | 2021-01-11 19:36:04 +0100 | [diff] [blame] | 3806 | |
| 3807 | TEST_EQUAL( status, expected_result ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3808 | |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 3809 | if( expected_result == PSA_SUCCESS ) |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 3810 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 3811 | output_data, output_length ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3812 | |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3813 | exit: |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 3814 | psa_destroy_key( key ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3815 | mbedtls_free( output_data ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 3816 | PSA_DONE( ); |
Gilles Peskine | a1cac84 | 2018-06-11 19:33:02 +0200 | [diff] [blame] | 3817 | } |
| 3818 | /* END_CASE */ |
| 3819 | |
| 3820 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3821 | void aead_multipart_encrypt( int key_type_arg, data_t *key_data, |
| 3822 | int alg_arg, |
| 3823 | data_t *nonce, |
| 3824 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3825 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3826 | int do_set_lengths, |
| 3827 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3828 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3829 | size_t ad_part_len = 0; |
| 3830 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 3831 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3832 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 3833 | 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] | 3834 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 3835 | mbedtls_test_set_step( ad_part_len ); |
| 3836 | |
| 3837 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3838 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 3839 | if( ad_part_len & 0x01 ) |
| 3840 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 3841 | else |
| 3842 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3843 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 3844 | |
| 3845 | /* Split ad into length(ad_part_len) parts. */ |
| 3846 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3847 | alg_arg, nonce, |
| 3848 | additional_data, |
| 3849 | ad_part_len, |
| 3850 | input_data, -1, |
| 3851 | set_lengths_method, |
| 3852 | expected_output, |
| 3853 | 1, 0 ) ) |
| 3854 | break; |
| 3855 | |
| 3856 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 3857 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 3858 | |
| 3859 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3860 | alg_arg, nonce, |
| 3861 | additional_data, |
| 3862 | ad_part_len, |
| 3863 | input_data, -1, |
| 3864 | set_lengths_method, |
| 3865 | expected_output, |
| 3866 | 1, 1 ) ) |
| 3867 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3868 | } |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3869 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 3870 | 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] | 3871 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 3872 | /* Split data into length(data_part_len) parts. */ |
| 3873 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 3874 | |
| 3875 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3876 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 3877 | if( data_part_len & 0x01 ) |
| 3878 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 3879 | else |
| 3880 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3881 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3882 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 3883 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3884 | alg_arg, nonce, |
| 3885 | additional_data, -1, |
| 3886 | input_data, data_part_len, |
| 3887 | set_lengths_method, |
| 3888 | expected_output, |
| 3889 | 1, 0 ) ) |
| 3890 | break; |
| 3891 | |
| 3892 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3893 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3894 | |
| 3895 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3896 | alg_arg, nonce, |
| 3897 | additional_data, -1, |
| 3898 | input_data, data_part_len, |
| 3899 | set_lengths_method, |
| 3900 | expected_output, |
| 3901 | 1, 1 ) ) |
| 3902 | break; |
| 3903 | } |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3904 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 3905 | /* Goto is required to silence warnings about unused labels, as we |
| 3906 | * don't actually do any test assertions in this function. */ |
| 3907 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3908 | } |
| 3909 | /* END_CASE */ |
| 3910 | |
| 3911 | /* BEGIN_CASE */ |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3912 | void aead_multipart_decrypt( int key_type_arg, data_t *key_data, |
| 3913 | int alg_arg, |
| 3914 | data_t *nonce, |
| 3915 | data_t *additional_data, |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3916 | data_t *input_data, |
Paul Elliott | 97fd1ba | 2021-07-21 18:46:06 +0100 | [diff] [blame] | 3917 | int do_set_lengths, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 3918 | data_t *expected_output ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3919 | { |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3920 | size_t ad_part_len = 0; |
| 3921 | size_t data_part_len = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 3922 | set_lengths_method_t set_lengths_method = DO_NOT_SET_LENGTHS; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3923 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 3924 | 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] | 3925 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 3926 | /* Split ad into length(ad_part_len) parts. */ |
| 3927 | mbedtls_test_set_step( ad_part_len ); |
| 3928 | |
| 3929 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3930 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 3931 | if( ad_part_len & 0x01 ) |
| 3932 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 3933 | else |
| 3934 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3935 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 3936 | |
| 3937 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3938 | alg_arg, nonce, |
| 3939 | additional_data, |
| 3940 | ad_part_len, |
| 3941 | input_data, -1, |
| 3942 | set_lengths_method, |
| 3943 | expected_output, |
| 3944 | 0, 0 ) ) |
| 3945 | break; |
| 3946 | |
| 3947 | /* length(0) part, length(ad_part_len) part, length(0) part... */ |
| 3948 | mbedtls_test_set_step( 1000 + ad_part_len ); |
| 3949 | |
| 3950 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3951 | alg_arg, nonce, |
| 3952 | additional_data, |
| 3953 | ad_part_len, |
| 3954 | input_data, -1, |
| 3955 | set_lengths_method, |
| 3956 | expected_output, |
| 3957 | 0, 1 ) ) |
| 3958 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3959 | } |
| 3960 | |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 3961 | 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] | 3962 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 3963 | /* Split data into length(data_part_len) parts. */ |
| 3964 | mbedtls_test_set_step( 2000 + data_part_len ); |
| 3965 | |
| 3966 | if( do_set_lengths ) |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3967 | { |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 3968 | if( data_part_len & 0x01 ) |
| 3969 | set_lengths_method = SET_LENGTHS_AFTER_NONCE; |
| 3970 | else |
| 3971 | set_lengths_method = SET_LENGTHS_BEFORE_NONCE; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3972 | } |
Paul Elliott | 32f46ba | 2021-09-23 18:24:36 +0100 | [diff] [blame] | 3973 | |
| 3974 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3975 | alg_arg, nonce, |
| 3976 | additional_data, -1, |
| 3977 | input_data, data_part_len, |
| 3978 | set_lengths_method, |
| 3979 | expected_output, |
| 3980 | 0, 0 ) ) |
| 3981 | break; |
| 3982 | |
| 3983 | /* length(0) part, length(data_part_len) part, length(0) part... */ |
| 3984 | mbedtls_test_set_step( 3000 + data_part_len ); |
| 3985 | |
| 3986 | if( !aead_multipart_internal_func( key_type_arg, key_data, |
| 3987 | alg_arg, nonce, |
| 3988 | additional_data, -1, |
| 3989 | input_data, data_part_len, |
| 3990 | set_lengths_method, |
| 3991 | expected_output, |
| 3992 | 0, 1 ) ) |
| 3993 | break; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3994 | } |
| 3995 | |
Paul Elliott | 8fc4516 | 2021-06-23 16:06:01 +0100 | [diff] [blame] | 3996 | /* Goto is required to silence warnings about unused labels, as we |
| 3997 | * don't actually do any test assertions in this function. */ |
Paul Elliott | d3f8241 | 2021-06-16 16:52:21 +0100 | [diff] [blame] | 3998 | goto exit; |
Paul Elliott | 0023e0a | 2021-04-27 10:06:22 +0100 | [diff] [blame] | 3999 | } |
| 4000 | /* END_CASE */ |
| 4001 | |
| 4002 | /* BEGIN_CASE */ |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4003 | void aead_multipart_generate_nonce( int key_type_arg, data_t *key_data, |
| 4004 | int alg_arg, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4005 | int nonce_length, |
| 4006 | int expected_nonce_length_arg, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4007 | data_t *additional_data, |
| 4008 | data_t *input_data, |
| 4009 | int expected_status_arg ) |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4010 | { |
| 4011 | |
| 4012 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4013 | psa_key_type_t key_type = key_type_arg; |
| 4014 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4015 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4016 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 4017 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4018 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 4019 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4020 | size_t actual_nonce_length = 0; |
| 4021 | size_t expected_nonce_length = expected_nonce_length_arg; |
| 4022 | unsigned char *output = NULL; |
| 4023 | unsigned char *ciphertext = NULL; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4024 | size_t output_size = 0; |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4025 | size_t ciphertext_size = 0; |
| 4026 | size_t ciphertext_length = 0; |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4027 | size_t tag_length = 0; |
| 4028 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4029 | |
| 4030 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4031 | |
| 4032 | psa_set_key_usage_flags( & attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4033 | psa_set_key_algorithm( & attributes, alg ); |
| 4034 | psa_set_key_type( & attributes, key_type ); |
| 4035 | |
| 4036 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4037 | &key ) ); |
| 4038 | |
| 4039 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4040 | |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4041 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 4042 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4043 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4044 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4045 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4046 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4047 | TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4048 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4049 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4050 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4051 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 4052 | |
| 4053 | /* If the operation is not supported, just skip and not fail in case the |
| 4054 | * encryption involves a common limitation of cryptography hardwares and |
| 4055 | * an alternative implementation. */ |
| 4056 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4057 | { |
| 4058 | 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] | 4059 | 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] | 4060 | } |
| 4061 | |
| 4062 | PSA_ASSERT( status ); |
| 4063 | |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4064 | status = psa_aead_generate_nonce( &operation, nonce_buffer, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4065 | nonce_length, |
| 4066 | &actual_nonce_length ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4067 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 4068 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4069 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4070 | TEST_EQUAL( actual_nonce_length, expected_nonce_length ); |
Paul Elliott | d85f547 | 2021-07-16 18:20:16 +0100 | [diff] [blame] | 4071 | |
Paul Elliott | 88ecbe1 | 2021-09-22 17:23:03 +0100 | [diff] [blame] | 4072 | if( expected_status == PSA_SUCCESS ) |
| 4073 | TEST_EQUAL( actual_nonce_length, PSA_AEAD_NONCE_LENGTH( key_type, |
| 4074 | alg ) ); |
| 4075 | |
Paul Elliott | d79c5c5 | 2021-10-06 21:49:41 +0100 | [diff] [blame] | 4076 | TEST_ASSERT( actual_nonce_length <= PSA_AEAD_NONCE_MAX_SIZE ); |
Paul Elliott | e0fcb3b | 2021-07-16 18:52:03 +0100 | [diff] [blame] | 4077 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 4078 | if( expected_status == PSA_SUCCESS ) |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4079 | { |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4080 | /* Ensure we can still complete operation. */ |
Paul Elliott | d79c5c5 | 2021-10-06 21:49:41 +0100 | [diff] [blame] | 4081 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4082 | input_data->len ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4083 | |
| 4084 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 4085 | additional_data->len ) ); |
| 4086 | |
| 4087 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4088 | output, output_size, |
| 4089 | &ciphertext_length ) ); |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4090 | |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4091 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 4092 | &ciphertext_length, tag_buffer, |
Paul Elliott | 3bd5dba | 2021-06-23 17:14:40 +0100 | [diff] [blame] | 4093 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 4094 | } |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4095 | |
| 4096 | exit: |
| 4097 | psa_destroy_key( key ); |
Paul Elliott | f127763 | 2021-08-24 18:11:37 +0100 | [diff] [blame] | 4098 | mbedtls_free( output ); |
| 4099 | mbedtls_free( ciphertext ); |
Paul Elliott | 8eb9daf | 2021-06-04 16:42:21 +0100 | [diff] [blame] | 4100 | psa_aead_abort( &operation ); |
| 4101 | PSA_DONE( ); |
| 4102 | } |
| 4103 | /* END_CASE */ |
| 4104 | |
| 4105 | /* BEGIN_CASE */ |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4106 | void aead_multipart_set_nonce( int key_type_arg, data_t *key_data, |
| 4107 | int alg_arg, |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4108 | int nonce_length_arg, |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4109 | data_t *additional_data, |
| 4110 | data_t *input_data, |
| 4111 | int expected_status_arg ) |
| 4112 | { |
| 4113 | |
| 4114 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4115 | psa_key_type_t key_type = key_type_arg; |
| 4116 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4117 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4118 | uint8_t *nonce_buffer = NULL; |
| 4119 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4120 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 4121 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4122 | unsigned char *output = NULL; |
| 4123 | unsigned char *ciphertext = NULL; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4124 | size_t nonce_length; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4125 | size_t output_size = 0; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4126 | size_t ciphertext_size = 0; |
| 4127 | size_t ciphertext_length = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4128 | size_t tag_length = 0; |
| 4129 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4130 | size_t index = 0; |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4131 | |
| 4132 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4133 | |
| 4134 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4135 | psa_set_key_algorithm( &attributes, alg ); |
| 4136 | psa_set_key_type( &attributes, key_type ); |
| 4137 | |
| 4138 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4139 | &key ) ); |
| 4140 | |
| 4141 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4142 | |
| 4143 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 4144 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4145 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4146 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4147 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4148 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4149 | TEST_ASSERT( ciphertext_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4150 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4151 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4152 | |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4153 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 4154 | |
| 4155 | /* If the operation is not supported, just skip and not fail in case the |
| 4156 | * encryption involves a common limitation of cryptography hardwares and |
| 4157 | * an alternative implementation. */ |
| 4158 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4159 | { |
| 4160 | 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] | 4161 | 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] | 4162 | } |
| 4163 | |
| 4164 | PSA_ASSERT( status ); |
| 4165 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4166 | /* -1 == zero length and valid buffer, 0 = zero length and NULL buffer. */ |
| 4167 | if( nonce_length_arg == -1 ) |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4168 | { |
Paul Elliott | 5e69aa5 | 2021-08-25 17:24:37 +0100 | [diff] [blame] | 4169 | /* Arbitrary size buffer, to test zero length valid buffer. */ |
| 4170 | ASSERT_ALLOC( nonce_buffer, 4 ); |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4171 | nonce_length = 0; |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 4172 | } |
| 4173 | else |
| 4174 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4175 | /* If length is zero, then this will return NULL. */ |
| 4176 | nonce_length = ( size_t ) nonce_length_arg; |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4177 | ASSERT_ALLOC( nonce_buffer, nonce_length ); |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 4178 | |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4179 | if( nonce_buffer ) |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 4180 | { |
Paul Elliott | 4023ffd | 2021-09-10 16:21:22 +0100 | [diff] [blame] | 4181 | for( index = 0; index < nonce_length - 1; ++index ) |
| 4182 | { |
| 4183 | nonce_buffer[index] = 'a' + index; |
| 4184 | } |
Paul Elliott | 66696b5 | 2021-08-16 18:42:41 +0100 | [diff] [blame] | 4185 | } |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4186 | } |
| 4187 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4188 | status = psa_aead_set_nonce( &operation, nonce_buffer, nonce_length ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4189 | |
Paul Elliott | 693bf31 | 2021-07-23 17:40:41 +0100 | [diff] [blame] | 4190 | TEST_EQUAL( status, expected_status ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4191 | |
| 4192 | if( expected_status == PSA_SUCCESS ) |
| 4193 | { |
| 4194 | /* Ensure we can still complete operation. */ |
Paul Elliott | e4c08ed | 2021-10-06 18:53:04 +0100 | [diff] [blame] | 4195 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4196 | input_data->len ) ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4197 | |
| 4198 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 4199 | additional_data->len ) ); |
| 4200 | |
| 4201 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4202 | output, output_size, |
| 4203 | &ciphertext_length ) ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4204 | |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4205 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 4206 | &ciphertext_length, tag_buffer, |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4207 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 4208 | } |
| 4209 | |
| 4210 | exit: |
| 4211 | psa_destroy_key( key ); |
Paul Elliott | 6f0e720 | 2021-08-25 12:57:18 +0100 | [diff] [blame] | 4212 | mbedtls_free( output ); |
| 4213 | mbedtls_free( ciphertext ); |
Paul Elliott | 863864a | 2021-07-23 17:28:31 +0100 | [diff] [blame] | 4214 | mbedtls_free( nonce_buffer ); |
| 4215 | psa_aead_abort( &operation ); |
| 4216 | PSA_DONE( ); |
| 4217 | } |
| 4218 | /* END_CASE */ |
| 4219 | |
| 4220 | /* BEGIN_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4221 | void aead_multipart_update_buffer_test( int key_type_arg, data_t *key_data, |
| 4222 | int alg_arg, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 4223 | int output_size_arg, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4224 | data_t *nonce, |
| 4225 | data_t *additional_data, |
| 4226 | data_t *input_data, |
| 4227 | int expected_status_arg ) |
| 4228 | { |
| 4229 | |
| 4230 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4231 | psa_key_type_t key_type = key_type_arg; |
| 4232 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4233 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4234 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4235 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 4236 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 4237 | unsigned char *output = NULL; |
| 4238 | unsigned char *ciphertext = NULL; |
| 4239 | size_t output_size = output_size_arg; |
| 4240 | size_t ciphertext_size = 0; |
| 4241 | size_t ciphertext_length = 0; |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4242 | size_t tag_length = 0; |
| 4243 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 4244 | |
| 4245 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4246 | |
| 4247 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4248 | psa_set_key_algorithm( &attributes, alg ); |
| 4249 | psa_set_key_type( &attributes, key_type ); |
| 4250 | |
| 4251 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4252 | &key ) ); |
| 4253 | |
| 4254 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4255 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 4256 | ASSERT_ALLOC( output, output_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4257 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 4258 | ciphertext_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4259 | |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 4260 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4261 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4262 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 4263 | |
| 4264 | /* If the operation is not supported, just skip and not fail in case the |
| 4265 | * encryption involves a common limitation of cryptography hardwares and |
| 4266 | * an alternative implementation. */ |
| 4267 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4268 | { |
| 4269 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4270 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4271 | } |
| 4272 | |
| 4273 | PSA_ASSERT( status ); |
| 4274 | |
Paul Elliott | 47b9a14 | 2021-10-07 15:04:57 +0100 | [diff] [blame] | 4275 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4276 | input_data->len ) ); |
| 4277 | |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4278 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4279 | |
| 4280 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 4281 | additional_data->len ) ); |
| 4282 | |
| 4283 | status = psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 4284 | output, output_size, &ciphertext_length ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4285 | |
| 4286 | TEST_EQUAL( status, expected_status ); |
| 4287 | |
| 4288 | if( expected_status == PSA_SUCCESS ) |
| 4289 | { |
| 4290 | /* Ensure we can still complete operation. */ |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 4291 | PSA_ASSERT( psa_aead_finish( &operation, ciphertext, ciphertext_size, |
| 4292 | &ciphertext_length, tag_buffer, |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4293 | PSA_AEAD_TAG_MAX_SIZE, &tag_length ) ); |
| 4294 | } |
| 4295 | |
| 4296 | exit: |
| 4297 | psa_destroy_key( key ); |
Paul Elliott | c6d11d0 | 2021-09-01 12:04:23 +0100 | [diff] [blame] | 4298 | mbedtls_free( output ); |
| 4299 | mbedtls_free( ciphertext ); |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4300 | psa_aead_abort( &operation ); |
| 4301 | PSA_DONE( ); |
| 4302 | } |
| 4303 | /* END_CASE */ |
| 4304 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4305 | /* BEGIN_CASE */ |
| 4306 | void aead_multipart_finish_buffer_test( int key_type_arg, data_t *key_data, |
| 4307 | int alg_arg, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4308 | int finish_ciphertext_size_arg, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 4309 | int tag_size_arg, |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4310 | data_t *nonce, |
| 4311 | data_t *additional_data, |
| 4312 | data_t *input_data, |
| 4313 | int expected_status_arg ) |
| 4314 | { |
| 4315 | |
| 4316 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4317 | psa_key_type_t key_type = key_type_arg; |
| 4318 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4319 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4320 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4321 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 4322 | psa_status_t expected_status = expected_status_arg; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4323 | unsigned char *ciphertext = NULL; |
| 4324 | unsigned char *finish_ciphertext = NULL; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 4325 | unsigned char *tag_buffer = NULL; |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4326 | size_t ciphertext_size = 0; |
| 4327 | size_t ciphertext_length = 0; |
| 4328 | size_t finish_ciphertext_size = ( size_t ) finish_ciphertext_size_arg; |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 4329 | size_t tag_size = ( size_t ) tag_size_arg; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4330 | size_t tag_length = 0; |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4331 | |
| 4332 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4333 | |
| 4334 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 4335 | psa_set_key_algorithm( &attributes, alg ); |
| 4336 | psa_set_key_type( &attributes, key_type ); |
| 4337 | |
| 4338 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4339 | &key ) ); |
| 4340 | |
| 4341 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4342 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4343 | 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] | 4344 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4345 | ASSERT_ALLOC( ciphertext, ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4346 | |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4347 | ASSERT_ALLOC( finish_ciphertext, finish_ciphertext_size ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4348 | |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 4349 | ASSERT_ALLOC( tag_buffer, tag_size ); |
| 4350 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4351 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 4352 | |
| 4353 | /* If the operation is not supported, just skip and not fail in case the |
| 4354 | * encryption involves a common limitation of cryptography hardwares and |
| 4355 | * an alternative implementation. */ |
| 4356 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4357 | { |
| 4358 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4359 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4360 | } |
| 4361 | |
| 4362 | PSA_ASSERT( status ); |
| 4363 | |
| 4364 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4365 | |
Paul Elliott | 76bda48 | 2021-10-07 17:07:23 +0100 | [diff] [blame] | 4366 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4367 | input_data->len ) ); |
| 4368 | |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4369 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 4370 | additional_data->len ) ); |
| 4371 | |
| 4372 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, input_data->len, |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4373 | ciphertext, ciphertext_size, &ciphertext_length ) ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4374 | |
| 4375 | /* Ensure we can still complete operation. */ |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4376 | status = psa_aead_finish( &operation, finish_ciphertext, |
| 4377 | finish_ciphertext_size, |
| 4378 | &ciphertext_length, tag_buffer, |
Paul Elliott | 719c132 | 2021-09-13 18:27:22 +0100 | [diff] [blame] | 4379 | tag_size, &tag_length ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4380 | |
| 4381 | TEST_EQUAL( status, expected_status ); |
| 4382 | |
| 4383 | exit: |
| 4384 | psa_destroy_key( key ); |
Paul Elliott | e58cb1e | 2021-09-10 18:36:00 +0100 | [diff] [blame] | 4385 | mbedtls_free( ciphertext ); |
| 4386 | mbedtls_free( finish_ciphertext ); |
Paul Elliott | 4a76088 | 2021-09-20 09:42:21 +0100 | [diff] [blame] | 4387 | mbedtls_free( tag_buffer ); |
Paul Elliott | 91b021e | 2021-07-23 18:52:31 +0100 | [diff] [blame] | 4388 | psa_aead_abort( &operation ); |
| 4389 | PSA_DONE( ); |
| 4390 | } |
| 4391 | /* END_CASE */ |
Paul Elliott | 43fbda6 | 2021-07-23 18:30:59 +0100 | [diff] [blame] | 4392 | |
| 4393 | /* BEGIN_CASE */ |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4394 | void aead_multipart_verify( int key_type_arg, data_t *key_data, |
| 4395 | int alg_arg, |
| 4396 | data_t *nonce, |
| 4397 | data_t *additional_data, |
| 4398 | data_t *input_data, |
| 4399 | data_t *tag, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 4400 | int tag_usage_arg, |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4401 | int expected_status_arg ) |
| 4402 | { |
| 4403 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4404 | psa_key_type_t key_type = key_type_arg; |
| 4405 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4406 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4407 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4408 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 4409 | psa_status_t expected_status = expected_status_arg; |
| 4410 | unsigned char *plaintext = NULL; |
| 4411 | unsigned char *finish_plaintext = NULL; |
| 4412 | size_t plaintext_size = 0; |
| 4413 | size_t plaintext_length = 0; |
| 4414 | size_t verify_plaintext_size = 0; |
Paul Elliott | bb979e7 | 2021-09-22 12:54:42 +0100 | [diff] [blame] | 4415 | tag_usage_method_t tag_usage = tag_usage_arg; |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 4416 | unsigned char *tag_buffer = NULL; |
| 4417 | size_t tag_size = 0; |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4418 | |
| 4419 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4420 | |
| 4421 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 4422 | psa_set_key_algorithm( &attributes, alg ); |
| 4423 | psa_set_key_type( &attributes, key_type ); |
| 4424 | |
| 4425 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4426 | &key ) ); |
| 4427 | |
| 4428 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4429 | |
| 4430 | plaintext_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, |
| 4431 | input_data->len ); |
| 4432 | |
| 4433 | ASSERT_ALLOC( plaintext, plaintext_size ); |
| 4434 | |
| 4435 | verify_plaintext_size = PSA_AEAD_VERIFY_OUTPUT_SIZE( key_type, alg ); |
| 4436 | |
| 4437 | ASSERT_ALLOC( finish_plaintext, verify_plaintext_size ); |
| 4438 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4439 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 4440 | |
| 4441 | /* If the operation is not supported, just skip and not fail in case the |
| 4442 | * encryption involves a common limitation of cryptography hardwares and |
| 4443 | * an alternative implementation. */ |
| 4444 | if( status == PSA_ERROR_NOT_SUPPORTED ) |
| 4445 | { |
| 4446 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_AES_192( key_type, key_data->len * 8 ); |
| 4447 | MBEDTLS_TEST_PSA_SKIP_IF_ALT_GCM_NOT_12BYTES_NONCE( alg, nonce->len ); |
| 4448 | } |
| 4449 | |
| 4450 | PSA_ASSERT( status ); |
| 4451 | |
| 4452 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4453 | |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 4454 | status = psa_aead_set_lengths( &operation, additional_data->len, |
| 4455 | input_data->len ); |
| 4456 | |
| 4457 | if( status != PSA_SUCCESS ) |
| 4458 | { |
| 4459 | /* Invalid tag lengths are detected in CCM at this point, as they |
Paul Elliott | 37ec16b | 2021-12-08 20:14:49 +0000 | [diff] [blame] | 4460 | * would be written into the first block. They should really be |
| 4461 | * detected in psa_aead_encrypt/decrypt_setup, and will be fixed |
| 4462 | * to do so in the future, until that point, this is a |
| 4463 | * workaround.*/ |
Paul Elliott | fec6f37 | 2021-10-06 17:15:02 +0100 | [diff] [blame] | 4464 | TEST_EQUAL( status, expected_status ); |
| 4465 | goto exit; |
| 4466 | } |
| 4467 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4468 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 4469 | additional_data->len ) ); |
| 4470 | |
| 4471 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 4472 | input_data->len, |
| 4473 | plaintext, plaintext_size, |
| 4474 | &plaintext_length ) ); |
| 4475 | |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 4476 | if( tag_usage == USE_GIVEN_TAG ) |
| 4477 | { |
| 4478 | tag_buffer = tag->x; |
| 4479 | tag_size = tag->len; |
| 4480 | } |
| 4481 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4482 | status = psa_aead_verify( &operation, finish_plaintext, |
| 4483 | verify_plaintext_size, |
| 4484 | &plaintext_length, |
Paul Elliott | 1c67e0b | 2021-09-19 13:11:50 +0100 | [diff] [blame] | 4485 | tag_buffer, tag_size ); |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4486 | |
| 4487 | TEST_EQUAL( status, expected_status ); |
| 4488 | |
| 4489 | exit: |
| 4490 | psa_destroy_key( key ); |
| 4491 | mbedtls_free( plaintext ); |
| 4492 | mbedtls_free( finish_plaintext ); |
| 4493 | psa_aead_abort( &operation ); |
| 4494 | PSA_DONE( ); |
| 4495 | } |
| 4496 | /* END_CASE */ |
| 4497 | |
Paul Elliott | 9961a66 | 2021-09-17 19:19:02 +0100 | [diff] [blame] | 4498 | /* BEGIN_CASE */ |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 4499 | void aead_multipart_setup( int key_type_arg, data_t *key_data, |
| 4500 | int alg_arg, int expected_status_arg ) |
| 4501 | { |
| 4502 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4503 | psa_key_type_t key_type = key_type_arg; |
| 4504 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4505 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 4506 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4507 | psa_status_t status = PSA_ERROR_GENERIC_ERROR; |
| 4508 | psa_status_t expected_status = expected_status_arg; |
| 4509 | |
| 4510 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4511 | |
| 4512 | psa_set_key_usage_flags( &attributes, |
| 4513 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4514 | psa_set_key_algorithm( &attributes, alg ); |
| 4515 | psa_set_key_type( &attributes, key_type ); |
| 4516 | |
| 4517 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4518 | &key ) ); |
| 4519 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 4520 | status = psa_aead_encrypt_setup( &operation, key, alg ); |
| 4521 | |
| 4522 | TEST_EQUAL( status, expected_status ); |
| 4523 | |
| 4524 | psa_aead_abort( &operation ); |
| 4525 | |
Paul Elliott | 5221ef6 | 2021-09-19 17:33:03 +0100 | [diff] [blame] | 4526 | status = psa_aead_decrypt_setup( &operation, key, alg ); |
| 4527 | |
| 4528 | TEST_EQUAL(status, expected_status ); |
| 4529 | |
| 4530 | exit: |
| 4531 | psa_destroy_key( key ); |
| 4532 | psa_aead_abort( &operation ); |
| 4533 | PSA_DONE( ); |
| 4534 | } |
| 4535 | /* END_CASE */ |
| 4536 | |
| 4537 | /* BEGIN_CASE */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4538 | void aead_multipart_state_test( int key_type_arg, data_t *key_data, |
| 4539 | int alg_arg, |
| 4540 | data_t *nonce, |
| 4541 | data_t *additional_data, |
| 4542 | data_t *input_data ) |
| 4543 | { |
| 4544 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 4545 | psa_key_type_t key_type = key_type_arg; |
| 4546 | psa_algorithm_t alg = alg_arg; |
Paul Elliott | fbb4c6d | 2021-09-22 16:44:21 +0100 | [diff] [blame] | 4547 | psa_aead_operation_t operation = PSA_AEAD_OPERATION_INIT; |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4548 | unsigned char *output_data = NULL; |
| 4549 | unsigned char *final_data = NULL; |
| 4550 | size_t output_size = 0; |
| 4551 | size_t finish_output_size = 0; |
| 4552 | size_t output_length = 0; |
| 4553 | size_t key_bits = 0; |
| 4554 | size_t tag_length = 0; |
| 4555 | size_t tag_size = 0; |
| 4556 | size_t nonce_length = 0; |
| 4557 | uint8_t nonce_buffer[PSA_AEAD_NONCE_MAX_SIZE]; |
| 4558 | uint8_t tag_buffer[PSA_AEAD_TAG_MAX_SIZE]; |
| 4559 | size_t output_part_length = 0; |
| 4560 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 4561 | |
| 4562 | PSA_ASSERT( psa_crypto_init( ) ); |
| 4563 | |
| 4564 | psa_set_key_usage_flags( & attributes, |
| 4565 | PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 4566 | psa_set_key_algorithm( & attributes, alg ); |
| 4567 | psa_set_key_type( & attributes, key_type ); |
| 4568 | |
| 4569 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 4570 | &key ) ); |
| 4571 | |
| 4572 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 4573 | key_bits = psa_get_key_bits( &attributes ); |
| 4574 | |
| 4575 | tag_length = PSA_AEAD_TAG_LENGTH( key_type, key_bits, alg ); |
| 4576 | |
| 4577 | TEST_ASSERT( tag_length <= PSA_AEAD_TAG_MAX_SIZE ); |
| 4578 | |
| 4579 | output_size = PSA_AEAD_UPDATE_OUTPUT_SIZE( key_type, alg, input_data->len ); |
| 4580 | |
| 4581 | ASSERT_ALLOC( output_data, output_size ); |
| 4582 | |
| 4583 | finish_output_size = PSA_AEAD_FINISH_OUTPUT_SIZE( key_type, alg ); |
| 4584 | |
| 4585 | TEST_ASSERT( finish_output_size <= PSA_AEAD_FINISH_OUTPUT_MAX_SIZE ); |
| 4586 | |
| 4587 | ASSERT_ALLOC( final_data, finish_output_size ); |
| 4588 | |
| 4589 | /* Test all operations error without calling setup first. */ |
| 4590 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4591 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 4592 | PSA_ERROR_BAD_STATE ); |
| 4593 | |
| 4594 | psa_aead_abort( &operation ); |
| 4595 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4596 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4597 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4598 | &nonce_length ), |
| 4599 | PSA_ERROR_BAD_STATE ); |
| 4600 | |
| 4601 | psa_aead_abort( &operation ); |
| 4602 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4603 | /* ------------------------------------------------------- */ |
| 4604 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4605 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4606 | input_data->len ), |
| 4607 | PSA_ERROR_BAD_STATE ); |
| 4608 | |
| 4609 | psa_aead_abort( &operation ); |
| 4610 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4611 | /* ------------------------------------------------------- */ |
| 4612 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4613 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4614 | additional_data->len ), |
| 4615 | PSA_ERROR_BAD_STATE ); |
| 4616 | |
| 4617 | psa_aead_abort( &operation ); |
| 4618 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4619 | /* ------------------------------------------------------- */ |
| 4620 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4621 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 4622 | input_data->len, output_data, |
| 4623 | output_size, &output_length ), |
| 4624 | PSA_ERROR_BAD_STATE ); |
| 4625 | |
| 4626 | psa_aead_abort( &operation ); |
| 4627 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4628 | /* ------------------------------------------------------- */ |
| 4629 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4630 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 4631 | finish_output_size, |
| 4632 | &output_part_length, |
| 4633 | tag_buffer, tag_length, |
| 4634 | &tag_size ), |
| 4635 | PSA_ERROR_BAD_STATE ); |
| 4636 | |
| 4637 | psa_aead_abort( &operation ); |
| 4638 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4639 | /* ------------------------------------------------------- */ |
| 4640 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4641 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 4642 | finish_output_size, |
| 4643 | &output_part_length, |
| 4644 | tag_buffer, |
| 4645 | tag_length ), |
| 4646 | PSA_ERROR_BAD_STATE ); |
| 4647 | |
| 4648 | psa_aead_abort( &operation ); |
| 4649 | |
| 4650 | /* Test for double setups. */ |
| 4651 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4652 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4653 | |
| 4654 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 4655 | PSA_ERROR_BAD_STATE ); |
| 4656 | |
| 4657 | psa_aead_abort( &operation ); |
| 4658 | |
Paul Elliott | 481be34 | 2021-07-16 17:38:47 +0100 | [diff] [blame] | 4659 | /* ------------------------------------------------------- */ |
| 4660 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4661 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4662 | |
| 4663 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 4664 | PSA_ERROR_BAD_STATE ); |
| 4665 | |
| 4666 | psa_aead_abort( &operation ); |
| 4667 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4668 | /* ------------------------------------------------------- */ |
| 4669 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4670 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4671 | |
| 4672 | TEST_EQUAL( psa_aead_decrypt_setup( &operation, key, alg ), |
| 4673 | PSA_ERROR_BAD_STATE ); |
| 4674 | |
| 4675 | psa_aead_abort( &operation ); |
| 4676 | |
| 4677 | /* ------------------------------------------------------- */ |
| 4678 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4679 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4680 | |
| 4681 | TEST_EQUAL( psa_aead_encrypt_setup( &operation, key, alg ), |
| 4682 | PSA_ERROR_BAD_STATE ); |
| 4683 | |
| 4684 | psa_aead_abort( &operation ); |
| 4685 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4686 | /* Test for not setting a nonce. */ |
| 4687 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4688 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4689 | |
| 4690 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4691 | additional_data->len ), |
| 4692 | PSA_ERROR_BAD_STATE ); |
| 4693 | |
| 4694 | psa_aead_abort( &operation ); |
| 4695 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 4696 | /* ------------------------------------------------------- */ |
| 4697 | |
Paul Elliott | 7f62842 | 2021-09-01 12:08:29 +0100 | [diff] [blame] | 4698 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4699 | |
| 4700 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 4701 | input_data->len, output_data, |
| 4702 | output_size, &output_length ), |
| 4703 | PSA_ERROR_BAD_STATE ); |
| 4704 | |
| 4705 | psa_aead_abort( &operation ); |
| 4706 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 4707 | /* ------------------------------------------------------- */ |
| 4708 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 4709 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4710 | |
| 4711 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 4712 | finish_output_size, |
| 4713 | &output_part_length, |
| 4714 | tag_buffer, tag_length, |
| 4715 | &tag_size ), |
| 4716 | PSA_ERROR_BAD_STATE ); |
| 4717 | |
| 4718 | psa_aead_abort( &operation ); |
| 4719 | |
| 4720 | /* ------------------------------------------------------- */ |
| 4721 | |
Paul Elliott | bdc2c68 | 2021-09-21 18:37:10 +0100 | [diff] [blame] | 4722 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4723 | |
| 4724 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 4725 | finish_output_size, |
| 4726 | &output_part_length, |
| 4727 | tag_buffer, |
| 4728 | tag_length ), |
| 4729 | PSA_ERROR_BAD_STATE ); |
| 4730 | |
| 4731 | psa_aead_abort( &operation ); |
| 4732 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4733 | /* Test for double setting nonce. */ |
| 4734 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4735 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4736 | |
| 4737 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4738 | |
| 4739 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 4740 | PSA_ERROR_BAD_STATE ); |
| 4741 | |
| 4742 | psa_aead_abort( &operation ); |
| 4743 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4744 | /* Test for double generating nonce. */ |
| 4745 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4746 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4747 | |
| 4748 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4749 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4750 | &nonce_length ) ); |
| 4751 | |
| 4752 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4753 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4754 | &nonce_length ), |
| 4755 | PSA_ERROR_BAD_STATE ); |
| 4756 | |
| 4757 | |
| 4758 | psa_aead_abort( &operation ); |
| 4759 | |
| 4760 | /* Test for generate nonce then set and vice versa */ |
| 4761 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4762 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4763 | |
| 4764 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4765 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4766 | &nonce_length ) ); |
| 4767 | |
| 4768 | TEST_EQUAL( psa_aead_set_nonce( &operation, nonce->x, nonce->len ), |
| 4769 | PSA_ERROR_BAD_STATE ); |
| 4770 | |
| 4771 | psa_aead_abort( &operation ); |
| 4772 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame^] | 4773 | /* Test for generating nonce after calling set lengths */ |
| 4774 | |
| 4775 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4776 | |
| 4777 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4778 | input_data->len ) ); |
| 4779 | |
| 4780 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4781 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4782 | &nonce_length ) ); |
| 4783 | |
| 4784 | psa_aead_abort( &operation ); |
| 4785 | |
| 4786 | /* Test for generating nonce after calling set lengths with UINT32_MAX length */ |
| 4787 | |
| 4788 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4789 | |
| 4790 | if( operation.alg == PSA_ALG_CCM ) |
| 4791 | { |
| 4792 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 4793 | input_data->len ), |
| 4794 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4795 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4796 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4797 | &nonce_length ), |
| 4798 | PSA_ERROR_BAD_STATE ); |
| 4799 | } |
| 4800 | else |
| 4801 | { |
| 4802 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 4803 | input_data->len ) ); |
| 4804 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4805 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4806 | &nonce_length ) ); |
| 4807 | } |
| 4808 | |
| 4809 | psa_aead_abort( &operation ); |
| 4810 | |
| 4811 | /* Test for generating nonce after calling set lengths with SIZE_MAX length */ |
| 4812 | #if SIZE_MAX > UINT32_MAX |
| 4813 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4814 | |
| 4815 | if( operation.alg == PSA_ALG_CCM || operation.alg == PSA_ALG_GCM ) |
| 4816 | { |
| 4817 | TEST_EQUAL( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 4818 | input_data->len ), |
| 4819 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4820 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4821 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4822 | &nonce_length ), |
| 4823 | PSA_ERROR_BAD_STATE ); |
| 4824 | } |
| 4825 | else |
| 4826 | { |
| 4827 | PSA_ASSERT( psa_aead_set_lengths( &operation, SIZE_MAX, |
| 4828 | input_data->len ) ); |
| 4829 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4830 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4831 | &nonce_length ) ); |
| 4832 | } |
| 4833 | |
| 4834 | psa_aead_abort( &operation ); |
| 4835 | #endif |
| 4836 | |
| 4837 | /* Test for calling set lengths with a length too long, after generating nonce */ |
| 4838 | |
| 4839 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4840 | |
| 4841 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4842 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4843 | &nonce_length ) ); |
| 4844 | |
| 4845 | if( operation.alg == PSA_ALG_CCM ) |
| 4846 | { |
| 4847 | TEST_EQUAL( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 4848 | input_data->len ), |
| 4849 | PSA_ERROR_INVALID_ARGUMENT ); |
| 4850 | } |
| 4851 | else |
| 4852 | { |
| 4853 | PSA_ASSERT( psa_aead_set_lengths( &operation, UINT32_MAX, |
| 4854 | input_data->len ) ); |
| 4855 | } |
| 4856 | |
| 4857 | psa_aead_abort( &operation ); |
| 4858 | |
| 4859 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4860 | /* ------------------------------------------------------- */ |
| 4861 | |
Paul Elliott | 374a2be | 2021-07-16 17:53:40 +0100 | [diff] [blame] | 4862 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4863 | |
| 4864 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4865 | |
| 4866 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4867 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4868 | &nonce_length ), |
| 4869 | PSA_ERROR_BAD_STATE ); |
| 4870 | |
| 4871 | psa_aead_abort( &operation ); |
| 4872 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 4873 | /* Test for generating nonce in decrypt setup. */ |
| 4874 | |
Paul Elliott | 7220cae | 2021-06-22 17:25:57 +0100 | [diff] [blame] | 4875 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 4876 | |
| 4877 | TEST_EQUAL( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4878 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4879 | &nonce_length ), |
| 4880 | PSA_ERROR_BAD_STATE ); |
| 4881 | |
| 4882 | psa_aead_abort( &operation ); |
| 4883 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4884 | /* Test for setting lengths twice. */ |
| 4885 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4886 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4887 | |
| 4888 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4889 | |
| 4890 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 4891 | input_data->len ) ); |
| 4892 | |
| 4893 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4894 | input_data->len ), |
| 4895 | PSA_ERROR_BAD_STATE ); |
| 4896 | |
| 4897 | psa_aead_abort( &operation ); |
| 4898 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame^] | 4899 | /* Test for setting lengths after setting nonce + already starting data. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4900 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4901 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4902 | |
| 4903 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4904 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame^] | 4905 | if( operation.alg == PSA_ALG_CCM ) |
| 4906 | { |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 4907 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame^] | 4908 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4909 | additional_data->len ), |
| 4910 | PSA_ERROR_BAD_STATE ); |
| 4911 | } |
| 4912 | else |
| 4913 | { |
| 4914 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 4915 | additional_data->len ) ); |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 4916 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame^] | 4917 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4918 | input_data->len ), |
| 4919 | PSA_ERROR_BAD_STATE ); |
| 4920 | } |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 4921 | psa_aead_abort( &operation ); |
| 4922 | |
| 4923 | /* ------------------------------------------------------- */ |
| 4924 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 4925 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4926 | |
| 4927 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4928 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame^] | 4929 | if( operation.alg == PSA_ALG_CCM ) |
| 4930 | { |
| 4931 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 4932 | input_data->len, output_data, |
| 4933 | output_size, &output_length ), |
| 4934 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4935 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame^] | 4936 | } |
| 4937 | else |
| 4938 | { |
| 4939 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 4940 | input_data->len, output_data, |
| 4941 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 4942 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame^] | 4943 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4944 | input_data->len ), |
| 4945 | PSA_ERROR_BAD_STATE ); |
| 4946 | } |
| 4947 | psa_aead_abort( &operation ); |
| 4948 | |
| 4949 | /* ------------------------------------------------------- */ |
| 4950 | |
| 4951 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4952 | |
| 4953 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 4954 | |
| 4955 | if( operation.alg == PSA_ALG_CCM ) |
| 4956 | { |
| 4957 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 4958 | finish_output_size, |
| 4959 | &output_part_length, |
| 4960 | tag_buffer, tag_length, |
| 4961 | &tag_size ) ); |
| 4962 | } |
| 4963 | else |
| 4964 | { |
| 4965 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 4966 | finish_output_size, |
| 4967 | &output_part_length, |
| 4968 | tag_buffer, tag_length, |
| 4969 | &tag_size ) ); |
| 4970 | |
| 4971 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4972 | input_data->len ), |
| 4973 | PSA_ERROR_BAD_STATE ); |
| 4974 | } |
| 4975 | psa_aead_abort( &operation ); |
| 4976 | |
| 4977 | /* Test for setting lengths after generating nonce + already starting data. */ |
| 4978 | |
| 4979 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 4980 | |
| 4981 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 4982 | PSA_AEAD_NONCE_MAX_SIZE, |
| 4983 | &nonce_length ) ); |
| 4984 | if( operation.alg == PSA_ALG_CCM ) |
| 4985 | { |
| 4986 | |
| 4987 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 4988 | additional_data->len ), |
| 4989 | PSA_ERROR_BAD_STATE ); |
| 4990 | } |
| 4991 | else |
| 4992 | { |
| 4993 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 4994 | additional_data->len ) ); |
| 4995 | |
| 4996 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 4997 | input_data->len ), |
| 4998 | PSA_ERROR_BAD_STATE ); |
| 4999 | } |
| 5000 | psa_aead_abort( &operation ); |
| 5001 | |
| 5002 | /* ------------------------------------------------------- */ |
| 5003 | |
| 5004 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5005 | |
| 5006 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5007 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5008 | &nonce_length ) ); |
| 5009 | if( operation.alg == PSA_ALG_CCM ) |
| 5010 | { |
| 5011 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5012 | input_data->len, output_data, |
| 5013 | output_size, &output_length ), |
| 5014 | PSA_ERROR_BAD_STATE ); |
| 5015 | |
| 5016 | } |
| 5017 | else |
| 5018 | { |
| 5019 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5020 | input_data->len, output_data, |
| 5021 | output_size, &output_length ) ); |
| 5022 | |
| 5023 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5024 | input_data->len ), |
| 5025 | PSA_ERROR_BAD_STATE ); |
| 5026 | } |
| 5027 | psa_aead_abort( &operation ); |
| 5028 | |
| 5029 | /* ------------------------------------------------------- */ |
| 5030 | |
| 5031 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5032 | |
| 5033 | PSA_ASSERT( psa_aead_generate_nonce( &operation, nonce_buffer, |
| 5034 | PSA_AEAD_NONCE_MAX_SIZE, |
| 5035 | &nonce_length ) ); |
| 5036 | if( operation.alg == PSA_ALG_CCM ) |
| 5037 | { |
| 5038 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 5039 | finish_output_size, |
| 5040 | &output_part_length, |
| 5041 | tag_buffer, tag_length, |
| 5042 | &tag_size ) ); |
| 5043 | } |
| 5044 | else |
| 5045 | { |
| 5046 | PSA_ASSERT( psa_aead_finish( &operation, final_data, |
| 5047 | finish_output_size, |
| 5048 | &output_part_length, |
| 5049 | tag_buffer, tag_length, |
| 5050 | &tag_size ) ); |
| 5051 | |
| 5052 | TEST_EQUAL( psa_aead_set_lengths( &operation, additional_data->len, |
| 5053 | input_data->len ), |
| 5054 | PSA_ERROR_BAD_STATE ); |
| 5055 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5056 | psa_aead_abort( &operation ); |
| 5057 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 5058 | /* Test for not sending any additional data or data after setting non zero |
| 5059 | * lengths for them. (encrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5060 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5061 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5062 | |
| 5063 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5064 | |
| 5065 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5066 | input_data->len ) ); |
| 5067 | |
| 5068 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5069 | finish_output_size, |
| 5070 | &output_part_length, |
| 5071 | tag_buffer, tag_length, |
| 5072 | &tag_size ), |
| 5073 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5074 | |
| 5075 | psa_aead_abort( &operation ); |
| 5076 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 5077 | /* Test for not sending any additional data or data after setting non-zero |
| 5078 | * lengths for them. (decrypt) */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5079 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5080 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5081 | |
| 5082 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5083 | |
| 5084 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5085 | input_data->len ) ); |
| 5086 | |
| 5087 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5088 | finish_output_size, |
| 5089 | &output_part_length, |
| 5090 | tag_buffer, |
| 5091 | tag_length ), |
| 5092 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5093 | |
| 5094 | psa_aead_abort( &operation ); |
| 5095 | |
Paul Elliott | 243080c | 2021-07-21 19:01:17 +0100 | [diff] [blame] | 5096 | /* Test for not sending any additional data after setting a non-zero length |
| 5097 | * for it. */ |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5098 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5099 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5100 | |
| 5101 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5102 | |
| 5103 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5104 | input_data->len ) ); |
| 5105 | |
| 5106 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5107 | input_data->len, output_data, |
| 5108 | output_size, &output_length ), |
| 5109 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5110 | |
| 5111 | psa_aead_abort( &operation ); |
| 5112 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 5113 | /* Test for not sending any data after setting a non-zero length for it.*/ |
| 5114 | |
Paul Elliott | f94bd99 | 2021-09-19 18:15:59 +0100 | [diff] [blame] | 5115 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5116 | |
| 5117 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5118 | |
| 5119 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5120 | input_data->len ) ); |
| 5121 | |
| 5122 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5123 | additional_data->len ) ); |
| 5124 | |
| 5125 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5126 | finish_output_size, |
| 5127 | &output_part_length, |
| 5128 | tag_buffer, tag_length, |
| 5129 | &tag_size ), |
| 5130 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5131 | |
| 5132 | psa_aead_abort( &operation ); |
| 5133 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 5134 | /* Test for sending too much additional data after setting lengths. */ |
| 5135 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 5136 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5137 | |
| 5138 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5139 | |
| 5140 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 5141 | |
| 5142 | |
| 5143 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5144 | additional_data->len ), |
| 5145 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5146 | |
| 5147 | psa_aead_abort( &operation ); |
| 5148 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 5149 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 5150 | |
| 5151 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5152 | |
| 5153 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5154 | |
| 5155 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5156 | input_data->len ) ); |
| 5157 | |
| 5158 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5159 | additional_data->len ) ); |
| 5160 | |
| 5161 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5162 | 1 ), |
| 5163 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5164 | |
| 5165 | psa_aead_abort( &operation ); |
| 5166 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 5167 | /* Test for sending too much data after setting lengths. */ |
| 5168 | |
Paul Elliott | b0450fe | 2021-09-01 15:06:26 +0100 | [diff] [blame] | 5169 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5170 | |
| 5171 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5172 | |
| 5173 | PSA_ASSERT( psa_aead_set_lengths( &operation, 0, 0 ) ); |
| 5174 | |
| 5175 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5176 | input_data->len, output_data, |
| 5177 | output_size, &output_length ), |
| 5178 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5179 | |
| 5180 | psa_aead_abort( &operation ); |
| 5181 | |
Paul Elliott | a2a09b0 | 2021-09-22 14:56:40 +0100 | [diff] [blame] | 5182 | /* ------------------------------------------------------- */ |
Paul Elliott | fd0c154c | 2021-09-17 18:03:52 +0100 | [diff] [blame] | 5183 | |
| 5184 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5185 | |
| 5186 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5187 | |
| 5188 | PSA_ASSERT( psa_aead_set_lengths( &operation, additional_data->len, |
| 5189 | input_data->len ) ); |
| 5190 | |
| 5191 | PSA_ASSERT( psa_aead_update_ad( &operation, additional_data->x, |
| 5192 | additional_data->len ) ); |
| 5193 | |
| 5194 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5195 | input_data->len, output_data, |
| 5196 | output_size, &output_length ) ); |
| 5197 | |
| 5198 | TEST_EQUAL( psa_aead_update( &operation, input_data->x, |
| 5199 | 1, output_data, |
| 5200 | output_size, &output_length ), |
| 5201 | PSA_ERROR_INVALID_ARGUMENT ); |
| 5202 | |
| 5203 | psa_aead_abort( &operation ); |
| 5204 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5205 | /* Test sending additional data after data. */ |
| 5206 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5207 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5208 | |
| 5209 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5210 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame^] | 5211 | if( operation.alg != PSA_ALG_CCM ) |
| 5212 | { |
| 5213 | PSA_ASSERT( psa_aead_update( &operation, input_data->x, |
| 5214 | input_data->len, output_data, |
| 5215 | output_size, &output_length ) ); |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5216 | |
Andrzej Kurek | ad83752 | 2021-12-15 15:28:49 +0100 | [diff] [blame^] | 5217 | TEST_EQUAL( psa_aead_update_ad( &operation, additional_data->x, |
| 5218 | additional_data->len ), |
| 5219 | PSA_ERROR_BAD_STATE ); |
| 5220 | } |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5221 | psa_aead_abort( &operation ); |
| 5222 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 5223 | /* Test calling finish on decryption. */ |
| 5224 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 5225 | PSA_ASSERT( psa_aead_decrypt_setup( &operation, key, alg ) ); |
| 5226 | |
| 5227 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5228 | |
| 5229 | TEST_EQUAL( psa_aead_finish( &operation, final_data, |
| 5230 | finish_output_size, |
| 5231 | &output_part_length, |
| 5232 | tag_buffer, tag_length, |
| 5233 | &tag_size ), |
| 5234 | PSA_ERROR_BAD_STATE ); |
| 5235 | |
| 5236 | psa_aead_abort( &operation ); |
| 5237 | |
| 5238 | /* Test calling verify on encryption. */ |
| 5239 | |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 5240 | PSA_ASSERT( psa_aead_encrypt_setup( &operation, key, alg ) ); |
| 5241 | |
| 5242 | PSA_ASSERT( psa_aead_set_nonce( &operation, nonce->x, nonce->len ) ); |
| 5243 | |
| 5244 | TEST_EQUAL( psa_aead_verify( &operation, final_data, |
| 5245 | finish_output_size, |
| 5246 | &output_part_length, |
| 5247 | tag_buffer, |
| 5248 | tag_length ), |
Paul Elliott | 5b065cb | 2021-06-23 08:33:22 +0100 | [diff] [blame] | 5249 | PSA_ERROR_BAD_STATE ); |
Paul Elliott | 534d0b4 | 2021-06-22 19:15:20 +0100 | [diff] [blame] | 5250 | |
| 5251 | psa_aead_abort( &operation ); |
| 5252 | |
| 5253 | |
Paul Elliott | c23a9a0 | 2021-06-21 18:32:46 +0100 | [diff] [blame] | 5254 | exit: |
| 5255 | psa_destroy_key( key ); |
| 5256 | psa_aead_abort( &operation ); |
| 5257 | mbedtls_free( output_data ); |
| 5258 | mbedtls_free( final_data ); |
| 5259 | PSA_DONE( ); |
| 5260 | } |
| 5261 | /* END_CASE */ |
| 5262 | |
| 5263 | /* BEGIN_CASE */ |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 5264 | void signature_size( int type_arg, |
| 5265 | int bits, |
| 5266 | int alg_arg, |
| 5267 | int expected_size_arg ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5268 | { |
| 5269 | psa_key_type_t type = type_arg; |
| 5270 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5271 | size_t actual_size = PSA_SIGN_OUTPUT_SIZE( type, bits, alg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 5272 | |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5273 | TEST_EQUAL( actual_size, (size_t) expected_size_arg ); |
Gilles Peskine | 841b14b | 2019-11-26 17:37:37 +0100 | [diff] [blame] | 5274 | |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5275 | exit: |
| 5276 | ; |
| 5277 | } |
| 5278 | /* END_CASE */ |
| 5279 | |
| 5280 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 5281 | void sign_hash_deterministic( int key_type_arg, data_t *key_data, |
| 5282 | int alg_arg, data_t *input_data, |
| 5283 | data_t *output_data ) |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5284 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5285 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | e59236f | 2018-01-27 23:32:46 +0100 | [diff] [blame] | 5286 | psa_key_type_t key_type = key_type_arg; |
| 5287 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5288 | size_t key_bits; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5289 | unsigned char *signature = NULL; |
| 5290 | size_t signature_size; |
| 5291 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5292 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5293 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5294 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5295 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5296 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5297 | psa_set_key_algorithm( &attributes, alg ); |
| 5298 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 5299 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5300 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5301 | &key ) ); |
| 5302 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5303 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5304 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 5305 | /* Allocate a buffer which has the size advertized by the |
| 5306 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5307 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | c1bb6c8 | 2018-06-18 16:04:39 +0200 | [diff] [blame] | 5308 | key_bits, alg ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5309 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5310 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5311 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5312 | |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 5313 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5314 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5315 | input_data->x, input_data->len, |
| 5316 | signature, signature_size, |
| 5317 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5318 | /* Verify that the signature is what is expected. */ |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 5319 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 5320 | signature, signature_length ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5321 | |
| 5322 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5323 | /* |
| 5324 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5325 | * thus reset them as required. |
| 5326 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5327 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5328 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5329 | psa_destroy_key( key ); |
Gilles Peskine | 0189e75 | 2018-02-03 23:57:22 +0100 | [diff] [blame] | 5330 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5331 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5332 | } |
| 5333 | /* END_CASE */ |
| 5334 | |
| 5335 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 5336 | void sign_hash_fail( int key_type_arg, data_t *key_data, |
| 5337 | int alg_arg, data_t *input_data, |
| 5338 | int signature_size_arg, int expected_status_arg ) |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5339 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5340 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5341 | psa_key_type_t key_type = key_type_arg; |
| 5342 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 5343 | size_t signature_size = signature_size_arg; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5344 | psa_status_t actual_status; |
| 5345 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 40f68b9 | 2018-03-07 16:43:36 +0100 | [diff] [blame] | 5346 | unsigned char *signature = NULL; |
Gilles Peskine | 93aa033 | 2018-02-03 23:58:03 +0100 | [diff] [blame] | 5347 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5348 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5349 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5350 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5351 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5352 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5353 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5354 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5355 | psa_set_key_algorithm( &attributes, alg ); |
| 5356 | psa_set_key_type( &attributes, key_type ); |
mohammad1603 | a97cb8c | 2018-03-28 03:46:26 -0700 | [diff] [blame] | 5357 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5358 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5359 | &key ) ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5360 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5361 | actual_status = psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5362 | input_data->x, input_data->len, |
| 5363 | signature, signature_size, |
| 5364 | &signature_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5365 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 860ce9d | 2018-06-28 12:23:00 +0200 | [diff] [blame] | 5366 | /* The value of *signature_length is unspecified on error, but |
| 5367 | * whatever it is, it should be less than signature_size, so that |
| 5368 | * if the caller tries to read *signature_length bytes without |
| 5369 | * checking the error code then they don't overflow a buffer. */ |
| 5370 | TEST_ASSERT( signature_length <= signature_size ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5371 | |
| 5372 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5373 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5374 | psa_destroy_key( key ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5375 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5376 | PSA_DONE( ); |
Gilles Peskine | 20035e3 | 2018-02-03 22:44:14 +0100 | [diff] [blame] | 5377 | } |
| 5378 | /* END_CASE */ |
mohammad1603 | 8cc1cee | 2018-03-28 01:21:33 +0300 | [diff] [blame] | 5379 | |
| 5380 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 5381 | void sign_verify_hash( int key_type_arg, data_t *key_data, |
| 5382 | int alg_arg, data_t *input_data ) |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5383 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5384 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5385 | psa_key_type_t key_type = key_type_arg; |
| 5386 | psa_algorithm_t alg = alg_arg; |
| 5387 | size_t key_bits; |
| 5388 | unsigned char *signature = NULL; |
| 5389 | size_t signature_size; |
| 5390 | size_t signature_length = 0xdeadbeef; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5391 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5392 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5393 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5394 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5395 | 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] | 5396 | psa_set_key_algorithm( &attributes, alg ); |
| 5397 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5398 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5399 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5400 | &key ) ); |
| 5401 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5402 | key_bits = psa_get_key_bits( &attributes ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5403 | |
| 5404 | /* Allocate a buffer which has the size advertized by the |
| 5405 | * library. */ |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5406 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5407 | key_bits, alg ); |
| 5408 | TEST_ASSERT( signature_size != 0 ); |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5409 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5410 | ASSERT_ALLOC( signature, signature_size ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5411 | |
| 5412 | /* Perform the signature. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5413 | PSA_ASSERT( psa_sign_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5414 | input_data->x, input_data->len, |
| 5415 | signature, signature_size, |
| 5416 | &signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5417 | /* Check that the signature length looks sensible. */ |
| 5418 | TEST_ASSERT( signature_length <= signature_size ); |
| 5419 | TEST_ASSERT( signature_length > 0 ); |
| 5420 | |
| 5421 | /* Use the library to verify that the signature is correct. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5422 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5423 | input_data->x, input_data->len, |
| 5424 | signature, signature_length ) ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5425 | |
| 5426 | if( input_data->len != 0 ) |
| 5427 | { |
| 5428 | /* Flip a bit in the input and verify that the signature is now |
| 5429 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 5430 | * because ECDSA may ignore the last few bits of the input. */ |
| 5431 | input_data->x[0] ^= 1; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5432 | TEST_EQUAL( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5433 | input_data->x, input_data->len, |
| 5434 | signature, signature_length ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 5435 | PSA_ERROR_INVALID_SIGNATURE ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5436 | } |
| 5437 | |
| 5438 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5439 | /* |
| 5440 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5441 | * thus reset them as required. |
| 5442 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5443 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5444 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5445 | psa_destroy_key( key ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5446 | mbedtls_free( signature ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5447 | PSA_DONE( ); |
Gilles Peskine | 9911b02 | 2018-06-29 17:30:48 +0200 | [diff] [blame] | 5448 | } |
| 5449 | /* END_CASE */ |
| 5450 | |
| 5451 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 5452 | void verify_hash( int key_type_arg, data_t *key_data, |
| 5453 | int alg_arg, data_t *hash_data, |
| 5454 | data_t *signature_data ) |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 5455 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5456 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 5457 | psa_key_type_t key_type = key_type_arg; |
| 5458 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5459 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 5460 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5461 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
Gilles Peskine | 69c1267 | 2018-06-28 00:07:19 +0200 | [diff] [blame] | 5462 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5463 | PSA_ASSERT( psa_crypto_init( ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 5464 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5465 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5466 | psa_set_key_algorithm( &attributes, alg ); |
| 5467 | psa_set_key_type( &attributes, key_type ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 5468 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5469 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5470 | &key ) ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 5471 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5472 | PSA_ASSERT( psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5473 | hash_data->x, hash_data->len, |
| 5474 | signature_data->x, signature_data->len ) ); |
Gilles Peskine | 0627f98 | 2019-11-26 19:12:16 +0100 | [diff] [blame] | 5475 | |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 5476 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5477 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5478 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5479 | PSA_DONE( ); |
itayzafrir | 5c75339 | 2018-05-08 11:18:38 +0300 | [diff] [blame] | 5480 | } |
| 5481 | /* END_CASE */ |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5482 | |
| 5483 | /* BEGIN_CASE */ |
gabor-mezei-arm | b953023 | 2021-04-16 14:21:21 +0200 | [diff] [blame] | 5484 | void verify_hash_fail( int key_type_arg, data_t *key_data, |
| 5485 | int alg_arg, data_t *hash_data, |
| 5486 | data_t *signature_data, |
| 5487 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5488 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5489 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5490 | psa_key_type_t key_type = key_type_arg; |
| 5491 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5492 | psa_status_t actual_status; |
| 5493 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5494 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5495 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5496 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5497 | |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5498 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH ); |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5499 | psa_set_key_algorithm( &attributes, alg ); |
| 5500 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 5501 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5502 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5503 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5504 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5505 | actual_status = psa_verify_hash( key, alg, |
Gilles Peskine | 89d8c5c | 2019-11-26 17:01:59 +0100 | [diff] [blame] | 5506 | hash_data->x, hash_data->len, |
| 5507 | signature_data->x, signature_data->len ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5508 | TEST_EQUAL( actual_status, expected_status ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5509 | |
| 5510 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5511 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5512 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5513 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5514 | } |
| 5515 | /* END_CASE */ |
| 5516 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5517 | /* BEGIN_CASE */ |
gabor-mezei-arm | 5302848 | 2021-04-15 18:19:50 +0200 | [diff] [blame] | 5518 | void sign_message_deterministic( int key_type_arg, |
| 5519 | data_t *key_data, |
| 5520 | int alg_arg, |
| 5521 | data_t *input_data, |
| 5522 | data_t *output_data ) |
| 5523 | { |
| 5524 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5525 | psa_key_type_t key_type = key_type_arg; |
| 5526 | psa_algorithm_t alg = alg_arg; |
| 5527 | size_t key_bits; |
| 5528 | unsigned char *signature = NULL; |
| 5529 | size_t signature_size; |
| 5530 | size_t signature_length = 0xdeadbeef; |
| 5531 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5532 | |
| 5533 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5534 | |
| 5535 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 5536 | psa_set_key_algorithm( &attributes, alg ); |
| 5537 | psa_set_key_type( &attributes, key_type ); |
| 5538 | |
| 5539 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5540 | &key ) ); |
| 5541 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5542 | key_bits = psa_get_key_bits( &attributes ); |
| 5543 | |
| 5544 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 5545 | TEST_ASSERT( signature_size != 0 ); |
| 5546 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 5547 | ASSERT_ALLOC( signature, signature_size ); |
| 5548 | |
| 5549 | PSA_ASSERT( psa_sign_message( key, alg, |
| 5550 | input_data->x, input_data->len, |
| 5551 | signature, signature_size, |
| 5552 | &signature_length ) ); |
| 5553 | |
| 5554 | ASSERT_COMPARE( output_data->x, output_data->len, |
| 5555 | signature, signature_length ); |
| 5556 | |
| 5557 | exit: |
| 5558 | psa_reset_key_attributes( &attributes ); |
| 5559 | |
| 5560 | psa_destroy_key( key ); |
| 5561 | mbedtls_free( signature ); |
| 5562 | PSA_DONE( ); |
| 5563 | |
| 5564 | } |
| 5565 | /* END_CASE */ |
| 5566 | |
| 5567 | /* BEGIN_CASE */ |
| 5568 | void sign_message_fail( int key_type_arg, |
| 5569 | data_t *key_data, |
| 5570 | int alg_arg, |
| 5571 | data_t *input_data, |
| 5572 | int signature_size_arg, |
| 5573 | int expected_status_arg ) |
| 5574 | { |
| 5575 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5576 | psa_key_type_t key_type = key_type_arg; |
| 5577 | psa_algorithm_t alg = alg_arg; |
| 5578 | size_t signature_size = signature_size_arg; |
| 5579 | psa_status_t actual_status; |
| 5580 | psa_status_t expected_status = expected_status_arg; |
| 5581 | unsigned char *signature = NULL; |
| 5582 | size_t signature_length = 0xdeadbeef; |
| 5583 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5584 | |
| 5585 | ASSERT_ALLOC( signature, signature_size ); |
| 5586 | |
| 5587 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5588 | |
| 5589 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE ); |
| 5590 | psa_set_key_algorithm( &attributes, alg ); |
| 5591 | psa_set_key_type( &attributes, key_type ); |
| 5592 | |
| 5593 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5594 | &key ) ); |
| 5595 | |
| 5596 | actual_status = psa_sign_message( key, alg, |
| 5597 | input_data->x, input_data->len, |
| 5598 | signature, signature_size, |
| 5599 | &signature_length ); |
| 5600 | TEST_EQUAL( actual_status, expected_status ); |
| 5601 | /* The value of *signature_length is unspecified on error, but |
| 5602 | * whatever it is, it should be less than signature_size, so that |
| 5603 | * if the caller tries to read *signature_length bytes without |
| 5604 | * checking the error code then they don't overflow a buffer. */ |
| 5605 | TEST_ASSERT( signature_length <= signature_size ); |
| 5606 | |
| 5607 | exit: |
| 5608 | psa_reset_key_attributes( &attributes ); |
| 5609 | psa_destroy_key( key ); |
| 5610 | mbedtls_free( signature ); |
| 5611 | PSA_DONE( ); |
| 5612 | } |
| 5613 | /* END_CASE */ |
| 5614 | |
| 5615 | /* BEGIN_CASE */ |
| 5616 | void sign_verify_message( int key_type_arg, |
| 5617 | data_t *key_data, |
| 5618 | int alg_arg, |
| 5619 | data_t *input_data ) |
| 5620 | { |
| 5621 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5622 | psa_key_type_t key_type = key_type_arg; |
| 5623 | psa_algorithm_t alg = alg_arg; |
| 5624 | size_t key_bits; |
| 5625 | unsigned char *signature = NULL; |
| 5626 | size_t signature_size; |
| 5627 | size_t signature_length = 0xdeadbeef; |
| 5628 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5629 | |
| 5630 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5631 | |
| 5632 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE | |
| 5633 | PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 5634 | psa_set_key_algorithm( &attributes, alg ); |
| 5635 | psa_set_key_type( &attributes, key_type ); |
| 5636 | |
| 5637 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5638 | &key ) ); |
| 5639 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5640 | key_bits = psa_get_key_bits( &attributes ); |
| 5641 | |
| 5642 | signature_size = PSA_SIGN_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 5643 | TEST_ASSERT( signature_size != 0 ); |
| 5644 | TEST_ASSERT( signature_size <= PSA_SIGNATURE_MAX_SIZE ); |
| 5645 | ASSERT_ALLOC( signature, signature_size ); |
| 5646 | |
| 5647 | PSA_ASSERT( psa_sign_message( key, alg, |
| 5648 | input_data->x, input_data->len, |
| 5649 | signature, signature_size, |
| 5650 | &signature_length ) ); |
| 5651 | TEST_ASSERT( signature_length <= signature_size ); |
| 5652 | TEST_ASSERT( signature_length > 0 ); |
| 5653 | |
| 5654 | PSA_ASSERT( psa_verify_message( key, alg, |
| 5655 | input_data->x, input_data->len, |
| 5656 | signature, signature_length ) ); |
| 5657 | |
| 5658 | if( input_data->len != 0 ) |
| 5659 | { |
| 5660 | /* Flip a bit in the input and verify that the signature is now |
| 5661 | * detected as invalid. Flip a bit at the beginning, not at the end, |
| 5662 | * because ECDSA may ignore the last few bits of the input. */ |
| 5663 | input_data->x[0] ^= 1; |
| 5664 | TEST_EQUAL( psa_verify_message( key, alg, |
| 5665 | input_data->x, input_data->len, |
| 5666 | signature, signature_length ), |
| 5667 | PSA_ERROR_INVALID_SIGNATURE ); |
| 5668 | } |
| 5669 | |
| 5670 | exit: |
| 5671 | psa_reset_key_attributes( &attributes ); |
| 5672 | |
| 5673 | psa_destroy_key( key ); |
| 5674 | mbedtls_free( signature ); |
| 5675 | PSA_DONE( ); |
| 5676 | } |
| 5677 | /* END_CASE */ |
| 5678 | |
| 5679 | /* BEGIN_CASE */ |
| 5680 | void verify_message( int key_type_arg, |
| 5681 | data_t *key_data, |
| 5682 | int alg_arg, |
| 5683 | data_t *input_data, |
| 5684 | data_t *signature_data ) |
| 5685 | { |
| 5686 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5687 | psa_key_type_t key_type = key_type_arg; |
| 5688 | psa_algorithm_t alg = alg_arg; |
| 5689 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5690 | |
| 5691 | TEST_ASSERT( signature_data->len <= PSA_SIGNATURE_MAX_SIZE ); |
| 5692 | |
| 5693 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5694 | |
| 5695 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 5696 | psa_set_key_algorithm( &attributes, alg ); |
| 5697 | psa_set_key_type( &attributes, key_type ); |
| 5698 | |
| 5699 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5700 | &key ) ); |
| 5701 | |
| 5702 | PSA_ASSERT( psa_verify_message( key, alg, |
| 5703 | input_data->x, input_data->len, |
| 5704 | signature_data->x, signature_data->len ) ); |
| 5705 | |
| 5706 | exit: |
| 5707 | psa_reset_key_attributes( &attributes ); |
| 5708 | psa_destroy_key( key ); |
| 5709 | PSA_DONE( ); |
| 5710 | } |
| 5711 | /* END_CASE */ |
| 5712 | |
| 5713 | /* BEGIN_CASE */ |
| 5714 | void verify_message_fail( int key_type_arg, |
| 5715 | data_t *key_data, |
| 5716 | int alg_arg, |
| 5717 | data_t *hash_data, |
| 5718 | data_t *signature_data, |
| 5719 | int expected_status_arg ) |
| 5720 | { |
| 5721 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 5722 | psa_key_type_t key_type = key_type_arg; |
| 5723 | psa_algorithm_t alg = alg_arg; |
| 5724 | psa_status_t actual_status; |
| 5725 | psa_status_t expected_status = expected_status_arg; |
| 5726 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 5727 | |
| 5728 | PSA_ASSERT( psa_crypto_init( ) ); |
| 5729 | |
| 5730 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_MESSAGE ); |
| 5731 | psa_set_key_algorithm( &attributes, alg ); |
| 5732 | psa_set_key_type( &attributes, key_type ); |
| 5733 | |
| 5734 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
| 5735 | &key ) ); |
| 5736 | |
| 5737 | actual_status = psa_verify_message( key, alg, |
| 5738 | hash_data->x, hash_data->len, |
| 5739 | signature_data->x, |
| 5740 | signature_data->len ); |
| 5741 | TEST_EQUAL( actual_status, expected_status ); |
| 5742 | |
| 5743 | exit: |
| 5744 | psa_reset_key_attributes( &attributes ); |
| 5745 | psa_destroy_key( key ); |
| 5746 | PSA_DONE( ); |
| 5747 | } |
| 5748 | /* END_CASE */ |
| 5749 | |
| 5750 | /* BEGIN_CASE */ |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5751 | void asymmetric_encrypt( int key_type_arg, |
| 5752 | data_t *key_data, |
| 5753 | int alg_arg, |
| 5754 | data_t *input_data, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5755 | data_t *label, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5756 | int expected_output_length_arg, |
| 5757 | int expected_status_arg ) |
| 5758 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5759 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5760 | psa_key_type_t key_type = key_type_arg; |
| 5761 | psa_algorithm_t alg = alg_arg; |
| 5762 | size_t expected_output_length = expected_output_length_arg; |
| 5763 | size_t key_bits; |
| 5764 | unsigned char *output = NULL; |
| 5765 | size_t output_size; |
| 5766 | size_t output_length = ~0; |
| 5767 | psa_status_t actual_status; |
| 5768 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5769 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5770 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5771 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | bdf309c | 2018-12-03 15:36:32 +0100 | [diff] [blame] | 5772 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5773 | /* Import the key */ |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5774 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT ); |
| 5775 | psa_set_key_algorithm( &attributes, alg ); |
| 5776 | psa_set_key_type( &attributes, key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5777 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5778 | &key ) ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5779 | |
| 5780 | /* Determine the maximum output length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5781 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5782 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5783 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5784 | 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] | 5785 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5786 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5787 | |
| 5788 | /* Encrypt the input */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5789 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5790 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5791 | label->x, label->len, |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5792 | output, output_size, |
| 5793 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5794 | TEST_EQUAL( actual_status, expected_status ); |
| 5795 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5796 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5797 | /* If the label is empty, the test framework puts a non-null pointer |
| 5798 | * in label->x. Test that a null pointer works as well. */ |
| 5799 | if( label->len == 0 ) |
| 5800 | { |
| 5801 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5802 | if( output_size != 0 ) |
| 5803 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5804 | actual_status = psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5805 | input_data->x, input_data->len, |
| 5806 | NULL, label->len, |
| 5807 | output, output_size, |
| 5808 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 5809 | TEST_EQUAL( actual_status, expected_status ); |
| 5810 | TEST_EQUAL( output_length, expected_output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5811 | } |
| 5812 | |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5813 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5814 | /* |
| 5815 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5816 | * thus reset them as required. |
| 5817 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5818 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5819 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5820 | psa_destroy_key( key ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5821 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5822 | PSA_DONE( ); |
Gilles Peskine | 656896e | 2018-06-29 19:12:28 +0200 | [diff] [blame] | 5823 | } |
| 5824 | /* END_CASE */ |
| 5825 | |
| 5826 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5827 | void asymmetric_encrypt_decrypt( int key_type_arg, |
| 5828 | data_t *key_data, |
| 5829 | int alg_arg, |
| 5830 | data_t *input_data, |
| 5831 | data_t *label ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5832 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5833 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5834 | psa_key_type_t key_type = key_type_arg; |
| 5835 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5836 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5837 | unsigned char *output = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5838 | size_t output_size; |
| 5839 | size_t output_length = ~0; |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 5840 | unsigned char *output2 = NULL; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5841 | size_t output2_size; |
| 5842 | size_t output2_length = ~0; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5843 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5844 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5845 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5846 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5847 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT ); |
| 5848 | psa_set_key_algorithm( &attributes, alg ); |
| 5849 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 5850 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5851 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5852 | &key ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5853 | |
| 5854 | /* Determine the maximum ciphertext length */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5855 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 5856 | key_bits = psa_get_key_bits( &attributes ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5857 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5858 | 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] | 5859 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_ENCRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5860 | ASSERT_ALLOC( output, output_size ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5861 | |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5862 | output2_size = input_data->len; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5863 | TEST_ASSERT( output2_size <= |
| 5864 | PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ) ); |
| 5865 | TEST_ASSERT( output2_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5866 | ASSERT_ALLOC( output2, output2_size ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5867 | |
Gilles Peskine | eebd738 | 2018-06-08 18:11:54 +0200 | [diff] [blame] | 5868 | /* We test encryption by checking that encrypt-then-decrypt gives back |
| 5869 | * the original plaintext because of the non-optional random |
| 5870 | * part of encryption process which prevents using fixed vectors. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5871 | PSA_ASSERT( psa_asymmetric_encrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5872 | input_data->x, input_data->len, |
| 5873 | label->x, label->len, |
| 5874 | output, output_size, |
| 5875 | &output_length ) ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5876 | /* We don't know what ciphertext length to expect, but check that |
| 5877 | * it looks sensible. */ |
| 5878 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 0f3bdbd | 2018-05-02 23:56:12 +0300 | [diff] [blame] | 5879 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5880 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5881 | output, output_length, |
| 5882 | label->x, label->len, |
| 5883 | output2, output2_size, |
| 5884 | &output2_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 5885 | ASSERT_COMPARE( input_data->x, input_data->len, |
| 5886 | output2, output2_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5887 | |
| 5888 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5889 | /* |
| 5890 | * Key attributes may have been returned by psa_get_key_attributes() |
| 5891 | * thus reset them as required. |
| 5892 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5893 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 5894 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5895 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 5896 | mbedtls_free( output ); |
| 5897 | mbedtls_free( output2 ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5898 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5899 | } |
| 5900 | /* END_CASE */ |
| 5901 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5902 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5903 | void asymmetric_decrypt( int key_type_arg, |
| 5904 | data_t *key_data, |
| 5905 | int alg_arg, |
| 5906 | data_t *input_data, |
| 5907 | data_t *label, |
Gilles Peskine | 66763a0 | 2018-06-29 21:54:10 +0200 | [diff] [blame] | 5908 | data_t *expected_data ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5909 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5910 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5911 | psa_key_type_t key_type = key_type_arg; |
| 5912 | psa_algorithm_t alg = alg_arg; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5913 | size_t key_bits; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5914 | unsigned char *output = NULL; |
Nir Sonnenschein | d70bc48 | 2018-06-04 16:31:13 +0300 | [diff] [blame] | 5915 | size_t output_size = 0; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5916 | size_t output_length = ~0; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5917 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5918 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5919 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5920 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5921 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5922 | psa_set_key_algorithm( &attributes, alg ); |
| 5923 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 5924 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5925 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5926 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5927 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 5928 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
| 5929 | key_bits = psa_get_key_bits( &attributes ); |
| 5930 | |
| 5931 | /* Determine the maximum ciphertext length */ |
| 5932 | output_size = PSA_ASYMMETRIC_DECRYPT_OUTPUT_SIZE( key_type, key_bits, alg ); |
| 5933 | TEST_ASSERT( output_size <= PSA_ASYMMETRIC_DECRYPT_OUTPUT_MAX_SIZE ); |
| 5934 | ASSERT_ALLOC( output, output_size ); |
| 5935 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5936 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5937 | input_data->x, input_data->len, |
| 5938 | label->x, label->len, |
| 5939 | output, |
| 5940 | output_size, |
| 5941 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 5942 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 5943 | output, output_length ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5944 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5945 | /* If the label is empty, the test framework puts a non-null pointer |
| 5946 | * in label->x. Test that a null pointer works as well. */ |
| 5947 | if( label->len == 0 ) |
| 5948 | { |
| 5949 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 5950 | if( output_size != 0 ) |
| 5951 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5952 | PSA_ASSERT( psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5953 | input_data->x, input_data->len, |
| 5954 | NULL, label->len, |
| 5955 | output, |
| 5956 | output_size, |
| 5957 | &output_length ) ); |
Gilles Peskine | bd7dea9 | 2018-09-27 13:57:19 +0200 | [diff] [blame] | 5958 | ASSERT_COMPARE( expected_data->x, expected_data->len, |
| 5959 | output, output_length ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5960 | } |
| 5961 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5962 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 5963 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5964 | psa_destroy_key( key ); |
itayzafrir | 3e02b3b | 2018-06-12 17:06:52 +0300 | [diff] [blame] | 5965 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 5966 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5967 | } |
| 5968 | /* END_CASE */ |
| 5969 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5970 | /* BEGIN_CASE */ |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 5971 | void asymmetric_decrypt_fail( int key_type_arg, |
| 5972 | data_t *key_data, |
| 5973 | int alg_arg, |
| 5974 | data_t *input_data, |
| 5975 | data_t *label, |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 5976 | int output_size_arg, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 5977 | int expected_status_arg ) |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5978 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5979 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5980 | psa_key_type_t key_type = key_type_arg; |
| 5981 | psa_algorithm_t alg = alg_arg; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5982 | unsigned char *output = NULL; |
Jaeden Amero | f8daab7 | 2019-02-06 12:57:46 +0000 | [diff] [blame] | 5983 | size_t output_size = output_size_arg; |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 5984 | size_t output_length = ~0; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5985 | psa_status_t actual_status; |
| 5986 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5987 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5988 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 5989 | ASSERT_ALLOC( output, output_size ); |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 5990 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 5991 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5992 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 5993 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DECRYPT ); |
| 5994 | psa_set_key_algorithm( &attributes, alg ); |
| 5995 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | d708260 | 2018-06-04 16:45:27 +0300 | [diff] [blame] | 5996 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 5997 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 5998 | &key ) ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 5999 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6000 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 6001 | input_data->x, input_data->len, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6002 | label->x, label->len, |
Gilles Peskine | 4abf741 | 2018-06-18 16:35:34 +0200 | [diff] [blame] | 6003 | output, output_size, |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 6004 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6005 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6006 | TEST_ASSERT( output_length <= output_size ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6007 | |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6008 | /* If the label is empty, the test framework puts a non-null pointer |
| 6009 | * in label->x. Test that a null pointer works as well. */ |
| 6010 | if( label->len == 0 ) |
| 6011 | { |
| 6012 | output_length = ~0; |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 6013 | if( output_size != 0 ) |
| 6014 | memset( output, 0, output_size ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6015 | actual_status = psa_asymmetric_decrypt( key, alg, |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6016 | input_data->x, input_data->len, |
| 6017 | NULL, label->len, |
| 6018 | output, output_size, |
| 6019 | &output_length ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6020 | TEST_EQUAL( actual_status, expected_status ); |
Gilles Peskine | 55c94dd | 2018-06-30 18:54:48 +0200 | [diff] [blame] | 6021 | TEST_ASSERT( output_length <= output_size ); |
Gilles Peskine | 6842812 | 2018-06-30 18:42:41 +0200 | [diff] [blame] | 6022 | } |
| 6023 | |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6024 | exit: |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6025 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6026 | psa_destroy_key( key ); |
Gilles Peskine | 2d27786 | 2018-06-18 15:41:12 +0200 | [diff] [blame] | 6027 | mbedtls_free( output ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6028 | PSA_DONE( ); |
Nir Sonnenschein | 39e5914 | 2018-05-02 23:16:26 +0300 | [diff] [blame] | 6029 | } |
Gilles Peskine | 5b051bc | 2018-05-31 13:25:48 +0200 | [diff] [blame] | 6030 | /* END_CASE */ |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6031 | |
| 6032 | /* BEGIN_CASE */ |
Gilles Peskine | cbe6650 | 2019-05-16 16:59:18 +0200 | [diff] [blame] | 6033 | void key_derivation_init( ) |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 6034 | { |
| 6035 | /* Test each valid way of initializing the object, except for `= {0}`, as |
| 6036 | * Clang 5 complains when `-Wmissing-field-initializers` is used, even |
| 6037 | * though it's OK by the C standard. We could test for this, but we'd need |
| 6038 | * to supress the Clang warning for the test. */ |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 6039 | size_t capacity; |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6040 | psa_key_derivation_operation_t func = psa_key_derivation_operation_init( ); |
| 6041 | psa_key_derivation_operation_t init = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6042 | psa_key_derivation_operation_t zero; |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 6043 | |
| 6044 | memset( &zero, 0, sizeof( zero ) ); |
| 6045 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6046 | /* A default operation should not be able to report its capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6047 | TEST_EQUAL( psa_key_derivation_get_capacity( &func, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 6048 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6049 | TEST_EQUAL( psa_key_derivation_get_capacity( &init, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 6050 | PSA_ERROR_BAD_STATE ); |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6051 | TEST_EQUAL( psa_key_derivation_get_capacity( &zero, &capacity ), |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 6052 | PSA_ERROR_BAD_STATE ); |
Jaeden Amero | 5229bbb | 2019-02-07 16:33:37 +0000 | [diff] [blame] | 6053 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6054 | /* A default operation should be abortable without error. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6055 | PSA_ASSERT( psa_key_derivation_abort(&func) ); |
| 6056 | PSA_ASSERT( psa_key_derivation_abort(&init) ); |
| 6057 | PSA_ASSERT( psa_key_derivation_abort(&zero) ); |
Jaeden Amero | d94d671 | 2019-01-04 14:11:48 +0000 | [diff] [blame] | 6058 | } |
| 6059 | /* END_CASE */ |
| 6060 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 6061 | /* BEGIN_CASE */ |
| 6062 | void derive_setup( int alg_arg, int expected_status_arg ) |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6063 | { |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6064 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6065 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6066 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6067 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6068 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6069 | |
Janos Follath | 16de4a4 | 2019-06-13 16:32:24 +0100 | [diff] [blame] | 6070 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 6071 | expected_status ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6072 | |
| 6073 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6074 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6075 | PSA_DONE( ); |
Gilles Peskine | ea0fb49 | 2018-07-12 17:17:20 +0200 | [diff] [blame] | 6076 | } |
| 6077 | /* END_CASE */ |
| 6078 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6079 | /* BEGIN_CASE */ |
Janos Follath | a27c927 | 2019-06-14 09:59:36 +0100 | [diff] [blame] | 6080 | void derive_set_capacity( int alg_arg, int capacity_arg, |
| 6081 | int expected_status_arg ) |
| 6082 | { |
| 6083 | psa_algorithm_t alg = alg_arg; |
| 6084 | size_t capacity = capacity_arg; |
| 6085 | psa_status_t expected_status = expected_status_arg; |
| 6086 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6087 | |
| 6088 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6089 | |
| 6090 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 6091 | |
| 6092 | TEST_EQUAL( psa_key_derivation_set_capacity( &operation, capacity ), |
| 6093 | expected_status ); |
| 6094 | |
| 6095 | exit: |
| 6096 | psa_key_derivation_abort( &operation ); |
| 6097 | PSA_DONE( ); |
| 6098 | } |
| 6099 | /* END_CASE */ |
| 6100 | |
| 6101 | /* BEGIN_CASE */ |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6102 | void derive_input( int alg_arg, |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 6103 | int step_arg1, int key_type_arg1, data_t *input1, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6104 | int expected_status_arg1, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 6105 | int step_arg2, int key_type_arg2, data_t *input2, |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6106 | int expected_status_arg2, |
Gilles Peskine | 2058c07 | 2019-09-24 17:19:33 +0200 | [diff] [blame] | 6107 | int step_arg3, int key_type_arg3, data_t *input3, |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 6108 | int expected_status_arg3, |
| 6109 | int output_key_type_arg, int expected_output_status_arg ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6110 | { |
| 6111 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 6112 | psa_key_derivation_step_t steps[] = {step_arg1, step_arg2, step_arg3}; |
| 6113 | 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] | 6114 | psa_status_t expected_statuses[] = {expected_status_arg1, |
| 6115 | expected_status_arg2, |
| 6116 | expected_status_arg3}; |
| 6117 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6118 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 6119 | MBEDTLS_SVC_KEY_ID_INIT, |
| 6120 | MBEDTLS_SVC_KEY_ID_INIT }; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6121 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6122 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6123 | size_t i; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 6124 | psa_key_type_t output_key_type = output_key_type_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6125 | mbedtls_svc_key_id_t output_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 6126 | psa_status_t expected_output_status = expected_output_status_arg; |
| 6127 | psa_status_t actual_output_status; |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6128 | |
| 6129 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6130 | |
| 6131 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 6132 | psa_set_key_algorithm( &attributes, alg ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6133 | |
| 6134 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 6135 | |
| 6136 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
| 6137 | { |
Gilles Peskine | 4023c01 | 2021-05-27 13:21:20 +0200 | [diff] [blame] | 6138 | mbedtls_test_set_step( i ); |
| 6139 | if( steps[i] == 0 ) |
| 6140 | { |
| 6141 | /* Skip this step */ |
| 6142 | } |
| 6143 | else if( key_types[i] != PSA_KEY_TYPE_NONE ) |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6144 | { |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 6145 | psa_set_key_type( &attributes, key_types[i] ); |
| 6146 | PSA_ASSERT( psa_import_key( &attributes, |
| 6147 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6148 | &keys[i] ) ); |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 6149 | if( PSA_KEY_TYPE_IS_KEY_PAIR( key_types[i] ) && |
| 6150 | steps[i] == PSA_KEY_DERIVATION_INPUT_SECRET ) |
| 6151 | { |
| 6152 | // When taking a private key as secret input, use key agreement |
| 6153 | // to add the shared secret to the derivation |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6154 | TEST_EQUAL( mbedtls_test_psa_key_agreement_with_self( |
| 6155 | &operation, keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 6156 | expected_statuses[i] ); |
| 6157 | } |
| 6158 | else |
| 6159 | { |
| 6160 | TEST_EQUAL( psa_key_derivation_input_key( &operation, steps[i], |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6161 | keys[i] ), |
Steven Cooreman | 0ee0d52 | 2020-10-05 16:03:42 +0200 | [diff] [blame] | 6162 | expected_statuses[i] ); |
| 6163 | } |
Gilles Peskine | 6842ba4 | 2019-09-23 13:49:33 +0200 | [diff] [blame] | 6164 | } |
| 6165 | else |
| 6166 | { |
| 6167 | TEST_EQUAL( psa_key_derivation_input_bytes( |
| 6168 | &operation, steps[i], |
| 6169 | inputs[i]->x, inputs[i]->len ), |
| 6170 | expected_statuses[i] ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6171 | } |
| 6172 | } |
| 6173 | |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 6174 | if( output_key_type != PSA_KEY_TYPE_NONE ) |
| 6175 | { |
| 6176 | psa_reset_key_attributes( &attributes ); |
Dave Rodgman | 491d849 | 2021-11-16 12:12:49 +0000 | [diff] [blame] | 6177 | psa_set_key_type( &attributes, output_key_type ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 6178 | psa_set_key_bits( &attributes, 8 ); |
| 6179 | actual_output_status = |
| 6180 | psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6181 | &output_key ); |
Gilles Peskine | 1a2904c | 2019-09-24 17:45:07 +0200 | [diff] [blame] | 6182 | } |
| 6183 | else |
| 6184 | { |
| 6185 | uint8_t buffer[1]; |
| 6186 | actual_output_status = |
| 6187 | psa_key_derivation_output_bytes( &operation, |
| 6188 | buffer, sizeof( buffer ) ); |
| 6189 | } |
| 6190 | TEST_EQUAL( actual_output_status, expected_output_status ); |
| 6191 | |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6192 | exit: |
| 6193 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6194 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 6195 | psa_destroy_key( keys[i] ); |
| 6196 | psa_destroy_key( output_key ); |
Janos Follath | af3c2a0 | 2019-06-12 12:34:34 +0100 | [diff] [blame] | 6197 | PSA_DONE( ); |
| 6198 | } |
| 6199 | /* END_CASE */ |
| 6200 | |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 6201 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 6202 | void derive_over_capacity( int alg_arg ) |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 6203 | { |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 6204 | psa_algorithm_t alg = alg_arg; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6205 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Nir Sonnenschein | 4eda37b | 2018-10-31 12:15:58 +0200 | [diff] [blame] | 6206 | size_t key_type = PSA_KEY_TYPE_DERIVE; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6207 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 6208 | unsigned char input1[] = "Input 1"; |
| 6209 | size_t input1_length = sizeof( input1 ); |
| 6210 | unsigned char input2[] = "Input 2"; |
| 6211 | size_t input2_length = sizeof( input2 ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6212 | uint8_t buffer[42]; |
Nir Sonnenschein | 1caf6d2 | 2018-11-01 12:27:20 +0200 | [diff] [blame] | 6213 | size_t capacity = sizeof( buffer ); |
Nir Sonnenschein | dd69d8b | 2018-11-01 12:24:23 +0200 | [diff] [blame] | 6214 | const uint8_t key_data[22] = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 6215 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
| 6216 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b}; |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6217 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 6218 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6219 | PSA_ASSERT( psa_crypto_init( ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6220 | |
Gilles Peskine | 2c2cf0e | 2019-04-19 19:58:20 +0200 | [diff] [blame] | 6221 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 6222 | psa_set_key_algorithm( &attributes, alg ); |
| 6223 | psa_set_key_type( &attributes, key_type ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6224 | |
Gilles Peskine | 73676cb | 2019-05-15 20:15:10 +0200 | [diff] [blame] | 6225 | PSA_ASSERT( psa_import_key( &attributes, |
| 6226 | key_data, sizeof( key_data ), |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6227 | &key ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6228 | |
| 6229 | /* valid key derivation */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6230 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 6231 | input1, input1_length, |
| 6232 | input2, input2_length, |
| 6233 | capacity ) ) |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 6234 | goto exit; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6235 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6236 | /* state of operation shouldn't allow additional generation */ |
Janos Follath | d958bb7 | 2019-07-03 15:02:16 +0100 | [diff] [blame] | 6237 | TEST_EQUAL( psa_key_derivation_setup( &operation, alg ), |
Gilles Peskine | f812dcf | 2018-12-18 00:33:25 +0100 | [diff] [blame] | 6238 | PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6239 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6240 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, buffer, capacity ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6241 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6242 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, buffer, capacity ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 6243 | PSA_ERROR_INSUFFICIENT_DATA ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6244 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6245 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6246 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6247 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6248 | PSA_DONE( ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6249 | } |
| 6250 | /* END_CASE */ |
| 6251 | |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6252 | /* BEGIN_CASE */ |
Gilles Peskine | 1c77edd | 2021-05-27 11:55:02 +0200 | [diff] [blame] | 6253 | void derive_actions_without_setup( ) |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6254 | { |
| 6255 | uint8_t output_buffer[16]; |
| 6256 | size_t buffer_size = 16; |
| 6257 | size_t capacity = 0; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6258 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6259 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6260 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 6261 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 6262 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6263 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6264 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 6265 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6266 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6267 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6268 | |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6269 | TEST_ASSERT( psa_key_derivation_output_bytes( &operation, |
| 6270 | output_buffer, buffer_size ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 6271 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6272 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6273 | TEST_ASSERT( psa_key_derivation_get_capacity( &operation, &capacity ) |
Jaeden Amero | cf2010c | 2019-02-15 13:05:49 +0000 | [diff] [blame] | 6274 | == PSA_ERROR_BAD_STATE ); |
Nir Sonnenschein | b46e7ca | 2018-10-25 14:46:09 +0300 | [diff] [blame] | 6275 | |
| 6276 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6277 | psa_key_derivation_abort( &operation ); |
Nir Sonnenschein | e5204c9 | 2018-10-22 17:24:55 +0300 | [diff] [blame] | 6278 | } |
| 6279 | /* END_CASE */ |
| 6280 | |
| 6281 | /* BEGIN_CASE */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6282 | void derive_output( int alg_arg, |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 6283 | int step1_arg, data_t *input1, |
| 6284 | int step2_arg, data_t *input2, |
| 6285 | int step3_arg, data_t *input3, |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6286 | int requested_capacity_arg, |
| 6287 | data_t *expected_output1, |
| 6288 | data_t *expected_output2 ) |
| 6289 | { |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6290 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 6291 | psa_key_derivation_step_t steps[] = {step1_arg, step2_arg, step3_arg}; |
| 6292 | data_t *inputs[] = {input1, input2, input3}; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6293 | mbedtls_svc_key_id_t keys[] = { MBEDTLS_SVC_KEY_ID_INIT, |
| 6294 | MBEDTLS_SVC_KEY_ID_INIT, |
| 6295 | MBEDTLS_SVC_KEY_ID_INIT }; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6296 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6297 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6298 | uint8_t *expected_outputs[2] = |
| 6299 | {expected_output1->x, expected_output2->x}; |
| 6300 | size_t output_sizes[2] = |
| 6301 | {expected_output1->len, expected_output2->len}; |
| 6302 | size_t output_buffer_size = 0; |
| 6303 | uint8_t *output_buffer = NULL; |
| 6304 | size_t expected_capacity; |
| 6305 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6306 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6307 | psa_status_t status; |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 6308 | size_t i; |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6309 | |
| 6310 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 6311 | { |
| 6312 | if( output_sizes[i] > output_buffer_size ) |
| 6313 | output_buffer_size = output_sizes[i]; |
| 6314 | if( output_sizes[i] == 0 ) |
| 6315 | expected_outputs[i] = NULL; |
| 6316 | } |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6317 | ASSERT_ALLOC( output_buffer, output_buffer_size ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6318 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6319 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6320 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 6321 | psa_set_key_algorithm( &attributes, alg ); |
| 6322 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6323 | |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6324 | /* Extraction phase. */ |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 6325 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
| 6326 | PSA_ASSERT( psa_key_derivation_set_capacity( &operation, |
| 6327 | requested_capacity ) ); |
| 6328 | for( i = 0; i < ARRAY_LENGTH( steps ); i++ ) |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6329 | { |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 6330 | switch( steps[i] ) |
| 6331 | { |
| 6332 | case 0: |
| 6333 | break; |
| 6334 | case PSA_KEY_DERIVATION_INPUT_SECRET: |
| 6335 | PSA_ASSERT( psa_import_key( &attributes, |
| 6336 | inputs[i]->x, inputs[i]->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6337 | &keys[i] ) ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6338 | |
| 6339 | if ( PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) ) |
| 6340 | { |
| 6341 | PSA_ASSERT( psa_get_key_attributes( keys[i], &attributes ) ); |
| 6342 | TEST_ASSERT( PSA_BITS_TO_BYTES( psa_get_key_bits( &attributes ) ) <= |
| 6343 | PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ); |
| 6344 | } |
| 6345 | |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 6346 | PSA_ASSERT( psa_key_derivation_input_key( |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6347 | &operation, steps[i], keys[i] ) ); |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 6348 | break; |
| 6349 | default: |
| 6350 | PSA_ASSERT( psa_key_derivation_input_bytes( |
| 6351 | &operation, steps[i], |
| 6352 | inputs[i]->x, inputs[i]->len ) ); |
| 6353 | break; |
| 6354 | } |
Gilles Peskine | b70a0fd | 2019-01-07 22:59:38 +0100 | [diff] [blame] | 6355 | } |
Gilles Peskine | 1468da7 | 2019-05-29 17:35:49 +0200 | [diff] [blame] | 6356 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6357 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6358 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6359 | TEST_EQUAL( current_capacity, requested_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6360 | expected_capacity = requested_capacity; |
| 6361 | |
| 6362 | /* Expansion phase. */ |
| 6363 | for( i = 0; i < ARRAY_LENGTH( expected_outputs ); i++ ) |
| 6364 | { |
| 6365 | /* Read some bytes. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6366 | status = psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6367 | output_buffer, output_sizes[i] ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6368 | if( expected_capacity == 0 && output_sizes[i] == 0 ) |
| 6369 | { |
| 6370 | /* Reading 0 bytes when 0 bytes are available can go either way. */ |
| 6371 | TEST_ASSERT( status == PSA_SUCCESS || |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 6372 | status == PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6373 | continue; |
| 6374 | } |
| 6375 | else if( expected_capacity == 0 || |
| 6376 | output_sizes[i] > expected_capacity ) |
| 6377 | { |
| 6378 | /* Capacity exceeded. */ |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 6379 | TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6380 | expected_capacity = 0; |
| 6381 | continue; |
| 6382 | } |
| 6383 | /* Success. Check the read data. */ |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6384 | PSA_ASSERT( status ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6385 | if( output_sizes[i] != 0 ) |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 6386 | ASSERT_COMPARE( output_buffer, output_sizes[i], |
| 6387 | expected_outputs[i], output_sizes[i] ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6388 | /* Check the operation status. */ |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6389 | expected_capacity -= output_sizes[i]; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6390 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6391 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6392 | TEST_EQUAL( expected_capacity, current_capacity ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6393 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6394 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6395 | |
| 6396 | exit: |
| 6397 | mbedtls_free( output_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6398 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6399 | for( i = 0; i < ARRAY_LENGTH( keys ); i++ ) |
| 6400 | psa_destroy_key( keys[i] ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6401 | PSA_DONE( ); |
Gilles Peskine | 96ee5c7 | 2018-07-12 17:24:54 +0200 | [diff] [blame] | 6402 | } |
| 6403 | /* END_CASE */ |
| 6404 | |
| 6405 | /* BEGIN_CASE */ |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6406 | void derive_full( int alg_arg, |
| 6407 | data_t *key_data, |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 6408 | data_t *input1, |
| 6409 | data_t *input2, |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6410 | int requested_capacity_arg ) |
| 6411 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6412 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6413 | psa_algorithm_t alg = alg_arg; |
| 6414 | size_t requested_capacity = requested_capacity_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6415 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6416 | unsigned char output_buffer[16]; |
| 6417 | size_t expected_capacity = requested_capacity; |
| 6418 | size_t current_capacity; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6419 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6420 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6421 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6422 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6423 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 6424 | psa_set_key_algorithm( &attributes, alg ); |
| 6425 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6426 | |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6427 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6428 | &key ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6429 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6430 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg, |
| 6431 | input1->x, input1->len, |
| 6432 | input2->x, input2->len, |
| 6433 | requested_capacity ) ) |
Janos Follath | f2815ea | 2019-07-03 12:41:36 +0100 | [diff] [blame] | 6434 | goto exit; |
Janos Follath | 47f27ed | 2019-06-25 13:24:52 +0100 | [diff] [blame] | 6435 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6436 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6437 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6438 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6439 | |
| 6440 | /* Expansion phase. */ |
| 6441 | while( current_capacity > 0 ) |
| 6442 | { |
| 6443 | size_t read_size = sizeof( output_buffer ); |
| 6444 | if( read_size > current_capacity ) |
| 6445 | read_size = current_capacity; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6446 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6447 | output_buffer, |
| 6448 | read_size ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6449 | expected_capacity -= read_size; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6450 | PSA_ASSERT( psa_key_derivation_get_capacity( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6451 | ¤t_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6452 | TEST_EQUAL( current_capacity, expected_capacity ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6453 | } |
| 6454 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6455 | /* Check that the operation refuses to go over capacity. */ |
| 6456 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output_buffer, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 6457 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6458 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6459 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6460 | |
| 6461 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6462 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6463 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6464 | PSA_DONE( ); |
Gilles Peskine | d54931c | 2018-07-17 21:06:59 +0200 | [diff] [blame] | 6465 | } |
| 6466 | /* END_CASE */ |
| 6467 | |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 6468 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6469 | void derive_key_exercise( int alg_arg, |
| 6470 | data_t *key_data, |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 6471 | data_t *input1, |
| 6472 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6473 | int derived_type_arg, |
| 6474 | int derived_bits_arg, |
| 6475 | int derived_usage_arg, |
| 6476 | int derived_alg_arg ) |
| 6477 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6478 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6479 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6480 | psa_algorithm_t alg = alg_arg; |
| 6481 | psa_key_type_t derived_type = derived_type_arg; |
| 6482 | size_t derived_bits = derived_bits_arg; |
| 6483 | psa_key_usage_t derived_usage = derived_usage_arg; |
| 6484 | psa_algorithm_t derived_alg = derived_alg_arg; |
| 6485 | size_t capacity = PSA_BITS_TO_BYTES( derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6486 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6487 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6488 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6489 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6490 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6491 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6492 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 6493 | psa_set_key_algorithm( &attributes, alg ); |
| 6494 | psa_set_key_type( &attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6495 | PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6496 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6497 | |
| 6498 | /* Derive a key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6499 | if ( mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 6500 | input1->x, input1->len, |
| 6501 | input2->x, input2->len, |
| 6502 | capacity ) ) |
Janos Follath | e60c905 | 2019-07-03 13:51:30 +0100 | [diff] [blame] | 6503 | goto exit; |
| 6504 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6505 | psa_set_key_usage_flags( &attributes, derived_usage ); |
| 6506 | psa_set_key_algorithm( &attributes, derived_alg ); |
| 6507 | psa_set_key_type( &attributes, derived_type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 6508 | psa_set_key_bits( &attributes, derived_bits ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6509 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6510 | &derived_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6511 | |
| 6512 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6513 | PSA_ASSERT( psa_get_key_attributes( derived_key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6514 | TEST_EQUAL( psa_get_key_type( &got_attributes ), derived_type ); |
| 6515 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), derived_bits ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6516 | |
| 6517 | /* Exercise the derived key. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6518 | if( ! mbedtls_test_psa_exercise_key( derived_key, derived_usage, derived_alg ) ) |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6519 | goto exit; |
| 6520 | |
| 6521 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6522 | /* |
| 6523 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6524 | * thus reset them as required. |
| 6525 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6526 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6527 | |
| 6528 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6529 | psa_destroy_key( base_key ); |
| 6530 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6531 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6532 | } |
| 6533 | /* END_CASE */ |
| 6534 | |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 6535 | /* BEGIN_CASE */ |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6536 | void derive_key_export( int alg_arg, |
| 6537 | data_t *key_data, |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 6538 | data_t *input1, |
| 6539 | data_t *input2, |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6540 | int bytes1_arg, |
| 6541 | int bytes2_arg ) |
| 6542 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6543 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6544 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6545 | psa_algorithm_t alg = alg_arg; |
| 6546 | size_t bytes1 = bytes1_arg; |
| 6547 | size_t bytes2 = bytes2_arg; |
| 6548 | size_t capacity = bytes1 + bytes2; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6549 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6550 | uint8_t *output_buffer = NULL; |
| 6551 | uint8_t *export_buffer = NULL; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6552 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6553 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6554 | size_t length; |
| 6555 | |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6556 | ASSERT_ALLOC( output_buffer, capacity ); |
| 6557 | ASSERT_ALLOC( export_buffer, capacity ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6558 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6559 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6560 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 6561 | psa_set_key_algorithm( &base_attributes, alg ); |
| 6562 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6563 | 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] | 6564 | &base_key ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6565 | |
| 6566 | /* Derive some material and output it. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6567 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 6568 | input1->x, input1->len, |
| 6569 | input2->x, input2->len, |
| 6570 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 6571 | goto exit; |
| 6572 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6573 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6574 | output_buffer, |
| 6575 | capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6576 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6577 | |
| 6578 | /* 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] | 6579 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 6580 | input1->x, input1->len, |
| 6581 | input2->x, input2->len, |
| 6582 | capacity ) ) |
Janos Follath | 42fd888 | 2019-07-03 14:17:09 +0100 | [diff] [blame] | 6583 | goto exit; |
| 6584 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6585 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 6586 | psa_set_key_algorithm( &derived_attributes, 0 ); |
| 6587 | psa_set_key_type( &derived_attributes, PSA_KEY_TYPE_RAW_DATA ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 6588 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes1 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6589 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6590 | &derived_key ) ); |
| 6591 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6592 | export_buffer, bytes1, |
| 6593 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6594 | TEST_EQUAL( length, bytes1 ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6595 | PSA_ASSERT( psa_destroy_key( derived_key ) ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 6596 | psa_set_key_bits( &derived_attributes, PSA_BYTES_TO_BITS( bytes2 ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6597 | PSA_ASSERT( psa_key_derivation_output_key( &derived_attributes, &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6598 | &derived_key ) ); |
| 6599 | PSA_ASSERT( psa_export_key( derived_key, |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6600 | export_buffer + bytes1, bytes2, |
| 6601 | &length ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6602 | TEST_EQUAL( length, bytes2 ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6603 | |
| 6604 | /* Compare the outputs from the two runs. */ |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 6605 | ASSERT_COMPARE( output_buffer, bytes1 + bytes2, |
| 6606 | export_buffer, capacity ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6607 | |
| 6608 | exit: |
| 6609 | mbedtls_free( output_buffer ); |
| 6610 | mbedtls_free( export_buffer ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6611 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6612 | psa_destroy_key( base_key ); |
| 6613 | psa_destroy_key( derived_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6614 | PSA_DONE( ); |
Gilles Peskine | 0386fba | 2018-07-12 17:29:22 +0200 | [diff] [blame] | 6615 | } |
| 6616 | /* END_CASE */ |
| 6617 | |
| 6618 | /* BEGIN_CASE */ |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 6619 | void derive_key( int alg_arg, |
| 6620 | data_t *key_data, data_t *input1, data_t *input2, |
| 6621 | int type_arg, int bits_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 6622 | int expected_status_arg, |
| 6623 | int is_large_output ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6624 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6625 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
| 6626 | mbedtls_svc_key_id_t derived_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6627 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 6628 | psa_key_type_t type = type_arg; |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6629 | size_t bits = bits_arg; |
| 6630 | psa_status_t expected_status = expected_status_arg; |
| 6631 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
| 6632 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6633 | psa_key_attributes_t derived_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6634 | |
| 6635 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6636 | |
| 6637 | psa_set_key_usage_flags( &base_attributes, PSA_KEY_USAGE_DERIVE ); |
| 6638 | psa_set_key_algorithm( &base_attributes, alg ); |
| 6639 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
| 6640 | 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] | 6641 | &base_key ) ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6642 | |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6643 | if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, base_key, alg, |
| 6644 | input1->x, input1->len, |
| 6645 | input2->x, input2->len, |
| 6646 | SIZE_MAX ) ) |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6647 | goto exit; |
| 6648 | |
| 6649 | psa_set_key_usage_flags( &derived_attributes, PSA_KEY_USAGE_EXPORT ); |
| 6650 | psa_set_key_algorithm( &derived_attributes, 0 ); |
Gilles Peskine | 7c227ae | 2019-07-31 15:14:44 +0200 | [diff] [blame] | 6651 | psa_set_key_type( &derived_attributes, type ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6652 | psa_set_key_bits( &derived_attributes, bits ); |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 6653 | |
| 6654 | psa_status_t status = |
| 6655 | psa_key_derivation_output_key( &derived_attributes, |
| 6656 | &operation, |
| 6657 | &derived_key ); |
| 6658 | if( is_large_output > 0 ) |
| 6659 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 6660 | TEST_EQUAL( status, expected_status ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6661 | |
| 6662 | exit: |
| 6663 | psa_key_derivation_abort( &operation ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6664 | psa_destroy_key( base_key ); |
| 6665 | psa_destroy_key( derived_key ); |
Gilles Peskine | c744d99 | 2019-07-30 17:26:54 +0200 | [diff] [blame] | 6666 | PSA_DONE( ); |
| 6667 | } |
| 6668 | /* END_CASE */ |
| 6669 | |
| 6670 | /* BEGIN_CASE */ |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6671 | void key_agreement_setup( int alg_arg, |
Steven Cooreman | ce48e85 | 2020-10-05 16:02:45 +0200 | [diff] [blame] | 6672 | int our_key_type_arg, int our_key_alg_arg, |
| 6673 | data_t *our_key_data, data_t *peer_key_data, |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6674 | int expected_status_arg ) |
| 6675 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6676 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6677 | psa_algorithm_t alg = alg_arg; |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6678 | psa_algorithm_t our_key_alg = our_key_alg_arg; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6679 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6680 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6681 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 6682 | psa_status_t expected_status = expected_status_arg; |
| 6683 | psa_status_t status; |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6684 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6685 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6686 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6687 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
Steven Cooreman | fa5e631 | 2020-10-15 17:07:12 +0200 | [diff] [blame] | 6688 | psa_set_key_algorithm( &attributes, our_key_alg ); |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6689 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6690 | PSA_ASSERT( psa_import_key( &attributes, |
| 6691 | our_key_data->x, our_key_data->len, |
| 6692 | &our_key ) ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6693 | |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 6694 | /* The tests currently include inputs that should fail at either step. |
| 6695 | * Test cases that fail at the setup step should be changed to call |
| 6696 | * key_derivation_setup instead, and this function should be renamed |
| 6697 | * to key_agreement_fail. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6698 | status = psa_key_derivation_setup( &operation, alg ); |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 6699 | if( status == PSA_SUCCESS ) |
| 6700 | { |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6701 | TEST_EQUAL( psa_key_derivation_key_agreement( |
| 6702 | &operation, PSA_KEY_DERIVATION_INPUT_SECRET, |
| 6703 | our_key, |
| 6704 | peer_key_data->x, peer_key_data->len ), |
Gilles Peskine | 77f40d8 | 2019-04-11 21:27:06 +0200 | [diff] [blame] | 6705 | expected_status ); |
| 6706 | } |
| 6707 | else |
| 6708 | { |
| 6709 | TEST_ASSERT( status == expected_status ); |
| 6710 | } |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6711 | |
| 6712 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6713 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6714 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6715 | PSA_DONE( ); |
Gilles Peskine | 01d718c | 2018-09-18 12:01:02 +0200 | [diff] [blame] | 6716 | } |
| 6717 | /* END_CASE */ |
| 6718 | |
| 6719 | /* BEGIN_CASE */ |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 6720 | void raw_key_agreement( int alg_arg, |
| 6721 | int our_key_type_arg, data_t *our_key_data, |
| 6722 | data_t *peer_key_data, |
| 6723 | data_t *expected_output ) |
| 6724 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6725 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 6726 | psa_algorithm_t alg = alg_arg; |
| 6727 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6728 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 6729 | unsigned char *output = NULL; |
| 6730 | size_t output_length = ~0; |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6731 | size_t key_bits; |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 6732 | |
| 6733 | ASSERT_ALLOC( output, expected_output->len ); |
| 6734 | PSA_ASSERT( psa_crypto_init( ) ); |
| 6735 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6736 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 6737 | psa_set_key_algorithm( &attributes, alg ); |
| 6738 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6739 | PSA_ASSERT( psa_import_key( &attributes, |
| 6740 | our_key_data->x, our_key_data->len, |
| 6741 | &our_key ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 6742 | |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6743 | PSA_ASSERT( psa_get_key_attributes( our_key, &attributes ) ); |
| 6744 | key_bits = psa_get_key_bits( &attributes ); |
| 6745 | |
Gilles Peskine | be697d8 | 2019-05-16 18:00:41 +0200 | [diff] [blame] | 6746 | PSA_ASSERT( psa_raw_key_agreement( alg, our_key, |
| 6747 | peer_key_data->x, peer_key_data->len, |
| 6748 | output, expected_output->len, |
| 6749 | &output_length ) ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 6750 | ASSERT_COMPARE( output, output_length, |
| 6751 | expected_output->x, expected_output->len ); |
gabor-mezei-arm | ceface2 | 2021-01-21 12:26:17 +0100 | [diff] [blame] | 6752 | TEST_ASSERT( output_length <= |
| 6753 | PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( our_key_type, key_bits ) ); |
| 6754 | TEST_ASSERT( output_length <= |
| 6755 | PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 6756 | |
| 6757 | exit: |
| 6758 | mbedtls_free( output ); |
| 6759 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6760 | PSA_DONE( ); |
Gilles Peskine | f0cba73 | 2019-04-11 22:12:38 +0200 | [diff] [blame] | 6761 | } |
| 6762 | /* END_CASE */ |
| 6763 | |
| 6764 | /* BEGIN_CASE */ |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6765 | void key_agreement_capacity( int alg_arg, |
| 6766 | int our_key_type_arg, data_t *our_key_data, |
| 6767 | data_t *peer_key_data, |
| 6768 | int expected_capacity_arg ) |
| 6769 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6770 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6771 | psa_algorithm_t alg = alg_arg; |
| 6772 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6773 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6774 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6775 | size_t actual_capacity; |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 6776 | unsigned char output[16]; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6777 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6778 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6779 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6780 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 6781 | psa_set_key_algorithm( &attributes, alg ); |
| 6782 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6783 | PSA_ASSERT( psa_import_key( &attributes, |
| 6784 | our_key_data->x, our_key_data->len, |
| 6785 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6786 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6787 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6788 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 6789 | &operation, |
| 6790 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 6791 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 6792 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 6793 | { |
| 6794 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6795 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6796 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 6797 | NULL, 0 ) ); |
| 6798 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6799 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 6800 | /* Test the advertized capacity. */ |
Gilles Peskine | a99d3fb | 2019-05-16 15:28:51 +0200 | [diff] [blame] | 6801 | PSA_ASSERT( psa_key_derivation_get_capacity( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6802 | &operation, &actual_capacity ) ); |
Gilles Peskine | fe11b72 | 2018-12-18 00:24:04 +0100 | [diff] [blame] | 6803 | TEST_EQUAL( actual_capacity, (size_t) expected_capacity_arg ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6804 | |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 6805 | /* Test the actual capacity by reading the output. */ |
| 6806 | while( actual_capacity > sizeof( output ) ) |
| 6807 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6808 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6809 | output, sizeof( output ) ) ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 6810 | actual_capacity -= sizeof( output ); |
| 6811 | } |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6812 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6813 | output, actual_capacity ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6814 | TEST_EQUAL( psa_key_derivation_output_bytes( &operation, output, 1 ), |
David Saada | b4ecc27 | 2019-02-14 13:48:10 +0200 | [diff] [blame] | 6815 | PSA_ERROR_INSUFFICIENT_DATA ); |
Gilles Peskine | bf49197 | 2018-10-25 22:36:12 +0200 | [diff] [blame] | 6816 | |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6817 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6818 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6819 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6820 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6821 | } |
| 6822 | /* END_CASE */ |
| 6823 | |
| 6824 | /* BEGIN_CASE */ |
| 6825 | void key_agreement_output( int alg_arg, |
| 6826 | int our_key_type_arg, data_t *our_key_data, |
| 6827 | data_t *peer_key_data, |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 6828 | data_t *expected_output1, data_t *expected_output2 ) |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6829 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6830 | mbedtls_svc_key_id_t our_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6831 | psa_algorithm_t alg = alg_arg; |
| 6832 | psa_key_type_t our_key_type = our_key_type_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6833 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6834 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 6835 | uint8_t *actual_output = NULL; |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6836 | |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 6837 | ASSERT_ALLOC( actual_output, MAX( expected_output1->len, |
| 6838 | expected_output2->len ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6839 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6840 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6841 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6842 | psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_DERIVE ); |
| 6843 | psa_set_key_algorithm( &attributes, alg ); |
| 6844 | psa_set_key_type( &attributes, our_key_type ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 6845 | PSA_ASSERT( psa_import_key( &attributes, |
| 6846 | our_key_data->x, our_key_data->len, |
| 6847 | &our_key ) ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6848 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6849 | PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6850 | PSA_ASSERT( psa_key_derivation_key_agreement( |
| 6851 | &operation, |
| 6852 | PSA_KEY_DERIVATION_INPUT_SECRET, our_key, |
| 6853 | peer_key_data->x, peer_key_data->len ) ); |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 6854 | if( PSA_ALG_IS_HKDF( PSA_ALG_KEY_AGREEMENT_GET_KDF( alg ) ) ) |
| 6855 | { |
| 6856 | /* The test data is for info="" */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6857 | PSA_ASSERT( psa_key_derivation_input_bytes( &operation, |
Gilles Peskine | 03410b5 | 2019-05-16 16:05:19 +0200 | [diff] [blame] | 6858 | PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | f8a9d94 | 2019-04-11 22:13:20 +0200 | [diff] [blame] | 6859 | NULL, 0 ) ); |
| 6860 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6861 | |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6862 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6863 | actual_output, |
| 6864 | expected_output1->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 6865 | ASSERT_COMPARE( actual_output, expected_output1->len, |
| 6866 | expected_output1->x, expected_output1->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 6867 | if( expected_output2->len != 0 ) |
| 6868 | { |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6869 | PSA_ASSERT( psa_key_derivation_output_bytes( &operation, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 6870 | actual_output, |
| 6871 | expected_output2->len ) ); |
Gilles Peskine | 0dfba2d | 2018-12-18 00:40:50 +0100 | [diff] [blame] | 6872 | ASSERT_COMPARE( actual_output, expected_output2->len, |
| 6873 | expected_output2->x, expected_output2->len ); |
Gilles Peskine | 3ec8ed8 | 2018-10-25 22:37:15 +0200 | [diff] [blame] | 6874 | } |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6875 | |
| 6876 | exit: |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 6877 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6878 | psa_destroy_key( our_key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6879 | PSA_DONE( ); |
Gilles Peskine | 5968559 | 2018-09-18 12:11:34 +0200 | [diff] [blame] | 6880 | mbedtls_free( actual_output ); |
| 6881 | } |
| 6882 | /* END_CASE */ |
| 6883 | |
| 6884 | /* BEGIN_CASE */ |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6885 | void generate_random( int bytes_arg ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6886 | { |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6887 | size_t bytes = bytes_arg; |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6888 | unsigned char *output = NULL; |
| 6889 | unsigned char *changed = NULL; |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6890 | size_t i; |
| 6891 | unsigned run; |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6892 | |
Simon Butcher | 49f8e31 | 2020-03-03 15:51:50 +0000 | [diff] [blame] | 6893 | TEST_ASSERT( bytes_arg >= 0 ); |
| 6894 | |
Gilles Peskine | 9189202 | 2021-02-08 19:50:26 +0100 | [diff] [blame] | 6895 | ASSERT_ALLOC( output, bytes ); |
Gilles Peskine | 8cebbba | 2018-09-27 13:54:18 +0200 | [diff] [blame] | 6896 | ASSERT_ALLOC( changed, bytes ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6897 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6898 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6899 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6900 | /* Run several times, to ensure that every output byte will be |
| 6901 | * nonzero at least once with overwhelming probability |
| 6902 | * (2^(-8*number_of_runs)). */ |
| 6903 | for( run = 0; run < 10; run++ ) |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6904 | { |
Gilles Peskine | f7ab5ad | 2018-09-26 18:19:24 +0200 | [diff] [blame] | 6905 | if( bytes != 0 ) |
| 6906 | memset( output, 0, bytes ); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6907 | PSA_ASSERT( psa_generate_random( output, bytes ) ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6908 | |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6909 | for( i = 0; i < bytes; i++ ) |
| 6910 | { |
| 6911 | if( output[i] != 0 ) |
| 6912 | ++changed[i]; |
| 6913 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6914 | } |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6915 | |
| 6916 | /* Check that every byte was changed to nonzero at least once. This |
| 6917 | * validates that psa_generate_random is overwriting every byte of |
| 6918 | * the output buffer. */ |
| 6919 | for( i = 0; i < bytes; i++ ) |
| 6920 | { |
| 6921 | TEST_ASSERT( changed[i] != 0 ); |
| 6922 | } |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6923 | |
| 6924 | exit: |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6925 | PSA_DONE( ); |
Gilles Peskine | a50d739 | 2018-06-21 10:22:13 +0200 | [diff] [blame] | 6926 | mbedtls_free( output ); |
| 6927 | mbedtls_free( changed ); |
Gilles Peskine | 05d6989 | 2018-06-19 22:00:52 +0200 | [diff] [blame] | 6928 | } |
| 6929 | /* END_CASE */ |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6930 | |
| 6931 | /* BEGIN_CASE */ |
| 6932 | void generate_key( int type_arg, |
| 6933 | int bits_arg, |
| 6934 | int usage_arg, |
| 6935 | int alg_arg, |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 6936 | int expected_status_arg, |
| 6937 | int is_large_key ) |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6938 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6939 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6940 | psa_key_type_t type = type_arg; |
| 6941 | psa_key_usage_t usage = usage_arg; |
| 6942 | size_t bits = bits_arg; |
| 6943 | psa_algorithm_t alg = alg_arg; |
| 6944 | psa_status_t expected_status = expected_status_arg; |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6945 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6946 | psa_key_attributes_t got_attributes = PSA_KEY_ATTRIBUTES_INIT; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6947 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 6948 | PSA_ASSERT( psa_crypto_init( ) ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6949 | |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6950 | psa_set_key_usage_flags( &attributes, usage ); |
| 6951 | psa_set_key_algorithm( &attributes, alg ); |
| 6952 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 6953 | psa_set_key_bits( &attributes, bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6954 | |
| 6955 | /* Generate a key */ |
Steven Cooreman | 83fdb70 | 2021-01-21 14:24:39 +0100 | [diff] [blame] | 6956 | psa_status_t status = psa_generate_key( &attributes, &key ); |
| 6957 | |
| 6958 | if( is_large_key > 0 ) |
| 6959 | TEST_ASSUME( status != PSA_ERROR_INSUFFICIENT_MEMORY ); |
| 6960 | TEST_EQUAL( status , expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6961 | if( expected_status != PSA_SUCCESS ) |
Gilles Peskine | ff5f0e7 | 2019-04-18 12:53:30 +0200 | [diff] [blame] | 6962 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6963 | |
| 6964 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6965 | PSA_ASSERT( psa_get_key_attributes( key, &got_attributes ) ); |
Gilles Peskine | 8c8f2ab | 2019-04-18 21:44:46 +0200 | [diff] [blame] | 6966 | TEST_EQUAL( psa_get_key_type( &got_attributes ), type ); |
| 6967 | TEST_EQUAL( psa_get_key_bits( &got_attributes ), bits ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6968 | |
Gilles Peskine | 818ca12 | 2018-06-20 18:16:48 +0200 | [diff] [blame] | 6969 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 6970 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | 02b7507 | 2018-07-01 22:31:34 +0200 | [diff] [blame] | 6971 | goto exit; |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6972 | |
| 6973 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6974 | /* |
| 6975 | * Key attributes may have been returned by psa_get_key_attributes() |
| 6976 | * thus reset them as required. |
| 6977 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 6978 | psa_reset_key_attributes( &got_attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 6979 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6980 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 6981 | PSA_DONE( ); |
Gilles Peskine | 12313cd | 2018-06-20 00:20:32 +0200 | [diff] [blame] | 6982 | } |
| 6983 | /* END_CASE */ |
itayzafrir | 0adf0fc | 2018-09-06 16:24:41 +0300 | [diff] [blame] | 6984 | |
Ronald Cron | ee414c7 | 2021-03-18 18:50:08 +0100 | [diff] [blame] | 6985 | /* 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] | 6986 | void generate_key_rsa( int bits_arg, |
| 6987 | data_t *e_arg, |
| 6988 | int expected_status_arg ) |
| 6989 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 6990 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | c93b80c | 2019-05-16 19:39:54 +0200 | [diff] [blame] | 6991 | psa_key_type_t type = PSA_KEY_TYPE_RSA_KEY_PAIR; |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 6992 | size_t bits = bits_arg; |
| 6993 | psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; |
| 6994 | psa_algorithm_t alg = PSA_ALG_RSA_PKCS1V15_SIGN_RAW; |
| 6995 | psa_status_t expected_status = expected_status_arg; |
| 6996 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 6997 | uint8_t *exported = NULL; |
| 6998 | size_t exported_size = |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 6999 | PSA_EXPORT_KEY_OUTPUT_SIZE( PSA_KEY_TYPE_RSA_PUBLIC_KEY, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7000 | size_t exported_length = SIZE_MAX; |
| 7001 | uint8_t *e_read_buffer = NULL; |
| 7002 | int is_default_public_exponent = 0; |
Gilles Peskine | aa02c17 | 2019-04-28 11:44:17 +0200 | [diff] [blame] | 7003 | size_t e_read_size = PSA_KEY_DOMAIN_PARAMETERS_SIZE( type, bits ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7004 | size_t e_read_length = SIZE_MAX; |
| 7005 | |
| 7006 | if( e_arg->len == 0 || |
| 7007 | ( e_arg->len == 3 && |
| 7008 | e_arg->x[0] == 1 && e_arg->x[1] == 0 && e_arg->x[2] == 1 ) ) |
| 7009 | { |
| 7010 | is_default_public_exponent = 1; |
| 7011 | e_read_size = 0; |
| 7012 | } |
| 7013 | ASSERT_ALLOC( e_read_buffer, e_read_size ); |
| 7014 | ASSERT_ALLOC( exported, exported_size ); |
| 7015 | |
| 7016 | PSA_ASSERT( psa_crypto_init( ) ); |
| 7017 | |
| 7018 | psa_set_key_usage_flags( &attributes, usage ); |
| 7019 | psa_set_key_algorithm( &attributes, alg ); |
| 7020 | PSA_ASSERT( psa_set_key_domain_parameters( &attributes, type, |
| 7021 | e_arg->x, e_arg->len ) ); |
| 7022 | psa_set_key_bits( &attributes, bits ); |
| 7023 | |
| 7024 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7025 | TEST_EQUAL( psa_generate_key( &attributes, &key ), expected_status ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7026 | if( expected_status != PSA_SUCCESS ) |
| 7027 | goto exit; |
| 7028 | |
| 7029 | /* Test the key information */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7030 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7031 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 7032 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
| 7033 | PSA_ASSERT( psa_get_key_domain_parameters( &attributes, |
| 7034 | e_read_buffer, e_read_size, |
| 7035 | &e_read_length ) ); |
| 7036 | if( is_default_public_exponent ) |
| 7037 | TEST_EQUAL( e_read_length, 0 ); |
| 7038 | else |
| 7039 | ASSERT_COMPARE( e_read_buffer, e_read_length, e_arg->x, e_arg->len ); |
| 7040 | |
| 7041 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7042 | if( ! mbedtls_test_psa_exercise_key( key, usage, alg ) ) |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7043 | goto exit; |
| 7044 | |
| 7045 | /* Export the key and check the public exponent. */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7046 | PSA_ASSERT( psa_export_public_key( key, |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7047 | exported, exported_size, |
| 7048 | &exported_length ) ); |
| 7049 | { |
| 7050 | uint8_t *p = exported; |
| 7051 | uint8_t *end = exported + exported_length; |
| 7052 | size_t len; |
| 7053 | /* RSAPublicKey ::= SEQUENCE { |
| 7054 | * modulus INTEGER, -- n |
| 7055 | * publicExponent INTEGER } -- e |
| 7056 | */ |
| 7057 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7058 | MBEDTLS_ASN1_SEQUENCE | |
| 7059 | MBEDTLS_ASN1_CONSTRUCTED ) ); |
Gilles Peskine | 8e94efe | 2021-02-13 00:25:53 +0100 | [diff] [blame] | 7060 | TEST_ASSERT( mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7061 | TEST_EQUAL( 0, mbedtls_asn1_get_tag( &p, end, &len, |
| 7062 | MBEDTLS_ASN1_INTEGER ) ); |
| 7063 | if( len >= 1 && p[0] == 0 ) |
| 7064 | { |
| 7065 | ++p; |
| 7066 | --len; |
| 7067 | } |
| 7068 | if( e_arg->len == 0 ) |
| 7069 | { |
| 7070 | TEST_EQUAL( len, 3 ); |
| 7071 | TEST_EQUAL( p[0], 1 ); |
| 7072 | TEST_EQUAL( p[1], 0 ); |
| 7073 | TEST_EQUAL( p[2], 1 ); |
| 7074 | } |
| 7075 | else |
| 7076 | ASSERT_COMPARE( p, len, e_arg->x, e_arg->len ); |
| 7077 | } |
| 7078 | |
| 7079 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7080 | /* |
| 7081 | * Key attributes may have been returned by psa_get_key_attributes() or |
| 7082 | * set by psa_set_key_domain_parameters() thus reset them as required. |
| 7083 | */ |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7084 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7085 | |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7086 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7087 | PSA_DONE( ); |
Gilles Peskine | e56e878 | 2019-04-26 17:34:02 +0200 | [diff] [blame] | 7088 | mbedtls_free( e_read_buffer ); |
| 7089 | mbedtls_free( exported ); |
| 7090 | } |
| 7091 | /* END_CASE */ |
| 7092 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7093 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */ |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7094 | void persistent_key_load_key_from_storage( data_t *data, |
| 7095 | int type_arg, int bits_arg, |
| 7096 | int usage_flags_arg, int alg_arg, |
| 7097 | int generation_method ) |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7098 | { |
Ronald Cron | 71016a9 | 2020-08-28 19:01:50 +0200 | [diff] [blame] | 7099 | 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] | 7100 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7101 | mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT; |
| 7102 | mbedtls_svc_key_id_t base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7103 | psa_key_type_t type = type_arg; |
| 7104 | size_t bits = bits_arg; |
| 7105 | psa_key_usage_t usage_flags = usage_flags_arg; |
| 7106 | psa_algorithm_t alg = alg_arg; |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7107 | psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7108 | unsigned char *first_export = NULL; |
| 7109 | unsigned char *second_export = NULL; |
gabor-mezei-arm | cbcec21 | 2020-12-18 14:23:51 +0100 | [diff] [blame] | 7110 | size_t export_size = PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7111 | size_t first_exported_length; |
| 7112 | size_t second_exported_length; |
| 7113 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7114 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 7115 | { |
| 7116 | ASSERT_ALLOC( first_export, export_size ); |
| 7117 | ASSERT_ALLOC( second_export, export_size ); |
| 7118 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7119 | |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7120 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7121 | |
Gilles Peskine | c87af66 | 2019-05-15 16:12:22 +0200 | [diff] [blame] | 7122 | psa_set_key_id( &attributes, key_id ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7123 | psa_set_key_usage_flags( &attributes, usage_flags ); |
| 7124 | psa_set_key_algorithm( &attributes, alg ); |
| 7125 | psa_set_key_type( &attributes, type ); |
Gilles Peskine | 3a4f1f8 | 2019-04-26 13:49:28 +0200 | [diff] [blame] | 7126 | psa_set_key_bits( &attributes, bits ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7127 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7128 | switch( generation_method ) |
| 7129 | { |
| 7130 | case IMPORT_KEY: |
| 7131 | /* Import the key */ |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7132 | PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7133 | &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7134 | break; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7135 | |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7136 | case GENERATE_KEY: |
| 7137 | /* Generate a key */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7138 | PSA_ASSERT( psa_generate_key( &attributes, &key ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7139 | break; |
| 7140 | |
| 7141 | case DERIVE_KEY: |
Steven Cooreman | 70f654a | 2021-02-15 10:51:43 +0100 | [diff] [blame] | 7142 | #if defined(PSA_WANT_ALG_HKDF) && defined(PSA_WANT_ALG_SHA_256) |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7143 | { |
| 7144 | /* Create base key */ |
| 7145 | psa_algorithm_t derive_alg = PSA_ALG_HKDF( PSA_ALG_SHA_256 ); |
| 7146 | psa_key_attributes_t base_attributes = PSA_KEY_ATTRIBUTES_INIT; |
| 7147 | psa_set_key_usage_flags( &base_attributes, |
| 7148 | PSA_KEY_USAGE_DERIVE ); |
| 7149 | psa_set_key_algorithm( &base_attributes, derive_alg ); |
| 7150 | psa_set_key_type( &base_attributes, PSA_KEY_TYPE_DERIVE ); |
Gilles Peskine | 049c753 | 2019-05-15 20:22:09 +0200 | [diff] [blame] | 7151 | PSA_ASSERT( psa_import_key( &base_attributes, |
| 7152 | data->x, data->len, |
| 7153 | &base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7154 | /* Derive a key. */ |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7155 | PSA_ASSERT( psa_key_derivation_setup( &operation, derive_alg ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7156 | PSA_ASSERT( psa_key_derivation_input_key( |
| 7157 | &operation, |
| 7158 | PSA_KEY_DERIVATION_INPUT_SECRET, base_key ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7159 | PSA_ASSERT( psa_key_derivation_input_bytes( |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7160 | &operation, PSA_KEY_DERIVATION_INPUT_INFO, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7161 | NULL, 0 ) ); |
Gilles Peskine | cf7292e | 2019-05-16 17:53:40 +0200 | [diff] [blame] | 7162 | PSA_ASSERT( psa_key_derivation_output_key( &attributes, |
| 7163 | &operation, |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7164 | &key ) ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7165 | PSA_ASSERT( psa_key_derivation_abort( &operation ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7166 | PSA_ASSERT( psa_destroy_key( base_key ) ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7167 | base_key = MBEDTLS_SVC_KEY_ID_INIT; |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7168 | } |
Gilles Peskine | 6fea21d | 2021-01-12 00:02:15 +0100 | [diff] [blame] | 7169 | #else |
| 7170 | TEST_ASSUME( ! "KDF not supported in this configuration" ); |
| 7171 | #endif |
| 7172 | break; |
| 7173 | |
| 7174 | default: |
| 7175 | TEST_ASSERT( ! "generation_method not implemented in test" ); |
| 7176 | break; |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7177 | } |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7178 | psa_reset_key_attributes( &attributes ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7179 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7180 | /* Export the key if permitted by the key policy. */ |
| 7181 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
| 7182 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7183 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7184 | first_export, export_size, |
| 7185 | &first_exported_length ) ); |
| 7186 | if( generation_method == IMPORT_KEY ) |
| 7187 | ASSERT_COMPARE( data->x, data->len, |
| 7188 | first_export, first_exported_length ); |
| 7189 | } |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7190 | |
| 7191 | /* Shutdown and restart */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7192 | PSA_ASSERT( psa_purge_key( key ) ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7193 | PSA_DONE(); |
Gilles Peskine | 8817f61 | 2018-12-18 00:18:46 +0100 | [diff] [blame] | 7194 | PSA_ASSERT( psa_crypto_init() ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7195 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7196 | /* Check key slot still contains key data */ |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7197 | PSA_ASSERT( psa_get_key_attributes( key, &attributes ) ); |
Ronald Cron | ecfb237 | 2020-07-23 17:13:42 +0200 | [diff] [blame] | 7198 | TEST_ASSERT( mbedtls_svc_key_id_equal( |
| 7199 | psa_get_key_id( &attributes ), key_id ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7200 | TEST_EQUAL( psa_get_key_lifetime( &attributes ), |
| 7201 | PSA_KEY_LIFETIME_PERSISTENT ); |
| 7202 | TEST_EQUAL( psa_get_key_type( &attributes ), type ); |
| 7203 | TEST_EQUAL( psa_get_key_bits( &attributes ), bits ); |
gabor-mezei-arm | 4ff7303 | 2021-05-13 12:05:01 +0200 | [diff] [blame] | 7204 | TEST_EQUAL( psa_get_key_usage_flags( &attributes ), |
gabor-mezei-arm | 98a3435 | 2021-06-28 14:05:00 +0200 | [diff] [blame] | 7205 | mbedtls_test_update_key_usage_flags( usage_flags ) ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7206 | TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg ); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7207 | |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7208 | /* Export the key again if permitted by the key policy. */ |
| 7209 | if( usage_flags & PSA_KEY_USAGE_EXPORT ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7210 | { |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7211 | PSA_ASSERT( psa_export_key( key, |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7212 | second_export, export_size, |
| 7213 | &second_exported_length ) ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7214 | ASSERT_COMPARE( first_export, first_exported_length, |
| 7215 | second_export, second_exported_length ); |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7216 | } |
| 7217 | |
| 7218 | /* Do something with the key according to its type and permitted usage. */ |
Gilles Peskine | c18e25f | 2021-02-12 23:48:20 +0100 | [diff] [blame] | 7219 | if( ! mbedtls_test_psa_exercise_key( key, usage_flags, alg ) ) |
Darryl Green | 0c6575a | 2018-11-07 16:05:30 +0000 | [diff] [blame] | 7220 | goto exit; |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7221 | |
| 7222 | exit: |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7223 | /* |
| 7224 | * Key attributes may have been returned by psa_get_key_attributes() |
| 7225 | * thus reset them as required. |
| 7226 | */ |
Gilles Peskine | a1ace9c | 2019-04-26 16:03:33 +0200 | [diff] [blame] | 7227 | psa_reset_key_attributes( &attributes ); |
Ronald Cron | 3a4f0e3 | 2020-11-19 17:55:23 +0100 | [diff] [blame] | 7228 | |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7229 | mbedtls_free( first_export ); |
| 7230 | mbedtls_free( second_export ); |
Gilles Peskine | 51ae0e4 | 2019-05-16 17:31:03 +0200 | [diff] [blame] | 7231 | psa_key_derivation_abort( &operation ); |
Gilles Peskine | 5c648ab | 2019-04-19 14:06:53 +0200 | [diff] [blame] | 7232 | psa_destroy_key( base_key ); |
Ronald Cron | 5425a21 | 2020-08-04 14:58:35 +0200 | [diff] [blame] | 7233 | psa_destroy_key( key ); |
Gilles Peskine | 1153e7b | 2019-05-28 15:10:21 +0200 | [diff] [blame] | 7234 | PSA_DONE(); |
Darryl Green | d49a499 | 2018-06-18 17:27:26 +0100 | [diff] [blame] | 7235 | } |
| 7236 | /* END_CASE */ |